Skip to content

Commit

Permalink
Removed SetEq and moved two functions around
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPietzschmann committed Jul 17, 2024
1 parent c038ddb commit cac275d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
11 changes: 9 additions & 2 deletions src/Math/Haskellator/Internal/AstProcessingSteps/Evaluate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ execVal :: Value Dimension -> SimpleAstFold (Value Dimension)
execVal = return

execBinOp :: Value Dimension -> Op -> Value Dimension -> SimpleAstFold (Value Dimension)
execBinOp lhs Plus rhs | unit lhs =~= unit rhs = return $ combineValues (+) lhs rhs
execBinOp lhs Plus rhs | unit lhs == unit rhs = return $ combineValues (+) lhs rhs
| otherwise = throwError $ Error RuntimeError $ "Cannot add units " ++ show (unit lhs) ++ " and " ++ show (unit rhs)
execBinOp lhs Minus rhs | unit lhs =~= unit rhs = return $ combineValues (-) lhs rhs
execBinOp lhs Minus rhs | unit lhs == unit rhs = return $ combineValues (-) lhs rhs
| otherwise = throwError $ Error RuntimeError $ "Cannot subtract units " ++ show (unit lhs) ++ " and " ++ show (unit rhs)
execBinOp lhs Mult rhs = do
let u = mergeUnits (unit lhs) (unit rhs)
Expand Down Expand Up @@ -107,3 +107,10 @@ findPair x (y:ys) | dimUnit x == dimUnit y = ([(x, y)], ([], ys))

filterZeroPower :: Dimension -> Dimension
filterZeroPower = filter ((/=0) . power)

mapValue :: (Double -> Double) -> Value u -> Value u
mapValue f (Value v u) = Value (f v) u

combineValues :: Eq u => (Double -> Double -> Double) -> Value u -> Value u -> Value u
combineValues f (Value v1 u1) (Value v2 u2) | u1 == u2 = Value (v1 `f` v2) u1
| otherwise = error "Cannot map values with different units"
16 changes: 3 additions & 13 deletions src/Math/Haskellator/Internal/Units.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,12 @@ instance Eq UnitExp where
(UnitExp Multiplier _) == (UnitExp Multiplier _) = True
(UnitExp u1 e1) == (UnitExp u2 e2) = u1 == u2 && e1 == e2

mapValue :: (Double -> Double) -> Value u -> Value u
mapValue f (Value v u) = Value (f v) u

combineValues :: SetEq u => (Double -> Double -> Double) -> Value u -> Value u -> Value u
combineValues f (Value v1 u1) (Value v2 u2) | u1 =~= u2 = Value (v1 `f` v2) u1
| otherwise = error "Cannot map values with different units"

class SetEq a where
(=~=) :: a -> a -> Bool

-- | A dimension is a list of exponentiated units
type Dimension = [UnitExp]

instance {-# OVERLAPPING #-} Eq Dimension where
a == b = all (`elem` b) a && all (`elem` a) b

instance {-# OVERLAPPING #-} Show Dimension where
show xs = write $ divide xs ([],[])
where
Expand All @@ -98,6 +91,3 @@ instance {-# OVERLAPPING #-} Show Dimension where
divide::Dimension -> (Dimension,Dimension) -> (Dimension, Dimension)
divide (uExp@(UnitExp _ e):xs) (pos,neg) = if e<0 then divide xs (pos,neg ++ [uExp]) else divide xs (pos ++ [uExp], neg)
divide [] (pos,neg) = (pos,neg)

instance SetEq Dimension where
(=~=) a b = all (`elem` b) a && all (`elem` a) b

0 comments on commit cac275d

Please sign in to comment.