adamc@190
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@190
|
2 *
|
adamc@190
|
3 * This work is licensed under a
|
adamc@190
|
4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
|
adamc@190
|
5 * Unported License.
|
adamc@190
|
6 * The license text is available at:
|
adamc@190
|
7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
|
adamc@190
|
8 *)
|
adamc@190
|
9
|
adamc@190
|
10 (* begin hide *)
|
adamc@190
|
11 Require Import Arith List Omega.
|
adamc@190
|
12
|
adamc@190
|
13 Require Import Axioms Tactics.
|
adamc@190
|
14
|
adamc@190
|
15 Set Implicit Arguments.
|
adamc@190
|
16 (* end hide *)
|
adamc@190
|
17
|
adamc@190
|
18
|
adamc@190
|
19 (** %\chapter{Modeling Impure Languages}% *)
|
adamc@190
|
20
|
adamc@190
|
21 (** TODO: Prose for this chapter *)
|
adamc@190
|
22
|
adamc@190
|
23 Section var.
|
adamc@190
|
24 Variable var : Type.
|
adamc@190
|
25
|
adamc@190
|
26 Inductive term : Type :=
|
adamc@190
|
27 | Var : var -> term
|
adamc@190
|
28 | App : term -> term -> term
|
adamc@190
|
29 | Abs : (var -> term) -> term
|
adamc@190
|
30 | Unit : term.
|
adamc@190
|
31 End var.
|
adamc@190
|
32
|
adamc@190
|
33 Implicit Arguments Unit [var].
|
adamc@190
|
34
|
adamc@190
|
35 Notation "# v" := (Var v) (at level 70).
|
adamc@190
|
36 Notation "()" := Unit.
|
adamc@190
|
37
|
adamc@190
|
38 Infix "@" := App (left associativity, at level 72).
|
adamc@190
|
39 Notation "\ x , e" := (Abs (fun x => e)) (at level 73).
|
adamc@190
|
40 Notation "\ ? , e" := (Abs (fun _ => e)) (at level 73).
|
adamc@190
|
41
|
adamc@191
|
42 Definition Term := forall var, term var.
|
adamc@191
|
43
|
adamc@191
|
44 Definition ident : Term := fun _ => \x, #x.
|
adamc@191
|
45 Definition unite : Term := fun _ => ().
|
adamc@191
|
46 Definition ident_self : Term := fun _ => ident _ @ ident _.
|
adamc@191
|
47 Definition ident_unit : Term := fun _ => ident _ @ unite _.
|
adamc@191
|
48
|
adamc@191
|
49
|
adamc@191
|
50 Module impredicative.
|
adamc@191
|
51 Inductive dynamic : Set :=
|
adamc@191
|
52 | Dyn : forall (dynTy : Type), dynTy -> dynamic.
|
adamc@191
|
53
|
adamc@191
|
54 Inductive computation (T : Type) : Set :=
|
adamc@191
|
55 | Return : T -> computation T
|
adamc@191
|
56 | Bind : forall (T' : Type),
|
adamc@191
|
57 computation T' -> (T' -> computation T) -> computation T
|
adamc@191
|
58 | Unpack : dynamic -> computation T.
|
adamc@191
|
59
|
adamc@191
|
60 Inductive eval : forall T, computation T -> T -> Prop :=
|
adamc@191
|
61 | EvalReturn : forall T (v : T),
|
adamc@191
|
62 eval (Return v) v
|
adamc@191
|
63 | EvalUnpack : forall T (v : T),
|
adamc@191
|
64 eval (Unpack T (Dyn v)) v
|
adamc@191
|
65 | EvalBind : forall T1 T2 (c1 : computation T1) (c2 : T1 -> computation T2) v1 v2,
|
adamc@191
|
66 eval c1 v1
|
adamc@191
|
67 -> eval (c2 v1) v2
|
adamc@191
|
68 -> eval (Bind c1 c2) v2.
|
adamc@191
|
69
|
adamc@191
|
70 Fixpoint termDenote (e : term dynamic) : computation dynamic :=
|
adamc@191
|
71 match e with
|
adamc@191
|
72 | Var v => Return v
|
adamc@191
|
73 | App e1 e2 => Bind (termDenote e1) (fun f =>
|
adamc@191
|
74 Bind (termDenote e2) (fun x =>
|
adamc@191
|
75 Bind (Unpack (dynamic -> computation dynamic) f) (fun f' =>
|
adamc@191
|
76 f' x)))
|
adamc@191
|
77 | Abs e' => Return (Dyn (fun x => termDenote (e' x)))
|
adamc@191
|
78
|
adamc@191
|
79 | Unit => Return (Dyn tt)
|
adamc@191
|
80 end.
|
adamc@191
|
81
|
adamc@191
|
82 Definition TermDenote (E : Term) := termDenote (E _).
|
adamc@191
|
83
|
adamc@191
|
84 Eval compute in TermDenote ident.
|
adamc@191
|
85 Eval compute in TermDenote unite.
|
adamc@191
|
86 Eval compute in TermDenote ident_self.
|
adamc@191
|
87 Eval compute in TermDenote ident_unit.
|
adamc@191
|
88
|
adamc@191
|
89 Theorem eval_ident_unit : eval (TermDenote ident_unit) (Dyn tt).
|
adamc@191
|
90 compute.
|
adamc@191
|
91 repeat econstructor.
|
adamc@191
|
92 simpl.
|
adamc@191
|
93 constructor.
|
adamc@191
|
94 Qed.
|
adamc@191
|
95
|
adamc@191
|
96 Theorem invert_ident : forall (E : Term) d,
|
adamc@191
|
97 eval (TermDenote (fun _ => ident _ @ E _)) d
|
adamc@191
|
98 -> eval (TermDenote E) d.
|
adamc@191
|
99 inversion 1.
|
adamc@191
|
100
|
adamc@191
|
101 crush.
|
adamc@191
|
102
|
adamc@191
|
103 Focus 3.
|
adamc@191
|
104 crush.
|
adamc@191
|
105 unfold TermDenote in H0.
|
adamc@191
|
106 simpl in H0.
|
adamc@191
|
107 (** [injection H0.] *)
|
adamc@191
|
108 Abort.
|
adamc@191
|
109
|
adamc@191
|
110 End impredicative.
|
adamc@191
|
111
|
adamc@190
|
112
|
adamc@190
|
113 Module predicative.
|
adamc@190
|
114
|
adamc@190
|
115 Inductive val : Type :=
|
adamc@190
|
116 | Func : nat -> val
|
adamc@190
|
117 | VUnit.
|
adamc@190
|
118
|
adamc@190
|
119 Inductive computation : Type :=
|
adamc@190
|
120 | Return : val -> computation
|
adamc@190
|
121 | Bind : computation -> (val -> computation) -> computation
|
adamc@190
|
122 | CAbs : (val -> computation) -> computation
|
adamc@190
|
123 | CApp : val -> val -> computation.
|
adamc@190
|
124
|
adamc@190
|
125 Definition func := val -> computation.
|
adamc@190
|
126
|
adamc@190
|
127 Fixpoint get (n : nat) (ls : list func) {struct ls} : option func :=
|
adamc@190
|
128 match ls with
|
adamc@190
|
129 | nil => None
|
adamc@190
|
130 | x :: ls' =>
|
adamc@190
|
131 if eq_nat_dec n (length ls')
|
adamc@190
|
132 then Some x
|
adamc@190
|
133 else get n ls'
|
adamc@190
|
134 end.
|
adamc@190
|
135
|
adamc@190
|
136 Inductive eval : list func -> computation -> list func -> val -> Prop :=
|
adamc@190
|
137 | EvalReturn : forall ds d,
|
adamc@190
|
138 eval ds (Return d) ds d
|
adamc@190
|
139 | EvalBind : forall ds c1 c2 ds' d1 ds'' d2,
|
adamc@190
|
140 eval ds c1 ds' d1
|
adamc@190
|
141 -> eval ds' (c2 d1) ds'' d2
|
adamc@190
|
142 -> eval ds (Bind c1 c2) ds'' d2
|
adamc@190
|
143 | EvalCAbs : forall ds f,
|
adamc@190
|
144 eval ds (CAbs f) (f :: ds) (Func (length ds))
|
adamc@190
|
145 | EvalCApp : forall ds i d2 f ds' d3,
|
adamc@190
|
146 get i ds = Some f
|
adamc@190
|
147 -> eval ds (f d2) ds' d3
|
adamc@190
|
148 -> eval ds (CApp (Func i) d2) ds' d3.
|
adamc@190
|
149
|
adamc@190
|
150 Fixpoint termDenote (e : term val) : computation :=
|
adamc@190
|
151 match e with
|
adamc@190
|
152 | Var v => Return v
|
adamc@190
|
153 | App e1 e2 => Bind (termDenote e1) (fun f =>
|
adamc@190
|
154 Bind (termDenote e2) (fun x =>
|
adamc@190
|
155 CApp f x))
|
adamc@190
|
156 | Abs e' => CAbs (fun x => termDenote (e' x))
|
adamc@190
|
157
|
adamc@190
|
158 | Unit => Return VUnit
|
adamc@190
|
159 end.
|
adamc@190
|
160
|
adamc@190
|
161 Definition TermDenote (E : Term) := termDenote (E _).
|
adamc@190
|
162
|
adamc@190
|
163 Eval compute in TermDenote ident.
|
adamc@190
|
164 Eval compute in TermDenote unite.
|
adamc@190
|
165 Eval compute in TermDenote ident_self.
|
adamc@190
|
166 Eval compute in TermDenote ident_unit.
|
adamc@190
|
167
|
adamc@190
|
168 Theorem eval_ident_unit : exists ds, eval nil (TermDenote ident_unit) ds VUnit.
|
adamc@190
|
169 compute.
|
adamc@190
|
170 repeat econstructor.
|
adamc@190
|
171 simpl.
|
adamc@190
|
172 rewrite (eta Return).
|
adamc@190
|
173 reflexivity.
|
adamc@190
|
174 Qed.
|
adamc@190
|
175
|
adamc@190
|
176 Hint Constructors eval.
|
adamc@190
|
177
|
adamc@190
|
178 Lemma app_nil_start : forall A (ls : list A),
|
adamc@190
|
179 ls = nil ++ ls.
|
adamc@190
|
180 reflexivity.
|
adamc@190
|
181 Qed.
|
adamc@190
|
182
|
adamc@190
|
183 Lemma app_cons : forall A (x : A) (ls : list A),
|
adamc@190
|
184 x :: ls = (x :: nil) ++ ls.
|
adamc@190
|
185 reflexivity.
|
adamc@190
|
186 Qed.
|
adamc@190
|
187
|
adamc@190
|
188 Theorem eval_monotone : forall ds c ds' d,
|
adamc@190
|
189 eval ds c ds' d
|
adamc@190
|
190 -> exists ds'', ds' = ds'' ++ ds.
|
adamc@190
|
191 Hint Resolve app_nil_start app_ass app_cons.
|
adamc@190
|
192
|
adamc@190
|
193 induction 1; firstorder; subst; eauto.
|
adamc@190
|
194 Qed.
|
adamc@190
|
195
|
adamc@190
|
196 Lemma length_app : forall A (ds2 ds1 : list A),
|
adamc@190
|
197 length (ds1 ++ ds2) = length ds1 + length ds2.
|
adamc@190
|
198 induction ds1; simpl; intuition.
|
adamc@190
|
199 Qed.
|
adamc@190
|
200
|
adamc@190
|
201 Lemma get_app : forall ds2 d ds1,
|
adamc@190
|
202 get (length ds2) (ds1 ++ d :: ds2) = Some d.
|
adamc@190
|
203 Hint Rewrite length_app : cpdt.
|
adamc@190
|
204
|
adamc@190
|
205 induction ds1; crush;
|
adamc@190
|
206 match goal with
|
adamc@190
|
207 | [ |- context[if ?E then _ else _] ] => destruct E
|
adamc@190
|
208 end; crush.
|
adamc@190
|
209 Qed.
|
adamc@190
|
210
|
adamc@190
|
211 Theorem invert_ident : forall (E : Term) ds ds' d,
|
adamc@190
|
212 eval ds (TermDenote (fun _ => ident _ @ E _)) ds' d
|
adamc@190
|
213 -> eval ((fun x => Return x) :: ds) (TermDenote E) ds' d.
|
adamc@190
|
214 inversion 1; subst.
|
adamc@190
|
215 clear H.
|
adamc@190
|
216 inversion H3; clear H3; subst.
|
adamc@190
|
217 inversion H6; clear H6; subst.
|
adamc@190
|
218 generalize (eval_monotone H2); crush.
|
adamc@190
|
219 inversion H5; clear H5; subst.
|
adamc@190
|
220 rewrite get_app in H3.
|
adamc@190
|
221 inversion H3; clear H3; subst.
|
adamc@190
|
222 inversion H7; clear H7; subst.
|
adamc@190
|
223 assumption.
|
adamc@190
|
224 Qed.
|
adamc@190
|
225
|
adamc@190
|
226 End predicative.
|