Debian Codegraph @

src/Util/FixCoqMistakes.v

raw
1 (** * Fixes *)
2 From Coq Require Import Morphisms.
3 Require Export Crypto.Util.GlobalSettings.
4
5 (** Coq is poorly designed in some ways. We fix some of these issues
6 in this file. *)
7
8 (** [intuition] means [intuition auto with *]. This is very wrong and
9 fragile and slow. We make [intuition] mean [intuition auto]. *)
10 Tactic Notation "intuition" tactic3(tactic) := intuition tactic.
11 Tactic Notation "intuition" := intuition auto.
12
13 (** [firstorder] means [firstorder auto with *]. This is very wrong
14 and fragile and slow. We make [firstorder] mean [firstorder
15 auto]. *)
16 Global Set Firstorder Solver auto.
17
18 (** A version of [intuition] that allows you to see how the old
19 [intuition] tactic solves the proof. *)
20 Ltac debug_intuition := idtac "<infomsg>Warning: debug_intuition should not be used in production code.</infomsg>"; intuition debug auto with *.
21
22 (** [Coq.Init.Logic.f_equal2] is opaque. A transparent version would serve us better. *)
23 Definition f_equal2 {A1 A2 B} (f : A1 -> A2 -> B) {x1 y1 : A1} {x2 y2 : A2} (H : x1 = y1)
24 := match H in (_ = y) return (x2 = y2 -> f x1 x2 = f y y2) with
25 | eq_refl =>
26 fun H0 : x2 = y2 =>
27 match H0 in (_ = y) return (f x1 x2 = f x1 y) with
28 | eq_refl => eq_refl
29 end
30 end.
31
32 (** Work around BZ#5341, https://coq.inria.fr/bugs/show_bug.cgi?id=5341, [subst] fails with bogus error message about universe polymorphism *)
33 Local Theorem create_internal_eq_rew_r_dep : forall A (a : A) (x : A) (e : a = x),
34 e = e -> True.
35 Proof.
36 intros ? ? ? H.
37 match goal with |- ?G => assert G end;
38 [ rewrite -> H
39 | rewrite <- H ];
40 constructor.
41 Defined.
42
43 (** Typeclass resolution is stupid. So we write a tactic that makes
44 it less stupid. We may occasionally have to redeclare the
45 instance, because, unfortunately, instance priorities cannot be
46 negative (which is also stupid). *)
47 (* We don't actually have function extensionality
48 Ltac make_rel do_pointwise T :=
49 lazymatch T with
50 | (?A -> ?B)
51 => let RA := make_rel true A in
52 let RB := make_rel do_pointwise B in
53 let default _ := constr:(@respectful A B RA RB) in
54 lazymatch do_pointwise with
55 | true => lazymatch RA with
56 | eq => constr:(@pointwise_relation A B RB)
57 | _ => default ()
58 end
59 | _ => default ()
60 end
61 | (forall a : ?A, ?B)
62 => let B' := fresh in
63 constr:(@forall_relation A (fun a : A => B) (fun a : A => match B with B' => ltac:(let B'' := (eval cbv delta [B'] in B') in
64 let RB := make_rel do_pointwise B in
65 exact RB) end))
66 | _ => constr:(@eq T)
67 end.
68 *)
69 Ltac make_eq_rel T :=
70 lazymatch T with
71 | (?A -> ?B)
72 => let RB := make_eq_rel B in
73 constr:(@respectful A B (@eq A) RB)
74 | (forall a : ?A, ?B)
75 => let B' := fresh in
76 constr:(@forall_relation A (fun a : A => B) (fun a : A => match B with B' => ltac:(let B'' := (eval cbv delta [B'] in B') in
77 let RB := make_eq_rel B in
78 exact RB) end))
79 | _ => constr:(@eq T)
80 end.
81 Ltac solve_Proper_eq :=
82 match goal with
83 | [ |- @Proper ?A ?R ?f ]
84 => let R' := make_eq_rel A in
85 unify R R';
86 apply (@reflexive_proper A R')
87 end.
88 Global Hint Extern 0 (Proper _ _) => solve_Proper_eq : typeclass_instances.