| 1 |
Require Export Crypto.Util.FixCoqMistakes. |
| 2 |
Require Import Crypto.Util.Tactics.Head. |
| 3 |
|
| 4 |
(** destruct discriminees of [match]es in the goal *) |
| 5 |
(* Prioritize breaking apart things in the context, then things which |
| 6 |
don't need equations, then simple matches (which can be displayed |
| 7 |
as [if]s), and finally matches in general. *) |
| 8 |
Ltac set_match_refl v' only_when := |
| 9 |
lazymatch goal with |
| 10 |
| [ |- context G[match ?e with _ => _ end eq_refl] ] |
| 11 |
=> only_when e; |
| 12 |
let T := fresh in |
| 13 |
evar (T : Type); evar (v' : T); |
| 14 |
subst T; |
| 15 |
let vv := (eval cbv delta [v'] in v') in |
| 16 |
let G' := context G[vv] in |
| 17 |
let G''' := context G[v'] in |
| 18 |
lazymatch goal with |- ?G'' => unify G' G'' end; |
| 19 |
change G''' |
| 20 |
end. |
| 21 |
Ltac set_match_refl_hyp v' only_when := |
| 22 |
lazymatch goal with |
| 23 |
| [ H : context G[match ?e with _ => _ end eq_refl] |- _ ] |
| 24 |
=> only_when e; |
| 25 |
let T := fresh in |
| 26 |
evar (T : Type); evar (v' : T); |
| 27 |
subst T; |
| 28 |
let vv := (eval cbv delta [v'] in v') in |
| 29 |
let G' := context G[vv] in |
| 30 |
let G''' := context G[v'] in |
| 31 |
let G'' := type of H in |
| 32 |
unify G' G''; |
| 33 |
change G''' in H |
| 34 |
end. |
| 35 |
Ltac destruct_by_existing_equation match_refl_hyp := |
| 36 |
let v := (eval cbv delta [match_refl_hyp] in match_refl_hyp) in |
| 37 |
lazymatch v with |
| 38 |
| match ?e with _ => _ end (@eq_refl ?T ?e) |
| 39 |
=> let H := fresh in |
| 40 |
let e' := fresh in |
| 41 |
pose e as e'; |
| 42 |
change e with e' in (value of match_refl_hyp) at 1; |
| 43 |
first [ pose (@eq_refl T e : e = e') as H; |
| 44 |
change (@eq_refl T e) with H in (value of match_refl_hyp); |
| 45 |
clearbody H e' |
| 46 |
| pose (@eq_refl T e : e' = e) as H; |
| 47 |
change (@eq_refl T e) with H in (value of match_refl_hyp); |
| 48 |
clearbody H e' ]; |
| 49 |
destruct e'; subst match_refl_hyp |
| 50 |
end. |
| 51 |
Ltac destruct_rewrite_sumbool e := |
| 52 |
let H := fresh in |
| 53 |
destruct e as [H|H]; |
| 54 |
try lazymatch type of H with |
| 55 |
| ?LHS = ?RHS |
| 56 |
=> lazymatch RHS with |
| 57 |
| context[LHS] => fail |
| 58 |
| _ => idtac |
| 59 |
end; |
| 60 |
rewrite ?H; rewrite ?H in *; |
| 61 |
repeat match goal with |
| 62 |
| [ |- context G[LHS] ] |
| 63 |
=> let LHS' := fresh in |
| 64 |
pose LHS as LHS'; |
| 65 |
let G' := context G[LHS'] in |
| 66 |
change G'; |
| 67 |
replace LHS' with RHS by (subst LHS'; symmetry; apply H); |
| 68 |
subst LHS' |
| 69 |
end |
| 70 |
end. |
| 71 |
Ltac break_match_step only_when := |
| 72 |
match goal with |
| 73 |
| [ |- context[match ?e with _ => _ end] ] |
| 74 |
=> only_when e; is_var e; destruct e |
| 75 |
| [ |- context[match ?e with _ => _ end] ] |
| 76 |
=> only_when e; |
| 77 |
match type of e with |
| 78 |
| sumbool _ _ => destruct_rewrite_sumbool e |
| 79 |
end |
| 80 |
| [ |- context[if ?e then _ else _] ] |
| 81 |
=> only_when e; destruct e eqn:? |
| 82 |
| [ |- context[match ?e with _ => _ end] ] |
| 83 |
=> only_when e; destruct e eqn:? |
| 84 |
| _ => let v := fresh in set_match_refl v only_when; destruct_by_existing_equation v |
| 85 |
end. |
| 86 |
Ltac break_match_hyps_step only_when := |
| 87 |
match goal with |
| 88 |
| [ H : context[match ?e with _ => _ end] |- _ ] |
| 89 |
=> only_when e; is_var e; destruct e |
| 90 |
| [ H : context[match ?e with _ => _ end] |- _ ] |
| 91 |
=> only_when e; |
| 92 |
match type of e with |
| 93 |
| sumbool _ _ => destruct_rewrite_sumbool e |
| 94 |
end |
| 95 |
| [ H : context[if ?e then _ else _] |- _ ] |
| 96 |
=> only_when e; destruct e eqn:? |
| 97 |
| [ H : context[match ?e with _ => _ end] |- _ ] |
| 98 |
=> only_when e; destruct e eqn:? |
| 99 |
| _ => let v := fresh in set_match_refl_hyp v only_when; destruct_by_existing_equation v |
| 100 |
end. |
| 101 |
Ltac break_match := repeat break_match_step ltac:(fun _ => idtac). |
| 102 |
Ltac break_match_hyps := repeat break_match_hyps_step ltac:(fun _ => idtac). |
| 103 |
Ltac break_match_when_head_step T := |
| 104 |
break_match_step |
| 105 |
ltac:(fun e => let T' := type of e in |
| 106 |
let T' := head T' in |
| 107 |
constr_eq T T'). |
| 108 |
Ltac break_match_hyps_when_head_step T := |
| 109 |
break_match_hyps_step |
| 110 |
ltac:(fun e => let T' := type of e in |
| 111 |
let T' := head T' in |
| 112 |
constr_eq T T'). |
| 113 |
Ltac break_match_when_head T := repeat break_match_when_head_step T. |
| 114 |
Ltac break_match_hyps_when_head T := repeat break_match_hyps_when_head_step T. |
| 115 |
Ltac break_innermost_match_step := |
| 116 |
break_match_step ltac:(fun v => lazymatch v with |
| 117 |
| context[match _ with _ => _ end] => fail |
| 118 |
| _ => idtac |
| 119 |
end). |
| 120 |
Ltac break_innermost_match_hyps_step := |
| 121 |
break_match_hyps_step ltac:(fun v => lazymatch v with |
| 122 |
| context[match _ with _ => _ end] => fail |
| 123 |
| _ => idtac |
| 124 |
end). |
| 125 |
Ltac break_innermost_match := repeat break_innermost_match_step. |
| 126 |
Ltac break_innermost_match_hyps := repeat break_innermost_match_hyps_step. |