-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProofMode.v
1874 lines (1592 loc) · 69 KB
/
ProofMode.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
Require Import FOL.
Require Import Deduction.
Require Import Tarski.
Require Import VectorTech.
Require Import Theories.
Require Import List.
Require Import Lia.
Require Import List.
Require Import String.
Require Import Equations.Equations Equations.Prop.DepElim.
Import ListNotations.
Open Scope string_scope.
(*
* I want to have an Iris-like proof mode, where the context is displayed
* above a line with the current goal below. Also the assumptions should
* have names.
* This is all done using notation. But this notation should only be applied
* in the goal, not in other hypothesis. Therefore I define aliases for
* `prv` and lists that the notation can match for. Also the list alias
* holds the assumption names as an extra argument.
*)
Definition pm {p cf cp} C phi := @prv p cf cp C phi.
Arguments pm {_} {_} {_} _ _.
Definition tpm {p cf cp} C phi := @tprv p cf cp C phi.
Arguments tpm {_} {_} {_} _ _.
Section PM.
Context {Σ_funcs : funcs_signature}.
Context {Σ_preds : preds_signature}.
Definition cnil := @nil form.
Definition ccons (s : string) phi C := @cons form phi C.
(* Special alias for unknown lists. Only used to indent with one space in Notation *)
Definition cblackbox (A : list form) := A.
Definition tnil : theory := fun _ => False.
Definition tcons (s : string) phi (T : theory) : theory := extend T phi.
(* Special alias for unknown theories. Only used to indent with one space in Notation *)
Definition tblackbox (T : theory) := T.
End PM.
(* Dummy tactic the end user *must* override if defined
* terms like `zero` are used. *)
Ltac custom_fold := idtac.
(* Dummy tactic the end user *must* override if defined
* terms like `zero` are used. *)
Ltac custom_unfold := idtac.
(* Dummy tactic the end user can override to add domain
* specific simplifications. *)
Ltac custom_simpl := idtac.
(** Overload deduction rules to also work for theories: *)
Class DeductionRules `{funcs_signature, preds_signature} (context : Type) (ent : context -> form -> Type) (cons : form -> context -> context) (map : (form -> form) -> context -> context) (In : form -> context -> Prop) :=
{
II A phi psi : ent (cons phi A) psi -> ent A (phi --> psi) ;
IE A phi psi : ent A (phi --> psi) -> ent A phi -> ent A psi ;
AllI A phi : ent (map (subst_form ↑) A) phi -> ent A (∀ phi) ;
AllE A t phi : ent A (∀ phi) -> ent A (phi[t..]) ;
ExI A t phi : ent A (phi[t..]) -> ent A (∃ phi) ;
ExE A phi psi : ent A (∃ phi) -> ent (cons phi (map (subst_form ↑) A)) (psi[↑]) -> ent A psi ;
Exp A phi : ent A ⊥ -> ent A phi ;
Ctx A phi : In phi A -> ent A phi ;
CI A phi psi : ent A phi -> ent A psi -> ent A (phi ∧ psi) ;
CE1 A phi psi : ent A (phi ∧ psi) -> ent A phi ;
CE2 A phi psi : ent A (phi ∧ psi) -> ent A psi ;
DI1 A phi psi : ent A phi -> ent A (phi ∨ psi) ;
DI2 A phi psi : ent A psi -> ent A (phi ∨ psi) ;
DE A phi psi theta : ent A (phi ∨ psi) -> ent (cons phi A) theta -> ent (cons psi A) theta -> ent A theta ;
}.
Class ClassicalDeductionRules `{funcs_signature, preds_signature} (context : Type) (ent : context -> form -> Type) :=
{
Pc A phi psi : ent A (((phi --> psi) --> phi) --> phi)
}.
Class WeakClass `{funcs_signature, preds_signature} (context : Type) (ent : context -> form -> Type) (incl : context -> context -> Prop) :=
{
Weak A B phi : ent A phi -> incl A B -> ent B phi
}.
Instance prv_DeductionRules `{funcs_signature, preds_signature, peirce} : DeductionRules (list form) prv cons (@List.map form form) (@In form) :=
{|
II := Deduction.II ;
IE := Deduction.IE ;
AllI := Deduction.AllI ;
AllE := Deduction.AllE ;
ExI := Deduction.ExI ;
ExE := Deduction.ExE ;
Exp := Deduction.Exp ;
Ctx := Deduction.Ctx ;
CI := Deduction.CI ;
CE1 := Deduction.CE1 ;
CE2 := Deduction.CE2 ;
DI1 := Deduction.DI1 ;
DI2 := Deduction.DI2 ;
DE := Deduction.DE ;
|}.
Instance prv_ClassicalDeductionRules `{funcs_signature, preds_signature} : ClassicalDeductionRules (list form) (@prv _ _ class) :=
{|
Pc := Deduction.Pc
|}.
Instance prv_WeakClass `{funcs_signature, preds_signature, peirce} : WeakClass (list form) prv (@List.incl form) :=
{|
Weak := Deduction.Weak
|}.
Instance tprv_DeductionRules `{funcs_signature, preds_signature, peirce, EqDec syms, EqDec preds} : DeductionRules theory tprv (fun a b => extend b a) mapT (fun a b => in_theory b a) :=
{|
II := Theories.T_II ;
IE := Theories.T_IE ;
AllI := Theories.T_AllI ;
AllE := Theories.T_AllE ;
ExI := Theories.T_ExI ;
ExE := Theories.T_ExE ;
Exp := Theories.T_Exp ;
Ctx := Theories.T_Ctx ;
CI := Theories.T_CI ;
CE1 := Theories.T_CE1 ;
CE2 := Theories.T_CE2 ;
DI1 := Theories.T_DI1 ;
DI2 := Theories.T_DI2 ;
DE := Theories.T_DE ;
|}.
Instance tprv_ClassicalDeductionRules `{funcs_signature, preds_signature} : ClassicalDeductionRules theory (@tprv _ _ class) :=
{|
Pc := Theories.T_Pc
|}.
Instance tprv_WeakClass `{funcs_signature, preds_signature, peirce} : WeakClass theory tprv subset_T :=
{|
Weak := Theories.WeakT
|}.
(** Context utilities *)
Definition digit_to_string n := match n with
| 0 => "0" | 1 => "1" | 2 => "2" | 3 => "3" | 4 => "4" | 5 => "5"
| 6 => "6" | 7 => "7" | 8 => "8" | 9 => "9" | _ => "_"
end.
Fixpoint nat_to_string' fuel n := match fuel with
| 0 => "OUT OF FUEL"
| S fuel' => match n with
| 0 => ""
| _ => nat_to_string' fuel' (Nat.div n 10) ++ digit_to_string (Nat.modulo n 10)
end
end.
Definition nat_to_string n := match n with 0 => "0" | _ => nat_to_string' 100 n end.
(* Returns the index of the first occurence of `name` in the
* context `C`, or `None` if it doesn't exist. *)
Ltac lookup' n C name :=
match C with
| ccons name _ _ => constr:(Some n)
| ccons _ _ ?C' => lookup' (S n) C' name
| tcons name _ _ => constr:(Some n)
| tcons _ _ ?T' => lookup' (S n) T' name
| _ => None
end.
Ltac lookup := lookup' 0.
Ltac nth A n :=
match n with
| 0 => match A with ?t :: _ => t | extend _ ?t => t | ccons _ ?t _ => t | tcons _ ?t _ => t end
| S ?n' => match A with _ :: ?A' => nth A' n' | extend ?A' _ => nth A' n' | ccons _ _ ?A' => nth A' n' | tcons _ _ ?T' => nth T' n' end
end.
Ltac remove A n :=
match n with
| 0 => match A with _ :: ?A' => A' | extend ?A' _ => A' | ccons _ _ ?A' => A' | tcons _ _ ?A' => A' end
| S ?n' => match A with
| ?t :: ?A' => let A'' := remove A' n' in constr:(t::A'')
| extend ?A' ?t => let A'' := remove A' n' in constr:(extend t A'')
| ccons ?s ?t ?A' => let A'' := remove A' n' in constr:(ccons s t A'')
| tcons ?s ?t ?A' => let A'' := remove A' n' in constr:(tcons s t A'')
end
end.
Ltac replace_ltac A n phi :=
match n with
| 0 => match A with _ :: ?A' => constr:(phi::A') | extend ?A' _ => constr:(extend A' phi) | ccons ?s _ ?A' => constr:(ccons s phi A') | tcons ?s _ ?A' => constr:(tcons s phi A') end
| S ?n' => match A with
| ?t :: ?A' => let A'' := replace_ltac A' n' phi in constr:(t::A'')
| extend ?A' ?t => let A'' := replace_ltac A' n' phi in constr:(extend t A'')
| ccons ?s ?t ?A' => let A'' := replace_ltac A' n' phi in constr:(ccons s t A'')
| tcons ?s ?t ?A' => let A'' := replace_ltac A' n' phi in constr:(tcons s t A'')
end
end.
Ltac map_ltac A f :=
match A with
| nil => constr:(nil)
| cnil => constr:(cnil)
| tnil => constr:(tnil)
| @Vector.nil ?a => constr:(@Vector.nil a)
| cblackbox ?A' => A
| tblackbox ?A' => A
| cons ?x ?A' => let x' := f x in let A'' := map_ltac A' f in constr:(cons x' A'')
| ccons ?s ?x ?A' => let x' := f x in let A'' := map_ltac A' f in constr:(ccons s x' A'')
| tcons ?s ?x ?A' => let x' := f x in let A'' := map_ltac A' f in constr:(tcons s x' A'')
| @Vector.cons _ ?x _ ?A' => let x' := f x in let A'' := map_ltac A' f in constr:(@Vector.cons _ x' _ A'')
end.
(* Finds the first name of form `base`, `base0`, `base1`, ... thats not
* contained in the context/variable list `C`. *)
Ltac new_name' n base C :=
let name := match n with
| 0 => base
| S ?n' => let s := eval cbn in (nat_to_string n') in eval cbn in (base ++ s)
end in
match lookup C name with
| @None => name
| @Some _ _ => new_name' (S n) base C
end.
Ltac new_name base C := new_name' 0 base C.
(* For context creation we need to give names to the initial formulas.
* This is done using syntactic matching with ltac instead of a Galina
* function, because if we want to prove `A ⊢ φ` for an unknown A we
* don't want to go into the `A`. *)
Ltac create_context' A :=
match A with
| ?phi::?A' =>
let x := create_context' A' in match x with (?c, ?n) =>
match n with
| 0 => constr:((ccons "H" phi c, S n))
| S ?n' => let s' := eval cbn in ("H" ++ nat_to_string n') in constr:((ccons s' phi c, S n))
end
end
| extend ?T' ?phi =>
let x := create_context' T' in match x with (?c, ?n) =>
match n with
| 0 => constr:((tcons "H" phi c, S n))
| S ?n' => let s' := eval cbn in ("H" ++ nat_to_string n') in constr:((tcons s' phi c, S n))
end
end
| nil => constr:((cnil, 0))
| _ =>
(* If it's not a cons or nil, it's a variable/function call/...
* and we don't want to look into it *)
match type of A with
| list form => constr:((cblackbox A, 0))
| theory => constr:((tblackbox A, 0))
| form -> Prop => constr:((tblackbox A, 0))
end
end.
Ltac create_context A := let x := create_context' A in match x with (?c, _) => c end.
(** Variable names utilities: *)
Definition named_quant {fsig psig ops} op (x : string) phi := @quant fsig psig ops op phi.
Definition named_var {fsig} n (x : string) := @var fsig n.
Arguments named_var {_ _} _.
Ltac annotate_term f t :=
match t with
| var ?n =>
let name := eval cbn in (f n) in
constr:(@named_var _ n name)
| func ?fu ?v =>
let map_fun := annotate_term f in
let v' := map_ltac v map_fun in
constr:(func fu v')
| _ => t
end.
Ltac annotate_form' f idx phi :=
match phi with
| fal => fal
| atom ?P ?v =>
let map_fun := annotate_term f in
let v' := map_ltac v map_fun in
constr:(atom P v')
| bin ?op ?psi1 ?psi2 =>
let psi1' := annotate_form' f idx psi1 in
let psi2' := annotate_form' f idx psi2 in
constr:(bin op psi1' psi2')
| quant ?op ?psi =>
let name := eval cbn in ("x" ++ nat_to_string idx) in
let f' := constr:(fun n => match n with 0 => name | S n' => f n' end) in
let psi' := annotate_form' f' (S idx) psi in
constr:(named_quant op name psi')
| _ => phi
end.
Ltac add_binder_names :=
match goal with
| [ |- @pm _ _ ?p ?C ?phi ] =>
let f := constr:(fun (n : nat) => "ERROR") in
let annotate_form := annotate_form' f 0 in
let phi' := annotate_form phi in
let C' := map_ltac C annotate_form in
change (@pm _ _ p C' phi')
| [ |- @tpm _ _ ?p ?C ?phi ] =>
let f := constr:(fun (n : nat) => "ERROR") in
let annotate_form := annotate_form' f 0 in
let phi' := annotate_form phi in
let C' := map_ltac C annotate_form in
change (@tpm _ _ p C' phi')
end.
Ltac update_binder_names := unfold named_quant; unfold named_var; add_binder_names.
(** Proof Mode: *)
Notation "" := cnil (only printing).
Notation "A" := (cblackbox A) (at level 1, only printing, format " A").
Notation "C name : phi" := (ccons name phi C)
(at level 1, C at level 200, phi at level 200,
left associativity, format "C '//' name : '[' phi ']'", only printing).
Notation "" := tnil (only printing).
Notation "A" := (tblackbox A) (at level 1, only printing, format " A").
Notation "C name : phi" := (tcons name phi C)
(at level 1, C at level 200, phi at level 200,
left associativity, format "C '//' name : '[' phi ']'", only printing).
Notation "∀ x .. y , phi" := (named_quant All x ( .. (named_quant All y phi) .. )) (at level 50, only printing,
format "'[' '∀' '/ ' x .. y , '/ ' phi ']'").
Notation "∃ x .. y , phi" := (named_quant Ex x ( .. (named_quant Ex y phi) .. )) (at level 50, only printing,
format "'[' '∃' '/ ' x .. y , '/ ' phi ']'").
Notation "x" := (named_var x) (at level 1, only printing).
Notation "C '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━' phi" :=
(pm C phi)
(at level 1, left associativity,
format " C '//' '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━' '//' phi", only printing).
Notation "T '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━' phi" :=
(tpm T phi)
(at level 1, left associativity,
format " T '//' '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━' '//' phi", only printing).
(* Tactics to toggle proof mode *)
Ltac fstart :=
match goal with
| [ |- @prv _ _ ?p ?A ?phi ] => let C := create_context A in change (@pm _ _ p C phi)
| [ |- @tprv _ _ ?p ?T ?phi ] => let C := create_context T in change (@tpm _ _ p C phi)
end;
add_binder_names.
Ltac fstop :=
match goal with
| [ |- @pm _ _ ?p ?C ?phi ] => change (@prv _ _ p C phi)
| [ |- @tpm _ _ ?p ?C ?phi ] => change (@tprv _ _ p C phi)
end;
unfold pm in *; unfold cnil; unfold ccons;unfold cblackbox;
unfold tpm in *; unfold tnil; unfold tcons;unfold tblackbox;
unfold named_quant; unfold named_var.
(** Compatability tactics: *)
(* All the tactics defined below work with the original `prv` type.
* The following tactic lifts them to be compatible with `pm`.
*
* Every tactic must have an additional argument where the current
* context is filled in if the proof mode is active, and `cnil`
* otherwise. *)
Ltac make_compatible tac :=
match goal with
| [ |- prv ?A _ ] => tac A
| [ |- tprv ?T _ ] => tac T
| [ |- @pm _ _ ?p ?C _ ] =>
fstop;
tac C;
match goal with
| [ |- pm _ _ ?G ] => change (@pm _ _ p C G)
| [ |- prv _ ?G ] => change (@pm _ _ p C G)
| _ => idtac
end;
try update_binder_names (* [try] because some tactics add normal Coq goals *)
| [ |- @tpm _ _ ?p ?C _ ] =>
fstop;
tac C;
match goal with
| [ |- tprv _ ?G ] => change (@tpm _ _ p C G)
| _ => idtac
end;
try update_binder_names (* [try] because some tactics add normal Coq goals *)
end.
(* [assert] and [enough] that are compatible with all proof modes.
* This way we can avoid matching on the goal each time. *)
Ltac assert_compat' phi H :=
match goal with
| [ |- ?C ⊢ _ ] => assert (@prv _ _ _ C phi) as H
| [ |- ?C ⊩ _ ] => assert (@tprv _ _ _ C phi) as H
| [ |- @pm _ _ _ ?C _ ] => assert (@pm _ _ _ C phi) as H
| [ |- @tpm _ _ _ ?C _ ] => assert (@tpm _ _ _ C phi) as H
end.
Tactic Notation "assert_compat" constr(phi) := let H := fresh in assert_compat' phi H.
Tactic Notation "assert_compat" constr(phi) "as" ident(H) := assert_compat' phi H.
Tactic Notation "assert_compat" constr(phi) "by" tactic(tac) := let H := fresh in assert_compat' phi H; [tac|].
Tactic Notation "assert_compat" constr(phi) "as" ident(H) "by" tactic(tac) := assert_compat' phi H; [tac|].
Ltac enough_compat' phi H :=
match goal with
| [ |- ?C ⊢ _ ] => enough (@prv _ _ _ C phi) as H
| [ |- ?C ⊩ _ ] => enough (@tprv _ _ _ C phi) as H
| [ |- @pm _ _ _ ?C _ ] => enough (@pm _ _ _ C phi) as H
| [ |- @tpm _ _ _ ?C _ ] => enough (@tpm _ _ _ C phi) as H
end.
Tactic Notation "enough_compat" constr(phi) := let H := fresh in enough_compat' phi H.
Tactic Notation "enough_compat" constr(phi) "as" ident(H) := enough_compat' phi H.
Tactic Notation "enough_compat" constr(phi) "by" tactic(tac) := let H := fresh in enough_compat' phi H; [tac|].
Tactic Notation "enough_compat" constr(phi) "as" ident(H) "by" tactic(tac) := enough_compat' phi H; [tac|].
Ltac apply_compat' H1 H2 :=
match goal with
| [ |- _ ⊢ _ ] => apply H1
| [ |- _ ⊩ _ ] => apply H2
end.
Ltac apply_compat_in H1 H2 H :=
match goal with
| [ |- _ ⊢ _ ] => apply H1 in H
| [ |- _ ⊩ _ ] => apply H2 in H
end.
Tactic Notation "apply_compat" constr(H1) constr(H2) := apply_compat' H1 H2.
Tactic Notation "apply_compat" constr(H1) constr(H2) "in" hyp(H) := apply_compat_in H1 H2 H.
(* Return the context of the goal or a hypothesis *)
Ltac get_context_goal :=
match goal with
| [ |- ?C ⊢ _ ] => C
| [ |- ?C ⊩ _ ] => C
| [ |- pm ?C _ ] => C
| [ |- tpm ?C _ ] => C
end.
Ltac get_context_hyp H :=
match type of H with
| ?C ⊢ _ => C
| ?C ⊩ _ => C
| pm ?C _ => C
| tpm ?C _ => C
end.
Tactic Notation "get_context" := get_context_goal.
Tactic Notation "get_context" hyp(H) := get_context_hyp H.
(* Return the formula inside the goal or a hypothesis *)
Ltac get_form_goal :=
match goal with
| [ |- _ ⊢ ?phi ] => phi
| [ |- _ ⊩ ?phi ] => phi
| [ |- pm _ ?phi ] => phi
| [ |- tpm _ ?phi ] => phi
end.
Ltac get_form_hyp H :=
match type of H with
| _ ⊢ ?phi => phi
| _ ⊩ ?phi => phi
| pm _ ?phi => phi
| tpm _ ?phi => phi
end.
Tactic Notation "get_form" := get_form_goal.
Tactic Notation "get_form" hyp(H) := get_form_hyp H.
(** Simplification: *)
(* Spimplify terms that occur during specialization *)
Ltac simpl_subst_hyp H :=
cbn in H;
repeat match type of H with
| context C[S >> var] => let H' := context C[↑] in change H' in H
end;
try rewrite !up_term in H;
try rewrite !subst_term_shift in H;
try rewrite !up_form in H;
try rewrite !subst_shift in H;
(* Turn `(S >> var) 4` into `$5` *)
unfold ">>";
(* Domain specific simplifications: *)
custom_fold;
custom_simpl.
Ltac simpl_subst_goal :=
cbn;
repeat match goal with
| [ |- context C[S >> var] ] => let G := context C[↑] in change G
end;
try rewrite !up_term;
try rewrite !subst_term_shift;
try rewrite !up_form;
try rewrite !subst_shift;
(* Turn `(S >> var) 4` into `$5` *)
unfold ">>";
(* Domain specific simplifications: *)
custom_fold;
custom_simpl.
Tactic Notation "simpl_subst" hyp(H) := (simpl_subst_hyp H).
Tactic Notation "simpl_subst" := (simpl_subst_goal).
(* Syntactically evaluate `mapT f (T ⋄ a ⋄ b ⋄ c)` to
* `(mapT f T) ⋄ f a ⋄ f b ⋄ f c` like it would happen using
* [cbn] for map in normal lists. *)
Ltac eval_mapT M :=
match M with
| mapT ?f (extend ?T ?a) => let T' := eval_mapT (mapT f T) in constr:(extend T' (f a))
| mapT ?f (tcons ?s ?a ?T) => let T' := eval_mapT (mapT f T) in constr:(tcons s (f a) T')
| mapT ?f (tblackbox ?T) => constr:(tblackbox (mapT f T))
| _ => M
end.
Lemma mapT_step `{s1 : funcs_signature, s2 : preds_signature, p : peirce} f a T1 T2 :
subset_T T1 (mapT f T2) -> subset_T (extend T1 (f a)) (mapT f (extend T2 a)).
Proof.
intros H psi H1. destruct H1 as [H1|H1].
- destruct (H psi H1) as [rho [H2 H3]]. exists rho. split. now left. assumption.
- exists a. split. now right. auto.
Qed.
(* Replace `mapT f (T ⋄ a ⋄ b ⋄ c)` in the context with
* `(mapT f T) ⋄ f a ⋄ f b ⋄ f c`. *)
Ltac simpl_context_mapT :=
match goal with
| [ |- tprv ?T ?phi ] =>
let T' := eval_mapT T in
let X := fresh in
enough (tprv T' phi) as X; [
eapply Weak; [now apply X | repeat apply mapT_step; apply subset_refl ]
|]
| [ |- tpm ?T ?phi ] =>
let T' := eval_mapT T in
let X := fresh in
enough (tpm T' phi) as X; [
eapply Weak; [now apply X | repeat apply mapT_step; apply subset_refl ]
|]
end.
(** End user proof tactics: *)
Ltac ctx := make_compatible ltac:(fun _ => apply Ctx; firstorder).
Ltac fexfalso := make_compatible ltac:(fun _ => apply Exp).
Ltac fsplit := make_compatible ltac:(fun _ => apply CI).
Ltac fleft := make_compatible ltac:(fun _ => apply DI1).
Ltac fright := make_compatible ltac:(fun _ => apply DI2).
(*
* [fintro], [fintros]
*
* Similar to Coq. Identifiers need to be given as strings (e.g.
* [fintros "H1" "H2"]). With "?" you can automatically generate
* a name (e.g. [fintros "?" "H"]).
*
* Now also handles intro patterns! For now unneccessary spaces
* are not alowed in intro patterns. E.g. instead of "[H1 | H2]",
* write "[H1|H2]".
*)
(* Intro pattern parsing. This gets its own section to avoid
* importing Ascii globally. *)
Section IntroPattern.
Import Ascii.
Inductive intro_pattern :=
| patId : string -> intro_pattern
| patAnd : intro_pattern -> intro_pattern -> intro_pattern
| patOr : intro_pattern -> intro_pattern -> intro_pattern.
Fixpoint read_name s := match s with
| String "]" s' => ("", String "]" s')
| String " " s' => ("", String " " s')
| String "|" s' => ("", String "|" s')
| String c s' => let (a, s'') := read_name s' in (String c a, s'')
| EmptyString => ("", EmptyString)
end.
Fixpoint parse_intro_pattern' s fuel := match fuel with
| 0 => (None, s)
| S fuel' =>
match s with
| String ("[") s' =>
match parse_intro_pattern' s' fuel' with
| (Some p1, String "|" s'') => match parse_intro_pattern' s'' fuel' with
| (Some p2, String "]" s''') => (Some (patOr p1 p2), s''')
| _ => (None, "")
end
| (Some p1, String " " s'') => match parse_intro_pattern' s'' fuel' with
| (Some p2, String "]" s''') => (Some (patAnd p1 p2), s''')
| _ => (None, "")
end
| _ => (None, "")
end
| String ("]") s' => (Some (patId "?"), String "]" s')
| String " " s' => (Some (patId "?"), String " " s')
| String "|" s' => (Some (patId "?"), String "|" s')
| EmptyString => (None, EmptyString)
| s => let (a, s') := read_name s in (Some (patId a), s')
end
end.
Definition parse_intro_pattern s := fst (parse_intro_pattern' s 100).
End IntroPattern.
Section Fintro.
Context {Σ_funcs : funcs_signature}.
Context {Σ_preds : preds_signature}.
Variable p : peirce.
(* Lemmas for alternative ∀-intro and ∃-application.
* Taken from https://www.ps.uni-saarland.de/extras/fol-completeness/html/Undecidability.FOLC.FullND.html#nameless_equiv_all' *)
Lemma nameless_equiv_all' A phi :
exists t, A ⊢ phi[t..] <-> (map (subst_form ↑) A) ⊢ phi.
Admitted.
Lemma nameless_equiv_ex A phi psi :
exists t, (psi[t..]::A) ⊢ phi <-> (psi::map (subst_form ↑) A) ⊢ phi[↑].
Admitted.
Lemma intro_and_destruct A s t G :
A ⊢ (s --> t --> G) -> A ⊢ (s ∧ t --> G).
Proof.
intros. now apply switch_conj_imp.
Qed.
Lemma intro_or_destruct A s t G :
A ⊢ (s --> G) -> A ⊢ (t --> G) -> A ⊢ (s ∨ t --> G).
Proof.
intros Hs Ht. apply II. eapply DE. ctx.
eapply Weak in Hs. eapply IE. apply Hs. ctx. firstorder.
eapply Weak in Ht. eapply IE. apply Ht. ctx. firstorder.
Qed.
Context {eq_dec_Funcs : EqDec syms}.
Context {eq_dec_Preds : EqDec preds}.
Lemma intro_and_destruct_T T s t G :
T ⊩ (s --> t --> G) -> T ⊩ (s ∧ t --> G).
Proof.
intros. apply II. apply (IE _ t). apply (IE _ s).
eapply Weak. apply H. firstorder.
eapply CE1, Ctx; firstorder.
eapply CE2, Ctx; firstorder.
Qed.
Lemma intro_or_destruct_T T s t G :
T ⊩ (s --> G) -> T ⊩ (t --> G) -> T ⊩ (s ∨ t --> G).
Proof.
intros Hs Ht. apply II. eapply DE. ctx.
eapply Weak in Hs. eapply IE. apply Hs. ctx. firstorder.
eapply Weak in Ht. eapply IE. apply Ht. ctx. firstorder.
Qed.
Lemma subst_zero phi x :
$0 = x -> phi = phi[fun n => match n with 0 => x | S n => $(S n) end].
Proof.
intros. symmetry. apply subst_id. intros [|]; cbn. now rewrite H. reflexivity.
Qed.
Lemma subst_zero_term t x :
$0 = x -> t`[fun n => match n with 0 => x | S n => $(S n) end] = t.
Proof.
intros. apply subst_term_id. intros [|]; cbn. now rewrite H. reflexivity.
Qed.
End Fintro.
(* Check if the name `id` doesn't already occur in the context or
* create a new name if `id = "?"`. *)
Ltac hypname_from_pattern C id :=
match id with
| "?" => new_name "H" C
| _ => match lookup C id with
| @None => id
| @Some _ _ => let msg := eval cbn in ("Identifier already used: " ++ id) in fail 7 msg
end
end.
(* For variable names that are introduced with ∀ this gets infinitely
* more difficult.
* Ltac doesn't have an easy way to convert a Coq string into an identifier.
* I found this snippet using Ltac2 that is used in the Iris proof
* mode, but doesn't seem that stable.
* See https://github.com/coq/coq/issues/7412
*
* Nonetheless I am going to use it, but split up the intro tactic into
* ident and hyp intro. I use tactic notation at the end to also support
* intro with a 'real' Coq ident instead of a string. This should keep
* working if this hack breaks down. *)
Require Import Ltac2StringIdent.
Ltac varname_from_pat pat :=
match pat with
| patId "?" => fresh "x"
| patId ?id => string_to_ident id
end.
Ltac fintro_ident x :=
let H := fresh "H" in
match goal with
| [ |- _ ⊢ ∀ ?t ] =>
apply AllI;
edestruct nameless_equiv_all' as [x H];
apply H; clear H;
simpl_subst
| [ |- @pm _ _ ?p ?C (named_quant All _ ?t) ] =>
apply AllI;
edestruct nameless_equiv_all' as [x H];
apply H; clear H;
simpl_subst;
match goal with [ |- prv _ ?t'] => change (@pm _ _ p C t') end;
update_binder_names
| [ |- _ ⊩ ∀ ?t ] =>
let E := fresh "E" in
apply AllI;
assert (exists x, $0 = x) as [x E] by (now exists ($0));
rewrite (subst_zero t x E);
simpl_context_mapT;
simpl_subst;
repeat (try rewrite subst_zero_term; [| apply E]);
clear E
| [ |- @tpm _ _ ?p ?C (named_quant All _ ?t) ] =>
let E := fresh "E" in
apply AllI;
assert (exists x, $0 = x) as [x E] by (now exists ($0));
rewrite (subst_zero t x E);
simpl_context_mapT;
simpl_subst;
repeat (try rewrite subst_zero_term; [| apply E]);
clear E;
update_binder_names
| _ =>
(* Unfold definitions to check if there are hidden ∀ underneath.
* Also perform simplification and fix names if the definition
* does something nasty. *)
progress custom_unfold; simpl_subst; try update_binder_names;
custom_unfold; (* Unfold again because [simpl_subst] folds *)
fintro_ident x;
custom_fold
end.
Ltac fintro_pat' pat :=
match pat with
| patAnd ?p1 ?p2 => (* Existential *)
make_compatible ltac:(fun C =>
apply II; eapply ExE; [ apply Ctx; now left |
let x := varname_from_pat p1 in
let H := fresh "H" in
edestruct nameless_equiv_ex as [x H];
apply H; clear H; cbn; simpl_subst; apply -> switch_imp;
apply (Weak C); [| firstorder] ]
);
fintro_pat' p2
| patAnd ?p1 ?p2 => (* Conjunction *)
make_compatible ltac:(fun _ =>
match goal with
| [ |- prv _ _ ] => apply intro_and_destruct
| [ |- tprv _ _ ] => apply intro_and_destruct_T
end
);
fintro_pat' p1; fintro_pat' p2
| patOr ?p1 ?p2 =>
make_compatible ltac:(fun _ =>
match goal with
| [ |- prv _ _ ] => apply intro_or_destruct
| [ |- tprv _ _ ] => apply intro_or_destruct_T
end
);
[fintro_pat' p1 | fintro_pat' p2]
| patId ?id =>
match goal with
| [ |- ?A ⊢ ∀ ?t ] => let x := varname_from_pat pat in fintro_ident x
| [ |- ?A ⊩ ∀ ?t ] => let x := varname_from_pat pat in fintro_ident x
| [ |- ?A ⊢ (?s --> ?t) ] => apply II
| [ |- ?A ⊩ (?s --> ?t) ] => apply II
(* Special care for intro in proof mode *)
| [ |- @pm _ _ ?p ?C (named_quant All _ ?t) ] => let x := varname_from_pat pat in fintro_ident x
| [ |- @tpm _ _ ?p ?C (named_quant All _ ?t) ] => let x := varname_from_pat pat in fintro_ident x
| [ |- @pm _ _ ?p ?C (?s --> ?t) ] => apply II; let name := hypname_from_pattern C id in change (@pm _ _ p (ccons name s C) t)
| [ |- @tpm _ _ ?p ?C (?s --> ?t) ] => apply II; let name := hypname_from_pattern C id in change (@tpm _ _ p (tcons name s C) t)
| _ =>
(* Unfold definitions to check if there are hidden ∀ underneath.
* Also perform simplification and fix names if the definition
* does something nasty. *)
progress custom_unfold; simpl_subst; try update_binder_names;
custom_unfold; (* Unfold again because [simpl_subst] folds *)
fintro_pat' pat;
custom_fold
end
end.
Ltac fintro_pat intro_pat :=
match eval cbn in (parse_intro_pattern intro_pat) with
| Some ?p => fintro_pat' p
| None => let msg := eval cbn in ("Invalid intro pattern: " ++ intro_pat) in fail 2 msg
end.
Tactic Notation "fintro" := fintro_pat constr:("?").
Tactic Notation "fintro" constr(H) := fintro_pat H.
Tactic Notation "fintro" ident(x) := fintro_ident x.
Tactic Notation "fintros" := repeat fintro.
Tactic Notation "fintros" constr(H1) := fintro_pat H1.
Tactic Notation "fintros" ident(H1) := fintro_ident H1.
Tactic Notation "fintros" constr(H1) constr(H2) := fintro_pat H1; fintro_pat H2.
Tactic Notation "fintros" ident(H1) constr(H2) := fintro_ident H1; fintro_pat H2.
Tactic Notation "fintros" constr(H1) ident(H2) := fintro_pat H1; fintro_ident H2.
Tactic Notation "fintros" ident(H1) ident(H2) := fintro_ident H1; fintro_ident H2.
Tactic Notation "fintros" constr(H1) constr(H2) constr(H3) := fintro_pat H1; fintro_pat H2; fintro_pat H3.
Tactic Notation "fintros" ident(H1) constr(H2) constr(H3) := fintro_ident H1; fintro_pat H2; fintro_pat H3.
Tactic Notation "fintros" constr(H1) ident(H2) constr(H3) := fintro_pat H1; fintro_ident H2; fintro_pat H3.
Tactic Notation "fintros" constr(H1) constr(H2) ident(H3) := fintro_pat H1; fintro_pat H2; fintro_ident H3.
Tactic Notation "fintros" ident(H1) ident(H2) constr(H3) := fintro_ident H1; fintro_ident H2; fintro_pat H3.
Tactic Notation "fintros" constr(H1) ident(H2) ident(H3) := fintro_pat H1; fintro_ident H2; fintro_ident H3.
Tactic Notation "fintros" ident(H1) ident(H2) ident(H3) := fintro_ident H1; fintro_ident H2; fintro_ident H3.
Tactic Notation "fintros" constr(H1) constr(H2) constr(H3) constr(H4) := fintro_pat H1; fintro_pat H2; fintro_pat H3; fintro_pat H4.
Tactic Notation "fintros" constr(H1) constr(H2) constr(H3) constr(H4) constr(H5) := fintro_pat H1; fintro_pat H2; fintro_pat H3; fintro_pat H4; fintro_pat H4.
(* High level context managment *)
(* Tactic Notation "is_hyp" hyp(H) := idtac. *)
Ltac is_hyp H := match type of H with ?t => match type of t with Prop => idtac end end.
(* Check wether T is a hypothesis, a context index, a context formula
* or a context name and put it into hypothesis H. *)
Ltac turn_into_hypothesis T H contxt :=
tryif is_hyp T
then assert (H := T) (* Hypothesis *)
else match goal with
| [ |- @prv _ _ ?p ?C _ ] =>
match type of T with
| form => assert (@prv _ _ p C T) as H by ctx (* Explicit form *)
| nat => let T' := nth C T in assert (@prv _ _ p C T') as H by ctx (* Idx in context *)
| string => match lookup contxt T with (* Context name *)
| @None => let msg := eval cbn in ("Unknown identifier: " ++ T) in fail 4 msg
| @Some _ ?n => let T' := nth C n in assert (@prv _ _ p C T') as H by ctx
end
end
| [ |- @tprv _ _ ?p ?C _ ] =>
match type of T with
| form => assert (@tprv _ _ p C T) as H by ctx (* Explicit form *)
| nat => let T' := nth C T in assert (@tprv _ _ p C T') as H by ctx (* Idx in context *)
| string => match lookup contxt T with (* Context name *)
| @None => let msg := eval cbn in ("Unknown identifier: " ++ T) in fail 4 msg
| @Some _ ?n => let T' := nth C n in assert (@tprv _ _ p C T') as H by ctx
end
end
end.
(* Replace the context entry T_old with formula `phi` in
* `H_new : X ⊢ phi` *)
Ltac replace_context T_old H_new :=
let C := get_context_goal in
let phi := get_form_hyp H_new in
let psi := get_form_goal in
let X := fresh in
(enough_compat (phi --> psi) as X by eapply (IE _ _ _ X); apply H_new);
let C' := match type of T_old with
| nat => replace_ltac C T_old phi
| form => map_ltac C ltac:(fun f => match f with T_old => phi | ?psi => psi end)
| string => match lookup C T_old with
| @None => let msg := eval cbn in ("Unknown identifier: " ++ T_old) in fail 4 msg
| @Some _ ?n => replace_ltac C n phi
end
end in
fintro; apply (Weak C'); [| firstorder].
(*
* [fspecialize (H x1 x2 ... xn)], [fspecialize H with x1 x2 ... xn]
*
* Specializes a Coq hypothesis `H` of the form `X ⊢ ∀∀...∀ p1 --> ... --> pn --> g`
* with `x1, x2, ..., xn`.
*)
Ltac fspecialize_list H A :=
match A with
| [] => simpl_subst H
| ?x::?A' =>
tryif apply (fun H => IE _ _ _ H x) in H
then idtac
else (
(* For some reason we cannot directly [apply (AllE _ x)]
if x contains ⊕, σ, etc. But evar seems to work. *)
let x' := fresh "x" in
eapply (AllE _ ?[x']) in H;
instantiate (x' := x) );
fspecialize_list H A'
end.
Tactic Notation "fspecialize" "(" hyp(H) constr(x1) ")" := make_compatible ltac:(fun _ => fspecialize_list H constr:([x1])).
Tactic Notation "fspecialize" "(" hyp(H) constr(x1) constr(x2) ")" := make_compatible ltac:(fun _ => fspecialize_list H constr:([x1; x2])).
Tactic Notation "fspecialize" "(" hyp(H) constr(x1) constr(x2) constr(x3) ")" := make_compatible ltac:(fun _ => fspecialize_list H constr:([x1;x2;x3])).
Tactic Notation "fspecialize" hyp(H) "with" constr(x1) := make_compatible ltac:(fun _ => fspecialize_list H constr:([x1])).
Tactic Notation "fspecialize" hyp(H) "with" constr(x1) constr(x2) := make_compatible ltac:(fun _ => fspecialize_list H constr:([x1;x2])).
Tactic Notation "fspecialize" hyp(H) "with" constr(x1) constr(x2) constr(x3) := make_compatible ltac:(fun _ => fspecialize_list H constr:([x1;x2;x3])).
(* Specialize in context *)
Ltac fspecialize_context T A :=
let H := fresh "H" in
make_compatible ltac:(fun C => turn_into_hypothesis "IH" H C);
fspecialize_list H A;
replace_context T H;
clear H.
Tactic Notation "fspecialize" "(" constr(H) constr(x1) ")" := fspecialize_context H constr:([x1]).
Tactic Notation "fspecialize" "(" constr(H) constr(x1) constr(x2) ")" := make_compatible ltac:(fspecialize_context H constr:([x1; x2])).
Tactic Notation "fspecialize" "(" constr(H) constr(x1) constr(x2) constr(x3) ")" := make_compatible ltac:(fspecialize_context H constr:([x1;x2;x3])).
Tactic Notation "fspecialize" constr(H) "with" constr(x1) := make_compatible ltac:(fspecialize_context H constr:([x1])).
Tactic Notation "fspecialize" constr(H) "with" constr(x1) constr(x2) := make_compatible ltac:(fspecialize_context H constr:([x1;x2])).
Tactic Notation "fspecialize" constr(H) "with" constr(x1) constr(x2) constr(x3) := make_compatible ltac:(fspecialize_context H constr:([x1;x2;x3])).
(*
* [fapply (H x1 ... xn)], [feapply (H x1 ... xn)]
*
* Works on
* - Coq hypothesis by name
* - Formula in in ND context by index (e.g. [fapply 3])
* - Explicit formula type in the context (e.g. [fapply ax_symm])
* - Name of a context assumption in proof mode (e.g. [fapply "H2"])
*)
Section Fapply.
Context {Σ_funcs : funcs_signature}.
Context {Σ_preds : preds_signature}.
Variable p : peirce.
Lemma fapply_equiv_l A phi psi :
A ⊢ (phi <--> psi) -> A ⊢ phi -> A ⊢ psi.
Proof.
intros. apply (IE _ phi). eapply CE1. apply H. apply H0.
Qed.
Lemma fapply_equiv_r A phi psi :
A ⊢ (phi <--> psi) -> A ⊢ psi -> A ⊢ phi.
Proof.
intros. apply (IE _ psi). eapply CE2. apply H. apply H0.
Qed.
Context {eq_dec_Funcs : EqDec syms}.
Context {eq_dec_Preds : EqDec preds}.
Lemma fapply_equiv_l_T A phi psi :
A ⊩ (phi <--> psi) -> A ⊩ phi -> A ⊩ psi.
Proof.
intros. apply (IE _ phi). eapply CE1. apply H. apply H0.
Qed.
Lemma fapply_equiv_r_T A phi psi :