Debian Codegraph @

src/Util/Tactics/CPSId.v

raw
1 (** * [cps_id] is a scaffold for writing tactics that convert to cps normal form (where the continuation is the identity *)
2
3 Ltac ensure_complex_continuation allow_option k :=
4 lazymatch k with
5 | @id _ => fail
6 | (fun x => x) => fail
7 | _
8 => lazymatch allow_option with
9 | true => lazymatch k with
10 | @Some _ => fail
11 | (fun x => Some x) => fail
12 | _ => idtac
13 end
14 | false => idtac
15 | ?ao => fail 100 "Argument allow_option to ensure_complex_continuation must be true or false, not" ao
16 end
17 end.
18
19 Ltac continuation_of_rhs eqn :=
20 match eqn with
21 | ?lhs = ?k ?rhs => k
22 end.
23
24 Ltac continuation_of_lhs eqn :=
25 match eqn with
26 | ?lhs ?k = ?rhs => k
27 end.
28
29 Ltac cps_id_step continuation_of_eqn allow_option lem :=
30 let lem := open_constr:(lem) in
31 lazymatch type of lem with
32 | ?T -> _ => cps_id_step continuation_of_eqn allow_option open_constr:(lem _)
33 | ?lhs = ?rhs :> ?T
34 => let k := continuation_of_eqn (@eq T lhs rhs) in
35 match goal with
36 | [ |- context[?e] ]
37 => (* take advantage of lazymatch treating evars as holes/wildcards *)
38 lazymatch e with
39 | lhs
40 => let lem := constr:(lem : e = _) in
41 ensure_complex_continuation allow_option k;
42 rewrite lem
43 end
44 | [ H : context[?e] |- _ ]
45 => (* take advantage of lazymatch treating evars as holes/wildcards *)
46 lazymatch e with
47 | lhs
48 => let lem := constr:(lem : e = _) in
49 ensure_complex_continuation allow_option k;
50 rewrite lem in H
51 end
52 end
53 end.
54
55 Ltac cps_id allow_option lem := repeat cps_id_step continuation_of_rhs allow_option lem.
56 Ltac cps_id' allow_option lem := repeat cps_id_step continuation_of_lhs allow_option lem.
57 Tactic Notation "cps_id_with_option" uconstr(lem) := cps_id true lem.
58 Tactic Notation "cps_id_no_option" uconstr(lem) := cps_id false lem.
59 Tactic Notation "cps_id'_with_option" uconstr(lem) := cps_id' true lem.
60 Tactic Notation "cps_id'_no_option" uconstr(lem) := cps_id' false lem.