-
Notifications
You must be signed in to change notification settings - Fork 84
/
inferPropsScript.sml
4342 lines (4143 loc) · 133 KB
/
inferPropsScript.sml
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
(*
Various lemmas that are handy in the soundness and completeness
proofs of the type inferencer.
*)
open preamble;
open namespacePropsTheory typeSystemTheory astTheory semanticPrimitivesTheory inferTheory unifyTheory infer_tTheory;
open astPropsTheory typeSysPropsTheory;
val _ = new_theory "inferProps";
Theorem ienv_unchanged[simp]:
(ienv with inf_v := ienv.inf_v) = ienv ∧
(ienv with inf_c := ienv.inf_c) = ienv ∧
(ienv with inf_t := ienv.inf_t) = ienv
Proof
rw [inf_env_component_equality]
QED
Theorem extend_dec_ienv_empty:
!ienv.
extend_dec_ienv ienv <| inf_v := nsEmpty; inf_c := nsEmpty; inf_t := nsEmpty |> = ienv ∧
extend_dec_ienv <| inf_v := nsEmpty; inf_c := nsEmpty; inf_t := nsEmpty |> ienv = ienv
Proof
rw [extend_dec_ienv_def, inf_env_component_equality]
QED
(* ---------- Facts about deBruijn increment ---------- *)
Theorem infer_deBruijn_inc0:
!(n:num) t. infer_deBruijn_inc 0 t = t
Proof
ho_match_mp_tac infer_deBruijn_inc_ind >>
rw [infer_deBruijn_inc_def] >>
induct_on `ts` >>
rw []
QED
Theorem infer_deBruijn_inc0_id:
infer_deBruijn_inc 0 = (\x.x)
Proof
metis_tac [infer_deBruijn_inc0]
QED
Theorem t_vars_inc:
!tvs t. t_vars (infer_deBruijn_inc tvs t) = t_vars t
Proof
ho_match_mp_tac infer_deBruijn_inc_ind >>
rw [t_vars_def, encode_infer_t_def, infer_deBruijn_inc_def] >>
induct_on `ts` >>
rw [encode_infer_t_def]
QED
Theorem inc_wfs:
!tvs s. t_wfs s ⇒ t_wfs (infer_deBruijn_inc tvs o_f s)
Proof
rw [t_wfs_eqn] >>
`t_vR s = t_vR (infer_deBruijn_inc tvs o_f s)`
by (rw [FLOOKUP_o_f, FUN_EQ_THM, t_vR_eqn] >>
full_case_tac >>
rw [t_vars_inc]) >>
metis_tac []
QED
Theorem vwalk_inc:
!s tvs n.
t_wfs s
⇒
t_vwalk (infer_deBruijn_inc tvs o_f s) n = infer_deBruijn_inc tvs (t_vwalk s n)
Proof
rw [] >>
imp_res_tac (DISCH_ALL t_vwalk_ind) >>
`t_wfs (infer_deBruijn_inc tvs o_f s)` by metis_tac [inc_wfs] >>
rw [] >>
Q.SPEC_TAC (`n`, `n`) >>
qpat_x_assum `!p. (!v. q v ⇒ p v) ⇒ !v. p v` ho_match_mp_tac >>
rw [] >>
imp_res_tac t_vwalk_eqn >>
once_asm_rewrite_tac [] >>
pop_assum (fn _ => all_tac) >>
pop_assum (fn _ => all_tac) >>
cases_on `FLOOKUP s n` >>
rw [FLOOKUP_o_f, infer_deBruijn_inc_def] >>
cases_on `x` >>
rw [infer_deBruijn_inc_def]
QED
Theorem walk_inc:
!s tvs t.
t_wfs s
⇒
t_walk (infer_deBruijn_inc tvs o_f s) (infer_deBruijn_inc tvs t) = infer_deBruijn_inc tvs (t_walk s t)
Proof
rw [] >>
cases_on `t` >>
rw [t_walk_eqn, infer_deBruijn_inc_def, vwalk_inc]
QED
Theorem walkstar_inc:
!tvs s t.
t_wfs s ⇒
(t_walkstar (infer_deBruijn_inc tvs o_f s) (infer_deBruijn_inc tvs t) =
infer_deBruijn_inc tvs (t_walkstar s t))
Proof
rw [] >>
imp_res_tac t_walkstar_ind >>
Q.SPEC_TAC (`t`, `t`) >>
pop_assum ho_match_mp_tac >>
rw [] >>
rw [walk_inc] >>
cases_on `t_walk s t` >>
rw [infer_deBruijn_inc_def] >>
imp_res_tac inc_wfs >>
rw [t_walkstar_eqn,infer_deBruijn_inc_def, walk_inc] >>
pop_assum (fn _ => all_tac) >>
pop_assum mp_tac >>
pop_assum (fn _ => all_tac) >>
induct_on `l` >>
rw [] >>
fs []
QED
Theorem walkstar_inc2:
!tvs s n.
t_wfs s ⇒
(t_walkstar (infer_deBruijn_inc tvs o_f s) (Infer_Tuvar n) =
infer_deBruijn_inc tvs (t_walkstar s (Infer_Tuvar n)))
Proof
rw [GSYM walkstar_inc, infer_deBruijn_inc_def]
QED
(* ---------- Type substitution ---------- *)
Theorem subst_infer_subst_swap:
(!t tvs s uvar.
t_wfs s ⇒
(t_walkstar s (infer_type_subst (ZIP (tvs, MAP (λn. Infer_Tuvar (uvar + n)) (COUNT_LIST (LENGTH tvs)))) t)
=
infer_type_subst (ZIP (tvs, MAP (λn. t_walkstar s (Infer_Tuvar (uvar + n))) (COUNT_LIST (LENGTH tvs)))) t)) ∧
(!ts tvs s uvar.
t_wfs s ⇒
(MAP (t_walkstar s) (MAP (infer_type_subst (ZIP (tvs, MAP (λn. Infer_Tuvar (uvar + n)) (COUNT_LIST (LENGTH tvs))))) ts)
=
MAP (infer_type_subst (ZIP (tvs, MAP (λn. t_walkstar s (Infer_Tuvar (uvar + n))) (COUNT_LIST (LENGTH tvs))))) ts))
Proof
ho_match_mp_tac t_induction >>
rw [type_subst_def, infer_type_subst_alt, t_walkstar_eqn1]
>- (every_case_tac >>
rw [t_walkstar_eqn1] >>
fs [ALOOKUP_FAILS] >>
fs [MAP_ZIP, LENGTH_COUNT_LIST, ALOOKUP_ZIP_MAP_SND] >>
imp_res_tac ALOOKUP_MEM >>
fs [MEM_ZIP, LENGTH_COUNT_LIST] >>
metis_tac [])
QED
val infer_t_induction = infer_tTheory.infer_t_induction;
Theorem infer_subst_FEMPTY:
(!t. infer_subst FEMPTY t = t) ∧
(!ts. MAP (infer_subst FEMPTY) ts = ts)
Proof
ho_match_mp_tac infer_t_induction >>
rw [SUBSET_DEF, infer_subst_def] >>
metis_tac []
QED
Theorem infer_subst_submap:
(!t s1 s2 m.
s1 SUBMAP s2 ∧
{uv | uv ∈ t_vars t ∧ m ≤ uv} ⊆ FDOM s1 ∧
(!uv. uv ∈ FDOM s2 DIFF FDOM s1 ⇒ m ≤ uv)
⇒
(infer_subst s1 t = infer_subst s2 t)) ∧
(!ts s1 s2 m.
s1 SUBMAP s2 ∧
{uv | ?s. uv ∈ s ∧ MEM s (MAP t_vars ts) ∧ m ≤ uv} ⊆ FDOM s1 ∧
(!uv. uv ∈ FDOM s2 DIFF FDOM s1 ⇒ m ≤ uv)
⇒
(MAP (infer_subst s1) ts = MAP (infer_subst s2) ts))
Proof
ho_match_mp_tac infer_t_induction >>
rw [SUBSET_DEF, infer_subst_def, t_vars_eqn]
>-
metis_tac []
>- (
full_case_tac >>
full_case_tac >>
rw [] >>
fs [SUBMAP_DEF, FLOOKUP_DEF] >>
metis_tac [])
>-
metis_tac []
>>
metis_tac []
QED
Theorem generalise_list_length:
∀a b c d e f g.
generalise_list a b c d = (e,f,g) ⇒ LENGTH g = LENGTH d
Proof
Induct_on`d`>>fs[generalise_def]>>rw[]>>
pairarg_tac>>fs[]>>
pairarg_tac>>fs[]>>
res_tac>>fs[]>>rveq>>fs[]
QED
Theorem generalise_subst:
(!t m n s tvs s' t'.
(generalise m n s t = (tvs, s', t'))
⇒
(s SUBMAP s') ∧
(FDOM s' = FDOM s ∪ { uv | uv ∈ t_vars t ∧ m ≤ uv }) ∧
(!uv. uv ∈ FDOM s' DIFF FDOM s ⇒ ∃tv. (FAPPLY s' uv = tv) ∧ n ≤ tv ∧ tv < tvs + n) ∧
(!uv. uv ∈ t_vars t' ⇒ uv < m) ∧
(infer_subst s' t = infer_subst s t')) ∧
(!ts m n s tvs s' ts'.
(generalise_list m n s ts = (tvs, s', ts'))
⇒
(s SUBMAP s') ∧
(FDOM s' = FDOM s ∪ { uv | uv ∈ BIGUNION (set (MAP t_vars ts)) ∧ m ≤ uv }) ∧
(!uv. uv ∈ FDOM s' DIFF FDOM s ⇒ ∃tv. (FAPPLY s' uv = tv) ∧ n ≤ tv ∧ tv < tvs + n) ∧
(!uv. uv ∈ BIGUNION (set (MAP t_vars ts')) ⇒ uv < m) ∧
(MAP (infer_subst s') ts = MAP (infer_subst s) ts'))
Proof
Induct >>
SIMP_TAC (srw_ss()) [t_vars_eqn, generalise_def, infer_subst_def]
>- (
REPEAT GEN_TAC >>
STRIP_TAC >>
fs[]>>pairarg_tac>>fs[]>>
first_x_assum old_drule>> simp[]>>
rveq>>fs[]>>
rw [EXTENSION, infer_subst_def] >>
fs [t_vars_eqn] >>
metis_tac [])
>- (
rw [] >>
every_case_tac >>
fs [] >>
rw [] >>
rw [FLOOKUP_DEF, EXTENSION] >>
TRY (EQ_TAC) >>
rw [] >>
fs [FLOOKUP_DEF, infer_subst_def, t_vars_eqn] >>
decide_tac)
>>
REPEAT GEN_TAC >>
STRIP_TAC >>
`?tvs s' t'. generalise m n s t = (tvs, s', t')`
by (cases_on `generalise m n s t` >>
rw [] >>
cases_on `r` >>
fs []) >>
fs [LET_THM] >>
`?tvs s' ts'. generalise_list m (tvs'+n) s'' ts = (tvs, s', ts')`
by (cases_on `generalise_list m (tvs'+n) s'' ts` >>
rw [] >>
cases_on `r` >>
fs []) >>
fs [LET_THM] >>
qpat_x_assum `!m'. P m'`
(mp_tac o Q.SPECL [`m`, `tvs'+n`, `s''`, `tvs''`, `s'''`, `ts''`]) >>
qpat_x_assum `!m'. P m'`
(mp_tac o Q.SPECL [`m`, `n`, `s`, `tvs'`, `s''`, `t'`]) >>
rw [INTER_UNION]
>-
metis_tac [SUBMAP_TRANS]
>- (rw [EXTENSION] >> metis_tac [])
>- (
`uv ∈ FDOM s''` by fs [] >>
res_tac >>
rw [INTER_UNION] >>
fs [SUBMAP_DEF])
>- (
`uv ∈ FDOM s''` by fs [] >>
res_tac >>
rw [INTER_UNION] >>
fs [SUBMAP_DEF] >>
res_tac >>
decide_tac)
>- (
`uv ∈ FDOM s'` by (fs [] >> metis_tac []) >>
cases_on `uv ∈ t_vars t` >>
rw [] >>
res_tac >>
rw [INTER_UNION] >>
fs [SUBMAP_DEF] >>
res_tac >>
decide_tac)
>- (
`uv ∈ FDOM s'` by (fs [] >> metis_tac []) >>
cases_on `uv ∈ t_vars t` >>
rw [] >>
`uv ∈ FDOM s''` by (fs [] >> metis_tac []) >>
res_tac >>
rw [INTER_UNION] >>
fs [SUBMAP_DEF] >>
res_tac >>
decide_tac)
>- metis_tac []
>- metis_tac []
>- (
`{uv | uv ∈ t_vars t ∧ m ≤ uv} ⊆ FDOM s'' ∧
(!uv. uv ∈ FDOM s' DIFF FDOM s'' ⇒ m ≤ uv)` by
rw [SUBSET_DEF] >>
metis_tac [infer_subst_submap])
>>
`{uv | ∃s. uv ∈ s ∧ MEM s (MAP t_vars ts'') ∧ m ≤ uv} ⊆ FDOM s ∧ (!uv. uv ∈ FDOM s'' DIFF FDOM s ⇒ m ≤ uv)` by
(rw [SUBSET_DEF] >>
`¬(x < m)` by decide_tac >>
metis_tac []) >>
metis_tac [infer_subst_submap]
QED
Theorem generalise_subst_empty:
!n ts tvs s ts'.
(generalise_list 0 n FEMPTY ts = (tvs, s, ts'))
⇒
(FDOM s = { uv | uv ∈ BIGUNION (set (MAP t_vars ts)) }) ∧
(!uv. uv ∈ FDOM s ⇒ ∃tv. (FAPPLY s uv = tv) ∧ tv < tvs + n) ∧
(BIGUNION (set (MAP t_vars ts')) = {}) ∧
(ts' = MAP (infer_subst s) ts)
Proof
rw [] >>
imp_res_tac generalise_subst >>
fs [] >>
rw []
>- (
rw [BIGUNION, EXTENSION] >>
metis_tac [])
>- (
fs [EXTENSION] >>
metis_tac [])
>- (
cases_on `ts'` >>
rw [] >>
rw [EXTENSION] >>
eq_tac >>
rw [] >>
fs [t_vars_eqn] >>
metis_tac [])
>>
metis_tac [infer_subst_FEMPTY]
QED
(* ---------- Dealing with the monad ---------- *)
(* TODO: update *)
Theorem infer_st_rewrs:
(!st. (st with next_uvar := st.next_uvar) = st) ∧
(!st. (st with subst := st.subst) = st) ∧
(!st s. (st with subst := s).subst = s) ∧
(!st s. (st with subst := s).next_uvar = st.next_uvar) ∧
(!st uv. (st with next_uvar := uv).next_uvar = uv) ∧
(!st uv. (st with next_uvar := uv).subst = st.subst)
Proof
rw [] >>
cases_on `st` >>
rw [infer_st_component_equality]
QED
Triviality st_ex_return_success:
!v st v' st'.
(st_ex_return v st = (Success v', st')) =
((v = v') ∧ (st = st'))
Proof
rw [st_ex_return_def]
QED
Triviality st_ex_bind_success:
!f g st st' v.
(st_ex_bind f g st = (Success v, st')) =
?v' st''. (f st = (Success v', st'')) /\ (g v' st'' = (Success v, st'))
Proof
rw [st_ex_bind_def] >>
cases_on `f st` >>
rw [] >>
cases_on `q` >>
rw []
QED
Triviality fresh_uvar_success:
!st t st'.
(fresh_uvar st = (Success t, st')) =
((t = Infer_Tuvar st.next_uvar) ∧
(st' = st with next_uvar := st.next_uvar + 1))
Proof
rw [fresh_uvar_def] >>
metis_tac []
QED
Theorem n_fresh_uvar_success:
!n st ts st'.
(n_fresh_uvar n st = (Success ts, st')) =
((ts = MAP (\n. Infer_Tuvar (st.next_uvar + n)) (COUNT_LIST n)) ∧
(st' = st with next_uvar := st.next_uvar + n))
Proof
ho_match_mp_tac n_fresh_uvar_ind >>
rw [] >>
rw [st_ex_return_success, Once n_fresh_uvar_def, COUNT_LIST_SNOC,
st_ex_bind_success, fresh_uvar_success, infer_st_rewrs] >-
metis_tac [] >>
fs [] >>
srw_tac [ARITH_ss] [] >>
rw [count_list_sub1, MAP_APPEND, MAP_MAP_o, combinTheory.o_DEF] >>
eq_tac >>
srw_tac [ARITH_ss] [arithmeticTheory.ADD1]
QED
Triviality apply_subst_success:
!t1 st1 t2 st2.
(apply_subst t1 st1 = (Success t2, st2))
=
((st2 = st1) ∧
(t2 = t_walkstar st1.subst t1))
Proof
rw [st_ex_return_def, st_ex_bind_def, LET_THM, apply_subst_def, read_def] >>
eq_tac >>
rw []
QED
Theorem add_constraint_success:
!l t1 t2 st st' x.
(add_constraint l t1 t2 st = (Success x, st'))
=
((x = ()) ∧ (?s. (t_unify st.subst t1 t2 = SOME s) ∧ (st' = st with subst := s)))
Proof
rw [add_constraint_def] >>
full_case_tac >>
metis_tac []
QED
Triviality add_constraints_success:
!l ts1 ts2 st st' x.
(add_constraints l ts1 ts2 st = (Success x, st'))
=
((LENGTH ts1 = LENGTH ts2) ∧
((x = ()) ∧
(st.next_uvar = st'.next_uvar) ∧
(st.next_id = st'.next_id) ∧
pure_add_constraints st.subst (ZIP (ts1,ts2)) st'.subst))
Proof
ho_match_mp_tac add_constraints_ind >>
rw [add_constraints_def, pure_add_constraints_def, st_ex_return_success,
failwith_def, st_ex_bind_success, add_constraint_success] >>
TRY (cases_on `x`) >>
rw [pure_add_constraints_def] >-
metis_tac [infer_st_component_equality] >>
eq_tac >>
rw [] >>
cases_on `t_unify st.subst t1 t2` >>
fs []
QED
Triviality add_constraints_nil2_success:
(add_constraints l ts1 [] st = (Success x, st'))
= (ts1 = [] /\ st = st')
Proof
Cases_on `ts1` \\ simp [add_constraints_def]
\\ simp [failwith_def, st_ex_bind_success, st_ex_return_success]
QED
Triviality add_constraints_cons2_success:
(add_constraints l ts1 (t2 :: ts2) st = (Success x, st'))
= (?t1 tl1 st''. ts1 = t1 :: tl1 /\
add_constraint l t1 t2 st = (Success (), st'') /\
add_constraints l tl1 ts2 st'' = (Success x, st'))
Proof
Cases_on `ts1` \\ simp [add_constraints_def]
\\ simp [failwith_def, st_ex_bind_success, st_ex_return_success]
QED
Triviality failwith_success:
!l m st v st'. (failwith l m st = (Success v, st')) = F
Proof
rw [failwith_def]
QED
Triviality lookup_st_ex_success:
!loc x err l st v st'.
(lookup_st_ex loc err x l st = (Success v, st'))
=
((nsLookup l x = SOME v) ∧ (st = st'))
Proof
rw [lookup_st_ex_def, failwith_def, st_ex_return_success]
>> every_case_tac
QED
val op_data = {nchotomy = op_nchotomy, case_def = op_case_def};
val op_case_eq = prove_case_eq_thm op_data;
val op_case_rand = prove_case_rand_thm op_data;
val list_data = {nchotomy = list_nchotomy, case_def = list_case_def}
val list_case_eq = prove_case_eq_thm list_data;
val list_case_rand = prove_case_rand_thm list_data;
val bool_data = {nchotomy = TypeBase.nchotomy_of bool,
case_def = TypeBase.case_def_of bool}
val bool_case_rand = prove_case_rand_thm bool_data;
fun mk_case_rator case_rand =
case_rand
|> GEN (case_rand |> concl |> lhs |> rator)
|> ISPEC (let val z = genvar(gen_tyvar())
val r = genvar(type_of z --> gen_tyvar())
in mk_abs(r,mk_comb(r,z)) end)
|> BETA_RULE
val list_case_rator = mk_case_rator list_case_rand
val op_case_rator = mk_case_rator op_case_rand
val bool_case_rator = mk_case_rator bool_case_rand
Theorem UNCURRY_rator:
UNCURRY f x y = UNCURRY (\a b. f a b y) x
Proof
Cases_on `x` \\ simp []
QED
Triviality constrain_op_op_case:
constrain_op l op ts st = (case op of
Opapp => let x = () in constrain_op l op ts st
| _ => constrain_op l op ts st)
Proof
CASE_TAC \\ simp []
QED
val constrain_op_success =
``(constrain_op l op ts st = (Success v, st'))``
|> (REWRITE_CONV [Once constrain_op_op_case, op_case_eq]
THENC SIMP_CONV (srw_ss () ++ CONJ_ss) [constrain_op_dtcase_def,
op_simple_constraints_def, LET_THM, bool_case_eq,
st_ex_bind_success,st_ex_return_success,
add_constraint_success,failwith_success,
add_constraints_cons2_success,
add_constraints_nil2_success,
list_case_rator, list_case_eq, bool_case_rator, bool_case_eq,
PULL_EXISTS]
)
Theorem constrain_op_success =
constrain_op_success
Triviality get_next_uvar_success:
!st v st'.
(get_next_uvar st = (Success v, st'))
=
((v = st.next_uvar) ∧ (st = st'))
Proof
rw [get_next_uvar_def] >>
metis_tac []
QED
val apply_subst_list_success =
SIMP_CONV (srw_ss()) [apply_subst_list_def, LET_THM]
``(apply_subst_list ts st = (Success v, st'))``
Triviality guard_success:
∀P l m st v st'.
(guard P l m st = (Success v, st'))
=
(P ∧ (v = ()) ∧ (st = st'))
Proof
rw [guard_def, st_ex_return_def, failwith_def] >>
metis_tac []
QED
Theorem check_dups_success:
!l f ls s r s'.
check_dups l f ls s = (Success r, s')
⇔
s' = s ∧ ALL_DISTINCT ls
Proof
Induct_on `ls` >>
rw [check_dups_def, st_ex_return_def, failwith_def] >>
metis_tac []
QED
Theorem type_name_check_subst_success:
(!t l f tenvT tvs r (s:'a) s'.
type_name_check_subst l f tenvT tvs t s = (Success r, s')
⇔
s = s' ∧ r = type_name_subst tenvT t ∧
check_freevars_ast tvs t ∧ check_type_names tenvT t) ∧
(!ts l f tenvT tvs r (s:'a) s'.
type_name_check_subst_list l f tenvT tvs ts s = (Success r, s')
⇔
s = s' ∧ r = MAP (type_name_subst tenvT) ts ∧
EVERY (check_freevars_ast tvs) ts ∧ EVERY (check_type_names tenvT) ts)
Proof
Induct >>
rw [type_name_check_subst_def, st_ex_bind_def, guard_def, st_ex_return_def,
check_freevars_ast_def, check_type_names_def, failwith_def,
type_name_subst_def] >>
every_case_tac >>
fs [] >>
rw [] >>
TRY pairarg_tac >>
fs [] >>
every_case_tac >>
fs [lookup_st_ex_success, lookup_st_ex_def] >>
metis_tac [exc_distinct, PAIR_EQ, NOT_EVERY]
QED
Theorem check_ctor_types_success:
!l tenvT tvs ts s s'.
check_ctor_types l tenvT tvs ts s = (Success (),s') ⇔
s = s' ∧
EVERY (λ(cn,ts). EVERY (check_freevars_ast tvs) ts ∧
EVERY (check_type_names tenvT) ts) ts
Proof
Induct_on `ts` >>
rw [check_ctor_types_def, st_ex_return_def] >>
PairCases_on `h` >>
rw [check_ctor_types_def, st_ex_bind_def] >>
every_case_tac >>
fs [type_name_check_subst_success] >>
CCONTR_TAC >>
fs [combinTheory.o_DEF] >>
metis_tac [exc_distinct, PAIR_EQ, type_name_check_subst_success]
QED
Theorem check_dup_ctors_thm:
check_dup_ctors (tvs,tn,condefs) = ALL_DISTINCT (MAP FST condefs)
Proof
rw [check_dup_ctors_def] >>
induct_on `condefs` >>
rw [] >>
pairarg_tac >>
fs [] >>
eq_tac >>
rw [] >>
induct_on `condefs` >>
rw [] >>
pairarg_tac >>
fs []
QED
Theorem check_ctors_success:
!l tenvT tds s s'.
ALL_DISTINCT (MAP (FST o SND) tds) ⇒
(check_ctors l tenvT tds s = (Success (),s') ⇔
s' = s ∧ check_ctor_tenv tenvT tds)
Proof
Induct_on `tds` >>
rw [] >>
TRY (PairCases_on `h`) >>
fs [check_ctor_tenv_def, check_type_definition_def, st_ex_bind_def,
check_ctors_def, st_ex_return_def, check_dup_ctors_thm]
>- metis_tac [] >>
every_case_tac >>
fs [check_dups_success, st_ex_return_def, check_type_definition_def,
check_ctor_types_success] >>
fs [check_dups_def, st_ex_return_def, st_ex_bind_def, LAMBDA_PROD,
combinTheory.o_DEF] >>
CCONTR_TAC >>
fs [combinTheory.o_DEF, ETA_THM] >>
rw [] >>
TRY (
Induct_on `h2` >>
fs [check_dups_def, st_ex_return_def] >>
rw [] >>
NO_TAC)
>- metis_tac [exc_distinct, PAIR_EQ, check_dups_success]
>- (
Induct_on `h2` >>
fs [] >>
rw [check_ctor_types_def, st_ex_return_def] >>
PairCases_on `h` >>
fs [check_ctor_types_def, st_ex_return_def, st_ex_bind_def] >>
every_case_tac >>
fs [type_name_check_subst_success] >>
rw [] >>
metis_tac [NOT_EVERY, exc_distinct, PAIR_EQ, type_name_check_subst_success])
>- metis_tac [exc_distinct, PAIR_EQ, check_dups_success]
>- metis_tac [exc_distinct, PAIR_EQ, check_dups_success]
>- metis_tac [exc_distinct, PAIR_EQ, check_dups_success]
QED
Theorem check_type_definition_success:
!l tenvT tds s r s'.
check_type_definition l tenvT tds s = (Success r, s')
⇔
s' = s ∧ check_ctor_tenv tenvT tds
Proof
rw [check_type_definition_def, st_ex_bind_def] >>
every_case_tac >>
fs [check_dups_success]
>- metis_tac [check_ctors_success] >>
`~ALL_DISTINCT (MAP (FST ∘ SND) tds)`
by metis_tac [exc_distinct, PAIR_EQ, check_dups_success] >>
pop_assum mp_tac >>
pop_assum kall_tac >>
Induct_on `tds` >>
rw [] >>
PairCases_on `h` >>
rw [check_ctor_tenv_def] >>
fs [LAMBDA_PROD, combinTheory.o_DEF]
QED
Triviality option_case_eq:
!opt f g v st st'.
((case opt of NONE => f | SOME x => g x) st = (Success v, st')) =
(((opt = NONE) ∧ (f st = (Success v, st'))) ∨ (?x. (opt = SOME x) ∧ (g x st = (Success v, st'))))
Proof
rw [] >>
cases_on `opt` >>
fs []
QED
val success_eqns =
LIST_CONJ [st_ex_return_success, st_ex_bind_success, fresh_uvar_success,
apply_subst_success, add_constraint_success, lookup_st_ex_success,
n_fresh_uvar_success, failwith_success, add_constraints_success,
oneTheory.one, check_type_definition_success,
get_next_uvar_success, apply_subst_list_success, guard_success,
read_def, option_case_eq, check_dups_success,
type_name_check_subst_success,
check_ctor_types_success,
check_ctors_success];
Theorem success_eqns =
success_eqns
Theorem remove_pair_lem:
(!f v. (\(x,y). f x y) v = f (FST v) (SND v)) ∧
(!f v. (\(x,y,z). f x y z) v = f (FST v) (FST (SND v)) (SND (SND v)))
Proof
rw [] >>
PairCases_on `v` >>
rw []
QED
(* ---------- Simple structural properties ---------- *)
Theorem infer_funs_length:
!l ienv funs ts st1 st2.
(infer_funs l ienv funs st1 = (Success ts, st2)) ⇒
(LENGTH funs = LENGTH ts)
Proof
induct_on `funs` >>
rw [infer_e_def, success_eqns] >>
rw [] >>
PairCases_on `h` >>
fs [infer_e_def, success_eqns] >>
rw [] >>
metis_tac []
QED
Theorem type_name_check_subst_state:
(!t l err tenvT fvs (st:'a) r st'. type_name_check_subst l err tenvT fvs t st = (r,st') ⇒ st = st') ∧
(!ts l err tenvT fvs (st:'a) r st'. type_name_check_subst_list l err tenvT fvs ts st = (r,st') ⇒ st = st')
Proof
Induct >>
rw [type_name_check_subst_def, st_ex_bind_def, guard_def, st_ex_return_def,
failwith_def, lookup_st_ex_def] >>
every_case_tac >>
fs [] >>
rw [] >>
TRY pairarg_tac >>
fs [] >>
every_case_tac >>
fs [] >>
metis_tac []
QED
Theorem infer_p_bindings:
(!l cenv p st t env st' x.
(infer_p l cenv p st = (Success (t,env), st'))
⇒
(pat_bindings p x = MAP FST env ++ x)) ∧
(!l cenv ps st ts env st' x.
(infer_ps l cenv ps st = (Success (ts,env), st'))
⇒
(pats_bindings ps x = MAP FST env ++ x))
Proof
ho_match_mp_tac infer_p_ind >>
rw [pat_bindings_def, infer_p_def, success_eqns, remove_pair_lem]
>- (PairCases_on `v'` >>
rw [] >>
metis_tac [])
>- (PairCases_on `v''` >>
rw [] >>
metis_tac [])
>- (PairCases_on `v'` >>
rw [] >>
metis_tac [])
>- (
PairCases_on `v'` >>
first_x_assum drule>>
simp[])
>- metis_tac []
>- (PairCases_on `v'` >>
PairCases_on `v''` >>
rw [] >>
metis_tac [APPEND_ASSOC])
QED
(* ---------- Dealing with the constraint set ---------- *)
Triviality pure_add_constraints_append2:
!s1 ts s2 t1 t2.
t_wfs s1 ∧
pure_add_constraints s1 ts s2 ∧
(t_walkstar s1 t1 = t_walkstar s1 t2)
⇒
(t_walkstar s2 t1 = t_walkstar s2 t2)
Proof
induct_on `ts` >>
rw [pure_add_constraints_def] >>
rw [] >>
PairCases_on `h` >>
fs [pure_add_constraints_def] >>
metis_tac [t_unify_wfs, t_unify_apply2]
QED
Theorem pure_add_constraints_apply:
!s1 ts s2.
t_wfs s1 ∧
pure_add_constraints s1 ts s2
⇒
MAP (t_walkstar s2 o FST) ts = MAP (t_walkstar s2 o SND) ts
Proof
induct_on `ts` >>
rw [pure_add_constraints_def] >>
PairCases_on `h` >>
fs [pure_add_constraints_def] >>
metis_tac [t_unify_apply, pure_add_constraints_append2, t_unify_wfs]
QED
Theorem pure_add_constraints_append:
!s1 ts1 s3 ts2.
pure_add_constraints s1 (ts1 ++ ts2) s3
=
(?s2. pure_add_constraints s1 ts1 s2 ∧ pure_add_constraints s2 ts2 s3)
Proof
ho_match_mp_tac pure_add_constraints_ind >>
rw [pure_add_constraints_def] >>
metis_tac []
QED
Theorem infer_p_constraints:
(!l cenv p st t env st'.
(infer_p l cenv p st = (Success (t,env), st'))
⇒
(?ts. pure_add_constraints st.subst ts st'.subst)) ∧
(!l cenv ps st ts env st'.
(infer_ps l cenv ps st = (Success (ts,env), st'))
⇒
(?ts'. pure_add_constraints st.subst ts' st'.subst))
Proof
ho_match_mp_tac infer_p_ind >>
rw [infer_p_def, success_eqns, remove_pair_lem, GSYM FORALL_PROD] >>
rw [] >>
res_tac >>
fs [] >>
prove_tac [pure_add_constraints_append, pure_add_constraints_def, type_name_check_subst_state]
QED
Theorem infer_e_constraints:
(!l ienv e st st' t.
(infer_e l ienv e st = (Success t, st'))
⇒
(?ts. pure_add_constraints st.subst ts st'.subst)) ∧
(!l ienv es st st' ts.
(infer_es l ienv es st = (Success ts, st'))
⇒
(?ts. pure_add_constraints st.subst ts st'.subst)) ∧
(!l ienv pes t1 t2 st st'.
(infer_pes l ienv pes t1 t2 st = (Success (), st'))
⇒
(?ts. pure_add_constraints st.subst ts st'.subst)) ∧
(!l ienv funs st st' ts'.
(infer_funs l ienv funs st = (Success ts', st'))
⇒
(?ts. pure_add_constraints st.subst ts st'.subst))
Proof
ho_match_mp_tac infer_e_ind >>
rw [infer_e_def, constrain_op_success, success_eqns, remove_pair_lem, GSYM FORALL_PROD] >>
rw [] >>
res_tac >>
fs [] >>
TRY (cases_on `v'`) >>
every_case_tac >> TRY (Cases_on `uop:fp_uop`) >>
fs [success_eqns] >>
rw [] >>
fs [infer_st_rewrs] >>
TRY (prove_tac [pure_add_constraints_append, pure_add_constraints_def,
infer_p_constraints, type_name_check_subst_state])
QED
Theorem pure_add_constraints_wfs:
!s1 ts s2.
pure_add_constraints s1 ts s2 ∧
t_wfs s1
⇒
t_wfs s2
Proof
induct_on `ts` >>
rw [pure_add_constraints_def] >-
metis_tac [] >>
PairCases_on `h` >>
fs [pure_add_constraints_def] >>
metis_tac [t_unify_wfs]
QED
(* ---------- The next unification variable is monotone non-decreasing ---------- *)
Theorem infer_p_next_uvar_mono:
(!l cenv p st t env st'.
(infer_p l cenv p st = (Success (t,env), st'))
⇒
st.next_uvar ≤ st'.next_uvar) ∧
(!l cenv ps st ts env st'.
(infer_ps l cenv ps st = (Success (ts,env), st'))
⇒
st.next_uvar ≤ st'.next_uvar)
Proof
ho_match_mp_tac infer_p_ind >>
rw [infer_p_def, success_eqns, remove_pair_lem, GSYM FORALL_PROD] >>
rw [] >>
res_tac >>
fs [] >>
`st''' = st''` by metis_tac [type_name_check_subst_state] >>
metis_tac [DECIDE ``!(x:num) y z. x ≤ y ⇒ x ≤ y + z``,
arithmeticTheory.LESS_EQ_TRANS]
QED
Theorem infer_e_next_uvar_mono:
(!l ienv e st st' t.
(infer_e l ienv e st = (Success t, st'))
⇒
st.next_uvar ≤ st'.next_uvar) ∧
(!l ienv es st st' ts.
(infer_es l ienv es st = (Success ts, st'))
⇒
st.next_uvar ≤ st'.next_uvar) ∧
(!l ienv pes t1 t2 st st'.
(infer_pes l ienv pes t1 t2 st = (Success (), st'))
⇒
st.next_uvar ≤ st'.next_uvar) ∧
(!l ienv funs st st' ts.
(infer_funs l ienv funs st = (Success ts, st'))
⇒
st.next_uvar ≤ st'.next_uvar)
Proof
ho_match_mp_tac infer_e_ind >>
rw [infer_e_def, constrain_op_success, success_eqns, remove_pair_lem, GSYM FORALL_PROD] >>
rw [] >>
res_tac >>
fs [] >>
every_case_tac >> TRY (Cases_on `uop:fp_uop`) >>
fs [success_eqns]>>
metis_tac [infer_p_next_uvar_mono, arithmeticTheory.LESS_EQ_TRANS,
pair_CASES,type_name_check_subst_state,
DECIDE ``!(x:num) y. x ≤ x + y``,
DECIDE ``!(x:num) y. x + 1 ≤ y ⇒ x ≤ y``,
DECIDE ``!(x:num) y z. x ≤ y ⇒ x ≤ y + z``]
QED
(* ---------- The inferencer builds well-formed substitutions ---------- *)
Theorem infer_p_wfs:
(!l cenv p st t env st'.
t_wfs st.subst ∧
(infer_p l cenv p st = (Success (t,env), st'))
⇒
t_wfs st'.subst) ∧
(!l cenv ps st ts env st'.
t_wfs st.subst ∧
(infer_ps l cenv ps st = (Success (ts,env), st'))
⇒
t_wfs st'.subst)
Proof
ho_match_mp_tac infer_p_ind >>
rw [infer_p_def, success_eqns, remove_pair_lem, GSYM FORALL_PROD] >>
rw [] >>
res_tac >>
fs []
>- prove_tac [pure_add_constraints_wfs]
>- metis_tac [t_unify_wfs, type_name_check_subst_state]
QED
Theorem infer_e_wfs:
(!l ienv e st st' t.
infer_e l ienv e st = (Success t, st') ∧
t_wfs st.subst
⇒
t_wfs st'.subst) ∧
(!l ienv es st st' ts.
infer_es l ienv es st = (Success ts, st') ∧
t_wfs st.subst
⇒
t_wfs st'.subst) ∧
(!l ienv pes t1 t2 st st'.
infer_pes l ienv pes t1 t2 st = (Success (), st') ∧
t_wfs st.subst
⇒
t_wfs st'.subst) ∧
(!l ienv funs st st' ts'.
infer_funs l ienv funs st = (Success ts', st') ∧
t_wfs st.subst