added practicum files, updated gitignore
[fp1415.git] / files / practicum / RefactorXX.icl
1 implementation module RefactorXX
2
3 import StdClass, StdInt, StdList, StdOverloaded, StdString
4
5 //Start = map toString [E1,E2,E3,E4,E5]
6
7 /*
8 E1 = (let x = 42 - 3 in x / 0) + (let y = 6 in y * y)
9 E2 = let x = 42 in x + (let x = 58 in x)
10 E3 = let x = 1 in let y = 2 in let x = 3 in 4
11 E4 = let x = 1 in x + y
12 E5 = (let x = 1 in x) * x
13 E6 = let x = 5 in let y = x in 0
14 */
15
16 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"))))
17 E2 = (LET "x" (NR 42) (OP (VAR "x") PLUS (LET "x" (NR 58) (VAR "x"))))
18 E3 = (LET "x" (NR 1) (LET "y" (NR 2) (LET "x" (NR 3) (NR 4))))
19 E4 = (LET "x" (NR 1) (OP (VAR "x") PLUS (VAR "y")))
20 E5 = (OP (LET "x" (NR 1) (VAR "x")) MUL (VAR "x"))
21 E6 = (LET "x" (NR 5) (LET "y" (VAR "x") (NR 0)))
22
23 :: Expr = NR Int
24 | VAR Name
25 | OP Expr Operator Expr
26 | LET Name Expr Expr
27 :: Name :== String
28 :: Operator = PLUS | MIN | MUL | DIV
29 :: Val a = Result a | Undef
30
31 class fail c :: c a
32 class return c :: a -> c a
33 class (>>=) infix 0 c :: (c a) (a -> c b) -> c b
34 class Monad c | return, >>= c
35 class MonadFail c | Monad, fail c
36
37 instance fail Val where // maak deze instance af
38 instance return Val where // maak deze instance af
39 instance >>= Val where // maak deze instance af
40
41 instance fail [] where // maak deze instance af
42 instance return [] where // maak deze instance af
43 instance >>= [] where // maak deze instance af
44
45 // expressies afdrukken:
46 instance toString Expr where // maak deze instance af
47
48 // vrije variabelen:
49 free :: Expr -> [Name]
50 free // maak deze functie af
51
52 // verwijder deelexpressies met ongebruikte let-variabelen:
53 remove_unused_lets :: Expr -> Expr
54 remove_unused_lets // maak deze functie af
55
56 // evaluator, monadische stijl:
57 eval :: Expr -> c Int | MonadFail c
58 eval // maak deze functie af
59
60
61 Start :: [[Int]]
62 Start = [eval E1, eval E2, eval E3, eval E4, eval E5]