-
Notifications
You must be signed in to change notification settings - Fork 4
/
termsDB.v
1844 lines (1654 loc) · 54.9 KB
/
termsDB.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 bin_rels.
Require Import eq_rel.
Require Import universe.
Require Import LibTactics.
Require Import tactics.
Require Import Coq.Bool.Bool.
Require Import Coq.Program.Tactics.
Require Import Omega.
Require Import Coq.Program.Basics.
Require Import Coq.Lists.List.
Require Import Coq.Init.Notations.
Require Import UsefulTypes.
Require Import Coq.Classes.DecidableClass.
Require Import Coq.Classes.Morphisms.
Require Import ExtLib.Data.Map.FMapPositive.
Require Import Ring.
Require Import Recdef.
Require Import Eqdep_dec.
Require Import varInterface.
Require Import terms.
Require Import substitution.
Require Import Nsatz.
Require Import Psatz.
Require Import AssociationList.
Require Import SquiggleEq.list.
Require Import alphaeq.
Generalizable Variables Opid Name.
Section terms.
Context `{Deq Name} `{Deq Opid} {gts : GenericTermSig Opid}.
Inductive DTerm : Type :=
| vterm: N (* generalize over N?*) -> DTerm
| oterm: Opid -> list DBTerm -> DTerm
with DBTerm : Type :=
| bterm: list Name -> DTerm -> DBTerm.
(* binders have names, but only for readability.*)
Definition isvar (t : DTerm) :=
match t with
| vterm _ => true
| _ => false
end.
Definition getOpidBTerms (n: DTerm) : option (Opid * list DBTerm) :=
match n with
| vterm _ => None
| oterm o lb => Some (o, lb)
end.
Definition getOpid (n: DTerm) : option Opid :=
option_map fst (getOpidBTerms n).
Definition get_nt (bt: DBTerm ) : DTerm :=
match bt with
| bterm _ nt => nt
end.
Definition get_bvars (bt: DBTerm ) : list Name :=
match bt with
| bterm n _ => n
end.
Definition num_bvars (bt: DBTerm ) : nat := length (get_bvars bt).
Inductive nt_wf: DTerm -> [univ] :=
| wfvt: forall nv : N, nt_wf (vterm nv)
| wfot: forall (o: Opid) (lnt: list DBTerm),
(forall l, LIn l lnt -> bt_wf l)
-> map (num_bvars) lnt
= (OpBindings o)
-> nt_wf (oterm o lnt)
with bt_wf : DBTerm -> [univ] :=
| wfbt : forall (lnv : list Name) (nt: DTerm),
nt_wf nt -> bt_wf (bterm lnv nt).
Open Scope N_scope.
(** Just decidability of equality on variables suffices for these definitions.
The full [VarType] may not be needed until [ssubst]*)
Inductive fvars_below : N -> DTerm -> Prop :=
| var_below: forall i j, j < i -> fvars_below i (vterm j)
| ot_below: forall i (o: Opid) (lnt: list DBTerm),
(forall l, In l lnt -> fvars_below_bt i l)
-> fvars_below i (oterm o lnt)
with fvars_below_bt : N->DBTerm -> Prop :=
| bt_below : forall (i : N) (lv: list Name) (nt: DTerm),
fvars_below (NLength lv +i) nt -> fvars_below_bt i (bterm lv nt).
End terms.
(* By combining several substitutions into one, it enables simpler proofs,
where nested induction (induction on those several substititions, and induction on term)
is avoided.
Also, by not decrementing, the properties of a term is less dependent on history
*)
Fixpoint subst_aux_simpl {Name Opid :Type} (sub: list (N*DTerm)) (e:@DTerm Opid Name)
: @DTerm Opid Name:=
match e with
| vterm i =>
match ALFind sub i with
| Some v => v
| None => vterm i (* never decremented (simple) *)
end
| oterm o lbt => oterm o (map (subst_aux_simpl_bt sub) lbt)
end
with subst_aux_simpl_bt {Opid Name:Type} (sub: list (N*DTerm))
(e:@DBTerm Opid Name): @DBTerm Opid Name:=
match e with
| bterm m t => bterm m (subst_aux_simpl (ALMapDom (N.add (NLength m)) sub) t)
end.
(* this one is used in Certicoq, when substitution with closed terms. *)
Fixpoint subst_aux {Name Opid:Type}(v:DTerm) k (e:@DTerm Name Opid)
: @DTerm Name Opid:=
match e with
| vterm i =>
match N.compare i k with
| Lt => vterm i
| Eq => v
| Gt => vterm (i - 1)%N (* this causes difficulties in reasoning *)
end
| oterm o lbt => oterm o (map (subst_aux_bt v k) lbt)
end
with subst_aux_bt {Name Opid:Type} (v:@DTerm Name Opid)
k (e:@DBTerm Name Opid): @DBTerm Name Opid:=
match e with
| bterm m t => bterm m (subst_aux v (NLength m+k) t)%N
end.
Fixpoint maxFree {Name Opid:Type} (e:@DTerm Name Opid)
: Z :=
(match e with
| vterm i => Z.of_N i
| oterm _ lbt => ZLmax (map maxFree_bt lbt) (-1)
end)%Z
with maxFree_bt {Name Opid:Type} (e:@DBTerm Name Opid): Z:=
(match e with
| bterm m t => (maxFree t) - (Z.of_nat (length m))
end)%Z.
Definition lookupNDef {Name:Type} (def: Name) (names : pmap Name) (var:N) : Name :=
opExtract def (pmap_lookup (N.succ_pos var) names).
Definition insertN {Name:Type} (names : pmap Name) (nvar:N*Name): pmap Name :=
let (var,name) := nvar in
pmap_insert (N.succ_pos var) name names.
Definition insertNs {Name:Type} (names : pmap Name) (nvars: list (N*Name)): pmap Name :=
fold_left insertN nvars names.
Open Scope N_scope.
Fixpoint fromDB {Name Opid NVar : Type} (defn: Name) (mkVar : (N * Name) -> NVar)
(max : N)
(names: pmap Name) (e:@DTerm Name Opid) {struct e}
: (@NTerm NVar Opid) :=
match e with
| vterm n => terms.vterm (mkVar (max-n-1,lookupNDef defn names (max-n-1)))
| oterm o lbt => terms.oterm o (map (fromDB_bt defn mkVar max names) lbt)
end
with fromDB_bt {Name Opid NVar : Type} (defn: Name) (mkVar : (N * Name) -> NVar)
(max : N)
(names: pmap Name) (e:@DBTerm Name Opid) {struct e}
: (@BTerm NVar Opid) :=
match e with
| bterm ln dt =>
let len := length ln in
let vars := (SquiggleEq.list.seq N.succ max len) in
let nvars := (combine vars ln) in
let names := insertNs names nvars in
let bvars := map mkVar nvars in
terms.bterm bvars (fromDB defn mkVar (max+ N.of_nat len) names dt)
end.
(* WARNING : picks vterm 0 if the variable is not found. Thus may delay detection
of bugs. The client should check that e has no free vars, or in general,
subst (free_vars e) context *)
Fixpoint toDB {Name Opid NVar : Type} {deqn: Deq NVar} (getName : NVar -> Name)
(context : list NVar)
(e:@NTerm NVar Opid) {struct e}
: (@DTerm Name Opid) :=
match e with
| terms.vterm n => vterm (opExtract 0(*error*) (firstIndex n context))
| terms.oterm o lbt => oterm o (map (toDB_bt getName context) lbt)
end
with toDB_bt {Name Opid NVar : Type} {deqn: Deq NVar} (getName : NVar -> Name)
(context : list NVar)
(e:@BTerm NVar Opid) {struct e}
: (@DBTerm Name Opid) :=
match e with
| terms.bterm lv nt =>
let context := (rev lv)++context in
bterm (map getName lv) (toDB getName context nt)
end.
Fixpoint shift {Name Opid : Type} (d(*isp*) s(*tart*) :N)
(e:@DTerm Name Opid) {struct e}
: (@DTerm Name Opid) :=
match e with
| vterm i => vterm (if(i <? s) then i else (i+d))
| oterm o lbt => oterm o (map (shift_bt d s) lbt)
end
with shift_bt {Name Opid : Type} (d(*isp*) s(*tart*) :N)
(e:@DBTerm Name Opid) {struct e}
: (@DBTerm Name Opid) :=
match e with
| bterm ln dt =>
bterm ln (shift d (s+NLength ln) dt)
end.
Fixpoint subst_simpl {Name Opid :Type} (sub: list (N*DTerm)) (e:@DTerm Opid Name) {struct e}
: @DTerm Opid Name:=
match e with
| vterm i =>
match ALFind sub i with
| Some v => shift i 0 v
| None => vterm i (* never decremented (simple) *)
end
| oterm o lbt => oterm o (map (subst_simpl_bt sub) lbt)
end
with subst_simpl_bt {Opid Name:Type} (sub: list (N*DTerm))
(e:@DBTerm Opid Name): @DBTerm Opid Name:=
match e with
| bterm m t => bterm m (subst_simpl (ALMapDom (N.add (NLength m)) sub) t)
end.
(* copied from terms2.v *)
Fixpoint size {NVar Opid:Type} (t : @DTerm NVar Opid) : nat :=
match t with
| vterm _ => 1
| oterm op bterms => S (addl (map (@size_bterm NVar _) bterms))
end
with size_bterm {NVar Opid:Type} (bt: @DBTerm NVar Opid) :nat :=
match bt with
| bterm lv nt => @size NVar Opid nt
end.
Fixpoint binderDepth {NVar Opid:Type} (t : @DTerm NVar Opid) : nat :=
match t with
| vterm _ => 0
| oterm op bterms => (maxl (map (@binderDepth_bt NVar _) bterms))
end
with binderDepth_bt {NVar Opid:Type} (bt: @DBTerm NVar Opid) :nat :=
match bt with
| bterm lv nt => length lv + @binderDepth NVar Opid nt
end.
Require Import Coq.Unicode.Utf8.
Section DBToNamed.
Context {Name NVar VarClass : Type} {deqv vcc fvv}
`{vartyp: @VarType NVar VarClass deqv vcc fvv} `{deqo: Deq Opid} {gts : GenericTermSig Opid} (def:Name).
(* copied from terms2.v *)
Lemma size_subterm2 :
forall (o:Opid) (lb : list DBTerm) (b : DBTerm) ,
LIn b lb
-> (size_bterm b < @size Name _ (oterm o lb))%nat.
Proof using.
simpl. induction lb; intros ? Hin; inverts Hin as; simpl; try omega.
intros Hin. apply IHlb in Hin; omega.
Qed.
Lemma size_subterm3 :
forall (o:Opid) (lb : list DBTerm) (nt : DTerm) (lv : list Name) ,
In (bterm lv nt) lb
-> (size nt < size (oterm o lb))%nat.
Proof using.
introv X.
apply size_subterm2 with (o:=o) in X. auto.
Qed.
Lemma NTerm_better_ind3 :
forall P : (@DTerm Name Opid) -> Type,
(forall n : N, P (vterm n))
-> (forall (o : Opid) (lbt : list DBTerm),
(forall (nt: DTerm),
(size nt < size (oterm o lbt))%nat
-> P nt
)
-> P (oterm o lbt)
)
-> forall t : DTerm, P t.
Proof using.
intros P Hvar Hbt.
assert (forall n t, size t = n -> P t) as Hass.
Focus 2. intros. apply Hass with (n := size t) ; eauto; fail.
induction n as [n Hind] using comp_ind_type.
intros t Hsz.
destruct t.
apply Hvar.
apply Hbt. introv Hs.
apply Hind with (m := size nt) ; auto.
subst.
assert(size nt < size (oterm o l))%nat; auto.
Qed.
Lemma NTerm_better_ind2 :
forall P : (@DTerm Name Opid) -> Type,
(forall n : N, P (vterm n))
-> (forall (o : Opid) (lbt : list DBTerm),
(forall (nt nt': DTerm) (lv: list Name),
(LIn (bterm lv nt) lbt)
-> (size nt' <= size nt)%nat
-> P nt'
)
-> P (oterm o lbt)
)
-> forall t : DTerm, P t.
Proof using.
intros P Hvar Hbt.
apply NTerm_better_ind3; eauto.
intros ? ? H.
apply Hbt.
intros ? ? ? Hin Hs. apply H.
eapply le_lt_trans;[apply Hs|].
eapply size_subterm3; eauto.
Qed.
(* TODO: this is structurally recursive use fix to make it compute *)
Lemma NTerm_BTerm_ind :
forall (PN : (@DTerm Name Opid) -> Type) (PB : DBTerm -> Type),
(forall n : N, PN (vterm n))
-> (forall (o : Opid) (lbt : list DBTerm),
(forall b,
(LIn b lbt) -> PB b
)
-> PN (oterm o lbt)
)
-> (forall (lv: list Name) (nt : DTerm),
PN nt -> PB (bterm lv nt)
)
-> ((forall t : DTerm, PN t) * (forall t : DBTerm, PB t)).
Proof using.
introv Hv Hind Hb.
assert (forall A B, A -> (A -> B) -> (A*B)) as H by tauto.
apply H; clear H.
- apply NTerm_better_ind2; auto.
introv Hx. apply Hind. introv Hin. destruct b. eauto.
- intro Hnt. intro b. destruct b. eauto.
Qed.
Lemma pmap_lookup_insert_neq {T}
: ∀ (m : pmap T) (k : positive) (v : T) (k' : positive),
k ≠ k' →
pmap_lookup k' (pmap_insert k v m) = pmap_lookup k' m.
Proof using.
intros.
remember (pmap_lookup k' m).
destruct o; [
apply pmap_lookup_insert_Some_neq; intuition |
apply pmap_lookup_insert_None_neq; intuition].
Qed.
Lemma lookupNDef_insert_neq {T}
: ∀ (m : pmap T) (k : N) (def : T) (p : N*T),
fst p ≠ k →
lookupNDef def (insertN m p) k = lookupNDef def m k.
Proof using.
intros. unfold lookupNDef, insertN. f_equal. destruct p.
apply pmap_lookup_insert_neq. intros Hc.
apply injectiveNsuccpos in Hc. contradiction.
Qed.
(* move to a pmap file? *)
Lemma lookupNDef_insert_eq {T}
: ∀ (m : pmap T) (def : T) (p : N*T),
lookupNDef def (insertN m p) (fst p) = snd p.
Proof using.
intros. unfold lookupNDef, insertN.
destruct p. simpl. rewrite pmap_lookup_insert_eq.
refl.
Qed.
Lemma lookupNDef_inserts_neq {T}
: ∀ (k : N) (def : T) (p : list (N*T)) (m : pmap T),
disjoint (map fst p) [k] →
lookupNDef def (insertNs m p) k = lookupNDef def m k.
Proof using.
intros ? ? ?. induction p;[reflexivity| intros ? Hd].
simpl in *. apply disjoint_cons_l in Hd.
rewrite IHp;[| tauto].
apply lookupNDef_insert_neq; auto. simpl in *.
rewrite N.eq_sym_iff in Hd. tauto.
Qed.
Lemma lookupNDef_inserts_neq_seq {T}
: ∀ (k : N) (def : T) (m : pmap T) len lv n,
k < n →
lookupNDef def (insertNs m (combine (SquiggleEq.list.seq N.succ n len) lv)) k
= lookupNDef def m k.
Proof using.
intros. apply lookupNDef_inserts_neq.
intros ? Hin Hinc.
apply in_single_iff in Hinc. subst.
apply in_map_iff in Hin. exrepnd. subst.
simpl. apply in_combine in Hin1. repnd.
apply in_seq_Nplus in Hin0.
repnd. simpl in *. lia.
Qed.
Lemma lookupNDef_inserts_eq {T}
: ∀ k (def : T) (p : list (N*T)) (m : pmap T),
In k (map fst p)
-> exists v, lookupNDef def (insertNs m p) k = v /\ In (k,v) p.
Proof.
intros ? ?. induction p; simpl; auto;[ tauto |].
intros ? Hin.
destruct (in_dec N.eq_dec k (map fst p)) as [Hd | Hd].
- eapply IHp with (m:=(insertN m a)) in Hd.
exrepnd. eexists; eauto.
- dorn Hin;[| tauto]. subst. rewrite lookupNDef_inserts_neq;
[| repeat disjoint_reasoning; auto].
rewrite lookupNDef_insert_eq. exists (snd a); cpx.
Qed.
Lemma fvars_below_mono:
(forall (nt:@DTerm Name Opid) n m,
n<=m
-> fvars_below n nt
-> fvars_below m nt)
*
(forall (nt:@DBTerm Name Opid) n m,
n<=m
-> fvars_below_bt n nt
-> fvars_below_bt m nt).
Proof using.
apply NTerm_BTerm_ind.
- intros v n m ? Hfb. inverts Hfb. constructor. lia.
- intros ? ? Hind n m ? Hfb. invertsn Hfb.
constructor. eauto.
- intros ? ? Hind n m ? Hfb. invertsn Hfb.
constructor. revert Hfb. apply Hind.
lia.
Qed.
Lemma exp_wf_maxFree:
(forall (s : @DTerm Name Opid) (n:N),
fvars_below n s
<-> maxFree s < Z.of_N n)%Z
*
(forall (s : @DBTerm Name Opid) (n:N),
fvars_below_bt n s
<-> maxFree_bt s < Z.of_N n)%Z.
Proof using.
apply NTerm_BTerm_ind.
- intros v n. split; intros Hfb;
simpl in *.
+ inverts Hfb. lia.
+ constructor. lia.
- intros lb n Hind nn. split; intros Hfb;
simpl in *.
+ inverts Hfb.
apply ZLmax_lub_lt.
intros ? Hin. simpl in Hin.
dorn Hin; subst; simpl in *; try lia;[].
apply in_map_iff in Hin. exrepnd. subst.
eapply Hind; eauto.
+ constructor. intros ? Hin.
specialize (Hind _ Hin).
apply Hind. eapply Z.le_lt_trans; eauto.
clear Hfb.
eapply ZLmax_le3. right.
apply in_map. assumption.
- intros lb n Hind nn. split; intros Hfb;
simpl in *.
+ invertsn Hfb. unfold NLength in Hfb.
apply Hind in Hfb. lia.
+ constructor. unfold NLength in *.
apply Hind. lia.
Qed.
Local Opaque lookupNDef.
(* Local Opaque insertNs. *)
Local Opaque insertN.
Variable mkNVar: (N * Name) -> NVar.
Variable getId: NVar -> N.
Hypothesis getIdCorr: forall p n, getId (mkNVar (p,n)) = p.
Lemma mkNVarInj1 : forall i1 i2 n1 n2,
i1 <> i2
-> mkNVar (i1, n1) ≠ mkNVar (i2, n2).
Proof using getId getIdCorr.
intros ? ? ? ? Heq.
intros Hc. apply (f_equal getId) in Hc.
repeat rewrite getIdCorr in Hc.
contradiction.
Qed.
Local Opaque N.sub.
Local Opaque N.add.
Open Scope N_scope.
Lemma InMkVarCombine : forall i n li ln,
length li = length ln
-> LIn (mkNVar (i, n)) (map mkNVar (combine li ln))
-> LIn i li.
Proof using getIdCorr getId.
intros ? ? ? ? Hl Hin.
apply in_map_iff in Hin.
exrepnd. apply (f_equal getId) in Hin0.
repeat rewrite getIdCorr in Hin0. subst.
eapply in_combine_l; eauto.
Qed.
Lemma InMkVarCombine2 : forall i n li ln,
length li = length ln
-> ¬ LIn i li
-> ¬ LIn (mkNVar (i, n)) (map mkNVar (combine li ln)).
Proof using getIdCorr getId.
intros ? ? ? ? Hl Hin Hc. apply InMkVarCombine in Hc; auto.
Qed.
Lemma InMkVarCombine3 : forall a li ln,
length li = length ln
-> LIn a (map mkNVar (combine li ln))
-> LIn (getId a) li.
Proof using getIdCorr getId.
intros ? ? ? Hl Hin.
apply in_map_iff in Hin. exrepnd. subst.
apply in_combine_l in Hin1.
rewrite getIdCorr. assumption.
Qed.
Let fromDB_bt:= (@fromDB_bt Name Opid NVar def mkNVar).
Lemma fromDB_numbvars:
forall (e : DBTerm) names n,
terms.num_bvars (fromDB_bt n names e) = num_bvars e.
Proof using.
clear.
unfold fromDB_bt.
intros e. destruct e. intros. simpl.
unfold num_bvars, terms.num_bvars. simpl.
simpl.
autorewrite with list.
refl.
Qed.
Let fromDB := (@fromDB Name Opid NVar def mkNVar).
Let fvarsProp (n:N) names (vars : list NVar): Prop :=
lforall
(fun v =>
let id := (getId v) in
(id < n)%N
/\ v = mkNVar (id,(lookupNDef def names id))
) vars.
Lemma fromDB_fvars:
(forall (s : DTerm) (n:N),
fvars_below n s
-> forall names, fvarsProp n names (@free_vars _ _ Opid (fromDB n names s)))
*
(forall (s : DBTerm) (n:N),
fvars_below_bt n s
-> forall names, fvarsProp n names (@free_vars_bterm _ _ Opid (fromDB_bt n names s))).
Proof using getIdCorr.
apply NTerm_BTerm_ind; unfold fvarsProp.
- intros n nv Hfb ? ? Hin.
simpl in *. rewrite or_false_r in Hin. subst.
repeat rewrite getIdCorr in *. split; [ | refl].
inverts Hfb.
lia.
- intros ? ? Hind n Hfb ? ? Hin.
simpl in *. rewrite flat_map_map in Hin.
apply in_flat_map in Hin.
exrepnd. unfold compose in *. simpl in *.
inverts Hfb.
apply Hind in Hin0; eauto.
- intros ? ? Hind n Hfb ? ? Hin.
simpl in *.
rewrite N.add_comm in Hin.
apply in_remove_nvars in Hin. repnd.
invertsn Hfb.
apply Hind in Hin0; [ | assumption].
clear Hind Hfb. exrepnd.
rewrite Hin0.
rewrite Hin0 in Hin.
rewrite Hin0 in Hin1.
clear Hin0.
repeat rewrite getIdCorr in *.
pose proof (N.ltb_spec0 (getId a) n) as Hc.
destruct (getId a <? n); invertsn Hc;[ clear Hin Hin1 | ].
+ split;[ assumption |].
rewrite lookupNDef_inserts_neq_seq; auto.
+ provefalse. apply Hin. apply in_map.
clear Hin. apply N.nlt_ge in Hc.
rewrite N.add_comm in Hin1.
pose proof (conj Hc Hin1) as Hcc. rewrite <- in_seq_Nplus in Hcc.
clear Hc Hin1.
pose proof (combine_map_fst (seq N.succ n (length lv)) lv
(seq_length _ _ _ _)) as He.
rewrite He in Hcc.
apply lookupNDef_inserts_eq with (m:=names) (def0:=def) in Hcc.
exrepnd. rewrite Hcc1. assumption.
Qed.
(* comes up again and again *)
Lemma lengthMapCombineSeq n2 lv:
length (map mkNVar (combine (seq N.succ n2 (length lv)) lv)) = length lv.
Proof using.
repeat rewrite map_length, length_combine_eq;
repeat rewrite seq_length; refl.
Qed.
Lemma getIdMkVar x:
getId (mkNVar x) = fst x.
Proof using getId getIdCorr.
destruct x. simpl.
apply getIdCorr.
Qed.
Lemma mapGetIdMkVar {T} lvn f:
(map (λ x : T, getId (mkNVar (f x))) lvn)
=(map (λ x : T, fst (f x)) lvn).
Proof using getId getIdCorr.
intros.
Fail rewrite getIdMkVar.
Fail setoid_rewrite getIdMkVar.
apply map_ext.
intros.
rewrite getIdMkVar. refl.
Qed.
Lemma mapFstSeqCombine n1 (lv: list Name):
(map fst (combine (seq N.succ n1 (length lv)) lv))
= (seq N.succ n1 (length lv)).
Proof using getIdCorr.
rewrite <- combine_map_fst2;[| rewrite seq_length; refl].
refl.
Qed.
Lemma mapGetIdMapMkVarCombine n1 lv:
(map getId (map mkNVar (combine (seq N.succ n1 (length lv)) lv)))
= (seq N.succ n1 (length lv)).
Proof using getIdCorr.
rewrite map_map. unfold compose. simpl.
rewrite mapGetIdMkVar. apply mapFstSeqCombine.
Qed.
Let fvarsProp2 (n:N) (mf : Z) (vars : list NVar): Prop :=
exists v, In v vars /\ Z.of_N (getId v) = (Z.of_N n - mf -1)%Z.
Lemma fromDB_maxFree:
(forall (s : DTerm) (n:N),
(0 <= maxFree s)%Z
-> (maxFree s < Z.of_N n)%Z
-> forall names, fvarsProp2 n (maxFree s) (@free_vars _ _ Opid
(fromDB n names s)))
*
(forall (s : DBTerm) (n:N),
(0 <= maxFree_bt s)%Z
-> (maxFree_bt s < Z.of_N n)%Z
-> forall names, fvarsProp2 n (maxFree_bt s)
(@free_vars_bterm _ _ Opid (fromDB_bt n names s))).
Proof using getIdCorr.
apply NTerm_BTerm_ind; unfold fvarsProp2.
- intros nv n Hfb ? ?.
simpl.
simpl in *. eexists; split; [left;refl |].
repeat rewrite getIdCorr. lia.
- intros ? ? Hind n Hfb Hlt ?.
simpl in *.
pose proof (ZLmax_In (map maxFree_bt lbt) (-1)) as Hin.
dorn Hin;[provefalse; lia |].
apply in_map_iff in Hin.
exrepnd.
specialize (Hind _ Hin1 n ltac:(lia) ltac:(lia) names).
exrepnd. exists v.
rewrite flat_map_map. unfold compose. simpl.
dands; try lia; try eauto;[].
apply in_flat_map; eauto.
- intros ? ? Hind n Hfb Hlt ?.
simpl in *. fold fromDB.
match goal with
[|- context[fromDB _ ?names _]] =>
specialize (Hind (n + N.of_nat (length lv)) ltac:(lia) ltac:(lia) names)
end.
exrepnd.
exists v.
split;[| lia].
apply in_remove_nvars.
split;[assumption|].
intros Hinc.
apply (in_map getId) in Hinc.
rewrite mapGetIdMapMkVarCombine in Hinc.
apply in_seq_Nplus in Hinc. lia.
Qed.
Let bvarsProp (n:N) (md:nat) (vars : list NVar): Prop :=
lforall
(fun v =>
let id := (getId v) in
(n <= id < n + N.of_nat md)%N
) vars.
Lemma fromDB_bvars:
(forall (s : DTerm) (n:N) names,
bvarsProp n (binderDepth s) (@bound_vars _ _ Opid (fromDB n names s)))
*
(forall (s : DBTerm) (n:N) names,
bvarsProp n (binderDepth_bt s) (@bound_vars_bterm _ _ Opid (fromDB_bt n names s))).
Proof using getIdCorr.
clear fvarsProp.
apply NTerm_BTerm_ind; unfold bvarsProp.
- intros n nv ? ? Hin. simpl in Hin. contradiction.
- intros ? ? Hind n ? ? Hin.
simpl in *. rewrite flat_map_map in Hin.
apply in_flat_map in Hin.
exrepnd. unfold compose in *. simpl in *.
apply Hind in Hin0; eauto.
dands; [tauto | ]. apply proj2 in Hin0.
apply (in_map binderDepth_bt) in Hin1.
apply maxl_prop in Hin1. lia.
- intros ? ? Hind n ? ? Hin.
simpl in *.
apply in_app_or in Hin.
dorn Hin.
+ apply InMkVarCombine3 in Hin;[| apply seq_length].
rewrite in_seq_Nplus in Hin. lia.
+ apply Hind in Hin. clear Hind. lia.
Qed.
Lemma fromDB_all_vars: forall (s : DTerm) (n:N),
fvars_below n s
-> forall names,
lforall
(fun v => getId v < n + N.of_nat (binderDepth s))
(@all_vars _ _ Opid (fromDB n names s)).
Proof using getIdCorr.
intros. intros ? Hin.
apply in_app_or in Hin.
dorn Hin.
- apply (fst fromDB_fvars) in Hin; trivial;[].
simpl in *. repnd. rewrite Hin.
rewrite getIdCorr.
lia.
- apply (fst fromDB_bvars) in Hin; trivial;[].
simpl in *. repnd. tauto.
Qed.
Hint Rewrite seq_length : list.
Definition subst_aux_list n : DTerm -> (list DTerm) -> (@DTerm Name Opid) :=
fold_right (fun v e => subst_aux v n e).
Definition subst_aux_bt_list n : DBTerm -> (list DTerm) -> (@DBTerm Name Opid) :=
fold_right (fun v e => subst_aux_bt v n e).
Lemma subst_aux_list_ot n lbt o l:
subst_aux_list n (oterm o lbt) l
= oterm o (map (fun b => subst_aux_bt_list n b l) lbt).
Proof using.
clear.
induction l; [rewrite map_id;refl|].
simpl. rewrite IHl. simpl.
f_equal. apply map_map.
Qed.
Lemma subst_aux_list_bt n lv nt l:
subst_aux_bt_list n (bterm lv nt) l
= bterm lv (subst_aux_list (NLength lv + n) nt l).
Proof using.
clear.
induction l; auto;[].
simpl. rewrite IHl. simpl. clear IHl.
refl.
Qed.
Lemma subst_aux_closed_nb (v : @DTerm Name Opid) :
(forall t n m,
m<=n
-> fvars_below m t
-> subst_aux v n t = t)
*
(forall t n m,
m<=n
-> fvars_below_bt m t
-> subst_aux_bt v n t = t).
Proof using.
clear.
apply NTerm_BTerm_ind.
- intros ? ? ? Hlt Hfb. inverts Hfb.
simpl. assert (n<n0) as Hltt by lia.
apply N.compare_lt_iff in Hltt.
rewrite Hltt. refl.
- intros ? ? Hind ? ? Hlt Hfb. inverts Hfb.
simpl. f_equal.
rewrite <- map_id.
apply eq_maps. eauto.
- intros ? ? Hind ? ? Hlt Hfb. invertsn Hfb.
simpl. f_equal. apply Hind with (m:=(NLength lv + n)); [lia|].
eapply fvars_below_mono; eauto. lia.
Qed.
Lemma subst_aux_closed (v : @DTerm Name Opid) :
forall t n,
fvars_below 0 t
-> subst_aux v n t = t.
Proof using.
clear. intros.
apply (fun v => fst (subst_aux_closed_nb v)) with (m:=0); auto.
lia.
Qed.
Lemma subst_aux_list_closed n (a : @DTerm Name Opid) l :
fvars_below 0 a
-> fold_left (λ x y : DTerm, subst_aux y n x) l a = a.
Proof using.
clear.
induction l; intro Hfb; auto.
unfold lforall in *.
simpl in *.
rewrite subst_aux_closed;[| apply Hfb; cpx].
eauto.
Qed.
Lemma subst_aux_list_same_aux :
let sub n l := (combine (seq N.succ n (length l)) l) in
(forall (e:DTerm) (l:list DTerm) n,
lforall (fvars_below 0) l
-> fvars_below (n + NLength l) e
-> subst_aux_list n e (rev l) = subst_aux_simpl (sub n l) e)
*
(forall (e:DBTerm) (l:list DTerm) n,
lforall (fvars_below 0) l
-> fvars_below_bt (n + NLength l) e
-> subst_aux_bt_list n e (rev l) = subst_aux_simpl_bt (sub n l) e).
Proof using.
clear.
simpl.
apply NTerm_BTerm_ind.
- intros v ? ? Hc Hfb.
setoid_rewrite fold_left_rev_right. simpl.
revert Hfb. revert v.
induction l; intros ? ?; auto.
simpl in *. rewrite beq_var_sym.
rewrite N.eqb_compare.
remember (v ?= n) as cc.
destruct cc; symmetry in Heqcc.
+ apply N.compare_eq_iff in Heqcc. subst.
apply subst_aux_list_closed. apply Hc. cpx.
+ rewrite N.compare_lt_iff in Heqcc.
rewrite IHl;[| intros ? ?; apply Hc; cpx; fail| constructor; lia].
revert Heqcc. clear. intro.
rewrite ALFindNoneIf;[|
rewrite ALDomCombine; autorewrite with list; auto;[];
rewrite in_seq_Nplus; lia].
rewrite ALFindNoneIf;[|
rewrite ALDomCombine; autorewrite with list; auto;[];
rewrite in_seq_Nplus; lia]. refl.
+ rewrite N.compare_gt_iff in Heqcc.
invertsn Hfb. unfold NLength in *. simpl in *.
rewrite IHl;[| intros ? ?; apply Hc; cpx; fail | constructor; lia].
rewrite <- (option_map_id (ALFind (combine (seq N.succ n (length l)) l) (v - 1))).
rewrite <- ALFindMap with (fk:=N.succ);
[| apply injSucc].
rewrite ALMapCombine.
replace (N.succ (v-1)) with v by lia.
rewrite map_id.
rewrite <- seq_shift. simpl.
dALFind ss; [refl|].
provefalse.
symmetry in Heqss.
apply ALFindNone in Heqss.
apply Heqss. clear Heqss.
rewrite ALDomCombine;[ | autorewrite with list; refl].
rewrite in_seq_Nplus. lia.
- intros ? ? Hind ? ? Hle Hfb. simpl.
rewrite subst_aux_list_ot. f_equal.
apply eq_maps. intros ? Hin. apply Hind; auto.
inverts Hfb. eauto.
- intros ? ? Hind ? ? Hle Hfb. simpl.
rewrite subst_aux_list_bt. f_equal.
rewrite N.add_comm. invertsn Hfb.
rewrite Hind;[| intros ? ?; apply Hle; cpx; fail|
eapply fvars_below_mono; eauto; lia].
f_equal.
setoid_rewrite ALMapCombine.
rewrite map_id.
f_equal.
rewrite N.add_comm.
apply seq_map. intros. lia.
Qed.
Fixpoint subst_aux_list2_aux (e: DTerm) (l:list DTerm) (n:N): (@DTerm Name Opid) :=
match l with
| [] => e
| h::tl => subst_aux h n (subst_aux_list2_aux e tl (N.pred n))
end.
Fixpoint subst_aux_bt_list2_aux (e: DBTerm) (l:list DTerm) (n:N): (@DBTerm Name Opid) :=
match l with
| [] => e
| h::tl => subst_aux_bt h n (subst_aux_bt_list2_aux e tl (N.pred n))
end.
Definition subst_aux_list2 (e: DTerm) (l:list DTerm): (@DTerm Name Opid) :=
subst_aux_list2_aux e l (N.pred (NLength l)).
Definition subst_aux_bt_list2 (e: DBTerm) (l:list DTerm): (@DBTerm Name Opid) :=
subst_aux_bt_list2_aux e l (N.pred (NLength l)).
(*
Lemma subst_aux_list_same_aux (l:list DTerm):
let len := NLength l in
(forall (e:DTerm),
fvars_below len e
-> subst_aux_list e l = subst_aux_list2 e l)
*
(forall (e:DBTerm),
fvars_below_bt len e
-> subst_aux_bt_list e l = subst_aux_bt_list2 e l).
simpl.
Proof.
induction l; [tauto |].
unfold subst_aux_list.
unfold subst_aux_list.
apply NTerm_BTerm_ind.
- unfold subst_aux_list. intros ? Hfb.
rewrite fold_left_right_rev. simpl.
SearchAbout fold_right rev.
intros ? Hfb. simpl.
simpl.
*)
Infix "≡" := alpha_eq (at level 100).
Open Scope program_scope.
Infix "∘≡" := alpha_eq_bterm (at level 100).
Let var names n : NVar := (mkNVar (n,lookupNDef def names n)).
(*
Lemma fromDBHigherAlpha :
(forall v (n1 n2 : N) names1 names2,
fvars_below 0 v
-> fromDB n2 names2 v
≡ fromDB n1 names1 v)
*
(forall v (n1 n2 : N) names1 names2,
fvars_below_bt 0 v
-> fromDB_bt n2 names2 v
∘≡ fromDB_bt n1 names1 v).
Proof.
clear fvarsProp.
apply NTerm_BTerm_ind.
- intros. inverts H. lia.
- admit.
- intros. inverts H0. unfold fromDB_bt.