-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpure_unificationScript.sml
1907 lines (1796 loc) · 66.5 KB
/
pure_unificationScript.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
(*
Adapt unification from HOL/examples/algorithms/unification for use in PureCake
*)
open HolKernel Parse boolLib bossLib BasicProvers dep_rewrite;
open pairTheory arithmeticTheory integerTheory stringTheory optionTheory
pred_setTheory relationTheory listTheory alistTheory finite_mapTheory;
open unifPropsTheory unifDefTheory walkTheory walkstarTheory collapseTheory substTheory;
open pure_typingTheory pure_inference_commonTheory;
val _ = new_theory "pure_unification";
Datatype:
utype = uDBVar num
| uPrimTy prim_ty
| uException
| uTypeCons num
| uTuple
| uFunction
| uArray
| uM
| uNone
End
Definition encode_itype_def[nocompute]:
encode_itype (DBVar n) = Const (uDBVar n) ∧
encode_itype (PrimTy p) = Const (uPrimTy p) ∧
encode_itype Exception = Const (uException) ∧
encode_itype (TypeCons c ts) = Pair (Const (uTypeCons c)) (encode_itypes ts) ∧
encode_itype (Tuple ts) = Pair (Const uTuple) (encode_itypes ts) ∧
encode_itype (Function t1 t2) =
Pair (Const uFunction) (Pair (encode_itype t1) (encode_itype t2)) ∧
encode_itype (Array t) = Pair (Const uArray) (encode_itype t) ∧
encode_itype (M t) = Pair (Const uM) (encode_itype t) ∧
encode_itype (CVar n) = Var n ∧
encode_itypes [] = Const uNone ∧
encode_itypes (t::ts) = Pair (encode_itype t) (encode_itypes ts)
End
Definition decode_utype_def[nocompute]:
decode_utype (Var n) = SOME $ CVar n ∧
decode_utype (Const (uDBVar n)) = SOME $ DBVar n ∧
decode_utype (Const (uPrimTy p)) = SOME $ PrimTy p ∧
decode_utype (Const uException) = SOME $ Exception ∧
decode_utype (Pair (Const (uTypeCons n)) uts) =
OPTION_MAP (TypeCons n) (decode_utypes uts) ∧
decode_utype (Pair (Const uTuple) uts) = OPTION_MAP Tuple (decode_utypes uts) ∧
decode_utype (Pair (Const uFunction) (Pair ut1 ut2)) = (
OPTION_BIND (decode_utype ut1) $ λt1.
OPTION_BIND (decode_utype ut2) $ λt2. SOME $ Function t1 t2) ∧
decode_utype (Pair (Const uArray) ut1) = OPTION_MAP Array (decode_utype ut1) ∧
decode_utype (Pair (Const uM) ut1) = OPTION_MAP M (decode_utype ut1) ∧
decode_utype _ = NONE ∧
decode_utypes (Const uNone) = SOME [] ∧
decode_utypes (Pair u1 u2) = (
OPTION_BIND (decode_utype u1) $ λt.
OPTION_BIND (decode_utypes u2) $ λts. SOME $ t::ts) ∧
decode_utypes _ = NONE
End
Triviality I_o_f:
∀m. I o_f m = m
Proof
rw[GSYM fmap_EQ_THM]
QED
Triviality option_map_case:
∀f opt. OPTION_MAP f opt = case opt of NONE => NONE | SOME a => SOME $ f a
Proof
gen_tac >> Cases >> simp[]
QED
Triviality option_bind_case:
∀x f. OPTION_BIND x f = case x of NONE => NONE | SOME y => f y
Proof
Cases >> simp[]
QED
Theorem decode_encode:
(∀it. decode_utype (encode_itype it) = SOME it) ∧
(∀its. decode_utypes (encode_itypes its) = SOME its)
Proof
Induct >> rw[encode_itype_def, decode_utype_def]
QED
Definition pure_wfs_def[nocompute]:
pure_wfs s = wfs (encode_itype o_f s)
End
Definition pure_vwalk_def[nocompute]:
pure_vwalk s v = THE $ decode_utype (vwalk (encode_itype o_f s) v)
End
Theorem pure_vwalk_ind:
∀s. pure_wfs s ⇒
∀P. (∀v. (∀w u. FLOOKUP s v = SOME (CVar u) ⇒ P u) ⇒ P v) ⇒ ∀v. P v
Proof
ntac 4 strip_tac >> gvs[pure_wfs_def] >>
drule $ DISCH_ALL vwalk_ind >> disch_then irule >> rw[] >>
last_x_assum irule >> rw[] >>
first_x_assum irule >>
rw[FLOOKUP_o_f, encode_itype_def]
QED
Theorem pure_vwalk:
∀s. pure_wfs s ⇒
∀v.
pure_vwalk s v =
case FLOOKUP s v of
| NONE => CVar v
| SOME (CVar w) => pure_vwalk s w
| SOME t => t
Proof
rw[pure_vwalk_def, pure_wfs_def] >>
CASE_TAC >> gvs[] >>
simp[Once vwalk_def, FLOOKUP_o_f, decode_utype_def] >>
Cases_on `x` >> gvs[encode_itype_def, decode_utype_def, decode_encode]
QED
Definition pure_walk_def[nocompute]:
pure_walk s t = THE $ decode_utype (walk (encode_itype o_f s) (encode_itype t))
End
Theorem pure_walk:
∀s t. pure_walk s t =
case t of
| CVar v => pure_vwalk s v
| t => t
Proof
rw[pure_walk_def, pure_vwalk_def, walk_def] >>
Cases_on `t` >> gvs[decode_utype_def, encode_itype_def, decode_encode]
QED
Definition pure_oc_def[nocompute]:
pure_oc s t v = oc (encode_itype o_f s) (encode_itype t) v
End
Definition pure_vars_def[nocompute]:
pure_vars t = vars (encode_itype t)
End
Theorem encode_vwalk:
∀s. pure_wfs s ⇒
∀u. vwalk (encode_itype o_f s) u = encode_itype (pure_vwalk s u)
Proof
ntac 2 strip_tac >>
recInduct $ (UNDISCH o Q.SPEC `s`) pure_vwalk_ind >> rw[] >>
`wfs (encode_itype o_f s)` by metis_tac [pure_wfs_def] >>
rw [Once vwalk_def, Once pure_vwalk, FLOOKUP_o_f] >>
Cases_on `FLOOKUP s v` >> rw [encode_itype_def] >>
Cases_on `x` >> rw[encode_itype_def]
QED
Theorem pure_oc_lemma:
∀l v s.
oc (encode_itype o_f s) (encode_itypes l) v ⇔
EXISTS (λt. oc (encode_itype o_f s) (encode_itype t) v) l
Proof
Induct >> rw[encode_itype_def]
QED
Theorem pure_oc:
∀s. pure_wfs s ⇒
∀t v.
pure_oc s t v =
case pure_walk s t of
| CVar u => v = u
| TypeCons n its => EXISTS (λit. pure_oc s it v) its
| Tuple its => EXISTS (λit. pure_oc s it v) its
| Function it1 it2 => EXISTS (λit. pure_oc s it v) [it1;it2]
| Array it => pure_oc s it v
| M it => pure_oc s it v
| _ => F
Proof
rw[pure_oc_def] >>
`wfs (encode_itype o_f s)` by fs [pure_wfs_def] >>
rw[Once oc_walking, pure_walk_def] >>
Cases_on `t` >>
rw[walk_def, encode_itype_def, decode_utype_def, decode_encode,
encode_vwalk, pure_oc_lemma] >>
Cases_on `pure_vwalk s n` >> rw[encode_itype_def, pure_oc_lemma] >> gvs[] >>
simp[decode_encode] >>
Cases_on `x` >> gvs[encode_itype_def, pure_oc_lemma]
QED
Definition pure_ext_s_check_def[nocompute]:
pure_ext_s_check s v t =
OPTION_MAP ((o_f) (THE o decode_utype)) $
ext_s_check (encode_itype o_f s) v (encode_itype t)
End
Theorem pure_ext_s_check:
∀s v t.
pure_ext_s_check s v t = if pure_oc s t v then NONE else SOME (s |+ (v,t))
Proof
rw[pure_ext_s_check_def, pure_oc_def, I_o_f,
combinTheory.o_DEF, decode_encode]
QED
Definition pure_unify_def[nocompute]:
pure_unify s t1 t2 =
OPTION_MAP ((o_f) (THE o decode_utype)) $
unify (encode_itype o_f s) (encode_itype t1) (encode_itype t2)
End
Definition pure_unifyl_def:
pure_unifyl s [] [] = SOME s ∧
pure_unifyl s (t1::ts1) (t2::ts2) = (
case pure_unify s t1 t2 of
| NONE => NONE
| SOME s' => pure_unifyl s' ts1 ts2) ∧
pure_unifyl s _ _ = NONE
End
Theorem encode_walk:
∀s. pure_wfs s ⇒
∀t. walk (encode_itype o_f s) (encode_itype t) =
encode_itype (pure_walk s t)
Proof
rw[walk_def] >>
imp_res_tac encode_vwalk >> simp[] >>
every_case_tac >> rw[pure_walk_def, decode_encode] >> gvs[] >>
pop_assum $ SUBST_ALL_TAC o GSYM >> simp[decode_encode]
QED
Theorem encode_pair_cases:
∀t t1 t2. encode_itype t = Pair t1 t2 ⇒
(∃c ts. t1 = Const (uTypeCons c) ∧ t2 = encode_itypes ts) ∨
(∃ts. t1 = Const uTuple ∧ t2 = encode_itypes ts) ∨
(∃it1 it2. t1 = Const uFunction ∧
t2 = Pair (encode_itype it1) (encode_itype it2)) ∨
(∃t. t1 = Const uArray ∧ t2 = encode_itype t) ∨
(∃t. t1 = Const uM ∧ t2 = encode_itype t)
Proof
Cases >> rw[encode_itype_def] >> rpt $ irule_at Any EQ_REFL
QED
Theorem encode_itype_injective:
(∀t1 t2. encode_itype t1 = encode_itype t2 ⇔ t1 = t2) ∧
(∀t1s t2s. encode_itypes t1s = encode_itypes t2s ⇔ t1s = t2s)
Proof
rw[] >> eq_tac >> rw[] >>
irule $ iffLR SOME_11 >> once_rewrite_tac[GSYM decode_encode] >> simp[]
QED
Theorem encode_itype_o_f_injective:
∀s1 s2. encode_itype o_f s1 = encode_itype o_f s2 ⇔ s1 = s2
Proof
rw[] >> eq_tac >> rw[] >>
gvs[fmap_eq_flookup, FLOOKUP_o_f] >> rw[] >>
pop_assum $ qspec_then `x` assume_tac >>
every_case_tac >> gvs[encode_itype_injective]
QED
Theorem encode_unify_lemma:
∀s t1 t2 s' t1' t2'.
s = encode_itype o_f s' ∧
pure_wfs s' ∧
(
(t1 = encode_itype t1' ∧ t2 = encode_itype t2') ∨
(∃c. t1 = Const c ∧ t2 = Const c ∧ t1' = t2') ∨
(∃c ts1 ts2.
t1 = Pair (Const c) (encode_itypes ts1) ∧
t2 = Pair (Const c) (encode_itypes ts2) ∧
((t1' = TypeCons ARB ts1 ∧ t2' = TypeCons ARB ts2) ∨
(t1' = Tuple ts1 ∧ t2' = Tuple ts2))) ∨
(∃c t11 t12 t21 t22.
t1 = Pair (Const c) (Pair (encode_itype t11) (encode_itype t12)) ∧
t2 = Pair (Const c) (Pair (encode_itype t21) (encode_itype t22)) ∧
t1' = Function t11 t12 ∧ t2' = Function t21 t22) ∨
(∃c ta tb.
t1 = Pair (Const c) (encode_itype ta) ∧
t2 = Pair (Const c) (encode_itype tb) ∧
((t1' = Array ta ∧ t2' = Array tb) ∨
(t1' = M ta ∧ t2' = M tb))) ∨
(∃ts1 ts2.
t1 = encode_itypes ts1 ∧
t2 = encode_itypes ts2 ∧
((t1' = TypeCons ARB ts1 ∧ t2' = TypeCons ARB ts2) ∨
(t1' = Tuple ts1 ∧ t2' = Tuple ts2))) ∨
(∃t11 t12 t21 t22.
t1 = Pair (encode_itype t11) (encode_itype t12) ∧
t2 = Pair (encode_itype t21) (encode_itype t22) ∧
t1' = Function t11 t12 ∧ t2' = Function t21 t22) ∨
(∃ta tb.
t1 = encode_itype ta ∧
t2 = encode_itype tb ∧
((t1' = Array ta ∧ t2' = Array tb) ∨
(t1' = M ta ∧ t2' = M tb)))
)
⇒ unify s t1 t2 = OPTION_MAP ((o_f) encode_itype) (pure_unify s' t1' t2')
Proof
recInduct unify_ind >> simp[] >>
rw[pure_unify_def] >> gvs[pure_wfs_def, option_map_case] >>
qmatch_assum_abbrev_tac `wfs es`
>- (
qmatch_goalsub_abbrev_tac `unify es e1 e2` >>
Cases_on `unify es e1 e2` >> gvs[combinTheory.o_DEF] >>
qsuff_tac `∃s. x = encode_itype o_f s`
>- (strip_tac >> rw[GSYM fmap_EQ_THM, decode_encode]) >>
pop_assum mp_tac >> simp[Once unify_def] >>
Cases_on `walk es e1` >> Cases_on `walk es e2` >> gvs[]
>- (
unabbrev_all_tac >> rw[]
>- (irule_at Any EQ_REFL) >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
rw[] >> gvs[] >>
qmatch_assum_rename_tac `walk es e1 = Pair t1a t1d` >>
qmatch_assum_rename_tac `walk es e2 = Pair t2a t2d` >>
`Pair t1a t1d = encode_itype (pure_walk s' t1')` by
metis_tac[encode_walk,pure_wfs_def] >>
`Pair t2a t2d = encode_itype (pure_walk s' t2')` by
metis_tac[encode_walk,pure_wfs_def] >>
`wfs sx` by metis_tac[unify_unifier] >>
`∀c1 c2.
((t1a = Const c1 ∧ t2a = Const c2) ∨
(t2d = Const c1 ∧ t2d = Const c2)) ⇒ c1 = c2` by (
rw[] >> ntac 2 $ qpat_x_assum `unify _ _ _ = _` mp_tac >>
simp[Once unify_def] >> strip_tac >> simp[Once unify_def]) >>
pop_assum mp_tac >> simp[DISJ_IMP_THM, FORALL_AND_THM] >> strip_tac >>
qspecl_then [`pure_walk s' t1'`,`t1a`,`t1d`] mp_tac encode_pair_cases >>
qspecl_then [`pure_walk s' t2'`,`t2a`,`t2d`] mp_tac encode_pair_cases >>
simp[] >> strip_tac >> strip_tac >> gvs[] >>
rpt $ qpat_x_assum `Pair _ _ = _` $ assume_tac o GSYM
>- (
last_x_assum $
qspecl_then[`s'`,`TypeCons ARB ts'`,`TypeCons ARB ts`] mp_tac >>
simp[] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`Tuple ts'`,`Tuple ts`] mp_tac >>
simp[] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`Function it1' it2'`,`Function it1 it2`] mp_tac >>
simp[encode_itype_def] >>
CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`Array t'`,`Array t`] mp_tac >>
simp[encode_itype_def] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`M t'`,`M t`] mp_tac >>
simp[encode_itype_def] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (unabbrev_all_tac >> rw[] >> irule_at Any EQ_REFL)
)
>- simp[decode_encode, SF ETA_ss, combinTheory.o_DEF]
>- (unabbrev_all_tac >> rw[Once unify_def])
>- (unabbrev_all_tac >> rw[Once unify_def])
>- (unabbrev_all_tac >> rw[Once unify_def])
>- (unabbrev_all_tac >> rw[Once unify_def])
>- (unabbrev_all_tac >> rw[Once unify_def])
>- (
simp[encode_itype_def] >>
simp[Once unify_def, SimpRHS] >> CASE_TAC >> pop_assum mp_tac >>
Cases_on `ts1` >> Cases_on `ts2` >> simp[encode_itype_def, Once unify_def]
>- (
rw[Abbr `es`] >>
simp[o_f_o_f, combinTheory.o_DEF, decode_encode, SF ETA_ss]
) >>
rw[] >> gvs[encode_itype_def] >>
first_x_assum $ qspecl_then [`s'`,`h`,`h'`] mp_tac >> simp[] >> rw[] >>
`wfs sx` by metis_tac[unify_unifier] >>
qabbrev_tac `dx = decode_utype o_f sx` >> simp[combinTheory.o_DEF] >>
`sx = encode_itype o_f THE o_f dx` by rw[Abbr `dx`] >>
first_x_assum $ qspecl_then
[`THE o_f dx`,`TypeCons ARB t`,`TypeCons ARB t'`] mp_tac >>
pop_assum $ SUBST1_TAC o GSYM >> simp[encode_itype_def] >>
simp[Once unify_def, combinTheory.o_DEF]
)
>- (
simp[encode_itype_def] >>
simp[Once unify_def, SimpRHS] >> CASE_TAC >> pop_assum mp_tac >>
Cases_on `ts1` >> Cases_on `ts2` >> simp[encode_itype_def, Once unify_def]
>- (
rw[Abbr `es`] >>
simp[o_f_o_f, combinTheory.o_DEF, decode_encode, SF ETA_ss]
) >>
rw[] >> gvs[encode_itype_def] >>
first_x_assum $ qspecl_then [`s'`,`h`,`h'`] mp_tac >> simp[] >> rw[] >>
`wfs sx` by metis_tac[unify_unifier] >>
qabbrev_tac `dx = decode_utype o_f sx` >> simp[combinTheory.o_DEF] >>
`sx = encode_itype o_f THE o_f dx` by rw[Abbr `dx`] >>
first_x_assum $ qspecl_then [`THE o_f dx`,`Tuple t`,`Tuple t'`] mp_tac >>
pop_assum $ SUBST1_TAC o GSYM >> simp[encode_itype_def] >>
simp[Once unify_def, combinTheory.o_DEF]
)
>- (
simp[encode_itype_def] >>
simp[Once unify_def, SimpRHS] >> CASE_TAC >> pop_assum mp_tac >>
simp[Once unify_def] >> rw[] >> gvs[encode_itype_def] >>
first_x_assum $ qspecl_then [`s'`,`t11`,`t21`] mp_tac >> simp[] >> rw[] >>
`wfs sx` by metis_tac[unify_unifier] >>
qabbrev_tac `dx = decode_utype o_f sx` >> simp[combinTheory.o_DEF] >>
`sx = encode_itype o_f THE o_f dx` by rw[Abbr `dx`] >>
first_x_assum $ qspecl_then [`THE o_f dx`,`t12`,`t22`] mp_tac >>
pop_assum $ SUBST1_TAC o GSYM >> simp[] >>
simp[Once unify_def, combinTheory.o_DEF]
)
>- (
simp[encode_itype_def] >>
simp[Once unify_def, SimpRHS] >>
qmatch_goalsub_abbrev_tac `unify es e1 e2` >> CASE_TAC >>
qsuff_tac `∃s. x = encode_itype o_f s`
>- (strip_tac >> rw[GSYM fmap_EQ_THM, decode_encode]) >>
pop_assum mp_tac >> simp[Once unify_def] >>
Cases_on `walk es e1` >> Cases_on `walk es e2` >> gvs[]
>- (
unabbrev_all_tac >> rw[]
>- (irule_at Any EQ_REFL) >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
rw[] >> gvs[] >>
qmatch_assum_rename_tac `walk es e1 = Pair t1a t1d` >>
qmatch_assum_rename_tac `walk es e2 = Pair t2a t2d` >>
`Pair t1a t1d = encode_itype (pure_walk s' ta)` by
metis_tac[encode_walk,pure_wfs_def] >>
`Pair t2a t2d = encode_itype (pure_walk s' tb)` by
metis_tac[encode_walk,pure_wfs_def] >>
`wfs sx` by metis_tac[unify_unifier] >>
`∀c1 c2.
((t1a = Const c1 ∧ t2a = Const c2) ∨
(t2d = Const c1 ∧ t2d = Const c2)) ⇒ c1 = c2` by (
rw[] >> ntac 2 $ qpat_x_assum `unify _ _ _ = _` mp_tac >>
simp[Once unify_def] >> strip_tac >> simp[Once unify_def]) >>
pop_assum mp_tac >> simp[DISJ_IMP_THM, FORALL_AND_THM] >> strip_tac >>
qspecl_then [`pure_walk s' ta`,`t1a`,`t1d`] mp_tac encode_pair_cases >>
qspecl_then [`pure_walk s' tb`,`t2a`,`t2d`] mp_tac encode_pair_cases >>
simp[] >> strip_tac >> strip_tac >> gvs[] >>
rpt $ qpat_x_assum `Pair _ _ = _` $ assume_tac o GSYM
>- (
last_x_assum $
qspecl_then[`s'`,`TypeCons ARB ts'`,`TypeCons ARB ts`] mp_tac >>
simp[] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`Tuple ts'`,`Tuple ts`] mp_tac >>
simp[] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`Function it1' it2'`,`Function it1 it2`] mp_tac >>
simp[encode_itype_def] >>
CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`Array t'`,`Array t`] mp_tac >>
simp[encode_itype_def] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`M t'`,`M t`] mp_tac >>
simp[encode_itype_def] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (unabbrev_all_tac >> rw[] >> irule_at Any EQ_REFL)
)
>- (
simp[encode_itype_def] >>
simp[Once unify_def, SimpRHS] >>
qmatch_goalsub_abbrev_tac `unify es e1 e2` >> CASE_TAC >>
qsuff_tac `∃s. x = encode_itype o_f s`
>- (strip_tac >> rw[GSYM fmap_EQ_THM, decode_encode]) >>
pop_assum mp_tac >> simp[Once unify_def] >>
Cases_on `walk es e1` >> Cases_on `walk es e2` >> gvs[]
>- (
unabbrev_all_tac >> rw[]
>- (irule_at Any EQ_REFL) >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (
rw[] >> gvs[] >>
qmatch_assum_rename_tac `walk es e1 = Pair t1a t1d` >>
qmatch_assum_rename_tac `walk es e2 = Pair t2a t2d` >>
`Pair t1a t1d = encode_itype (pure_walk s' ta)` by
metis_tac[encode_walk,pure_wfs_def] >>
`Pair t2a t2d = encode_itype (pure_walk s' tb)` by
metis_tac[encode_walk,pure_wfs_def] >>
`wfs sx` by metis_tac[unify_unifier] >>
`∀c1 c2.
((t1a = Const c1 ∧ t2a = Const c2) ∨
(t2d = Const c1 ∧ t2d = Const c2)) ⇒ c1 = c2` by (
rw[] >> ntac 2 $ qpat_x_assum `unify _ _ _ = _` mp_tac >>
simp[Once unify_def] >> strip_tac >> simp[Once unify_def]) >>
pop_assum mp_tac >> simp[DISJ_IMP_THM, FORALL_AND_THM] >> strip_tac >>
qspecl_then [`pure_walk s' ta`,`t1a`,`t1d`] mp_tac encode_pair_cases >>
qspecl_then [`pure_walk s' tb`,`t2a`,`t2d`] mp_tac encode_pair_cases >>
simp[] >> strip_tac >> strip_tac >> gvs[] >>
rpt $ qpat_x_assum `Pair _ _ = _` $ assume_tac o GSYM
>- (
last_x_assum $
qspecl_then[`s'`,`TypeCons ARB ts'`,`TypeCons ARB ts`] mp_tac >>
simp[] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`Tuple ts'`,`Tuple ts`] mp_tac >>
simp[] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`Function it1' it2'`,`Function it1 it2`] mp_tac >>
simp[encode_itype_def] >>
CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`Array t'`,`Array t`] mp_tac >>
simp[encode_itype_def] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
>- (
last_x_assum $
qspecl_then[`s'`,`M t'`,`M t`] mp_tac >>
simp[encode_itype_def] >> CASE_TAC >> simp[] >> metis_tac[o_f_o_f]
)
)
>- (
unabbrev_all_tac >> rw[] >>
mp_tac encode_walk >> simp[pure_wfs_def] >>
disch_then imp_res_tac >> rgs[] >> rgs[pure_walk_def] >>
qmatch_goalsub_abbrev_tac ` _ m |+ (k,v)` >>
qmatch_asmsub_abbrev_tac `encode_itype foo = v` >>
qexists_tac `m |+ (k,foo)` >> rw[]
)
>- (unabbrev_all_tac >> rw[] >> irule_at Any EQ_REFL)
)
QED
Theorem encode_unify:
∀s t1 t2 s' t1' t2'.
s = encode_itype o_f s' ∧
t1 = encode_itype t1' ∧
t2 = encode_itype t2' ∧
pure_wfs s'
⇒ unify s t1 t2 = OPTION_MAP ((o_f) encode_itype) (pure_unify s' t1' t2')
Proof
metis_tac[encode_unify_lemma]
QED
Theorem encode_unify_alt:
∀s t1 t2. pure_wfs s ⇒
unify (encode_itype o_f s) (encode_itype t1) (encode_itype t2) =
OPTION_MAP ((o_f) encode_itype) (pure_unify s t1 t2)
Proof
metis_tac[encode_unify_lemma]
QED
Theorem wfs_unify:
∀s t1 t2 s'. wfs s ∧ unify s t1 t2 = SOME s' ⇒ wfs s'
Proof
metis_tac[unify_unifier]
QED
Theorem pure_unifyl:
∀s l1 l2.
pure_wfs s ⇒
pure_unifyl s l1 l2 =
OPTION_MAP ((o_f) (THE o decode_utype)) $
unify (encode_itype o_f s) (encode_itypes l1) (encode_itypes l2)
Proof
Induct_on `l1` >> Cases_on `l2` >> rw[pure_unifyl_def, encode_itype_def] >>
imp_res_tac pure_wfs_def >>
gvs[OPTION_MAP_CASE, combinTheory.o_DEF, decode_encode]
>- rw[Once unify_def] >- rw[Once unify_def] >>
rw[Once unify_def] >> rw[pure_unify_def, combinTheory.o_DEF] >>
simp[OPTION_MAP_CASE, combinTheory.o_DEF] >>
CASE_TAC >> simp[] >>
imp_res_tac encode_unify_alt >> gvs[combinTheory.o_DEF, decode_encode] >>
last_x_assum irule >> rgs[pure_wfs_def, pure_unify_def] >>
irule wfs_unify >> goal_assum drule >>
irule_at Any EQ_TRANS >> pop_assum $ irule_at Any >> simp[PULL_EXISTS] >>
goal_assum drule >> simp[]
QED
Theorem pure_unify:
∀t1 t2 s.
pure_wfs s ⇒
pure_unify s t1 t2 =
case (pure_walk s t1, pure_walk s t2) of
| (CVar v1, CVar v2) =>
SOME (if v1 = v2 then s else s |+ (v1, CVar v2))
| (CVar v1, t2) => pure_ext_s_check s v1 t2
| (t1, CVar v2) => pure_ext_s_check s v2 t1
| (TypeCons c1 ts1, TypeCons c2 ts2) =>
if c1 = c2 then pure_unifyl s ts1 ts2 else NONE
| (Tuple ts1, Tuple ts2) => pure_unifyl s ts1 ts2
| (Function t11 t12, Function t21 t22) =>
pure_unifyl s [t11;t12] [t21;t22]
| (Array t1, Array t2) => pure_unify s t1 t2
| (M t1, M t2) => pure_unify s t1 t2
| (DBVar db1, DBVar db2) => if db1 = db2 then SOME s else NONE
| (PrimTy pty1, PrimTy pty2) => if pty1 = pty2 then SOME s else NONE
| (Exception, Exception) => SOME s
| (t1, t2) => NONE
Proof
rw[pure_unify_def] >> imp_res_tac pure_wfs_def >>
rw [Once unify_def, pure_walk_def] >>
Cases_on `t1` >> Cases_on `t2` >>
rw[encode_itype_def, decode_utype_def, option_map_case, option_bind_case,
combinTheory.o_DEF, decode_encode, encode_vwalk, pure_unifyl, pure_oc_lemma]
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- simp[Once unify_def]
>- simp[Once unify_def]
>- simp[Once unify_def]
>- simp[Once unify_def]
>- simp[Once unify_def]
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- simp[Once unify_def]
>- simp[Once unify_def]
>- simp[Once unify_def]
>- simp[Once unify_def]
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- simp[Once unify_def]
>- simp[Once unify_def]
>- (
simp[Once unify_def, SimpRHS] >> simp[Once unify_def, option_bind_case] >>
CASE_TAC >> drule_all wfs_unify >> strip_tac >>
simp[Once unify_def, SimpRHS, option_bind_case] >>
CASE_TAC >> drule_all wfs_unify >> strip_tac >> simp[Once unify_def]
)
>- simp[Once unify_def]
>- simp[Once unify_def]
>- (
ntac 2 $ simp[Once unify_def, option_bind_case] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[]
>- (
simp[Once unify_def, option_bind_case] >>
CASE_TAC >> drule_all wfs_unify >> strip_tac >>
simp[Once unify_def, SimpRHS, option_bind_case] >>
CASE_TAC >> drule_all wfs_unify >> strip_tac >> simp[Once unify_def]
) >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- simp[Once unify_def]
>- simp[Once unify_def]
>- simp[Once unify_def]
>- simp[Once unify_def]
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- simp[Once unify_def]
>- simp[Once unify_def]
>- simp[Once unify_def]
>- simp[Once unify_def]
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[Once unify_def, option_bind_case] >>
simp[encode_itype_def] >> rw[]
>- (
simp[Once unify_def, option_bind_case] >>
CASE_TAC >> drule_all wfs_unify >> strip_tac >>
simp[Once unify_def, SimpRHS, option_bind_case] >>
CASE_TAC >> drule_all wfs_unify >> strip_tac >> simp[Once unify_def]
) >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
simp[Once unify_def] >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
) >>
Cases_on `pure_vwalk s n` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[]
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[Once unify_def] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode, pure_oc_lemma]
)
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[Once unify_def] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode, pure_oc_lemma]
)
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
ntac 2 $ simp[Once unify_def, option_bind_case] >>
simp[encode_itype_def] >> rw[]
>- (
simp[Once unify_def, option_bind_case] >>
CASE_TAC >> drule_all wfs_unify >> strip_tac >>
simp[Once unify_def, SimpRHS, option_bind_case] >>
CASE_TAC >> drule_all wfs_unify >> strip_tac >> simp[Once unify_def]
) >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode]
)
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[Once unify_def] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode, pure_oc_lemma]
)
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[Once unify_def] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode, pure_oc_lemma]
)
>- (
Cases_on `pure_vwalk s n'` >> gvs[decode_encode] >>
simp[Once unify_def] >>
simp[encode_itype_def] >> rw[] >>
simp[pure_ext_s_check, Once pure_oc, pure_walk, decode_utype_def, pure_oc_def] >>
gvs[combinTheory.o_DEF, decode_encode, pure_oc_lemma]
)
QED
Theorem pure_unify_ind:
∀P0 P1.
(∀s t1 t2.
(∀ts1 ts2 c.
(pure_walk s t1 = TypeCons c ts1 ∧
pure_walk s t2 = TypeCons c ts2) ∨
(pure_walk s t1 = Tuple ts1 ∧
pure_walk s t2 = Tuple ts2) ∨
(∃t11 t12 t21 t22.
pure_walk s t1 = Function t11 t12 ∧
pure_walk s t2 = Function t21 t22 ∧
ts1 = [t11;t12] ∧ ts2 = [t21;t22])
⇒ P1 s ts1 ts2) ∧
(∀ta tb.
(pure_walk s t1 = Array ta ∧
pure_walk s t2 = Array tb) ∨
(pure_walk s t1 = M ta ∧
pure_walk s t2 = M tb)
⇒ P0 s ta tb)
⇒ P0 s t1 t2) ∧
(∀s ts1 ts2.
(∀t1 ts1' t2 ts2' s'.
ts1 = t1::ts1' ∧ ts2 = t2::ts2' ⇒
P0 s t1 t2 ∧ (pure_unify s t1 t2 = SOME s' ⇒ P1 s' ts1' ts2'))
⇒ P1 s ts1 ts2)
⇒ (∀s t1 t2. pure_wfs s ⇒ P0 s t1 t2) ∧
(∀s ts1 ts2. pure_wfs s ⇒ P1 s ts1 ts2)
Proof
rpt gen_tac >> strip_tac >>
(fn qt => qspec_then qt strip_assume_tac unify_ind)
`λs t1 t2.
(∀us u1 u2.
wfs s ∧ s = encode_itype o_f us ∧
t1 = encode_itype u1 ∧ t2 = encode_itype u2
⇒ P0 us u1 u2) ∧
(∀us tag us1 us2 c.
wfs s ∧ s = encode_itype o_f us ∧
(c = uTypeCons tag ∨ c = uTuple) ∧
t1 = Pair (Const c) (encode_itypes us1) ∧
t2 = Pair (Const c) (encode_itypes us2)
⇒ P1 us us1 us2) ∧
(∀us u11 u12 u21 u22.
wfs s ∧ s = encode_itype o_f us ∧
t1 = Pair (Const uFunction) (Pair (encode_itype u11) (encode_itype u12)) ∧
t2 = Pair (Const uFunction) (Pair (encode_itype u21) (encode_itype u22))
⇒ P1 us [u11;u12] [u21;u22]) ∧
(∀us u1 u2 c.
wfs s ∧ s = encode_itype o_f us ∧
(c = uArray ∨ c = uM) ∧
t1 = Pair (Const c) (encode_itype u1) ∧
t2 = Pair (Const c) (encode_itype u2)
⇒ P0 us u1 u2) ∧
(∀us v1 u1 v2 u2.
wfs s ∧ s = encode_itype o_f us ∧
t1 = Pair (encode_itype v1) (encode_itypes u1) ∧
t2 = Pair (encode_itype v2) (encode_itypes u2)
⇒ P0 us v1 v2 ∧
(∀usx.
unify s (encode_itype v1) (encode_itype v2) =
SOME (encode_itype o_f usx)
⇒ P1 usx u1 u2)) ∧
(∀us v1 u1 v2 u2.
wfs s ∧ s = encode_itype o_f us ∧
t1 = Pair (encode_itype v1) (encode_itype u1) ∧
t2 = Pair (encode_itype v2) (encode_itype u2)
⇒ P0 us v1 v2 ∧
(∀usx.