-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicFol.v
2678 lines (2389 loc) · 115 KB
/
BasicFol.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.Prelude.Prelude.
Require Import Fol.Prelude.ConstructiveFacts.
Require Import Fol.Math.ThN.
Require Import Fol.Data.Vector.
Require Import Coq.Arith.Wf_nat.
#[local] Infix "\in" := E.In.
#[local] Infix "\subseteq" := E.isSubsetOf.
#[local] Notation In := List.In.
#[projections(primitive)]
Record language : Type :=
{ function_symbols : Set
; constant_symbols : Set
; relation_symbols : Set
; function_arity_table : function_symbols -> nat
; relation_arity_table : relation_symbols -> nat
}.
Section FOL_DEF.
Definition ivar : Set :=
nat.
Context {L : language}.
Inductive trm : Set :=
| Var_trm (x : ivar) : trm
| Fun_trm (f : L.(function_symbols)) (ts : trms (L.(function_arity_table) f)) : trm
| Con_trm (c : L.(constant_symbols)) : trm
with trms : forall arity : nat, Set :=
| O_trms : trms O
| S_trms (n : nat) (t : trm) (ts : trms n) : trms (S n).
Inductive frm : Set :=
| Rel_frm (R : L.(relation_symbols)) (ts : trms (L.(relation_arity_table) R)) : frm
| Eqn_frm (t1 : trm) (t2 : trm) : frm
| Neg_frm (p1 : frm) : frm
| Imp_frm (p1 : frm) (p2 : frm) : frm
| All_frm (y : ivar) (p1 : frm) : frm.
Lemma trms_case0 (phi : trms O -> Type)
(phi_nil : phi O_trms)
: forall ts, phi ts.
Proof.
intros ts. revert phi phi_nil.
exact (
match ts as ts in trms n return (match n as n return trms n -> Type with O => fun ts => forall phi : trms O -> Type, phi O_trms -> phi ts | S n' => fun ts => unit end) ts with
| O_trms => fun phi => fun phi_O => phi_O
| S_trms n' t' ts' => tt
end
).
Defined.
Lemma trms_caseS {n' : nat} (phi : trms (S n') -> Type)
(phi_cons : forall t', forall ts', phi (S_trms n' t' ts'))
: forall ts, phi ts.
Proof.
intros ts. revert phi phi_cons.
exact (
match ts as ts in trms n return (match n as n return trms n -> Type with O => fun _ => unit | S n' => fun ts => forall phi : trms (S n') -> Type, (forall t' : trm, forall ts' : trms n', phi (S_trms n' t' ts')) -> phi ts end) ts with
| O_trms => tt
| S_trms n' t' ts' => fun phi => fun phi_S => phi_S t' ts'
end
).
Defined.
Definition head {n : nat} (ts : trms (S n)) : trm :=
match ts in trms n' return (match n' as n' return Set with O => unit | S n => trm end) with
| O_trms => tt
| S_trms _ t _ => t
end.
Definition tail {n : nat} (ts : trms (S n)) : trms n :=
match ts in trms n' return (match n' as n' return Set with O => unit | S n => trms n end) with
| O_trms => tt
| S_trms _ _ ts => ts
end.
Lemma trms_rec2 (phi : forall arity, trms arity -> trms arity -> Type)
(phi_O : phi O O_trms O_trms)
(phi_S : forall n, forall t, forall t', forall ts, forall ts', phi n ts ts' -> phi (S n) (S_trms n t ts) (S_trms n t' ts'))
: forall n, forall ts, forall ts', phi n ts ts'.
Proof.
induction ts as [ | n t ts IH].
- eapply trms_case0. exact phi_O.
- eapply trms_caseS. intros t' ts'. exact (phi_S n t t' ts ts' (IH ts')).
Defined.
Fixpoint trms_to_vec {n : nat} (ts : trms n) : Vector.t trm n :=
match ts with
| O_trms => []
| S_trms n' t ts => t :: trms_to_vec ts
end.
Lemma trms_to_vec_eq_iff arity (ts : trms arity) (ts' : trms arity)
: trms_to_vec ts = trms_to_vec ts' <-> ts = ts'.
Proof.
split; intros EQ.
- revert EQ. pattern arity, ts, ts'. revert arity ts ts'.
eapply trms_rec2 with (phi := fun n => fun ts => fun ts' => @trms_to_vec n ts = @trms_to_vec n ts' -> ts = ts'); ii.
+ reflexivity.
+ simpl in H0. f_equal.
* apply f_equal with (f := V.head) in H0. do 2 rewrite V.head_unfold in H0; eauto.
* apply f_equal with (f := V.tail) in H0. do 2 rewrite V.tail_unfold in H0; eauto.
- f_equal; eauto.
Qed.
Fixpoint vec_to_trms {n : nat} (ts : Vector.t trm n) : trms n :=
match ts with
| VNil => O_trms
| VCons n t ts => S_trms n t (vec_to_trms ts)
end.
Lemma vec_to_trms_to_vec arity (xs : Vector.t trm arity)
: trms_to_vec (vec_to_trms xs) = xs.
Proof.
induction xs as [ | n x xs IH]; done!.
Qed.
Lemma trms_to_vec_to_trms arity (ts : trms arity)
: vec_to_trms (trms_to_vec ts) = ts.
Proof.
induction ts as [ | n t ts IH]; done!.
Qed.
Definition Bot_frm : frm :=
Neg_frm (All_frm 0 (Eqn_frm (Var_trm 0) (Var_trm 0))).
Definition Con_frm (p1 : frm) (p2 : frm) : frm :=
Neg_frm (Imp_frm p1 (Neg_frm p2)).
Definition Dis_frm (p1 : frm) (p2 : frm) : frm :=
Neg_frm (Con_frm (Neg_frm p1) (Neg_frm p2)).
Definition Iff_frm (p1 : frm) (p2 : frm) : frm :=
Con_frm (Imp_frm p1 p2) (Imp_frm p2 p1).
Definition Exs_frm (y : ivar) (p1 : frm) : frm :=
Neg_frm (All_frm y (Neg_frm p1)).
End FOL_DEF.
#[global] Arguments trm : clear implicits.
#[global] Arguments trms : clear implicits.
#[global] Arguments frm : clear implicits.
Tactic Notation "trm_ind" ident( t ) :=
induction t as [x | f ts | c].
Tactic Notation "trms_ind" ident( ts ) :=
induction ts as [ | n t ts IH].
Tactic Notation "frm_ind" ident( p ) :=
induction p as [R ts | t1 t2 | p1 IH1 | p1 IH1 p2 IH2 | y p1 IH1].
Tactic Notation "trm_ind2" ident( t ) ident( t' ) :=
revert t'; induction t as [x | f ts | c]; intros [x' | f' ts' | c'].
Tactic Notation "trms_ind2" ident( ts ) ident( ts' ) :=
revert ts'; induction ts as [ | n t ts IH]; [intros ts'; pattern ts'; revert ts'; apply trms_case0 | intros ts'; pattern ts'; revert ts'; apply trms_caseS; intros t' ts'].
Tactic Notation "frm_ind2" ident( p ) ident( p' ) :=
revert p'; induction p as [R ts | t1 t2 | p1 IH1 | p1 IH1 p2 IH2 | y p1 IH1]; intros [R' ts' | t1' t2' | p1' | p1' p2' | y' p1'].
Section EQ_DEC.
#[global]
Instance ivar_hasEqDec : hasEqDec ivar :=
Nat.eq_dec.
Context {L : language}.
Hypothesis function_symbols_hasEqDec : hasEqDec L.(function_symbols).
Hypothesis constant_symbols_hasEqDec : hasEqDec L.(constant_symbols).
Lemma trm_eq_dec (t1 : trm L) (t2 : trm L)
: {t1 = t2} + {t1 <> t2}
with trms_eq_dec n (ts1 : trms L n) (ts2 : trms L n)
: {ts1 = ts2} + {ts1 <> ts2}.
Proof with try first [now right; congruence | now left; congruence].
- pose proof ivar_hasEqDec as ivar_hasEqDec.
red in ivar_hasEqDec, function_symbols_hasEqDec, constant_symbols_hasEqDec.
clear trm_eq_dec. trm_ind2 t1 t2...
+ pose proof (ivar_hasEqDec x x') as [? | ?]...
+ pose proof (function_symbols_hasEqDec f f') as [f_eq_f' | f_ne_f']...
subst f'. pose proof (trms_eq_dec (L.(function_arity_table) f) ts ts') as [EQ | NE]...
right. intros CONTRA. eapply NE. inv CONTRA.
apply @projT2_eq_fromEqDec with (B := fun f : L.(function_symbols) => trms L (function_arity_table L f)) in H0.
* exact H0.
* exact function_symbols_hasEqDec.
+ pose proof (constant_symbols_hasEqDec c c') as [? | ?]...
- clear trms_eq_dec. trms_ind2 ts1 ts2...
pose proof (trm_eq_dec t t') as [? | ?]; pose proof (IH ts2) as [EQ | NE]...
right. intros CONTRA. eapply NE. inv CONTRA.
apply @projT2_eq_fromEqDec with (B := fun n : nat => trms L n) in H1.
+ exact H1.
+ exact nat_hasEqDec.
Defined.
#[global]
Instance trm_hasEqDec : hasEqDec (trm L) :=
trm_eq_dec.
#[global]
Instance trms_hasEqDec (n : nat) : hasEqDec (trms L n) :=
trms_eq_dec n.
Hypothesis relation_symbols_hasEqDec : hasEqDec L.(relation_symbols).
Lemma frm_eq_dec (p : frm L) (p' : frm L)
: {p = p'} + {p <> p'}.
Proof with try first [now right; congruence | now left; congruence].
pose proof ivar_hasEqDec as ivar_hasEqDec. red in ivar_hasEqDec. frm_ind2 p p'...
- pose proof (relation_symbols_hasEqDec R R') as [R_eq_R' | R_ne_R']...
subst R'. pose proof (trms_eq_dec (L.(relation_arity_table) R) ts ts') as [EQ | NE]...
right. intros CONTRA. eapply NE. inv CONTRA.
apply @projT2_eq_fromEqDec with (B := fun R : L.(relation_symbols) => trms L (relation_arity_table L R)) in H0.
+ exact H0.
+ exact relation_symbols_hasEqDec.
- pose proof (trm_eq_dec t1 t1') as [? | ?]; pose proof (trm_eq_dec t2 t2') as [? | ?]...
- pose proof (IH1 p1') as [? | ?]...
- pose proof (IH1 p1') as [? | ?]; pose proof (IH2 p2') as [? | ?]...
- pose proof (ivar_hasEqDec y y') as [? | ?]; pose proof (IH1 p1') as [? | ?]...
Defined.
#[global] Instance frm_hasEqDec : hasEqDec (frm L) :=
frm_eq_dec.
End EQ_DEC.
Section ENUMERATION.
Context {L : language}.
Let rank : Set := nat.
Fixpoint trm_depth (t : trm L) : rank :=
match t with
| Var_trm x => 0
| Fun_trm f ts => 1 + trms_depth ts
| Con_trm c => 1
end
with trms_depth {n : nat} (ts : trms L n) : rank :=
match ts with
| O_trms => 0
| S_trms n t ts => 1 + max (trm_depth t) (trms_depth ts)
end.
Lemma trm_depth_unfold (t : trm L) :
trm_depth t =
match t with
| Var_trm x => 0
| Fun_trm f ts => 1 + trms_depth ts
| Con_trm c => 1
end.
Proof.
destruct t; reflexivity.
Defined.
Lemma trms_depth_unfold n (ts : trms L n) :
trms_depth ts =
match ts with
| O_trms => 0
| S_trms n t ts => 1 + max (trm_depth t) (trms_depth ts)
end.
Proof.
destruct ts; reflexivity.
Defined.
Fixpoint frm_depth (p : frm L) : rank :=
match p with
| Rel_frm R ts => 0
| Eqn_frm t1 t2 => 0
| Neg_frm p1 => 1 + frm_depth p1
| Imp_frm p1 p2 => 1 + max (frm_depth p1) (frm_depth p2)
| All_frm y p1 => 1 + frm_depth p1
end.
Lemma frm_depth_unfold (p : frm L) :
frm_depth p =
match p with
| Rel_frm R ts => 0
| Eqn_frm t1 t2 => 0
| Neg_frm p1 => 1 + frm_depth p1
| Imp_frm p1 p2 => 1 + max (frm_depth p1) (frm_depth p2)
| All_frm y p1 => 1 + frm_depth p1
end.
Proof.
destruct p; reflexivity.
Defined.
Lemma frm_depth_lt_ind (P : frm L -> Type)
(IND : forall p : frm L, forall IH : forall p' : frm L, forall RANK_LT : frm_depth p' < frm_depth p, P p', P p)
: forall p : frm L, P p.
Proof.
intros p. induction (relation_on_image_liftsWellFounded Nat.lt frm_depth lt_wf p) as [p _ IH]. exact (IND p IH).
Defined.
Hypothesis function_symbols_countable : isCountable L.(function_symbols).
Hypothesis constant_symbols_countable : isCountable L.(constant_symbols).
Fixpoint gen_trm (seed : nat) (rk : nat) {struct rk} : trm L :=
match rk with
| O => Var_trm seed
| S rk' =>
let '(seed1, seed') := cp seed in
let '(seed2, seed3) := cp seed' in
match seed1 with
| 0 =>
match decode seed' with
| Some c => Con_trm c
| None => Var_trm seed'
end
| 1 =>
match decode seed2 with
| Some f => Fun_trm f (gen_trms seed3 rk')
| None => Var_trm seed2
end
| S (S i) => Var_trm i
end
end
with gen_trms {n : nat} (seed : nat) (rk : nat) {struct rk} : trms L n :=
match n with
| O => O_trms
| S n' =>
match rk with
| O => nat_rec (trms L) O_trms (fun x => S_trms x (Var_trm seed)) (S n')
| S rk' =>
let '(seed1, seed2) := cp seed in
S_trms n' (gen_trm seed1 rk') (gen_trms seed2 rk')
end
end.
Lemma gen_trm_unfold (seed : nat) (rk : nat) :
gen_trm seed rk =
match rk with
| O => Var_trm seed
| S rk' =>
let '(seed1, seed') := cp seed in
let '(seed2, seed3) := cp seed' in
match seed1 with
| 0 =>
match decode seed' with
| Some c => Con_trm c
| None => Var_trm seed'
end
| 1 =>
match decode seed2 with
| Some f => Fun_trm f (gen_trms seed3 rk')
| None => Var_trm seed2
end
| S (S i) => Var_trm i
end
end.
Proof.
destruct rk; reflexivity.
Defined.
Lemma gen_trms_unfold (n : nat) (seed : nat) (rk : nat) :
gen_trms seed rk =
match n with
| O => O_trms
| S n' =>
match rk with
| O => nat_rec (trms L) O_trms (fun x => S_trms x (Var_trm seed)) (S n')
| S rk' =>
let '(seed1, seed2) := cp seed in
S_trms n' (gen_trm seed1 rk') (gen_trms seed2 rk')
end
end.
Proof.
destruct rk, n; reflexivity.
Defined.
Lemma gen_trm_good (t : trm L) (rk : nat)
(RANK_LE : trm_depth t <= rk)
: { seed : nat | gen_trm seed rk = t }
with gen_trms_good n (ts : trms L n) (rk : nat)
(RANK_LE : trms_depth ts <= rk)
: { seed : nat | gen_trms seed rk = ts }.
Proof.
- revert rk RANK_LE. trm_ind t; simpl; i.
+ destruct rk as [ | rk'].
* exists x. reflexivity.
* simpl. exists (cpInv (2 + x) 0).
destruct (cp (cpInv (2 + x) 0)) as [x1 x2] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
simpl. reflexivity.
+ destruct rk as [ | rk']; [lia | assert (RANK_LE' : trms_depth ts <= rk') by lia].
pose proof (gen_trms_good _ ts rk' RANK_LE') as [seed2 H_OBS].
exists (cpInv 1 (cpInv (encode f) seed2)). rewrite gen_trm_unfold.
destruct (cp (cpInv 1 (cpInv (encode f) seed2))) as [x1 x2] eqn: H_OBS'.
rewrite cp_spec in H_OBS'. apply cpInv_inj in H_OBS'. destruct H_OBS' as [<- <-].
destruct (cp (cpInv (encode f) seed2)) as [x2 y2] eqn: H_OBS''.
rewrite cp_spec in H_OBS''. apply cpInv_inj in H_OBS''. destruct H_OBS'' as [<- <-].
rewrite decode_encode. congruence.
+ destruct rk as [ | rk']; [lia | assert (RANK_LE' : 0 <= rk') by lia].
exists (cpInv 0 (encode c)). rewrite gen_trm_unfold.
destruct (cp (cpInv 0 (encode c))) as [x1 x2] eqn: H_OBS'.
rewrite cp_spec in H_OBS'. apply cpInv_inj in H_OBS'. destruct H_OBS' as [<- <-].
destruct (cp (encode c)) as [x1 x2] eqn: H_OBS'. now rewrite decode_encode.
- revert rk RANK_LE. trms_ind ts; simpl; i.
+ simpl. exists 0. rewrite gen_trms_unfold. reflexivity.
+ destruct rk as [ | rk'].
* lia.
* assert (claim1 : trm_depth t <= rk') by lia.
assert (claim2 : trms_depth ts <= rk') by lia.
apply gen_trm_good in claim1. apply IH in claim2.
destruct claim1 as [seed1 P_t'], claim2 as [seed2 P_ts'].
exists (cpInv seed1 seed2). rewrite <- P_t' at 1; rewrite <- P_ts' at 1. rewrite gen_trms_unfold.
destruct (cp (cpInv seed1 seed2)) as [x y] eqn: H_OBS. rewrite cp_spec in H_OBS.
apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-]. reflexivity.
Qed.
Definition enum_trm (x : nat) : trm L :=
let rk := fst (cp x) in
let seed := snd (cp x) in
gen_trm seed rk.
Theorem trm_is_enumerable (t : trm L)
: { x : nat | enum_trm x = t }.
Proof.
epose proof (gen_trm_good t (trm_depth t) _) as [seed H_EQ].
exists (cpInv (trm_depth t) seed). unfold enum_trm. destruct (cp (cpInv (trm_depth t) seed)) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-]. simpl. done.
Unshelve.
reflexivity.
Qed.
Definition enum_trms {n : nat} (x : nat) : trms L n :=
let rk := fst (cp x) in
let seed := snd (cp x) in
gen_trms seed rk.
Theorem trms_is_enumerable n (ts : trms L n)
: { x : nat | enum_trms x = ts }.
Proof.
epose proof (gen_trms_good n ts (trms_depth ts) _) as [seed H_EQ].
exists (cpInv (trms_depth ts) seed). unfold enum_trms. destruct (cp (cpInv (trms_depth ts) seed)) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-]. simpl. done.
Unshelve.
reflexivity.
Qed.
#[local]
Instance trm_isEnumerable : isEnumerable (trm L) :=
{ enum := enum_trm
; enum_spec := trm_is_enumerable
}.
#[local]
Instance trms_isEnumerable (n : nat) : isEnumerable (trms L n) :=
{ enum := enum_trms
; enum_spec := trms_is_enumerable n
}.
Hypothesis relation_symbols_countable : isCountable L.(relation_symbols).
Fixpoint gen_frm (seed : nat) (rk : nat) {struct rk} : frm L :=
match rk with
| O =>
let '(seed1, seed') := cp seed in
let '(seed2, seed3) := cp seed' in
match seed1 with
| 0 => Eqn_frm (enum seed2) (enum seed3)
| _ =>
match decode seed2 with
| Some R => Rel_frm R (enum seed3)
| None => Eqn_frm (enum seed2) (enum seed3)
end
end
| S rk' =>
let '(seed1, seed') := cp seed in
let '(seed2, seed3) := cp seed' in
match seed1 with
| 0 => Neg_frm (gen_frm seed' rk')
| 1 => Imp_frm (gen_frm seed2 rk') (gen_frm seed3 rk')
| 2 => All_frm seed2 (gen_frm seed3 rk')
| S (S (S i)) =>
match i with
| 0 => Eqn_frm (enum seed2) (enum seed3)
| _ =>
match decode seed2 with
| Some R => Rel_frm R (enum seed3)
| None => Eqn_frm (enum seed2) (enum seed3)
end
end
end
end.
Lemma gen_frm_unfold (seed : nat) (rk : nat) :
gen_frm seed rk =
match rk with
| O =>
let '(seed1, seed') := cp seed in
let '(seed2, seed3) := cp seed' in
match seed1 with
| 0 => Eqn_frm (enum seed2) (enum seed3)
| _ =>
match decode seed2 with
| Some R => Rel_frm R (enum seed3)
| None => Eqn_frm (enum seed2) (enum seed3)
end
end
| S rk' =>
let '(seed1, seed') := cp seed in
let '(seed2, seed3) := cp seed' in
match seed1 with
| 0 => Neg_frm (gen_frm seed' rk')
| 1 => Imp_frm (gen_frm seed2 rk') (gen_frm seed3 rk')
| 2 => All_frm seed2 (gen_frm seed3 rk')
| S (S (S i)) =>
match i with
| 0 => Eqn_frm (enum seed2) (enum seed3)
| _ =>
match decode seed2 with
| Some R => Rel_frm R (enum seed3)
| None => Eqn_frm (enum seed2) (enum seed3)
end
end
end
end.
Proof.
destruct rk; reflexivity.
Defined.
Lemma gen_frm_spec (p : frm L) (rk : nat)
(LE : frm_depth p <= rk)
: { seed : nat | gen_frm seed rk = p }.
Proof.
revert rk LE. frm_ind p; simpl; i.
- destruct rk as [ | rk'].
+ exists (cpInv 1 (cpInv (encode R) (proj1_sig (enum_spec ts)))).
rewrite gen_frm_unfold. destruct (cp (cpInv 1 (cpInv (encode R) (proj1_sig (enum_spec ts))))) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
destruct (cp (cpInv (encode R) (proj1_sig (enum_spec ts)))) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
rewrite decode_encode. destruct (enum_spec ts) as [ts_n H_ts]; subst ts. reflexivity.
+ exists (cpInv 4 (cpInv (encode R) (proj1_sig (enum_spec ts)))).
rewrite gen_frm_unfold. destruct (cp (cpInv 4 (cpInv (encode R) (proj1_sig (enum_spec ts))))) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
destruct (cp (cpInv (encode R) (proj1_sig (enum_spec ts)))) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
rewrite decode_encode. destruct (enum_spec ts) as [ts_n H_ts]; subst ts; try reflexivity.
- destruct rk as [ | rk'].
+ exists (cpInv 0 (cpInv (proj1_sig (enum_spec t1)) (proj1_sig (enum_spec t2)))).
rewrite gen_frm_unfold. destruct (cp (cpInv 0 (cpInv (proj1_sig (enum_spec t1)) (proj1_sig (enum_spec t2))))) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
destruct (cp (cpInv (proj1_sig (enum_spec t1)) (proj1_sig (enum_spec t2)))) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
destruct (enum_spec t1) as [t1_n H_t1], (enum_spec t2) as [t2_n H_t2]; subst t1 t2. reflexivity.
+ exists (cpInv 3 (cpInv (proj1_sig (enum_spec t1)) (proj1_sig (enum_spec t2)))).
rewrite gen_frm_unfold. destruct (cp (cpInv 3 (cpInv (proj1_sig (enum_spec t1)) (proj1_sig (enum_spec t2))))) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
destruct (cp (cpInv (proj1_sig (enum_spec t1)) (proj1_sig (enum_spec t2)))) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
destruct (enum_spec t1) as [t1_n H_t1], (enum_spec t2) as [t2_n H_t2]; subst t1 t2. reflexivity.
- destruct rk as [ | rk'].
+ lia.
+ assert (claim1 : frm_depth p1 <= rk') by lia.
apply IH1 in claim1. destruct claim1 as [seed1 H_seed1]. exists (cpInv 0 seed1).
rewrite gen_frm_unfold. destruct (cp (cpInv 0 seed1)) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
destruct (cp seed1) as [x y]. f_equal; done.
- destruct rk as [ | rk'].
+ lia.
+ assert (claim1 : frm_depth p1 <= rk') by lia.
assert (claim2 : frm_depth p2 <= rk') by lia.
apply IH1 in claim1. apply IH2 in claim2. destruct claim1 as [seed1 H_seed1]. destruct claim2 as [seed2 H_seed2]. exists (cpInv 1 (cpInv seed1 seed2)).
rewrite gen_frm_unfold. destruct (cp (cpInv 1 (cpInv seed1 seed2))) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
destruct (cp (cpInv seed1 seed2)) as [x y] eqn: H_OBS. rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-]. f_equal; done.
- destruct rk as [ | rk'].
+ lia.
+ assert (claim1 : frm_depth p1 <= rk') by lia.
apply IH1 in claim1. destruct claim1 as [seed1 H_seed1]. exists (cpInv 2 (cpInv y seed1)).
rewrite gen_frm_unfold. destruct (cp (cpInv 2 (cpInv y seed1))) as [x z] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-].
destruct (cp (cpInv y seed1)) as [x z] eqn: H_OBS. rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-]. f_equal; done.
Qed.
Definition enum_frm (x : nat) : frm L :=
let rk := fst (cp x) in
let seed := snd (cp x) in
gen_frm seed rk.
Theorem frm_is_enumerable (p : frm L)
: { x : nat | enum_frm x = p }.
Proof.
epose proof (gen_frm_spec p (frm_depth p) _) as [seed H_EQ].
exists (cpInv (frm_depth p) seed). unfold enum_frm. destruct (cp (cpInv (frm_depth p) seed)) as [x y] eqn: H_OBS.
rewrite cp_spec in H_OBS. apply cpInv_inj in H_OBS. destruct H_OBS as [<- <-]. simpl. done.
Unshelve.
reflexivity.
Qed.
#[local]
Instance frm_isEnumerable : isEnumerable (frm L) :=
{ enum := enum_frm
; enum_spec := frm_is_enumerable
}.
End ENUMERATION.
Section FOL_SYNTAX. (* Reference: "https://github.com/ernius/formalmetatheory-stoughton" *)
#[local] Hint Unfold compose : simplication_hints.
#[local] Open Scope program_scope.
Import ListNotations.
Definition renaming : Set :=
ivar -> ivar.
Definition subst (L : language) : Set :=
ivar -> trm L.
Context {L : language}.
Fixpoint fvs_trm (t : trm L) : list ivar :=
match t with
| Var_trm x => [x]
| Fun_trm f ts => fvs_trms ts
| Con_trm c => []
end
with fvs_trms {n : nat} (ts : trms L n) : list ivar :=
match ts with
| O_trms => []
| S_trms n t ts => fvs_trm t ++ fvs_trms ts
end.
Fixpoint fvs_frm (p : frm L) : list ivar :=
match p with
| Rel_frm r ts => fvs_trms ts
| Eqn_frm t1 t2 => fvs_trm t1 ++ fvs_trm t2
| Neg_frm p1 => fvs_frm p1
| Imp_frm p1 p2 => fvs_frm p1 ++ fvs_frm p2
| All_frm y p1 => List.remove eq_dec y (fvs_frm p1)
end.
Lemma fvs_trm_unfold (t : trm L) :
fvs_trm t =
match t with
| Var_trm x => [x]
| Fun_trm f ts => fvs_trms ts
| Con_trm c => []
end.
Proof.
destruct t; reflexivity.
Defined.
Lemma fvs_trms_unfold (n : nat) (ts : trms L n) :
fvs_trms ts =
match ts with
| O_trms => []
| S_trms n t ts => fvs_trm t ++ fvs_trms (n := n) ts
end.
Proof.
destruct ts; reflexivity.
Defined.
Lemma fvs_frm_unfold (p : frm L) :
fvs_frm p =
match p with
| Rel_frm r ts => fvs_trms ts
| Eqn_frm t1 t2 => fvs_trm t1 ++ fvs_trm t2
| Neg_frm p1 => fvs_frm p1
| Imp_frm p1 p2 => fvs_frm p1 ++ fvs_frm p2
| All_frm y p1 => List.remove eq_dec y (fvs_frm p1)
end.
Proof.
destruct p; reflexivity.
Defined.
Fixpoint is_free_in_trm (z : ivar) (t : trm L) : bool :=
match t with
| Var_trm x => Nat.eqb x z
| Fun_trm f ts => is_free_in_trms (n := L.(function_arity_table) f) z ts
| Con_trm c => false
end
with is_free_in_trms {n : nat} (z : ivar) (ts : trms L n) : bool :=
match ts with
| O_trms => false
| S_trms n t ts => is_free_in_trm z t || is_free_in_trms (n := n) z ts
end.
Fixpoint is_free_in_frm (z : ivar) (p : frm L) : bool :=
match p with
| Rel_frm R ts => is_free_in_trms (n := L.(relation_arity_table) R) z ts
| Eqn_frm t1 t2 => is_free_in_trm z t1 || is_free_in_trm z t2
| Neg_frm p1 => is_free_in_frm z p1
| Imp_frm p1 p2 => is_free_in_frm z p1 || is_free_in_frm z p2
| All_frm y p1 => is_free_in_frm z p1 && negb (Nat.eqb z y)
end.
Lemma is_free_in_trm_unfold (z : ivar) (t : trm L) :
is_free_in_trm z t =
match t with
| Var_trm x => Nat.eqb x z
| Fun_trm f ts => is_free_in_trms z ts
| Con_trm c => false
end.
Proof.
destruct t; reflexivity.
Defined.
Lemma is_free_in_trms_unfold n (z : ivar) (ts : trms L n) :
is_free_in_trms z ts =
match ts with
| O_trms => false
| S_trms n t ts => is_free_in_trm z t || is_free_in_trms z ts
end.
Proof.
destruct ts; reflexivity.
Defined.
Lemma is_free_in_frm_unfold (z : ivar) (p : frm L) :
is_free_in_frm z p =
match p with
| Rel_frm R ts => is_free_in_trms z ts
| Eqn_frm t1 t2 => is_free_in_trm z t1 || is_free_in_trm z t2
| Neg_frm p1 => is_free_in_frm z p1
| Imp_frm p1 p2 => is_free_in_frm z p1 || is_free_in_frm z p2
| All_frm y p1 => is_free_in_frm z p1 && negb (Nat.eqb z y)
end.
Proof.
destruct p; reflexivity.
Defined.
Lemma fv_is_free_in_trm (t : trm L)
: forall z, In z (fvs_trm t) <-> is_free_in_trm z t = true
with fv_is_free_in_trms n (ts : trms L n)
: forall z, In z (fvs_trms ts) <-> is_free_in_trms z ts = true.
Proof.
- clear fv_is_free_in_trm. trm_ind t; simpl; i; ss!.
- clear fv_is_free_in_trms. trms_ind ts; simpl; i; ss!.
Qed.
#[local] Hint Rewrite fv_is_free_in_trm fv_is_free_in_trms : simplication_hints.
Lemma fv_is_free_in_frm (p : frm L)
: forall z, In z (fvs_frm p) <-> is_free_in_frm z p = true.
Proof.
frm_ind p; simpl; i; ss!.
Qed.
#[local] Hint Rewrite fv_is_free_in_frm : simplication_hints.
Definition is_not_free_in_trm (x : ivar) (t : trm L) : Prop :=
is_free_in_trm x t = false.
Definition is_not_free_in_trms {n : nat} (x : ivar) (ts : trms L n) : Prop :=
is_free_in_trms x ts = false.
Definition is_not_free_in_frm (x : ivar) (p : frm L) : Prop :=
is_free_in_frm x p = false.
#[local] Hint Unfold is_not_free_in_frm is_free_in_trms is_not_free_in_frm : simplication_hints.
Definition fvs_frms (ps : ensemble (frm L)) : ensemble ivar :=
ps >>= E.fromList ∘ fvs_frm.
Lemma fvs_is_free_in_frms (ps : ensemble (frm L))
: forall z, z \in fvs_frms ps <-> (exists p, p \in ps /\ is_free_in_frm z p = true).
Proof.
unfold fvs_frms; ss!; exists x; done!.
Qed.
#[local] Hint Rewrite fvs_is_free_in_frms : simplication_hints.
Definition is_not_free_in_frms (x : ivar) (ps : ensemble (frm L)) : Prop :=
forall p, p \in ps -> is_free_in_frm x p = false.
#[local] Hint Unfold is_not_free_in_frms : simplication_hints.
Definition last_ivar_trm (t : trm L) : ivar :=
maxs (fvs_trm t).
Fixpoint last_ivar_trms {n : nat} (ts : trms L n) : ivar :=
match ts with
| O_trms => 0
| S_trms n t ts => max (last_ivar_trm t) (last_ivar_trms (n := n) ts)
end.
Definition last_ivar_frm (p : frm L) : ivar :=
maxs (fvs_frm p).
Lemma last_ivar_trms_eq_maxs_fvs n (ts : trms L n)
: last_ivar_trms ts = maxs (fvs_trms ts).
Proof.
trms_ind ts; simpl.
- done.
- rewrite maxs_app. done!.
Qed.
#[local] Hint Unfold last_ivar_trm last_ivar_frm : simplication_hints.
#[local] Hint Rewrite <- last_ivar_trms_eq_maxs_fvs : simplication_hints.
Lemma last_ivar_trm_gt (t : trm L) (z : ivar)
(GT : z > last_ivar_trm t)
: is_free_in_trm z t = false
with last_ivar_trms_gt n (ts : trms L n) (z : ivar)
(GT : z > last_ivar_trms ts)
: is_free_in_trms z ts = false.
Proof.
- clear last_ivar_trm_gt; revert z GT. trm_ind t; simpl; i; ss!.
- clear last_ivar_trms_gt; revert z GT. trms_ind ts; simpl; i; ss!.
Qed.
Lemma last_ivar_frm_gt (p : frm L) (z: ivar)
(GT : z > last_ivar_frm p)
: is_free_in_frm z p = false.
Proof.
enough (ENOUGH : ~ In z (fvs_frm p)) by ss!.
pose proof (in_maxs_ge (fvs_frm p)) as claim1.
intros CONTRA. apply claim1 in CONTRA. ss!.
Qed.
Definition chi_frm (s : subst L) (p : frm L) : ivar :=
1 + maxs (List.map (last_ivar_trm ∘ s) (fvs_frm p)).
Lemma chi_frm_not_free (s : subst L) (p : frm L) (x : ivar)
(FREE : is_free_in_frm x p = true)
: is_free_in_trm (chi_frm s p) (s x) = false.
Proof.
enough (ENOUGH : last_ivar_trm (s x) < chi_frm s p) by now eapply last_ivar_trm_gt.
unfold chi_frm. s!. unfold "<". apply fv_is_free_in_frm in FREE.
enough (TO_SHOW : last_ivar_trm (s x) <= maxs (L.map (last_ivar_trm ∘ s) (fvs_frm p))) by done!.
eapply in_maxs_ge. ss!. exists x. done!.
Qed.
Definition nil_subst : subst L :=
fun z : ivar => Var_trm z.
Definition cons_subst (y : ivar) (t : trm L) (s : subst L) : subst L :=
fun z : ivar => if eq_dec z y then t else s z.
Definition one_subst (x1 : ivar) (t1 : trm L) : subst L :=
cons_subst x1 t1 nil_subst.
#[local] Hint Unfold nil_subst cons_subst one_subst : simplication_hints.
Fixpoint subst_trm (s : subst L) (t : trm L) : trm L :=
match t with
| Var_trm x => s x
| Fun_trm f ts => Fun_trm f (subst_trms s ts)
| Con_trm c => Con_trm c
end
with subst_trms {n : nat} (s : subst L) (ts : trms L n) : trms L n :=
match ts with
| O_trms => O_trms
| S_trms n t ts => S_trms n (subst_trm s t) (subst_trms (n := n) s ts)
end.
Fixpoint subst_frm (s : subst L) (p : frm L) : frm L :=
let chi : ivar := chi_frm s p in
match p with
| Rel_frm R ts => Rel_frm R (subst_trms s ts)
| Eqn_frm t1 t2 => Eqn_frm (subst_trm s t1) (subst_trm s t2)
| Neg_frm p1 => Neg_frm (subst_frm s p1)
| Imp_frm p1 p2 => Imp_frm (subst_frm s p1) (subst_frm s p2)
| All_frm y p1 => All_frm chi (subst_frm (cons_subst y (Var_trm chi) s) p1)
end.
Definition subst_compose (s : subst L) (s' : subst L) : subst L :=
fun z : ivar => subst_trm s' (s z).
#[local] Hint Unfold subst_compose : simplication_hints.
Lemma subst_trm_unfold (s : subst L) (t : trm L) :
subst_trm s t =
match t with
| Var_trm x => s x
| Fun_trm f ts => Fun_trm f (subst_trms s ts)
| Con_trm c => Con_trm c
end.
Proof.
destruct t; reflexivity.
Defined.
Lemma subst_trms_unfold n (s : subst L) (ts : trms L n) :
subst_trms s ts =
match ts with
| O_trms => O_trms
| S_trms n t ts => S_trms n (subst_trm s t) (subst_trms s ts)
end.
Proof.
destruct ts; reflexivity.
Defined.
Lemma subst_frm_unfold (s : subst L) (p : frm L) :
subst_frm s p =
let z : ivar := chi_frm s p in
match p with
| Rel_frm R ts => Rel_frm R (subst_trms s ts)
| Eqn_frm t1 t2 => Eqn_frm (subst_trm s t1) (subst_trm s t2)
| Neg_frm p1 => Neg_frm (subst_frm s p1)
| Imp_frm p1 p2 => Imp_frm (subst_frm s p1) (subst_frm s p2)
| All_frm y p1 => All_frm z (subst_frm (cons_subst y (Var_trm z) s) p1)
end.
Proof.
destruct p; reflexivity.
Defined.
Definition trm_is_fresh_in_subst (x : ivar) (s : subst L) (t : trm L) : bool :=
L.forallb (negb ∘ is_free_in_trm x ∘ s) (fvs_trm t).
Definition trms_is_fresh_in_subst {n : nat} (x : ivar) (s : subst L) (ts : trms L n) : bool :=
L.forallb (negb ∘ is_free_in_trm x ∘ s) (fvs_trms ts).
Definition frm_is_fresh_in_subst (x : ivar) (s : subst L) (p : frm L) : bool :=
L.forallb (negb ∘ is_free_in_trm x ∘ s) (fvs_frm p).
Theorem chi_frm_is_fresh_in_subst (p : frm L) (s : subst L)
: frm_is_fresh_in_subst (chi_frm s p) s p = true.
Proof.
unfold frm_is_fresh_in_subst. rewrite forallb_forall. ii.
unfold "∘". rewrite negb_true_iff. eapply chi_frm_not_free.
rewrite <- fv_is_free_in_frm. done.
Qed.
Lemma chi_frm_nil (p : frm L)
: is_free_in_frm (chi_frm nil_subst p) p = false.
Proof.
pose proof (chi_frm_is_fresh_in_subst p nil_subst) as claim1.
unfold frm_is_fresh_in_subst in claim1.
eapply not_true_iff_false. intros CONTRA.
rewrite forallb_forall in claim1. unfold "∘" in claim1. simpl in claim1.
rewrite <- fv_is_free_in_frm in CONTRA. apply claim1 in CONTRA.
rewrite negb_true_iff, Nat.eqb_neq in CONTRA. contradiction.
Qed.
Theorem trm_is_fresh_in_subst_iff (t : trm L) (z : ivar) (s : subst L)
: trm_is_fresh_in_subst z s t = true <-> is_free_in_trm z (subst_trm s t) = false
with trms_is_fresh_in_subst_iff n (ts : trms L n) (z : ivar) (s : subst L)
: trms_is_fresh_in_subst z s ts = true <-> is_free_in_trms z (subst_trms s ts) = false.
Proof.
- clear trm_is_fresh_in_subst_iff; unfold trm_is_fresh_in_subst. revert z s; trm_ind t; ii; ss!.
- clear trms_is_fresh_in_subst_iff; unfold trms_is_fresh_in_subst. revert z s; trms_ind ts; ii; ss!.
Qed.
Theorem frm_is_fresh_in_subst_iff (p : frm L) (z : ivar) (s : subst L)
: frm_is_fresh_in_subst z s p = true <-> is_free_in_frm z (subst_frm s p) = false.
Proof.
unfold frm_is_fresh_in_subst; revert z s. frm_ind p; simpl; ii; s!.
- now rewrite <- trms_is_fresh_in_subst_iff.
- now do 2 rewrite <- trm_is_fresh_in_subst_iff.
- done!.
- done!.
- split.
+ intros H_forallb.
destruct (z =? chi_frm s (All_frm y p1))%nat as [ | ] eqn: OBS; [right; ss! | left].
s!. eapply IH1. rewrite forallb_forall. intros x x_in. s!. destruct (eq_dec x y) as [H_eq | H_ne].
* subst y. rewrite is_free_in_trm_unfold. ss!.
* rewrite forallb_forall in H_forallb. rewrite <- negb_true_iff. eapply H_forallb. ss!.
+ intros [NOT_FREE | ->].
* eapply IH1 in NOT_FREE. rewrite forallb_forall in NOT_FREE. rewrite forallb_forall. intros x x_in; s!.
exploit (NOT_FREE x). ss!. destruct (eq_dec x y) as [EQ | NE]; ss!.
* rewrite forallb_forall. intros x x_in. ss!. eapply chi_frm_not_free. rewrite is_free_in_frm_unfold; ss!.
Qed.
Definition equiv_subst_in_frm (s1 : subst L) (s2 : subst L) (p : frm L) : Prop :=
forall z : ivar, forall FREE : is_free_in_frm z p = true, s1 z = s2 z.
Lemma chi_frm_compat_equiv_subst (s1 : subst L) (s2 : subst L) (p : frm L)
(EQUIV : equiv_subst_in_frm s1 s2 p)
: chi_frm s1 p = chi_frm s2 p.
Proof.
unfold chi_frm. f_equal. eapply maxs_ext. i; ss!; exists x; ss!.
Qed.
Lemma equiv_subst_in_trm_implies_subst_trm_same (s1 : subst L) (s2 : subst L) (t : trm L)
(EQUIV : forall z : ivar, forall FREE : is_free_in_trm z t = true, s1 z = s2 z)
: subst_trm s1 t = subst_trm s2 t
with equiv_subst_in_trms_implies_subst_trms_same n (s1 : subst L) (s2 : subst L) (ts : trms L n)
(EQUIV : forall z : ivar, forall FREE : is_free_in_trms z ts = true, s1 z = s2 z)
: subst_trms s1 ts = subst_trms s2 ts.
Proof.
- clear equiv_subst_in_trm_implies_subst_trm_same; revert s1 s2 EQUIV. trm_ind t; ii; s!.
+ eapply EQUIV; ss!.
+ ss!. eapply equiv_subst_in_trms_implies_subst_trms_same; ii. eapply EQUIV; ss!.
+ ss!.
- clear equiv_subst_in_trms_implies_subst_trms_same; revert s1 s2 EQUIV. trms_ind ts; ii; s!.
+ ss!.
+ ss!.
* eapply equiv_subst_in_trm_implies_subst_trm_same; ii. eapply EQUIV; ss!.
* eapply IH; ii. eapply EQUIV; ss!.
Qed.
Lemma equiv_subst_in_frm_implies_subst_frm_same (s1 : subst L) (s2 : subst L) (p : frm L)
(EQUIV : equiv_subst_in_frm s1 s2 p)
: subst_frm s1 p = subst_frm s2 p.
Proof.
unfold equiv_subst_in_frm; revert s1 s2 EQUIV. frm_ind p; ii; s!.
- f_equal; eapply equiv_subst_in_trms_implies_subst_trms_same; ii; eapply EQUIV; rewrite is_free_in_frm_unfold; ss!.
- f_equal; eapply equiv_subst_in_trm_implies_subst_trm_same; ii; eapply EQUIV; rewrite is_free_in_frm_unfold; ss!.