| 1 |
Require Import Crypto.Util.FixCoqMistakes. |
| 2 |
From Coq Require Import Morphisms Relation_Definitions. |
| 3 |
Require Import Crypto.Util.Tactics.GetGoal. |
| 4 |
Require Import Crypto.Util.Notations. |
| 5 |
Require Rewriter.Util.LetIn. |
| 6 |
|
| 7 |
(* Make this a notation for the version in [Rewriter] for ease of conversion *) |
| 8 |
Notation Let_In := Rewriter.Util.LetIn.Let_In (only parsing). |
| 9 |
Notation Let_In_pf := Rewriter.Util.LetIn.Let_In_pf (only parsing). |
| 10 |
|
| 11 |
Notation "'dlet_nd' x .. y := v 'in' f" := (Let_In (P:=fun _ => _) v (fun x => .. (fun y => f) .. )) (only parsing). |
| 12 |
Notation "'dlet' x .. y := v 'in' f" := (Let_In v (fun x => .. (fun y => f) .. )). |
| 13 |
Notation "'pflet' x , pf := y 'in' f" := (Let_In_pf y (fun x pf => f)). |
| 14 |
|
| 15 |
Module Bug5107WorkAround. |
| 16 |
Notation "'dlet' x .. y := v 'in' f" := (Let_In (P:=fun _ => _) v (fun x => .. (fun y => f) .. )). |
| 17 |
End Bug5107WorkAround. |
| 18 |
|
| 19 |
Global Instance Proper_Let_In_nd_changebody {A P R} {Reflexive_R:@Reflexive P R} |
| 20 |
: Proper (eq ==> pointwise_relation _ R ==> R) (@Let_In A (fun _ => P)). |
| 21 |
Proof. lazy; intros; subst; auto; congruence. Qed. |
| 22 |
|
| 23 |
Global Instance Proper_Let_In_nd_changevalue {A B} (RA:relation A) {RB:relation B} |
| 24 |
: Proper (RA ==> (RA ==> RB) ==> RB) (Let_In (P:=fun _=>B)). |
| 25 |
Proof. cbv; intuition. Qed. |
| 26 |
|
| 27 |
Lemma Proper_Let_In_nd_changebody_eq {A P R} {Reflexive_R:@Reflexive P R} {x} |
| 28 |
: Proper ((fun f g => forall a, x = a -> R (f a) (g a)) ==> R) (@Let_In A (fun _ => P) x). |
| 29 |
Proof. lazy; intros; subst; auto; congruence. Qed. |
| 30 |
|
| 31 |
Definition app_Let_In_nd {A B T} (f:B->T) (e:A) (C:A->B) |
| 32 |
: f (Let_In e C) = Let_In e (fun v => f (C v)) := eq_refl. |
| 33 |
|
| 34 |
Definition Let_app_In_nd {A B T} (f:A->B) (e:A) (C:B->T) |
| 35 |
: Let_In (f e) C = Let_In e (fun v => C (f v)) := eq_refl. |
| 36 |
|
| 37 |
Lemma unfold_Let_In {A B} v f : @Let_In A B v f = f v. |
| 38 |
Proof. reflexivity. Qed. |
| 39 |
|
| 40 |
Class _call_let_in_to_Let_In {T} (e:T) := _let_in_to_Let_In_return : T. |
| 41 |
(* : forall T, gallina T -> gallina T, structurally recursive in the argument *) |
| 42 |
Ltac let_in_to_Let_In e := |
| 43 |
lazymatch e with |
| 44 |
| let x := ?ex in @?eC x => |
| 45 |
let ex := let_in_to_Let_In ex in |
| 46 |
let eC := let_in_to_Let_In eC in |
| 47 |
constr:(Let_In ex eC) |
| 48 |
| ?f ?x => |
| 49 |
let f := let_in_to_Let_In f in |
| 50 |
let x := let_in_to_Let_In x in |
| 51 |
constr:(f x) |
| 52 |
| (fun x : ?T => ?C) => |
| 53 |
lazymatch constr:(fun (x : T) => (_ : _call_let_in_to_Let_In C)) |
| 54 |
(* [C] here above is an open term that references "x" by name *) |
| 55 |
with fun x => @?C x => C end (* match drops the type cast *) |
| 56 |
| ?x => x |
| 57 |
end. |
| 58 |
Global Hint Extern 0 (_call_let_in_to_Let_In ?e) => ( |
| 59 |
let e := let_in_to_Let_In e in eexact e |
| 60 |
) : typeclass_instances. |
| 61 |
Ltac change_let_in_with_Let_In := |
| 62 |
let g := get_goal in |
| 63 |
let g' := let_in_to_Let_In g in |
| 64 |
change g'. |