diff options
Diffstat (limited to 'theories/dynlang/interp.v')
-rw-r--r-- | theories/dynlang/interp.v | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/theories/dynlang/interp.v b/theories/dynlang/interp.v new file mode 100644 index 0000000..dcf6b95 --- /dev/null +++ b/theories/dynlang/interp.v | |||
@@ -0,0 +1,49 @@ | |||
1 | From mininix Require Export res dynlang.operational_props. | ||
2 | From stdpp Require Import options. | ||
3 | |||
4 | Module Import dynlang. | ||
5 | Export dynlang. | ||
6 | |||
7 | Inductive thunk := Thunk { thunk_env : gmap string thunk; thunk_expr : expr }. | ||
8 | Add Printing Constructor thunk. | ||
9 | Notation env := (gmap string thunk). | ||
10 | |||
11 | Inductive val := | ||
12 | | VString (s : string) | ||
13 | | VClo (x : string) (E : env) (e : expr). | ||
14 | |||
15 | Global Instance maybe_VString : Maybe VString := λ v, | ||
16 | if v is VString s then Some s else None. | ||
17 | Global Instance maybe_VClo : Maybe3 VClo := λ v, | ||
18 | if v is VClo x E e then Some (x, E, e) else None. | ||
19 | |||
20 | Definition interp1 (interp : env → expr → res val) (E : env) (e : expr) : res val := | ||
21 | match e with | ||
22 | | EString s => | ||
23 | mret (VString s) | ||
24 | | EId ds e => | ||
25 | v ← interp E e; | ||
26 | x ← Res $ maybe VString v; | ||
27 | t ← Res $ (E !! x) ∪ (Thunk ∅ <$> ds !! x); | ||
28 | interp (thunk_env t) (thunk_expr t) | ||
29 | | EAbs ex e => | ||
30 | v ← interp E ex; | ||
31 | x ← Res $ maybe VString v; | ||
32 | mret (VClo x E e) | ||
33 | | EApp e1 e2 => | ||
34 | v1 ← interp E e1; | ||
35 | '(x, E', e') ← Res (maybe3 VClo v1); | ||
36 | interp (<[x:=Thunk E e2]> E') e' | ||
37 | end. | ||
38 | |||
39 | Fixpoint interp (n : nat) (E : env) (e : expr) : res val := | ||
40 | match n with | ||
41 | | O => NoFuel | ||
42 | | S n => interp1 (interp n) E e | ||
43 | end. | ||
44 | |||
45 | Global Opaque interp. | ||
46 | |||
47 | End dynlang. | ||
48 | |||
49 | Add Printing Constructor dynlang.thunk. | ||