adamc@158
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@158
|
2 *
|
adamc@158
|
3 * This work is licensed under a
|
adamc@158
|
4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
|
adamc@158
|
5 * Unported License.
|
adamc@158
|
6 * The license text is available at:
|
adamc@158
|
7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
|
adamc@158
|
8 *)
|
adamc@158
|
9
|
adamc@158
|
10 (* begin hide *)
|
adamc@158
|
11 Require Import Arith Eqdep String List.
|
adamc@158
|
12
|
adamc@160
|
13 Require Import Axioms DepList Tactics.
|
adamc@158
|
14
|
adamc@158
|
15 Set Implicit Arguments.
|
adamc@158
|
16 (* end hide *)
|
adamc@158
|
17
|
adamc@158
|
18
|
adamc@158
|
19 (** %\chapter{Higher-Order Abstract Syntax}% *)
|
adamc@158
|
20
|
adamc@158
|
21 (** TODO: Prose for this chapter *)
|
adamc@158
|
22
|
adamc@158
|
23
|
adamc@158
|
24 (** * Parametric Higher-Order Abstract Syntax *)
|
adamc@158
|
25
|
adamc@158
|
26 Inductive type : Type :=
|
adamc@159
|
27 | Nat : type
|
adamc@158
|
28 | Arrow : type -> type -> type.
|
adamc@158
|
29
|
adamc@158
|
30 Infix "-->" := Arrow (right associativity, at level 60).
|
adamc@158
|
31
|
adamc@158
|
32 Section exp.
|
adamc@158
|
33 Variable var : type -> Type.
|
adamc@158
|
34
|
adamc@158
|
35 Inductive exp : type -> Type :=
|
adamc@159
|
36 | Const' : nat -> exp Nat
|
adamc@159
|
37 | Plus' : exp Nat -> exp Nat -> exp Nat
|
adamc@159
|
38
|
adamc@158
|
39 | Var : forall t, var t -> exp t
|
adamc@158
|
40 | App' : forall dom ran, exp (dom --> ran) -> exp dom -> exp ran
|
adamc@158
|
41 | Abs' : forall dom ran, (var dom -> exp ran) -> exp (dom --> ran).
|
adamc@158
|
42 End exp.
|
adamc@158
|
43
|
adamc@158
|
44 Implicit Arguments Const' [var].
|
adamc@158
|
45 Implicit Arguments Var [var t].
|
adamc@158
|
46 Implicit Arguments Abs' [var dom ran].
|
adamc@158
|
47
|
adamc@158
|
48 Definition Exp t := forall var, exp var t.
|
adamc@158
|
49 Definition Exp1 t1 t2 := forall var, var t1 -> exp var t2.
|
adamc@158
|
50
|
adamc@159
|
51 Definition Const (n : nat) : Exp Nat :=
|
adamc@159
|
52 fun _ => Const' n.
|
adamc@159
|
53 Definition Plus (E1 E2 : Exp Nat) : Exp Nat :=
|
adamc@159
|
54 fun _ => Plus' (E1 _) (E2 _).
|
adamc@158
|
55 Definition App dom ran (F : Exp (dom --> ran)) (X : Exp dom) : Exp ran :=
|
adamc@158
|
56 fun _ => App' (F _) (X _).
|
adamc@158
|
57 Definition Abs dom ran (B : Exp1 dom ran) : Exp (dom --> ran) :=
|
adamc@158
|
58 fun _ => Abs' (B _).
|
adamc@158
|
59
|
adamc@158
|
60 Section flatten.
|
adamc@158
|
61 Variable var : type -> Type.
|
adamc@158
|
62
|
adamc@158
|
63 Fixpoint flatten t (e : exp (exp var) t) {struct e} : exp var t :=
|
adamc@158
|
64 match e in exp _ t return exp _ t with
|
adamc@159
|
65 | Const' n => Const' n
|
adamc@159
|
66 | Plus' e1 e2 => Plus' (flatten e1) (flatten e2)
|
adamc@158
|
67 | Var _ e' => e'
|
adamc@158
|
68 | App' _ _ e1 e2 => App' (flatten e1) (flatten e2)
|
adamc@158
|
69 | Abs' _ _ e' => Abs' (fun x => flatten (e' (Var x)))
|
adamc@158
|
70 end.
|
adamc@158
|
71 End flatten.
|
adamc@158
|
72
|
adamc@158
|
73 Definition Subst t1 t2 (E1 : Exp t1) (E2 : Exp1 t1 t2) : Exp t2 := fun _ =>
|
adamc@158
|
74 flatten (E2 _ (E1 _)).
|
adamc@158
|
75
|
adamc@158
|
76
|
adamc@158
|
77 (** * A Type Soundness Proof *)
|
adamc@158
|
78
|
adamc@158
|
79 Reserved Notation "E1 ==> E2" (no associativity, at level 90).
|
adamc@158
|
80
|
adamc@158
|
81 Inductive Val : forall t, Exp t -> Prop :=
|
adamc@159
|
82 | VConst : forall n, Val (Const n)
|
adamc@158
|
83 | VAbs : forall dom ran (B : Exp1 dom ran), Val (Abs B).
|
adamc@158
|
84
|
adamc@158
|
85 Hint Constructors Val.
|
adamc@158
|
86
|
adamc@162
|
87 Inductive Ctx : type -> type -> Type :=
|
adamc@162
|
88 | AppCong1 : forall (dom ran : type),
|
adamc@162
|
89 Exp dom -> Ctx (dom --> ran) ran
|
adamc@162
|
90 | AppCong2 : forall (dom ran : type),
|
adamc@162
|
91 Exp (dom --> ran) -> Ctx dom ran
|
adamc@162
|
92 | PlusCong1 : Exp Nat -> Ctx Nat Nat
|
adamc@162
|
93 | PlusCong2 : Exp Nat -> Ctx Nat Nat.
|
adamc@162
|
94
|
adamc@162
|
95 Inductive isCtx : forall t1 t2, Ctx t1 t2 -> Prop :=
|
adamc@162
|
96 | IsApp1 : forall dom ran (X : Exp dom), isCtx (AppCong1 ran X)
|
adamc@162
|
97 | IsApp2 : forall dom ran (F : Exp (dom --> ran)), Val F -> isCtx (AppCong2 F)
|
adamc@162
|
98 | IsPlus1 : forall E2, isCtx (PlusCong1 E2)
|
adamc@162
|
99 | IsPlus2 : forall E1, Val E1 -> isCtx (PlusCong2 E1).
|
adamc@162
|
100
|
adamc@162
|
101 Definition plug t1 t2 (C : Ctx t1 t2) : Exp t1 -> Exp t2 :=
|
adamc@162
|
102 match C in Ctx t1 t2 return Exp t1 -> Exp t2 with
|
adamc@162
|
103 | AppCong1 _ _ X => fun F => App F X
|
adamc@162
|
104 | AppCong2 _ _ F => fun X => App F X
|
adamc@162
|
105 | PlusCong1 E2 => fun E1 => Plus E1 E2
|
adamc@162
|
106 | PlusCong2 E1 => fun E2 => Plus E1 E2
|
adamc@162
|
107 end.
|
adamc@162
|
108
|
adamc@162
|
109 Infix "@" := plug (no associativity, at level 60).
|
adamc@162
|
110
|
adamc@158
|
111 Inductive Step : forall t, Exp t -> Exp t -> Prop :=
|
adamc@158
|
112 | Beta : forall dom ran (B : Exp1 dom ran) (X : Exp dom),
|
adamc@160
|
113 Val X
|
adamc@160
|
114 -> App (Abs B) X ==> Subst X B
|
adamc@159
|
115 | Sum : forall n1 n2,
|
adamc@159
|
116 Plus (Const n1) (Const n2) ==> Const (n1 + n2)
|
adamc@162
|
117 | Cong : forall t t' (C : Ctx t t') E E' E1,
|
adamc@162
|
118 isCtx C
|
adamc@162
|
119 -> E1 = C @ E
|
adamc@162
|
120 -> E ==> E'
|
adamc@162
|
121 -> E1 ==> C @ E'
|
adamc@159
|
122
|
adamc@158
|
123 where "E1 ==> E2" := (Step E1 E2).
|
adamc@158
|
124
|
adamc@162
|
125 Hint Constructors isCtx Step.
|
adamc@158
|
126
|
adamc@158
|
127 Inductive Closed : forall t, Exp t -> Prop :=
|
adamc@164
|
128 | CConst : forall n,
|
adamc@164
|
129 Closed (Const n)
|
adamc@159
|
130 | CPlus : forall E1 E2,
|
adamc@159
|
131 Closed E1
|
adamc@159
|
132 -> Closed E2
|
adamc@159
|
133 -> Closed (Plus E1 E2)
|
adamc@158
|
134 | CApp : forall dom ran (E1 : Exp (dom --> ran)) E2,
|
adamc@158
|
135 Closed E1
|
adamc@158
|
136 -> Closed E2
|
adamc@158
|
137 -> Closed (App E1 E2)
|
adamc@158
|
138 | CAbs : forall dom ran (E1 : Exp1 dom ran),
|
adamc@158
|
139 Closed (Abs E1).
|
adamc@158
|
140
|
adamc@158
|
141 Axiom closed : forall t (E : Exp t), Closed E.
|
adamc@158
|
142
|
adamc@163
|
143 Ltac my_subst :=
|
adamc@163
|
144 repeat match goal with
|
adamc@163
|
145 | [ x : type |- _ ] => subst x
|
adamc@163
|
146 end.
|
adamc@163
|
147
|
adamc@160
|
148 Ltac my_crush' :=
|
adamc@163
|
149 crush; my_subst;
|
adamc@158
|
150 repeat (match goal with
|
adamc@158
|
151 | [ H : _ |- _ ] => generalize (inj_pairT2 _ _ _ _ _ H); clear H
|
adamc@163
|
152 end; crush; my_subst).
|
adamc@163
|
153
|
adamc@163
|
154 Ltac equate_conj F G :=
|
adamc@163
|
155 match constr:(F, G) with
|
adamc@163
|
156 | (_ ?x1, _ ?x2) => constr:(x1 = x2)
|
adamc@163
|
157 | (_ ?x1 ?y1, _ ?x2 ?y2) => constr:(x1 = x2 /\ y1 = y2)
|
adamc@163
|
158 | (_ ?x1 ?y1 ?z1, _ ?x2 ?y2 ?z2) => constr:(x1 = x2 /\ y1 = y2 /\ z1 = z2)
|
adamc@163
|
159 | (_ ?x1 ?y1 ?z1 ?u1, _ ?x2 ?y2 ?z2 ?u2) => constr:(x1 = x2 /\ y1 = y2 /\ z1 = z2 /\ u1 = u2)
|
adamc@163
|
160 | (_ ?x1 ?y1 ?z1 ?u1 ?v1, _ ?x2 ?y2 ?z2 ?u2 ?v2) => constr:(x1 = x2 /\ y1 = y2 /\ z1 = z2 /\ u1 = u2 /\ v1 = v2)
|
adamc@163
|
161 end.
|
adamc@158
|
162
|
adamc@160
|
163 Ltac my_crush :=
|
adamc@160
|
164 my_crush';
|
adamc@163
|
165 repeat (match goal with
|
adamc@163
|
166 | [ H : ?F = ?G |- _ ] =>
|
adamc@163
|
167 (let H' := fresh "H'" in
|
adamc@163
|
168 assert (H' : F (fun _ => unit) = G (fun _ => unit)); [ congruence
|
adamc@163
|
169 | discriminate || injection H'; clear H' ];
|
adamc@163
|
170 my_crush';
|
adamc@163
|
171 repeat match goal with
|
adamc@163
|
172 | [ H : context[fun _ => unit] |- _ ] => clear H
|
adamc@163
|
173 end;
|
adamc@163
|
174 match type of H with
|
adamc@163
|
175 | ?F = ?G =>
|
adamc@163
|
176 let ec := equate_conj F G in
|
adamc@163
|
177 let var := fresh "var" in
|
adamc@163
|
178 assert ec; [ intuition; unfold Exp; apply ext_eq; intro var;
|
adamc@163
|
179 assert (H' : F var = G var); try congruence;
|
adamc@163
|
180 match type of H' with
|
adamc@163
|
181 | ?X = ?Y =>
|
adamc@163
|
182 let X := eval hnf in X in
|
adamc@163
|
183 let Y := eval hnf in Y in
|
adamc@163
|
184 change (X = Y) in H'
|
adamc@163
|
185 end; injection H'; my_crush'; tauto
|
adamc@163
|
186 | intuition; subst ]
|
adamc@163
|
187 end);
|
adamc@163
|
188 clear H
|
adamc@163
|
189 end; my_crush');
|
adamc@163
|
190 my_crush'.
|
adamc@160
|
191
|
adamc@162
|
192 Hint Extern 1 (_ = _ @ _) => simpl.
|
adamc@162
|
193
|
adamc@158
|
194 Lemma progress' : forall t (E : Exp t),
|
adamc@158
|
195 Closed E
|
adamc@158
|
196 -> Val E \/ exists E', E ==> E'.
|
adamc@158
|
197 induction 1; crush;
|
adamc@159
|
198 repeat match goal with
|
adamc@159
|
199 | [ H : Val _ |- _ ] => inversion H; []; clear H; my_crush
|
adamc@162
|
200 end; eauto 6.
|
adamc@158
|
201 Qed.
|
adamc@158
|
202
|
adamc@158
|
203 Theorem progress : forall t (E : Exp t),
|
adamc@158
|
204 Val E \/ exists E', E ==> E'.
|
adamc@158
|
205 intros; apply progress'; apply closed.
|
adamc@158
|
206 Qed.
|
adamc@158
|
207
|
adamc@160
|
208
|
adamc@160
|
209 (** * Big-Step Semantics *)
|
adamc@160
|
210
|
adamc@160
|
211 Reserved Notation "E1 ===> E2" (no associativity, at level 90).
|
adamc@160
|
212
|
adamc@160
|
213 Inductive BigStep : forall t, Exp t -> Exp t -> Prop :=
|
adamc@160
|
214 | SConst : forall n,
|
adamc@160
|
215 Const n ===> Const n
|
adamc@160
|
216 | SPlus : forall E1 E2 n1 n2,
|
adamc@160
|
217 E1 ===> Const n1
|
adamc@160
|
218 -> E2 ===> Const n2
|
adamc@160
|
219 -> Plus E1 E2 ===> Const (n1 + n2)
|
adamc@160
|
220
|
adamc@160
|
221 | SApp : forall dom ran (E1 : Exp (dom --> ran)) E2 B V2 V,
|
adamc@160
|
222 E1 ===> Abs B
|
adamc@160
|
223 -> E2 ===> V2
|
adamc@160
|
224 -> Subst V2 B ===> V
|
adamc@160
|
225 -> App E1 E2 ===> V
|
adamc@160
|
226 | SAbs : forall dom ran (B : Exp1 dom ran),
|
adamc@160
|
227 Abs B ===> Abs B
|
adamc@160
|
228
|
adamc@160
|
229 where "E1 ===> E2" := (BigStep E1 E2).
|
adamc@160
|
230
|
adamc@160
|
231 Hint Constructors BigStep.
|
adamc@160
|
232
|
adamc@160
|
233 Reserved Notation "E1 ==>* E2" (no associativity, at level 90).
|
adamc@160
|
234
|
adamc@160
|
235 Inductive MultiStep : forall t, Exp t -> Exp t -> Prop :=
|
adamc@160
|
236 | Done : forall t (E : Exp t), E ==>* E
|
adamc@160
|
237 | OneStep : forall t (E E' E'' : Exp t),
|
adamc@160
|
238 E ==> E'
|
adamc@160
|
239 -> E' ==>* E''
|
adamc@160
|
240 -> E ==>* E''
|
adamc@160
|
241
|
adamc@160
|
242 where "E1 ==>* E2" := (MultiStep E1 E2).
|
adamc@160
|
243
|
adamc@160
|
244 Hint Constructors MultiStep.
|
adamc@160
|
245
|
adamc@160
|
246 Theorem MultiStep_trans : forall t (E1 E2 E3 : Exp t),
|
adamc@160
|
247 E1 ==>* E2
|
adamc@160
|
248 -> E2 ==>* E3
|
adamc@160
|
249 -> E1 ==>* E3.
|
adamc@160
|
250 induction 1; eauto.
|
adamc@160
|
251 Qed.
|
adamc@160
|
252
|
adamc@160
|
253 Theorem Big_Val : forall t (E V : Exp t),
|
adamc@160
|
254 E ===> V
|
adamc@160
|
255 -> Val V.
|
adamc@160
|
256 induction 1; crush.
|
adamc@160
|
257 Qed.
|
adamc@160
|
258
|
adamc@160
|
259 Theorem Val_Big : forall t (V : Exp t),
|
adamc@160
|
260 Val V
|
adamc@160
|
261 -> V ===> V.
|
adamc@160
|
262 destruct 1; crush.
|
adamc@160
|
263 Qed.
|
adamc@160
|
264
|
adamc@160
|
265 Hint Resolve Big_Val Val_Big.
|
adamc@160
|
266
|
adamc@162
|
267 Lemma Multi_Cong : forall t t' (C : Ctx t t'),
|
adamc@162
|
268 isCtx C
|
adamc@162
|
269 -> forall E E', E ==>* E'
|
adamc@162
|
270 -> C @ E ==>* C @ E'.
|
adamc@160
|
271 induction 2; crush; eauto.
|
adamc@160
|
272 Qed.
|
adamc@160
|
273
|
adamc@162
|
274 Lemma Multi_Cong' : forall t t' (C : Ctx t t') E1 E2 E E',
|
adamc@162
|
275 isCtx C
|
adamc@162
|
276 -> E1 = C @ E
|
adamc@162
|
277 -> E2 = C @ E'
|
adamc@162
|
278 -> E ==>* E'
|
adamc@162
|
279 -> E1 ==>* E2.
|
adamc@162
|
280 crush; apply Multi_Cong; auto.
|
adamc@162
|
281 Qed.
|
adamc@162
|
282
|
adamc@162
|
283 Hint Resolve Multi_Cong'.
|
adamc@162
|
284
|
adamc@162
|
285 Ltac mtrans E :=
|
adamc@162
|
286 match goal with
|
adamc@162
|
287 | [ |- E ==>* _ ] => fail 1
|
adamc@162
|
288 | _ => apply MultiStep_trans with E; [ solve [ eauto ] | eauto ]
|
adamc@162
|
289 end.
|
adamc@160
|
290
|
adamc@160
|
291 Theorem Big_Multi : forall t (E V : Exp t),
|
adamc@160
|
292 E ===> V
|
adamc@160
|
293 -> E ==>* V.
|
adamc@162
|
294 induction 1; crush; eauto;
|
adamc@162
|
295 repeat match goal with
|
adamc@162
|
296 | [ n1 : _, E2 : _ |- _ ] => mtrans (Plus (Const n1) E2)
|
adamc@162
|
297 | [ n1 : _, n2 : _ |- _ ] => mtrans (Plus (Const n1) (Const n2))
|
adamc@162
|
298 | [ B : _, E2 : _ |- _ ] => mtrans (App (Abs B) E2)
|
adamc@162
|
299 end.
|
adamc@160
|
300 Qed.
|
adamc@160
|
301
|
adamc@160
|
302 Lemma Big_Val' : forall t (V1 V2 : Exp t),
|
adamc@160
|
303 Val V2
|
adamc@160
|
304 -> V1 = V2
|
adamc@160
|
305 -> V1 ===> V2.
|
adamc@160
|
306 crush.
|
adamc@160
|
307 Qed.
|
adamc@160
|
308
|
adamc@160
|
309 Hint Resolve Big_Val'.
|
adamc@160
|
310
|
adamc@160
|
311 Lemma Multi_Big' : forall t (E E' : Exp t),
|
adamc@160
|
312 E ==> E'
|
adamc@160
|
313 -> forall E'', E' ===> E''
|
adamc@160
|
314 -> E ===> E''.
|
adamc@160
|
315 induction 1; crush; eauto;
|
adamc@160
|
316 match goal with
|
adamc@160
|
317 | [ H : _ ===> _ |- _ ] => inversion H; my_crush; eauto
|
adamc@162
|
318 end;
|
adamc@162
|
319 match goal with
|
adamc@162
|
320 | [ H : isCtx _ |- _ ] => inversion H; my_crush; eauto
|
adamc@160
|
321 end.
|
adamc@160
|
322 Qed.
|
adamc@160
|
323
|
adamc@160
|
324 Hint Resolve Multi_Big'.
|
adamc@160
|
325
|
adamc@160
|
326 Theorem Multi_Big : forall t (E V : Exp t),
|
adamc@160
|
327 E ==>* V
|
adamc@160
|
328 -> Val V
|
adamc@160
|
329 -> E ===> V.
|
adamc@160
|
330 induction 1; crush; eauto.
|
adamc@160
|
331 Qed.
|
adamc@160
|
332
|
adamc@160
|
333
|
adamc@160
|
334 (** * Constant folding *)
|
adamc@160
|
335
|
adamc@160
|
336 Section cfold.
|
adamc@160
|
337 Variable var : type -> Type.
|
adamc@160
|
338
|
adamc@160
|
339 Fixpoint cfold t (e : exp var t) {struct e} : exp var t :=
|
adamc@160
|
340 match e in exp _ t return exp _ t with
|
adamc@160
|
341 | Const' n => Const' n
|
adamc@160
|
342 | Plus' e1 e2 =>
|
adamc@160
|
343 let e1' := cfold e1 in
|
adamc@160
|
344 let e2' := cfold e2 in
|
adamc@160
|
345 match e1', e2' with
|
adamc@160
|
346 | Const' n1, Const' n2 => Const' (n1 + n2)
|
adamc@160
|
347 | _, _ => Plus' e1' e2'
|
adamc@160
|
348 end
|
adamc@160
|
349
|
adamc@160
|
350 | Var _ x => Var x
|
adamc@160
|
351 | App' _ _ e1 e2 => App' (cfold e1) (cfold e2)
|
adamc@160
|
352 | Abs' _ _ e' => Abs' (fun x => cfold (e' x))
|
adamc@160
|
353 end.
|
adamc@160
|
354 End cfold.
|
adamc@160
|
355
|
adamc@160
|
356 Definition Cfold t (E : Exp t) : Exp t := fun _ => cfold (E _).
|
adamc@163
|
357 Definition Cfold1 t1 t2 (E : Exp1 t1 t2) : Exp1 t1 t2 := fun _ x => cfold (E _ x).
|
adamc@163
|
358
|
adamc@163
|
359 Lemma fold_Cfold : forall t (E : Exp t),
|
adamc@163
|
360 (fun _ => cfold (E _)) = Cfold E.
|
adamc@163
|
361 reflexivity.
|
adamc@163
|
362 Qed.
|
adamc@163
|
363
|
adamc@163
|
364 Hint Rewrite fold_Cfold : fold.
|
adamc@163
|
365
|
adamc@163
|
366 Lemma fold_Cfold1 : forall t1 t2 (E : Exp1 t1 t2),
|
adamc@163
|
367 (fun _ x => cfold (E _ x)) = Cfold1 E.
|
adamc@163
|
368 reflexivity.
|
adamc@163
|
369 Qed.
|
adamc@163
|
370
|
adamc@164
|
371 Hint Rewrite fold_Cfold1 : fold.
|
adamc@164
|
372
|
adamc@163
|
373 Lemma fold_Subst_Cfold1 : forall t1 t2 (E : Exp1 t1 t2) (V : Exp t1),
|
adamc@163
|
374 (fun _ => flatten (cfold (E _ (V _))))
|
adamc@163
|
375 = Subst V (Cfold1 E).
|
adamc@163
|
376 reflexivity.
|
adamc@163
|
377 Qed.
|
adamc@163
|
378
|
adamc@163
|
379 Axiom cheat : forall T, T.
|
adamc@163
|
380
|
adamc@163
|
381
|
adamc@163
|
382 Lemma fold_Const : forall n, (fun _ => Const' n) = Const n.
|
adamc@163
|
383 reflexivity.
|
adamc@163
|
384 Qed.
|
adamc@163
|
385 Lemma fold_Plus : forall (E1 E2 : Exp _), (fun _ => Plus' (E1 _) (E2 _)) = Plus E1 E2.
|
adamc@163
|
386 reflexivity.
|
adamc@163
|
387 Qed.
|
adamc@163
|
388 Lemma fold_App : forall dom ran (F : Exp (dom --> ran)) (X : Exp dom),
|
adamc@163
|
389 (fun _ => App' (F _) (X _)) = App F X.
|
adamc@163
|
390 reflexivity.
|
adamc@163
|
391 Qed.
|
adamc@163
|
392 Lemma fold_Abs : forall dom ran (B : Exp1 dom ran),
|
adamc@163
|
393 (fun _ => Abs' (B _)) = Abs B.
|
adamc@163
|
394 reflexivity.
|
adamc@163
|
395 Qed.
|
adamc@163
|
396
|
adamc@163
|
397 Hint Rewrite fold_Const fold_Plus fold_App fold_Abs : fold.
|
adamc@163
|
398
|
adamc@163
|
399 Lemma fold_Subst : forall t1 t2 (E1 : Exp1 t1 t2) (V : Exp t1),
|
adamc@163
|
400 (fun _ => flatten (E1 _ (V _))) = Subst V E1.
|
adamc@163
|
401 reflexivity.
|
adamc@163
|
402 Qed.
|
adamc@163
|
403
|
adamc@163
|
404 Hint Rewrite fold_Subst : fold.
|
adamc@163
|
405
|
adamc@163
|
406 Section Closed1.
|
adamc@163
|
407 Variable xt : type.
|
adamc@163
|
408
|
adamc@163
|
409 Definition Const1 (n : nat) : Exp1 xt Nat :=
|
adamc@163
|
410 fun _ _ => Const' n.
|
adamc@163
|
411 Definition Var1 : Exp1 xt xt := fun _ x => Var x.
|
adamc@163
|
412 Definition Plus1 (E1 E2 : Exp1 xt Nat) : Exp1 xt Nat :=
|
adamc@163
|
413 fun _ s => Plus' (E1 _ s) (E2 _ s).
|
adamc@163
|
414 Definition App1 dom ran (F : Exp1 xt (dom --> ran)) (X : Exp1 xt dom) : Exp1 xt ran :=
|
adamc@163
|
415 fun _ s => App' (F _ s) (X _ s).
|
adamc@163
|
416 Definition Abs1 dom ran (B : forall var, var dom -> Exp1 xt ran) : Exp1 xt (dom --> ran) :=
|
adamc@163
|
417 fun _ s => Abs' (fun x => B _ x _ s).
|
adamc@163
|
418
|
adamc@163
|
419 Inductive Closed1 : forall t, Exp1 xt t -> Prop :=
|
adamc@164
|
420 | CConst1 : forall n,
|
adamc@164
|
421 Closed1 (Const1 n)
|
adamc@163
|
422 | CPlus1 : forall E1 E2,
|
adamc@163
|
423 Closed1 E1
|
adamc@163
|
424 -> Closed1 E2
|
adamc@163
|
425 -> Closed1 (Plus1 E1 E2)
|
adamc@163
|
426 | CApp1 : forall dom ran (E1 : Exp1 _ (dom --> ran)) E2,
|
adamc@163
|
427 Closed1 E1
|
adamc@163
|
428 -> Closed1 E2
|
adamc@163
|
429 -> Closed1 (App1 E1 E2)
|
adamc@163
|
430 | CAbs1 : forall dom ran (E1 : forall var, var dom -> Exp1 _ ran),
|
adamc@163
|
431 Closed1 (Abs1 E1).
|
adamc@163
|
432
|
adamc@163
|
433 Axiom closed1 : forall t (E : Exp1 xt t), Closed1 E.
|
adamc@163
|
434 End Closed1.
|
adamc@163
|
435
|
adamc@163
|
436 Hint Resolve closed1.
|
adamc@163
|
437
|
adamc@164
|
438 Ltac ssimp := unfold Subst, Cfold in *; simpl in *; autorewrite with fold in *;
|
adamc@163
|
439 repeat match goal with
|
adamc@163
|
440 | [ xt : type |- _ ] =>
|
adamc@163
|
441 rewrite (@fold_Subst xt) in *
|
adamc@163
|
442 end;
|
adamc@163
|
443 autorewrite with fold in *.
|
adamc@163
|
444
|
adamc@165
|
445 Lemma cfold_thorough : forall var t (e : exp var t),
|
adamc@165
|
446 cfold (cfold e) = cfold e.
|
adamc@165
|
447 induction e; crush; try (f_equal; ext_eq; eauto);
|
adamc@165
|
448 match goal with
|
adamc@165
|
449 | [ e1 : exp _ Nat, e2 : exp _ Nat |- _ ] =>
|
adamc@165
|
450 dep_destruct (cfold e1); crush;
|
adamc@165
|
451 dep_destruct (cfold e2); crush
|
adamc@165
|
452 end.
|
adamc@165
|
453 Qed.
|
adamc@165
|
454
|
adamc@165
|
455 Lemma Cfold_thorough : forall t (E : Exp t),
|
adamc@165
|
456 Cfold (Cfold E) = Cfold E.
|
adamc@165
|
457 intros; unfold Cfold, Exp; ext_eq;
|
adamc@165
|
458 apply cfold_thorough.
|
adamc@165
|
459 Qed.
|
adamc@165
|
460
|
adamc@165
|
461 Hint Resolve Cfold_thorough.
|
adamc@165
|
462
|
adamc@165
|
463 Section eq_arg.
|
adamc@165
|
464 Variable A : Type.
|
adamc@165
|
465 Variable B : A -> Type.
|
adamc@165
|
466
|
adamc@165
|
467 Variable x : A.
|
adamc@165
|
468
|
adamc@165
|
469 Variables f g : forall x, B x.
|
adamc@165
|
470 Hypothesis Heq : f = g.
|
adamc@165
|
471
|
adamc@165
|
472 Theorem eq_arg : f x = g x.
|
adamc@165
|
473 congruence.
|
adamc@165
|
474 Qed.
|
adamc@165
|
475 End eq_arg.
|
adamc@165
|
476
|
adamc@165
|
477 Implicit Arguments eq_arg [A B f g].
|
adamc@165
|
478
|
adamc@165
|
479 Lemma Cfold_Subst_thorough : forall t1 (V : Exp t1) t2 (B : Exp1 t1 t2),
|
adamc@165
|
480 Subst (Cfold V) (Cfold1 B) = Cfold (Subst (Cfold V) (Cfold1 B)).
|
adamc@165
|
481
|
adamc@165
|
482 Lemma Cfold_Step_thorough' : forall t (E V : Exp t),
|
adamc@165
|
483 E ===> V
|
adamc@165
|
484 -> forall E', E = Cfold E'
|
adamc@165
|
485 -> Cfold V = V.
|
adamc@165
|
486 induction 1; crush.
|
adamc@165
|
487 apply IHBigStep3 with (Subst V2 B).
|
adamc@165
|
488
|
adamc@165
|
489 generalize (closed E'); inversion 1; my_crush.
|
adamc@165
|
490
|
adamc@165
|
491 generalize (eq_arg (fun _ => Set) H2); ssimp.
|
adamc@165
|
492 dep_destruct (cfold (E0 (fun _ => Set))); try discriminate;
|
adamc@165
|
493 dep_destruct (cfold (E3 (fun _ => Set))); discriminate.
|
adamc@165
|
494
|
adamc@165
|
495 ssimp; my_crush.
|
adamc@165
|
496
|
adamc@165
|
497 rewrite <- (IHBigStep2 _ (refl_equal _)).
|
adamc@165
|
498 generalize (IHBigStep1 _ (refl_equal _)).
|
adamc@165
|
499 my_crush.
|
adamc@165
|
500 ssimp.
|
adamc@165
|
501 assert (B = Cfold1 B).
|
adamc@165
|
502 generalize H2; clear_all; my_crush.
|
adamc@165
|
503 unfold Exp1; ext_eq.
|
adamc@165
|
504 generalize (eq_arg x H2); injection 1; my_crush.
|
adamc@165
|
505
|
adamc@165
|
506 rewrite H8.
|
adamc@165
|
507
|
adamc@165
|
508
|
adamc@165
|
509 my_crush.
|
adamc@165
|
510
|
adamc@165
|
511 Lemma Cfold_thorough : forall t (E V : Exp t),
|
adamc@165
|
512 Cfold E ===> V
|
adamc@165
|
513 -> V = Cfold V.
|
adamc@165
|
514
|
adamc@164
|
515 Lemma Cfold_Subst' : forall t (E V : Exp t),
|
adamc@164
|
516 E ===> V
|
adamc@164
|
517 -> forall t' B (V' : Exp t') V'', E = Cfold (Subst V' B)
|
adamc@164
|
518 -> V = Cfold V''
|
adamc@164
|
519 -> Closed1 B
|
adamc@164
|
520 -> Subst (Cfold V') (Cfold1 B) ===> Cfold V''.
|
adamc@164
|
521 induction 1; inversion 3; my_crush; ssimp; my_crush.
|
adamc@163
|
522
|
adamc@164
|
523 rewrite <- H0; auto.
|
adamc@163
|
524
|
adamc@163
|
525 apply cheat.
|
adamc@164
|
526 apply cheat.
|
adamc@163
|
527 apply cheat.
|
adamc@163
|
528
|
adamc@164
|
529 repeat rewrite (@fold_Subst_Cfold1 t') in *.
|
adamc@164
|
530 repeat rewrite fold_Cfold in *.
|
adamc@164
|
531 apply SApp with (Cfold1 B) V2.
|
adamc@163
|
532
|
adamc@164
|
533 unfold Abs, Subst, Cfold, Cfold1 in *.
|
adamc@164
|
534 match goal with
|
adamc@164
|
535 | [ |- _ ===> ?F ] =>
|
adamc@164
|
536 replace F with (fun var => cfold (Abs' (fun x : var _ => B var x)))
|
adamc@164
|
537 end.
|
adamc@164
|
538 apply IHBigStep1; auto.
|
adamc@165
|
539 ssimp.
|
adamc@164
|
540 apply cheat.
|
adamc@164
|
541 reflexivity.
|
adamc@163
|
542
|
adamc@164
|
543 replace V2 with (Cfold V2).
|
adamc@164
|
544 unfold Cfold, Subst.
|
adamc@164
|
545 apply IHBigStep2; auto.
|
adamc@164
|
546 apply cheat.
|
adamc@164
|
547 apply cheat.
|
adamc@163
|
548
|
adamc@164
|
549 replace V2 with (Cfold V2).
|
adamc@164
|
550 unfold Subst, Cfold.
|
adamc@164
|
551 apply IHBigStep3; auto.
|
adamc@164
|
552 apply cheat.
|
adamc@164
|
553 apply cheat.
|
adamc@163
|
554
|
adamc@164
|
555 apply cheat.
|
adamc@164
|
556 Qed.
|
adamc@163
|
557
|
adamc@163
|
558 Theorem Cfold_Subst : forall t1 t2 (V : Exp t1) B (V' : Exp t2),
|
adamc@164
|
559 Subst (Cfold V) (Cfold1 B) ===> Cfold V'
|
adamc@163
|
560 -> Subst (Cfold V) (Cfold1 B) ===> Cfold V'.
|
adamc@164
|
561 Hint Resolve Cfold_Subst'.
|
adamc@164
|
562
|
adamc@164
|
563 eauto.
|
adamc@164
|
564 Qed.
|
adamc@164
|
565
|
adamc@164
|
566 Hint Resolve Cfold_Subst.
|
adamc@163
|
567
|
adamc@163
|
568 Theorem Cfold_correct : forall t (E V : Exp t),
|
adamc@163
|
569 E ===> V
|
adamc@163
|
570 -> Cfold E ===> Cfold V.
|
adamc@165
|
571 induction 1; crush; ssimp; eauto.
|
adamc@163
|
572
|
adamc@164
|
573 change ((fun H1 : type -> Type =>
|
adamc@164
|
574 match Cfold E1 H1 with
|
adamc@164
|
575 | Const' n3 =>
|
adamc@164
|
576 match Cfold E2 H1 with
|
adamc@164
|
577 | Const' n4 => Const' (var:=H1) (n3 + n4)
|
adamc@164
|
578 | Plus' _ _ => Plus' (cfold (E1 H1)) (cfold (E2 H1))
|
adamc@164
|
579 | Var _ _ => Plus' (cfold (E1 H1)) (cfold (E2 H1))
|
adamc@164
|
580 | App' _ _ _ _ => Plus' (cfold (E1 H1)) (cfold (E2 H1))
|
adamc@164
|
581 | Abs' _ _ _ => Plus' (cfold (E1 H1)) (cfold (E2 H1))
|
adamc@164
|
582 end
|
adamc@164
|
583 | Plus' _ _ => Plus' (cfold (E1 H1)) (cfold (E2 H1))
|
adamc@164
|
584 | Var _ _ => Plus' (cfold (E1 H1)) (cfold (E2 H1))
|
adamc@164
|
585 | App' _ _ _ _ => Plus' (cfold (E1 H1)) (cfold (E2 H1))
|
adamc@164
|
586 | Abs' _ _ _ => Plus' (cfold (E1 H1)) (cfold (E2 H1))
|
adamc@164
|
587 end) ===> Const (n1 + n2)).
|
adamc@163
|
588
|
adamc@164
|
589 Ltac simp :=
|
adamc@164
|
590 repeat match goal with
|
adamc@164
|
591 | [ H : _ = Cfold _ |- _ ] => rewrite <- H in *
|
adamc@164
|
592 | [ H : Const _ ===> Const _ |- _ ] =>
|
adamc@164
|
593 inversion H; clear H; my_crush
|
adamc@164
|
594 end.
|
adamc@163
|
595
|
adamc@164
|
596 generalize (closed (Cfold E1)); inversion 1; my_crush; simp;
|
adamc@164
|
597 try solve [ ssimp; simp; eauto ];
|
adamc@164
|
598 generalize (closed (Cfold E2)); inversion 1; my_crush; simp; ssimp; simp; eauto.
|
adamc@164
|
599 Qed.
|