forked from mit-plv/fiat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.v
1775 lines (1566 loc) · 66.3 KB
/
Common.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 Coq.Lists.List.
Require Import Coq.ZArith.ZArith Coq.Lists.SetoidList.
Require Export Coq.Setoids.Setoid Coq.Classes.RelationClasses
Coq.Program.Program Coq.Classes.Morphisms.
Require Export Fiat.Common.Tactics.SplitInContext.
Require Export Fiat.Common.Tactics.Combinators.
Require Export Fiat.Common.Tactics.FreeIn.
Require Export Fiat.Common.Tactics.SetoidSubst.
Require Export Fiat.Common.Tactics.BreakMatch.
Require Export Fiat.Common.Tactics.Head.
Require Export Fiat.Common.Tactics.FoldIsTrue.
Require Export Fiat.Common.Tactics.SpecializeBy.
Require Export Fiat.Common.Tactics.DestructHyps.
Require Export Fiat.Common.Tactics.DestructSig.
Require Export Fiat.Common.Tactics.DestructHead.
Require Export Fiat.Common.Coq__8_4__8_5__Compat.
Global Set Implicit Arguments.
Global Generalizable All Variables.
Global Set Asymmetric Patterns.
Global Coercion is_true : bool >-> Sortclass.
Coercion bool_of_sumbool {A B} (x : {A} + {B}) : bool := if x then true else false.
Coercion bool_of_sum {A B} (b : sum A B) : bool := if b then true else false.
Lemma bool_of_sum_distr_match_eta {A B C D} (x : sum A B) (c : A -> C) (d : B -> D)
: bool_of_sum (match x with inl k => inl (c k) | inr k => inr (d k) end) = bool_of_sum x.
Proof. destruct x; reflexivity. Qed.
Lemma bool_of_sum_distr_match_sumbool_eta {A B C D} (x : sumbool A B) (c : A -> C) (d : B -> D)
: bool_of_sum (match x with left k => inl (c k) | right k => inr (d k) end) = bool_of_sumbool x.
Proof. destruct x; reflexivity. Qed.
Lemma bool_of_sumbool_distr_match_sum_eta {A B} {C D : Prop} (x : sum A B) (c : A -> C) (d : B -> D)
: bool_of_sumbool (match x with inl k => left (c k) | inr k => right (d k) end) = bool_of_sum x.
Proof. destruct x; reflexivity. Qed.
Lemma bool_of_sumbool_distr_match_eta {A B} {C D : Prop} (x : sumbool A B) (c : A -> C) (d : B -> D)
: bool_of_sumbool (match x with left k => left (c k) | right k => right (d k) end) = bool_of_sumbool x.
Proof. destruct x; reflexivity. Qed.
Lemma distr_match_sum_dep {A B C D} (l : forall a : A, C (inl a)) (r : forall b : B, C (inr b))
(f : forall (x : sum A B), C x -> D x)
(x : sum A B)
: f x match x with inl a => l a | inr b => r b end
= match x with inl a => f _ (l a) | inr b => f _ (r b) end.
Proof. destruct x; reflexivity. Qed.
Lemma distr_match_sumbool_dep {A B : Prop} {C D} (l : forall a : A, C (left a)) (r : forall b : B, C (right b))
(f : forall (x : sumbool A B), C x -> D x)
(x : sumbool A B)
: f x match x with left a => l a | right b => r b end
= match x with left a => f _ (l a) | right b => f _ (r b) end.
Proof. destruct x; reflexivity. Qed.
Lemma distr_match_sum_fun_dep {Y A B C D} (l : forall (y : Y) (a : A), C y (inl a))
(r : forall (y : Y) (b : B), C y (inr b))
(f : forall (y : Y) (x : sum A B), C y x -> D y x)
(x : sum A B)
: (fun y => f y x match x with inl a => l y a | inr b => r y b end)
= (fun y => match x with inl a => f y _ (l y a) | inr b => f y _ (r y b) end).
Proof. destruct x; reflexivity. Qed.
Lemma distr_match_sumbool_fun_dep {Y} {A B : Prop} {C D} (l : forall (y : Y) (a : A), C y (left a))
(r : forall (y : Y) (b : B), C y (right b))
(f : forall (y : Y) (x : sumbool A B), C y x -> D y x)
(x : sumbool A B)
: (fun y => f y x match x with left a => l y a | right b => r y b end)
= (fun y => match x with left a => f y _ (l y a) | right b => f y _ (r y b) end).
Proof. destruct x; reflexivity. Qed.
Definition bool_of_sum_distr_match {A B C D} x (c : _ -> sum _ _) (d : _ -> sum _ _) : _ = _
:= @distr_match_sum_dep A B (fun _ => sum C D) (fun _ => bool) c d (fun _ => bool_of_sum) x.
Definition bool_of_sum_distr_match_sumbool {A B C D} x (c : _ -> sum _ _) (d : _ -> sum _ _) : _ = _
:= @distr_match_sumbool_dep A B (fun _ => sum C D) (fun _ => bool) c d (fun _ => bool_of_sum) x.
Definition bool_of_sumbool_distr_match_sum {A B C D} x (c : _ -> sumbool _ _) (d : _ -> sumbool _ _) : _ = _
:= @distr_match_sum_dep A B (fun _ => sumbool C D) (fun _ => bool) c d (fun _ => bool_of_sumbool) x.
Definition bool_of_sumbool_distr_match {A B C D} x (c : _ -> sumbool _ _) (d : _ -> sumbool _ _) : _ = _
:= @distr_match_sumbool_dep A B (fun _ => sumbool C D) (fun _ => bool) c d (fun _ => bool_of_sumbool) x.
Definition bool_of_sum_distr_match_fun {Y} {A B C D} x (c : _ -> _ -> sum _ _) (d : _ -> _ -> sum _ _) : _ = _
:= @distr_match_sum_fun_dep Y A B (fun _ _ => sum C D) (fun _ _ => bool) c d (fun _ _ => bool_of_sum) x.
Definition bool_of_sum_distr_match_sumbool_fun {Y} {A B C D} x (c : _ -> _ -> sum _ _) (d : _ -> _ -> sum _ _) : _ = _
:= @distr_match_sumbool_fun_dep Y A B (fun _ _ => sum C D) (fun _ _ => bool) c d (fun _ _ => bool_of_sum) x.
Definition bool_of_sumbool_distr_match_sum_fun {Y} {A B C D} x (c : _ -> _ -> sumbool _ _) (d : _ -> _ -> sumbool _ _) : _ = _
:= @distr_match_sum_fun_dep Y A B (fun _ _ => sumbool C D) (fun _ _ => bool) c d (fun _ _ => bool_of_sumbool) x.
Definition bool_of_sumbool_distr_match_fun {Y} {A B C D} x (c : _ -> _ -> sumbool _ _) (d : _ -> _ -> sumbool _ _) : _ = _
:= @distr_match_sumbool_fun_dep Y A B (fun _ _ => sumbool C D) (fun _ _ => bool) c d (fun _ _ => bool_of_sumbool) x.
Definition bool_of_sum_inl {A B} x : @bool_of_sum A B (inl x) = true := eq_refl.
Definition bool_of_sum_inr {A B} x : @bool_of_sum A B (inr x) = false := eq_refl.
Definition bool_of_sumbool_left {A B} x : @bool_of_sumbool A B (left x) = true := eq_refl.
Definition bool_of_sumbool_right {A B} x : @bool_of_sumbool A B (right x) = false := eq_refl.
Definition bool_of_sum_eta {A B} (x : sum A B) : (if x then true else false) = bool_of_sum x := eq_refl.
Definition bool_of_sumbool_eta {A B} (x : sumbool A B) : (if x then true else false) = bool_of_sumbool x := eq_refl.
Definition bool_of_sum_orb_true_l {A B} (x : sum A B) b
: (match x with inl _ => true | inr _ => b end)
= orb (bool_of_sum x) b.
Proof. destruct x, b; reflexivity. Qed.
#[global]
Hint Rewrite @bool_of_sum_inl @bool_of_sum_inr @bool_of_sum_distr_match_eta @bool_of_sum_distr_match_sumbool_eta @bool_of_sum_distr_match @bool_of_sum_distr_match_sumbool @bool_of_sum_distr_match_fun @bool_of_sum_distr_match_sumbool_fun @bool_of_sum_eta @bool_of_sum_orb_true_l : push_bool_of_sum.
#[global]
Hint Rewrite @bool_of_sumbool_left @bool_of_sumbool_right @bool_of_sumbool_distr_match_eta @bool_of_sumbool_distr_match_sum_eta @bool_of_sumbool_distr_match @bool_of_sumbool_distr_match_sum @bool_of_sumbool_distr_match_fun @bool_of_sumbool_distr_match_sum_fun @bool_of_sumbool_eta : push_bool_of_sumbool.
(** Runs [abstract] after clearing the environment, solving the goal
with the tactic associated with [cls <goal type>]. In 8.5, we
could pass a tactic instead. *)
Tactic Notation "clear" "abstract" constr(cls) :=
let G := match goal with |- ?G => constr:(G) end in
let pf := constr:(_ : cls G) in
let pf' := (eval cbv beta in pf) in
repeat match goal with
| [ H : _ |- _ ] => clear H; test (abstract (exact pf'))
end;
[ abstract (exact pf') ].
(** fail if [x] is a function application, a dependent product ([fun _
=> _]), or a sigma type ([forall _, _]) *)
Ltac atomic x :=
idtac;
match x with
| _ => is_evar x; fail 1 x "is not atomic (evar)"
| ?f _ => fail 1 x "is not atomic (application)"
| (fun _ => _) => fail 1 x "is not atomic (fun)"
| forall _, _ => fail 1 x "is not atomic (forall)"
| let x := _ in _ => fail 1 x "is not atomic (let in)"
| match _ with _ => _ end => fail 1 x "is not atomic (match)"
| _ => is_fix x; fail 1 x "is not atomic (fix)"
| context[?E] => (* catch-all *) (not constr_eq E x); fail 1 x "is not atomic (has subterm" E ")"
| _ => idtac
end.
(* [pose proof defn], but only if no hypothesis of the same type exists.
most useful for proofs of a proposition *)
Tactic Notation "unique" "pose" "proof" constr(defn) :=
let T := type of defn in
lazymatch goal with
| [ H : T |- _ ] => fail
| _ => pose proof defn
end.
(** [pose defn], but only if that hypothesis doesn't exist *)
Tactic Notation "unique" "pose" constr(defn) :=
lazymatch goal with
| [ H := defn |- _ ] => fail
| _ => pose defn
end.
Tactic Notation "unique" "assert" "(" ident(H) ":" constr(T) ")" :=
lazymatch goal with
| [ H : T |- _ ] => fail
| _ => assert (H : T)
end.
Tactic Notation "unique" "assert" "(" ident(H) ":" constr(T) ")" "by" tactic3(tac) :=
lazymatch goal with
| [ H : T |- _ ] => fail
| _ => assert (H : T) by tac
end.
(** check's if the given hypothesis has a body, i.e., if [clearbody]
could ever succeed. We can't just do [test_tac (clearbody H)],
because maybe the correctness of the proof depends on the body of
H *)
Tactic Notation "has" "body" hyp(H) :=
test (let H' := fresh in pose H as H'; unfold H in H').
Tactic Notation "etransitivity_rev" open_constr(v)
:= match goal with
| [ |- ?R ?LHS ?RHS ]
=> refine ((fun q p => @transitivity _ R _ LHS v RHS p q) _ _)
end.
Tactic Notation "etansitivity_rev" := etransitivity_rev _.
(** call [tac H], but first [simpl]ify [H].
This tactic leaves behind the simplified hypothesis. *)
Ltac simpl_do tac H :=
let H' := fresh in pose H as H'; simpl; simpl in H'; tac H'.
(** clear the left-over hypothesis after [simpl_do]ing it *)
Ltac simpl_do_clear tac H := simpl_do ltac:(fun H => tac H; try clear H) H.
Ltac simpl_rewrite term := simpl_do_clear ltac:(fun H => rewrite H) term.
Ltac simpl_rewrite_rev term := simpl_do_clear ltac:(fun H => rewrite <- H) term.
Tactic Notation "simpl" "rewrite" open_constr(term) := simpl_rewrite term.
Tactic Notation "simpl" "rewrite" "->" open_constr(term) := simpl_rewrite term.
Tactic Notation "simpl" "rewrite" "<-" open_constr(term) := simpl_rewrite_rev term.
Tactic Notation "csimpl" "rewrite" constr(term) := simpl_rewrite term.
Tactic Notation "csimpl" "rewrite" "->" constr(term) := simpl_rewrite term.
Tactic Notation "csimpl" "rewrite" "<-" constr(term) := simpl_rewrite_rev term.
Ltac do_with_hyp tac :=
match goal with
| [ H : _ |- _ ] => tac H
end.
Ltac rewrite_hyp' := do_with_hyp ltac:(fun H => rewrite H).
Ltac rewrite_hyp := repeat rewrite_hyp'.
Ltac rewrite_rev_hyp' := do_with_hyp ltac:(fun H => rewrite <- H).
Ltac rewrite_rev_hyp := repeat rewrite_rev_hyp'.
Ltac apply_hyp' := do_with_hyp ltac:(fun H => apply H).
Ltac apply_hyp := repeat apply_hyp'.
Ltac eapply_hyp' := do_with_hyp ltac:(fun H => eapply H).
Ltac eapply_hyp := repeat eapply_hyp'.
(** solve simple setiod goals that can be solved by [transitivity] *)
Ltac simpl_transitivity :=
try solve [ match goal with
| [ _ : ?Rel ?a ?b, _ : ?Rel ?b ?c |- ?Rel ?a ?c ] => transitivity b; assumption
end ].
(** if progress can be made by [exists _], but it doesn't matter what
fills in the [_], assume that something exists, and leave the two
goals of finding a member of the apropriate type, and proving that
all members of the appropriate type prove the goal *)
Ltac destruct_exists' T := cut T; try (let H := fresh in intro H; exists H).
Ltac destruct_exists := destruct_head_hnf @sigT;
match goal with
(* | [ |- @sig ?T _ ] => destruct_exists' T*)
| [ |- @sigT ?T _ ] => destruct_exists' T
(* | [ |- @sig2 ?T _ _ ] => destruct_exists' T*)
| [ |- @sigT2 ?T _ _ ] => destruct_exists' T
end.
(** if the goal can be solved by repeated specialization of some
hypothesis with other [specialized] hypotheses, solve the goal by
brute force *)
Ltac specialized_assumption tac := tac;
match goal with
| [ x : ?T, H : forall _ : ?T, _ |- _ ] => specialize (H x); specialized_assumption tac
| _ => assumption
end.
(** for each hypothesis of type [H : forall _ : ?T, _], if there is
exactly one hypothesis of type [H' : T], do [specialize (H H')]. *)
Ltac specialize_uniquely :=
repeat match goal with
| [ x : ?T, y : ?T, H : _ |- _ ] => test (specialize (H x)); fail 1
| [ x : ?T, H : _ |- _ ] => specialize (H x)
end.
(** specialize all hypotheses of type [forall _ : ?T, _] with
appropriately typed hypotheses *)
Ltac specialize_all_ways_forall :=
repeat match goal with
| [ x : ?T, H : forall _ : ?T, _ |- _ ] => unique pose proof (H x)
end.
(** try to specialize all hypotheses with all other hypotheses. This
includes [specialize (H x)] where [H x] requires a coercion from
the type of [H] to Funclass. *)
Ltac specialize_all_ways :=
repeat match goal with
| [ x : ?T, H : _ |- _ ] => unique pose proof (H x)
end.
Ltac apply_in_hyp lem :=
match goal with
| [ H : _ |- _ ] => apply lem in H
end.
Ltac apply_in_hyp_no_match lem :=
match goal with
| [ H : _ |- _ ] => apply lem in H;
match type of H with
| context[match _ with _ => _ end] => fail 1
| _ => idtac
end
end.
Ltac apply_in_hyp_no_cbv_match lem :=
match goal with
| [ H : _ |- _ ]
=> apply lem in H;
cbv beta iota in H;
match type of H with
| context[match _ with _ => _ end] => fail 1
| _ => idtac
end
end.
Ltac destruct_sum_in_match' :=
match goal with
| [ H : context[match ?E with inl _ => _ | inr _ => _ end] |- _ ]
=> destruct E
| [ |- context[match ?E with inl _ => _ | inr _ => _ end] ]
=> destruct E
end.
Ltac destruct_sum_in_match := repeat destruct_sum_in_match'.
Ltac destruct_ex :=
repeat match goal with
| [ H : ex _ |- _ ] => destruct H
end.
Ltac setoid_rewrite_hyp' := do_with_hyp ltac:(fun H => setoid_rewrite H).
Ltac setoid_rewrite_hyp := repeat setoid_rewrite_hyp'.
Ltac setoid_rewrite_rev_hyp' := do_with_hyp ltac:(fun H => setoid_rewrite <- H).
Ltac setoid_rewrite_rev_hyp := repeat setoid_rewrite_rev_hyp'.
Ltac apply_reflexivity := lazymatch goal with
| |- ?R ?x ?y => tryif is_evar R then fail else
(* Avoid a bad interaction with `Proper` proof search *)
lazymatch R with
| @Normalizes _ => fail
| @subrelation _ => fail
| _ => solve [apply reflexivity]
end
end.
#[global]
Hint Extern 0 => apply_reflexivity : typeclass_instances.
#[global]
Hint Extern 10 (Proper _ _) => progress cbv beta : typeclass_instances.
Ltac set_evars :=
repeat match goal with
| [ |- context[?E] ] => is_evar E; let H := fresh in set (H := E)
end.
Ltac subst_evars :=
repeat match goal with
| [ H := ?e |- _ ] => is_evar e; subst H
end.
Tactic Notation "eunify" open_constr(A) open_constr(B) := unify A B.
#[global]
Instance pointwise_refl A B (eqB : relation B) `{Reflexive _ eqB} : Reflexive (pointwise_relation A eqB).
Proof.
compute in *; auto.
Defined.
#[global]
Instance pointwise_sym A B (eqB : relation B) `{Symmetric _ eqB} : Symmetric (pointwise_relation A eqB).
Proof.
compute in *; auto.
Defined.
#[global]
Instance pointwise_transitive A B (eqB : relation B) `{Transitive _ eqB} : Transitive (pointwise_relation A eqB).
Proof.
compute in *; eauto.
Defined.
Lemma Some_ne_None {T} {x : T} : Some x <> None.
Proof.
congruence.
Qed.
Lemma None_ne_Some {T} {x : T} : None <> Some x.
Proof.
congruence.
Qed.
(* We define a wrapper for [if then else] in order for it to play
nicely with setoid_rewriting. *)
Definition If_Then_Else {A}
(c : bool)
(t e : A) :=
if c then t else e.
Notation "'If' c 'Then' t 'Else' e" :=
(If_Then_Else c t e)
(at level 70).
Definition If_Opt_Then_Else {A B}
(c : option A)
(t : A -> B)
(e : B) :=
match c with
| Some a => t a
| None => e
end.
Notation "'Ifopt' c 'as' c' 'Then' t 'Else' e" :=
(If_Opt_Then_Else c (fun c' => t) e)
(at level 70).
Global Instance If_Then_Else_fun_Proper {T} {R : relation T} {A B C D RA RB RC RD}
{bv tv fv}
{H0 : Proper (RA ==> RB ==> RC ==> RD ==> eq) bv}
{H1 : Proper (RA ==> RB ==> RC ==> RD ==> R) tv}
{H2 : Proper (RA ==> RB ==> RC ==> RD ==> R) fv}
: Proper (RA ==> RB ==> RC ==> RD ==> R) (fun (a : A) (b : B) (c : C) (d : D) => If bv a b c d Then tv a b c d Else fv a b c d).
Proof.
intros ?? Ha ?? Hb ?? Hc ?? Hd.
specialize (H0 _ _ Ha _ _ Hb _ _ Hc _ _ Hd).
rewrite H0; clear H0.
edestruct bv; simpl;
unfold Proper, impl, flip, respectful in *; eauto with nocore.
Qed.
Ltac find_if_inside :=
match goal with
| [ |- context[if ?X then _ else _] ] => destruct X
| [ |- context[If_Then_Else ?X _ _] ] => destruct X
| [ H : context[if ?X then _ else _] |- _ ]=> destruct X
| [ H : context[If_Then_Else ?X _ _] |- _ ]=> destruct X
end.
Ltac substs :=
repeat match goal with
| [ H : ?x = ?y |- _ ]
=> first [ subst x | subst y ]
end.
Ltac substss :=
repeat match goal with
| [ H : ?x = _ ,
H0 : ?x = _ |- _ ]
=> rewrite H in H0
end.
Ltac injections :=
repeat match goal with
| [ H : _ = _ |- _ ]
=> injection H; intros; subst; clear H
end.
Ltac inversion_by rule :=
progress repeat first [ progress destruct_ex
| progress split_and
| apply_in_hyp_no_cbv_match rule ].
Class can_transform_sigma A B := do_transform_sigma : A -> B.
#[global]
Instance can_transform_sigT_base {A} {P : A -> Type}
: can_transform_sigma (sigT P) (sigT P) | 0
:= fun x => x.
#[global]
Instance can_transform_sig_base {A} {P : A -> Prop}
: can_transform_sigma (sig P) (sig P) | 0
:= fun x => x.
#[global]
Instance can_transform_sigT {A B B' C'}
`{forall x : A, can_transform_sigma (B x) (@sigT (B' x) (C' x))}
: can_transform_sigma (forall x : A, B x)
(@sigT (forall x, B' x) (fun b => forall x, C' x (b x))) | 0
:= fun f => existT
(fun b => forall x : A, C' x (b x))
(fun x => projT1 (do_transform_sigma (f x)))
(fun x => projT2 (do_transform_sigma (f x))).
#[global]
Instance can_transform_sig {A B B' C'}
`{forall x : A, can_transform_sigma (B x) (@sig (B' x) (C' x))}
: can_transform_sigma (forall x : A, B x)
(@sig (forall x, B' x) (fun b => forall x, C' x (b x))) | 0
:= fun f => exist
(fun b => forall x : A, C' x (b x))
(fun x => proj1_sig (do_transform_sigma (f x)))
(fun x => proj2_sig (do_transform_sigma (f x))).
Ltac split_sig' :=
match goal with
| [ H : _ |- _ ]
=> let H' := fresh in
pose proof (@do_transform_sigma _ _ _ H) as H';
clear H;
destruct H'
end.
Ltac split_sig :=
repeat split_sig'.
Ltac clearbodies :=
repeat match goal with
| [ H := _ |- _ ] => clearbody H
end.
Ltac subst_body :=
repeat match goal with
| [ H := _ |- _ ] => subst H
end.
(** TODO: Maybe we should replace uses of this with [case_eq], which the stdlib defined for us? *)
Ltac caseEq x := generalize (refl_equal x); pattern x at -1; case x; intros.
Class ReflexiveT A (R : A -> A -> Type) :=
reflexivityT : forall x, R x x.
Class TransitiveT A (R : A -> A -> Type) :=
transitivityT : forall x y z, R x y -> R y z -> R x z.
Class PreOrderT A (R : A -> A -> Type) :=
{ PreOrderT_ReflexiveT :> ReflexiveT R;
PreOrderT_TransitiveT :> TransitiveT R }.
Definition respectful_heteroT A B C D
(R : A -> B -> Type)
(R' : forall (x : A) (y : B), C x -> D y -> Type)
(f : forall x, C x) (g : forall x, D x)
:= forall x y, R x y -> R' x y (f x) (g y).
(* Lifting forall and pointwise relations to multiple arguments. *)
Definition forall_relation2 {A : Type} {B : A -> Type} {C : forall a, B a -> Type} R :=
forall_relation (fun a => (@forall_relation (B a) (C a) (R a))).
Definition pointwise_relation2 {A B C : Type} (R : relation C) :=
pointwise_relation A (@pointwise_relation B C R).
Definition forall_relation3 {A : Type} {B : A -> Type}
{C : forall a, B a -> Type} {D : forall a b, C a b -> Type} R :=
forall_relation (fun a => (@forall_relation2 (B a) (C a) (D a) (R a))).
Definition pointwise_relation3 {A B C D : Type} (R : relation D) :=
pointwise_relation A (@pointwise_relation2 B C D R).
Definition forall_relation4 {A : Type} {B : A -> Type}
{C : forall a, B a -> Type} {D : forall a b, C a b -> Type}
{E : forall a b c, D a b c -> Type} R :=
forall_relation (fun a => (@forall_relation3 (B a) (C a) (D a) (E a) (R a))).
Definition pointwise_relation4 {A B C D E : Type} (R : relation E) :=
pointwise_relation A (@pointwise_relation3 B C D E R).
Ltac higher_order_1_reflexivity' :=
let a := match goal with |- ?R ?a (?f ?x) => constr:(a) end in
let f := match goal with |- ?R ?a (?f ?x) => constr:(f) end in
let x := match goal with |- ?R ?a (?f ?x) => constr:(x) end in
let a' := (eval pattern x in a) in
let f' := match a' with ?f' _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
Ltac sym_higher_order_1_reflexivity' :=
let a := match goal with |- ?R (?f ?x) ?a => constr:(a) end in
let f := match goal with |- ?R (?f ?x) ?a => constr:(f) end in
let x := match goal with |- ?R (?f ?x) ?a => constr:(x) end in
let a' := (eval pattern x in a) in
let f' := match a' with ?f' _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
(* refine is an antisymmetric relation, so we can try to apply
symmetric versions of higher_order_1_reflexivity. *)
Ltac higher_order_1_reflexivity :=
solve [ higher_order_1_reflexivity'
| sym_higher_order_1_reflexivity' ].
Ltac higher_order_1_f_reflexivity :=
let a := match goal with |- ?R (?g ?a) (?g' (?f ?x)) => constr:(a) end in
let f := match goal with |- ?R (?g ?a) (?g' (?f ?x)) => constr:(f) end in
let x := match goal with |- ?R (?g ?a) (?g' (?f ?x)) => constr:(x) end in
let a' := (eval pattern x in a) in
let f' := match a' with ?f' _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
(* This applies reflexivity after refining a method. *)
Ltac higher_order_2_reflexivity' :=
let x := match goal with |- ?R ?x (?f ?a ?b) => constr:(x) end in
let f := match goal with |- ?R ?x (?f ?a ?b) => constr:(f) end in
let a := match goal with |- ?R ?x (?f ?a ?b) => constr:(a) end in
let b := match goal with |- ?R ?x (?f ?a ?b) => constr:(b) end in
let x' := (eval pattern a, b in x) in
let f' := match x' with ?f' _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
Ltac sym_higher_order_2_reflexivity' :=
let x := match goal with |- ?R (?f ?a ?b) ?x => constr:(x) end in
let f := match goal with |- ?R (?f ?a ?b) ?x => constr:(f) end in
let a := match goal with |- ?R (?f ?a ?b) ?x => constr:(a) end in
let b := match goal with |- ?R (?f ?a ?b) ?x => constr:(b) end in
let x' := (eval pattern a, b in x) in
let f' := match x' with ?f' _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
Ltac higher_order_2_reflexivity :=
solve [ higher_order_2_reflexivity'
| sym_higher_order_2_reflexivity' ].
Ltac higher_order_2_f_reflexivity :=
let x := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b)) => constr:(x) end in
let f := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b)) => constr:(f) end in
let a := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b)) => constr:(a) end in
let b := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b)) => constr:(b) end in
let x' := (eval pattern a, b in x) in
let f' := match x' with ?f' _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
Ltac higher_order_3_reflexivity :=
let x := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(x) end in
let f := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(f) end in
let a := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(a) end in
let b := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(b) end in
let c := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(c) end in
let x' := (eval pattern a, b, c in x) in
let f' := match x' with ?f' _ _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
Ltac higher_order_3_f_reflexivity :=
let x := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(x) end in
let f := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(f) end in
let a := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(a) end in
let b := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(b) end in
let c := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(c) end in
let x' := (eval pattern a, b, c in x) in
let f' := match x' with ?f' _ _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
Ltac higher_order_4_reflexivity :=
let x := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(x) end in
let f := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(f) end in
let a := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(a) end in
let b := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(b) end in
let c := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(c) end in
let d := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(d) end in
let x' := (eval pattern a, b, c, d in x) in
let f' := match x' with ?f' _ _ _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
Ltac higher_order_4_f_reflexivity :=
let x := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(x) end in
let f := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(f) end in
let a := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(a) end in
let b := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(b) end in
let c := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(c) end in
let d := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(d) end in
let x' := (eval pattern a, b, c, d in x) in
let f' := match x' with ?f' _ _ _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivity].
Ltac higher_order_reflexivity :=
match goal with
| |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => higher_order_4_f_reflexivity
| |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => higher_order_3_f_reflexivity
| |- ?R (?g ?x) (?g' (?f ?a ?b)) => higher_order_2_f_reflexivity
| |- ?R (?g ?x) (?g' (?f ?a)) => higher_order_1_f_reflexivity
| |- ?R ?x (?f ?a ?b ?c ?d) => higher_order_4_reflexivity
| |- ?R ?x (?f ?a ?b ?c) => higher_order_3_reflexivity
| |- ?R ?x (?f ?a ?b) => higher_order_2_reflexivity
| |- ?R ?x (?f ?a) => higher_order_1_reflexivity
| |- _ => reflexivity
end.
Ltac pre_higher_order_reflexivity_single_evar :=
idtac;
match goal with
| [ |- ?L = ?R ] => has_evar R; not has_evar L; symmetry
| [ |- ?L = ?R ] => has_evar L; not has_evar R
| [ |- ?L = ?R ] => fail 1 "Goal has evars on both sides of the equality" L "=" R
| [ |- ?G ] => fail 1 "Goal is not an equality" G
end.
Ltac higher_order_reflexivity_single_evar_step :=
clear;
match goal with
| [ |- ?f ?x = ?R ] => is_var x; revert x
| [ |- ?f ?x = ?R ]
=> not has_evar x;
let R' := (eval pattern x in R) in
change (f x = R' x)
end;
(lazymatch goal with
| [ |- forall x, ?f x = @?R x ]
=> refine (fun x => f_equal (fun F => F x) (_ : f = R))
| [ |- ?f ?x = ?R ?x ]
=> refine (f_equal (fun F => F x) (_ : f = R))
end);
clear.
Ltac higher_order_reflexivity_single_evar :=
pre_higher_order_reflexivity_single_evar;
repeat (reflexivity || higher_order_reflexivity_single_evar_step).
Ltac higher_order_1_reflexivityT' :=
let a := match goal with |- ?R ?a (?f ?x) => constr:(a) end in
let f := match goal with |- ?R ?a (?f ?x) => constr:(f) end in
let x := match goal with |- ?R ?a (?f ?x) => constr:(x) end in
let a' := (eval pattern x in a) in
let f' := match a' with ?f' _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
Ltac sym_higher_order_1_reflexivityT' :=
let a := match goal with |- ?R (?f ?x) ?a => constr:(a) end in
let f := match goal with |- ?R (?f ?x) ?a => constr:(f) end in
let x := match goal with |- ?R (?f ?x) ?a => constr:(x) end in
let a' := (eval pattern x in a) in
let f' := match a' with ?f' _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
(* refine is an antisymmetric relation, so we can try to apply
symmetric versions of higher_order_1_reflexivityT. *)
Ltac higher_order_1_reflexivityT :=
solve [ higher_order_1_reflexivityT'
| sym_higher_order_1_reflexivityT' ].
Ltac higher_order_1_f_reflexivityT :=
let a := match goal with |- ?R (?g ?a) (?g' (?f ?x)) => constr:(a) end in
let f := match goal with |- ?R (?g ?a) (?g' (?f ?x)) => constr:(f) end in
let x := match goal with |- ?R (?g ?a) (?g' (?f ?x)) => constr:(x) end in
let a' := (eval pattern x in a) in
let f' := match a' with ?f' _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
(* This applies reflexivityT after refining a method. *)
Ltac higher_order_2_reflexivityT' :=
let x := match goal with |- ?R ?x (?f ?a ?b) => constr:(x) end in
let f := match goal with |- ?R ?x (?f ?a ?b) => constr:(f) end in
let a := match goal with |- ?R ?x (?f ?a ?b) => constr:(a) end in
let b := match goal with |- ?R ?x (?f ?a ?b) => constr:(b) end in
let x' := (eval pattern a, b in x) in
let f' := match x' with ?f' _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
Ltac sym_higher_order_2_reflexivityT' :=
let x := match goal with |- ?R (?f ?a ?b) ?x => constr:(x) end in
let f := match goal with |- ?R (?f ?a ?b) ?x => constr:(f) end in
let a := match goal with |- ?R (?f ?a ?b) ?x => constr:(a) end in
let b := match goal with |- ?R (?f ?a ?b) ?x => constr:(b) end in
let x' := (eval pattern a, b in x) in
let f' := match x' with ?f' _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
Ltac higher_order_2_reflexivityT :=
solve [ higher_order_2_reflexivityT'
| sym_higher_order_2_reflexivityT' ].
Ltac higher_order_2_f_reflexivityT :=
let x := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b)) => constr:(x) end in
let f := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b)) => constr:(f) end in
let a := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b)) => constr:(a) end in
let b := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b)) => constr:(b) end in
let x' := (eval pattern a, b in x) in
let f' := match x' with ?f' _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
Ltac higher_order_3_reflexivityT :=
let x := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(x) end in
let f := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(f) end in
let a := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(a) end in
let b := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(b) end in
let c := match goal with |- ?R ?x (?f ?a ?b ?c) => constr:(c) end in
let x' := (eval pattern a, b, c in x) in
let f' := match x' with ?f' _ _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
Ltac higher_order_3_f_reflexivityT :=
let x := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(x) end in
let f := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(f) end in
let a := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(a) end in
let b := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(b) end in
let c := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => constr:(c) end in
let x' := (eval pattern a, b, c in x) in
let f' := match x' with ?f' _ _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
Ltac higher_order_4_reflexivityT :=
let x := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(x) end in
let f := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(f) end in
let a := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(a) end in
let b := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(b) end in
let c := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(c) end in
let d := match goal with |- ?R ?x (?f ?a ?b ?c ?d) => constr:(d) end in
let x' := (eval pattern a, b, c, d in x) in
let f' := match x' with ?f' _ _ _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
Ltac higher_order_4_f_reflexivityT :=
let x := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(x) end in
let f := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(f) end in
let a := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(a) end in
let b := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(b) end in
let c := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(c) end in
let d := match goal with |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => constr:(d) end in
let x' := (eval pattern a, b, c, d in x) in
let f' := match x' with ?f' _ _ _ _ => constr:(f') end in
unify f f';
cbv beta;
solve [apply reflexivityT].
Ltac higher_order_0_f_reflexivityT :=
match goal with
|- ?R (?g ?a) (?g' ?x) =>
unify a x; solve [apply reflexivityT]
end.
Ltac higher_order_reflexivityT :=
match goal with
| |- ?R (?g ?x) (?g' (?f ?a ?b ?c ?d)) => higher_order_4_f_reflexivityT
| |- ?R (?g ?x) (?g' (?f ?a ?b ?c)) => higher_order_3_f_reflexivityT
| |- ?R (?g ?x) (?g' (?f ?a ?b)) => higher_order_2_f_reflexivityT
| |- ?R (?g ?x) (?g' (?f ?a)) => higher_order_1_f_reflexivityT
| |- ?R (?g ?a) (?g' ?x) => higher_order_0_f_reflexivityT
| |- ?R ?x (?f ?a ?b ?c ?d) => higher_order_4_reflexivityT
| |- ?R ?x (?f ?a ?b ?c) => higher_order_3_reflexivityT
| |- ?R ?x (?f ?a ?b) => higher_order_2_reflexivityT
| |- ?R ?x (?f ?a) => higher_order_1_reflexivityT
| |- _ => reflexivityT
end.
Global Arguments f_equal {A B} f {x y} _ .
Lemma fst_fold_right {A B A'} (f : B -> A -> A) (g : B -> A * A' -> A') a a' ls
: fst (fold_right (fun b aa' => (f b (fst aa'), g b aa')) (a, a') ls)
= fold_right f a ls.
Proof.
induction ls; simpl; trivial.
rewrite IHls; reflexivity.
Qed.
Lemma if_app {A} (ls1 ls1' ls2 : list A) (b : bool)
: (if b then ls1 else ls1') ++ ls2 = if b then (ls1 ++ ls2) else (ls1' ++ ls2).
Proof.
destruct b; reflexivity.
Qed.
Definition pull_if_dep {A B} (P : forall b : bool, A b -> B b) (a : A true) (a' : A false)
(b : bool)
: P b (if b as b return A b then a else a') =
if b as b return B b then P _ a else P _ a'
:= match b with true => eq_refl | false => eq_refl end.
Definition pull_if {A B} (P : A -> B) (a a' : A) (b : bool)
: P (if b then a else a') = if b then P a else P a'
:= pull_if_dep (fun _ => P) a a' b.
(** From jonikelee@gmail.com on coq-club *)
Ltac simplify_hyp' H :=
let T := type of H in
let X := (match eval hnf in T with ?X -> _ => constr:(X) end) in
let H' := fresh in
assert (H' : X) by (tauto || congruence);
specialize (H H');
clear H'.
Ltac simplify_hyps :=
repeat match goal with
| [ H : ?X -> _ |- _ ] => simplify_hyp' H
| [ H : ~?X |- _ ] => simplify_hyp' H
end.
Local Ltac bool_eq_t :=
destruct_head_hnf bool; simpl;
repeat (split || intro || destruct_head iff || congruence);
repeat match goal with
| [ H : ?x = ?x -> _ |- _ ] => specialize (H eq_refl)
| [ H : ?x <> ?x |- _ ] => specialize (H eq_refl)
| [ H : False |- _ ] => destruct H
| _ => progress simplify_hyps
| [ H : ?x = ?y |- _ ] => solve [ inversion H ]
| [ H : false = true -> _ |- _ ] => clear H
end.
Lemma bool_true_iff_beq (b0 b1 b2 b3 : bool)
: (b0 = b1 <-> b2 = b3) <-> (b0 = (if b1
then if b3
then b2
else negb b2
else if b3
then negb b2
else b2)).
Proof. bool_eq_t. Qed.
Lemma bool_true_iff_bneq (b0 b1 b2 b3 : bool)
: (b0 = b1 <-> b2 <> b3) <-> (b0 = (if b1
then if b3
then negb b2
else b2
else if b3
then b2
else negb b2)).
Proof. bool_eq_t. Qed.
Lemma bool_true_iff_bnneq (b0 b1 b2 b3 : bool)
: (b0 = b1 <-> ~b2 <> b3) <-> (b0 = (if b1
then if b3
then b2
else negb b2
else if b3
then negb b2
else b2)).
Proof. bool_eq_t. Qed.
Lemma dn_eqb (x y : bool) : ~~(x = y) -> x = y.
Proof.
destruct x, y; try congruence;
intro H; exfalso; apply H; congruence.
Qed.
Lemma neq_to_eq_negb (x y : bool) : x <> y -> x = negb y.
Proof.
destruct x, y; try congruence; try tauto.
Qed.
Lemma InA_In {A} R (ls : list A) x `{Reflexive _ R}
: List.In x ls -> InA R x ls.
Proof.
revert x.
induction ls; simpl; try tauto.
intros ? [?|?]; subst; [ left | right ]; auto.
Qed.
Lemma InA_In_eq {A} (ls : list A) x
: InA eq x ls <-> List.In x ls.
Proof.
split; [ | eapply InA_In; exact _ ].
revert x.
induction ls; simpl.
{ intros ? H. inversion H. }
{ intros ? H.
inversion H; subst;
first [ left; reflexivity
| right; eauto ]. }
Qed.
Lemma NoDupA_NoDup {A} R (ls : list A) `{Reflexive _ R}
: NoDupA R ls -> NoDup ls.
Proof.
intro H'.
induction H'; constructor; auto.
intro H''; apply (@InA_In _ R) in H''; intuition.
Qed.
Lemma push_if_existT {A} (P : A -> Type) (b : bool) (x y : sigT P)
: (if b then x else y)
= existT P
(if b then (projT1 x) else (projT1 y))
(if b as b return P (if b then (projT1 x) else (projT1 y))
then projT2 x
else projT2 y).
Proof.
destruct b, x, y; reflexivity.
Defined.
(** TODO: Find a better place for these *)
Lemma fold_right_projT1 {A B X} (P : A -> Type) (init : A * B) (ls : list X) (f : X -> A -> A) (g : X -> A -> B -> B) pf pf'
: List.fold_right (fun (x : X) (acc : A * B) =>
(f x (fst acc), g x (fst acc) (snd acc)))
init
ls
= let fr := List.fold_right (fun (x : X) (acc : sigT P * B) =>
(existT P (f x (projT1 (fst acc))) (pf' x acc),
g x (projT1 (fst acc)) (snd acc)))
(existT P (fst init) pf, snd init)
ls in
(projT1 (fst fr), snd fr).
Proof.
revert init pf.
induction ls; simpl; intros [? ?]; trivial; simpl.
intro.
simpl in *.
erewrite IHls; simpl.
reflexivity.
Qed.
Lemma fold_right_projT1' {A X} (P : A -> Type) (init : A) (ls : list X) (f : X -> A -> A) pf pf'
: List.fold_right f init ls
= projT1 (List.fold_right (fun (x : X) (acc : sigT P) =>
existT P (f x (projT1 acc)) (pf' x acc))
(existT P init pf)
ls).
Proof.
revert init pf.
induction ls; simpl; intros; trivial; simpl.
simpl in *.
erewrite IHls; simpl.
reflexivity.
Qed.
Fixpoint combine_sig_helper {T} {P : T -> Prop} (ls : list T) : (forall x, In x ls -> P x) -> list (sig P).
Proof.
refine match ls with