Debian Codegraph @

tactics/elim.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Util
12 open Names
13 open Termops
14 open EConstr
15 open Inductiveops
16 open Hipattern
17 open Tacticals
18 open Tactics
19
20 type elim_kind = Case of bool | Elim
21
22 (* Find the right elimination suffix corresponding to the sort of the goal *)
23 (* c should be of type A1->.. An->B with B an inductive definition *)
24 let general_elim_using mk_elim (ind, u, args) id = match mk_elim with
25 | Case dep ->
26 Clenv.case_pf ~dep (mkVar id, mkApp (mkIndU (ind, u), args))
27 | Elim ->
28 Proofview.Goal.enter begin fun gl ->
29 let env = Proofview.Goal.env gl in
30 let sigma = Proofview.Goal.sigma gl in
31 let sort = Retyping.get_sort_quality_of env sigma (Proofview.Goal.concl gl) in
32 let flags = Unification.elim_flags () in
33 let gr = Elimschemes.lookup_eliminator env ind sort in
34 let sigma, elim = Evd.fresh_global env sigma gr in
35 let elimt = Retyping.get_type_of env sigma elim in
36 (* applying elimination_scheme just a little modified *)
37 let elimclause = Clenv.mk_clenv_from env sigma (elim, elimt) in
38 let indmv = List.last (Clenv.clenv_arguments elimclause) in
39 let elimclause = Clenv.clenv_instantiate indmv elimclause (mkVar id, mkApp (mkIndU (ind, u), args)) in
40 Clenv.res_pf ~flags elimclause
41 end
42
43 (* computing the case/elim combinators *)
44
45 let elim_on_ba tac nassums =
46 Proofview.Goal.enter begin fun gl ->
47 let branches =
48 try List.rev (List.firstn nassums (Proofview.Goal.hyps gl))
49 with Failure _ -> CErrors.anomaly (Pp.str "make_elim_branch_assumptions.")
50 in
51 tac branches
52 end
53
54 let case_tac dep names tac (ind, u, args as spec) c =
55 let open Proofview.Notations in
56 Proofview.Goal.enter begin fun gl ->
57 let env = Proofview.Goal.env gl in
58 let branchsigns = Tacticals.compute_constructor_signatures env ~rec_flag:false (ind, u) in
59 let brnames = Tacticals.compute_induction_names false branchsigns names in
60 let after_tac i =
61 let branchnames = brnames.(i) in
62 let n1 = List.length branchsigns.(i) in
63 let n2 = List.length branchnames in
64 let (l1,l2),l3 =
65 if n1 < n2 then List.chop n1 branchnames, []
66 else (branchnames, []), List.make (n1-n2) false in
67 (intro_patterns false l1) <*> (intros_clearing l3) <*> (elim_on_ba (tac l2) n1)
68 in
69 let branchtacs = List.init (Array.length branchsigns) after_tac in
70 general_elim_using (Case dep) spec c <*>
71 (Proofview.tclEXTEND [] tclIDTAC branchtacs)
72 end
73
74 (* The following tactic Decompose repeatedly applies the
75 elimination(s) rule(s) of the types satisfying the predicate
76 ``recognizer'' onto a certain hypothesis. For example :
77
78 Require Elim.
79 Require Le.
80 Goal (y:nat){x:nat | (le O x)/\(le x y)}->{x:nat | (le O x)}.
81 Intros y H.
82 Decompose [sig and] H;EAuto.
83 Qed.
84
85 Another example :
86
87 Goal (A,B,C:Prop)(A/\B/\C \/ B/\C \/ C/\A) -> C.
88 Intros A B C H; Decompose [and or] H; Assumption.
89 Qed.
90 *)
91
92 let rec general_decompose_aux recognizer id =
93 let open Declarations in
94 let open Proofview.Notations in
95 Proofview.Goal.enter begin fun gl ->
96 let env = Proofview.Goal.env gl in
97 let sigma = Proofview.Goal.sigma gl in
98 let ((ind, u), t) = Tacred.reduce_to_atomic_ind env sigma (Retyping.get_type_of env sigma (mkVar id)) in
99 let _, args = decompose_app sigma t in
100 let rec_flag, mkelim =
101 match (Environ.lookup_mind (fst ind) env).mind_packets.(snd ind).mind_record with
102 | NotRecord -> true, Elim
103 | FakeRecord | PrimRecord _ -> false, Case false
104 in
105 let branchsigns = Tacticals.compute_constructor_signatures env ~rec_flag (ind, u) in
106 let next_tac bas =
107 let map id = ifOnHyp recognizer (general_decompose_aux recognizer) (fun _ -> tclIDTAC) id in
108 tclMAP map (ids_of_named_context bas)
109 in
110 let after_tac i =
111 let nassums = List.length branchsigns.(i) in
112 (tclDO nassums intro) <*> (clear [id]) <*> (elim_on_ba next_tac nassums)
113 in
114 let branchtacs = List.init (Array.length branchsigns) after_tac in
115 general_elim_using mkelim (ind, u, args) id <*>
116 (Proofview.tclEXTEND [] tclIDTAC branchtacs)
117 end
118
119
120 (* We should add a COMPLETE to be sure that the created hypothesis
121 doesn't stay if no elimination is possible *)
122
123 (* Best strategies but loss of compatibility *)
124 let tmphyp_name = Id.of_string "_TmpHyp"
125
126 let general_decompose recognizer c =
127 Proofview.Goal.enter begin fun gl ->
128 let env = Proofview.Goal.env gl in
129 let sigma = Proofview.Goal.sigma gl in
130 let typc = Retyping.get_type_of env sigma c in
131 tclTHENS (cut typc)
132 [ intro_using_then tmphyp_name (fun id ->
133 ifOnHyp recognizer (general_decompose_aux recognizer)
134 (fun id -> clear [id])
135 id);
136 exact_no_check c ]
137 end
138
139 let head_in indl t gl =
140 let env = Proofview.Goal.env gl in
141 let sigma = Proofview.Goal.sigma gl in
142 try
143 let ity,_ = extract_mrectype sigma t in
144 List.exists (fun i -> Environ.QInd.equal env (fst i) (fst ity)) indl
145 with Not_found -> false
146
147 let decompose_these c l =
148 Proofview.Goal.enter begin fun gl ->
149 let indl = List.map (fun x -> x, UVars.Instance.empty) l in
150 general_decompose (fun env sigma (_,t) -> head_in indl t gl) c
151 end
152
153 let decompose_and c =
154 general_decompose
155 (fun env sigma (_,t) -> is_record env sigma t)
156 c
157
158 let decompose_or c =
159 general_decompose
160 (fun env sigma (_,t) -> is_disjunction env sigma t)
161 c
162
163 let h_decompose l c = decompose_these c l
164
165 let h_decompose_or = decompose_or
166
167 let h_decompose_and = decompose_and