-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubstitution.v
7360 lines (6440 loc) · 225 KB
/
substitution.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 list.
Require Import Ring.
Require Import Recdef.
Require Import Eqdep_dec.
Require Import varInterface.
Require Import terms.
Require Import terms2.
Require Import AssociationList.
(** printing # $\times$ #×# *)
(** printing $ $\times$ #×# *)
(** printing <=> $\Leftrightarrow$ #⇔# *)
(** printing & $\times$ #×# *)
(* ---- substitution: td[x\ts] *) (*(\x.x+1)(x+2)*)
(*(\y.y+z)[z->y]*)
(** The goal of this section is to formalize the notion of simultaneous
substitution([ssubst]) and alpha equality [alpha_eq].
We needed many properties about substitution and alpha equality
to formalize all of Nuprl. Proofs of all these properties run into
several thousands of lines and took us several weeks to finish.
These proofs are independent
of the operators of the language and will work unchanged
even if we formalize some different language, e.g. first order logic
by merely changing the definition of [Opid]. Thus, we believe
that we have accidentally created a fairly general-purpose
library for nominal reasoning about virtually any language. *)
(** ** Substitution*)
(** The Substitution operation
is a key concept in functional languages.
Among other things, it is required to define the
computation system and the semantics of dependent types.
The goal of this subsection is to formalize [ssubst], a function
that simultaneously substitutes some variables for some terms.
We define a Substitution as a list of pairs:
[[
Definition Substitution : Type := list (NVar # NTerm).
]]
*)
Local Opaque beq_var.
Generalizable Variable Opid.
Section SubstGeneric.
Context {NVar VarClass} {deqnvar : Deq NVar} {varcl freshv}
{varclass: @VarType NVar VarClass deqnvar varcl freshv}
`{hdeq: Deq Opid} {gts : GenericTermSig Opid}.
Notation NTerm := (@NTerm NVar Opid).
Notation BTerm := (@BTerm NVar Opid).
Notation WTerm := (@WTerm NVar Opid).
Notation CTerm := (@CTerm NVar _ Opid gts).
Notation oterm := (@oterm NVar Opid).
Notation bterm := (@bterm NVar Opid).
Notation vterm := (@vterm NVar Opid).
Definition Substitution : Type := AssocList NVar NTerm.
Definition WfSubstitution : Type := lmap NVar WTerm.
Definition CSubstitution : Type := lmap NVar CTerm.
(** % \noindent %
The function [var_ren] below provides a way to
define the specialized substitutions that are
variable renamings (substituting one variable for another).
The %\coqslibref{combine}{Coq.Lists.List}{\coqdocdefinition{combine}}% function
from the standard library takes two lists and zips them up.
*)
Definition var_ren (lvo lvn : list NVar) : Substitution :=
combine lvo (map vterm lvn).
(** % \noindent \\* %
The domain and range of a substitution are defined as follows:
[[
Definition dom_sub (sub : Substitution) : list NVar := map (fun x => fst x) sub.
]]
*)
Definition Sub := Substitution.
Definition CSub := CSubstitution.
Definition dom_sub : Substitution -> (list NVar):= @ALDom NVar NTerm.
Definition dom_csub (sub : CSubstitution) := map (fun x => fst x) sub.
Definition wf_dom_sub (sub : WfSubstitution) := map (fun x => fst x) sub.
Definition range (sub : Substitution) : list NTerm := map (fun x => snd x) sub.
(** % \noindent \\*%
We need to define some helper functions before defining the
substitution function that simultaneously substitutes the
in the first component([NVar]s) of the pairs with the second ones([NTerm]s).
*)
Definition crange (sub : CSubstitution) : list CTerm := map (fun x => snd x) sub.
Lemma deq_in_sub :
forall v t (sub : Substitution),
LIn (v,t) sub + !LIn (v,t) sub.
Proof using deqnvar hdeq.
introv.
apply in_deq; sp.
apply deq_prod; sp; try (apply deq_nvar); try (apply deq_nterm).
Qed.
Definition sub_range_sat (sub: Substitution) (P: NTerm -> [univ]) :=
forall v t, LIn (v,t) sub -> P t.
Definition sub_range_satb (sub: Substitution) (P: NTerm -> [univ]) :=
forall t, assert (memberb _ t (range sub)) -> P t.
Lemma in_range :
forall t (sub: Substitution), LIn t (range sub) -> {v : NVar $ LIn (v,t) sub}.
Proof using.
induction sub; allsimpl; sp; allsimpl; subst.
exists a0; sp.
exists v; sp.
Qed.
Lemma in_range_t :
forall t (sub: Substitution), LIn t (range sub) -> {v : NVar & LIn (v,t) sub}.
Proof using deqnvar hdeq.
introv i.
rw <- (@assert_memberb NTerm _) in i.
induction sub; allsimpl; sp; allsimpl.
unfold assert in i; sp.
rewrite decide_decideP in i.
destruct (decideP (a=t)); subst; sp.
exists a0; sp.
exists v; sp.
Qed.
Lemma sub_range_sat_implies_b :
forall (sub: Substitution) P, sub_range_sat sub P -> sub_range_satb sub P.
Proof using deqnvar.
unfold sub_range_sat, sub_range_satb; introv s a.
rw (@assert_memberb NTerm _) in a.
allapply in_range_t; sp.
discover; sp. eapply s; eauto.
Qed.
Lemma sub_range_sat_implies :
forall (P Q : NTerm -> [univ]),
(forall t, P t -> Q t)
-> forall (sub: Substitution),
sub_range_sat sub P
-> sub_range_sat sub Q.
Proof using.
introv Himp Hsat Hin. apply Hsat in Hin.
apply Himp in Hin. sp.
Qed.
Definition prog_sub (sub : Substitution) := sub_range_sat sub isprogram.
Definition wf_sub (sub : Substitution) := sub_range_sat sub nt_wf.
Require Import list.
(* will not specialize this lemma.. those are always trivial*)
Lemma sub_app_sat :
forall (P : NTerm -> [univ]) sub1 sub2,
sub_range_sat sub1 P
-> sub_range_sat sub2 P
-> sub_range_sat (sub1 ++ sub2) P.
Proof using.
introv sat1 sat2 Hin.
apply in_app_iff in Hin.
dorn Hin; [ apply sat1 in Hin | apply sat2 in Hin]; trivial.
Qed.
Lemma sub_app_sat_if :
forall (P : NTerm -> [univ]) sub1 sub2,
sub_range_sat (sub1 ++ sub2) P
-> sub_range_sat sub1 P # sub_range_sat sub2 P.
Proof using.
introv Hsat.
split;
introv Hin;
assert (LIn (v, t) (sub1 ++ sub2))
as Hx
by (apply in_app_iff;((left;sp;fail) || (right;sp;fail)));
apply Hsat in Hx;sp.
Qed.
Definition sub_find : forall (sub : Substitution) (var : NVar), option NTerm :=
@ALFind NVar NTerm _.
Lemma fold_var_ren :
forall lvo lvn,
combine lvo (map vterm lvn) = var_ren lvo lvn.
Proof using. sp.
Qed.
Definition lmap_apply {A : Type} (eqdec: Deq A) (sub: lmap A A) (a:A): A :=
match lmap_find eqdec sub a with
| inl (existT _ a' _) => a'
| inr _ => a
end.
Definition lmap_lapply {A : Type} (eqdec: Deq A) (sub: lmap A A) (la:list A): list A :=
map (fun a:A => lmap_apply eqdec sub a) la.
Definition lvmap_lapply (sub: lmap NVar NVar) (la:list NVar): list NVar :=
map (fun a:NVar => lmap_apply deq_nvar sub a) la.
Hint Rewrite deqP_refl.
Hint Rewrite deq_refl.
Hint Rewrite deqP_refl:SquiggleEq.
Hint Rewrite deq_refl:SquiggleEq.
Lemma sub_lmap_find: forall (sub: Substitution) v, sub_find sub v =
proj_as_option (lmap_find deq_nvar sub v).
Proof using.
clear hdeq.
induction sub as [| a]; intros ; auto; simpl.
destruct a.
Locate beq_deq.
rewrite (@beq_deq NVar). simpl.
destruct (decideP (n = v));
subst; autorewrite with SquiggleEq; simpl in *; auto.
rewrite IHsub. destruct ((lmap_find deq_nvar sub v)); simpl;
try(destruct s; simpl); auto.
Qed.
Lemma sub_lmap_find_first: forall (sub: Substitution) v, sub_find sub v =
proj_as_option (lmap_find_first deq_nvar sub v).
Proof using.
induction sub as [| a]; intros ; auto; simpl.
destruct a. repeat rewrite beq_deq. simpl in *.
destruct (decideP (n = v));
simpl; subst; autorewrite with SquiggleEq; subst; auto.
rewrite IHsub. destruct ((lmap_find_first deq_nvar sub v)); simpl;
exrepnd; auto.
Qed.
(*
Lemma match_sub_lmap_find: forall sub v cs cn,
match (sub_find sub v)
| Some t => cs t
| None => cn
end
=
match (sub_find sub v)
| Some t => cs t
| None => cn
end
*)
Definition csub2sub (sub : CSubstitution) : Substitution :=
map (fun x => (fst x, get_cterm (snd x))) sub.
Lemma csub2sub_app :
forall sub1 sub2,
csub2sub sub1 ++ csub2sub sub2 = csub2sub (sub1 ++ sub2).
Proof using.
unfold csub2sub; sp.
rewrite <- map_app; sp.
Qed.
Lemma csub2sub_snoc :
forall sub v t,
csub2sub (snoc sub (v, t)) = snoc (csub2sub sub) (v, get_cterm t).
Proof using.
unfold csub2sub; sp.
rewrite map_snoc; sp.
Qed.
Lemma in_csub2sub :
forall sub : CSubstitution,
forall v : NVar,
forall u : NTerm,
LIn (v, u) (csub2sub sub)
-> isprogram u.
Proof using.
induction sub; simpl; sp; destruct a; allsimpl.
inj.
allrw @isprogram_eq; sp.
apply_in_hyp p; sp.
Qed.
(**
We say that a term [t] is covered by a list of variables [vs] if the
free variables of [t] are all in [vs].
*)
Definition covered (t : NTerm) (vs : list NVar) :=
assert (sub_vars (free_vars t) vs).
Definition over_vars (vs : list NVar) (sub : CSubstitution) :=
assert (sub_vars vs (dom_csub sub)).
(**
A term [t] is covered by a substitution [sub] if the free variables
of [t] are all in the domain of [sub].
*)
Definition cover_vars (t : NTerm) (sub : CSubstitution) :=
over_vars (free_vars t) sub.
(**
We sometimes need the slightly more general definition that
expresses that a term [t] is covered by a substitution [sub] up to a
set of variables [vs], meaning that the free variables of [t] have
to either be in [vs] or in the domain of [sub]. Such a concept is
needed to deal with type families such as function or W types.
*)
Definition cover_vars_upto (t : NTerm) (sub : CSub) (vs : list NVar) :=
assert (sub_vars (free_vars t) (vs ++ dom_csub sub)).
(* filters out the mappings whose domain lies in vars *)
Fixpoint lmap_filter {A B: Type}
(eqdec: Deq A) (sub: lmap A B) (vars : list A) : lmap A B :=
match sub with
| nil => nil
| (v, t) :: xs =>
if in_deq A eqdec v vars
then lmap_filter eqdec xs vars
else (v, t) :: lmap_filter eqdec xs vars
end.
(* removes from sub the variables from vars *)
Definition sub_filter :
forall (sub : Substitution) (vars : list NVar), Substitution :=
@ALFilter NVar NTerm _.
Lemma sub_filter_subset :
forall (sub: Substitution) vars,
subset (sub_filter sub vars) sub.
Proof using.
induction sub; simpl; sp.
destruct (memvar a0 vars).
apply subset_cons1; auto.
apply subset_cons2; auto.
Qed.
Lemma sub_filter_nil_r :
forall sub, sub_filter sub [] = sub.
Proof using.
induction sub; simpl; sp.
rewrite IHsub; auto.
Qed.
Lemma in_sub_filter :
forall v t (sub: Substitution) vars,
LIn (v, t) (sub_filter sub vars)
<=>
(
LIn (v, t) sub
#
! LIn v vars
).
Proof using.
induction sub; simpl; sp.
split; sp.
boolvar; simpl; allrw; split; sp; cpx.
Qed.
Lemma sub_filter_sat : forall P (sub: Substitution) lv,
sub_range_sat sub P
-> sub_range_sat (sub_filter sub lv) P.
Proof using. introv Hall hsub. apply in_sub_filter in hsub. repnd.
apply Hall in hsub0; auto.
Qed.
Lemma sub_filter_app :
forall sub1 sub2 vars,
sub_filter (sub1 ++ sub2) vars = sub_filter sub1 vars ++ sub_filter sub2 vars.
Proof using.
induction sub1; simpl; sp.
rewrite IHsub1; auto.
destruct (memvar a0 vars); sp.
Qed.
Lemma sub_filter_snoc :
forall (sub: Substitution) v t vars,
sub_filter (snoc sub (v, t)) vars
= if memvar v vars
then sub_filter sub vars
else snoc (sub_filter sub vars) (v, t).
Proof using.
induction sub; simpl; sp; allsimpl.
rewrite IHsub.
destruct (eq_var_dec a0 v); subst.
destruct (memvar v vars); sp.
destruct (memvar v vars); sp.
destruct (memvar a0 vars); sp.
Qed.
Lemma dom_sub_sub_filter :
forall l (sub: Substitution),
remove_nvars l (dom_sub sub) = dom_sub (sub_filter sub l).
Proof using.
induction sub; simpl; sp; allsimpl.
apply remove_nvars_nil_r.
rewrite remove_nvars_cons_r.
destruct (memvar a0 l); auto.
rewrite IHsub.
simpl; auto.
Qed.
Lemma sub_filter_app_r :
forall (sub: Substitution) vs1 vs2,
sub_filter sub (vs1 ++ vs2)
= sub_filter (sub_filter sub vs1) vs2.
Proof using.
induction sub; simpl; sp.
rewrite memvar_app.
destruct (memvar a0 vs1); simpl.
apply IHsub.
destruct (memvar a0 vs2); simpl.
apply IHsub.
rewrite IHsub; auto.
Qed.
Lemma cover_vars_proof_irrelevance :
forall t sub,
forall x y : cover_vars t sub,
x = y.
Proof using.
intros.
apply UIP_dec.
apply bool_dec.
Qed.
Lemma cover_vars_upto_proof_irrelevance :
forall t sub vs,
forall x y : cover_vars_upto t sub vs,
x = y.
Proof using.
intros.
apply UIP_dec.
apply bool_dec.
Qed.
Lemma dom_sub_snoc :
forall s v t,
dom_sub (snoc s (v, t)) = snoc (dom_sub s) v.
Proof using.
induction s; simpl; sp; simpl; allrw; sp.
Qed.
Lemma dom_csub_snoc :
forall sub x,
dom_csub (snoc sub x) = snoc (dom_csub sub) (fst x).
Proof using.
induction sub; simpl; sp.
rewrite IHsub; sp.
Qed.
Lemma dom_csub_app :
forall sub1 sub2,
dom_csub (sub1 ++ sub2) = dom_csub sub1 ++ dom_csub sub2.
Proof using.
unfold dom_csub; sp.
rewrite map_app; sp.
Qed.
Lemma dom_csub_eq :
forall sub,
dom_sub (csub2sub sub) = dom_csub sub.
Proof using.
induction sub; simpl; sp.
rewrite IHsub; sp.
Qed.
Lemma in_dom_sub :
forall v t sub,
LIn (v, t) sub
-> LIn v (dom_sub sub).
Proof using.
unfold dom_sub; sp.
rw in_map_iff.
exists (v, t); sp.
Qed.
Lemma dom_sub_app :
forall sub1 sub2,
dom_sub (sub1 ++ sub2) = dom_sub sub1 ++ dom_sub sub2.
Proof using.
unfold dom_sub, ALDom; intros; rw map_app; auto.
Qed.
Lemma in_dom_sub_exists :
forall v sub,
LIn v (dom_sub sub)
-> {t : NTerm $ sub_find sub v = Some t}.
Proof.
induction sub; simpl; sp; allsimpl; subst; simpl; boolvar.
exists a; sp.
exists a; sp.
exists a; sp.
exists t; sp.
Qed.
Definition insub sub var : bool :=
match sub_find sub var with
| Some _ => true
| None => false
end.
Lemma sub_find_some :
forall sub : Substitution,
forall v : NVar,
forall u : NTerm,
sub_find sub v = Some u
-> LIn (v, u) sub.
Proof using.
induction sub; simpl; sp.
inversion H.
remember (beq_var a0 v).
destruct b.
inversion H; subst.
apply beq_var_eq in Heqb; subst.
left; auto.
apply IHsub in H; right; auto.
Qed.
Lemma sub_find_some_eq :
forall sub : Substitution,
forall v : NVar,
forall u t : NTerm,
sub_find sub v = Some t
-> sub_find sub v = Some u
-> t = u.
Proof using.
induction sub; simpl; sp.
inversion H.
remember (beq_var a0 v).
destruct b.
inversion H; subst.
inversion H0; subst.
auto.
apply IHsub with (t := t) in H0; auto.
Qed.
Lemma sub_find_app :
forall v sub1 sub2,
sub_find (sub1 ++ sub2) v
= match sub_find sub1 v with
| Some t => Some t
| None => sub_find sub2 v
end.
Proof using.
induction sub1; simpl; sp.
destruct (beq_var a0 v); auto.
Qed.
Lemma sub_find_snoc :
forall v sub x t,
sub_find (snoc sub (x, t)) v
= match sub_find sub v with
| Some t => Some t
| None => if beq_var x v then Some t else None
end.
Proof using.
induction sub; simpl; sp; allsimpl.
destruct (beq_var a0 v); auto.
Qed.
Lemma sub_find_some_app :
forall v t sub1 sub2,
sub_find sub1 v = Some t
-> sub_find (sub1 ++ sub2) v = Some t.
Proof using.
intros.
rw sub_find_app.
rw H; auto.
Qed.
Lemma sub_find_none :
forall sub : Substitution,
forall v : NVar,
forall u : NTerm,
sub_find sub v = None
-> forall u, ! LIn (v, u) sub.
Proof using.
induction sub; simpl; sp; inj.
rw <- @beq_var_refl in H; sp.
remember (beq_var a0 v).
destruct b; sp.
apply IHsub with (u0 := u0) in H; auto.
Qed.
Lemma sub_find_none2 :
forall sub v,
sub_find sub v = None
-> ! LIn v (dom_sub sub).
Proof using.
induction sub; simpl; sp; subst; allsimpl.
rw <- @beq_var_refl in H; inversion H.
remember (beq_var a0 v).
destruct b.
inversion H.
apply IHsub in H; auto.
Qed.
Lemma sub_find_none_iff :
forall sub v,
sub_find sub v = None
<=> ! LIn v (dom_sub sub).
Proof using.
induction sub; simpl; sp; subst; split; sp; allsimpl; subst.
rw <- @beq_var_refl in H; inversion H.
remember (beq_var a0 v); destruct b.
inversion H.
rw IHsub in H; auto.
remember (beq_var a0 v); destruct b.
provefalse; apply H.
apply beq_var_eq in Heqb; left; auto.
symmetry in Heqb.
apply beq_var_false_not_eq in Heqb.
rw IHsub; intro.
apply H; right; auto.
Qed.
(* computes the set of free variables occurring in the co-domain of sub *)
Fixpoint sub_free_vars (sub : Substitution) : list NVar :=
match sub with
| nil => nil
| (v, t) :: xs => free_vars t ++ sub_free_vars xs
end.
Lemma in_sub_free_vars :
forall sub v,
LIn v (sub_free_vars sub)
-> {x : NVar $ {t : NTerm $
LIn (x,t) sub # LIn v (free_vars t) }}.
Proof using.
induction sub; simpl; sp; allsimpl.
allrw in_app_iff; sp.
exists a0,a; sp. apply IHsub in H. sp.
exists x,t; sp.
Qed.
Lemma in_sub_free_vars_iff :
forall sub v,
LIn v (sub_free_vars sub)
<=> {x : NVar $ {t : NTerm $
LIn (x,t) sub # LIn v (free_vars t)}}.
Proof using.
induction sub; simpl; sp.
split; sp.
rw in_app_iff.
rw IHsub; split; sp; inj; sp.
exists a0,a; sp.
exists x,t; sp.
right; exists x,t; sp.
Qed.
Lemma subset_free_vars_mem :
forall v t sub,
LIn (v, t) sub
-> subset (free_vars t) (sub_free_vars sub).
Proof using.
induction sub; simpl; sp; inj.
apply subset_app_r; apply subset_refl.
apply subset_app_l; auto.
Qed.
Lemma subset_sub_free_vars :
forall sub1 sub2,
subset sub1 sub2
-> subset (sub_free_vars sub1) (sub_free_vars sub2).
Proof using.
induction sub1; simpl; sp.
destruct sub2.
allapply subset_cons_nil; sp.
destruct p.
simpl.
allrw cons_subset; allsimpl; sp; inj.
rw app_subset; sp.
apply subset_app_r; apply subset_refl.
apply_in_hyp p; allsimpl; sp.
rw app_subset; sp.
apply subset_app_l.
apply subset_free_vars_mem with (v := a0); auto.
apply_in_hyp p; allsimpl; sp.
Qed.
Lemma sub_free_vars_isprogram :
forall sub,
(forall v t, LIn (v, t) sub -> isprogram t)
-> null (sub_free_vars sub).
Proof using.
induction sub; simpl; intros k; sp.
rw null_app; sp.
generalize (k a0 a); intro i.
dest_imp i hyp.
unfold isprogram, closed in i; sp.
allrw; sp.
apply IHsub; sp.
apply k with (v := v); sp.
Qed.
Definition sub_mk_rename (var : NVar) (fvars : list NVar) : NVar :=
if memvar var fvars
then fresh_var fvars
else var.
(** chose new variables if for bvars if they are in fvars.
if new variables have to be chose, make sure that
the new choices are disjoint from lva.
need not choose a new var if it is in lva but not in fvars.
This is to avoid renamings as much as possible
*)
Fixpoint sub_mk_renames2 (bvars : list NVar) (fvars : list NVar)
(lva: list NVar): (list NVar) * Substitution :=
match bvars with
| nil => (nil, nil)
| v :: vs =>
let (vars, sub) := sub_mk_renames2 vs fvars lva in
if memvar v fvars
then let u := fresh_var (vars ++ fvars ++ lva) in
(u :: vars, (v, vterm u) :: sub)
else (v :: vars, sub)
end.
(* generates renamings for all the variables in bvars that also occur in fvars *)
Fixpoint sub_mk_renames (bvars : list NVar) (fvars : list NVar) :
(list NVar) * Substitution :=
match bvars with
| nil => (nil, nil)
| v :: vs =>
let (vars, sub) := sub_mk_renames vs fvars in
if memvar v fvars
then let u := fresh_var (vars ++ fvars) in
(u :: vars, (v, vterm u) :: sub)
else (v :: vars, sub)
end.
Lemma sub_mk_renames_eta :
forall vs frees,
sub_mk_renames vs frees
= (fst (sub_mk_renames vs frees), snd (sub_mk_renames vs frees)).
Proof using.
induction vs; simpl; sp.
rw IHvs; simpl.
destruct (memvar a frees).
simpl; auto.
simpl; auto.
Qed.
Lemma sub_mk_renames2_eta :
forall vs frees lva,
sub_mk_renames2 vs frees lva
= (fst (sub_mk_renames2 vs frees lva), snd (sub_mk_renames2 vs frees lva)).
Proof using.
induction vs; simpl; sp.
rw IHvs; simpl.
destruct (memvar a frees).
simpl; auto.
simpl; auto.
Qed.
Lemma sub_mk_renames_snd_vterm :
forall bvars fvars v t,
LIn (v,t) (snd (sub_mk_renames bvars fvars))
-> {x : NVar $ t = vterm x}.
Proof using.
induction bvars; simpl; introv k; sp.
rw sub_mk_renames_eta in k; allsimpl.
destruct (memvar a fvars); allsimpl; sp; inj.
exists (fresh_var (fst (sub_mk_renames bvars fvars) ++ fvars)); auto.
discover; sp.
apply IHbvars in H. sp.
apply IHbvars in k. sp.
Qed.
Lemma sub_mk_renames2_snd_vterm :
forall bvars fvars v t lva,
LIn (v,t) (snd (sub_mk_renames2 bvars fvars lva))
-> {x : NVar $ t = vterm x}.
Proof using.
induction bvars; simpl; introv k; sp.
rw sub_mk_renames2_eta in k; allsimpl.
destruct (memvar a fvars); allsimpl; sp; inj.
eexists; eauto.
apply IHbvars in H. sp.
apply IHbvars in k. sp.
Qed.
Lemma sub_mk_renames2_nil :
forall vs lva,
sub_mk_renames2 vs [] lva = (vs, []).
Proof using.
induction vs; simpl; sp.
rw IHvs. sp.
Qed.
Lemma sub_mk_renames_nil :
forall vs,
sub_mk_renames vs [] = (vs, []).
Proof using.
induction vs; simpl; sp.
rw sub_mk_renames_eta.
rw IHvs; simpl; auto.
Qed.
Lemma sub_mk_renames_length :
forall vs frees,
length (fst (sub_mk_renames vs frees)) = length vs.
Proof using.
induction vs; simpl; sp.
rw sub_mk_renames_eta; simpl.
destruct (memvar a frees); simpl; rw IHvs; auto.
Qed.
Lemma sub_mk_renames2_length :
forall vs frees lva,
length (fst (sub_mk_renames2 vs frees lva)) = length vs.
Proof using.
induction vs; simpl; sp.
rw sub_mk_renames2_eta; simpl.
destruct (memvar a frees); simpl; rw IHvs; auto.
Qed.
Lemma in_snd_sub_mk_renames :
forall v t bvars fvars,
LIn (v, t) (snd (sub_mk_renames bvars fvars))
->
(
LIn v bvars
#
LIn v fvars
#
{x : NVar $ (t = vterm x # ! LIn x fvars)}
).
Proof using.
induction bvars; simpl; introv k; sp.
- rw sub_mk_renames_eta in k; allsimpl.
remember (memvar a fvars); destruct b; allsimpl; sp; inj; sp;
apply_in_hyp p; sp.
- rw sub_mk_renames_eta in k; allsimpl.
remember (memvar a fvars); destruct b; allsimpl; sp; inj; sp.
symmetry in Heqb.
rw fold_assert in Heqb.
rw @assert_memvar in Heqb; auto.
apply_in_hyp p; sp.
apply_in_hyp p; sp.
- rw sub_mk_renames_eta in k; allsimpl.
remember (memvar a fvars); destruct b; allsimpl; sp; inj; sp.
symmetry in Heqb.
rw fold_assert in Heqb.
rw @assert_memvar in Heqb; auto.
exists (fresh_var (fst (sub_mk_renames bvars fvars) ++ fvars)); sp.
assert (! (LIn (fresh_var (fst (sub_mk_renames bvars fvars) ++ fvars))
(fst (sub_mk_renames bvars fvars) ++ fvars))) as nin
by apply fresh_var_not_in.
apply nin.
rw in_app_iff; sp.
apply_in_hyp p; sp.
apply_in_hyp p; sp.
Qed.
Lemma sub_find_sub_filter :
forall sub vars n,
LIn n vars -> sub_find (sub_filter sub vars) n = None.
Proof using.
induction sub; simpl; sp.
remember (memvar a0 vars); destruct b; simpl; symmetry in Heqb.
apply_in_hyp p; sp.
remember (beq_var a0 n); destruct b.
apply beq_var_eq in Heqb0; subst.
rw not_of_assert in Heqb.
rw @assert_memvar in Heqb; sp.
apply_in_hyp p; sp.
Qed.
Definition disjoint_bv_sub (nt:NTerm) (sub: Substitution) :=
sub_range_sat sub (fun t => disjoint (free_vars t) (bound_vars nt)).
Theorem prog_sub_disjoint_bv_sub:
forall nt sub, prog_sub sub -> disjoint_bv_sub nt sub.
Proof using. intros nt. apply sub_range_sat_implies.
introv Hpr. invertsn Hpr.
rw Hpr. introv Hin. inverts Hin.
Qed.
Definition disjoint_bvbt_sub (bt:BTerm) (sub: Substitution) :=
sub_range_sat sub (fun t => disjoint (free_vars t) (bound_vars_bterm bt)).
(* Eval simpl in (ssubst (mk_lam nvarx (vterm nvary)) [(nvarz,vterm nvarx)]).
This was a bug in ssubst. it will return \lambda y.y because
the new variables were not disjoint from the fvars of the body
*)
(*
Lemma disjoint_bvbt_sub_ot : forall op lbt bt sub,
LIn bt lbt
-> disjoint_bv_sub (oterm op lbt) sub
-> disjoint_bvbt_sub bt sub.
AdCmitted.
Fixpoint ssubstd (t : NTerm) (sub : Substitution) (p: disjoint_bv_sub t sub): NTerm :=
(*if nullb sub then t else*)
match t with
| vterm var =>
match sub_find sub var with
| Some t => t
| None => t
end
| oterm op bts => let btsp := pairInProofs bts in
let f:= (fun ppp => match ppp with
| existT bt pp => ssubst_btermc bt sub _
(disjoint_bvbt_sub_ot _ _ _ _ pp p)
end) in
oterm op (map f bts)
end
with ssubst_btermc (bt : BTerm) (sub : Substitution) (p:disjoint_bvbt_sub bt sub): BTerm :=
match bt with
| bterm lv nt =>
bterm lv (ssubstc nt (sub_filter sub lv) _)
end.
*)
(** % \noindent \\* %
The following function is an auxilliary one that performs a
[Substitution] on an [NTerm] assuming that its bound
variables of are already disjoint from the free variables
of the range of the [Substitution].
*)
(*if nullb sub then t else*)
End SubstGeneric.
Ltac false_disjoint :=
match goal with
| [ H: !( disjoint _ _) |- _] => provefalse; apply H; clear H; disjoint_reasoningv
end.
(** clear_irr removes the duplicates of proofs of propositions that
* have proof irrelevance. *)
Ltac clear_irr :=
repeat match goal with
| [ H1 : cover_vars ?a ?b, H2 : cover_vars ?a ?b |- _ ] =>
assert (H2 = H1) by apply cover_vars_proof_irrelevance; subst
| [ H1 : cover_vars_upto ?a ?b ?c, H2 : cover_vars_upto ?a ?b ?c |- _ ] =>
assert (H2 = H1) by apply cover_vars_upto_proof_irrelevance; subst
| [ H1 : wf_term ?a, H2 : wf_term ?a |- _ ] =>
assert (H2 = H1) by apply wf_term_proof_irrelevance; subst