From cac275d0ec7fa575bdff79fbea09b5a11d379b4d Mon Sep 17 00:00:00 2001 From: Lukas Pietzschmann Date: Wed, 17 Jul 2024 15:30:11 +0200 Subject: [PATCH] Removed SetEq and moved two functions around --- .../Internal/AstProcessingSteps/Evaluate.hs | 11 +++++++++-- src/Math/Haskellator/Internal/Units.hs | 16 +++------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Math/Haskellator/Internal/AstProcessingSteps/Evaluate.hs b/src/Math/Haskellator/Internal/AstProcessingSteps/Evaluate.hs index c904089..fe84820 100644 --- a/src/Math/Haskellator/Internal/AstProcessingSteps/Evaluate.hs +++ b/src/Math/Haskellator/Internal/AstProcessingSteps/Evaluate.hs @@ -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) @@ -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" diff --git a/src/Math/Haskellator/Internal/Units.hs b/src/Math/Haskellator/Internal/Units.hs index a75c767..23b4ea9 100644 --- a/src/Math/Haskellator/Internal/Units.hs +++ b/src/Math/Haskellator/Internal/Units.hs @@ -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 @@ -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