From: Francis Russell Date: Fri, 12 Apr 2013 19:35:18 +0000 (+0100) Subject: Minor code clean-ups. X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=018bce624405fb81c2c3f305f77dfe87b2e1c67e;p=francis%2Flta.git Minor code clean-ups. --- diff --git a/LTA/Symbolic.hs b/LTA/Symbolic.hs index c0e04d0..d05f5f6 100644 --- a/LTA/Symbolic.hs +++ b/LTA/Symbolic.hs @@ -71,7 +71,7 @@ instance PairSeqLike (PairSeq ProductTag Expr) where let localMultiplier = multiplier * coeff in case (expr, localMultiplier) of (Product childSeq, _) -> mergeTerms oldSeq localMultiplier childSeq - (a, b) -> case evalPow a b of + (a, b) -> case a `evalPow` b of Just n -> transformOverall oldSeq (* n) Nothing -> addPair oldSeq (a, b) @@ -129,7 +129,7 @@ normalise :: (PairSeqLike (PairSeq t c), Eq c, Num c) => PairSeq t c -> PairSeq normalise = removeZeros . flatten . extractMultipliers isZero :: (Num e, Eq e) => e -> Bool -isZero n = n == 0 +isZero = (== 0) extractMultiplier :: Expr -> (Expr, Literal) extractMultiplier (Literal literal) = (1, literal) @@ -143,7 +143,7 @@ extractMultiplier (Sum (PairSeq coeff terms)) = extractMultiplier e = (e, 1) pow :: Expr -> Expr -> Expr -pow a b = simplify $ Product $ PairSeq 1 (Map.singleton a b) +pow a b = simplify . Product $ empty `addPair` (a, b) evalPow :: Expr -> Expr -> Maybe Literal evalPow (Literal a) (Literal b) = evalPow' a b where @@ -179,17 +179,17 @@ instance Num Literal where instance Num Expr where (+) n 0 = n (+) 0 n = n - (+) a b = simplify $ Sum $ empty `addPair` (a, 1) `addPair` (b, 1) + (+) a b = simplify . Sum $ empty `addPair` (a, 1) `addPair` (b, 1) (*) _ 0 = 0 (*) 0 _ = 0 (*) 1 n = n (*) n 1 = n - (*) a b = simplify $ Product $ empty `addPair` (a, 1) `addPair` (b, 1) + (*) a b = simplify . Product $ empty `addPair` (a, 1) `addPair` (b, 1) abs = UnaryFunction Abs fromInteger = Literal . fromInteger - negate a = simplify $ Sum $ empty `addPair` (a, -1) + negate a = simplify . Sum $ empty `addPair` (a, -1) signum = UnaryFunction Signum instance Fractional Expr where - (/) a b = simplify $ Product $ empty `addPair` (a, 1) `addPair` (b, -1) + (/) a b = simplify . Product $ empty `addPair` (a, 1) `addPair` (b, -1) fromRational = Literal . RationalLiteral