-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexprNoEffects.lhs
61 lines (42 loc) · 1.5 KB
/
exprNoEffects.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
> {-# LANGUAGE FlexibleContexts #-}
> {-# LANGUAGE FlexibleInstances #-}
> {-# LANGUAGE GADTs #-}
> {-# LANGUAGE MultiParamTypeClasses #-}
> {-# LANGUAGE ScopedTypeVariables #-}
> {-# LANGUAGE InstanceSigs #-} --enables type signatures in patterns
> import CompClasses --contains Correct Compiler classes
--toy source language without effects
> data Expr =
> Val Int
> | Add Expr Expr deriving Show
--toy target language without effects (GADT allows compositional code)
> data Code where
> HALT :: Code
> PUSH :: ExprValue -> Code -> Code
> ADD :: Code -> Code
--toy language Values
> data ExprValue = Num Int
--evaluation semantics for toy language without effects
> eval' :: Expr -> Int
> eval' (Val n) = n
> eval' (Add e1 e2) = eval' e1 + eval' e2
> instance Configuration Expr [ExprValue] where
> eval :: Expr -> [ExprValue] -> [ExprValue]
> eval (Val n) c = (Num n):c
> eval (Add e1 e2) c =
> case eval e1 c of
> c' -> case eval e2 c' of
> ((Num m):(Num n):c'') -> ((Num (m+n)) : c'')
> instance CorrectCompiler Expr Code [ExprValue] where
> exec :: Code -> [ExprValue] -> [ExprValue]
> exec HALT c = c
> exec (PUSH n t) c = exec t (n:c)
> exec (ADD t) ((Num m):(Num n):c) = exec t ((Num (m+n)):c)
> comp :: Expr -> Code
> comp x = comp' x HALT
> comp' :: Expr -> Code -> Code
> comp' (Val n) t = PUSH (Num n) t
> comp' (Add e1 e2) t = comp' e1 (comp' e2 (ADD t))
> --laws
> --exec t (eval s c) = exec (comp' s t) c
> --exec (comp s) c = eval s c