summaryrefslogtreecommitdiff
path: root/1415/fp2/week45/mart/RefactorX.icl
diff options
context:
space:
mode:
authorCamil Staps2016-02-02 19:24:50 +0100
committerCamil Staps2016-02-02 19:24:50 +0100
commita7d7542dc646a5fd124ef71e71ce260889f1701b (patch)
tree04ed89503bbb3cc9933273a1326a53ca724c3492 /1415/fp2/week45/mart/RefactorX.icl
parentweek6 camil: working positioning of lines by putting empties at left and righ... (diff)
Moved to 1415 directoryHEADmaster
Diffstat (limited to '1415/fp2/week45/mart/RefactorX.icl')
-rw-r--r--1415/fp2/week45/mart/RefactorX.icl72
1 files changed, 72 insertions, 0 deletions
diff --git a/1415/fp2/week45/mart/RefactorX.icl b/1415/fp2/week45/mart/RefactorX.icl
new file mode 100644
index 0000000..c74df08
--- /dev/null
+++ b/1415/fp2/week45/mart/RefactorX.icl
@@ -0,0 +1,72 @@
+// Mart Lubbers s4109503, Camil Staps s4498062
+
+implementation module RefactorX
+
+import StdEnv
+
+//Start = map toString [E1,E2,E3,E4,E5]
+//Start = map free [E1,E2,E3,E4,E5]
+//Start = map toString (map remove_unused_lets [E1,E2,E3,E4,E5])
+Start = map eval [E1,E2,E3,E4,E5]
+ where
+ E1 = OP (LET "x" (OP (NR 42) MIN (NR 3)) (OP (VAR "x") DIV (NR 0))) PLUS (LET "y" (NR 6) (OP (VAR "y") MUL (VAR "y")))
+ E2 = LET "x" (NR 42) (OP (VAR "x") PLUS (LET "x" (NR 58) (VAR "x")))
+ E3 = LET "x" (NR 1) (LET "y" (NR 2) (LET "x" (NR 3) (NR 4)))
+ E4 = LET "x" (NR 1) (OP (VAR "x") PLUS (VAR "y"))
+ E5 = OP (LET "x" (NR 1) (VAR "x")) MUL (VAR "x")
+
+(<+) infixl 9 :: String a -> String | toString a
+(<+) str a = str +++ toString a
+
+instance toString Operator where
+ toString PLUS = "+"
+ toString MIN = "-"
+ toString MUL = "*"
+ toString DIV = "/"
+
+instance toString Expr where
+ toString (NR n) = toString n
+ toString (VAR v) = v
+ toString (LET n e1 e2) = "(let " <+ n <+ "=" <+ e1 <+ " in " <+ e2 <+ ")"
+ toString (OP e1 o e2) = bracket e1 <+ o <+ bracket e2
+ where
+ bracket :: Expr -> String
+ bracket (OP e1 o e2) = "(" <+ (OP e1 o e2) <+ ")"
+ bracket e = toString e
+
+free:: Expr -> [Name]
+free (NR n) = []
+free (VAR v) = [v]
+free (OP e1 o e2) = removeDup (free e1 ++ free e2)
+free (LET n e1 e2) = removeMember n (free e2)
+
+remove_unused_lets:: Expr -> Expr
+remove_unused_lets (LET n e1 e2)
+| isMember n (free e2) = (LET n (remove_unused_lets e1) (remove_unused_lets e2))
+| otherwise = remove_unused_lets e2
+remove_unused_lets (OP e1 o e2) = (OP (remove_unused_lets e1) o (remove_unused_lets e2))
+remove_unused_lets e = e
+
+apply:: Operator Val Val -> Val
+apply _ Undef _ = Undef
+apply _ _ Undef = Undef
+apply DIV _ (Result 0) = Undef
+apply o (Result e1) (Result e2) = Result (apply` o e1 e2)
+ where
+ apply`:: Operator -> Int Int -> Int
+ apply` PLUS = +
+ apply` MIN = -
+ apply` MUL = *
+ apply` DIV = /
+
+eval:: Expr -> Val
+eval e = eval` e []
+ where
+ eval`:: Expr [(Name, Val)] -> Val
+ eval` (NR n) _ = Result n
+ eval` (VAR v) [] = Undef
+ eval` (VAR v) [(n, e):xs]
+ | v == n = e
+ | otherwise = eval` (VAR v) xs
+ eval` (OP e1 o e2) xs = apply o (eval` e1 xs) (eval` e2 xs)
+ eval` (LET n e1 e2) xs = eval` e2 [(n, eval` e1 xs):xs]