11 ingeleverd
[ap2015.git] / a11 / mart / skeleton11.icl
1 module skeleton11
2 //Mart Lubbers s4109503
3 //Charlie Gerhardus s3050009
4 import StdEnv
5
6 :: Prog :== [Instr]
7 :: Instr = Write Expr | Atomic [Instr]
8 :: Expr = Int Int
9 | Plus Expr Expr
10 | Times Expr Expr
11 | Read
12
13 possibleResults :: [Prog] -> [Int]
14 possibleResults x = produce x 0
15
16 produce :: [Prog] Int -> [Int]
17 produce x i
18 | all (isEmpty) x = [i]
19 = flatten [produce (updateAt idx ys x) (eval y i)\\(idx, [y:ys])<-zip2 [0..] x]
20
21 eval :: Instr Int -> Int
22 eval (Write (Int i)) s = i
23 eval (Write (Plus e1 e2)) s = eval (Write e1) s + eval (Write e2) s
24 eval (Write (Times e1 e2)) s = eval (Write e1) s * eval (Write e2) s
25 eval (Write Read) s = s
26 eval (Atomic x) s = hd (produce [x] s)
27
28 prog0 = [Write (Int 12), Write (Plus Read (Int 1))]
29 prog1 = [Write (Times Read (Int 2))]
30 test0 = [prog0]
31 test1 = [prog0, prog1]
32
33 Start = 0
34 /* Writing the semantics in clean is advantageous because you can execute the
35 * code, have it type checked and it is very readable for a programmer.
36 *
37 * Disadvantages might be that it might be harder to prove stuff. Also
38 * mathematic notation is generally more concise.
39 *
40 * Some things are modelled very difficultly in a functional language. For
41 * example file io, parallel processes or user interaction.
42 */