Debian Codegraph @

src/Util/Tactics/CacheTerm.v

raw
1 Require Import Crypto.Util.Tactics.EvarNormalize.
2 Require Import Crypto.Util.Tactics.ClearFree.
3
4 Ltac allow_debug_in_cache := constr:(false).
5
6 Ltac abstract_tac_with_debug abstract_tac tac arg :=
7 let ctrue := constr:(true) in
8 let cfalse := constr:(false) in
9 let T := lazymatch goal with |- ?T => T end in
10 let id' := fresh in
11 simple notypeclasses refine (match _ : T return T with id' => _ end);
12 [ lazymatch allow_debug_in_cache with
13 | false => solve [ unshelve tac ]
14 | true => tac; shelve
15 | ?v => fail 1000 "Invalid non-bool value for allow_debug_in_cache" v "(" "expected" ctrue "or" cfalse ")"
16 end
17 | let res := (eval cbv delta [id'] in id') in
18 clear id';
19 lazymatch allow_debug_in_cache with
20 | false
21 => abstract_tac ltac:(exact_no_check res) arg
22 | true
23 => tryif abstract_tac ltac:(exact_no_check res) arg
24 then idtac
25 else exact res
26 | ?v => fail 1000 "Invalid non-bool value for allow_debug_in_cache" v "(" "expected" ctrue "or" cfalse ")"
27 end ].
28
29 Ltac cache_term_with_type_by_gen ty abstract_tac tac id :=
30 let id' := fresh id in
31 let __ := lazymatch goal with
32 | [ |- ?T ]
33 => simple notypeclasses refine (match _ : ty return T with id' => _ end);
34 [ abstract_tac_with_debug abstract_tac tac id'
35 | ]
36 end in
37 let ret := (eval cbv [id'] in id') in
38 let __ := match goal with
39 | _ => clear id'
40 end in
41 ret.
42
43 Ltac clear_cache_term_with_type_by do_clear ty tac id :=
44 cache_term_with_type_by_gen ty ltac:(fun tac id' => do_clear (); transparent_abstract tac using id') ltac:(do_clear (); tac) id.
45 Ltac clear_cache_proof_with_type_by do_clear ty tac id :=
46 cache_term_with_type_by_gen ty ltac:(fun tac id' => do_clear (); abstract tac using id') ltac:(do_clear (); tac) id.
47 Ltac cache_term_with_type_by ty tac id := clear_cache_term_with_type_by ltac:(fun _ => idtac) ty tac id.
48 Ltac cache_proof_with_type_by ty tac id := clear_cache_proof_with_type_by ltac:(fun _ => idtac) ty tac id.
49 Ltac cache_term_by tac id :=
50 let ty := open_constr:(_) in
51 cache_term_with_type_by ty tac id.
52 Ltac cache_proof_by ty tac id :=
53 let ty := open_constr:(_) in
54 cache_proof_with_type_by ty tac id.
55 Ltac cache_term term id :=
56 let ty := type of term in
57 let term := evar_normalize term in (* COQBUG(https://github.com/coq/coq/issues/10044) *)
58 clear_cache_term_with_type_by ltac:(fun _ => clear_free term) ty ltac:(exact_no_check term) id.
59
60 Ltac cache_sig_red_with_type_by ty tac red_tac red_cast_no_check id :=
61 let id' := fresh id in
62 let id' := fresh id in
63 let id' := cache_term_with_type_by ty tac id' in
64 let P := lazymatch ty with sig ?P => P end in
65 cache_term_with_type_by
66 ty
67 ltac:(let v := (eval cbv beta iota delta [id' proj1_sig] in (proj1_sig id')) in
68 let v := red_tac v in
69 (exists v);
70 let tac :=
71 refine (eq_rect _ P (proj2_sig id') _ _);
72 red_cast_no_check (eq_refl v)
73 in
74 abstract_tac_with_debug ltac:(fun tac _ => abstract tac) tac ()
75 )
76 id.
77 Ltac cache_sig_red_with_type ty term red_tac red_cast_no_check id :=
78 let id' := fresh id in
79 let id' := fresh id in
80 let id' := cache_term term id' in
81 let P := lazymatch ty with sig ?P => P end in
82 cache_term_with_type_by
83 ty
84 ltac:(let v := (eval cbv beta iota delta [id' proj1_sig] in (proj1_sig id')) in
85 let v := red_tac v in
86 (exists v);
87 let tac _ :=
88 refine (eq_rect _ P (proj2_sig id') _ _);
89 red_cast_no_check (eq_refl v)
90 in
91 abstract_tac_with_debug ltac:(fun tac _ => abstract tac) tac ()
92 )
93 id.
94 Ltac cache_sig_with_type_by ty tac id
95 := cache_sig_red_with_type_by ty tac ltac:(fun v => v) exact_no_check id.
96 Ltac cache_vm_sig_with_type_by ty tac id
97 := cache_sig_red_with_type_by ty tac ltac:(fun v => eval vm_compute in v) vm_cast_no_check id.
98 Ltac cache_sig_with_type ty term id
99 := cache_sig_red_with_type ty term ltac:(fun v => v) exact_no_check id.
100 Ltac cache_vm_sig_with_type ty term id
101 := cache_sig_red_with_type ty term ltac:(fun v => eval vm_compute in v) vm_cast_no_check id.