Debian Codegraph @

plugins/ltac/leminv.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 Pp
12 open CErrors
13 open Util
14 open Names
15 open Termops
16 open Environ
17 open Constr
18 open Context
19 open EConstr
20 open Vars
21 open Namegen
22 open Evd
23 open Printer
24 open Reductionops
25 open Inductiveops
26 open Tacticals
27 open Tactics
28 open Context.Named.Declaration
29
30 module NamedDecl = Context.Named.Declaration
31
32 let no_inductive_inconstr env sigma constr =
33 (str "Cannot recognize an inductive predicate in " ++
34 pr_leconstr_env env sigma constr ++
35 str "." ++ spc () ++ str "If there is one, may be the structure of the arity" ++
36 spc () ++ str "or of the type of constructors" ++ spc () ++
37 str "is hidden by constant definitions.")
38
39 (* Inversion stored in lemmas *)
40
41 (* ALGORITHM:
42
43 An inversion stored in a lemma is computed from a term-pattern, in
44 a signature, as follows:
45
46 Suppose we have an inductive relation, (I abar), in a signature Gamma:
47
48 Gamma |- (I abar)
49
50 Then we compute the free-variables of abar. Suppose that Gamma is
51 thinned out to only include these.
52
53 [We need technically to require that all free-variables of the
54 types of the free variables of abar are themselves free-variables
55 of abar. This needs to be checked, but it should not pose a
56 problem - it is hard to imagine cases where it would not hold.]
57
58 Now, we pose the goal:
59
60 (P:(Gamma)Prop)(Gamma)(I abar)->(P vars[Gamma]).
61
62 We execute the tactic:
63
64 REPEAT Intro THEN (OnLastHyp (Inv NONE false o outSOME))
65
66 This leaves us with some subgoals. All the assumptions after "P"
67 in these subgoals are new assumptions. I.e. if we have a subgoal,
68
69 P:(Gamma)Prop, Gamma, Hbar:Tbar |- (P ybar)
70
71 then the assumption we needed to have was
72
73 (Hbar:Tbar)(P ybar)
74
75 So we construct all the assumptions we need, and rebuild the goal
76 with these assumptions. Then, we can re-apply the same tactic as
77 above, but instead of stopping after the inversion, we just apply
78 the respective assumption in each subgoal.
79
80 *)
81
82 (* returns the sub_signature of sign corresponding to those identifiers that
83 * are not global. *)
84 (*
85 let get_local_sign sign =
86 let lid = ids_of_sign sign in
87 let globsign = Global.named_context() in
88 let add_local id res_sign =
89 if not (mem_sign globsign id) then
90 add_sign (lookup_sign id sign) res_sign
91 else
92 res_sign
93 in
94 List.fold_right add_local lid nil_sign
95 *)
96 (* returns the identifier of lid that was the latest declared in sign.
97 * (i.e. is the identifier id of lid such that
98 * sign_length (sign_prefix id sign) > sign_length (sign_prefix id' sign) >
99 * for any id'<>id in lid).
100 * it returns both the pair (id,(sign_prefix id sign)) *)
101 (*
102 let max_prefix_sign lid sign =
103 let rec max_rec (resid,prefix) = function
104 | [] -> (resid,prefix)
105 | (id::l) ->
106 let pre = sign_prefix id sign in
107 if sign_length pre > sign_length prefix then
108 max_rec (id,pre) l
109 else
110 max_rec (resid,prefix) l
111 in
112 match lid with
113 | [] -> nil_sign
114 | id::l -> snd (max_rec (id, sign_prefix id sign) l)
115 *)
116 let rec add_prods_sign env sigma t =
117 match EConstr.kind sigma (whd_all env sigma t) with
118 | Prod (na,c1,b) ->
119 let id = id_of_name_using_hdchar env sigma t na.binder_name in
120 let b'= subst1 (mkVar id) b in
121 add_prods_sign (push_named (LocalAssum ({na with binder_name=id},c1)) env) sigma b'
122 | LetIn (na,c1,t1,b) ->
123 let id = id_of_name_using_hdchar env sigma t na.binder_name in
124 let b'= subst1 (mkVar id) b in
125 add_prods_sign (push_named (LocalDef ({na with binder_name=id},c1,t1)) env) sigma b'
126 | _ -> (env,t)
127
128 (* [dep_option] indicates whether the inversion lemma is dependent or not.
129 If it is dependent and I is of the form (x_bar:T_bar)(I t_bar) then
130 the stated goal will be (x_bar:T_bar)(H:(I t_bar))(P t_bar H)
131 where P:(x_bar:T_bar)(H:(I x_bar))[sort].
132 The generalisation of such a goal at the moment of the dependent case should
133 be easy.
134
135 If it is non dependent, then if [I]=(I t_bar) and (x_bar:T_bar) are the
136 variables occurring in [I], then the stated goal will be:
137 (x_bar:T_bar)(I t_bar)->(P x_bar)
138 where P: P:(x_bar:T_bar)[sort].
139 *)
140
141 let compute_first_inversion_scheme env sigma ind sort dep_option =
142 let indf,realargs = dest_ind_type ind in
143 let allvars = vars_of_env env in
144 let p = next_ident_away (Id.of_string "P") allvars in
145 let pty,goal =
146 if dep_option then
147 let pty = make_arity env sigma true indf sort in
148 let r = relevance_of_inductive_type env ind in
149 let goal =
150 mkProd
151 (make_annot Anonymous r, mkAppliedInd ind, applist(mkVar p,realargs@[mkRel 1]))
152 in
153 pty,goal
154 else
155 let i = mkAppliedInd ind in
156 let ivars = Termops.global_vars_set env sigma i in
157 let revargs,ownsign =
158 fold_named_context
159 (fun env d (revargs,hyps) ->
160 let d = EConstr.of_named_decl d in
161 let id = NamedDecl.get_id d in
162 if Id.Set.mem id ivars then
163 ((mkVar id)::revargs, Context.Named.add d hyps)
164 else
165 (revargs,hyps))
166 env ~init:([],[])
167 in
168 let pty = it_mkNamedProd_or_LetIn sigma (mkSort sort) ownsign in
169 let goal = mkArrow i ERelevance.relevant (applist(mkVar p, List.rev revargs)) in
170 (pty,goal)
171 in
172 let npty = nf_all env sigma pty in
173 let extenv = push_named (LocalAssum (make_annot p ERelevance.relevant,npty)) env in
174 extenv, goal
175
176 (* [inversion_scheme sign I]
177
178 Given a local signature, [sign], and an instance of an inductive
179 relation, [I], inversion_scheme will prove the associated inversion
180 scheme on sort [sort]. Depending on the value of [dep_option] it will
181 build a dependent lemma or a non-dependent one *)
182
183 let inversion_scheme ~name ~poly env sigma t sort dep_option inv_op =
184 let (env,i) = add_prods_sign env sigma t in
185 let ind =
186 try find_rectype env sigma i
187 with Not_found ->
188 user_err (no_inductive_inconstr env sigma i)
189 in
190 let (invEnv,invGoal) =
191 compute_first_inversion_scheme env sigma ind sort dep_option
192 in
193 assert
194 (Id.Set.subset
195 (Termops.global_vars_set env sigma invGoal)
196 (ids_of_named_context_val (named_context_val invEnv)));
197 (*
198 user_err
199 (str"Computed inversion goal was not closed in initial signature.");
200 *)
201 let pf = Proof.start ~name ~poly (Evd.from_ctx (ustate sigma)) [invEnv,invGoal] in
202 let pf, _, () = Proof.run_tactic env (tclTHEN intro (onLastHypId inv_op)) pf in
203 let pfterm = List.hd (Proof.partial_proof pf) in
204 let global_named_context = Global.named_context_val () in
205 let ownSign = ref begin
206 fold_named_context
207 (fun env d sign ->
208 let d = EConstr.of_named_decl d in
209 if mem_named_context_val (NamedDecl.get_id d) global_named_context then sign
210 else Context.Named.add d sign)
211 invEnv ~init:Context.Named.empty
212 end in
213 let avoid = ref Id.Set.empty in
214 let Proof.{sigma} = Proof.data pf in
215 let sigma = Evd.minimize_universes sigma in
216 let rec fill_holes c =
217 match EConstr.kind sigma c with
218 | Evar (e,args) ->
219 let h = next_ident_away (Id.of_string "H") !avoid in
220 let ty,inst = Evarutil.generalize_evar_over_rels sigma (e,args) in
221 avoid := Id.Set.add h !avoid;
222 ownSign := Context.Named.add (LocalAssum (make_annot h ERelevance.relevant,ty)) !ownSign;
223 applist (mkVar h, inst)
224 | _ -> EConstr.map sigma fill_holes c
225 in
226 let c = fill_holes pfterm in
227 (* warning: side-effect on ownSign *)
228 let invProof = it_mkNamedLambda_or_LetIn sigma c !ownSign in
229 invProof, sigma
230
231 let add_inversion_lemma ~poly (name:lident) env sigma t sort dep inv_op =
232 let invProof, sigma = inversion_scheme ~name:name.v ~poly env sigma t sort dep inv_op in
233 let cinfo = Declare.CInfo.make ?loc:name.loc ~name:name.v ~typ:None () in
234 let info = Declare.Info.make ~poly ~kind:Decls.(IsProof Lemma) () in
235 let _ : Names.GlobRef.t =
236 Declare.declare_definition ~cinfo ~info ~opaque:false ~body:invProof sigma
237 in
238 ()
239
240 (* inv_op = Inv (derives de complete inv. lemma)
241 * inv_op = InvNoThining (derives de semi inversion lemma) *)
242
243 let add_inversion_lemma_exn ~poly na com comsort bool tac =
244 let env = Global.env () in
245 let sigma = Evd.from_env env in
246 let c, uctx = Constrintern.interp_type env sigma com in
247 let sigma = Evd.from_ctx uctx in
248 let sigma, sort = Evd.fresh_sort_in_quality ~rigid:univ_rigid sigma comsort in
249 add_inversion_lemma ~poly na env sigma c sort bool tac
250
251 (* ================================= *)
252 (* Applying a given inversion lemma *)
253 (* ================================= *)
254
255 let lemInv id c =
256 Proofview.Goal.enter begin fun gls ->
257 let env = Proofview.Goal.env gls in
258 let sigma = Proofview.Goal.sigma gls in
259 let clause = Clenv.mk_clenv_from env sigma (c, Retyping.get_type_of env sigma c) in
260 let mv = let mvs = Clenv.clenv_arguments clause in
261 if List.is_empty mvs then
262 CErrors.user_err
263 Pp.(hov 0 (pr_econstr_env env sigma c ++ spc ()
264 ++ str "does not refer to an inversion lemma."))
265 else List.last mvs
266 in
267 try
268 let clause = Clenv.clenv_instantiate mv clause (EConstr.mkVar id, Typing.type_of_variable env id) in
269 Clenv.res_pf clause ~flags:(Unification.elim_flags ()) ~with_evars:false
270 with
271 | Failure _ | UserError _ ->
272 user_err
273 (str "Cannot refine current goal with the lemma " ++
274 pr_leconstr_env env sigma c ++ str ".")
275 end
276
277 let lemInv_gen id c = try_intros_until (fun id -> lemInv id c) id
278
279 let lemInvIn id c ids =
280 Proofview.Goal.enter begin fun gl ->
281 let hyps = List.map (fun id -> Tacmach.pf_get_hyp id gl) ids in
282 let intros_replace_ids =
283 let concl = Proofview.Goal.concl gl in
284 let sigma = Proofview.Goal.sigma gl in
285 let nb_of_new_hyp = nb_prod sigma concl - List.length ids in
286 if nb_of_new_hyp < 1 then
287 intros_replacing ids
288 else
289 (tclTHEN (tclDO nb_of_new_hyp intro) (intros_replacing ids))
290 in
291 ((tclTHEN (tclTHEN (Generalize.bring_hyps hyps) (lemInv id c))
292 (intros_replace_ids)))
293 end
294
295 let lemInvIn_gen id c l = try_intros_until (fun id -> lemInvIn id c l) id
296
297 let lemInv_clause id c = function
298 | [] -> lemInv_gen id c
299 | l -> lemInvIn_gen id c l