-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathMicroFStar.fst
6720 lines (6093 loc) · 279 KB
/
MicroFStar.fst
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module MicroFStar
(* Formalization of micro-fstar proofs of progress and preservation
for the PURE effect. The definitions cover most of micro-fstar, the
only exceptions are 3 more recently introduced features:
multi-monads, higher-order state, and dynamic
allocation, however, none of these are relevant here, since our
proofs are for the PURE effect. The proofs are mostly complete and
all assumed lemmas are listed and explained below. Many are not yet
complete because of limitations in our current F* implementation,
which we hope to fix soon. *)
(*TODO list:
definitions
(void)
substitution lemma
* update VInjTH
* write the substitution lemmas on type encodings (subst_on_bindall etc …)
[F* limitations and take a lot of time to write, but easy things on paper]
derived judgements
* write the v_* validity derived judgements [take a lot of time to write]
* write kdg_* to manipulate kinds with binding, wp etc …
[F* limitations and take a lot of time to write]
* finish validity_derived (V-Constr, VInjTH) [easy]
* write kwf_* [take a lot of time to write]
inversion lemmas (used in preservation and progress)
* finish scmp_transitivity [need to write a validity proof term; difficult]
* write stypingd_inversion_arrow [easy, same as styping_inversion_arrow]
* correctly reorder styping inversion arrow lemmas, and write the bindings [easy]
* code the case of application in value_inversion [long to write and difficult]
* write inversion_tforalle / inversion_tforallt [long to write but easy]
preservation lemma
* write pure_to_tot [F* limitations and take long to write]
* write the code for validity judgements [F* limitations]
* write the PsFixPure case [F* limitation and hard to write]
* write skdg_eqe and skdg_eqt [hard proofs that need a lot of rewriting]
progress lemma
* write tysub_derived [easy]
* make the code compile without commenting some parts
[F* limitations (I can not use split case here, as in other places)]
* remove useless code (like tint_inversion_* )
* write the bindings from already written code to *_empty
(like styping_inversion_arrow_empty) [easy]
optimization of verification
* add split case option where useful
* in substitution lemma, split_cases is mostly working but it is still
not possible to add the full body of validity_substitution
* update to finish with the adding of PsUpd, PsSel and PsFixPure
* remove the useless rule about heap (since we added PsUpd and PsSel)
other things to do
* write section names at the end
* remove useless code
*)
(*TODO in tiny-fstar.txt
* simplify the proof of preservation:
remove the useless preservation lemmas and do an induction on typing judgement
*)
open FStar.Classical
open FStar.FunctionalExtensionality
open FStar.Constructive
type var = nat
type loc = nat
type heap = loc -> Tot int
type eff =
| EfPure
| EfAll
type econst =
| EcUnit
| EcInt : i:int -> econst
| EcLoc : l:loc -> econst
| EcBang
| EcAssign
| EcSel
| EcUpd
| EcHeap : h:heap -> econst
| EcFixPure : tx:typ -> t':typ -> t'':typ -> wp:typ -> econst
| EcFixOmega : tx:typ -> t':typ -> wp:typ -> econst
and tconst =
| TcUnit
| TcInt
| TcRefInt
| TcHeap
| TcFalse
| TcAnd
| TcForallE
| TcForallT : k:knd -> tconst
| TcEqE
| TcEqT : k:knd -> tconst
| TcPrecedes
and knd =
| KType : knd
| KKArr : karg:knd -> kres:knd -> knd
| KTArr : targ:typ -> kres:knd -> knd
and typ =
| TVar : a:var -> typ
| TConst : c:tconst -> typ
| TArr : t:typ -> c:cmp -> typ
| TTLam : k:knd -> tbody:typ -> typ
| TELam : t:typ -> tbody:typ -> typ
| TTApp : t1:typ -> t2:typ -> typ
| TEApp : t:typ -> e:exp -> typ
and exp =
| EVar : x:var -> exp
| EConst : c:econst -> exp
| ELam : t:typ -> ebody:exp -> exp
| EIf0 : eguard:exp -> ethen:exp -> eelse:exp -> exp
| EApp : e1:exp -> e2:exp -> exp
and cmp =
| Cmp : m:eff -> t:typ -> wp:typ -> cmp
(******************)
(* Free variables *)
(******************)
val eeappears : x:var -> e:exp -> Tot bool
(decreases %[e])
val teappears : x:var -> t:typ -> Tot bool
(decreases %[t])
val keappears : x:var -> k:knd -> Tot bool
(decreases %[k])
val eceappears : x:var -> ec:econst -> Tot bool
(decreases %[ec])
val tceappears : x:var -> tc:tconst -> Tot bool
(decreases %[tc])
val ceappears : x:var -> c:cmp -> Tot bool
(decreases %[c])
let rec eeappears x e =
match e with
| EVar y -> x = y
| EConst c -> eceappears x c
| ELam t ebody -> teappears x t || eeappears (x+1) ebody
| EIf0 eg et ee -> eeappears x eg || eeappears x et || eeappears x ee
| EApp e1 e2 -> eeappears x e1 || eeappears x e2
and teappears x t =
match t with
| TVar a -> false
| TConst c -> tceappears x c
| TArr t c -> teappears x t || ceappears (x+1) c
| TTLam k tbody -> keappears x k || teappears x tbody
| TELam t tbody -> teappears x t || teappears (x+1) tbody
| TTApp t1 t2 -> teappears x t1|| teappears x t2
| TEApp t e -> teappears x t || eeappears x e
and keappears x k =
match k with
| KType -> false
| KKArr karg kres -> keappears x karg || keappears x kres
| KTArr targ kres -> teappears x targ || keappears (x+1) kres
and eceappears x ec =
match ec with
| EcFixPure tx t' t'' wp -> teappears x tx || teappears x t' || teappears x t'' || teappears x wp
| EcFixOmega tx t' wp -> teappears x tx || teappears x t' || teappears x wp
| _ -> false
and tceappears x tc =
match tc with
| TcForallT k -> keappears x k
| TcEqT k -> keappears x k
| _ -> false
and ceappears x c =
let Cmp m t wp = c in teappears x t || teappears x wp
val is_value : exp -> Tot bool
let rec is_value e =
match e with
| EConst _
| ELam _ _ -> true
| EVar _
| EIf0 _ _ _ -> false
| EApp e1 e2 -> is_value e2 &&
(match e1 with
| EApp e11 e12 ->
is_value e12 &&
(match e11 with
| EConst c -> (EcFixPure? c || EcUpd? c)
| _ -> false)
| EConst c -> (EcFixPure? c || EcFixOmega? c || EcUpd? c || EcSel? c || EcAssign? c)
| _ -> false)
type value = e:exp{is_value e}
let f e1 e2 = assert (is_value (EApp e1 e2) ==> is_value e1)
(****************************)
(* Sugar *)
(****************************)
let eunit = EConst EcUnit
let eint x = EConst (EcInt x)
let eloc l = EConst (EcLoc l)
let ebang el = EApp (EConst EcBang) el
let eassign el ei = EApp (EApp (EConst EcAssign) el) ei
let esel eh el = EApp (EApp (EConst EcSel) eh) el
let eupd eh el ei = EApp (EApp (EApp (EConst EcUpd) eh) el) ei
let eheap h = EConst (EcHeap h)
let tunit = TConst TcUnit
let tint = TConst TcInt
let tloc = TConst TcRefInt
let tref = TConst TcRefInt
let theap = TConst TcHeap
(*
fix_pure : tx:Type -> t':(tx->Type) -> t'':(tx->Type) ->
wp:(x:tx->K_PURE(t'' x)) -> d:(x:tx -> Tot (t' x)) ->
F:(x:tx -> f: (y:tx -> PURE (t'' y)
(up_PURE (precedes (d y) (d x)) /\_PURE (wp y)))
-> PURE (t'' x) (wp x)) ->
Tot t (where t = y:tx -> PURE (t'' y) (wp y))
fix_omega : tx:Type -> t':(tx->Type) -> wp:(x:tx->K_ALL(t' x)) ->
F:(x:tx -> f:t -> ALL (t' x) (wp x)) -> Tot t
(where t = y:tx -> ALL (t' y) (wp y))
*)
(*
let tfixpure = TTLam (KType) (*tx*)(
TTLam (KTArr (TVar 0) KType) (*t'*)(
TTLam (KTArr (TVar 1) KType) (*t''*)(
TTLam (KTArr (TVar 2) (k_m (EfPure) (TEApp (TVar 0) (EVar 0)))) (*wp*)(
*)
let ttapp2 tf t1 t2 = TTApp (TTApp tf t1) t2
let tfalse = TConst TcFalse
let tand a b = TTApp (TTApp (TConst TcAnd) a) b
let tforalle t p = TTApp (TTApp (TConst TcForallE) t) (TELam t p)
let tforallt k p = TTApp (TConst (TcForallT k)) (TTLam k p)
val teqe : typ -> exp -> exp -> Tot(typ)
let teqe t e1 e2 = TEApp (TEApp (TTApp (TConst TcEqE) t) e1) e2
(* SF : ^ TcEqE needs the type of the expression. To change *)
let teqt k t1 t2 = TTApp (TTApp (TConst (TcEqT k)) t1) t2
let teqtype = teqt KType
let tprecedes t1 t2 e1 e2 = TEApp (TEApp (ttapp2 (TConst TcPrecedes) t1 t2) e1) e2
let eapp2 efun earg1 earg2 = EApp (EApp efun earg1) earg2
let eapp3 efun earg1 earg2 earg3 = (EApp (EApp (EApp efun earg1) earg2) earg3)
(* SF : FIXME : problem here : bad kinding : TcPrecedes expect types *)
(*TODO:write a function {e|t|k}shift_up which
shift both expression and type variables
and prove some properties on it*)
(****************************)
(* Expression Substitutions *)
(****************************)
(* CH: My impression is that pairing up substitutions and having a
single set of operations for substituting would be better.
We can return to this later though. *)
type esub = var -> Tot exp
type erenaming (s:esub) = (forall (x:var). EVar? (s x))
opaque val is_erenaming : s:esub -> GTot (n:int{( erenaming s ==> n=0) /\
(~(erenaming s) ==> n=1)})
let is_erenaming s = (if excluded_middle (erenaming s) then 0 else 1)
type value_esub (s:esub) = (forall (x:var). is_value (s x))
val esub_id : esub
let esub_id = fun x -> EVar x
val esub_inc_gen : nat -> var -> Tot exp
let esub_inc_gen x y = EVar (y+x)
val esub_dec : var -> Tot exp
let esub_dec x = if x = 0 then EVar 0 else EVar (x-1)
(*
val esub_inc : var -> Tot exp
let esub_inc = esub_inc_gen 1
*)
val esub_inc : var -> Tot exp
let esub_inc x = EVar (x+1)
val esub_inc2 : var -> Tot exp
(* let esub_inc2 = esub_inc_gen 2 -- working around #311 *)
let esub_inc2 x = esub_inc_gen 2 x
let is_evar (e:exp) : int = if EVar? e then 0 else 1
val omap : ('a -> Tot 'b) -> option 'a -> Tot (option 'b)
let omap f o =
match o with
| Some x -> Some (f x)
| None -> None
(****************************)
(* Type Substitutions *)
(****************************)
type tsub = var -> Tot typ
opaque type trenaming (s:tsub) = (forall (x:var). TVar? (s x))
val is_trenaming : s:tsub -> GTot (n:int{( trenaming s ==> n=0) /\
(~(trenaming s) ==> n=1)})
let is_trenaming s = (if excluded_middle (trenaming s) then 0 else 1)
val tsub_inc_above : nat -> var -> Tot typ
let tsub_inc_above x y = if y<x then TVar y else TVar (y+1)
(*
val tsub_inc : var -> Tot typ
let tsub_inc = tsub_inc_above 0
*)
val tsub_inc : var -> Tot typ
let tsub_inc a = TVar (a+1)
val tsub_dec : var -> Tot typ
let tsub_dec x = if x = 0 then TVar 0 else TVar (x-1)
val tsub_id :tsub
let tsub_id = fun x -> TVar x
let is_tvar (t:typ) : int = if TVar? t then 0 else 1
(********************************)
(* Global substitution function *)
(********************************)
(*The projectors for pairs were not working well with substitutions*)(*{{{*)
type sub =
| Sub : es:esub -> ts:tsub -> sub
opaque type renaming (s:sub) = (erenaming (Sub.es s)) /\ (trenaming (Sub.ts s))
opaque val is_renaming : s:sub -> GTot (n:int{( renaming s ==> n=0) /\
(~(renaming s) ==> n=1)})
let is_renaming s = (if excluded_middle (renaming s) then 0 else 1)
type value_sub (s:sub) = (value_esub (Sub.es s))
type vsub = s:sub{value_sub s}
let sub_einc_gen y = Sub (esub_inc_gen y) tsub_id
(*
let sub_einc = sub_einc_gen 1
*)
let sub_einc = Sub esub_inc tsub_id
let sub_edec = Sub esub_dec tsub_id
let sub_tinc = Sub esub_id tsub_inc
let sub_tdec = Sub esub_id tsub_dec
let sub_id = Sub esub_id tsub_id
val esubst : s:sub -> e:exp -> Pure exp (requires True)
(ensures (fun e' -> renaming s /\ EVar? e ==> EVar? e'))
(decreases %[is_evar e; is_renaming s;1; e])
val ecsubst : s:sub -> ec:econst -> Tot econst
(decreases %[1; is_renaming s; 1; ec])
val tsubst : s:sub -> t:typ -> Pure typ (requires True)
(ensures (fun t' -> renaming s /\ TVar? t ==> TVar? t'))
(decreases %[is_tvar t; is_renaming s;1; t])
val tcsubst : s:sub -> tc: tconst -> Tot tconst
(decreases %[1; is_renaming s; 1; tc])
val csubst : s:sub -> c:cmp -> Tot cmp
(decreases %[1; is_renaming s; 1; c])
val ksubst : s:sub -> k:knd -> Tot knd
(decreases %[1; is_renaming s; 1; k])
val sub_elam : s:sub -> Tot (r:sub{(renaming s ==> renaming r)})
(decreases %[1; is_renaming s; 0; EVar 0])
val sub_tlam : s:sub -> Tot(r:sub{renaming s ==> renaming r})
(decreases %[1; is_renaming s; 0; EVar 0])
let rec sub_elam s =
let esub_elam : x:var -> Tot(e:exp{renaming s ==> EVar? e}) =
fun x -> if x = 0 then EVar x
else esubst sub_einc (Sub.es s (x-1))
in
let tsub_elam : x:var -> Tot(t:typ{renaming s ==> TVar? t}) =
fun a -> tsubst sub_einc (Sub.ts s a)
in
Sub esub_elam tsub_elam
and sub_tlam s =
let esub_tlam : x:var -> Tot(e:exp{renaming s ==> EVar? e}) =
fun x -> esubst sub_tinc (Sub.es s x)
in
let tsub_tlam : a:var -> Tot(t:typ{renaming s ==> TVar? t}) =
fun a -> if a = 0 then TVar a
else tsubst sub_tinc (Sub.ts s (a-1))
in
Sub esub_tlam tsub_tlam
(*Substitution inside expressions*)
and esubst s e =
match e with
| EVar x -> Sub.es s x
| EConst ec -> EConst (ecsubst s ec)
| ELam t ebody -> ELam (tsubst s t) (esubst (sub_elam s) ebody)
| EIf0 g ethen eelse -> EIf0 (esubst s g) (esubst s ethen) (esubst s eelse)
| EApp e1 e2 -> EApp (esubst s e1) (esubst s e2)
(*Substitution inside expression constants*)
and ecsubst s ec =
match ec with
| EcFixPure tx t' t'' wp -> EcFixPure (tsubst s tx) (tsubst s t') (tsubst s t'') (tsubst s wp)
| EcFixOmega tx t' wp -> EcFixOmega (tsubst s tx) (tsubst s t') (tsubst s wp)
| _ -> ec
(*Substitution inside types*)
and tsubst s t =
match t with
| TVar a -> (Sub.ts s a)
| TConst c -> TConst (tcsubst s c)
| TArr t c ->
TArr (tsubst s t)
(csubst (sub_elam s) c)
| TTLam k tbody ->
TTLam (ksubst s k) (tsubst (sub_tlam s) tbody)
| TELam t tbody ->
TELam (tsubst s t) (tsubst (sub_elam s) tbody)
| TTApp t1 t2 -> TTApp (tsubst s t1) (tsubst s t2)
| TEApp t e -> TEApp (tsubst s t) (esubst s e)
and tcsubst s tc = match tc with
| TcEqT k -> TcEqT (ksubst s k)
| TcForallT k -> TcForallT (ksubst s k)
| _ -> tc
and csubst s c = let Cmp m t wp = c in
Cmp m (tsubst s t) (tsubst s wp)
(*Substitution inside kinds*)
and ksubst s k =
match k with
| KType -> KType
| KKArr k kbody ->
KKArr (ksubst s k) (ksubst (sub_tlam s) kbody)
| KTArr t kbody ->
(KTArr (tsubst s t) (ksubst (sub_elam s) kbody))
val subst_on_value : s:sub -> e:exp -> Lemma (requires (is_value e)) (ensures (is_value (esubst s e)))
let rec subst_on_value s e =
admit()(*
match e with
| EConst _ -> ()
| ELam _ _ -> ()
| EVar _ -> ()
| EIf0 _ _ _ -> ()
| EApp e1 e2 -> (subst_on_value s e2;
match e1 with
| EApp e11 e12 -> (subst_on_value s e12;
match e11 with
| EApp (EConst EcUpd) e112 -> (subst_on_value s e112)
| EConst c -> ()
)
| EConst c -> ()
| _ -> ()
)
*)
(*
val elam_on_value_sub : s:sub -> Lemma (requires (value_sub s)) (ensures (value_sub (sub_elam s)))
let elam_on_value_sub s =
let intro_lemma : (x:var -> Lemma (is_value (Sub.es (sub_elam s) x))) =
fun x -> match x with
| 0 -> ()
| n -> (subst_on_value sub_einc ((Sub.es s) (x-1)))
in
forall_intro intro_lemma
val tlam_on_value_sub : s:sub -> Lemma (requires (value_sub s)) (ensures (value_sub (sub_tlam s)))
let tlam_on_value_sub s =
let intro_lemma : (x:var -> Lemma (is_value (Sub.es (sub_tlam s) x))) =
fun x -> (subst_on_value sub_tinc ((Sub.es s) (x)))
in
forall_intro intro_lemma
*)
val esub_elam_at0 : s:sub -> Lemma (Sub.es (sub_elam s) 0 = EVar 0)
let esub_elam_at0 s = ()
(*BUG : without this normally neutral code, the file does not compile -> ??? *)
let plouf s t1 ebody = assert (sub_elam s == sub_elam s)
val etsubst : s:tsub -> e:exp -> Tot exp
let etsubst s e = esubst (Sub esub_id s) e
val ttsubst : s:tsub -> t:typ -> Tot typ
let ttsubst s t = tsubst (Sub esub_id s) t
val ktsubst : s:tsub -> k:knd -> Tot knd
let ktsubst s k = ksubst (Sub esub_id s) k
val eesubst : s:esub -> e:exp -> Tot exp
val tesubst : s:esub -> t:typ -> Tot typ
val kesubst : s:esub -> k:knd -> Tot knd
(*SF : it is better to avoid these functions that build new substitutions*)
let eesubst s e = esubst (Sub s tsub_id) e
let tesubst s t = tsubst (Sub s tsub_id) t
let kesubst s k = ksubst (Sub s tsub_id) k
(* Beta substitution for expressions *)
val esub_ebeta_gen : var -> exp -> Tot esub
let esub_ebeta_gen x e = fun y -> if y < x then (EVar y)
else if y = x then e
else (EVar (y-1))
val esub_ebeta : exp -> Tot esub
(* let esub_ebeta = esub_ebeta_gen 0 -- working around #311 *)
let esub_ebeta e = esub_ebeta_gen 0 e
val sub_ebeta : exp -> Tot sub
let sub_ebeta e = Sub (esub_ebeta e) (tsub_id)
val esubst_ebeta : exp -> exp -> Tot exp
let esubst_ebeta e = esubst (sub_ebeta e)
val csubst_ebeta : exp -> cmp -> Tot cmp
let csubst_ebeta e = csubst (sub_ebeta e)
val tsubst_ebeta : exp -> typ -> Tot typ
let tsubst_ebeta e = tsubst (sub_ebeta e)
val ksubst_ebeta : exp -> knd -> Tot knd
let ksubst_ebeta e = ksubst (sub_ebeta e)
let eesh = esubst sub_einc
let cesh = csubst sub_einc
let tesh = tsubst sub_einc
let kesh = ksubst sub_einc
let eeshd = esubst sub_edec
let ceshd = csubst sub_edec
let teshd = tsubst sub_edec
let keshd = ksubst sub_edec
(* Beta substitution for types *)
val tsub_tbeta_gen : var -> typ -> Tot tsub
let tsub_tbeta_gen x t = fun y -> if y < x then (TVar y)
else if y = x then t
else (TVar (y-1))
val tsub_tbeta : typ -> Tot tsub
(* let tsub_tbeta = tsub_tbeta_gen 0 -- working around #311 *)
let tsub_tbeta t = tsub_tbeta_gen 0 t
val sub_tbeta : typ -> Tot sub
let sub_tbeta t = Sub (esub_id) (tsub_tbeta t)
val esubst_tbeta : typ -> exp -> Tot exp
let esubst_tbeta t = esubst (sub_tbeta t)
val tsubst_tbeta : typ -> typ -> Tot typ
let tsubst_tbeta t = tsubst (sub_tbeta t)
val ksubst_tbeta : typ -> knd -> Tot knd
let ksubst_tbeta t = ksubst (sub_tbeta t)
let etsh = esubst sub_tinc
let ttsh = tsubst sub_tinc
let ktsh = ksubst sub_tinc
let etshd = esubst sub_tdec
let ttshd = tsubst sub_tdec
let ktshd = ksubst sub_tdec
val elam_on_sub_id : unit -> Lemma (sub_elam sub_id = sub_id)
let elam_on_sub_id x =
cut (FEq (Sub.es (sub_elam sub_id)) (Sub.es (sub_id)));
cut (FEq (Sub.ts (sub_elam sub_id)) (Sub.ts (sub_id)))
val tlam_on_sub_id : unit -> Lemma (sub_tlam sub_id = sub_id)
let tlam_on_sub_id x =
cut (FEq (Sub.es (sub_tlam sub_id)) (Sub.es (sub_id)));
cut (FEq (Sub.ts (sub_tlam sub_id)) (Sub.ts (sub_id)))
val esubst_with_sub_id : e:exp -> Lemma (esubst sub_id e = e)
val ecsubst_with_sub_id : ec:econst -> Lemma(ecsubst sub_id ec = ec)
val tsubst_with_sub_id : t:typ -> Lemma (tsubst sub_id t = t)
val tcsubst_with_sub_id : tc:tconst -> Lemma (tcsubst sub_id tc = tc)
val csubst_with_sub_id : c:cmp -> Lemma (csubst sub_id c = c)
val ksubst_with_sub_id : k:knd -> Lemma (ksubst sub_id k = k)
let rec esubst_with_sub_id e =
match e with
| EVar x -> ()
| EConst ec -> ecsubst_with_sub_id ec
| ELam t ebody -> (tsubst_with_sub_id t;
elam_on_sub_id ();
esubst_with_sub_id ebody)
| EIf0 g ethen eelse -> (
esubst_with_sub_id g;
esubst_with_sub_id ethen;
esubst_with_sub_id eelse)
| EApp e1 e2 -> (
esubst_with_sub_id e1;
esubst_with_sub_id e2
)
and ecsubst_with_sub_id ec =
match ec with
| EcFixPure tx t' t'' wp -> (
tsubst_with_sub_id tx;
tsubst_with_sub_id t';
tsubst_with_sub_id t'';
tsubst_with_sub_id wp
)
| EcFixOmega tx t' wp -> (
tsubst_with_sub_id tx;
tsubst_with_sub_id t';
tsubst_with_sub_id wp
)
| _ -> ()
and tsubst_with_sub_id t =
match t with
| TVar a -> ()
| TConst tc -> tcsubst_with_sub_id tc
| TArr t c -> (
tsubst_with_sub_id t;
elam_on_sub_id ();
csubst_with_sub_id c
)
| TTLam k tbody -> (
ksubst_with_sub_id k;
tlam_on_sub_id ();
tsubst_with_sub_id tbody
)
| TELam t tbody -> (
tsubst_with_sub_id t;
elam_on_sub_id ();
tsubst_with_sub_id tbody
)
| TTApp t1 t2 -> (
tsubst_with_sub_id t1;
tsubst_with_sub_id t2
)
| TEApp t e -> (
tsubst_with_sub_id t;
esubst_with_sub_id e
)
and tcsubst_with_sub_id tc =
match tc with
| TcEqT k
| TcForallT k -> ksubst_with_sub_id k
| _ -> ()
and csubst_with_sub_id c =
let Cmp m t wp = c in
tsubst_with_sub_id t; tsubst_with_sub_id wp
and ksubst_with_sub_id k =
match k with
| KType -> ()
| KKArr k kbody -> (
ksubst_with_sub_id k;
tlam_on_sub_id ();
ksubst_with_sub_id kbody
)
| KTArr t kbody -> (
tsubst_with_sub_id t;
elam_on_sub_id ();
ksubst_with_sub_id kbody
)
(********************************)
(* Composition of substitutions *)
(********************************)
//{{{
val sub_comp : s1:sub -> s2:sub -> Tot sub
let sub_comp s1 s2 =
Sub (fun x -> esubst s1 (Sub.es s2 x)) (fun a -> tsubst s1 (Sub.ts s2 a))
val vsub_comp : s1:vsub -> s2:vsub -> Lemma (value_sub (sub_comp s1 s2))
let vsub_comp s1 s2 =
let intro_lemma : x:var -> Lemma (is_value (Sub.es (sub_comp s1 s2) x)) = fun x ->
subst_on_value s1 (Sub.es s2 x)
in
forall_intro intro_lemma
val sub_comp_einc1 : s:sub -> x:var -> Lemma (Sub.es (sub_comp sub_einc s) x = Sub.es (sub_comp (sub_elam s) sub_einc) x)
let sub_comp_einc1 s x = ()
val sub_comp_einc2 : s:sub -> x:var -> Lemma (Sub.ts (sub_comp sub_einc s) x = Sub.ts (sub_comp (sub_elam s) sub_einc) x)
let sub_comp_einc2 s x = ()
val sub_comp_einc : s:sub -> Lemma (sub_comp sub_einc s = sub_comp (sub_elam s) sub_einc)
let sub_comp_einc s =
forall_intro (sub_comp_einc1 s);
forall_intro (sub_comp_einc2 s);
cut(FEq (Sub.es (sub_comp sub_einc s)) (Sub.es (sub_comp (sub_elam s) sub_einc)));
cut(FEq (Sub.ts (sub_comp sub_einc s)) (Sub.ts (sub_comp (sub_elam s) sub_einc)))
val sub_comp_tinc1 : s:sub -> x:var -> Lemma (Sub.es (sub_comp sub_tinc s) x = Sub.es (sub_comp (sub_tlam s) sub_tinc) x)
let sub_comp_tinc1 s x = ()
val sub_comp_tinc2 : s:sub -> x:var -> Lemma (Sub.ts (sub_comp sub_tinc s) x = Sub.ts (sub_comp (sub_tlam s) sub_tinc) x)
let sub_comp_tinc2 s x = ()
val sub_comp_tinc : s:sub -> Lemma (sub_comp sub_tinc s = sub_comp (sub_tlam s) sub_tinc)
let sub_comp_tinc s =
forall_intro (sub_comp_tinc1 s);
forall_intro (sub_comp_tinc2 s);
cut(FEq (Sub.es (sub_comp sub_tinc s)) (Sub.es (sub_comp (sub_tlam s) sub_tinc)));
cut(FEq (Sub.ts (sub_comp sub_tinc s)) (Sub.ts (sub_comp (sub_tlam s) sub_tinc)))
#set-options "--split_cases 1"
val esubst_comp : s1:sub -> s2:sub -> e:exp -> Lemma (esubst s1 (esubst s2 e) = esubst (sub_comp s1 s2) e)
(decreases %[is_evar e; is_renaming s1; is_renaming s2; 1; e])
val ecsubst_comp : s1:sub -> s2:sub -> ec:econst -> Lemma (ecsubst s1 (ecsubst s2 ec) = ecsubst (sub_comp s1 s2) ec)
(decreases %[1; is_renaming s1; is_renaming s2; 1; ec])
val tsubst_comp : s1:sub -> s2:sub -> t:typ -> Lemma (tsubst s1 (tsubst s2 t) = tsubst (sub_comp s1 s2) t)
(decreases %[is_tvar t; is_renaming s1; is_renaming s2; 1; t])
val tcsubst_comp : s1:sub -> s2:sub -> tc:tconst -> Lemma (tcsubst s1 (tcsubst s2 tc) = tcsubst (sub_comp s1 s2) tc)
(decreases %[1; is_renaming s1; is_renaming s2; 1; tc])
val csubst_comp : s1:sub -> s2:sub -> c:cmp -> Lemma (csubst s1 (csubst s2 c) = csubst (sub_comp s1 s2) c)
(decreases %[1; is_renaming s1; is_renaming s2; 1; c])
val ksubst_comp : s1:sub -> s2:sub -> k:knd -> Lemma (ksubst s1 (ksubst s2 k) = ksubst (sub_comp s1 s2) k)
(decreases %[1; is_renaming s1; is_renaming s2; 1; k])
val sub_elam_comp : s1:sub -> s2:sub -> Lemma (sub_comp (sub_elam s1) (sub_elam s2) = sub_elam (sub_comp s1 s2))
(decreases %[1; is_renaming s1; is_renaming s2; 0; EVar 0])
val sub_tlam_comp : s1:sub -> s2:sub -> Lemma (sub_comp (sub_tlam s1) (sub_tlam s2) = sub_tlam (sub_comp s1 s2))
(decreases %[1; is_renaming s1; is_renaming s2; 0; TVar 0])
let rec esubst_comp s1 s2 e =
match e with
| EVar x -> ()
| EConst ec -> ecsubst_comp s1 s2 ec
| ELam t ebody ->
(
tsubst_comp s1 s2 t;
esubst_comp (sub_elam s1) (sub_elam s2) ebody;
sub_elam_comp s1 s2
)
| EIf0 eg et ee -> (esubst_comp s1 s2 eg; esubst_comp s1 s2 et; esubst_comp s1 s2 ee)
| EApp e1 e2 -> (esubst_comp s1 s2 e1; esubst_comp s1 s2 e2)
and ecsubst_comp s1 s2 ec = match ec with
| EcFixPure tx t' t'' wp -> (tsubst_comp s1 s2 tx; tsubst_comp s1 s2 t'; tsubst_comp s1 s2 t''; tsubst_comp s1 s2 wp)
| EcFixOmega tx t' wp -> (tsubst_comp s1 s2 tx; tsubst_comp s1 s2 t'; tsubst_comp s1 s2 wp)
| _ -> ()
and tsubst_comp s1 s2 t =
match t with
| TVar x -> ()
| TConst tc -> tcsubst_comp s1 s2 tc
| TArr t c -> (
tsubst_comp s1 s2 t;
csubst_comp (sub_elam s1) (sub_elam s2) c;
sub_elam_comp s1 s2
)
| TTLam k tbody -> (
ksubst_comp s1 s2 k;
tsubst_comp (sub_tlam s1) (sub_tlam s2) tbody;
sub_tlam_comp s1 s2
)
| TELam t tbody -> (
tsubst_comp s1 s2 t;
tsubst_comp (sub_elam s1) (sub_elam s2) tbody;
sub_elam_comp s1 s2
)
| TTApp t1 t2 -> (
tsubst_comp s1 s2 t1;
tsubst_comp s1 s2 t2
)
| TEApp t e -> (
tsubst_comp s1 s2 t;
esubst_comp s1 s2 e
)
and tcsubst_comp s1 s2 tc =
match tc with
| TcForallT k -> ksubst_comp s1 s2 k
| TcEqT k -> ksubst_comp s1 s2 k
| _ -> ()
and csubst_comp s1 s2 c =
let Cmp m t wp = c in
(
tsubst_comp s1 s2 t;
tsubst_comp s1 s2 wp
)
and ksubst_comp s1 s2 k =
match k with
| KType -> ()
| KKArr karg kres -> (
ksubst_comp s1 s2 karg;
ksubst_comp (sub_tlam s1) (sub_tlam s2) kres;
sub_tlam_comp s1 s2
)
| KTArr targ kres -> (
tsubst_comp s1 s2 targ;
ksubst_comp (sub_elam s1) (sub_elam s2) kres;
sub_elam_comp s1 s2
)
and sub_elam_comp s1 s2 =
let sub_elam_comp1 : x : var -> Lemma (Sub.es (sub_comp (sub_elam s1) (sub_elam s2)) x = Sub.es (sub_elam (sub_comp s1 s2)) x) = fun x -> match x with
| 0 -> ()
| n -> (
(*esubst (sub_elam s1) (eesh (s2 (x-1))) *)
esubst_comp (sub_elam s1) sub_einc (Sub.es s2 (x-1));
(*esubst (sub_comp (sub_elam s1) sub_einc) (s2 (x-1)) *)
sub_comp_einc s1;
(*esubst (sub_comp sub_einc s1) (s2 (x-1))*)
esubst_comp sub_einc s1 (Sub.es s2 (x-1))
(*eesh (esubst (sub_comp s1 s2) (x-1)) = sub_elam (sub_comp s1 s2) x*)
)
in
let sub_elam_comp2 : a : var -> Lemma (Sub.ts (sub_comp (sub_elam s1) (sub_elam s2)) a = Sub.ts (sub_elam (sub_comp s1 s2)) a) = fun a ->
(*tsubst (sub_elam s1) (Sub.ts (sub_elam s2) a = tsubst (sub_elam s1) (tesh (s2 a))*)
tsubst_comp (sub_elam s1) (sub_einc) (Sub.ts s2 a);
sub_comp_einc s1;
tsubst_comp sub_einc s1 (Sub.ts s2 a)
in
forall_intro sub_elam_comp1;
forall_intro sub_elam_comp2;
cut(FEq (Sub.es (sub_comp (sub_elam s1) (sub_elam s2))) (Sub.es (sub_elam (sub_comp s1 s2))));
cut(FEq (Sub.ts (sub_comp (sub_elam s1) (sub_elam s2))) (Sub.ts (sub_elam (sub_comp s1 s2))))
and sub_tlam_comp s1 s2 =
let sub_tlam_comp1 : a : var -> Lemma (Sub.ts (sub_comp (sub_tlam s1) (sub_tlam s2)) a = Sub.ts (sub_tlam (sub_comp s1 s2)) a) = fun a -> match a with
| 0 -> ()
| n -> (
tsubst_comp (sub_tlam s1) sub_tinc (Sub.ts s2 (a-1));
sub_comp_tinc s1;
tsubst_comp sub_tinc s1 (Sub.ts s2 (a-1))
)
in
let sub_tlam_comp2 : x : var -> Lemma (Sub.es (sub_comp (sub_tlam s1) (sub_tlam s2)) x = Sub.es (sub_tlam (sub_comp s1 s2)) x) = fun x ->
(*tsubst (sub_elam s1) (Sub.ts (sub_elam s2) a = tsubst (sub_elam s1) (tesh (s2 a))*)
esubst_comp (sub_tlam s1) (sub_tinc) (Sub.es s2 x);
sub_comp_tinc s1;
esubst_comp sub_tinc s1 (Sub.es s2 x)
in
forall_intro sub_tlam_comp1;
forall_intro sub_tlam_comp2;
cut(FEq (Sub.es (sub_comp (sub_tlam s1) (sub_tlam s2))) (Sub.es (sub_tlam (sub_comp s1 s2))));
cut(FEq (Sub.ts (sub_comp (sub_tlam s1) (sub_tlam s2))) (Sub.ts (sub_tlam (sub_comp s1 s2))))
#reset-options
type sub_equal (s1:sub) (s2:sub) = (FEq (Sub.es s1) (Sub.es s2) /\ FEq (Sub.ts s1) (Sub.ts s2) )
val sub_ext : s1:sub -> s2:sub{sub_equal s1 s2} -> Lemma(s1 = s2)
let sub_ext s1 s2 = ()
val edec_einc_comp : unit -> Lemma ((sub_comp sub_edec sub_einc) = sub_id)
let edec_einc_comp () = sub_ext (sub_comp sub_edec sub_einc) sub_id
val tdec_tinc_comp : unit -> Lemma ((sub_comp sub_tdec sub_tinc) = sub_id)
let tdec_tinc_comp () = sub_ext (sub_comp sub_tdec sub_tinc) sub_id
//}}}
(***********************************************)
(* Temporary zone to prove substitution lemmas *)
(***********************************************)
(****************************)
(* Derived logic constants *)
(****************************)
let timpl t1 t2 = tforalle t1 (tesh t2)
let tnot t = timpl t tfalse
let ttrue = tnot tfalse
let tor t1 t2 = timpl (tnot t1) t2
(*************************************)
(* Common substitution functions *)
(*************************************)
(*TODO:Settle down the substitution strategy*)
(*
val eshift_up_above : ei:nat -> ti:nat ->
eplus:nat -> tplus:nat ->
e:exp -> Tot exp
val tshift_up_above : ei:nat -> ti:nat ->
eplus:nat -> tplus:nat ->
t:typ -> Tot typ
val kshift_up_above : ei:nat -> ti:nat ->
eplus:nat -> tplus:nat ->
t:typ -> Tot typ
*)
(***********************)
(* Heap manipulation *)
(***********************)
val upd_heap : l:loc -> i:int -> h:heap -> Tot heap
let upd_heap l i h =
fun x -> if x = l then i else h x
(********************************************)
(* Reduction for types and pure expressions *)
(********************************************)
//{{{
type tstep : typ -> typ -> Type =
| TsEBeta : tx:typ ->
t:typ ->
e:exp ->
tstep (TEApp (TELam tx t) e) (tsubst_ebeta e t)
| TsTBeta : k:knd ->
t:typ ->
t':typ ->
tstep (TTApp (TTLam k t) t') (tsubst_tbeta t' t)
| TsArrT1 : #t1:typ->
#t1':typ->
c:cmp ->
=ht:tstep t1 t1' ->
tstep (TArr t1 c) (TArr t1' c)
| TsTAppT1 : #t1:typ ->
#t1':typ ->
t2 : typ ->
=ht:tstep t1 t1' ->
tstep (TTApp t1 t2) (TTApp t1' t2)
| TsTAppT2 : t1:typ ->
#t2:typ ->
#t2':typ ->
=ht:tstep t2 t2' ->
tstep (TTApp t1 t2) (TTApp t1 t2')
| TsEAppT : #t:typ ->
#t':typ ->
e:exp ->
=ht:tstep t t' ->
tstep (TEApp t e) (TEApp t' e)
| TsEAppE : t:typ ->
#e:exp ->
#e':exp ->
=he:epstep e e' ->
tstep (TEApp t e) (TEApp t e')
| TsTLamT : k:knd ->
#t:typ ->
#t':typ ->
=ht:tstep t t' ->
tstep (TTLam k t) (TTLam k t')
| TsELamT1 : #t1:typ ->
#t1':typ ->
t2:typ ->
=ht:tstep t1 t1' ->
tstep (TELam t1 t2) (TELam t1' t2)
(*Why do the last two rules reduce different part of the term ?
Why do we have TTLam k t ~> TTLam k t' and not TELam t1 t2 ~> TELam t1 t2' ? *)
and epstep : exp -> exp -> Type =
| PsBeta : t:typ ->
ebody:exp ->
e:exp ->
epstep (EApp (ELam t ebody) e) (esubst_ebeta e ebody)
| PsIf0 : e1:exp ->
e2:exp ->
epstep (EIf0 (eint 0) e1 e2) e1
| PsIfS : i:int{i<>0} ->
e1:exp ->
e2:exp ->
epstep (EIf0 (eint i) e1 e2) e2
| PsAppE1 : #e1:exp ->
#e1':exp ->
e2:exp ->
=ht:epstep e1 e1' ->
epstep (EApp e1 e2) (EApp e1' e2)
| PsAppE2 : e1:exp ->
#e2:exp ->
#e2':exp ->
=ht:epstep e2 e2' ->
epstep (EApp e1 e2) (EApp e1 e2')
| PsLamT : #t:typ ->
#t':typ ->
ebody:exp ->
=ht:tstep t t' ->
epstep (ELam t ebody) (ELam t' ebody)
| PsIf0E0 : #e0:exp ->
#e0':exp ->
ethen:exp ->
eelse:exp ->
=ht:epstep e0 e0' ->
epstep (EIf0 e0 ethen eelse) (EIf0 e0' ethen eelse)
| PsFixPure : tx:typ -> t':typ -> t'':typ -> wp:typ ->
d:exp -> f:exp -> v:value ->
epstep (eapp3 (EConst (EcFixPure tx t' t'' wp)) d f v)
(eapp2 f v (eapp2 (EConst (EcFixPure tx t' t'' wp)) d f))
| PsUpd : h:heap -> l:loc -> i:int ->
epstep (eapp3 (EConst EcUpd) (eheap h) (eloc l) (eint i)) (eheap (upd_heap l i h))
| PsSel : h:heap -> l:loc ->
epstep (eapp2 (EConst EcSel) (eheap h) (eloc l)) (eint (h l))
type cfg =
| Cfg : h:heap -> e:exp -> cfg
type eistep : cfg -> cfg -> Type =
| IsRd : h:heap ->