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)
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)
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
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