-
Notifications
You must be signed in to change notification settings - Fork 40
/
mm0.mm1
3462 lines (3096 loc) · 156 KB
/
mm0.mm1
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
import "peano_hex.mm1";
-- The sort modifiers 'pure', 'strict', 'provable', 'free'
--| `sPure : SortData -> Bool`
@_ def sPure (n: nat): wff = $ true (pi11 n) $;
--| `sStrict : SortData -> Bool`
@_ def sStrict (n: nat): wff = $ true (pi12 n) $;
--| `sProvable : SortData -> Bool`
@_ def sProvable (n: nat): wff = $ true (pi21 n) $;
--| `sFree : SortData -> Bool`
@_ def sFree (n: nat): wff = $ true (pi22 n) $;
-- A binder is either a bound variable with sort s, or a regular variable
-- with sort s and dependencies vs.
--| `PBound : SortID -> Binder`
@_ def PBound (s: nat): nat = $ b0 s $;
--| `PReg : SortID -> set VarID -> Binder`
@_ def PReg (s vs: nat): nat = $ b1 (s, vs) $;
--| Get the sort of a binder.
--|
--| `binderSort : Binder -> SortID`
@_ def binderSort (x: nat): nat =
$ case (\ n, n) (\ n, fst n) @ x $;
pub theorem binderSortBound (s: nat): $ binderSort (PBound s) = s $ =
(named '(eqtr casel @ applame id));
pub theorem binderSortReg (s vs: nat): $ binderSort (PReg s vs) = s $ =
(named '(eqtr caser @ applame @ syl6eq fstpr fsteq));
theorem binderSort_leid: $ binderSort bi <= bi $ =
'(eori
(mpbiri b0leid (leeqd (syl6eq binderSortBound binderSorteq) id))
(mpbiri (leb1tr fstleid)
(leeqd (syl6eq binderSortReg @ binderSorteqd @ syl6eqr (b1eq fstsnd) id) id)) b0orb1);
-- An s-expression, representing the terms and formulas. It can be either a
-- variable (v is a VarID) or an application of the term with TermID `f`
-- to arguments `x` (a list of s-expressions).
--| `SVar : VarID -> SExpr`
@_ def SVar (v: nat): nat = $ b0 v $;
--| `SApp : TermID -> list SExpr -> SExpr`
@_ def SApp (f x: nat): nat = $ b1 (f, x) $;
theorem SApp_ltid: $ e IN es -> e < SApp f es $ =
'(rsyl lmemlt @ mpi (ltb1tr leprid2) lttr);
@_ local def SExprRec (V F: set): set =
$ \ e, srec (\ ih, case V (\\ f, \ x, F @ (f, x, map ih x)) @ size (Dom ih)) e $;
theorem SExprRec_val: $ SExprRec V F @ e =
case V (\\ f, \ x, F @ (f, x, map (SExprRec V F |` upto e) x)) @ e $ =
(named '(eqtr (applame sreceq2) @
! eqtr _ $ _ @ lower (SExprRec V F |` _) $ _ srecval @
applame @ appeqd
(caseeq2d @ slameqd @ lameqd @ appeq2d @ preq2d @
preq2d @ mapeq1d @ bi2 @ eqlower2 @ finlam finns)
sreclem));
theorem SExprRec_Var: $ SExprRec V F @ SVar v = V @ v $ =
(named '(eqtr SExprRec_val casel));
theorem SExprRec_App:
$ SExprRec V F @ SApp f es = F @ (f, es, map (SExprRec V F) es) $ =
(named '(eqtr SExprRec_val @ eqtr caser @
appslame @ applamed @ appeq2d @ preqd anl @ preqd anr @ syl6eq
(mapeqg @ mpbir allal @ !! ax_gen e @ syl resapp @ sylibr elupto SApp_ltid)
(mapeq2d anr)));
theorem indd_aux (G p: wff x) (e: $ x = b -> (p <-> q) $)
(h: $ G /\ a = b -> q $): $ G /\ a = b -> [a / x] p $ =
'(mpbird (syl6bb (sbe e) @ anwr sbeq1) h);
theorem SExpr_indd {x f es} (n) (px: wff x) (pv: wff v) (pe: wff f es)
(hn: $ x = n -> (px <-> pn) $)
(hv: $ x = SVar v -> (px <-> pv) $)
(he: $ x = SApp f es -> (px <-> pe) $)
(h1: $ G -> pv $) (h2: $ G /\ all {x | px} es -> pe $): $ G -> pn $ =
(focus
'(sylib (sbe hn) @ !! indstr z w sbeq1 sbeq1 _)
(split-sop '{
($SVar v$ => (indd_aux hv @ anwll h1)) +
($SApp f es$ => (indd_aux he @ sylan h2 anll @
sylibr (alleq1 cbvabs) @ sylibr allal @ sylc _ anr anlr))
})
'(alimd @ imim1d @ syl5 SApp_ltid @ bi2d lteq2));
-- The environment is composed of declarations, which come in a few types:
--| A `sort` declaration has an associated `SortData` with the sort modifiers.
--| Sorts are indexed by SortID and picked out by `getSD`.
--|
--| `DSort : SortID -> SortData -> Decl`
@_ def DSort (id sd: nat): nat = $ b0 (b0 (b0 (id, sd))) $;
--| A `term` declaration has a list of binders, and a target type (a DepType,
--| which is a `(SortID, set VarID)` pair).
--| Terms are indexed by TermID and picked out by `getTerm`.
--|
--| `DTerm : TermID -> Ctx -> DepType -> Decl`
@_ def DTerm (id args ret: nat): nat = $ b0 (b0 (b1 (id, args, ret))) $;
--| An `axiom` declaration has a list of binders, a list of hypotheses,
--| and a consequent.
--|
--| Axioms are not indexed, they are simply found by statement.
--|
--| `DAxiom : Ctx -> list SExpr -> SExpr -> Decl`
@_ def DAxiom (args hs ret: nat): nat = $ b0 (b1 (args, hs, ret)) $;
--| A `def` declaration is the same as a `term` except it also has an optional
--| definition component which lists the sorts of the dummy variables and the
--| definition's expression.
--|
--| Defs are indexed by TermID and picked out by `getTerm`.
--|
--| `DDef : TermID -> Ctx -> DepType -> option (list SortID, SExpr) -> Decl`
@_ def DDef (id args ret def: nat): nat =
$ b1 (b0 (id, args, ret, def)) $;
--| A `theorem` declaration is exactly the same structure as an `axiom`, but
--| the interpretation is different - theorems require proofs, while axioms are
--| added in the spec.
--|
--| Theorems are not indexed, they are simply found by statement.
--|
--| `DThm : Ctx -> list SExpr -> SExpr -> Decl`
@_ def DThm (vs hs ret: nat): nat = $ b1 (b1 (vs, hs, ret)) $;
--| This function extracts the `SortData` for a sort given by `SortID`.
--|
--| `getSD : Env -> SortID -> SortData -> Bool`
@_ def getSD (env id sd: nat): wff = $ DSort id sd IN env $;
--| This function gets the term and definition data for a term.
--|
--| `getTerm : Env -> TermID -> Ctx -> DepType -> option (list SortID, SExpr) -> Bool`
@_ def getTerm (env id a r v: nat): wff =
$ v = 0 /\ DTerm id a r IN env \/ DDef id a r v IN env $;
theorem getTerm_ltid: $ getTerm env x a r v -> x < env /\ a < env /\ r < env /\ v < env $ =
(focus
'(eor _ _)
(focus
'(imp @ syl5 (rsyl lmemlt @ lelttr @ leb0tr @ leb0tr b1leid) @
mpbiri _ (imeq2d @ aneq2d lteq1))
'(iand (iand (iand _ _) _) (lelttr le01))
'(lelttr leprid1)
'(lelttr @ lepr2tr leprid1)
'(lelttr @ lepr2tr leprid2))
(focus
'(rsyl lmemlt @ rsyl (lelttr @ leb1tr b0leid) _)
'(iand (iand (iand _ _) _) _)
'(lelttr leprid1)
'(lelttr @ lepr2tr leprid1)
'(lelttr @ lepr2tr @ lepr2tr leprid1)
'(lelttr @ lepr2tr @ lepr2tr leprid2)));
theorem getTerm_retltid: $ getTerm env x a r v -> r < env $ = '(rsyl getTerm_ltid anlr);
theorem getTerm_valltid: $ getTerm env x a r v -> v < env $ = '(rsyl getTerm_ltid anr);
--| This function gets the data for an axiom or theorem.
--|
--| `getThm : Env -> Ctx -> list SExpr -> SExpr -> Bool`
@_ def getThm (env a h r: nat): wff =
$ DAxiom a h r IN env \/ DThm a h r IN env $;
--| Is this ID a valid sort?
--|
--| `isSort : Env -> SortID -> Bool`
@_ def isSort (env s .sd: nat): wff = $ E. sd getSD env s sd $;
--| Looks this variable up in the context, and reports whether it represents a
--| bound variable.
--|
--| `isBound : Ctx -> VarID -> Bool`
@_ def isBound (ctx x .s: nat): wff = $ E. s nth x ctx = suc (PBound s) $;
--| Checks if this a well formed dependent type in the context. A DepType is a
--| pair (SortID, set VarID) giving the sort and the variable dependencies.
--|
--| `DepType : Env -> Ctx -> DepType -> Bool`
@_ def DepType (env ctx ty .x: nat): wff =
$ isSort env (fst ty) /\ A. x (x e. snd ty -> isBound ctx x) $;
@_ local def Ctx_aux (env ctx: nat): nat =
$ rlrec 1 (\\ ctx2, \\ bi, \ ih, ih * nat (bi e. Sum
{s | E. sd (getSD env s sd /\ ~ sStrict sd)}
{svs | DepType env ctx2 svs})) ctx $;
theorem Ctx_aux0 (env: nat): $ Ctx_aux env 0 = 1 $ = (named 'rlrec0);
theorem Ctx_auxS (env: nat): $ Ctx_aux env (ctx |> bi) = Ctx_aux env ctx *
nat (bi e. Sum
{s | E. sd (getSD env s sd /\ ~ sStrict sd)}
{svs | DepType env ctx svs}) $ =
(named '(! eqtr _ $ _ @ (_, _, Ctx_aux env ctx) $ _ rlrecS @
appslame @ appslamed @ applamed @ muleqd anr @
nateqd @ eleqd anlr @ Sumeq2d @ abeqd @ DepTypeeq2d anll));
--| Checks if this a well formed context. A context can be extended with a bound
--| variable binder if the sort of the binder is not `strict`.
--| Note `Ctx = list Binder`.
--|
--| `Ctx : Env -> Ctx -> Bool`
@_ abstract def Ctx (env ctx: nat): wff = $ true (Ctx_aux env ctx) $;
pub theorem Ctx0 (env: nat): $ Ctx env 0 $ = '(mpbir (trueeq Ctx_aux0) true1);
pub theorem CtxBound (env ctx s: nat) {sd: nat}: $ Ctx env (ctx |> PBound s) <->
Ctx env ctx /\ E. sd (getSD env s sd /\ ~ sStrict sd) $ =
(named '(bitr (trueeq Ctx_auxS) @ bitr truemul @ aneq2i @
bitr truenat @ bitr Suml @ elabe @ exeqd @ aneq1d getSDeq2));
pub theorem CtxReg (env ctx s vs: nat): $ Ctx env (ctx |> PReg s vs) <->
Ctx env ctx /\ DepType env ctx (s, vs) $ =
(named '(bitr (trueeq Ctx_auxS) @ bitr truemul @ aneq2i @
bitr truenat @ bitr Sumr @ elabe DepTypeeq3));
@_ local def ExprBi_aux (ctx e ih: nat): set = $ Sum
{s | E. v (e = SVar v /\ nth v ctx = suc (PBound s))}
{svs | fst svs e. ih} $;
@_ local def Expr_aux (env ctx: nat): set =
$ SExprRec
(\ v, lower {s | E. bi (nth v ctx = suc bi /\ binderSort bi = s)})
(\\ f, \\ xs, \ ih, lower {s | E. args E. ret E. o
(getTerm env f args ret o /\
zip xs ih, args e. all2 (S\ x, ExprBi_aux ctx (fst x) (snd x)) /\
s = fst ret)}) $;
theorem Expr_aux_Var: $ Expr_aux env ctx @ SVar v =
lower {s | E. bi (nth v ctx = suc bi /\ binderSort bi = s)} $ =
(named '(eqtr SExprRec_Var @ applame @
lowereqd @ abeqd @ exeqd @ aneq1d @ eqeq1d @ ntheq1));
theorem Expr_aux_App: $ Expr_aux env ctx @ SApp f es =
lower {s | E. args E. ret E. o
(getTerm env f args ret o /\
zip es (map (Expr_aux env ctx) es), args e. all2 (S\ x, ExprBi_aux ctx (fst x) (snd x)) /\
s = fst ret)} $ =
(named '(eqtr SExprRec_App @
! appslame _ $_, map (Expr_aux _ _) _$ _ _ _ @ appslamed @ applamed @
lowereqd @ abeqd @ exeqd @ exeqd @ exeqd @ aneq1d @ aneqd
(getTermeq2d anll)
(eleq1d @ preq1d @ zipeqd anlr anr)));
--| These mutually recursive functions check if an expression `e` is well-typed
--| with sort `s`, and that `e` is well-typed and valid for entry into a binder
--| `bi`. The main difference is that for an expression to be valid for a
--| BV binder, the expression must itself be a bound variable.
--|
--| `Expr : Env -> Ctx -> SExpr -> SortID -> Bool`
@_ abstract def Expr (env ctx e s: nat): wff = $ s e. Expr_aux env ctx @ e $;
--| `ExprBi : Env -> Ctx -> SExpr -> Binder -> Bool`
@_ abstract def ExprBi (env ctx e bi: nat): wff =
$ bi e. ExprBi_aux ctx e (Expr_aux env ctx @ e) $;
theorem ExprBi_aux_map:
$ zip es (map (Expr_aux env ctx) es), args e. all2 (S\ p, ExprBi_aux ctx (fst p) (snd p)) <->
es, args e. all2 (S\ x, {a | ExprBi env ctx x a}) $ =
(named @ focus
(def h '(bieqd (eleq1d preq2) (eleq1d preq2)))
'(eale ,h ,(induct '(listind) 'es _ _))
'(ax_gen @ bitr (eleq1 @ preq1 zip01) @ bitr4 all201 all201)
'(sylbi (cbval ,h) @ iald @
bitr4g (bitr (eleq1 @ preq1 @ eqtr (zipeq2 mapS) zipS) all2S1) all2S1 @
exeqd @ syl exeq @ alimi @ syl aneq2a @ exp @ aneqd (a1i _) anl)
'(bitr4 (elsabe @ sylbir (eqeq1 fstsnd) @ sylbi prth ,eqtac) @
elsabe @ eleq2d @ syl5eqs abid2 ,eqtac));
pub theorem ExprVar (env ctx v s: nat) {bi: nat}: $ Expr env ctx (SVar v) s <->
E. bi (nth v ctx = suc bi /\ binderSort bi = s) $ =
(named @ focus
'(bitr (elneq2 Expr_aux_Var) @ bitr (ellower @ subsnfin @ subsnss _ subsnsn) @
elabe @ exeqd @ aneq2d eqeq2)
'(mpbi ssab1 @ ax_gen @ eex @ sylibr elsn @ eqtr2d _ anr)
'(binderSorteqd @ syl6eq sucsub1 @ subeq1d anl));
pub theorem ExprApp (env ctx f xs s: nat) {args ret o x a: nat}:
$ Expr env ctx (SApp f xs) s <-> E. args E. ret E. o
(getTerm env f args ret o /\
xs, args e. all2 (S\ x, {a | ExprBi env ctx x a}) /\
s = fst ret) $ =
(named @ focus
'(bitr (elneq2 Expr_aux_App) @ bitr (ellower @ finss _ ltfin) @
elabe @ exeqd @ exeqd @ exeqd @ aneqd (a1i @ aneq2i ExprBi_aux_map) eqeq1)
'(mpbi ssab @ ax_gen @ eex @ eex @ eex @ mpbird (lteq1d anr) @ anwll @
syl (lelttr fstleid) getTerm_retltid));
pub theorem ExprBiBound (env ctx e s: nat) {v: nat}:
$ ExprBi env ctx e (PBound s) <->
E. v (e = SVar v /\ nth v ctx = suc (PBound s)) $ =
(named '(bitr Suml @ elabe @ exeqd @ aneq2d @ eqeq2d @ suceqd @ PBoundeq));
pub theorem ExprBiReg (env ctx e s vs: nat):
$ ExprBi env ctx e (PReg s vs) <-> Expr env ctx e s $ =
(named '(bitr Sumr @ elabe @ eleq1d @ syl6eq fstpr fsteq));
--| Is this a type correct expression of provable type? This is used to
--| typecheck expressions appearing in hypotheses and conclusions of
--| axiom/theorem.
--|
--| `ExprProv : Env -> Ctx -> SExpr -> Bool`
@_ def ExprProv (env ctx e .s .sd: nat): wff =
$ E. s E. sd (Expr env ctx e s /\ getSD env s sd /\ sProvable sd) $;
--| A helper function to add dummy variables to the context.
--|
--| `appendDummies : Ctx -> list SortID -> Ctx`
@_ def appendDummies (ctx ds .d: nat): nat = $ ctx ++ map (\ d, PBound d) ds $;
@_ local def HasVar_aux (ctx: nat): set =
$ SExprRec
(\ u, lower {v | E. s nth u ctx = suc (PBound s) /\ u = v \/
E. s E. vs (nth u ctx = suc (PReg s vs) /\ v e. vs)})
(\\ f, \\ xs, \ ih, lower {v | E. s (s IN ih /\ v e. s)}) $;
--| Does this expression contain any occurrence of the variable `v`? This check
--| ignores bound variables, "metamath style". We use this stricter check for
--| verifying theorem applications.
--|
--| `HasVar : Ctx -> SExpr -> VarID -> Bool`
@_ abstract def HasVar (ctx e v: nat): wff = $ v e. HasVar_aux ctx @ e $;
theorem HasVar_Var: $ HasVar ctx (SVar u) v <->
E. s nth u ctx = suc (PBound s) /\ u = v \/
E. s E. vs (nth u ctx = suc (PReg s vs) /\ v e. vs) $ =
(named @ focus
'(bitr (elneq2 @ eqtr SExprRec_Var @ applame @ lowereqd @ abeqd @
oreqd (aneqd (exeqd @ eqeq1d ntheq1) eqeq1) (exeqd @ exeqd @ aneq1d @ eqeq1d ntheq1)) @
bitr (ellower @ finss (mpbi ssab @ ax_gen _) ltfin) @
elabe @ oreqd (aneq2d eqeq2) (exeqd @ exeqd @ aneq2d eleq1))
'(eor
(mpbid (lteq1d anr) @ anwl @ eex @ syl (mpi lenleid ltletr) @
sylib nthne0 sucne0)
(eex @ eex @ lttrd (anwr ellt) @
anwl @ syl (lelttr @ leb1tr leprid2) @ syl lmemlt nthlmem)));
pub theorem HasVarBound (ctx u v s: nat):
$ nth u ctx = suc (PBound s) -> (HasVar ctx (SVar u) v <-> u = v) $ =
(named '(syl5bb HasVar_Var @ bitrd
(syl bior2 @ nexd @ nexd @ mpi b0neb1 @
con3d @ exp @ sylib peano2 @ eqtr3d anl anrl)
(syl bian1 @ iexe @ eqeq2d @ suceqd PBoundeq)));
pub theorem HasVarReg (ctx u v s vs: nat):
$ nth u ctx = suc (PReg s vs) -> (HasVar ctx (SVar u) v <-> v e. vs) $ =
(named '(syl5bb HasVar_Var @ bitrd
(syl bior1 @ syl (con3 anl) @ nexd @ mpi b0neb1 @ con3d @ exp @
sylib peano2 @ eqtr3d anr anl)
(syl6bb (exeqe biidd) @ exeqd @ syl6bb (exeqe @ aneq2d elneq2) @ exeqd @
syl6bb anlass @ syl6bb anass @ aneq1d @
syl6bb (bitr peano2 @ bitr b1can @ bitr eqcomb prth) eqeq1)));
pub theorem HasVarApp (ctx f es v: nat) {e: nat}:
$ HasVar ctx (SApp f es) v <-> E. e (e IN es /\ HasVar ctx e v) $ =
(named @ focus
'(bitr (elneq2 @ eqtr {SExprRec_App : $ _ = _ @ (_, _, map (HasVar_aux ctx) _) $} @
appslame @ appslamed @ applamed @ lowereqd @ abeqd @
exeqd @ aneq1d @ lmemeq2d anr) @
bitr (ellower @ finss (mpbi ssab @ ax_gen _) ltfin) @
bitr (elabe @ exeqd @ syl6bbr exan2 @ aneqd (a1i lmemmap) eleq1) @
bitr excomb @ exeqi @ bitr (exeqi @ bitr (aneq1i ancomb) anass) @
exeqe @ aneq2d elneq2)
'(eex @ lttrd (anwr ellt) (anwl lmemlt)));
--| A helper function for `Free`. This constructs the set `_V \ deps(a)` if `a`
--| is a regular argument and `(/)` if `a` is a bound argument.
--|
--| `MaybeFreeArgs : list SExpr -> Binder -> VarID -> Bool`
@_ def MaybeFreeArgs (es a v .s .vs .u: nat): wff =
$ E. s E. vs (a = PReg s vs /\ ~(E. u (u e. vs /\ nth u es = suc (SVar v)))) $;
@_ local def Free_aux (env ctx v: nat): set =
$ SExprRec
(\ u, nat (HasVar ctx (SVar u) v))
(\\ f, \\ es, \ ih, nat (E. args E. r E. rs E. o
(getTerm env f args (r, rs) o /\
(ih, args e. ex2 (S\ ih1, {a | true ih1 /\ MaybeFreeArgs es a v}) \/
E. u (u e. rs /\ nth u es = suc (SVar v)))))) $;
--| Does this expression contain any _free_ occurrence of the variable `v`?
--| This is the more complex binder-respecting check. Intuitively, if
--| `term foo {x y: set} (ph: set x): set y;`, then `foo` binds occurrences of
--| `x` in `ph`, and adds a dependency on `y` regardless. We might write this
--| as `FV(foo x y ph) = (FV(ph) \ {x}) u {y}`, but the definition below is
--| for arbitrary binding structures.
--|
--| `Free : Env -> Ctx -> SExpr -> VarID -> Bool`
@_ abstract def Free (env ctx e v: nat): wff = $ true (Free_aux env ctx v @ e) $;
pub theorem FreeVar (env ctx u v: nat):
$ Free env ctx (SVar u) v <-> HasVar ctx (SVar u) v $ =
(named '(bitr
(trueeq @ eqtr SExprRec_Var @ applame @ nateqd @ HasVareq2d SVareq)
truenat));
pub theorem FreeApp (env ctx f es v: nat) {args r rs o e a u: nat}:
$ Free env ctx (SApp f es) v <-> E. args E. r E. rs E. o
(getTerm env f args (r, rs) o /\
(es, args e. ex2 (S\ e, {a | Free env ctx e v /\ MaybeFreeArgs es a v}) \/
E. u (u e. rs /\ nth u es = suc (SVar v)))) $ =
(named @ focus
'(bitr (trueeq _) truenat)
'(eqtr {SExprRec_App : $ _ = _ @ (_, _, map (Free_aux env ctx v) _) $} _)
'(appslame @ appslamed @ applamed @
nateqd @ exeqd @ exeqd @ exeqd @ exeqd @ aneqd (getTermeq2d anll) @
oreqd _ @ exeqd @ aneq2d @ eqeq1d @ ntheq2d anlr)
'(syl6bb _ (eleqd (preq1d anr) (ex2eqd @ sabeqd @ abeqd @ aneq2d @ MaybeFreeArgseq1d anlr)))
'(bitr4 elex2 @ bitr4 elex2 @ aneq (eqeq1 maplen) @ exeqi @
bitr excomb @ bitr (exeqi _) excomb)
'(bitr (exeqi @ bitr4 (aneq1i @ bitr4 (aneq1i mapnthb) exan2) exan2) @
bitr excomb @ exeqi @
bitr (exeqi @ bitr (aneq1i @ bitr (aneq1i ancomb) anass) anass) @
exeqe @ aneq2d @ bitr4g _ _ @ aneq1d trueeq)
'(elsabe @ elabed ,eqtac) '(elsabe @ elabed ,eqtac));
--| Is this a valid term in the given environment? A term is valid if
--| the argument list is valid, the return type is valid, and the return sort
--| is not `pure` (because `pure` sorts are not allowed to have term
--| constructors).
--|
--| `TermOk : Env -> TermID -> Ctx -> DepType -> Bool`
@_ def TermOk (env id args ret .a .r .v .sd: nat): wff =
$ ~E. a E. r E. v getTerm env id a r v /\
Ctx env args /\ DepType env args ret /\
E. sd (getSD env (fst ret) sd /\ ~ sPure sd) $;
--| Is this a valid definition in the given environment? A definition is valid
--| if it is a valid term, and the definition typechecks, and all free variables
--| are declared in the return type. (Note in particular that dummies cannot
--| appear in the return type dependencies, so this ensures that all dummies are
--| bound by the definition.)
--|
--| `DefOk : Env -> TermID -> Ctx -> DepType -> Bool`
@_ def DefOk (env id args ret ds e .ctx .v .s .sd: nat): wff =
$ TermOk env id args ret /\ [ appendDummies args ds / ctx ]
(Ctx env ctx /\ Expr env ctx e (fst ret) /\
A. v (Free env ctx e v -> v e. snd ret \/
E. sd E. s (nth v ctx = suc (PBound s) /\
getSD env s sd /\ sFree sd))) $;
@_ local def DeclThm_aux (env: nat): set =
$ S\ args, S\ hs, {ret | Ctx env args /\ all {x | ExprProv env args x} (ret : hs)} $;
theorem DeclThm_aux_val (env args hs ret: nat) {x: nat}:
$ args, hs, ret e. DeclThm_aux env <->
Ctx env args /\ all {x | ExprProv env args x} (ret : hs) $ =
(named '(elsabe @ elsabed @ elabed @ aneqd (Ctxeq2d anll) @
alleqd (abeqd @ ExprProveq2d anll) (conseqd anr anlr)));
--| Is this a valid declaration in the environment?
--|
--| `Decl : Env -> Decl -> Bool`
@_ abstract def Decl (env d: nat): wff =
$ d e. Sum
(Sum
(Sum
(S\ id, {sd | ~E. sd2 getSD env id sd2})
(S\ id, S\ args, {ret | TermOk env id args ret}))
(DeclThm_aux env))
(Sum
(S\ id, S\ args, S\ ret, {o | TermOk env id args ret /\
A. ds A. e (o = suc (ds, e) -> DefOk env id args ret ds e)})
(DeclThm_aux env)) $;
pub theorem DeclSort (env id sd: nat) {sd2: nat}:
$ Decl env (DSort id sd) <-> ~E. sd2 getSD env id sd2 $ =
(named '(bitr Suml @ bitr Suml @ bitr Suml @ elsabe @ elabed ,eqtac));
pub theorem DeclTerm (env id args ret: nat):
$ Decl env (DTerm id args ret) <-> TermOk env id args ret $ =
(named '(bitr Suml @ bitr Suml @ bitr Sumr @ elsabe @ elsabed @ elabed ,eqtac));
pub theorem DeclAxiom (env args hs ret: nat) {x: nat}:
$ Decl env (DAxiom args hs ret) <->
Ctx env args /\ all {x | ExprProv env args x} (ret : hs) $ =
(named '(bitr Suml @ bitr Sumr DeclThm_aux_val));
pub theorem DeclDef (env id args ret: nat) {ds e o: nat}:
$ Decl env (DDef id args ret o) <-> TermOk env id args ret /\
A. ds A. e (o = suc (ds, e) -> DefOk env id args ret ds e) $ =
(named '(bitr Sumr @ bitr Suml @ elsabe @ elsabed @ elsabed @ elabed @
aneqd (TermOkeqd eqidd an3l anllr anlr) @ aleqd @ aleqd @
imeqd (eqeq1d anr) @ DefOkeqd eqidd an3l anllr anlr eqidd eqidd));
pub theorem DeclThm (env args hs ret: nat) {x: nat}:
$ Decl env (DThm args hs ret) <->
Ctx env args /\ all {x | ExprProv env args x} (ret : hs) $ =
(named '(bitr Sumr @ bitr Sumr DeclThm_aux_val));
@_ local def Env_aux (e: nat): nat =
$ rlrec 1 (\\ e1, \\ s, \ ih, ih * nat (Decl e1 s)) e $;
--| This defines a valid mm0 specification. These are well formed ASTs for which
--| we can assign a provability predicate.
--|
--| `Env : Env -> Bool`
@_ abstract def Env (e: nat): wff = $ true (Env_aux e) $;
pub theorem Env0: $ Env 0 $ = (named '(mpbir (trueeq rlrec0) true1));
pub theorem EnvS (e s: nat): $ Env (e |> s) <-> Env e /\ Decl e s $ =
(named '(bitr
(trueeq @ eqtr {rlrecS : $_ = _ @ (_, _, Env_aux _)$} @
appslame @ appslamed @ applamed @ muleqd anr @ nateqd @ Decleqd anll anlr)
(bitr truemul @ aneq2i truenat)));
@_ local def EnvExtend_aux (e2_: nat): nat =
$ rlrec (sn 0)
(\\ e2, \\ d, \ ih, case
(case
(case
(\\ id, \ sd, lower {e1 | E. e (e1 = e |> DSort id sd /\ e e. ih)})
(\\ id, \\ a, \ r, lower {e1 | E. e (e1 = e |> DTerm id a r /\ e e. ih)}))
(\\ a, \\ h, \ r, lower {e1 | E. e (e1 = e |> DAxiom a h r /\ e e. ih)}))
(case
(\\ id, \\ a, \\ r, \ o, lower {e1 |
E. e E. o2 ((o2 != 0 -> o2 = o) /\
e1 = e |> DDef id a r o2 /\ e e. ih) \/
e1 e. ih})
(\\ a, \\ h, \ r, lower {e1 |
E. e (e1 = e |> DThm a h r /\ e e. ih) \/
e1 e. ih}))
@ d)
e2_ $;
--| `EnvExtend e1 e2` means that environment `e2` is an extension of `e1`,
--| meaning that all sorts, terms, and axioms are preserved, but abstract defs
--| may be provided definitions, and new defs and theorems can be added.
--|
--| `EnvExtend : Env -> Env -> Bool`
@_ abstract def EnvExtend (e1 e2: nat): wff = $ e1 e. EnvExtend_aux e2 $;
pub theorem EnvExtend0 (e: nat): $ EnvExtend e 0 <-> e = 0 $ =
(named '(bitr (elneq2 rlrec0) elsn));
theorem EnvExtend_lem {ih} (p: wff e1 e o2) (q: wff e o2) (A: nat o2)
(h1: $ ih = EnvExtend_aux E2 -> case
(case
(case
(\\ id, \ sd, lower {e1 | E. e (e1 = e |> DSort id sd /\ e e. ih)})
(\\ id, \\ a, \ r, lower {e1 | E. e (e1 = e |> DTerm id a r /\ e e. ih)}))
(\\ a, \\ h, \ r, lower {e1 | E. e (e1 = e |> DAxiom a h r /\ e e. ih)}))
(case
(\\ id, \\ a, \\ r, \ o, lower {e1 |
E. e E. o2 ((o2 != 0 -> o2 = o) /\
e1 = e |> DDef id a r o2 /\ e e. ih) \/
e1 e. ih})
(\\ a, \\ h, \ r, lower {e1 |
E. e (e1 = e |> DThm a h r /\ e e. ih) \/
e1 e. ih}))
@ d
= lower {e1 | p} $)
(h2: $ e1 = E1 -> (p <-> q) $)
(h3: $ p -> E. e E. x (x e. A /\ e1 = e |> x /\ EnvExtend e E2) \/ EnvExtend e1 E2 $):
$ EnvExtend E1 (E2 |> d) <-> q $ =
(named @ focus
'(bitr (elneq2 @ eqtr {rlrecS : $_ = _ @ (_, _, EnvExtend_aux _)$} @
appslame @ appslamed @ applamed @ eqtrd (appeq2d anlr) (anwr h1)) @
bitr (ellower _) (elabe h2))
'(finss (mpbi ssab @ ax_gen _) @
! ltfin $ lower ((\ u, fst u |> snd u) |` Xp (EnvExtend_aux E2) A) + EnvExtend_aux E2 $)
'(rsyl h3 @ eor
(ltletrd (eex @ eex @ mpbird (lteq1d anlr) _) (a1i leaddid1))
(ltletrd ellt (a1i leaddid2)))
'(lelttrd (a1i leprid2) @ syl ellt @
sylibr (ellower @ finlam @ xpfin finns finns) @
sylibr prelres @ iand (a1i _) @ sylibr prelxp @ iand anr anll)
'(mpbir ellam @ iexe (eqeq2d @ preqd id _) eqid)
'(snoceqd (syl6eq fstpr fsteq) (syl6eq sndpr sndeq)));
theorem EnvExtend_lem2 (p: wff e x)
(h: $ p -> E. e (e1 = e |> A /\ EnvExtend e E2) \/ EnvExtend e1 E2 $):
$ p -> E. e E. x (x e. sn A /\ e1 = e |> x /\ EnvExtend e E2) \/ EnvExtend e1 E2 $ =
'(rsyl h @ orim1 @ eximi @ iexe @ aneq1d @
bitrd (aneqd (a1i elsn) @ eqeq2d snoceq2) bian1);
pub theorem EnvExtendSort (e1 e2 id sd: nat) {e: nat}:
$ EnvExtend e1 (e2 |> DSort id sd) <->
E. e (e1 = e |> DSort id sd /\ EnvExtend e e2) $ =
(named '(EnvExtend_lem
(syl5eq casel @ syl5eq casel @ syl5eq casel @ appslamed @ applamed @
lowereqd @ abeqd @ exeqd @
aneqd (eqeq2d @ snoceq2d @ DSorteqd anlr anr) (elneq2d anll))
(exeqd @ aneq1d eqeq1)
(EnvExtend_lem2 orl)));
pub theorem EnvExtendTerm (e1 e2 id a r: nat) {e: nat}:
$ EnvExtend e1 (e2 |> DTerm id a r) <->
E. e (e1 = e |> DTerm id a r /\ EnvExtend e e2) $ =
(named '(EnvExtend_lem
(syl5eq casel @ syl5eq casel @ syl5eq caser @ appslamed @ appslamed @ applamed @
lowereqd @ abeqd @ exeqd @
aneqd (eqeq2d @ snoceq2d @ DTermeqd anllr anlr anr) (elneq2d an3l))
(exeqd @ aneq1d eqeq1)
(EnvExtend_lem2 orl)));
pub theorem EnvExtendAxiom (e1 e2 a h r: nat) {e: nat}:
$ EnvExtend e1 (e2 |> DAxiom a h r) <->
E. e (e1 = e |> DAxiom a h r /\ EnvExtend e e2) $ =
(named '(EnvExtend_lem
(syl5eq casel @ syl5eq caser @ appslamed @ appslamed @ applamed @
lowereqd @ abeqd @ exeqd @
aneqd (eqeq2d @ snoceq2d @ DAxiomeqd anllr anlr anr) (elneq2d an3l))
(exeqd @ aneq1d eqeq1)
(EnvExtend_lem2 orl)));
pub theorem EnvExtendDef (e1 e2 id a r o: nat) {e o2: nat}:
$ EnvExtend e1 (e2 |> DDef id a r o) <->
E. e E. o2 ((o2 != 0 -> o2 = o) /\
e1 = e |> DDef id a r o2 /\ EnvExtend e e2) \/
EnvExtend e1 e2 $ =
(named @ focus
'(EnvExtend_lem
(syl5eq caser @ syl5eq casel @ appslamed @ appslamed @ appslamed @ applamed @
lowereqd @ abeqd @ oreqd
(exeqd @ exeqd @ aneqd
(aneqd (anwr @ imeq2d eqeq2) @
eqeq2d @ snoceq2d @ DDefeqd an3lr anllr anlr eqidd)
(elneq2d an4l))
(elneq2d an4l))
(oreqd (exeqd @ exeqd @ aneq1d @ aneq2d eqeq1) eleq1)
_)
'(orim1 @ eximi @ eex @ iexde @ imp @ com12 @ anim1d @
animd (syl5 _ (bi2d eleq1)) (bi2d @ eqeq2d snoceq2))
'(sylibr elupto @ sylib leltsuc _)
'(sylib b1le @ sylib b0le @ sylib lepr2 @ sylib lepr2 @ sylib lepr2 _)
'(eor (mpbiri le01 leeq1) eqle));
pub theorem EnvExtendThm (e1 e2 a h r: nat) {e: nat}:
$ EnvExtend e1 (e2 |> DThm a h r) <->
E. e (e1 = e |> DThm a h r /\ EnvExtend e e2) \/
EnvExtend e1 e2 $ =
(named '(EnvExtend_lem
(syl5eq caser @ syl5eq caser @ appslamed @ appslamed @ applamed @
lowereqd @ abeqd @ oreqd
(exeqd @ aneqd (eqeq2d @ snoceq2d @ DThmeqd anllr anlr anr) (elneq2d an3l))
(elneq2d an3l))
(oreqd (exeqd @ aneq1d eqeq1) eleq1)
(EnvExtend_lem2 id)));
------------------
-- Verification --
------------------
@_ local def substExpr_aux (subst: nat): set =
$ SExprRec (\ v, nth v subst - 1) (\\ f, \\ xs, \ ih, SApp f ih) $;
--| This performs simultaneous substitution of the variables in `e` with the
--| expressions in `subst`.
--|
--| `substExpr : list SExpr -> SExpr => SExpr`
@_ abstract def substExpr (subst e: nat): nat = $ substExpr_aux subst @ e $;
theorem substExpr_aux_lam: $ substExpr_aux subst == \ e, substExpr subst e $ =
(named '(eqscom (lameqi applam)));
pub theorem substExprVar (subst v: nat):
$ substExpr subst (SVar v) = nth v subst - 1 $ =
(named '(eqtr SExprRec_Var @ applame @ subeq1d ntheq1));
pub theorem substExprApp (subst f es: nat) {e: nat}:
$ substExpr subst (SApp f es) = SApp f (map (\ e, substExpr subst e) es) $ =
(named '(eqtr SExprRec_App @
! appslame _ $_, map (substExpr_aux _) _$ _ _ _ @ appslamed @ applamed @
SAppeqd anll @ syl6eq (mapeq1 substExpr_aux_lam) anr));
--| A CExpr is a convertibility proof:
--| `CRefl e : e = e`
--|
--| `CRefl : SExpr -> CExpr`
@_ def CRefl (e: nat): nat = $ b0 (b0 (b0 e)) $;
--| If `p : e = e'`, then `CSymm p : e' = e`
--|
--| `CSymm : CExpr -> CExpr`
@_ def CSymm (p: nat): nat = $ b0 (b0 (b1 p)) $;
--| If `p : e1 = e2` and `q : e2 = e3`, then `CTrans p q : e1 = e3`
--|
--| `CTrans : CExpr -> CExpr -> CExpr`
@_ def CTrans (p q: nat): nat = $ b0 (b1 (p, q)) $;
--| If `{p : e = e'}` then `CCong f {p} : f {e} = f {e'}`
--| where the braces denote iteration
--|
--| `CCong : TermID -> list CExpr -> CExpr`
@_ def CCong (f cs: nat): nat = $ b1 (b0 (f, cs)) $;
--| If `f {x} := {y}. e'`, then
--| `CCong f {z} p : f {e} = e'[{x}, {y} -> {e}, {z}]`
--|
--| `CUnfold : TermID -> list Expr -> list VarID -> CExpr`
@_ def CUnfold (f es zs: nat): nat = $ b1 (b1 (f, es, zs)) $;
theorem CSymm_ltid: $ c < CSymm c $ = '(ltb0tr @ ltb0tr b1ltid);
theorem CTrans_ltid1: $ c1 < CTrans c1 c2 $ = '(ltb0tr @ ltb1tr leprid1);
theorem CTrans_ltid2: $ c2 < CTrans c1 c2 $ = '(ltb0tr @ ltb1tr leprid2);
theorem CCong_ltid: $ c IN cs -> c < CCong f cs $ =
'(rsyl lmemlt @ mpi (ltb1tr @ leb0tr leprid2) lttr);
@_ local def CExprRec (R S T C U: set): set =
$ \ e, srec (\ ih, case
(case
(case R (\ c, S @ (c, ih @ c)))
(\\ c1, \ c2, T @ (c1, c2, ih @ c1, ih @ c2)))
(case
(\\ f, \ cs, C @ (f, cs, map ih cs))
U) @ size (Dom ih)) e $;
theorem CExprRec_val: $ CExprRec R S T C U @ e = case
(case
(case R (\ c, S @ (c, (CExprRec R S T C U |` upto e) @ c)))
(\\ c1, \ c2, T @ (c1, c2,
(CExprRec R S T C U |` upto e) @ c1,
(CExprRec R S T C U |` upto e) @ c2)))
(case
(\\ f, \ cs, C @ (f, cs, map (CExprRec R S T C U |` upto e) cs))
U) @ e $ =
(named '(eqtr (applame sreceq2) @
! eqtr _ $ _ @ lower (CExprRec R S T C U |` _) $ _ srecval @
!! applame ih @ appeqd (rsyl (bi2 @ eqlower2 @ finlam finns) ,eqtac) sreclem));
theorem CExprRec_CRefl: $ CExprRec R S T C U @ CRefl e = R @ e $ =
(named '(eqtr CExprRec_val @ eqtr casel @ eqtr casel casel));
theorem CExprRec_CSymm:
$ CExprRec R S T C U @ CSymm c = S @ (c, CExprRec R S T C U @ c) $ =
(named '(eqtr CExprRec_val @ eqtr casel @ eqtr casel @ eqtr caser @
applame @ appeq2d @ preqd id @ syl6eq (resapp @ mpbir elupto CSymm_ltid) appeq2));
theorem CExprRec_CTrans:
$ CExprRec R S T C U @ CTrans c1 c2 =
T @ (c1, c2, CExprRec R S T C U @ c1, CExprRec R S T C U @ c2) $ =
(named '(eqtr CExprRec_val @ eqtr casel @ eqtr caser @
appslame @ applamed @ appeq2d @ syl6eq (preq2 @ preq2 @
preq (resapp @ mpbir elupto CTrans_ltid1) (resapp @ mpbir elupto CTrans_ltid2))
,eqtac));
theorem CExprRec_CCong:
$ CExprRec R S T C U @ CCong f cs = C @ (f, cs, map (CExprRec R S T C U) cs) $ =
(named '(eqtr CExprRec_val @ eqtr caser @ eqtr casel @
appslame @ applamed @ appeq2d @ preqd anl @ preqd anr @ syl6eq
(mapeqg @ mpbir allal @ !! ax_gen e @ syl resapp @ sylibr elupto CCong_ltid)
(mapeq2d anr)));
theorem CExprRec_CUnfold:
$ CExprRec R S T C U @ CUnfold f es zs = U @ (f, es, zs) $ =
(named '(eqtr CExprRec_val @ eqtr caser caser));
theorem indd_aux2 (G p: wff x) (e: $ x = c -> (p <-> q) $)
(h: $ c < b $): $ G /\ A. z (z < a -> [z / x] p) /\ a = b -> q $ =
'(sylc (eale (imeqd lteq1 (syl6bb (sbe e) sbeq1))) anlr @ mpbiri h @ lteq2d anr);
theorem CExpr_indd {x f e c es zs cs} (n)
(px: wff x) (pi: wff c) (pi2: wff c2) (pr: wff e) (ps: wff c) (pt: wff c c2)
(pc: wff f cs) (pu: wff f es zs)
(hn: $ x = n -> (px <-> pn) $)
(hr: $ x = CRefl e -> (px <-> pr) $)
(hs: $ x = CSymm c -> (px <-> ps) $)
(ht: $ x = CTrans c c2 -> (px <-> pt) $)
(hc: $ x = CCong f cs -> (px <-> pc) $)
(hu: $ x = CUnfold f es zs -> (px <-> pu) $)
(hi: $ x = c -> (px <-> pi) $)
(hi2: $ x = c2 -> (px <-> pi2) $)
(ir: $ G -> pr $)
(is: $ G /\ pi -> ps $)
(it: $ G /\ pi /\ pi2 -> pt $)
(ic: $ G /\ all {x | px} cs -> pc $)
(iu: $ G -> pu $): $ G -> pn $ =
'(sylib (sbe hn) @ !! indstr z w sbeq1 sbeq1 ,(split-sop '{
{{($CRefl e$ => (indd_aux hr @ anwll ir)) +
($CSymm c$ => (indd_aux hs @ mpd (indd_aux2 hi CSymm_ltid) (anwll @ exp is)))} +
($CTrans c c2$ => (indd_aux ht @
mpd (indd_aux2 hi2 CTrans_ltid2) @ mpd (indd_aux2 hi CTrans_ltid1) @
anwll @ exp @ exp it))} +
{($CCong f cs$ => (indd_aux hc @ sylan ic anll @
sylibr (alleq1 cbvabs) @ sylibr allal @
sylc (alimd @ imim1d @ syl5 CCong_ltid @ bi2d lteq2) anr anlr)) +
($CUnfold f es zs$ => (indd_aux hu @ anwll iu))}
}));
-- A `VExpr` is a proof term.
--| A `VHyp` is a hypothesis step - a term is asserted from the local context.
--| Indexing is relative to the list of hypotheses to the theorem.
--|
--| `VHyp : HypID -> VExpr`
@_ def VHyp (n: nat): nat = $ b0 (b0 n) $;
--| A `VThm` is a theorem application - a step follows from previous steps by
--| application of a theorem. The arguments give the theorem to apply, the list
--| of substitutions of expressions for the variables, and the list of subproofs
--| for the hypotheses to the theorem.
--|
--| `VThm : ThmID -> list SExpr -> list VExpr -> VExpr`
@_ def VThm (a h r es ps: nat): nat = $ b0 (b1 (a, h, r, es, ps)) $;
--| `c : A = B, p : A |- VConv c p : B`
--|
--| `VConv : CExpr -> VExpr -> VExpr`
@_ def VConv (c p: nat): nat = $ b1 (c, p) $;
theorem VThm_ltid: $ p IN ps -> p < VThm a h r es ps $ =
'(rsyl lmemlt @ mpi (ltb0tr @ ltb1tr @ lepr2tr @ lepr2tr @ lepr2tr leprid2) lttr);
theorem VConv_ltid: $ p < VConv c p $ =
'(ltb1tr leprid2);
@_ local def VExprRec (H T C: set): set =
$ \ e, srec (\ ih, case
(case H
(\\ a, \\ h, \\ r, \\ es, \ ps, T @ (a, h, r, es, ps, map ih ps)))
(\\ c, \ p, C @ (c, p, ih @ p)) @ size (Dom ih)) e $;
theorem VExprRec_val: $ VExprRec H T C @ e = case
(case H
(\\ a, \\ h, \\ r, \\ es, \ ps, T @ (a, h, r, es, ps, map (VExprRec H T C |` upto e) ps)))
(\\ c, \ p, C @ (c, p, (VExprRec H T C |` upto e) @ p)) @ e $ =
(named '(eqtr (applame sreceq2) @
! eqtr _ $ _ @ lower (VExprRec H T C |` _) $ _ srecval @
applame @ appeqd
(rsyl (bi2 @ eqlower2 @ finlam finns) @
caseeqd (caseeq2d @ slameqd @ slameqd @ slameqd @ slameqd @ lameqd @
appeq2d @ preq2d @ preq2d @ preq2d @ preq2d @ preq2d mapeq1) @
slameqd @ lameqd @ appeq2d @ preq2d @ preq2d appeq1)
sreclem));
theorem VExprRec_VHyp: $ VExprRec H T C @ VHyp n = H @ n $ =
(named '(eqtr VExprRec_val @ eqtr casel casel));
theorem VExprRec_VThm:
$ VExprRec H T C @ VThm a h r es ps = T @ (a, h, r, es, ps, map (VExprRec H T C) ps) $ =
(named '(eqtr VExprRec_val @ eqtr casel @ eqtr caser @
appslame @ appslamed @ appslamed @ appslamed @ applamed @
appeq2d @ preqd an4l @ preqd an3lr @ preqd anllr @ preqd anlr @ preqd anr @ syl6eq
(mapeqg @ mpbir allal @ !! ax_gen p @ syl resapp @ sylibr elupto VThm_ltid)
(mapeq2d anr)));
theorem VExprRec_VConv:
$ VExprRec H T C @ VConv c p = C @ (c, p, VExprRec H T C @ p) $ =
(named '(eqtr VExprRec_val @ eqtr caser @
appslame @ applamed @ appeq2d @
preqd anl @ preqd anr @ syl6eq
(resapp @ mpbir elupto VConv_ltid)
(appeq2d anr)));
theorem VExpr_indd {x n a h r es ps c p} (q)
(px: wff x) (pi: wff p) (ph: wff n) (pt: wff a h r es ps) (pc: wff c p)
(hn: $ x = q -> (px <-> pq) $)
(hr: $ x = VHyp n -> (px <-> ph) $)
(hs: $ x = VThm a h r es ps -> (px <-> pt) $)
(hc: $ x = VConv c p -> (px <-> pc) $)
(hi: $ x = p -> (px <-> pi) $)
(h1: $ G -> ph $)
(h2: $ G /\ all {x | px} ps -> pt $)
(h3: $ G /\ pi -> pc $): $ G -> pq $ =
'(sylib (sbe hn) @ !! indstr z w sbeq1 sbeq1 ,(split-sop '{
{($VHyp n$ => (indd_aux hr @ anwll h1)) +
($VThm a h r es ps$ => (indd_aux hs @ sylan h2 anll @
sylibr (alleq1 cbvabs) @ sylibr allal @
sylc (alimd @ imim1d @ syl5 VThm_ltid @ bi2d lteq2) anr anlr))} +
($VConv c p$ => (indd_aux hc @ mpd (indd_aux2 hi VConv_ltid) (anwll @ exp h3)))}));
@_ local def VerifyConv_aux (env ctx: nat): set =
$ CExprRec
(\ e, lower (S\ e1, S\ e2, {s | Expr env ctx e s /\ e = e1 /\ e = e2}))
(\\ c, \ ih, lower (S\ e1, S\ e2, {s | e2, e1, s e. ih}))
(\\ c1, \\ c2, \\ ih1, \ ih2, lower (S\ e1, S\ e2, {s |
E. e (e1, e, s e. ih1 /\ e, e2, s e. ih2)}))
(\\ f, \\ cs, \ ih, lower (S\ e1, S\ e2, {s |
E. args E. ret E. o E. es1 E. es2 (
e1 = SApp f es1 /\ e2 = SApp f es2 /\
getTerm env f args ret o /\
E. n (len cs = n /\ len es1 = n /\ len es2 = n /\ len args = n /\
A. i A. c A. e1 A. e2 A. bi (nth i ih = suc c ->
nth i es1 = suc e1 -> nth i es2 = suc e2 -> nth i args = suc bi ->
ExprBi env ctx e1 bi /\ ExprBi env ctx e2 bi /\
e1, e2, binderSort bi e. c)) /\
s = fst ret)}))
(\\ f, \\ es, \ zs, lower (S\ e1, S\ e2, {s |
E. args E. ret E. ys E. val (
getTerm env f args ret (suc (ys, val)) /\
e1 = SApp f es /\ s = fst ret /\
es, args e. all2 (S\ e, {a | ExprBi env ctx e a}) /\
ys, zs e. all2 (S\ y, {z | nth z ctx = suc (PBound y) /\
A. e (e IN es -> ~HasVar ctx e z)}) /\
e2 = substExpr (es ++ map (\ i, SVar i) zs) val)})) $;
--| Checking a conversion proof `c : (e1 : s) = (e2 : s)`.
--| `VerifyConv : Env -> Ctx -> CExpr -> SExpr -> SExpr -> SortID -> Bool`
@_ abstract def VerifyConv (env ctx c e1 e2 s: nat): wff =
$ e1, e2, s e. VerifyConv_aux env ctx @ c $;
--| `VerifyConvs : Env -> Ctx -> list CExpr -> list SExpr ->
--| list SExpr -> list Binder -> Bool`
@_ def VerifyConvs (env ctx cs es1 es2 bis .n .i .c .e1 .e2 .bi: nat): wff =
$ E. n (len cs = n /\ len es1 = n /\ len es2 = n /\ len bis = n /\
A. i A. c A. e1 A. e2 A. bi (nth i cs = suc c ->
nth i es1 = suc e1 -> nth i es2 = suc e2 -> nth i bis = suc bi ->
ExprBi env ctx e1 bi /\ ExprBi env ctx e2 bi /\
VerifyConv env ctx c e1 e2 (binderSort bi))) $;
theorem VerifyConvs_fin: $ E. n A. es1 A. es2 A. bis
(VerifyConvs env ctx cs es1 es2 bis -> es1 < n /\ es2 < n) $ =
(focus
(have 'h1
$ len cs = m /\ len es1 = m /\ len es2 = m /\ len bis = m -> i < m ->
E. c nth i cs = suc c /\ E. e1 nth i es1 = suc e1 /\
E. e2 nth i es2 = suc e2 /\ E. bi nth i bis = suc bi $
(def h '(syl6ib (bitr3 nthne0 exsuc) (com12 @ bi2d lteq2)))
'(com12 @ animd (animd (animd ,h ,h) ,h) ,h))
'(! iexe _ $ lower (Array (upto (map (VerifyConv_aux env ctx) cs)) (len cs)) $ _ _
(aleqd @ aleqd @ aleqd @ imeq2d @ aneqd lteq2 lteq2) @
ax_gen @ ax_gen @ ax_gen @ eex @ iand _ _)
(def (lem hlen hlen2 hpr f) @ focus
'(syl ellt @ sylibr (ellower @ Arrayfin finns) @
sylibr elArray @ iand _ @ eqtr4d ,hlen an4l)
'(sylibr elList @ sylibr allnth @ imp @ alimd @
syl6ib (bitr3 alim1 @ aleqi @ bitr3 impexp @ imeq1i @ bian1a @ sylib nthne0 sucne0) @
com23 @ syl6 _ @ mpbird (imeq1d @ lteq2d ,hlen2) h1)
'(com12 @ impd @ impd @ impd @ sylibr eexb @ alimi @
sylbi (bitr (!! aleqi e1 @ bitr (!! aleqi e2 alim1) alim1) alim1) @
a2i @ syl5bi (aleqi @ bitr (aleqi alim1) @ bitr alim1 @ imeq2i @ aleqi alim1) _)
(f '(alimd @ imim2d @ imp @ com23 @ syl6ibr eexb @ alimd @ imim2d @
exp @ sylibr elupto @ syl (lelttr ,hpr) @
lttrd (syl ellt anrr) @
syl lmemlt @ syl lmemmapi @ anwl nthlmem)))
(lem 'an3lr 'anllr 'leprid1 (fn (x)
'(syl6 ax_1 @ com23 @ exp @ com23 @ exp @ alimd @ imim2d @
anrasss @ imp @ com23 @ syl6ibr eexb ,x)))
(lem 'anllr 'anlr '(lepr2tr leprid1) (fn (x)
'(syl6ibr eexb @ alimd @ imim2d @ syl6 ax_1 @ com23 @ exp ,x))));
theorem VerifyConv_aux_Refl:
$ VerifyConv_aux env ctx @ CRefl e = lower (S\ e1, S\ e2, {s |
Expr env ctx e s /\ e = e1 /\ e = e2}) $ =
(named '(eqtr CExprRec_CRefl @ applame @ lowereqd @ sabeqd @ sabeqd @ abeqd @
aneqd (aneqd Expreq3 eqeq1) eqeq1));
pub theorem VerifyConvRefl (env ctx e e1 e2 s: nat):
$ VerifyConv env ctx (CRefl e) e1 e2 s <->
Expr env ctx e s /\ e = e1 /\ e = e2 $ =
(named @ focus
'(bitr (elneq2 VerifyConv_aux_Refl) @ bitr (ellower @ trud _) @
elsabe @ elsabed @ elabed @
aneqd (aneqd (Expreq4d anr) (eqeq2d anll)) (eqeq2d anlr))
'(sabfin (eexsabd @ eelabd @ a1i @ sylibr elsn @ eqcomd anlr) (a1i finns) @
sabfin (eelabd @ a1i @ sylibr elsn @ eqcomd anr) (a1i finns) @
a1i @ finss (mpbi ssab1 @ ax_gen anll) finns));
theorem VerifyConv_aux_Symm:
$ VerifyConv_aux env ctx @ CSymm c = lower (S\ e1, S\ e2, {s |
VerifyConv env ctx c e2 e1 s}) $ =
(named '(eqtr {CExprRec_CSymm : $_ = _ @ (_, VerifyConv_aux _ _ @ c)$} @
appslame @ applamed @ lowereqd ,eqtac));
pub theorem VerifyConvSymm (env ctx c e1 e2 s: nat):
$ VerifyConv env ctx (CSymm c) e1 e2 s <-> VerifyConv env ctx c e2 e1 s $ =
(named @ focus
'(bitr (elneq2 VerifyConv_aux_Symm) @ bitr (ellower @ trud _) @
elsabe @ elsabed @ elabed @ VerifyConveqd eqidd eqidd eqidd anlr anll anr)
'(sabfin (eexsabd @ eelabd @ a1i @ syl preldm prelrn) (a1i @ dmfin @ rnfin finns) @
sabfin (eelabd @ a1i preldm) (a1i @ dmfin finns) @
a1i @ finss (mpbi ssab1 @ ax_gen @ syl prelrn prelrn) (rnfin @ rnfin finns)));
theorem VerifyConv_aux_Trans:
$ VerifyConv_aux env ctx @ CTrans c1 c2 = lower (S\ e1, S\ e2, {s |
E. e (VerifyConv env ctx c1 e1 e s /\ VerifyConv env ctx c2 e e2 s)}) $ =
(named '(eqtr {CExprRec_CTrans : $ _ = _ @
(_, _, VerifyConv_aux _ _ @ c1, VerifyConv_aux _ _ @ c2) $} @
appslame @ appslamed @ appslamed @ applamed ,eqtac));
pub theorem VerifyConvTrans (env ctx c1 c2 e1 e2 s: nat) {e: nat}:
$ VerifyConv env ctx (CTrans c1 c2) e1 e2 s <->
E. e (VerifyConv env ctx c1 e1 e s /\ VerifyConv env ctx c2 e e2 s) $ =
(named @ focus
'(bitr (elneq2 VerifyConv_aux_Trans) @ bitr (ellower @ trud _) @
elsabe @ elsabed @ elabed ,eqtac)
'(sabfin (eexsabd @ eelabd @ a1i @ eex @ anwl preldm) (a1i @ dmfin finns) @
sabfin (eelabd @ a1i @ eex @ anwr @ syl preldm prelrn) (a1i @ dmfin @ rnfin finns) @
a1i @ finss (mpbi ssab1 @ ax_gen @ eex @ anwr @ syl prelrn prelrn) (rnfin @ rnfin finns)));
theorem VerifyConv_aux_Cong:
$ VerifyConv_aux env ctx @ CCong f cs = lower (S\ e1, S\ e2, {s |
E. args E. ret E. o E. es1 E. es2 (
e1 = SApp f es1 /\ e2 = SApp f es2 /\
getTerm env f args ret o /\
VerifyConvs env ctx cs es1 es2 args /\
s = fst ret)}) $ =
(named @ focus
'(eqtr {CExprRec_CCong : $_ = _ @ (_, _, map (VerifyConv_aux _ _) _)$} @
appslame @ appslamed @ applamed @ lowereqd @ sabeqd @ sabeqd @ abeqd @
exeqd @ exeqd @ exeqd @ exeqd @ exeqd @ aneq1d @
aneqd (aneqd
(aneqd (eqeq2d @ SAppeq1d anll) (eqeq2d @ SAppeq1d anll))
(getTermeq2d anll)) _)
'(exeqd @ aneqd (aneq1d @ aneq1d @ aneq1d @ eqeq1d @ leneqd anlr) @
!! aleqd n @
bitr4g alcomb alcomb @ !! aleqd e1 @
bitr4g alcomb alcomb @ !! aleqd e2 @
bitr4g alcomb alcomb @ !! aleqd bi @
syl6bb _ @ aleqd @ imeq1d @ eqeq1d @ ntheq2d anr)
'(bitr (aleqi @ bitr (imeq1i @ mapnthb) eexb) @
bitr alcomb @ aleqi @
bitr (!! aleqi _ @ bitr (imeq1i ancomb) impexp) @ aleqe @
imeq2d @ imeq2d @ imeq2d @ imeq2d @ aneq2d elneq2));
pub theorem VerifyConvCong (env ctx f cs e1 e2 s: nat) {args ret o es1 es2: nat}:
$ VerifyConv env ctx (CCong f cs) e1 e2 s <->
E. args E. ret E. o E. es1 E. es2 (
e1 = SApp f es1 /\ e2 = SApp f es2 /\
getTerm env f args ret o /\
VerifyConvs env ctx cs es1 es2 args /\
s = fst ret) $ =
(named @ focus
'(bitr (elneq2 VerifyConv_aux_Cong) @ bitr (ellower _) @
elsabe @ elsabed @ elabed @ exeqd @ exeqd @ exeqd @ exeqd @ exeqd @
aneqd (aneq1d @ aneq1d @ aneqd (eqeq1d anll) (eqeq1d anlr)) (eqeq1d anr))
(suffices 'h $ _ -> _ -> e1_ < SApp env n /\ e2_ < SApp env n /\ s_ < env $ '_)
(focus
'(ax_mp (eex _) VerifyConvs_fin)
'(sabfin (eexsabd @ eelabd @ syl6ibr elupto @ syl6 anll h) (a1i finns) @ anwl @
sabfin (eelabd @ syl6ibr elupto @ syl6 anlr h) (a1i finns) @ anwl @
syl (mpi ltfin finss) @ ssabd @ syl6 anr h))
'(eexd @ eexd @ eexd @ sylibr eexb @ alimi @ sylibr eexb @ alimi @