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