-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_Nat1.v
410 lines (312 loc) · 7.85 KB
/
Test_Nat1.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
Require Export Basics.
Require Export EnvLibA.
Require Export RelLibA.
Require Export PRelLibA.
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 StaticSemA.
Require Import DynamicSemA.
Require Import TRInductA.
Require Import WeakenA.
Require Import TSoundnessA.
Require Import IdModTypeA.
Require Import AbbrevA.
Require Import ModNat1A.
Module Test_Nat1 <: IdModType.
Module LM := Abbrev ModNat1.
Export LM.
Definition Id := LM.Id.
Definition IdEqDec := LM.IdEqDec.
Definition IdEq := LM.IdEq.
Definition W := LM.W.
Definition Loc_PI := LM.Loc_PI.
Definition BInit := LM.BInit.
Definition WP := LM.WP.
Open Scope string_scope.
Import ListNotations.
Definition nat_read : XFun unit nat := xf_read id.
Definition nat_write : XFun nat unit := xf_write id.
Definition nat_reset : XFun (PState nat) unit := xf_reset.
Definition nat_incr : XFun nat unit := {|
b_mod := fun x y => (x + 1, tt)
|}.
Definition ReadN : Exp := Read NatVT id.
Definition WriteN (x: nat) : Exp := Write NatVT id x.
Definition ResetN : Exp := Reset.
Definition Incr (x: nat) : Exp :=
Modify nat unit NatVT UnitVT nat_incr (QV (cst nat x)).
Lemma expTypingTest1 : expTypingTest (BindN ResetN ReadN) Nat.
econstructor.
econstructor.
econstructor.
constructor.
simpl.
auto.
simpl.
apply PState_ValTyp.
constructor.
constructor.
constructor.
simpl.
auto.
simpl.
apply UnitVT.
Defined.
Definition Test1 (n: nat) := runTest (BindN ResetN ReadN) Nat
expTypingTest1 n.
Lemma Test1L (n: nat) : 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: nat) := runTest line1 Nat expTypingline1 n.
Lemma Test2 (n: nat) : line1Test n = cst nat 3.
auto.
Defined.
Lemma Test3 (n: nat) : exists x:nat, line1Test n = cst nat x.
eexists.
compute.
reflexivity.
Defined.
Eval compute in (line1Test 0).
(***********************************************)
(* 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: nat) := runTest binds1 Nat binds1typing n.
Lemma binds1Test_proof : binds1Test 0 = 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: nat) := runTest line2 Nat
expTypingline2 n.
Lemma line2Test_lemma1 :
EClosure emptyE emptyE (Conf Exp 0 line2) (Conf Exp 0 three).
unfold line2, three.
unfold add3.
unfold add3'.
unfold xf_add3.
unfold cst.
econstructor.
econstructor.
econstructor.
instantiate (1:= [(existT ValueI nat (Cst nat 0))]).
auto.
reflexivity.
simpl.
reflexivity.
econstructor.
econstructor.
reflexivity.
reflexivity.
simpl.
econstructor.
econstructor.
econstructor.
econstructor.
econstructor.
econstructor.
reflexivity.
reflexivity.
econstructor.
econstructor.
econstructor.
econstructor.
Defined.
Lemma line2Test_lemma2 :
EClosure emptyE emptyE (Conf Exp 0 line2) (Conf Exp 0 three).
econstructor.
econstructor.
econstructor.
instantiate (1:= [(existT ValueI nat (Cst nat 0))]).
auto.
auto.
auto.
repeat econstructor.
Defined.
(* Watch out!!
Lemma lin2Test_lemma : EClosure emptyE emptyE (Conf Exp 0 (Apply add3 (PS [zero])))
(Conf Exp 0 (Val (existT ValueI nat (Cst nat 3)))).
repeat econstructor.
instantiate (1:= [(existT ValueI nat (Cst nat 0))]).
auto.
auto.
simpl.
unfold cst.
eauto.
repeat econstructor.
*)
Lemma line2ExpTyping : forall (ftenv: funTC) (tenv: valTC) (fenv: funEnv)
(k1: MatchEnvsT FunTyping fenv ftenv),
ExpTyping ftenv tenv fenv line2 Nat.
Proof.
intros.
econstructor.
reflexivity.
assumption.
econstructor.
econstructor.
econstructor.
econstructor.
econstructor.
econstructor.
econstructor.
reflexivity.
econstructor.
econstructor.
econstructor.
constructor.
reflexivity.
constructor.
constructor.
Defined.
(*
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: nat) := 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_Nat1.