| 1 |
(** * Tactics for moving around [let ... in ...] and [dlet ... in ...] *) |
| 2 |
Require Import Crypto.Util.LetIn. |
| 3 |
(** The tactic [sig_dlet_in_rhs_to_context] moves to the context any |
| 4 |
[dlet x := y in ...] on the rhs of a goal of the form [{ a | lhs = |
| 5 |
rhs }]. *) |
| 6 |
Ltac sig_dlet_in_rhs_to_context_step v := |
| 7 |
lazymatch goal with |
| 8 |
| [ |- { a | _ = @Let_In ?A ?B ?x _ } ] |
| 9 |
=> pose x as v; |
| 10 |
change (@Let_In A B x) with (fun P : forall a : A, B a => P v); |
| 11 |
cbv beta |
| 12 |
end. |
| 13 |
Ltac sig_dlet_in_rhs_to_context := |
| 14 |
repeat let v := fresh "x" in sig_dlet_in_rhs_to_context_step v. |
| 15 |
(** The tactic [sig_dlet_in_lhs_to_context] moves to the context any |
| 16 |
[dlet x := y in ...] on the lhs of a goal of the form [{ a | lhs = |
| 17 |
rhs }]. *) |
| 18 |
Ltac sig_dlet_in_lhs_to_context_step v := |
| 19 |
lazymatch goal with |
| 20 |
| [ |- { a | @Let_In ?A ?B ?x _ = _ } ] |
| 21 |
=> pose x as v; |
| 22 |
change (@Let_In A B x) with (fun P : forall a : A, B a => P v); |
| 23 |
cbv beta |
| 24 |
end. |
| 25 |
Ltac sig_dlet_in_lhs_to_context := |
| 26 |
repeat let v := fresh "x" in sig_dlet_in_lhs_to_context_step v. |
| 27 |
(** The tactic [goal_dlet_to_context] moves to the context any |
| 28 |
[dlet x := y in ...] in the goal. *) |
| 29 |
Ltac goal_dlet_to_context_step v := |
| 30 |
match goal with |
| 31 |
| [ |- context[@Let_In ?A ?B ?x] ] |
| 32 |
=> pose x as v; |
| 33 |
change (@Let_In A B x) with (fun P : forall a : A, B a => P v); |
| 34 |
cbv beta |
| 35 |
end. |
| 36 |
Ltac goal_dlet_to_context := |
| 37 |
repeat let v := fresh "x" in goal_dlet_to_context_step v. |
| 38 |
(** Takes in a uconstr [uc], uses [set] to find it in the goal and |
| 39 |
passes the constr that it finds to [k] *) |
| 40 |
Local Ltac with_uconstr_in_goal uc k := |
| 41 |
let f := fresh in |
| 42 |
set (f := uc); |
| 43 |
let f' := (eval cbv delta [f] in f) in |
| 44 |
subst f; k f'. |
| 45 |
(** This tactic creates a [dlet x := f in rhs] in the rhs of a goal |
| 46 |
of the form [lhs = rhs]. *) |
| 47 |
Ltac context_to_dlet_in_rhs f := |
| 48 |
lazymatch goal with |
| 49 |
| [ |- ?R ?LHS ?RHS ] |
| 50 |
=> with_uconstr_in_goal |
| 51 |
f |
| 52 |
ltac:(fun f |
| 53 |
=> let RHS' := lazymatch (eval pattern f in RHS) with |
| 54 |
| ?RHS _ => RHS |
| 55 |
end in |
| 56 |
let x := fresh "x" in |
| 57 |
transitivity (dlet x := f in RHS' x); |
| 58 |
[ | clear; abstract (cbv [Let_In]; reflexivity) ] |
| 59 |
) |
| 60 |
end. |
| 61 |
Tactic Notation "context_to_dlet_in_rhs" uconstr(f) := context_to_dlet_in_rhs f. |