summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMart Lubbers2015-05-12 15:55:58 +0200
committerMart Lubbers2015-05-12 15:55:58 +0200
commitdbc700ae318715312242f04795b485ff9370d4ac (patch)
tree321314b726b65b22966396166da18c5153b31a8f
parentWeek 3: student numbers; tarball (diff)
week45 mandatory part finished
-rw-r--r--fp2/week3/mart/StdDynSet.icl5
-rwxr-xr-xfp2/week45/mart/RefactorXbin0 -> 112368 bytes
-rw-r--r--fp2/week45/mart/RefactorX.dcl15
-rw-r--r--fp2/week45/mart/RefactorX.icl71
4 files changed, 90 insertions, 1 deletions
diff --git a/fp2/week3/mart/StdDynSet.icl b/fp2/week3/mart/StdDynSet.icl
index ffc2590..1202ce2 100644
--- a/fp2/week3/mart/StdDynSet.icl
+++ b/fp2/week3/mart/StdDynSet.icl
@@ -14,7 +14,10 @@ instance toString Set
where toString (Set a) = abort "toString not implemented"
instance == Set
-where == a b = abort "== instance voor Set nog niet geimplementeerd.\n"
+where
+ (==) (Set []) (Set []) = True
+ (==) (Set []) _ = False
+ (==) _ (Set []) = False
toSet :: a -> Set | Set a
toSet a = Set [dynamic a]
diff --git a/fp2/week45/mart/RefactorX b/fp2/week45/mart/RefactorX
new file mode 100755
index 0000000..a700eaa
--- /dev/null
+++ b/fp2/week45/mart/RefactorX
Binary files differ
diff --git a/fp2/week45/mart/RefactorX.dcl b/fp2/week45/mart/RefactorX.dcl
new file mode 100644
index 0000000..e71f36b
--- /dev/null
+++ b/fp2/week45/mart/RefactorX.dcl
@@ -0,0 +1,15 @@
+definition module RefactorX
+
+import StdEnv
+
+:: Expr = NR Int | VAR Name | OP Expr Operator Expr | LET Name Expr Expr
+:: Name :== String
+:: Operator = PLUS | MIN | MUL | DIV
+:: Val = Result Int | Undef
+
+from StdClass import class toString
+
+instance toString Expr
+free :: Expr -> [Name]
+remove_unused_lets :: Expr -> Expr
+eval :: Expr -> Val
diff --git a/fp2/week45/mart/RefactorX.icl b/fp2/week45/mart/RefactorX.icl
new file mode 100644
index 0000000..3898c14
--- /dev/null
+++ b/fp2/week45/mart/RefactorX.icl
@@ -0,0 +1,71 @@
+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` (remove_unused_lets 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]