-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntrospectiveCalculus.v
5343 lines (4441 loc) · 163 KB
/
IntrospectiveCalculus.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
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
Set Implicit Arguments.
Set Asymmetric Patterns.
Require Import Unicode.Utf8.
Require Import List.
Require Import Coq.Program.Equality.
Require Import Ascii String.
Open Scope string_scope.
(* Hack - remove later *)
Parameter cheat : ∀ {A}, A.
(****************************************************
Typeclass practicalities
****************************************************)
(* We often want to view IC formulas as "extensible". That is, we know that the formula type has certain constructors, but don't guarantee that those are the ONLY constructors.
Theoretically, this is "simpler" than Coq's `Inductive` types (constructors come _before_ inductive instances), but in Coq, it's not elementary. So to represent a "set of constructors", we use a typeclass, where the typeclass methods are the constructors. Thus, anytime we have a type F with an instance of FormulaConstructors, we can view F as a legitimate "extension" of the concept of formulas.
Here we must do some bureaucracy about how we'll use typeclasses. *)
(* Sometimes we need to pass a typeclass as a function argument. This is fine because a typeclass is an ordinary value, but Coq's features for using typeclasses _implicitly_ don't have a built-in way to treat a local variable as a typeclass. To work around this, we make a wrapper class "Class", which is technically the identity function but is always treated as a typeclass. So when we want a local variable `C` to be treated as a typeclass, we say `Class C` instead. This is the same thing, but counts as a typeclass for implicits. *)
Definition Class [P] (p:P) := p.
Existing Class Class.
Hint Unfold Class : typeclass_instances.
Definition use_class {T P t} (c:@Class (T -> Type) P t) : P t := c.
(* Definition use_class {P T} (c:Class P T) : P T := c. *)
(* We also often want to extend a typeclass with additional constructors, while still leaving it open to still-further constructors. This gives us a _second typeclass_ that is a subclass of the first. It's useful to represent the subclass relation explicitly: *)
Definition SubclassOf {T} (Superclass Subclass : T -> Type) := ∀ T (_:Class Subclass T), Class Superclass T.
Existing Class SubclassOf.
Notation "R ⊆ S" := (SubclassOf S R) (at level 70).
Instance subclass_refl {T} {A : T -> Type}
: (A ⊆ A)
:= λ x xA, xA.
(* Once we make an instance for subclass transitivity (A ⊆ B -> B ⊆ C -> A ⊆ C), we risk making instance-search diverge: The subgoal looking for (A ⊆ B) tries looking for some additional transitivity-instance (A ⊆ X) and (X ⊆ B), ad infinitum. So we have to set a limit on typeclass search: *)
Set Typeclasses Iterative Deepening.
Set Typeclasses Depth 3.
Instance subclass_trans {T} {A B C : T -> Type}
: (A ⊆ B) ->
(B ⊆ C) ->
(A ⊆ C)
:= λ ab bc, λ x xA, bc _ (ab _ xA).
Definition test_subclass_trans {T} {A B C : T -> Type}
: (A ⊆ B) -> (B ⊆ C) -> (A ⊆ C) := _.
(* Set Typeclasses Debug Verbosity 2. *)
Definition test_subclass_trans_2 {T} {A B C D : T -> Type}
: (A ⊆ B) -> (B ⊆ C) -> (C ⊆ D) -> (A ⊆ D) := λ _ _ _, _.
(* Lemma subclass_trans_refl1 [T] [A B : T -> Type] (s : A ⊆ B) :
subclass_trans subclass_refl s = s.
reflexivity.
Qed.
Lemma subclass_trans_refl2 [T] [A B : T -> Type] (s : A ⊆ B) :
subclass_trans s subclass_refl = s.
reflexivity.
Qed. *)
Instance subclass_apply {T} {A B : T -> Type} {t}
: (A ⊆ B) -> Class A t -> Class B t
:= λ ab At, ab _ At.
(* The definitions above treat a class as an arbitrary constraint, but *constructor* classes also have a positivity requirement, which we sometimes need to require explicitly: *)
(* Class ConstructorClass (C : Type -> Type) := {
cc_embed : ∀ {a b} {_:C a}, (a -> b) -> C b
}. *)
(****************************************************
Relations between internal and external meaning
****************************************************)
Class ApplyConstructor F := {
apply : F -> F -> F
}.
Class FunctionConstructors F := {
const : F
; fuse : F
; fc_ac :: ApplyConstructor F
}.
Class PropositionConstructors F := {
pc_fc :: FunctionConstructors F
; f_implies : F
; f_and : F
; f_forall_quoted_formulas : F
}.
(* When we want to use PropositionConstructors methods, but only have our wrapper Class PropositionConstructors, we need to be able to unwrap it: *)
Instance pc_class_unwrap :
∀ {F}, Class PropositionConstructors F ->
PropositionConstructors F := λ _ b, b.
Instance aplc_class_unwrap :
∀ {F}, Class ApplyConstructor F ->
ApplyConstructor F := λ _ b, b.
Instance func_aplc_subclass : FunctionConstructors ⊆ ApplyConstructor := λ _, _.
(* And because we use a low depth limit, we need to provide some "shortcuts": *)
Instance shortcut_cpc_fnc F : Class PropositionConstructors F -> FunctionConstructors F := _.
Instance shortcut_cpc_aplc F : Class PropositionConstructors F -> ApplyConstructor F := λ _, _.
Notation "[ x y .. z ]" := (apply .. (apply x y) .. z)
(at level 0, x at next level, y at next level).
Notation "[ x -> y ]" := [f_implies x y] (at level 0, x at next level, y at next level).
Notation "[ x & y ]" := [f_and x y]
(at level 0, x at next level, y at next level).
Notation "[ ⋀ x ]" := [f_forall_quoted_formulas x]
(at level 0, x at next level).
Inductive UnfoldStep {F} {_f:FunctionConstructors F} : F -> F -> Prop :=
| unfold_const a b : UnfoldStep [const a b] a
| unfold_fuse a b c : UnfoldStep [fuse a b c] [[a c] [b c]]
| unfold_in_lhs a b c : UnfoldStep a b -> UnfoldStep [a c] [b c].
(* Notation "R ∧1 S" := (λ x, R x ∧ S x) (at level 70). *)
(* Notation "R ×1 S" := (λ x, prod (R x) (S x)) (at level 70). *)
(* Notation "R ∧3 S" := (λ x y z, R x y z ∧ S x y z) (at level 70). *)
(* Notation "R ∧4 S" := (λ x y z w, R x y z w ∧ S x y z w) (at level 70). *)
(* Notation "R ⊆ S" := (∀ x, R x -> S x) (at level 70). *)
(* Definition Construction Constructors := ∀ {T} `{Constructors T}, T. *)
Class OneMoreConstructor Constrs F := {
omc_embed : Class Constrs F
; omc_new : F
}.
Instance onemore_class_unwrap :
∀ {Constrs F}, Class (OneMoreConstructor Constrs) F ->
OneMoreConstructor Constrs F := λ _ _ c, c.
Instance OneMoreConstructor_subclass {C}
: OneMoreConstructor C ⊆ C
:= λ _ _, omc_embed.
Instance shortcut_omc_trans A B
: (A ⊆ B) -> (OneMoreConstructor A ⊆ B)
:= _.
Instance shortcut_omc_trans_apply A B T
: (A ⊆ B) -> (Class (OneMoreConstructor A) T) -> Class B T
:= λ _ _, _.
Instance shortcut_omc_func_aplc A T
: (A ⊆ FunctionConstructors) -> (Class (OneMoreConstructor A) T) -> Class ApplyConstructor T
:= λ _ _, _.
(* Propositions are formulas that say things about other formulas, but there's no intrinsic reason those formulas have to use the _same grammar_. So we will often be passing around _two_ formula-constructor-classes, which I call the "viewing type" (conventionally named V, with constructors named VC) and the "target type" (conventionally named T, with constructors named TC).
The gotcha: We don't want one of them to be accidentally passed where the other is expected (especially implicitly!)... but they are the same type!
So, we make the types different _in name_, which means Coq will implicitly pass them only in the correct role. *)
Definition VConsClass := Type -> Type.
Definition TConsClass := Type -> Type.
Implicit Type VC : VConsClass.
Implicit Type TC : TConsClass.
Existing Class VConsClass.
Existing Class TConsClass.
(* ...but when a Type -> Type is needed in a context that can disambiguate which one, we do want Coq to know that a VConsClass or TConsClass will suffice: *)
Hint Unfold VConsClass : typeclass_instances.
Hint Unfold TConsClass : typeclass_instances.
(* Set Typeclasses Debug Verbosity 2. *)
Definition test_subclass_apply_with_VConsClass
{VC VC2:VConsClass} {V} (v:Class VC2 V) `{VC2 ⊆ VC}
: Class VC V := _.
Definition test_subclass_apply_with_VConsClass_2
{C : Type -> Type} {VC2 : VConsClass} {V:Type} (v:@Class VConsClass VC2 V) `{VC2 ⊆ C}
: @Class (Type -> Type) C V := _.
Definition test_OneMoreConstructor_apply_and_unwrap_with_VConsClass
{VC oVC2:VConsClass} `{oVC2 ⊆ OneMoreConstructor VC}
{V} (_v:Class oVC2 V)
: OneMoreConstructor VC V := _.
(* Given any constructor-class, we may also be interested in the inductive type with those constructors. We will be using this a lot, so it gets a short name.
Unfortunately, we can't express the statement `C (Ind C)`, because it results in universe inconsistency. This would work fine in the Prop universe; I think I could theoretically rewrite most of the code here to use Prop instead of Type, and have it work. But Coq generally expects you to be working in the predicative hierarchy instead, and can't abstract over Prop vs Type in the way we'd need. But, I think it turns out to be unnecessary to express `C (Ind C)` anyway (we can just convert to other forms). *)
Definition Ind C := ∀ T (_:Class C T), T.
Definition ind_on_stricter_constructors C C2
: (C2 ⊆ C) -> Ind C -> Ind C2
:= λ _c x T eT, x T _.
(* We also deal with "quoted formulas". To say that a V-formula is a quoted version of a T-formula, we need to define the MeansQuoted relation (MQ for short). This relation is only well-defined once you first specify the V and T constructors: *)
Definition MQT V TC := V -> Ind TC -> Prop.
(* Definition mqt_on_looser_constructors [V TC TC2] [eT : (TC ⊆ TC2)]
: MQT V TC -> MQT V TC2
:= λ MQ v t, MQ v (ind_on_stricter_constructors eT t). *)
(* ...and usually, we need to be talking about _constructors_ (MQC) for that relation, because it must be just as extensible as the formula types: *)
Definition MQCT VC TC :=
∀ V (_v:Class VC V) (TC2 : TConsClass) (eT : TC2 ⊆ TC), MQT V TC2 -> Prop.
Existing Class MQCT.
(* We also define how an MQCT relates _inductive instances_ of VC and TC - which is the same thing as saying an inductive instance of those MQ constructors: *)
(* We also need to be able to say an inductive instance of those MQ constructors ("this concrete qx is forced by the constructors to mean a quoted version of this concrete x, no matter what embedding we're in..."); "any MQ that obeys MQC (bearing in mind that MQC can judge any MQ on any extension of TC) must hold (MQ qx x)" *)
Definition MQInd {VC TC V} {_v:Class VC V} (qx : V) (x : Ind TC) {MQC : MQCT VC TC}
:=
∀ TC2 eT MQ (_mq : MQC V _v TC2 eT MQ), MQ qx (ind_on_stricter_constructors eT x).
Print MQInd.
(* ...and to say that a particular Ind VC always construct something that quotes an Ind TC: *)
Definition VInd_MQInd {VC TC V} {_v:Class VC V} (qx : Ind VC) (x : Ind TC) {MQC : MQCT VC TC}
:= ∀ V _v, MQInd (qx V _v) x.
(* Definition MQInd {VC TC} (qx : Ind VC) (x : Ind TC) {MQC : MQCT}
:=
∀ V (_v:Class VC V) T (_t:Class TC T) (MQ : MQT),
MQC _ _ _ _ MQ ->
MQ _ _ _ _ (qx _ _) (x _ _). *)
(* ...the main case of which is to add one additional variable to each of V and T, and say that the new V-variable is a quoted version of the new T-variable. *)
Definition OneMoreMQConstructor [VC TC] (qx : Ind VC) (x: Ind TC) (MQC : MQCT VC TC) : MQCT VC TC
:= λ V _v TC2 eT MQ,
(MQC _ _ _ _ MQ) ∧
(∀ T (_t:Class (OneMoreConstructor TC) T), (MQ (qx _ _) (λ _ _, x _ _))).
Definition MQCOnStricterFormulaTypes
{VC TC VC2 TC2} {eV: VC2 ⊆ VC} {eT: TC2 ⊆ TC}
(MQC : MQCT VC TC) : MQCT VC2 TC2
:= λ _ _ _ _ MQ, (MQC _ _ _ _ MQ).
Print MQCOnStricterFormulaTypes.
Definition MQCSubclassOf [VC TC] (MQC1 MQC2 : MQCT VC TC)
:= ∀ V _v TC2 eT MQ, MQC1 V _v TC2 eT MQ -> MQC2 V _v TC2 eT MQ.
Lemma MQCSubclassOf_refl [VC TC] (MQC : MQCT VC TC) : MQCSubclassOf MQC MQC.
unfold MQCSubclassOf. trivial.
Defined.
Lemma OneMoreMQConstructor_embed
{VC TC} (qx : Ind VC) (x: Ind TC) (MQC : MQCT VC TC)
: MQCSubclassOf (OneMoreMQConstructor qx x MQC) MQC.
unfold MQCSubclassOf, OneMoreMQConstructor.
intros.
destruct H; assumption.
Qed.
Lemma MQC_embed_under_stricter
{VC TC VC2 TC2} {eV: VC2 ⊆ VC} {eT: TC2 ⊆ TC}
(MQC1 MQC2 : MQCT VC TC)
: MQCSubclassOf MQC1 MQC2 ->
MQCSubclassOf (MQCOnStricterFormulaTypes MQC1) (MQCOnStricterFormulaTypes MQC2).
unfold MQCSubclassOf, MQCOnStricterFormulaTypes.
intros.
exact (H _ _ _ _ _ H0).
Qed.
(* Lemma MQC_unembed_under_stricter
{VC TC VC2 TC2} {eV: VC2 ⊆ VC} {eT: TC2 ⊆ TC}
V (_v : Class VC2 V) (MQ : MQT V TC2)
(MQC1 MQC2 : MQCT VC TC)
:
MQCSubclassOf (MQCOnStricterFormulaTypes MQC1) (MQCOnStricterFormulaTypes MQC2) ->
MQCSubclassOf MQC1 MQC2.
unfold MQCSubclassOf, MQCOnStricterFormulaTypes.
intros.
unfold subclass_apply, mqt_on_looser_constructors, ind_on_stricter_constructors, subclass_apply in H.
specialize (H V _ MQ).
exact (H _ _ _ H0).
Qed. *)
(* Lemma MQC_stricter_trans
{VC TC VC2 TC2 VC3 TC3}
{eV12: VC2 ⊆ VC} {eT12: TC2 ⊆ TC}
{eV23: VC3 ⊆ VC2} {eT23: TC3 ⊆ TC2}
(MQC : MQCT VC TC)
: MQCSubclassOf
(@MQCOnStricterFormulaTypes VC2 TC2 VC3 TC3 _ _ (MQCOnStricterFormulaTypes MQC))
(@MQCOnStricterFormulaTypes VC TC VC3 TC3 _ _ MQC).
unfold MQCSubclassOf, MQCOnStricterFormulaTypes.
intros.
assumption.
Qed. *)
(* Lemma MQC_stricter_unwrap
{VC TC} (MQC : MQCT VC TC)
V _v TC2 eT MQ
: @MQCOnStricterFormulaTypes VC TC VC TC _ _ MQC V _v TC2 eT MQ
-> MQC V _v TC2 eT MQ.
unfold MQCOnStricterFormulaTypes.
intros.
assumption.
Qed. *)
(* Lemma MQInd_erjekr {VC TC V} {_v:Class VC V} (qx : V) (x : Ind TC) {MQC : MQCT VC TC} TC2 (eT : TC2 ⊆ TC) :
MQInd qx x -> @MQInd VC TC2 _ _ qx (ind_on_stricter_constructors eT x) (MQCOnStricterFormulaTypes _).
intro _mqi. unfold MQInd in *. intros.
apply _mqi.
unfold MQCOnStricterFormulaTypes in H.
exact H. *)
Definition OneMoreQuotvar {VC TC} (MQC : MQCT VC TC)
: MQCT (OneMoreConstructor VC) (OneMoreConstructor TC)
:=
OneMoreMQConstructor
(@omc_new _) (@omc_new _)
(MQCOnStricterFormulaTypes MQC).
(* λ oVC2 oTC2 _v _t MQ,
(MQC _ _ _ _ MQ) ∧
(∀ V (_v:Class oVC2 V) T (_t:Class oTC2 T),
MQ _ _ _ _ omc_new omc_new). *)
Print OneMoreQuotvar.
Eval compute in OneMoreQuotvar.
(* Propositions represent rules of inference. A Rule is a constraint on what inferences may be valid: for example, the rule (A & B) |- (B & A) says that for all values of B and A, the inference (A & B) |- (B & A) must be valid.
In practice, we don't use the full generality of arbitrary constraints. Our only recursive rule is transitivity ((A |- B) and (B |- C) imply (A |- C)), and there isn't a proposition that represents it, it's just always true. All propositions represent simple positive constraints, which just say that certain inferences must be valid.
Nevertheless, the simplest way to define Rule is as a predicate on InfSets, which are predicates on inferences. (An InfSet takes two formulas P,C and says whether it holds inference P |- C as valid.)
We must ask what the actual type of Rule is. A Rule must be agnostic to grammar-extensions, but you may express a rule that assumes particular constructors exist (otherwise, our example rule wouldn't be able to express &). Therefore: *)
Definition InfSet T := T -> T -> Prop.
Existing Class InfSet.
(* Definition _proves {T} {infs:InfSet T} p c := infs p c.
Notation "p ⊢ c" := (_proves p c) (at level 70). *)
Definition Rule {TC} := ∀ T (_:Class TC T), InfSet T -> Prop.
(* We can now make the big definition of what propositions mean. *)
(* definition specific to T, although we will often be dealing in values of type (∀ T (_t:Class TC T), Ruleset) *)
Inductive Ruleset TC :=
| ruleset_implies : Ind TC -> Ind TC -> Ruleset TC
| ruleset_and : Ruleset TC -> Ruleset TC -> Ruleset TC
| ruleset_forall : Ruleset (OneMoreConstructor TC) -> Ruleset TC
.
(* Definition specialize_ind {TC} (f : Ind (OneMoreConstructor TC)) (x : Ind TC) : Ind TC :=
λ T _t, f T {| omc_new := (x _ _) |}. *)
(* wrong variable order:
Fixpoint specialize_ruleset {TC} (R : @Ruleset (OneMoreConstructor TC)) (x : Ind TC) : @Ruleset TC :=
match R return Ruleset with
| ruleset_implies p c => ruleset_implies (specialize_ind p x) (specialize_ind c x)
| ruleset_and A B => ruleset_and (specialize_ruleset A x) (specialize_ruleset B x)
| ruleset_forall F => ruleset_forall (specialize_ruleset F (ind_on_stricter_constructors _ x))
end. *)
Fixpoint Ruleset_to_Coq [TC] (R : Ruleset TC) {T} {_t: Class TC T} (infs : InfSet T) : Prop :=
match R with
| ruleset_implies p c => infs (p _ _) (c _ _)
| ruleset_and A B => Ruleset_to_Coq A _ ∧ Ruleset_to_Coq B _
| ruleset_forall F => ∀ x, @Ruleset_to_Coq _ F _ {| omc_new := x |} _
end.
Fixpoint Ruleset_on_stricter_constructors [TC TC2] [eT : TC2 ⊆ TC] (R : Ruleset TC) : Ruleset TC2 :=
match R with
| ruleset_implies p c => ruleset_implies (ind_on_stricter_constructors _ p) (ind_on_stricter_constructors _ c)
| ruleset_and A B => ruleset_and (Ruleset_on_stricter_constructors A) (Ruleset_on_stricter_constructors B)
| ruleset_forall F => ruleset_forall (@Ruleset_on_stricter_constructors _ (OneMoreConstructor TC2) (λ _ _, {| omc_new := omc_new |}) F)
end.
(* Set Printing Implicit. *)
(* Set Typeclasses Debug Verbosity 2. *)
(* Set Typeclasses Depth 4. *)
Inductive FormulaMeansProp {VC TC} {MQC : MQCT VC TC}
{_vp:VC ⊆ PropositionConstructors}
{V} {_v: Class VC V}
(* {T} {_t: Class TC T}
{MQ : ∀ {V} {_v:Class VC V} {T} {_t:Class TC T}, V -> T -> Prop}
{_mq : MQC _ _ _ _ (@MQ)} *)
: V -> Ruleset TC -> Prop :=
| mi_implies [qp qc p c]
:
MQInd qp p ->
MQInd qc c ->
FormulaMeansProp [qp -> qc] (ruleset_implies p c)
| mi_unfold [a b B] :
UnfoldStep a b ->
FormulaMeansProp b B ->
FormulaMeansProp a B
| mi_and [a b A B] :
FormulaMeansProp a A ->
FormulaMeansProp b B ->
FormulaMeansProp [a & b] (ruleset_and A B)
| mi_forall_quoted_formulas
(f : Ind VC)
(F : Ruleset (OneMoreConstructor TC))
:
(∀ V _v,
@FormulaMeansProp _ _ (OneMoreQuotvar MQC) _ V _v
[(f _ _) omc_new]
F)
->
FormulaMeansProp
[⋀ (f _ _)]
(ruleset_forall (@F))
.
Definition VIndMeansProp {VC TC} {MQC : MQCT VC TC}
{_vp:VC ⊆ PropositionConstructors}
: Ind VC -> Ruleset TC -> Prop
:= λ p P, ∀ V _v, FormulaMeansProp (p V _v) P.
(****************************************************
Definitions of concrete formula types
****************************************************)
(* The above has been very abstract. We now proceed to the concrete formulas of IC. These belong to the Set universe, for convenience of programming.
All formulas of IC are apply-trees where the leaves are standard atoms... *)
Inductive Atom :=
| atom_const
| atom_fuse
| atom_implies
| atom_and
| atom_forall_quoted_formulas
| atom_quote
.
(* ...but our metatheorems may also extend the formula type with additional constructors, which represent metavariables. Each such variable is essentially an additional constructor, so our formula type is parameterized with the extension type. *)
Inductive Formula Ext :=
| f_atm : Atom -> Formula Ext
| f_ext : Ext -> Formula Ext
| fo_apl : Formula Ext -> Formula Ext -> Formula Ext.
Arguments f_atm [Ext] _.
Inductive OneMoreExt OldExt :=
| ome_embed : OldExt -> OneMoreExt OldExt
| ome_new : OneMoreExt OldExt.
Arguments ome_embed {OldExt} _.
Arguments ome_new {OldExt}.
(* We'll also need to "be able to talk to" the abstract definitions above, so we make an equivalent definition out of typeclasses. *)
Class FormulaConstructors F := {
fc_atm : Atom -> F
; fc_apl :: ApplyConstructor F
}.
(* Set Printing Implicit.
Set Typeclasses Debug Verbosity 2.
Definition test_subclass_apply_with_VConsClass_FormulaConstructors
{VC2 : (Type -> Type)} {TC2:(Type -> Type)} {V:Type} (v:@Class (Type -> Type) VC2 V) {_:VC2 ⊆ FormulaConstructors} {_:TC2 ⊆ FormulaConstructors}
: @Class (Type -> Type) FormulaConstructors V.
(* clear X0. *)
typeclasses eauto.
eauto with typeclass_instances. *)
(* Definition test_subclass_apply_with_VConsClass_FormulaConstructors
{VC2 : VConsClass} {TC2:TConsClass} {V:Type} (v:@Class VConsClass VC2 V) {_:VC2 ⊆ FormulaConstructors} {_:TC2 ⊆ FormulaConstructors}
: @Class (Type -> Type) FormulaConstructors V := _.
Definition test_subclass_apply_with_VConsClass_FormulaConstructors_2 :∀ (VC2:VConsClass) (TC2:TConsClass),
(VC2 ⊆ FormulaConstructors) -> (TC2 ⊆ FormulaConstructors) ->
Prop :=
λ VC2 TC2 eV eT,
∀ V (_v:Class VC2 V)
(* T (_t:Class TC2 T) *)
,
let _ : Class FormulaConstructors V := _ in
(* let _ : Class FormulaConstructors T := _ in
let MQ := MQ V _ T _ in *)
False. *)
Instance f_unwrap :
∀ {F}, Class FormulaConstructors F ->
FormulaConstructors F := λ _ b, b.
(* Instance fc_cc : ConstructorClass (Class FormulaConstructors) := {
cc_embed := λ a b FCa ab, {|
fc_atm := λ a, ab (fc_atm a)
; fc_apl := λ a b, ab (fc_apl a b)
|}
}. *)
Instance f_apli {Ext} : Class ApplyConstructor (Formula Ext) := {|
apply := @fo_apl Ext
|}.
Instance f_fc {Ext} : Class FormulaConstructors (Formula Ext) := {|
fc_atm := @f_atm Ext
|}.
Instance fc_func F : FormulaConstructors F ->
FunctionConstructors F := {
const := fc_atm atom_const
; fuse := fc_atm atom_fuse
}.
Instance fc_prop F : FormulaConstructors F ->
PropositionConstructors F := {
f_implies := (fc_atm atom_implies)
; f_and := (fc_atm atom_and)
; f_forall_quoted_formulas := (fc_atm atom_forall_quoted_formulas)
}.
Instance fc_subset_prop
: FormulaConstructors ⊆ PropositionConstructors
:= fc_prop.
(* Definition OneMoreConstructor_fc_trans_apply {C} {_vp : C ⊆ FormulaConstructors} {V} {_v : Class (OneMoreConstructor C) V}
: Class FormulaConstructors V := let _ : Class C V := _ in _.
Hint Immediate OneMoreConstructor_fc_trans_apply : typeclass_instances. *)
Instance shortcut_cfc_pc F : Class FormulaConstructors F -> PropositionConstructors F := _.
Instance shortcut_cfc_func F : Class FormulaConstructors F -> FunctionConstructors F := _.
Instance shortcut_cfc_aplc F : Class FormulaConstructors F -> ApplyConstructor F := _.
(* Definition ind_to_sf (i : Ind FormulaConstructors)
: StandardFormula := i _ _. *)
(* Definition ome_to_ind {Ext} {Constrs}
(embed : Ext -> Ind Constrs)
(e : @OneMoreExt Ext)
: Ind (OneMoreConstructor Constrs)
:= λ _ _, match e with
| ome_embed e => (embed e _ _)
| ome_new => omc_new
end. *)
Fixpoint NMoreConstructors n Constrs :=
match n with
| 0 => Constrs
| S pred => OneMoreConstructor (NMoreConstructors pred Constrs)
end.
(* Fixpoint NMoreConstructorsL n Constrs :=
match n with
| 0 => Constrs
| S pred => (NMoreConstructorsL pred (OneMoreConstructor Constrs))
end. *)
Fixpoint NMoreExt n Ext :=
match n with
| 0 => Ext
| S pred => @OneMoreExt (NMoreExt pred Ext)
end.
Fixpoint NMoreQuotvars n {VC} {TC} (MQC : MQCT VC TC) : MQCT (NMoreConstructors n VC) (NMoreConstructors n TC) :=
match n with
| 0 => MQC
| S p => OneMoreQuotvar (NMoreQuotvars p MQC)
end.
(* Fixpoint NMoreQuotvars_without_new_MQCs n {VC} {TC} (MQC : MQCT VC TC) : MQCT (NMoreConstructors n VC) (NMoreConstructors n TC) :=
match n with
| 0 => MQC
| S p => MQCOnStricterFormulaTypes (NMoreQuotvars_without_new_MQCs p MQC)
end. *)
Definition FormulaWithNVars n : Type := Formula (NMoreExt n False).
Definition FCWithNVars n : Type -> Type := NMoreConstructors n FormulaConstructors.
(* we could write (Formula False) instead, but this makes inference easier *)
Definition StandardFormula := FormulaWithNVars 0.
Definition StandardRuleset := Ruleset (FCWithNVars 0).
(* Fixpoint sf_to_ind (f : StandardFormula)
: Ind FormulaConstructors
:= λ _ _, match f with
| f_atm a => fc_atm a
| f_ext a => match a with end
| fo_apl a b => [(sf_to_ind a _) (sf_to_ind b _)]
end. *)
(* Instance fplus_fcplus {Ext} : Class (OneMoreConstructor FormulaConstructors) (Formula (@OneMoreExt Ext)) :=
{|
omc_embed := _
; omc_new := f_ext ome_new
|}.
Instance fplus2_fcplus2 {Ext} : Class (OneMoreConstructor (OneMoreConstructor FormulaConstructors)) (Formula (@OneMoreExt (@OneMoreExt Ext))) :=
{|
omc_embed := _
; omc_new := f_ext (ome_embed ome_new)
|}.
Print fplus2_fcplus2.
Definition test1 : Formula (@OneMoreExt (@OneMoreExt False))
:= @omc_new (OneMoreConstructor FormulaConstructors) _ _.
Definition test2 : Formula (@OneMoreExt (@OneMoreExt False))
:= @omc_new (FormulaConstructors) _ _.
Eval compute in (test1, test2). *)
(* Set Printing Implicit. *)
(* Fixpoint nth_new_ext {Ext} n : (@NMoreExt (S n) Ext) :=
match n with
| 0 => ome_new
| S pred => ome_embed (nth_new_ext pred)
end. *)
(* Instance fplus__eq_add_S {n m Ext} :
Class (NMoreConstructors n FormulaConstructors)
(Formula (@NMoreExt (n + S m) Ext))
->
Class (NMoreConstructors n FormulaConstructors)
(Formula (@NMoreExt (S n + m) Ext)).
intro.
change (S n + m) with (S (n + m)).
rewrite (plus_n_Sm n m).
assumption.
Defined. *)
(* Fixpoint fplusnm_fcplusn n m :
Class (FCWithNVars n)
(Formula (@NMoreExt n (@NMoreExt m False))) := match n return Class (FCWithNVars n)
(Formula (@NMoreExt n (@NMoreExt m False))) with
| 0 => f_fc
| S pred =>
{|
omc_embed :=
(* we can technically get away with writing just `_` for this, but that's too magical *)
fplusnm_fcplusn ()
; omc_new := f_ext ome_new
|}
end. *)
(* Instance fplusnm_fcplusn n m :
Class (FCWithNVars n)
(Formula (@NMoreExt n (@NMoreExt m False))).
induction n. exact _.
(* Set Printing Implicit. *)
cbn.
refine
{|
omc_embed :=
_
; omc_new := f_ext ome_new
|}.
:=
match n return Class (NMoreConstructors n FormulaConstructors)
(Formula (@NMoreExt (n + m) Ext)) with
| 0 => f_fc
| S pred =>
{|
omc_embed :=
(* we can technically get away with writing just `_` for this, but that's too magical *)
fplus__eq_add_S (fplusnm_fcplusn pred (S m))
; omc_new := f_ext (nth_new_ext (pred + m))
|}
end.
Existing Instance fplusnm_fcplusn.
Print fplusnm_fcplusn.
Print fplus__eq_add_S. *)
(* Instance fplus__plus_n_0 n {Ext} :
Class (NMoreConstructors n FormulaConstructors)
(Formula (@NMoreExt (n + 0) Ext))
->
Class (NMoreConstructors n FormulaConstructors)
(Formula (@NMoreExt n Ext)).
intro.
refine (eq_rect_r (λ m, Class (NMoreConstructors n FormulaConstructors)
(Formula (@NMoreExt m Ext))) X _).
apply plus_n_O.
Defined.
Print fplus__plus_n_0. *)
Fixpoint NMoreConstructors_subclass n C
: NMoreConstructors n C ⊆ C
:= match n with
| 0 => subclass_refl
| S p => subclass_trans OneMoreConstructor_subclass (NMoreConstructors_subclass p C)
end.
Existing Instance NMoreConstructors_subclass.
(* Lemma NMoreConstructors_flop n C : NMoreConstructors (S n) C = NMoreConstructors n (OneMoreConstructor C).
induction n; [trivial|].
cbn in *.
rewrite <- IHn.
reflexivity.
Qed. *)
(* Set Printing Implicit. *)
Instance nmore_fc n Constrs T : (Constrs ⊆ FormulaConstructors) -> Class (NMoreConstructors n Constrs) T -> Class FormulaConstructors T := λ _ _, _.
(* Definition fwn_to_ind n
: FormulaWithNVars n -> Ind (FCWithNVars n)
:=
nmf_to_ind n (False_rect _). *)
Instance fcn_sub_fc n
: FCWithNVars n ⊆ FormulaConstructors
:= NMoreConstructors_subclass n _.
Instance fcn_pc n : FCWithNVars n ⊆ PropositionConstructors
:= _.
Instance fn_cpc n : Class PropositionConstructors (FormulaWithNVars n)
:= _.
Instance fn_pc n : PropositionConstructors (FormulaWithNVars n)
:= _.
Fixpoint embed_formula
Ext1 Ext2 (embed : Ext1 -> Ext2)
(f : (Formula Ext1)) : (Formula Ext2)
:= match f with
| f_atm a => f_atm a
| f_ext a => f_ext (embed a)
| fo_apl a b => [(embed_formula embed a) (embed_formula embed b)]
end.
(* Fixpoint FCn_embed
n Ext1 Ext2 (embed : Ext1 -> Ext2)
: Class (FCWithNVars n) (Formula Ext1) ->
Class (FCWithNVars n) (Formula Ext2)
:= λ _f, match n return Class (FCWithNVars n) (Formula Ext1) -> Class (FCWithNVars n) (Formula Ext2) with
| 0 => λ _, f_fc
| S n => λ _, {|
omc_embed := FCn_embed ( OneMoreConstructor_subclass _) ;
omc_new := embed_formula embed omc_new
|}
end _f. *)
(* Definition FCn_embed n Ext1 Ext2 (embed : Ext1 -> Ext2) :
Class (FCWithNVars n) (Formula Ext1) ->
Class (FCWithNVars n) (Formula Ext2).
intro.
induction n.
exact f_fc.
exact {|
omc_embed := IHn (OneMoreConstructor_subclass X) ;
omc_new := embed_formula embed omc_new
|}.
Defined. *)
Definition FCn_embed [n m] :
Class (FCWithNVars n) (FormulaWithNVars m) ->
Class (FCWithNVars n) (FormulaWithNVars (S m)).
intro.
induction n.
exact f_fc.
exact {|
omc_embed := IHn (OneMoreConstructor_subclass X) ;
omc_new := embed_formula ome_embed omc_new
|}.
Defined.
(* Inductive ReifiedFCWithNVarsInM : nat -> nat -> Type :=
| rfcn_0 m : ReifiedFCWithNVarsInM 0 m
| rfcn_S n m : ReifiedFCWithNVarsInM n m -> ReifiedFCWithNVarsInM (S n) (S m)
.
Locate "≤".
Print le.
Print le_ind.
Print le_rect.
Fixpoint FCn_embed22 n m (_c : Class (FCWithNVars n) (FormulaWithNVars m)) :
Class (FCWithNVars n) (FormulaWithNVars (S m)) :=
match n with
| 0 => f_fc
| (S pred) => FCn_embed22 pred m
end.
intro.
induction n.
exact f_fc.
exact {|
omc_embed := IHn (OneMoreConstructor_subclass X) ;
omc_new := embed_formula ome_embed omc_new
|}.
Defined. *)
(* Fixpoint ome_new_embed3 {Ext} n R :
(@NMoreExt (S n) Ext -> R) -> R :=
match n with
| 0 => λ embed, embed ome_new
| S pred => λ embed, (@ome_new_embed3 _ pred R (λ e, embed (ome_embed e)))
end.
Print ome_new_embed3. *)
(* Definition next_new_var_in {Ext} m
(prev_var : (NMoreExt m Ext))
: (NMoreExt (S m) Ext)
:= ome_embed prev_var. *)
(* Fixpoint nth_new_var_in_wrong {Ext} n m
: (NMoreExt n (NMoreExt (S m) Ext))
:= match n with
| 0 => ome_new
| S pred => next_new_var_in pred (nth_new_var_in_wrong pred m)
end. *)
(* Fixpoint nth_new_var_in {Ext} n m
: (NMoreExt (S m) (NMoreExt n Ext))
:= match n with
| 0 => ome_new
| S pred => next_new_var_in pred (nth_new_var_in pred m)
end. *)
(* Definition FCn_embed3_impl n m :
Class (FCWithNVars (S n)) (FormulaWithNVars m) ->
Class (FCWithNVars (S n)) (FormulaWithNVars (S m)).
intro.
(* induction n.
exact f_fc. *)
notypeclasses refine {|
omc_embed := _ ;
omc_new := f_ext (nth_new_var_in n m)
|}.
change (Class (FCWithNVars n) (FormulaWithNVars (S m))).
IHn (OneMoreConstructor_subclass X)
Defined. *)
(* Require Import Vector.
Fixpoint nth_var_in n m (vars : Vector.t (NMoreExt (S m) False) n) : (NMoreExt (S m) False).
destruct vars.
exact ome_new.
exact (nth_var_in n m vars).
Defined.
Definition embed_vars n m (vars : Vector.t (NMoreExt m False) n) : Vector.t (NMoreExt (S m) False) n
:= map ome_embed vars. *)
(*
Definition vars_to_ n (vars : Vector.t (NMoreExt n False) n) :
(FCWithNVars n)
(FormulaWithNVars n).
induction n.
exact f_fc.
destruct vars.
exact {|
omc_embed := IHvars ;
omc_new := f_ext ome_new
|}. *)
(* Fixpoint vars_to_ n m (vars : Vector.t (NMoreExt m False) n) :
(FCWithNVars n)
(FormulaWithNVars m) :=
match vars with
| nil => f_fc
| cons h pred t => {|
omc_embed := vars_to_ m t ;
omc_new := f_ext h
|}
end.
Fixpoint right_vars n : Vector.t (NMoreExt n False) n :=
match n with
| 0 => Vector.nil _
| S pred => Vector.cons _ ome_new _ (map ome_embed (right_vars pred))
end. *)
(* Instance fn_fcn n : Class
(FCWithNVars n)
(FormulaWithNVars n)
:= vars_to_ n (right_vars n). *)
Inductive ler (n : nat) : nat -> Type :=
| ler_nn : ler n n
| ler_Sm m : ler n m → ler n (S m).
Definition ltr n m := ler (S n) m.
Inductive upto : nat -> nat -> Type :=
| upto_0m m : upto 0 m
| upto_Sn n m : ltr n m -> upto n m -> upto (S n) m.
(* Inductive leroute : nat -> nat -> Type :=
| leroute_00 : leroute 0 0
| leroute_Sm n m : leroute n m -> leroute n (S m)
| leroute_Sn n m : ltr n m -> leroute n m -> leroute (S n) m. *)
(* Fixpoint ler_0m m : ler 0 m :=
match m with
| 0 => ler_nn 0
| S pred => ler_Sm (ler_0m pred)
end. *)
(* Search le.
Lemma ler_SnSm n m (l : ler n m) : ler (S n) (S m).
induction l; constructor.
assumption.
Defined. *)
Fixpoint ler_Pn [n m] (l : ler (S n) m) : ler n m :=
match l with
| ler_nn => ler_Sm (ler_nn _)
| ler_Sm m l => ler_Sm (ler_Pn l)
end.
(* Set Printing Implicit.
Print ler_Pn. *)
(* Fixpoint ler_pboth n m (l : ler (S n) (S m)) : ler n m :=
match l in ler _ m0 return (m0 = (S m)) -> ler n m with
| ler_nn => λ eq, match eq with eq_refl => ler_nn n end
| ler_Sm m l => λ eq, _
end eq_refl.
dependent induction l.
constructor.
assumption.
Defined. *)
Lemma ler_pboth n m (l : ler (S n) (S m)) : ler n m.
dependent induction l.
constructor.
induction m.
dependent destruction l.
apply ler_Sm.
apply IHl.
reflexivity.
Qed.
Lemma S_not_ler n : ler (S n) n -> False.
intro l. induction n.
dependent destruction l.
exact (IHn (ler_pboth l) ).
Qed.
Lemma ler_unique n m (l0 l1 : ler n m) : l0 = l1.
induction l0.
dependent destruction l1.
reflexivity.
contradict l1; apply S_not_ler.
dependent destruction l1.
clear IHl0; contradict l0; apply S_not_ler.
rewrite (IHl0 l1); reflexivity.
Qed.
Fixpoint upto_Sm n m (u : upto n m) : upto n (S m) :=
match u with
| upto_0m _ => upto_0m _
| upto_Sn _ _ l u => upto_Sn (ler_Sm l) (upto_Sm u)
end.
Fixpoint upto_nn n : upto n n :=
match n with
| 0 => upto_0m _
| S pred => upto_Sn (ler_nn _) (upto_Sm (upto_nn pred))
end.
Fixpoint ler_upto n m (l : ler n m) : upto n m :=
match l with
| ler_nn => upto_nn n
| ler_Sm m l => upto_Sm (ler_upto l)
end.
(* Definition upto_ler n m (u : upto n m) : ler n m :=
match u with
| upto_0m m => ler_0m m
| upto_Sn _ _ l u => l
end. *)
(* Fixpoint leroute_ler n m (r : leroute n m) : ler n m :=
match r with
| leroute_00 => ler_nn _
| leroute_Sm _ _ r => ler_Sm (leroute_ler r)
| leroute_Sn _ _ l r => l
end. *)
Lemma upto_unique n m (u0 u1 : upto n m) : u0 = u1.
induction u0.
dependent destruction u1.
reflexivity.
dependent destruction u1.
rewrite (ler_unique l0 l).
rewrite (IHu0 u1); reflexivity.
Qed.
(* Lemma upto_SnSm n m (u : upto n m) : upto (S n) (S m).
induction u; constructor.
apply ler_SnSm; apply ler_0m.
assumption.
Defined. *)
(* Fixpoint leroute_0m m : leroute 0 m :=
match m with
| 0 => leroute_00