-
Notifications
You must be signed in to change notification settings - Fork 4
/
DeBruijn.v
1675 lines (1400 loc) · 54.3 KB
/
DeBruijn.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Set Implicit Arguments.
Generalizable All Variables.
Require Import Arith.
Require Import Lia.
Require Import Dblib.DblibTactics.
(* ---------------------------------------------------------------------------- *)
(* Terminology. *)
(* A variable, represented as a de Bruijn index, is [k]-free if it is greater
than or equal to [k]. It is [k]-bound otherwise. This terminology is useful
when one traverses a term and [k] represents the number of binders that have
been entered. *)
(* ---------------------------------------------------------------------------- *)
(* Operations provided by the client. *)
(* We expect the client to define a type [V] of values and a type [T] of terms.
These types may coincide, but need not coincide. The client provides a number
of operations on values and terms, described below, and the library provides
lifting and substitution operations. Lifting maps values to values and terms
to terms, while substitution allows substituting values for variables within
terms. *)
(* Our use of the terminology ``value'' and ``term'' is arbitrary. It is
inspired by the operational semantics of the call-by-value lambda-calculus.
The point is that [V] -- the type of the things that are substituted for
variables -- and [T] -- the type of the things within which substitution is
performed -- need not coincide. *)
(* Since values are substituted for variables, and since we need an identity
substitution, there follows that a variable must be a value, or, more
precisely, there must exist a function that maps variables to values. *)
Class Var (V : Type) := {
var:
nat -> V
}.
(* Let us immediately define [var] at variables. This may help clarify
things, and it is used in the statement of some laws below. *)
Instance Var_idx : Var nat := {
var x := x
}.
(* [traverse] can be thought of as a semantic substitution function. The idea
is, [traverse f l t] traverses the term [t], incrementing the index [l]
whenever a binder is entered, and, at every variable [x], it invokes [f l x].
This produces a value, which is grafted instead of [x]. *)
(* The definition of [traverse] is the main task that we require of the client.
Via the manner in which [l] is incremented, this function determines the
binding structure of terms. *)
Class Traverse (V T : Type) := {
traverse:
(nat -> nat -> V) -> (nat -> T -> T)
}.
(* [traverse_var] is a specialization of [traverse] to the case where [f]
maps variables to variables. *)
Notation traverse_var f := (traverse (fun l x => var (f l x))).
(* We expect the client to establish the following properties of [traverse]. *)
(* If [f] is injective, then [traverse_var f] is injective. *)
(* This reflects the fact that [traverse] preserves the structure of the term.
It acts only on variables. *)
Class TraverseVarInjective `{Var V, Traverse V T} := {
traverse_var_injective:
forall f,
(forall x1 x2 l, f l x1 = f l x2 -> x1 = x2) ->
forall t1 t2 l,
traverse_var f l t1 = traverse_var f l t2 ->
t1 = t2
}.
(* Two successive traversals can be fused. *)
(* This law is analogous to the law that describes the composition of two
syntactic substitutions -- see [SubstSubst] below. *)
Class TraverseFunctorial `{Traverse V V, Traverse V T} := {
traverse_functorial:
forall f g t l,
traverse g l (traverse f l t) = traverse (fun l x => traverse g l (f l x)) l t
}.
(* [traverse] is position-independent. That is, adding [p] to the argument
of [traverse] leads to adding [p] to the argument of every call to [f].
Furthermore, [traverse] is compatible with extensional equality. *)
Class TraverseRelative `{Traverse V T} := {
traverse_relative:
forall f g p t m l,
(forall l x, f (l + p) x = g l x) ->
m = l + p ->
traverse f m t =
traverse g l t
}.
(* [traverse] identifies [var] and simplifies to a call to [f]. *)
Class TraverseIdentifiesVar `{Var V, Traverse V V} := {
traverse_identifies_var:
forall f l x,
traverse f l (var x) = f l x
}.
(* The application of [traverse_var] to the identity is the identity. *)
Class TraverseVarIsIdentity `{Var V, Traverse V T} := {
traverse_var_is_identity:
forall f,
(forall l x, f l x = var x) ->
forall t l,
traverse f l t = t
}.
(* ---------------------------------------------------------------------------- *)
(* Operations provided by the library. *)
(* [lift w k t] is the term obtained by adding [w] to the [k]-free variables
of the term [t]. *)
Class Lift (T : Type) := {
lift:
nat -> nat -> T -> T
}.
(* Let us immediately define [lift] at variables. This may help clarify
things, and it is used in the statement of some laws below. *)
Instance Lift_idx : Lift nat := {
lift w k x :=
if le_gt_dec k x then w + x else x
}.
(* [shift k t] is an abbreviation for [lift 1 k t]. *)
(* From a programming language point of view, [shift] can be understood
as an end-of-scope construct. That is, if we are working in a context
where the variable [k] exists, then writing [shift k .] allows us to
go back to a context where this variable does not exist any more. In
this context, we can place a term [t] that makes sense in the absence
of this variable. Thus, [shift k t] can be read intuitively as [end k
in t], that is, end the scope of the variable [k] before interpreting
the term [t]. *)
Notation shift :=
(lift 1).
(* [subst v k t] is the term obtained by substituting the value [v] for
the variable [k] in the term [t]. *)
(* From a programming language point of view, [subst] can be understood as
a [let] construct. That is, [subst v k t] is in a certain sense equivalent
to [let k = v in t]. It is a binding construct for the variable [k], in the
sense that if [subst v k t] is used in a context where [k] does not exist,
then inside the sub-term [t], the variable [k] exists. *)
Class Subst (V T : Type) := {
subst:
V -> nat -> T -> T
}.
(* Let us immediately define [subst] at variables. This may help clarify
things, and it is used in the statement of some laws below. This definition
does not constitute an instance of the class [Subst] because we need a
heterogeneous version of substitution, one that maps variables to values,
as a basic building block. A homogeneous instance definition follows. *)
(* Note that [subst_idx] depends on [var]. *)
Definition subst_idx `{Var V} (v : V) (k x : nat) : V :=
match lt_eq_lt_dec x k with
| inleft (left _) => var x
| inleft (right _) => v
| inright _ => var (x - 1)
end.
Instance Subst_idx : Subst nat nat := {
subst := subst_idx
}.
(* The predicate [closed k t] means that the term [t] is [k]-closed, that is,
it has no [k]-free variables. In particular, if [k] is 0, then [t] is closed. *)
(* We defined [closed] in terms of [lift]: a term is closed if and only if it
is invariant under shifting (lifting by one). We show that this implies
that it is also invariant under arbitrary lifting and under substitution.
It is important to use a quantifier-free definition of closedness, as this
makes it much easier to write tactics that construct and destruct closedness
assertions. *)
Definition closed `{Lift T} k t :=
shift k t = t.
(* [rotate n] maps the variable [0] to the variable [n], maps [n] to [n-1], ...,
[1] to [0]. It preserves every variable above [n]. In particular, [rotate 1]
exchanges the variables [0] and [1]. *)
Definition rotate `{Var V, Lift T, Subst V T} (n : nat) (t : T) : T :=
subst (var n) 0 (shift (S n) t).
(* ---------------------------------------------------------------------------- *)
(* Properties of these operations. *)
(* [lift] commutes with [var]. *)
(* This law alone is probably not of great interest to the client, who needs a
more general way of simplifying the application of [lift] to a constructor.
We name this property because it is used by some of our lemmas. *)
Class LiftVar `{Var A, Lift A} := {
lift_var:
forall w k x,
lift w k (var x) = var (lift w k x)
}.
(* [lift 0] is the identity. *)
Class LiftZero `{Lift T} := {
lift_zero:
forall k t,
lift 0 k t = t
}.
(* [lift] is injective. *)
Class LiftInjective `{Lift T} := {
lift_injective:
forall w k t1 t2,
lift w k t1 = lift w k t2 ->
t1 = t2
}.
Ltac lift_injective :=
match goal with h: lift ?w ?k ?t1 = lift ?w ?k ?t2 |- _ =>
generalize (lift_injective _ _ _ _ h); clear h; intro h; try (subst t1)
end.
(* [lift] commutes with itself. *)
(* In the special case where [wk] and [ws] are 1, the law becomes:
[k <= s -> shift k (shift s t) = shift (1 + s) (shift k t)].
Since [k] is the smaller index, it is always called [k], regardless
of whether [s] is or is not in scope. Conversely, since [s] is the
larger index, it is called [s] when [k] is not in scope, but it is
called [1 + s] when [k] is in scope. The law expresses the fact
that ending the scope of [k] first, then the scope of [s], is the
same as ending the scope of [1 + s], then the scope of [k]. It is
a commutation law with a slight technical adjustment. *)
(* Roughly speaking, [end k in end s in t] is equivalent to [end s in
end k in t]. *)
Class LiftLift `{Lift T} := {
lift_lift:
forall t k s wk ws,
k <= s ->
lift wk k (lift ws s t) = lift ws (wk + s) (lift wk k t)
}.
(* One might wonder whether it is sufficient for this law to consider
the case where [k <= s] holds. What happens in the other cases?
Well, one can check that if [k >= s + ws] holds, then the law
applies in the other direction. The following lemma proves this. *)
Lemma lift_lift_reversed:
forall `{Lift T},
LiftLift ->
forall wk k ws s t,
k >= s + ws ->
lift wk k (lift ws s t) = lift ws s (lift wk (k - ws) t).
Proof.
intros.
replace k with (ws + (k - ws)) by lia.
erewrite <- lift_lift by lia.
replace (ws + (k - ws) - ws) with (k - ws) by lia.
reflexivity.
Qed.
(* If [ws] is 1, then the two cases considered above, namely [k <= s]
and [k >= s + ws], are exhaustive. Now, what happens when [k] is
comprised between [s] and [s + ws]? The following law answers this
question. This law also covers the extreme cases where [k = s] and
[k = s + ws], which may seem surprising, since they are already
covered by [lift_lift]. The law below states that, in this case,
the two [lift] operations can be combined into a single one. Note
that the exact value of [k] is irrelevant as long as it lies within
this interval. *)
Class LiftLiftFuse `{Lift T} := {
lift_lift_fuse:
forall t k s wk ws,
s <= k <= s + ws ->
lift wk k (lift ws s t) = lift (wk + ws) s t
}.
(* [subst] identifies [var] and simplifies to [subst_idx]. *)
(* This law alone is probably not of great interest to the client, who needs a
more general way of simplifying the application of [subst] to a constructor.
We name this property because it is used by some of our lemmas. *)
Class SubstVar `{Var A, Subst A A} := {
subst_var:
forall a k x,
subst a k (var x) = subst_idx a k x
}.
(* A substitution for the variable [k] vanishes when it reaches an
end-of-scope marker for the variable [k]. This is expressed by
the following law. *)
Class SubstLift `{Lift T, Subst V T} := {
subst_lift:
forall v k t,
subst v k (shift k t) =
t
}.
(* [lift] and [subst] commute. *)
(* Roughly speaking, [end k in let s = v in t] is equivalent to [let s = (end
k in v) in (end k in t)]. At least, that is the idea. The ``technical
adjustments'' make it quite difficult to understand what is going on in
detail. *)
(* Below, we propose two laws, which cover the cases [k <= s] and [s <= k]. It
may seem somewhat troubling that these cases are not disjoint! When [k = s],
the two right-hand sides differ in a certain manner that is described by the
law [PunPun] shown further on. *)
(* We also offer a general law, which unifies these two laws, and arbitrarily
gives preference to the second law in the case where [k = s]. It is not
clear whether this complex general law is of any use in practice. *)
Class LiftSubst1 `{Lift V, Lift T, Subst V T} := {
lift_subst_1:
forall t k v s w,
k <= s ->
lift w k (subst v s t) =
subst (lift w k v) (w + s) (lift w k t)
}.
Class LiftSubst2 `{Lift V, Lift T, Subst V T} := {
lift_subst_2:
forall t k v s w,
s <= k ->
lift w k (subst v s t) =
subst (lift w k v) s (lift w (1 + k) t)
}.
Class LiftSubst `{Lift V, Lift T, Subst V T} := {
lift_subst:
forall t k v s w,
lift w k (subst v s t) =
subst (lift w k v) (lift w (1 + k) s) (lift w (shift s k) t)
}.
(* [subst] commutes with itself. *)
(* Roughly speaking, [let s = v in let k = w in t] is equivalent to
[let k = (let s = v in w) in let s = v in t], again with ``technical
adjustments''. *)
(* This law covers only the case where [k <= s]. I am not sure what can
be said about the other case. Of course the law can be read from right
to left, and this says something about the case where [k >= 1 + s], but
only if the values [v] and [w] are of a specific form. Maybe there is
nothing more to be said. *)
Class SubstSubst `{Lift V, Subst V V, Subst V T} := {
subst_subst:
forall t k v s w,
k <= s ->
subst v s (subst w k t) =
subst (subst v s w) k (subst (shift k v) (1 + s) t)
}.
(* Substituting [x] for [x] is the identity. This is known as a pun. *)
(* In a de Bruijn setting, it does not even make sense to speak of replacing
a variable with itself, because in the term [subst v k t], the value [v]
and the variable [k] inhabit different spaces. *)
(* Instead, we can write [let S k = k in end k in t]. This introduces [S k] as
an alias for [k], then forgets about [k], so [S k] is renumbered and becomes
[k] again. Or we can write [let k = k in end S k in t]. This introduces [k]
as an alias for the old [k], which gets renumbered and becomes [S k], then
forgets about [S k]. In each of these two cases, we introduce a new variable
and forget about the old one; the apparent difference lies in how we number
the new variable, but this makes no deep difference. We give the variants
of the law for puns, plus a law that helps recognize these two variants as
equal. *)
Class Pun1 `{Var V, Lift T, Subst V T} := {
pun_1:
forall t k,
subst (var k) (S k) (shift k t) = t
}.
Class Pun2 `{Var V, Lift T, Subst V T} := {
pun_2:
forall t k,
subst (var k) k (shift (S k) t) = t
}.
Class PunPun `{Var V, Lift T, Subst V T} := {
pun_pun:
forall v w k t,
subst v (w + k) (lift w k t) =
subst v k (lift w (1 + k) t)
}.
(* [rotate 1] exchanges the variables [0] and [1], so it is own inverse. *)
Class Rotate1SelfInverse `{Var V, Lift T, Subst V T} := {
rotate1_self_inverse:
forall t,
rotate 1 (rotate 1 t) = t
}.
(* ---------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------- *)
(* A few consequences of the properties that the user proves about [traverse]. *)
(* We could present these properties in the form of classes, as opposed to lemmas.
This would be perhaps slightly more convenient but also more confusing. It is
seems preferable to work directly with the basic classes that the user knows
about. *)
(* [traverse] is compatible with extensional equality. *)
Lemma traverse_extensional:
forall `{Traverse V T},
TraverseRelative ->
forall f g,
(forall l x, f l x = g l x) ->
forall t l,
traverse f l t = traverse g l t.
Proof.
intros.
eapply traverse_relative with (p := 0).
intros m ?. replace (m + 0) with m by lia. eauto.
lia.
Qed.
(* A composition of [traverse] with [traverse_var] simplifies to a single call
to [traverse]. In other words, composing a substitution and a (not
necessarily bijective) renaming yields another substitution. *)
Lemma traverse_traverse_var:
forall `{Var V, Traverse V V, Traverse V T},
@TraverseFunctorial V _ T _ ->
@TraverseRelative V T _ -> (* TraverseExtensional *)
@TraverseIdentifiesVar V _ _ ->
forall f g t l,
traverse g l (traverse_var f l t) =
traverse (fun l x => g l (f l x)) l t.
Proof.
intros.
rewrite traverse_functorial.
eapply (traverse_extensional _).
eauto using traverse_identifies_var.
Qed.
(* ---------------------------------------------------------------------------- *)
(* This internal tactic helps prove goals by examination of all cases. *)
Ltac just_do_it :=
unfold subst, Subst_idx, subst_idx, lift, Lift_idx, var, Var_idx;
intros;
dblib_by_cases; eauto with lia.
(* The following two lemmas re-state the definition of [lift] at type [nat]. *)
Lemma lift_idx_recent:
forall w k x,
k > x ->
lift w k x = x.
Proof.
just_do_it.
Qed.
Lemma lift_idx_old:
forall w k x,
k <= x ->
lift w k x = w + x.
Proof.
just_do_it.
Qed.
(* This tactic applies whichever of the above two lemmas is applicable. *)
Create HintDb lift_idx_hints.
(* more hints to be added later on into this database *)
Ltac lift_idx :=
first [ rewrite @lift_idx_recent by solve [ lia | eauto with lift_idx_hints ]
| rewrite @lift_idx_old by lia ].
Hint Extern 1 => lift_idx : lift_idx.
Ltac lift_idx_in h :=
first [ rewrite @lift_idx_recent in h by solve [ lia | eauto with lift_idx_hints ]
| rewrite @lift_idx_old in h by lia ].
Ltac lift_idx_all :=
first [ rewrite @lift_idx_recent in * by solve [ lia | eauto with lift_idx_hints ]
| rewrite @lift_idx_old in * by lia ].
(* This tactic finds an occurrence of [lift _ _ _] at type [nat] and
examines both cases. *)
Ltac destruct_lift_idx :=
match goal with |- context[@lift nat _ _ ?y ?x] =>
destruct (le_gt_dec y x); lift_idx
end.
(* Our definition of [lift] at type [nat] satisfies the properties listed above. *)
Instance LiftVar_idx: @LiftVar nat _ _.
Proof. constructor. just_do_it. Qed.
Instance LiftZero_idx: @LiftZero nat _.
Proof. constructor. just_do_it. Qed.
Instance LiftInjective_idx: @LiftInjective nat _.
Proof. constructor. just_do_it. Qed.
Instance LiftLift_idx: @LiftLift nat _.
Proof. constructor. just_do_it. Qed.
Instance LiftLiftFuse_idx: @LiftLiftFuse nat _.
Proof. constructor. just_do_it. Qed.
(* We could also state and prove that our definition of [subst] at type [nat]
satisfies the properties listed above. However, this would not be very
interesting, because we need more general statements about [subst_idx],
which is heterogeneous: the type [V] is not [nat]. We are able to get away
without making these more general statements explicit: they appear inside
some proofs and we prove them on the fly. *)
(* The following three lemmas re-state the definition of [subst_idx]. *)
Lemma subst_idx_miss_1:
forall `{Var V} v k x,
k > x ->
subst_idx v k x = var x.
Proof.
just_do_it.
Qed.
Lemma subst_idx_identity:
forall `{Var V} v k x,
k = x ->
subst_idx v k x = v.
Proof.
just_do_it.
Qed.
Lemma subst_idx_miss_2:
forall `{Var V} v k x,
k < x ->
subst_idx v k x = var (x - 1).
Proof.
just_do_it.
Qed.
(* This tactic applies whichever of the above three lemmas is applicable. *)
Ltac subst_idx :=
first [
rewrite @subst_idx_identity by lia
| rewrite @subst_idx_miss_1 by lia
| rewrite @subst_idx_miss_2 by lia
].
Ltac subst_idx_in h :=
first [
rewrite @subst_idx_identity in h by lia
| rewrite @subst_idx_miss_1 in h by lia
| rewrite @subst_idx_miss_2 in h by lia
].
Ltac subst_idx_all :=
first [
rewrite @subst_idx_identity in * by lia
| rewrite @subst_idx_miss_1 in * by lia
| rewrite @subst_idx_miss_2 in * by lia
].
(* A little lemma: replacing a variable with a variable always yields a
variable. *)
Lemma subst_idx_var:
forall `{Var V},
forall v k x,
subst_idx (var v) k x =
var (subst_idx v k x).
Proof.
just_do_it.
Qed.
(* ---------------------------------------------------------------------------- *)
(* This is our generic definition of [lift] in terms of [traverse]. *)
Instance Lift_Traverse `{Var V, Traverse V T} : Lift T := {
lift w k t :=
traverse (fun l x => var (lift w (l + k) x)) 0 t
}.
(* This lemma repeats the definition (!) and is useful when rewriting. *)
Lemma expand_lift:
forall `{Var V, Traverse V T},
forall w k t,
lift w k t =
traverse (fun l x => var (lift w (l + k) x)) 0 t.
Proof.
reflexivity.
Qed.
(* This auxiliary tactic simplifies expressions of the form [x + 0] in
the goal. It does *not* affect [x + ?y] where [y] is a
meta-variable. *)
Ltac plus_0_r :=
repeat match goal with |- context[?x + 0] => rewrite (plus_0_r x) end.
Ltac plus_0_r_in h :=
repeat match type of h with context[?x + 0] => rewrite (plus_0_r x) in h end.
(* It is worth noting that instead of writing:
traverse (fun l x => var (lift w (l + k) x)) 0 t
one might have instead written:
traverse (fun l x => var (lift w l x)) k t
Indeed, because [traverse] is relative, these two expressions are
equivalent. The following lemma states this fact. When used as a
rewriting rule, it recognizes a general form of the above expressions
and replaces it with an application of [lift]. *)
Lemma recognize_lift:
forall `{Var V, Traverse V T},
TraverseRelative ->
forall w k1 k2 t,
forall traverse_,
traverse_ = traverse -> (* helps rewrite *)
traverse_ (fun l x => var (lift w (l + k2) x)) k1 t =
lift w (k1 + k2) t.
Proof.
intros. subst. simpl.
eapply traverse_relative; [ | instantiate (1 := k1); lia ].
just_do_it.
Qed.
(* This tactic recognizes an application of the user's [traverse_term]
function that is really a [lift] operation, and folds it. *)
Ltac recognize_lift :=
rewrite recognize_lift by eauto with typeclass_instances;
repeat rewrite plus_0_l. (* useful when [k1] is zero *)
Ltac recognize_lift_in h :=
rewrite recognize_lift in h by eauto with typeclass_instances;
repeat rewrite plus_0_l in h. (* useful when [k1] is zero *)
(* The tactic [simpl_lift] is used to simplify an application of [lift] to a
concrete term, such as [TApp t ...], where [TApp] is a user-defined
constructor of the user-defined type of [term]s. We assume that the user
has manually rewritten one or several occurrences of [lift] using the lemma
[expand_lift], so as to indicate where simplification is desired. Thus, one
or several instances of [traverse] must be visible in a hypothesis or in the
goal. We further assume that [simpl (@traverse _ _ _)] has been used so as to
replace the generic [traverse] with user-defined [_traverse] functions. We
start there and do the rest of the work. *)
(* A difficulty arises when the goal or hypothesis contains instances of
[lift] at multiple types. In that case, after expanding [traverse], we find
multiple user-defined [_traverse] functions, each of which we must deal
with in turn. After dealing with one specific [_traverse] function, we use
[recognize_lift] at a specific type in order to eliminate all occurrences
of this particular [_traverse] function. We are then able to iterate this
process without looping. *)
Ltac simpl_lift :=
match goal with
(* Case: [_traverse] appears in the goal. *)
(* this binds the meta-variable [_traverse] to the user's [traverse_term] *)
|- context[?_traverse (fun l x : nat => var (lift ?w (l + ?k) x)) _ _] =>
(* this causes the reduction of the fixpoint: *)
unfold _traverse; fold _traverse;
(* we now have a term of the form [TApp (traverse_term ...) ...].
There remains to recognize the definition of [lift]. *)
plus_0_r; (* useful when we have traversed a binder: 1 + 0 is 1 *)
(* use [recognize_lift] at the specific type of the [_traverse] function
that we have just simplified *)
match type of _traverse with (nat -> nat -> ?V) -> nat -> ?T -> ?T =>
repeat rewrite (@recognize_lift V _ T _ _) by eauto with typeclass_instances
end;
repeat rewrite plus_0_l (* useful when [k1] is zero and we are at a leaf *)
(* Case: [_traverse] appears in a hypothesis. *)
(* this binds the meta-variable [_traverse] to the user's [traverse_term] *)
| h: context[?_traverse (fun l x : nat => var (lift ?w (l + ?k) x)) _ _] |- _ =>
(* this causes the reduction of the fixpoint: *)
unfold _traverse in h; fold _traverse in h;
(* we now have a term of the form [TApp (traverse_term ...) ...].
There remains to recognize the definition of [lift]. *)
plus_0_r_in h; (* useful when we have traversed a binder: 1 + 0 is 1 *)
(* use [recognize_lift] at the specific type of the [_traverse] function
that we have just simplified *)
match type of _traverse with (nat -> nat -> ?V) -> nat -> ?T -> ?T =>
repeat rewrite (@recognize_lift V _ T _ _) in h by eauto with typeclass_instances
end;
repeat rewrite plus_0_l in h (* useful when [k1] is zero and we are at a leaf *)
end.
(* This tactic attempts to all occurrences of [lift] in the goal. *)
Ltac simpl_lift_goal :=
(* this replaces [lift] with applications of [traverse] *)
repeat rewrite @expand_lift;
(* this replaces the generic [traverse] with the user's [_traverse] functions *)
simpl (@traverse _ _ _);
(* this simplifies applications of each [_traverse] function and folds them back *)
repeat simpl_lift;
(* if we have exposed applications of [lift_idx], try simplifying them away *)
repeat lift_idx;
(* if this exposes uses of [var], replace them with the user's [TVar] constructor *)
simpl var.
Hint Extern 1 (lift _ _ _ = _) => simpl_lift_goal : simpl_lift_goal.
Hint Extern 1 (_ = lift _ _ _) => simpl_lift_goal : simpl_lift_goal.
(* This tactic attempts to simplify all occurrences of [lift] in the goal
and the hypotheses. *)
Ltac simpl_lift_all :=
repeat rewrite @expand_lift in *;
simpl (@traverse _ _ _) in *;
repeat simpl_lift;
repeat lift_idx_all;
simpl var in *.
(* This tactic attempts to simplify all occurrences of [lift] in a specific
hypothesis. *)
Ltac simpl_lift_in h :=
repeat rewrite @expand_lift in h;
simpl (@traverse _ _ _) in h;
repeat simpl_lift;
repeat lift_idx_in h;
simpl var in h.
(* ---------------------------------------------------------------------------- *)
(* Our definition of [lift] in terms of [traverse] satisfies all of the desired
properties. *)
Instance LiftVar_Traverse:
forall `{Var V, Traverse V V},
TraverseIdentifiesVar ->
@LiftVar V _ _.
Proof.
constructor. unfold lift, Lift_Traverse. intros.
rewrite traverse_identifies_var. reflexivity.
Qed.
Instance LiftZero_Traverse:
forall `{Var V, Traverse V V},
TraverseVarIsIdentity ->
@LiftZero V _.
Proof.
constructor. intros.
unfold lift, Lift_Traverse.
rewrite traverse_var_is_identity. reflexivity. intros.
rewrite lift_zero. reflexivity.
Qed.
Instance LiftInjective_Traverse:
forall `{Var V, Traverse V T},
TraverseVarInjective ->
@LiftInjective T _.
Proof.
constructor. unfold lift, Lift_Traverse. intros w k. intros.
eapply traverse_var_injective with (f := fun l x => lift w (l + k) x).
eauto using lift_injective.
eassumption.
Qed.
Instance LiftLift_Traverse:
forall `{Var V, Traverse V V, Traverse V T},
@TraverseFunctorial V _ T _ ->
@TraverseRelative V T _ -> (* TraverseExtensional *)
@TraverseIdentifiesVar V _ _ ->
@LiftLift T _.
Proof.
constructor. unfold lift, Lift_Traverse. intros.
rewrite (traverse_traverse_var _ _ _).
rewrite (traverse_traverse_var _ _ _).
eapply (traverse_extensional _).
intros. f_equal.
rewrite lift_lift by lia.
f_equal. lia.
Qed.
Instance LiftLiftFuse_Traverse:
forall `{Var V, Traverse V V, Traverse V T},
@TraverseFunctorial V _ T _ ->
@TraverseRelative V T _ -> (* TraverseExtensional *)
@TraverseIdentifiesVar V _ _ ->
@LiftLiftFuse T _.
Proof.
constructor. unfold lift, Lift_Traverse. intros.
rewrite (traverse_traverse_var _ _ _).
eapply (traverse_extensional _).
intros. f_equal.
rewrite lift_lift_fuse by lia. reflexivity.
Qed.
(* ---------------------------------------------------------------------------- *)
(* This is our generic definition of [subst] in terms of [traverse]. *)
Instance Subst_Traverse `{Var V, Traverse V V, Traverse V T} : Subst V T := {
subst v k t :=
traverse (fun l x => subst_idx (lift l 0 v) (l + k) x) 0 t
}.
(* This lemma repeats the definition (!) and is useful when rewriting. *)
Lemma expand_subst:
forall `{Var V, Traverse V V, Traverse V T},
forall v k t,
subst v k t =
traverse (fun l x => subst_idx (lift l 0 v) (l + k) x) 0 t.
Proof.
reflexivity.
Qed.
(* Again, because [traverse] is relative, there can be several ways of
writing a substitution. The following lemma states this. When used
as a rewriting rule, it helps recognize applications of [subst]. *)
Lemma recognize_subst:
forall `{Var V, Traverse V V, Traverse V T},
@TraverseFunctorial V _ V _ ->
@TraverseIdentifiesVar V _ _ ->
@TraverseRelative V V _ ->
@TraverseRelative V T _ ->
forall traverse_,
traverse_ = traverse -> (* helps rewrite *)
forall v k2 k1 t,
traverse_ (fun l x => subst_idx (lift l 0 v) (l + k2) x) k1 t =
subst (lift k1 0 v) (k1 + k2) t.
Proof.
intros. subst.
unfold subst, Subst_Traverse.
eapply traverse_relative; [ | instantiate (1 := k1); lia ].
intros.
f_equal.
rewrite lift_lift_fuse by lia. reflexivity.
lia.
Qed.
(* This tactic recognizes an application of the user's [traverse_term]
function that is really a [subst] operation, and folds it. *)
Ltac recognize_subst :=
rewrite recognize_subst by eauto with typeclass_instances;
try rewrite lift_zero; (* useful when [k1] is zero *)
repeat rewrite plus_0_l. (* useful when [k1] is zero *)
Ltac recognize_subst_in h :=
rewrite recognize_subst in h by eauto with typeclass_instances;
try rewrite lift_zero in h; (* useful when [k1] is zero *)
repeat rewrite plus_0_l in h. (* useful when [k1] is zero *)
(* This tactic is used to simplify an application of [subst] to a concrete
term, such as [TApp t1 t2], where [TApp] is a user-defined constructor of
the user-defined type of [term]s. We assume that the user has manually
rewritten one occurrence of [subst] using the lemma [expand_subst], so as to
indicate where simplification is desired. Thus, one instance of [traverse]
must be visible in a hypothesis or in the goal. We start there and do the
rest of the work. *)
Ltac simpl_subst :=
match goal with
(* Case: [_traverse] appears in the goal. *)
(* this binds the meta-variable [_traverse] to the user's [traverse_term] *)
|- context[?_traverse (fun l x : nat => subst_idx (lift l 0 ?v) (l + ?k) x) _ _] =>
(* this causes the reduction of the fixpoint: *)
unfold _traverse; fold _traverse;
(* we now have a term of the form [TApp (traverse_term ...) (traverse_term ...)].
There remains to recognize the definition of [subst]. *)
plus_0_r; (* useful when we have traversed a binder: 1 + 0 is 1 *)
(* use [recognize_subst] at the specific type of the [_traverse] function
that we have just simplified *)
match type of _traverse with (nat -> nat -> ?V) -> nat -> ?T -> ?T =>
repeat rewrite (@recognize_subst V _ _ T _ _ _ _ _) by eauto with typeclass_instances
end;
repeat rewrite plus_0_l; (* useful when [k1] is zero and we are at a leaf *)
repeat rewrite lift_zero (* useful when [k1] is zero and we are at a leaf *)
(* Case: [_traverse] appears in a hypothesis. *)
(* this binds the meta-variable [_traverse] to the user's [traverse_term] *)
| h: context[?_traverse (fun l x : nat => subst_idx (lift l 0 ?v) (l + ?k) x) _ _] |- _ =>
(* this causes the reduction of the fixpoint: *)
unfold _traverse in h; fold _traverse in h;
(* we now have a term of the form [TApp (traverse_term ...) (traverse_term ...)].
There remains to recognize the definition of [subst]. *)
plus_0_r_in h; (* useful when we have traversed a binder: 1 + 0 is 1 *)
(* use [recognize_subst] at the specific type of the [_traverse] function
that we have just simplified *)
match type of _traverse with (nat -> nat -> ?V) -> nat -> ?T -> ?T =>
repeat rewrite (@recognize_subst V _ _ T _ _ _ _ _) in h by eauto with typeclass_instances
end;
repeat rewrite plus_0_l in h; (* useful when [k1] is zero and we are at a leaf *)
repeat rewrite lift_zero in h (* useful when [k1] is zero and we are at a leaf *)
end.
(* This tactic attempts to simplify all occurrences of [subst] in the goal. *)
Ltac simpl_subst_goal :=
(* this replaces [subst] with applications of [traverse] *)
repeat rewrite @expand_subst;
(* this replaces the generic [traverse] with the user's [_traverse] functions *)
simpl (@traverse _ _ _);
(* this simplifies applications of each [_traverse] function and folds them back *)
repeat simpl_subst;
(* if we have exposed applications of [subst_idx], try simplifying them away *)
repeat subst_idx;
(* if this exposes uses of [var], replace them with the user's [TVar] constructor *)
simpl var.
Hint Extern 1 (subst _ _ _ = _) => simpl_subst_goal : simpl_subst_goal.
Hint Extern 1 (_ = subst _ _ _) => simpl_subst_goal : simpl_subst_goal.
(* This tactic attempts to simplify all occurrences of [subst] in the goal
and the hypotheses. *)
Ltac simpl_subst_all :=
repeat rewrite @expand_subst in *;
simpl (@traverse _ _ _) in *;
repeat simpl_subst;
repeat subst_idx_all;
simpl var in *.
(* This tactic attempts to simplify all occurrences of [subst] in a specific
hypothesis. *)
Ltac simpl_subst_in h :=
repeat rewrite @expand_subst in h;
simpl (@traverse _ _ _) in h;
repeat simpl_subst;
repeat subst_idx_in h;
simpl var in h.
(* ---------------------------------------------------------------------------- *)
(* Our definition of [subst] in terms of [traverse] satisfies all of the desired
properties. *)
Instance SubstVar_Traverse:
forall `{Var V, Traverse V V},
TraverseIdentifiesVar ->
TraverseVarIsIdentity ->
SubstVar.
Proof.
constructor. unfold subst, Subst_Traverse. intros.
rewrite traverse_identifies_var.
rewrite lift_zero.
reflexivity.
Qed.
Instance SubstLift_Traverse:
forall `{Var V, Traverse V V, Traverse V T},
@TraverseFunctorial V _ T _ ->
@TraverseIdentifiesVar V _ _ ->
@TraverseVarIsIdentity V _ T _ ->
@SubstLift T _ V _.