-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_LEnv.v
414 lines (306 loc) · 8.86 KB
/
Test_LEnv.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
Require Export Basics.
Require Export EnvLibA.
Require Export RelLibA.
Require Export Coq.Program.Equality.
Require Import Coq.Init.Specif.
Require Import Coq.Arith.PeanoNat.
Require Import Omega.
Require Import Coq.Lists.List.
Require Import Coq.Logic.ProofIrrelevance.
Require Import IdModTypeA.
Require Import StaticSemA.
Require Import DynamicSemA.
Require Import TRInductA.
Require Import WeakenA.
Require Import TSoundnessA.
Require Import AbbrevA.
Require Import ModLEnvA.
Module Test_LEnv <: IdModType.
Module TS := Abbrev ModLEnv.
Export TS.
Definition Id := TS.Id.
Definition IdEqDec := TS.IdEqDec.
Definition IdEq := TS.IdEq.
Definition W := TS.W.
Definition Loc_PI := TS.Loc_PI.
Definition BInit := TS.BInit.
Definition WP := TS.WP.
Open Scope string_scope.
Import ListNotations.
(**************************************************)
Fixpoint findD {K V: Type} {h: DEq K} (m: list (K * V)) (d: V) (k: K) : V :=
match m with
| nil => d
| cons (k', x) ls => match (dEq k k') with
| left _ => x
| right _ => findD ls d k
end
end.
Fixpoint update1 {K V: Type} {h: DEq K} (m: list (K * V)) (k: K) (v: V) :
list (K * V) :=
match m with
| nil => [(k,v)]
| cons (k', x) ls => match (dEq k k') with
| left _ => (k',v):: ls
| right _ => (k',x) :: update1 ls k v
end
end.
Fixpoint update0 {K V: Type} {h: DEq K} (m: list (K * V)) (k: K) (v: V) :
list (K * V) :=
match m with
| nil => nil
| cons (k', x) ls => match (dEq k k') with
| left _ => (k',v):: ls
| right _ => (k',x) :: update1 ls k v
end
end.
Fixpoint increase1 {K: Type} {h: DEq K} (m: list (K * nat)) (k: K) (v: nat) :
list (K * nat) :=
match m with
| nil => nil
| cons (k', x) ls => match (dEq k k') with
| left _ => (k',v):: ls
| right _ => (k',x+v) :: update1 ls k v
end
end.
Definition lenv_read : XFun Id nat := {|
b_mod := fun s x => (s, findD s 0 x)
|}.
Definition lenv_write : XFun (Id * nat) unit := {|
b_mod := fun s x => (update1 s (fst x) (snd x), tt)
|}.
Definition lenv_reset : XFun (PState W) unit := {|
b_mod := fun x _ => (b_init, tt)
|}.
Definition lenv_incr : XFun (Id * nat) unit := {|
b_mod := fun s x => (increase1 s (fst x) (snd x), tt)
|}.
Instance VT_Id : ValTyp Id.
Instance VT_IdNat : ValTyp (Id * nat).
Definition ReadL (x: Id) : Exp :=
Modify Id nat VT_Id NatVT lenv_read (QV (cst Id x)).
Definition WriteL (x: Id) (v: nat) : Exp :=
Modify (Id * nat) unit VT_IdNat UnitVT lenv_write (QV (cst (Id * nat) (x,v))).
Definition ResetL : Exp :=
Modify (PState W) unit PState_ValTyp UnitVT xf_reset
(QV (cst (PState W) WP)).
Definition Incr (x: Id) (v: nat) : Exp :=
Modify (Id * nat) unit VT_IdNat UnitVT lenv_incr
(QV (cst (Id * nat) (x, v))).
Lemma read_nat_1 (P0: Value -> Prop)
(fenv: funEnv) (env: valEnv):
forall (s s': W) (v: Value) (x: Id),
EClosure fenv env (Conf Exp s (ReadL x))
(Conf Exp s' (Val v)) ->
P0 (cst nat (findD s 0 x)) -> P0 v.
intros.
inversion X; subst.
inversion X0; subst.
eapply inj_pair2 in H6.
eapply inj_pair2 in H6.
subst.
clear H7.
eapply inj_pair2 in H8.
subst.
clear XF1.
inversion X1; subst.
unfold xf_read in *.
unfold b_exec, b_eval in *.
simpl in *.
auto.
inversion X2.
inversion X2.
Qed.
Lemma expTypingTest1 : ExpTyping emptyE emptyE emptyE
(BindN ResetL (ReadL "x")) Nat.
econstructor.
econstructor.
econstructor.
constructor.
simpl.
auto.
simpl.
apply PState_ValTyp.
constructor.
constructor.
constructor.
simpl.
auto.
simpl.
apply VT_Id.
Defined.
Definition Test1 (n: W) := runTest (BindN ResetL (ReadL "x")) Nat
expTypingTest1 n.
Lemma Test1L (n: W) : Test1 n = cst nat 0.
auto.
Qed.
(**************************************************)
(* line 1 : if true then 3 else 3) *)
Definition three : Exp := Val(cst nat 3).
Definition line1 : Exp := IfThenElse TrueV three three.
Lemma expTypingline1 : ExpTyping emptyE emptyE emptyE line1 Nat.
Proof.
repeat constructor.
Defined.
Definition line1Test (n: W) := runTest line1 Nat expTypingline1 n.
Lemma Test2 (n: W) : line1Test n = cst nat 3.
auto.
Defined.
Lemma Test3 (n: W) : exists x:nat, line1Test n = cst nat x.
eexists.
compute.
reflexivity.
Defined.
Eval compute in (line1Test nil).
(***********************************************)
(* binds1 : ... *)
Definition binds1 : Exp := BindS "x" (Val (cst nat 0)) (Return RR (Var "x")).
Definition expTypingTest (e: Exp) (t: VTyp): Type :=
ExpTyping emptyE emptyE emptyE e t.
Lemma binds1typing: expTypingTest binds1 Nat.
econstructor.
instantiate (1:= Nat).
constructor.
constructor.
reflexivity.
repeat constructor.
repeat constructor.
Defined.
Definition binds1Test (n: W) := runTest binds1 Nat binds1typing n.
Lemma binds1Test_proof : binds1Test nil = cst nat 0.
auto.
Qed.
(*********************************************************)
(* line 2 : apply add3 to 0*)
Definition zero: Exp := Val (cst nat 0).
Definition xf_add3 : XFun nat nat := {|
b_mod := fun st x => (st, x+3)
|}.
Definition add3': Exp := Modify nat nat NatVT NatVT xf_add3 (Var "x").
Definition add3: QFun := QF (FC emptyE [("x",Nat)] add3' zero "add3" 0).
Definition line2: Exp := Apply add3 (PS [zero]).
Lemma expTypingline2 : ExpTyping emptyE emptyE emptyE line2 Nat.
Proof.
repeat econstructor.
Defined.
Definition line2Test (n: W) := runTest line2 Nat
expTypingline2 n.
(*
Lemma Test2s :
ExpTyping emptyE emptyE emptyE line2 Nat ->
FEnvTyping emptyE emptyE ->
EnvTyping emptyE emptyE ->
forall n: W,
exists (v: nat) (p: (SoundExpA emptyE emptyE line2 Nat n)),
projT1 (sigT_of_sigT2 p) =
(cst nat v).
Proof.
intros.
eexists.
exists (WellTypedEvalM emptyE emptyE emptyE line2 Nat X X0 emptyE X1 n).
unfold cst.
simpl.
compute.
Require Import EqdepFacts.
eapply inj_pair2.
eapply expTypingline2.
eapply fenvTypingline2.
eapply envTypingline2.
Qed.
Lemma Test2s :
forall (ftenv: funTC) (tenv: valTC) (fenv: funEnv),
ExpTyping ftenv tenv fenv line2 Nat ->
FEnvTyping fenv ftenv ->
forall env: valEnv,
EnvTyping env tenv ->
forall n: W, SoundExpA fenv env line2 Nat n.
Proof.
intros.
eapply WellTypedEvalM.
eapply
Lemma Test2a : line2Test 0 = cst nat 3.
unfold line2Test.
simpl.
unfold cst.
destruct expTypingline2.
Admitted.
Lemma Test2b : exists n:nat, line2Test 0 = cst nat n.
eexists.
unfold line2Test.
simpl.
unfold cst.
simpl.
Admitted.
Lemma expTypingline2A : forall (ftenv: funTC) (tenv: valTC) (fenv: funEnv)
(k1: MatchEnvsT FunTyping fenv ftenv),
ExpTyping ftenv tenv fenv line2 Nat.
Proof.
econstructor.
reflexivity.
exact k1.
econstructor.
econstructor.
econstructor.
constructor.
constructor.
constructor.
constructor.
constructor.
constructor.
constructor.
constructor.
constructor.
constructor.
constructor.
constructor.
Defined.
Lemma line2TestA (n: nat) : exists
(ftenv: funTC) (tenv: valTC) (fenv: funEnv) (env: valEnv)
(k1: MatchEnvsT FunTyping fenv ftenv)
(k2: MatchEnvsT ValueTyping env tenv),
projT1 (sigT_of_sigT2
(WellTypedEvalM ftenv tenv fenv line2 Nat
(expTypingline2A ftenv tenv fenv k1) k1 env k2 n)) = cst nat 3.
eexists.
eexists.
eexists.
eexists.
eexists.
eexists.
simpl.
destruct expTypingline2A.
destruct expTypingline2.
destruct expTypingline2.
unfold expTypingline2.
Eval compute in (line2Test 0).
simpl.
Qed.
*)
(**************************************************)
Definition line3: Exp := BindN line1 line2.
Lemma expTypingline3 : ExpTyping emptyE emptyE emptyE line3 Nat.
Proof.
econstructor.
eapply expTypingline1.
apply expTypingline2.
Defined.
Definition line3Test (n: W) := runTest line3 Nat
expTypingline3 n.
(*
Lemma Test3a (n: nat) : line3Test n = cst nat 3.
compute.
auto.
Qed.
*)
(******************)
(*
Open Scope string_scope.
Import ListNotations.
Definition xf_add3 : XFun nat nat := {|
b_mod := fun st x => (st,x+3)
|}.
Definition add3':Exp := Modify nat nat NatV NatV xf_add3 (Var "x").
Definition add3:QFun := QF(FC emptyE [("x",Nat)] add3' zero "add3" 0).
Definition line2:Exp := Apply add3 (PS [zero]).
*)
End Test_LEnv.