Move Num instance around a little.

This commit is contained in:
Danila Fedorin 2020-09-26 15:06:37 -07:00
parent 02387bbf85
commit 3f673113b7

View File

@ -16,11 +16,6 @@ data Expr
| Mul Expr Expr -- ^ Multiplication expressions | Mul Expr Expr -- ^ Multiplication expressions
deriving (Eq,Show) deriving (Eq,Show)
instance Num Expr where
(+) = Add
(*) = Mul
fromInteger = Lit . fromInteger
fold :: (Int -> a) -> (a -> a -> a) -> (a -> a -> a) -> Expr -> a fold :: (Int -> a) -> (a -> a -> a) -> (a -> a -> a) -> Expr -> a
fold f1 f2 f3 = rec fold f1 f2 f3 = rec
where where
@ -28,6 +23,11 @@ fold f1 f2 f3 = rec
rec (Add l r) = f2 (rec l) (rec r) rec (Add l r) = f2 (rec l) (rec r)
rec (Mul l r) = f3 (rec l) (rec r) rec (Mul l r) = f3 (rec l) (rec r)
instance Num Expr where
(+) = Add
(*) = Mul
fromInteger = Lit . fromInteger
-- | The expression: 2 + 3 * 4 -- | The expression: 2 + 3 * 4
e1 :: Expr e1 :: Expr
e1 = Add (Lit 2) (Mul (Lit 3) (Lit 4)) e1 = Add (Lit 2) (Mul (Lit 3) (Lit 4))