-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algebra.hs
38 lines (26 loc) · 913 Bytes
/
Algebra.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE LambdaCase #-}
module Algebra where
import Expressions
disNeg :: Expr -> Expr
disNeg = descend $ \case
ExSub a b -> ExAdd a (ExMult (ExNum (-1)) b)
a -> a
distribute :: Expr -> Expr
distribute = descend $ \case
ExMult a (ExAdd b c) -> ExAdd (ExMult a b) (ExMult a c)
ExMult a (ExSub b c) -> ExSub (ExMult a b) (ExMult a c)
a -> a
gcf :: Expr -> Expr
gcf = descend $ \case
ExAdd (ExMult a c) (ExMult b d) | a == b -> ExMult a (ExAdd c d)
ExSub (ExMult a c) (ExMult b d) | a == b -> ExMult a (ExSub c d)
a -> a
arithmatic :: Expr -> Expr
arithmatic = descend $ \case
ExAdd (ExNum a) (ExNum b) -> ExNum (a + b)
ExSub (ExNum a) (ExNum b) -> ExNum (a - b)
ExMult (ExNum a) (ExNum b) -> ExNum (a * b)
ExFrac (ExNum a) (ExNum b) -> ExNum (a / b)
a -> a
expand :: Expr -> Expr
expand = distribute . disNeg