aboutsummaryrefslogtreecommitdiffstats
path: root/theories/dynlang/interp.v
diff options
context:
space:
mode:
Diffstat (limited to 'theories/dynlang/interp.v')
-rw-r--r--theories/dynlang/interp.v49
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 @@
1From mininix Require Export res dynlang.operational_props.
2From stdpp Require Import options.
3
4Module Import dynlang.
5Export dynlang.
6
7Inductive thunk := Thunk { thunk_env : gmap string thunk; thunk_expr : expr }.
8Add Printing Constructor thunk.
9Notation env := (gmap string thunk).
10
11Inductive val :=
12 | VString (s : string)
13 | VClo (x : string) (E : env) (e : expr).
14
15Global Instance maybe_VString : Maybe VString := λ v,
16 if v is VString s then Some s else None.
17Global Instance maybe_VClo : Maybe3 VClo := λ v,
18 if v is VClo x E e then Some (x, E, e) else None.
19
20Definition 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
39Fixpoint 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
45Global Opaque interp.
46
47End dynlang.
48
49Add Printing Constructor dynlang.thunk.