From a7d7542dc646a5fd124ef71e71ce260889f1701b Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 2 Feb 2016 19:24:50 +0100 Subject: Moved to 1415 directory --- 1415/fp2/week45/camil/RefactorX.dcl | 18 ++++++++ 1415/fp2/week45/camil/RefactorX.icl | 82 ++++++++++++++++++++++++++++++++++++ 1415/fp2/week45/mart/RefactorX.dcl | 17 ++++++++ 1415/fp2/week45/mart/RefactorX.icl | 72 +++++++++++++++++++++++++++++++ 1415/fp2/week45/week45.tar.gz | Bin 0 -> 1091 bytes 5 files changed, 189 insertions(+) create mode 100644 1415/fp2/week45/camil/RefactorX.dcl create mode 100644 1415/fp2/week45/camil/RefactorX.icl create mode 100644 1415/fp2/week45/mart/RefactorX.dcl create mode 100644 1415/fp2/week45/mart/RefactorX.icl create mode 100644 1415/fp2/week45/week45.tar.gz (limited to '1415/fp2/week45') diff --git a/1415/fp2/week45/camil/RefactorX.dcl b/1415/fp2/week45/camil/RefactorX.dcl new file mode 100644 index 0000000..3ddc8a4 --- /dev/null +++ b/1415/fp2/week45/camil/RefactorX.dcl @@ -0,0 +1,18 @@ +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/1415/fp2/week45/camil/RefactorX.icl b/1415/fp2/week45/camil/RefactorX.icl new file mode 100644 index 0000000..a7bf909 --- /dev/null +++ b/1415/fp2/week45/camil/RefactorX.icl @@ -0,0 +1,82 @@ +implementation module RefactorX + +import StdEnv + +Start = map eval [E1,E2,E3,E4,E5] + +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") + +:: Expr = NR Int + | VAR Name + | OP Expr Operator Expr + | LET Name Expr Expr +:: Name :== String +:: Operator = PLUS | MIN | MUL | DIV +:: Val = Result Int | Undef + +(<+) infixl 9 :: String a -> String | toString a +(<+) str a = str +++ toString a + +instance toString Operator where + toString PLUS = "+" + toString MIN = "-" + toString MUL = "*" + toString DIV = "/" + +// expressies afdrukken: +instance toString Expr where + toString (NR n) = toString n + toString (VAR s) = s + toString (LET s e1 e2) = "let " <+ s <+ " = " <+ e1 <+ " in " <+ e2 + toString (OP e1 o e2) = bracket e1 <+ o <+ bracket e2 + where + bracket :: Expr -> String + bracket (OP e1 o e2) = "(" <+ e1 <+ o <+ e2 <+ ")" + bracket (LET s e1 e2) = "(" <+ (LET s e1 e2) <+ ")" + bracket x = toString x + +// vrije variabelen: +free :: Expr -> [Name] +free (NR _) = [] +free (VAR s) = [s] +free (LET s _ e2) = [n \\ n <- free e2 | n <> s] +free (OP e1 _ e2) = removeDup ((free e1) ++ (free e2)) + +// verwijder deelexpressies met ongebruikte let-variabelen: +remove_unused_lets :: Expr -> Expr +remove_unused_lets (LET s e1 e2) +| isMember s (free e2) = (LET s (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 x = x + +// evaluator met tabel van naam-waarde paren: +eval :: Expr -> Val +eval e = eval` e [] +where + eval` :: Expr [(Name, Val)] -> Val + eval` (NR n) vs = Result n + eval` (VAR s) vs = find s vs + where + find :: Name [(Name, Val)] -> Val + find _ [] = Undef + find s [(t,v):vs] + | s == t = v + | otherwise = find s vs + eval` (LET s e1 e2) vs = eval` e2 [(s,eval` e1 vs):vs] + eval` (OP e1 o e2) vs = op o (eval` e1 vs) (eval` e2 vs) + where + op :: Operator Val Val -> Val + op _ Undef _ = Undef + op _ _ Undef = Undef + op PLUS (Result x) (Result y) = Result (x + y) + op MIN (Result x) (Result y) = Result (x - y) + op MUL (Result x) (Result y) = Result (x * y) + op DIV _ (Result 0) = Undef + op DIV (Result x) (Result y) = Result (x / y) diff --git a/1415/fp2/week45/mart/RefactorX.dcl b/1415/fp2/week45/mart/RefactorX.dcl new file mode 100644 index 0000000..393c097 --- /dev/null +++ b/1415/fp2/week45/mart/RefactorX.dcl @@ -0,0 +1,17 @@ +// Mart Lubbers s4109503, Camil Staps s4498062 + +definition module RefactorX + +from StdClass import class toString +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 + + +instance toString Expr +free :: Expr -> [Name] +remove_unused_lets :: Expr -> Expr +eval :: Expr -> Val 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] diff --git a/1415/fp2/week45/week45.tar.gz b/1415/fp2/week45/week45.tar.gz new file mode 100644 index 0000000..690e838 Binary files /dev/null and b/1415/fp2/week45/week45.tar.gz differ -- cgit v1.2.3