aboutsummaryrefslogtreecommitdiffstats
path: root/theories/evallang/interp.v
diff options
context:
space:
mode:
Diffstat (limited to 'theories/evallang/interp.v')
-rw-r--r--theories/evallang/interp.v52
1 files changed, 52 insertions, 0 deletions
diff --git a/theories/evallang/interp.v b/theories/evallang/interp.v
new file mode 100644
index 0000000..d98b87f
--- /dev/null
+++ b/theories/evallang/interp.v
@@ -0,0 +1,52 @@
1From mininix Require Export res evallang.operational_props.
2From stdpp Require Import options.
3
4Module Import evallang.
5Export evallang.
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 x =>
25 t ← Res $ (E !! x) ∪ (Thunk ∅ <$> ds);
26 interp (thunk_env t) (thunk_expr t)
27 | EEval ds e =>
28 v ← interp E e;
29 s ← Res $ maybe VString v;
30 e ← Res $ parse s;
31 interp (E ∪ (Thunk ∅ <$> ds)) e
32 | EAbs ex e =>
33 v ← interp E ex;
34 x ← Res $ maybe VString v;
35 mret (VClo x E e)
36 | EApp e1 e2 =>
37 v1 ← interp E e1;
38 '(x, E', e') ← Res (maybe3 VClo v1);
39 interp (<[x:=Thunk E e2]> E') e'
40 end.
41
42Fixpoint interp (n : nat) (E : env) (e : expr) : res val :=
43 match n with
44 | O => NoFuel
45 | S n => interp1 (interp n) E e
46 end.
47
48Global Opaque interp.
49
50End evallang.
51
52Add Printing Constructor evallang.thunk.