forked from plclub/metalib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssocList.v
1227 lines (1006 loc) · 34 KB
/
AssocList.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
(* This file is distributed under the terms of the MIT License, also
known as the X11 Licence. A copy of this license is in the README
file that accompanied the original distribution of this file.
Based on code written by:
Brian Aydemir
Aaron Bohannon
Arthur Charg\'eraud
With contributions from:
Edsko de Vries:
uniq_reorder_1, uniq_reorder_2, binds_In_inv *)
(** remove printing ~ *)
(** A library for association lists, i.e., lists of pairs *)
Require Import Coq.FSets.FSets.
Require Import Coq.Lists.List.
Require Import Coq.Logic.Decidable.
Require Import CoqFSetDecide.
Require Import CoqListFacts.
Require Import LibTactics.
(* *********************************************************************** *)
(** * Implementation *)
(** We implement association lists as lists of key-value pairs. We
use [eq] as the equality on both keys and values. This is evident
primarily in the definitions of [binds] and [maps].
(Implementation note: We can generalize the implementation to
arbitrary equivalence relations. For now, we have chosen not to,
in order to keep the implementation simple and to optimize for
what we expect is the common case.)
Note that our library has an implicit convention for the "normal
form" of an association list. This normal form requires that a
list be built only from [nil] (the empty list), [one] (the
singleton list), and [app] (concatenation). Additionally,
concatenations should be associated to the right and the list
should not contain any [nil] terms, unless it is the empty list
itself.
This allows association lists to be written in a slightly more
uniform manner when compared to using both [cons] and [app] to
build them. The downsides are that Coq's [simpl] tactic will
simplify instances of [one] down to [cons] and that there are
instances in which one needs to write association lists that are
not in normal form (e.g., some concatenations will need to be
associated to the left). The [simpl_alist] and [rewrite_alist]
tactics below are designed to minimize the impact of these
downsides.
A [Strategy] declaration can be used to prevent Coq's tactics from
simplifying [one], if one so desires. *)
(* Implementation note (BEA, XXX): Very little support for
- Inversion on association lists when [one] is [opaque].
- No real support for [get].
- No real support for [maps].
- No support for permutations. *)
Module Make
(X : UsualDecidableType)
(Import KeySet : FSetInterface.WSfun X).
Module Import D := CoqFSetDecide.WDecide_fun X KeySet.
Module KeySetProperties := FSetProperties.WProperties_fun X KeySet.
Module KeySetFacts := FSetFacts.WFacts_fun X KeySet.
(* *********************************************************************** *)
(** * Basic definitions *)
(** Implicit arguments are enabled for the following definitions. *)
Set Implicit Arguments.
(** [one] constructs an association list consisting of exactly one
binding. We define an infix notation for it and ensure that the
arguments to [app] are interpreted in the right scope, i.e.,
[list_scope].
Implementation note: The level associated with the notation gives
it a higher precedence than the "++" notation for [app]. This
should eliminate the need for parentheses. See the definition
below of [uniq], for example. *)
Definition one (C : Type) (item : C) : list C := cons item nil.
Notation "x ~ a" := (one (x, a)) (at level 50) : list_scope.
Arguments Scope app [ type_scope list_scope list_scope ].
Open Scope list_scope.
(** [dom] computes the domain of an association list, i.e., the
set consisting of its keys. *)
Fixpoint dom
(C : Type) (E : list (X.t*C))
: KeySet.t :=
match E with
| nil => empty
| (x, _) :: E' => add x (dom E')
end.
(** [get] looks up a key in an association list. *)
Fixpoint get
(C : Type) (x : X.t) (E : list (X.t*C))
: option C :=
match E with
| nil => None
| (y, c) :: F => if X.eq_dec x y then Some c else get x F
end.
(** [binds] is a ternary predicate that holds when a key-value pair
appears somewhere in the given association list. *)
Definition binds
(A : Type) (x : X.t) (a : A) (E : list (X.t*A))
: Prop :=
List.In (x, a) E.
(** [maps] is a ternary predicate that holds when the first binding in
the list for the given key is to the given value. *)
Definition maps
(A : Type) (x : X.t) (a : A) (E : list (X.t*A))
: Prop :=
get x E = Some a.
(** [disjoint] is a binary predicate that holds when the domains of
two association lists are disjoint. *)
Definition disjoint
(A B : Type) (E : list (X.t*A)) (F : list (X.t*B))
: Prop :=
inter (dom E) (dom F) [<=] empty.
(** [map] applies a function to each of the values in an association
list. *)
Definition map
(A B : Type) (f : A -> B) (E : list (X.t*A))
: list (X.t*B) :=
List.map (fun b => match b with (x, a) => (x, f a) end) E.
(** [uniq] is unary predicate that holds if and only if each key is
bound at most once in the given association list. Note that
[uniq] is defined in terms of [one], not [cons]. *)
Inductive uniq (A : Type) : list (X.t*A) -> Prop :=
| uniq_nil :
uniq nil
| uniq_push : forall x a E,
uniq E ->
~ In x (dom E) ->
uniq (x ~ a ++ E).
(** Unless stated otherwise, in the remainder of this file, implicit
arguments will not be declared by default. *)
Unset Implicit Arguments.
(* *********************************************************************** *)
(** * List properties *)
(** The following properties are used mainly for rewriting association
lists into the normal form described above. See the [simpl_alist]
and [rewrite_alist] tactics below. *)
Section ListProperties.
Variable X : Type.
Variables x y : X.
Variables l l1 l2 l3 : list X.
Lemma cons_app_one :
cons x l = one x ++ l.
Proof. clear. reflexivity. Qed.
Lemma cons_app_assoc :
(cons x l1) ++ l2 = cons x (l1 ++ l2).
Proof. clear. reflexivity. Qed.
Lemma app_assoc :
(l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3).
Proof. clear. auto with datatypes. Qed.
Lemma app_nil_1 :
nil ++ l = l.
Proof. clear. reflexivity. Qed.
Lemma app_nil_2 :
l ++ nil = l.
Proof. clear. auto with datatypes. Qed.
Lemma in_nil_iff :
List.In x nil <-> False.
Proof. clear. split; inversion 1. Qed.
Lemma in_one_iff :
List.In x (one y) <-> x = y.
Proof.
clear. split.
inversion 1 as [ | HIn]; intuition. inversion HIn.
constructor; intuition.
Qed.
Lemma in_app_iff :
List.In x (l1 ++ l2) <-> List.In x l1 \/ List.In x l2.
Proof. clear. split; auto using List.in_or_app, List.in_app_or. Qed.
End ListProperties.
(* *********************************************************************** *)
(** * Properties of [map] and [dom] *)
(** The following lemmas are used mainly to simplify applications of
[map] and [dom] to association lists. See also the [simpl_alist]
and [rewrite_alist] tactics below. *)
Section Properties.
Variables A B key : Type.
Variable f : A -> B.
Variable x : X.t.
Variable b : A.
Variables E F G : list (X.t*A).
Lemma map_nil :
map f (@nil (X.t*A)) = nil.
Proof. clear. reflexivity. Qed.
Lemma map_one :
map f (x ~ b) = (x ~ f b).
Proof. clear. reflexivity. Qed.
Lemma map_cons :
map f ((x, b) :: E) = x ~ f b ++ map f E.
Proof. clear. reflexivity. Qed.
Lemma map_app :
map f (E ++ F) = map f E ++ map f F.
Proof. clear. unfold map. rewrite List.map_app. reflexivity. Qed.
Lemma dom_nil :
dom (@nil (X.t*A)) = empty.
Proof. clear. reflexivity. Qed.
Lemma dom_one :
dom (x ~ b) [=] singleton x.
Proof. clear. intros. simpl. fsetdec. Qed.
Lemma dom_cons :
dom ((x, b) :: E) [=] union (singleton x) (dom E).
Proof. clear. intros. simpl. fsetdec. Qed.
Lemma dom_app :
dom (E ++ F) [=] union (dom E) (dom F).
Proof. clear. intros. induction E as [ | [? ?] ]; simpl; fsetdec. Qed.
Lemma dom_map :
dom (map f E) [=] dom E.
Proof. clear. intros. induction E as [ | [? ?] ]; simpl; fsetdec. Qed.
End Properties.
(* *********************************************************************** *)
(** * The [simpl_alist] tactic *)
(** Rewriting hints. *)
Hint Rewrite cons_app_one cons_app_assoc : rewr_list.
Hint Rewrite app_assoc app_nil_1 app_nil_2 : rewr_list.
Hint Rewrite in_nil_iff in_one_iff in_app_iff : rewr_list_in.
Hint Rewrite map_nil map_one map_cons map_app : rewr_map.
Hint Rewrite dom_nil dom_one dom_cons dom_app dom_map : rewr_dom.
(** The [simpl_alist] tactic rewrites association lists so that they
are in the normal form described above. Similar to the [simpl]
tactic, we define "[in *]" and "[in H]" variants of the tactic. *)
Ltac simpl_alist :=
autorewrite with rewr_list rewr_map rewr_dom.
Tactic Notation "simpl_alist" "in" hyp(H) :=
autorewrite with rewr_list rewr_map rewr_dom in H.
Tactic Notation "simpl_alist" "in" "*" :=
autorewrite with rewr_list rewr_map rewr_dom in *.
(* *********************************************************************** *)
(** * The [rewrite_alist] tactic *)
(** The tactic [(rewrite_alist E)] replaces an association list in the
conclusion of the goal with [E]. Suitability for replacement is
determined by whether [simpl_alist] can put [E] and the chosen
environment in the same normal form, up to convertibility's in
Coq. We also define an "[in H]" variant that performs the
replacement in a hypothesis [H].
Implementation note: The tactic depends on appropriate morphisms
being declared for finite set operations. *)
Tactic Notation "rewrite_alist" constr(E) :=
match goal with
| |- context[?x] =>
change x with E
| |- context[?x] =>
replace x with E;
[ | try reflexivity; simpl_alist; reflexivity ]
end.
Tactic Notation "rewrite_alist" constr(E) "in" hyp(H) :=
match type of H with
| context[?x] =>
change x with E in H
| context[?x] =>
replace x with E in H;
[ | try reflexivity; simpl_alist; reflexivity ]
end.
(* *********************************************************************** *)
(** * Induction *)
(** For convenience, we provide a tactic for reasoning by induction on
the structure of an association list. Compared to regular [list]
induction, in the "[cons]" case, this tactic automatically
destructs the pair at the head of the list and ensures that the
list is formed using [one] and [app]. *)
Lemma alist_ind : forall (A : Type) (P : list (X.t * A) -> Type),
(P nil) ->
(forall x a xs, P xs -> P (x ~ a ++ xs)) ->
(forall xs, P xs).
Proof.
induction xs as [ | [x a] xs ].
auto.
change (P (x ~ a ++ xs)). auto.
Defined.
Tactic Notation "alist" "induction" ident(E) :=
try (intros until E);
let T := type of E in
let T := eval compute in T in
match T with
| list (?key * ?A) => induction E using (alist_ind A)
end.
Tactic Notation "alist" "induction" ident(E) "as" simple_intropattern(P) :=
try (intros until E);
let T := type of E in
let T := eval compute in T in
match T with
| list (?key * ?A) => induction E as P using (alist_ind A)
end.
(* *********************************************************************** *)
(** * Basic facts about [disjoint] *)
Section Disjoint.
Implicit Types A B C : Type.
Lemma disjoint_sym_1 :
forall A B (E : list (X.t*A)) (F : list (X.t*B)),
disjoint E F ->
disjoint F E.
Proof. unfold disjoint. fsetdec. Qed.
Lemma disjoint_sym :
forall A B (E : list (X.t*A)) (F : list (X.t*B)),
disjoint E F <-> disjoint F E.
Proof. intuition auto using disjoint_sym_1. Qed.
Lemma disjoint_nil_1 :
forall A B (E : list (X.t*B)),
disjoint (@nil (X.t*A)) E.
Proof. unfold disjoint. fsetdec. Qed.
Lemma disjoint_one_1 :
forall A B (x : X.t) (a : A) (F : list (X.t*B)),
disjoint (x ~ a) F ->
~ In x (dom F).
Proof. unfold disjoint. simpl. fsetdec. Qed.
Lemma disjoint_one_2 :
forall A B (x : X.t) (a : A) (F : list (X.t*B)),
~ In x (dom F) ->
disjoint (x ~ a) F.
Proof. unfold disjoint. simpl. fsetdec. Qed.
Lemma disjoint_one_l :
forall A B (x : X.t) (a : A) (E : list (X.t*B)),
disjoint (x ~ a) E <-> ~ In x (dom E).
Proof. unfold disjoint. simpl. split; fsetdec. Qed.
Lemma disjoint_one_r :
forall A B (x : X.t) (a : A) (E : list (X.t*B)),
disjoint E (x ~ a) <-> ~ In x (dom E).
Proof. intros. rewrite disjoint_sym. apply disjoint_one_l. Qed.
Lemma disjoint_cons_1 :
forall A B x a (E : list (X.t*A)) (F : list (X.t*B)),
disjoint ((x, a) :: E) F ->
~ In x (dom F).
Proof. unfold disjoint. simpl. fsetdec. Qed.
Lemma disjoint_cons_2 :
forall A B x a (E : list (X.t*A)) (F : list (X.t*B)),
disjoint ((x, a) :: E) F ->
disjoint E F.
Proof. unfold disjoint. simpl. fsetdec. Qed.
Lemma disjoint_cons_3 :
forall A B x a (E : list (X.t*A)) (F : list (X.t*B)),
disjoint E F ->
~ In x (dom F) ->
disjoint ((x, a) :: E) F.
Proof. unfold disjoint. simpl. fsetdec. Qed.
Lemma disjoint_cons_l :
forall A B x a (E : list (X.t*A)) (F : list (X.t*B)),
disjoint ((x, a) :: E) F <-> ~ In x (dom F) /\ disjoint E F.
Proof.
split.
eauto using disjoint_cons_1, disjoint_cons_2.
intros [? ?]. auto using disjoint_cons_3.
Qed.
Lemma disjoint_cons_r :
forall A B x a (E : list (X.t*A)) (F : list (X.t*B)),
disjoint F ((x, a) :: E) <-> ~ In x (dom F) /\ disjoint E F.
Proof. intros. rewrite disjoint_sym. apply disjoint_cons_l. Qed.
Lemma disjoint_app_1 :
forall A B (E F : list (X.t*A)) (G : list (X.t*B)),
disjoint (E ++ F) G ->
disjoint E G.
Proof. unfold disjoint. intros. rewrite dom_app in *. fsetdec. Qed.
Lemma disjoint_app_2 :
forall A B (E F : list (X.t*A)) (G : list (X.t*B)),
disjoint (E ++ F) G ->
disjoint F G.
Proof. unfold disjoint. intros. rewrite dom_app in *. fsetdec. Qed.
Lemma disjoint_app_3 :
forall A B (E F : list (X.t*A)) (G : list (X.t*B)),
disjoint E G ->
disjoint F G ->
disjoint (E ++ F) G.
Proof. unfold disjoint. intros. rewrite dom_app in *. fsetdec. Qed.
Lemma disjoint_app_l :
forall A B (E F : list (X.t*A)) (G : list (X.t*B)),
disjoint (E ++ F) G <-> disjoint E G /\ disjoint F G.
Proof.
intuition eauto 2 using
disjoint_app_1, disjoint_app_2, disjoint_app_3.
Qed.
Lemma disjoint_app_r :
forall A B (E F : list (X.t*A)) (G : list (X.t*B)),
disjoint G (E ++ F) <-> disjoint E G /\ disjoint F G.
Proof. intros. rewrite disjoint_sym. apply disjoint_app_l. Qed.
Lemma disjoint_map_1 :
forall A B C (E : list (X.t*A)) (F : list (X.t*B)) (f:A->C),
disjoint (map f E) F ->
disjoint E F.
Proof. unfold disjoint. intros. rewrite dom_map in *. fsetdec. Qed.
Lemma disjoint_map_2 :
forall A B C (E : list (X.t*A)) (F : list (X.t*B)) (f:A->C),
disjoint E F ->
disjoint (map f E) F.
Proof. unfold disjoint. intros. rewrite dom_map in *. fsetdec. Qed.
Lemma disjoint_map_l :
forall A B C (E : list (X.t*A)) (F : list (X.t*B)) (f:A->C),
disjoint (map f E) F <-> disjoint E F.
Proof. intuition eauto using disjoint_map_1, disjoint_map_2. Qed.
Lemma disjoint_map_r :
forall A B C (E : list (X.t*A)) (F : list (X.t*B)) (f:A->C),
disjoint F (map f E) <-> disjoint E F.
Proof. intros. rewrite disjoint_sym. apply disjoint_map_l. Qed.
End Disjoint.
(* *********************************************************************** *)
(** * Basic facts about [uniq] *)
Section UniqProperties.
Variables A B : Type.
Variables f : A -> B.
Variables x : X.t.
Variables a b : A.
Variables E F G : list (X.t*A).
Lemma uniq_one_1 :
uniq (x ~ b).
Proof.
clear. rewrite_alist ((x ~ b) ++ nil).
apply uniq_push. apply uniq_nil. apply empty_1.
Qed.
Lemma uniq_cons_1 :
uniq ((x, a) :: E) ->
uniq E.
Proof. clear. inversion 1. trivial. Qed.
Lemma uniq_cons_2 :
uniq ((x, a) :: E) ->
~ In x (dom E).
Proof. clear. inversion 1. trivial. Qed.
Lemma uniq_cons_3 :
uniq E ->
~ In x (dom E) ->
uniq ((x, a) :: E).
Proof.
clear. intros. change (uniq (x ~ a ++ E)). apply uniq_push; trivial.
Qed.
Lemma uniq_cons_iff :
uniq ((x, a) :: E) <-> uniq E /\ ~ In x (dom E).
Proof.
clear. split.
eauto using uniq_cons_1, uniq_cons_2.
intros [? ?]. auto using uniq_cons_3.
Qed.
Lemma uniq_app_1 :
uniq (E ++ F) -> uniq E.
Proof.
clear. intros J. alist induction E.
apply uniq_nil.
inversion J; subst. rewrite dom_app in *. apply uniq_push.
auto.
fsetdec.
Qed.
Lemma uniq_app_2 :
uniq (E ++ F) -> uniq F.
Proof.
clear. intros J. alist induction E.
auto.
inversion J; subst. auto.
Qed.
Lemma uniq_app_3 :
uniq (E ++ F) -> disjoint E F.
Proof.
clear. intros J. unfold disjoint. alist induction E as [ | ? ? ? IH ].
fsetdec.
inversion J; subst. simpl_alist in *. lapply IH.
fsetdec.
auto.
Qed.
Lemma uniq_app_4 :
uniq E ->
uniq F ->
disjoint E F ->
uniq (E ++ F).
Proof.
clear. intros HE HF Hd. alist induction E as [ | x1 a1 E' ].
auto.
inversion HE; subst. rewrite app_assoc. apply uniq_push.
rewrite disjoint_app_l, disjoint_one_l in *. intuition.
rewrite disjoint_app_l, disjoint_one_l, dom_app in *. fsetdec.
Qed.
Lemma uniq_app_iff :
uniq (E ++ F) <-> uniq E /\ uniq F /\ disjoint E F.
Proof.
clear; intuition auto using
uniq_app_1, uniq_app_2, uniq_app_3, uniq_app_4.
Qed.
Lemma uniq_map_1 :
uniq (map f E) ->
uniq E.
Proof.
clear. intros J. alist induction E as [ | x1 a1 E' ].
apply uniq_nil.
inversion J; subst. rewrite dom_map in *. apply uniq_push; auto.
Qed.
Lemma uniq_map_2 :
uniq E ->
uniq (map f E).
Proof.
clear. intros J. alist induction E as [ | x1 a1 E' ].
apply uniq_nil.
inversion J; subst. simpl_alist. apply uniq_push.
auto.
rewrite dom_map. trivial.
Qed.
Lemma uniq_map_iff :
uniq (map f E) <-> uniq E.
Proof. clear. intuition auto using uniq_map_1, uniq_map_2. Qed.
End UniqProperties.
(* *********************************************************************** *)
(** * Basic facts about [binds] *)
Section BindsProperties.
Variable A B : Type.
Variables f : A -> B.
Variables x y : X.t.
Variables a b : A.
Variables E F G : list (X.t*A).
Lemma binds_nil_iff :
binds x a nil <-> False.
Proof. clear. split. inversion 1. intuition. Qed.
Lemma binds_one_1 :
binds x a (y ~ b) ->
x = y.
Proof.
clear. intros H. inversion H as [HEq | HIn].
inversion HEq; intuition.
inversion HIn.
Qed.
Lemma binds_one_2 :
binds x a (y ~ b) ->
a = b.
Proof.
clear. intros H. inversion H as [HEq | HIn].
inversion HEq; intuition.
inversion HIn.
Qed.
Lemma binds_one_3 :
x = y ->
a = b ->
binds x a (y ~ b).
Proof. clear. unfold binds. intros. simpl. left. congruence. Qed.
Lemma binds_one_iff :
binds x a (y ~ b) <-> x = y /\ a = b.
Proof.
clear. intuition auto using binds_one_1, binds_one_2, binds_one_3.
Qed.
Lemma binds_cons_1 :
binds x a ((y, b) :: E) ->
(x = y /\ a = b) \/ binds x a E.
Proof. clear. inversion 1 as [J | J]; try injection J; auto. Qed.
Lemma binds_cons_2 :
x = y ->
a = b ->
binds x a ((y, b) :: E).
Proof. clear. unfold binds. simpl. left. f_equal; auto. Qed.
Lemma binds_cons_3 :
binds x a E ->
binds x a ((y, b) :: E).
Proof. clear. unfold binds. simpl. right. trivial. Qed.
Lemma binds_cons_iff :
binds x a ((y, b) :: E) <-> (x = y /\ a = b) \/ binds x a E.
Proof.
clear. intuition auto using binds_cons_1, binds_cons_2, binds_cons_3.
Qed.
Lemma binds_app_1 :
binds x a (E ++ F) ->
binds x a E \/ binds x a F.
Proof. clear. unfold binds. rewrite in_app_iff. auto. Qed.
Lemma binds_app_2 :
binds x a E ->
binds x a (E ++ F).
Proof. clear. unfold binds. rewrite in_app_iff. auto. Qed.
Lemma binds_app_3 :
binds x a F ->
binds x a (E ++ F).
Proof. clear. unfold binds. rewrite in_app_iff. auto. Qed.
Lemma binds_app_iff :
binds x a (E ++ F) <-> binds x a E \/ binds x a F.
Proof. clear. unfold binds. rewrite in_app_iff. split; auto. Qed.
Lemma binds_map_1 :
(forall a b, f a = f b -> a = b) ->
binds x (f a) (map f E) ->
binds x a E.
Proof.
clear. alist induction E; intros ?.
inversion 1.
unfold binds in *. simpl. intros [K | K].
left. injection K. intros. f_equal; auto.
right. auto.
Qed.
Lemma binds_map_2 :
binds x a E ->
binds x (f a) (map f E).
Proof.
clear. alist induction E.
inversion 1.
unfold binds in *. simpl. intros [? | ?].
left. congruence.
right. auto.
Qed.
Lemma binds_dom_contradiction : forall (E : list (X.t*A)),
binds x a E ->
~ In x (dom E) ->
False.
Proof.
clear. intros E H1 H2.
alist induction E as [ | ? ? ? IH ].
inversion H1.
unfold binds in *. simpl in *. destruct H1 as [J | J].
injection J. fsetdec.
eapply IH. auto. fsetdec.
Qed.
Lemma binds_app_uniq_1 :
uniq (E ++ F) ->
binds x a (E ++ F) ->
(binds x a E /\ ~ In x (dom F)) \/ (binds x a F /\ ~ In x (dom E)).
Proof.
clear. intros J1 J2.
rewrite uniq_app_iff in J1. unfold disjoint in J1.
rewrite binds_app_iff in J2.
assert (~ In x (dom F) \/ ~ In x (dom E)) by fsetdec.
intuition eauto using binds_dom_contradiction.
Qed.
Lemma binds_app_uniq_iff :
uniq (E ++ F) ->
(binds x a (E ++ F) <->
(binds x a E /\ ~ In x (dom F)) \/
(binds x a F /\ ~ In x (dom E))).
Proof.
clear. intuition auto using binds_app_uniq_1, binds_app_2, binds_app_3.
Qed.
End BindsProperties.
Section BindsProperties2.
Variable A B : Type.
Variables f : A -> B.
Variables x y : X.t.
Variables a b : A.
Variables E F G : list (X.t*A).
Lemma binds_cons_uniq_1 :
uniq ((y, b) :: E) ->
binds x a ((y, b) :: E) ->
(x = y /\ a = b /\ ~ In x (dom E)) \/ (binds x a E /\ x <> y).
Proof.
clear. intros J1 J2.
change ((y, b) :: E) with (y ~ b ++ E) in J1.
change ((y, b) :: E) with (y ~ b ++ E) in J2.
eapply binds_app_uniq_1 in J1; [ | eassumption ].
destruct J1 as [[J3 ?] | [? ?]].
unfold binds in J3. simpl in J3. destruct J3 as [J4 | ].
injection J4. intros. subst. auto.
intuition.
simpl in *. right. split; [ trivial | fsetdec ].
Qed.
Lemma binds_cons_uniq_iff :
uniq ((y, b) :: E) ->
(binds x a ((y, b) :: E) <->
(x = y /\ a = b /\ ~ In x (dom E)) \/
(binds x a E /\ x <> y)).
Proof.
clear. intuition auto using binds_cons_uniq_1, binds_cons_2, binds_cons_3.
Qed.
End BindsProperties2.
(* *********************************************************************** *)
(** * Hints *)
Hint Resolve
@app_assoc @app_nil_2 @map_app @dom_one @dom_cons @dom_app @dom_map.
Hint Resolve
@disjoint_sym_1 @disjoint_nil_1 @disjoint_one_2 @disjoint_cons_3
@disjoint_app_3 @disjoint_map_2 @uniq_nil @uniq_push @uniq_one_1
@uniq_cons_3 @uniq_app_4 @uniq_map_2.
Hint Resolve
@binds_one_3 @binds_cons_2 @binds_cons_3 @binds_app_2 @binds_app_3
@binds_map_2.
(* *********************************************************************** *)
(** * List properties *)
(** The following properties are an assortment of structural
properties about association lists. *)
Section AssortedListProperties.
Variable X : Type.
Variables x : X.
Variables xs ys zs : list X.
Lemma one_eq_app :
one x ++ xs = ys ++ zs ->
(exists qs, ys = x :: qs /\ xs = qs ++ zs) \/
(ys = nil /\ zs = x :: xs).
Proof. clear. auto using CoqListFacts.cons_eq_app. Qed.
Lemma app_eq_one :
ys ++ zs = one x ++ xs ->
(exists qs, ys = x :: qs /\ xs = qs ++ zs) \/
(ys = nil /\ zs = x :: xs).
Proof. clear. auto using CoqListFacts.app_eq_cons. Qed.
Lemma nil_neq_one_mid :
nil <> xs ++ one x ++ ys.
Proof. clear. induction xs; simpl_alist; intros J; inversion J. Qed.
Lemma one_mid_neq_nil :
xs ++ one x ++ ys <> nil.
Proof. clear. intros H. symmetry in H. auto using nil_neq_one_mid. Qed.
End AssortedListProperties.
(* *********************************************************************** *)
(** * Tactic support for [disjoint] and [uniq] *)
(** [destruct_uniq] decomposes all [uniq] and [disjoint] hypotheses. *)
Ltac destruct_uniq :=
match goal with
| H : uniq nil |- _ =>
clear H;
destruct_uniq
| H : uniq (?x ~ ?a) |- _ =>
clear H;
destruct_uniq
| H : uniq ((?x, ?a) :: ?E) |- _ =>
let J := fresh "UniqTac" in
pose proof H as J;
apply uniq_cons_1 in H;
apply uniq_cons_2 in J;
autorewrite with rewr_dom in J;
destruct_uniq
| H : uniq (?E ++ ?F) |- _ =>
let J1 := fresh "UniqTac" in
let J2 := fresh "UniqTac" in
pose proof H as J1;
pose proof H as J2;
apply uniq_app_1 in H;
apply uniq_app_2 in J1;
apply uniq_app_3 in J2;
destruct_uniq
| H : uniq (map ?f ?E) |- _ =>
apply uniq_map_1 in H;
destruct_uniq
| H : disjoint nil ?E |- _ =>
clear H;
destruct_uniq
| H : disjoint (?x ~ ?a) ?F |- _ =>
apply disjoint_one_1 in H;
autorewrite with rewr_dom in H;
destruct_uniq
| H : disjoint ((?x, ?a) :: ?E) ?F |- _ =>
let J := fresh "UniqTac" in
pose proof H as J;
apply disjoint_cons_1 in H;
apply disjoint_cons_2 in J;
autorewrite with rewr_dom in H;
destruct_uniq
| H : disjoint (?E ++ ?F) ?G |- _ =>
let J := fresh "UniqTac" in
pose proof H as J;
apply disjoint_app_1 in H;
apply disjoint_app_2 in J;
destruct_uniq
| H : disjoint (map ?f ?E) ?F |- _ =>
apply disjoint_map_1 in H;
destruct_uniq
| H : disjoint ?E nil |- _ =>
clear H;
destruct_uniq
| H : disjoint ?F (?x ~ ?a) |- _ =>
apply disjoint_sym_1 in H;
destruct_uniq
| H : disjoint ?F ((?x, ?a) :: ?E) |- _ =>
apply disjoint_sym_1 in H;
destruct_uniq
| H : disjoint ?G (?E ++ ?F) |- _ =>
apply disjoint_sym_1 in H;
destruct_uniq
| H : disjoint ?F (map ?f ?E) |- _ =>
apply disjoint_sym_1 in H;
destruct_uniq
| _ =>
idtac
end.
(** [solve_uniq] attempts to solve goals by first decomposing
hypotheses about [disjoint] and [uniq] and then trying some
simple, if perhaps slow, heuristics. *)
Ltac solve_uniq :=
intros;
destruct_uniq;
repeat first [ apply uniq_push
| apply uniq_cons_3
| apply uniq_app_4
| apply uniq_one_1
| apply uniq_nil ];
auto;
try tauto;
unfold disjoint in *;
try fsetdec;
fail "Not solvable by [solve_uniq]; try [destruct_uniq]".
(* *********************************************************************** *)
(** * Facts about [uniq] *)
Section UniqDerived.
Variable A : Type.
Variables x y : X.t.
Variables a b : A.
Variables E F G : list (X.t*A).
Lemma uniq_insert_mid :
uniq (G ++ E) ->
~ In x (dom G) ->
~ In x (dom E) ->
uniq (G ++ (x ~ a) ++ E).
Proof. clear. solve_uniq. Qed.
Lemma uniq_remove_mid :
uniq (E ++ F ++ G) ->
uniq (E ++ G).
Proof. clear. solve_uniq. Qed.
Lemma uniq_reorder_1 :
uniq (E ++ F) ->
uniq (F ++ E).
Proof. clear. solve_uniq. Qed.
Lemma uniq_reorder_2 :
uniq (E ++ F ++ G) ->
uniq (F ++ E ++ G).
Proof. clear. solve_uniq. Qed.
Lemma uniq_map_app_l : forall (f : A -> A),
uniq (F ++ E) ->
uniq (map f F ++ E).
Proof. clear. solve_uniq. Qed.
Lemma fresh_mid_tail :
uniq (F ++ (x ~ a) ++ E) ->
~ In x (dom E).
Proof. clear. solve_uniq. Qed.
Lemma fresh_mid_head :
uniq (F ++ (x ~ a) ++ E) ->
~ In x (dom F).
Proof. clear. solve_uniq. Qed.
End UniqDerived.
(* *********************************************************************** *)
(** * Tactic support for [binds] *)
(** [destruct_binds_hyp] and [destruct_binds_hyp_uniq] tactics
decompose a hypotheses of the form [binds x a E], with the latter
tactic assuming that [uniq E] holds. The tactic [solve_uniq] is
used for discharging any [uniq] obligations that arise.
Implementation note (BEA, XXX): No support for [map]. I'm not
sure what to do about the "injectivity" side condition on
[binds_map_inv]. Perhaps just generate the extra subgoal, on the