Debian Codegraph @

plugins/ssr/ssrfwd.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 (* This file is (C) Copyright 2006-2015 Microsoft Corporation and Inria. *)
12
13 open Pp
14 open Names
15 open Constr
16 open Context
17
18 open Proofview.Notations
19
20 open Ssrmatching_plugin.Ssrmatching
21 open Ssrprinters
22 open Ssrcommon
23 open Ssrtacticals
24
25 module RelDecl = Context.Rel.Declaration
26 module ERelevance = EConstr.ERelevance
27
28 (** 8. Forward chaining tactics (pose, set, have, suffice, wlog) *)
29 (** Defined identifier *)
30
31 let ssrposetac (id, (_, t)) =
32 let open Proofview.Notations in
33 Proofview.Goal.enter begin fun gl ->
34 let env = Proofview.Goal.env gl in
35 let sigma = Proofview.Goal.sigma gl in
36 let ist, t =
37 match t.Ssrast.interp_env with
38 | Some ist -> ist, Ssrcommon.ssrterm_of_ast_closure_term t
39 | None -> assert false in
40 let sigma, t, _ = abs_ssrterm ist env sigma t in
41 Proofview.Unsafe.tclEVARS sigma <*>
42 Tactics.pose_tac (Name id) t
43 end
44
45 let redex_of_pattern_tc env p =
46 let sigma, e = match redex_of_pattern p with
47 | None -> CErrors.anomaly (str "pattern without redex.")
48 | Some (sigma, e) -> sigma, e
49 in
50 let sigma = Typeclasses.resolve_typeclasses ~fail:false env sigma in
51 Evarutil.nf_evar sigma e, Evd.ustate sigma
52
53 let ssrsettac id ((_, (pat, pty)), (_, occ)) =
54 let open Proofview.Notations in
55 Proofview.Goal.enter begin fun gl ->
56 let env = Proofview.Goal.env gl in
57 let sigma = Proofview.Goal.sigma gl in
58 let cl = Proofview.Goal.concl gl in
59 let pty = Option.map (fun { Ssrast.body; interp_env } ->
60 let ist = Option.get interp_env in
61 (mkRHole, Some body), ist) pty in
62 let pat = interp_cpattern env sigma pat pty in
63 let (c, ucst), cl =
64 try fill_occ_pattern ~raise_NoMatch:true env sigma cl pat occ 1
65 with NoMatch -> redex_of_pattern_tc env pat, cl in
66 let sigma = Evd.merge_universe_context sigma ucst in
67 if Termops.occur_existential sigma c then errorstrm(str"The pattern"++spc()++
68 pr_econstr_pat env sigma c++spc()++str"did not match and has holes."++spc()++
69 str"Did you mean pose?") else
70 let c, (sigma, cty) = match EConstr.kind sigma c with
71 | Cast(t, DEFAULTcast, ty) -> t, (sigma, ty)
72 | _ -> c, Typing.type_of env sigma c in
73 let cl' = EConstr.mkLetIn (make_annot (Name id) ERelevance.relevant, c, cty, cl) in
74 Proofview.Unsafe.tclEVARS sigma <*>
75 convert_concl ~check:true cl' <*>
76 introid id
77 end
78
79 open Util
80
81 open Printer
82
83 open Ssrast
84 open Ssripats
85
86 let ssrhaveNOtcresolution = Summary.ref ~name:"SSR:havenotcresolution" false
87
88 let () =
89 Goptions.(declare_bool_option
90 { optstage = Summary.Stage.Interp;
91 optkey = ["SsrHave";"NoTCResolution"];
92 optread = (fun _ -> !ssrhaveNOtcresolution);
93 optdepr = None;
94 optwrite = (fun b -> ssrhaveNOtcresolution := b);
95 })
96
97
98 open Constrexpr
99 open Glob_term
100
101 let combineCG t1 t2 f g = match t1, t2 with
102 | (x, (t1, None)), (_, (t2, None)) -> x, (g t1 t2, None)
103 | (x, (_, Some t1)), (_, (_, Some t2)) -> x, (mkRHole, Some (f t1 t2))
104 | _, (_, (_, None)) -> anomaly "have: mixed C-G constr"
105 | _ -> anomaly "have: mixed G-C constr"
106
107 type cut_kind = Have | HaveTransp | Suff
108
109 let basecuttac k c =
110 let open Proofview.Notations in
111 let open EConstr in
112 Proofview.Goal.enter begin fun gl ->
113 let env = Proofview.Goal.env gl in
114 let sigma = Proofview.Goal.sigma gl in
115 let concl = Proofview.Goal.concl gl in
116 match Typing.sort_of env sigma c with
117 | exception e when CErrors.noncritical e ->
118 let _, info = Exninfo.capture e in
119 Tacticals.tclZEROMSG ~info (str "Not a proposition or a type.")
120 | sigma, sc ->
121 let r = ESorts.relevance_of_sort sc in
122 let needs_typing, sigma, f, glf =
123 match k with
124 | HaveTransp ->
125 let name = Context.make_annot Name.Anonymous r in
126 let sigma, p = Evarutil.new_evar env sigma c in
127 let sigma, f = Evarutil.new_evar env sigma (mkLetIn (name,p,c,Vars.lift 1 concl)) in
128 let gp = Proofview_monad.with_empty_state (fst @@ destEvar sigma p) in
129 let gf = Proofview_monad.with_empty_state (fst @@ destEvar sigma f) in
130 false, sigma, f, [gp;gf]
131 | _ ->
132 let sigma, sg = Typing.sort_of env sigma concl in
133 let qc,qg = ESorts.quality sigma sc, ESorts.quality sigma sg in
134 match qc, qg with
135 | Sorts.Quality.(QConstant QProp), Sorts.Quality.(QConstant QProp) ->
136 let f = Rocqlib.lib_ref ("plugins.ssreflect.ssr_have") in
137 let sigma, f = EConstr.fresh_global env sigma f in
138 let sigma, step = Evarutil.new_evar env sigma c in
139 let stepg = Proofview_monad.with_empty_state (fst @@ destEvar sigma step) in
140 let sigma, rest = Evarutil.new_evar env sigma (mkArrow c r concl) in
141 let restg = Proofview_monad.with_empty_state (fst @@ destEvar sigma rest) in
142 let glf = [stepg;restg] in
143 let f = EConstr.mkApp (f, [|c;concl;step;rest|]) in
144 false, sigma, f, glf
145 | _ ->
146 let f = Rocqlib.lib_ref ("plugins.ssreflect.ssr_have_upoly") in
147 let sigma, uc = match qc with
148 | QConstant (QSProp | QProp) -> sigma, Univ.Level.set
149 | _ -> Evd.new_univ_level_variable Evd.univ_flexible sigma in
150 let sigma, ug = match qg with
151 | QConstant (QSProp | QProp) -> sigma, Univ.Level.set
152 | _ -> Evd.new_univ_level_variable Evd.univ_flexible sigma in
153 let names = EInstance.make @@ UVars.Instance.of_array ([|qc;qg|],[|uc;ug|]) in
154 let sigma, f = EConstr.fresh_global env sigma ~names f in
155 let sigma, step = Evarutil.new_evar env sigma c in
156 let stepg = Proofview_monad.with_empty_state (fst @@ destEvar sigma step) in
157 let sigma, rest = Evarutil.new_evar env sigma (mkArrow c r concl) in
158 let restg = Proofview_monad.with_empty_state (fst @@ destEvar sigma rest) in
159 let glf = [stepg;restg] in
160 let f = EConstr.mkApp (f, [|c;concl;step;rest|]) in
161 true, sigma, f, glf in
162 Proofview.Unsafe.tclEVARS sigma <*>
163 (if needs_typing
164 then Ssrcommon.tacTYPEOF f >>= fun _ -> Tacticals.tclIDTAC
165 else Tacticals.tclIDTAC) >>= fun () ->
166 Tactics.eapply ~with_classes:false f
167 <*>
168 Proofview.Unsafe.tclGETGOALS >>= begin fun gl ->
169 match k with
170 | Suff ->
171 Proofview.Unsafe.tclSETGOALS (List.rev glf @ gl) <*>
172 Proofview.tclFOCUS 1 (List.length glf) Tactics.reduce_after_refine
173 | Have | HaveTransp ->
174 let ngoals = List.length gl + 1 in
175 Proofview.Unsafe.tclSETGOALS (gl @ glf) <*>
176 Proofview.tclFOCUS ngoals (ngoals + List.length glf - 1) Tactics.reduce_after_refine
177 end
178 end
179
180 let basesufftac t = basecuttac Suff t
181
182 let introstac ipats = tclIPAT ipats
183
184 let make_ct t =
185 let open CAst in
186 let mkt t = mk_term NoFlag t in
187 let mkl t = (NoFlag, (t, None)) in
188 match Ssrcommon.ssrterm_of_ast_closure_term t with
189 | _, (_, Some { loc; v = CCast (ct, Some DEFAULTcast, cty)}) ->
190 mkt ct, mkt cty, mkt (mkCHole None), loc
191 | _, (_, Some ct) ->
192 mkt ct, mkt (mkCHole None), mkt (mkCHole None), None
193 | _, (t, None) ->
194 begin match DAst.get t with
195 | GCast (ct, Some DEFAULTcast, cty) ->
196 mkl ct, mkl cty, mkl mkRHole, t.CAst.loc
197 | _ -> mkl t, mkl mkRHole, mkl mkRHole, None
198 end
199
200 (* FIXME: understand why we have to play with the goal states *)
201
202 let drop_state =
203 let map gl = Proofview.with_empty_state (Proofview.drop_state gl) in
204 Proofview.Unsafe.tclGETGOALS >>= fun gls ->
205 Proofview.Unsafe.tclSETGOALS (List.map map gls)
206
207 let set_state s =
208 let map gl = Proofview.goal_with_state (Proofview.drop_state gl) s in
209 Proofview.Unsafe.tclGETGOALS >>= fun gls ->
210 Proofview.Unsafe.tclSETGOALS (List.map map gls)
211
212 let assert_is_conv (ctx, concl) =
213 Proofview.Goal.enter begin fun gl ->
214 Proofview.tclORELSE (convert_concl ~check:true (EConstr.it_mkProd_or_LetIn concl ctx))
215 (fun _ -> Tacticals.tclZEROMSG (str "Given proof term is not of type " ++
216 pr_econstr_env (Proofview.Goal.env gl) (Proofview.Goal.sigma gl) (EConstr.mkArrow (EConstr.mkVar (Id.of_string "_")) ERelevance.relevant concl)))
217 end
218
219 let push_goals gs =
220 Proofview.Goal.enter begin fun gl ->
221 (* FIXME: do we really want to preserve state? *)
222 let gstate = Proofview.Goal.state gl in
223 let map ev = Proofview.goal_with_state ev gstate in
224 Proofview.Unsafe.tclSETGOALS (List.map map (gs @ [Proofview.Goal.goal gl]))
225 end
226
227 let havetac ist
228 (transp,((((clr, orig_pats), binders), simpl), (((fk, _), t), hint)))
229 suff namefst
230 =
231 let open Proofview.Notations in
232 Ssrcommon.tacMK_SSR_CONST "abstract_key" >>= fun abstract_key ->
233 Ssrcommon.tacMK_SSR_CONST "abstract" >>= fun abstract ->
234 Proofview.Goal.enter begin fun gl ->
235 let concl = Proofview.Goal.concl gl in
236 let gstate = Proofview.Goal.state gl in
237 let pats = tclCompileIPats orig_pats in
238 let binders = tclCompileIPats binders in
239 let simpl = tclCompileIPats simpl in
240 let skols, pats =
241 List.partition (function IOpAbstractVars _ -> true | _ -> false) pats in
242 let itac_mkabs = introstac skols in
243 let itac_c, clr =
244 match clr with
245 | None -> introstac pats, []
246 | Some clr -> introstac (tclCompileIPats (IPatClear clr :: orig_pats)), clr in
247 let itac, clr = introstac pats, cleartac clr in
248 let binderstac n =
249 let rec aux = function 0 -> [] | n -> IOpInaccessible None :: aux (n-1) in
250 Tacticals.tclTHEN (if binders <> [] then introstac (aux n) else Tacticals.tclIDTAC)
251 (introstac binders) in
252 let simpltac = introstac simpl in
253 let fixtc =
254 not !ssrhaveNOtcresolution &&
255 match fk with FwdHint(_,true) -> false | _ -> true in
256 let hint = hinttac ist true hint in
257 let cuttac t = basecuttac (if transp then HaveTransp else Have) t in
258 (* Introduce now abstract constants, so that everything sees them *)
259 let unlock_abs env (idty,args_id) sigma =
260 let sigma, _ = Typing.type_of env sigma idty in
261 unify_HO env sigma args_id.(2) abstract_key
262 in
263 drop_state <*>
264 Tacticals.tclTHENFIRST itac_mkabs (Proofview.Goal.enter begin fun gl ->
265 let env = Proofview.Goal.env gl in
266 let sigma = Proofview.Goal.sigma gl in
267 let interp sigma rtc t =
268 abs_ssrterm ~resolve_typeclasses:rtc ist env sigma t
269 in
270 let ct, cty, hole, loc = make_ct t in
271 let sigma, cut, itac1, itac2 =
272 match fk, namefst, suff with
273 | FwdHave, true, true ->
274 errorstrm (str"Suff have does not accept a proof term")
275 | FwdHave, false, true ->
276 let cty = combineCG cty hole (mkCArrow ?loc) mkRArrow in
277 let sigma, t, _ = interp sigma false (combineCG ct cty (mkCCast ?loc) mkRCast) in
278 let sigma, ty = Typing.type_of env sigma t in
279 let ctx, _ = EConstr.decompose_prod_n_decls sigma 1 ty in
280 sigma, ty, assert_is_conv (ctx, concl) <*> Tactics.apply t, itac_c
281 | FwdHave, false, false ->
282 let skols = List.flatten (List.map (function
283 | IOpAbstractVars ids -> ids
284 | _ -> assert false) skols) in
285 let skols_args =
286 List.map (fun id -> snd @@ (* FIXME: evar leak *)
287 Ssripats.Internal.examine_abstract env sigma (EConstr.mkVar id)) skols in
288 let sigma = List.fold_right (unlock_abs env) skols_args sigma in
289 let sigma, t, n_evars =
290 interp sigma false (combineCG ct cty (mkCCast ?loc) mkRCast) in
291 if skols <> [] && n_evars <> 0 then
292 CErrors.user_err (Pp.strbrk @@ "Automatic generalization of unresolved implicit "^
293 "arguments together with abstract variables is "^
294 "not supported");
295 let gs =
296 List.map (fun (_,a) ->
297 Ssripats.Internal.find_abstract_proof env sigma false a.(1)) skols_args in
298 let tacopen_skols = push_goals gs in
299 let sigma, ty = Typing.type_of env sigma t in
300 sigma, ty, Tactics.apply t,
301 itac_c <*> simpltac <*> tacopen_skols <*> unfold [abstract; abstract_key]
302 | _,true,true ->
303 let sigma, _, ty, _ = pf_interp_ty ~resolve_typeclasses:fixtc env sigma ist cty in
304 sigma, EConstr.mkArrow ty ERelevance.relevant concl, hint <*> itac, clr
305 | _,false,true ->
306 let sigma, _, ty, _ = pf_interp_ty ~resolve_typeclasses:fixtc env sigma ist cty in
307 sigma, EConstr.mkArrow ty ERelevance.relevant concl, hint, itac_c
308 | _, false, false ->
309 let sigma, n, cty, _ = pf_interp_ty ~resolve_typeclasses:fixtc env sigma ist cty in
310 sigma, cty, (binderstac n) <*> hint, Tacticals.tclTHEN itac_c simpltac
311 | _, true, false -> assert false in
312 Proofview.Unsafe.tclEVARS sigma <*>
313 Tacticals.tclTHENS (cuttac cut) [ itac1; itac2 ] end) <*>
314 set_state gstate
315 end
316
317 let destProd_or_LetIn sigma c =
318 match EConstr.kind sigma c with
319 | Prod (n,ty,c) -> RelDecl.LocalAssum (n, ty), c
320 | LetIn (n,bo,ty,c) -> RelDecl.LocalDef (n, bo, ty), c
321 | _ -> raise DestKO
322
323 let wlogtac ist (((clr0, pats),_),_) (gens, ((_, ct))) hint suff ghave =
324 Proofview.Goal.enter begin fun gl ->
325 let env = Proofview.Goal.env gl in
326 let sigma = Proofview.Goal.sigma gl in
327 let concl = Proofview.Goal.concl gl in
328 let clr0 = Option.default [] clr0 in
329 let pats = tclCompileIPats pats in
330 let mkclr gen clrs = clr_of_wgen gen clrs in
331 let mkpats = function
332 | _, Some ((x, _), _) -> fun pats -> IOpId (hoi_id x) :: pats
333 | _ -> fun x -> x in
334 let ct = match Ssrcommon.ssrterm_of_ast_closure_term ct with
335 | (a, (b, Some ct)) ->
336 begin match ct.CAst.v with
337 | CCast (_, Some DEFAULTcast, cty) -> a, (b, Some cty)
338 | _ -> anomaly "wlog: ssr cast hole deleted by typecheck"
339 end
340 | (a, (t, None)) ->
341 begin match DAst.get t with
342 | GCast (_, Some DEFAULTcast, cty) -> a, (cty, None)
343 | _ -> anomaly "wlog: ssr cast hole deleted by typecheck"
344 end
345 in
346 let cut_implies_goal = not (suff || ghave <> `NoGen) in
347 let c, args, ct, sigma =
348 let gens = List.filter (function _, Some _ -> true | _ -> false) gens in
349 let c = EConstr.mkProp in
350 let c = if cut_implies_goal then EConstr.mkArrow c ERelevance.relevant concl else c in
351 let mkabs gen (sigma, args, c) =
352 abs_wgen env sigma false (fun x -> x) gen (args, c)
353 in
354 let sigma, args, c = List.fold_right mkabs gens (sigma, [], c) in
355 let env, _ =
356 List.fold_left (fun (env, c) _ ->
357 let rd, c = destProd_or_LetIn sigma c in
358 EConstr.push_rel rd env, c) (env, c) gens in
359 let sigma, _, ct, _ = pf_interp_ty env sigma ist ct in
360 let rec var2rel c g s = match EConstr.kind sigma c, g with
361 | Prod({binder_name=Anonymous} as x,_,c), [] -> EConstr.mkProd(x, EConstr.Vars.subst_vars sigma s ct, c)
362 | Sort _, [] -> EConstr.Vars.subst_vars sigma s ct
363 | LetIn({binder_name=Name id} as n,b,ty,c), _::g -> EConstr.mkLetIn (n,b,ty,var2rel c g (id::s))
364 | Prod({binder_name=Name id} as n,ty,c), _::g -> EConstr.mkProd (n,ty,var2rel c g (id::s))
365 | _ -> CErrors.anomaly(str"SSR: wlog: var2rel: " ++ pr_econstr_env env sigma c) in
366 let c = var2rel c gens [] in
367 let rec pired c = function
368 | [] -> c
369 | t::ts as args -> match EConstr.kind sigma c with
370 | Prod(_,_,c) -> pired (EConstr.Vars.subst1 t c) ts
371 | LetIn(id,b,ty,c) -> EConstr.mkLetIn (id,b,ty,pired c args)
372 | _ -> CErrors.anomaly(str"SSR: wlog: pired: " ++ pr_econstr_env env sigma c) in
373 c, args, pired c args, sigma
374 in
375 let tacipat pats = introstac pats in
376 let tacigens =
377 Tacticals.tclTHEN
378 (Tacticals.tclTHENLIST(List.rev(List.fold_right mkclr gens [cleartac clr0])))
379 (introstac (List.fold_right mkpats gens [])) in
380 let hinttac = hinttac ist true hint in
381 let cut_kind, fst_goal_tac, snd_goal_tac =
382 match suff, ghave with
383 | true, `NoGen -> Suff, Tacticals.tclTHEN hinttac (tacipat pats), tacigens
384 | false, `NoGen -> Suff, hinttac, Tacticals.tclTHEN tacigens (tacipat pats)
385 | true, `Gen _ -> assert false
386 | false, `Gen id ->
387 if gens = [] then errorstrm(str"gen have requires some generalizations");
388 let clear0 = cleartac clr0 in
389 let id, name_general_hyp, cleanup, pats = match id, pats with
390 | None, (IOpId id as ip)::pats -> Some id, tacipat [ip], clear0, pats
391 | None, _ -> None, Tacticals.tclIDTAC, clear0, pats
392 | Some (Some id),_ -> Some id, introid id, clear0, pats
393 | Some _,_ ->
394 let id = mk_anon_id "tmp" (Environ.named_context_val env) in
395 Some id, introid id, Tacticals.tclTHEN clear0 (Tactics.clear [id]), pats in
396 let tac_specialize = match id with
397 | None -> Tacticals.tclIDTAC
398 | Some id ->
399 if pats = [] then Tacticals.tclIDTAC else
400 let args = Array.of_list args in
401 debug_ssr (fun () -> str"specialized="++ pr_econstr_env env sigma EConstr.(mkApp (mkVar id,args)));
402 debug_ssr (fun () -> str"specialized_ty="++ pr_econstr_env env sigma ct);
403 Tacticals.tclTHENS (basecuttac Have ct)
404 [Tactics.apply EConstr.(mkApp (mkVar id,args)); Tacticals.tclIDTAC] in
405 Have,
406 (if hint = nohint then tacigens else hinttac),
407 Tacticals.tclTHENLIST [name_general_hyp; tac_specialize; tacipat pats; cleanup]
408 in
409 Proofview.Unsafe.tclEVARS sigma <*>
410 Tacticals.tclTHENS (basecuttac cut_kind c) [fst_goal_tac; snd_goal_tac]
411 end
412
413 (** The "suffice" tactic *)
414
415 open Proofview.Notations
416
417 let sufftac ist ((((clr, pats),binders),simpl), ((_, c), hint)) =
418 let clr = Option.default [] clr in
419 let pats = tclCompileIPats pats in
420 let binders = tclCompileIPats binders in
421 let simpl = tclCompileIPats simpl in
422 let htac = Tacticals.tclTHEN (introstac pats) (hinttac ist true hint) in
423 let c = match Ssrcommon.ssrterm_of_ast_closure_term c with
424 | (a, (b, Some ct)) ->
425 begin match ct.CAst.v with
426 | CCast (_, Some DEFAULTcast, cty) -> a, (b, Some cty)
427 | _ -> anomaly "suff: ssr cast hole deleted by typecheck"
428 end
429 | (a, (t, None)) ->
430 begin match DAst.get t with
431 | GCast (_, Some DEFAULTcast, cty) -> a, (cty, None)
432 | _ -> anomaly "suff: ssr cast hole deleted by typecheck"
433 end
434 in
435 let ctac =
436 Proofview.Goal.enter begin fun gl ->
437 let sigma = Proofview.Goal.sigma gl in
438 let env = Proofview.Goal.env gl in
439 let sigma, _, ty, _ = pf_interp_ty env sigma ist c in
440 Proofview.Unsafe.tclEVARS sigma <*> basesufftac ty
441 end in
442 Tacticals.tclTHENS ctac [htac; Tacticals.tclTHEN (cleartac clr) (introstac (binders@simpl))]
443
444 let is_app_evar sigma t =
445 match EConstr.kind sigma t with
446 | Constr.Evar _ -> true
447 | Constr.App(t,_) ->
448 begin match EConstr.kind sigma t with
449 | Constr.Evar _ -> true
450 | _ -> false end
451 | _ -> false
452
453 let rec ncons n e = match n with
454 | 0 -> []
455 | n when n > 0 -> e :: ncons (n - 1) e
456 | _ -> failwith "ncons"
457
458 let intro_lock ipats =
459 let hnf' = Proofview.numgoals >>= fun ng ->
460 Proofview.tclDISPATCH
461 (ncons (ng - 1) ssrsmovetac @ [Proofview.tclUNIT ()]) in
462 let protect_subgoal env sigma hd args =
463 Ssrcommon.tacMK_SSR_CONST "Under_rel" >>= fun under_rel ->
464 Ssrcommon.tacMK_SSR_CONST "Under_rel_from_rel" >>= fun under_from_rel ->
465 Tactics.refine ~typecheck:true (fun sigma ->
466 let lm2 = Array.length args - 2 in
467 let sigma, carrier =
468 Typing.type_of env sigma args.(lm2) in
469 let rel = EConstr.mkApp (hd, Array.sub args 0 lm2) in
470 let rel_args = Array.sub args lm2 2 in
471 let under_rel_args = Array.append [|carrier; rel|] rel_args in
472 let ty = EConstr.mkApp (under_rel, under_rel_args) in
473 let sigma, t = Evarutil.new_evar env sigma ty in
474 sigma, EConstr.mkApp(under_from_rel,Array.append under_rel_args [|t|])) in
475 let rec lock_eq () : unit Proofview.tactic = Proofview.Goal.enter begin fun _ ->
476 Proofview.tclORELSE
477 (Ssripats.tclIPAT [Ssripats.IOpTemporay; Ssripats.IOpEqGen (lock_eq ())])
478 (fun _exn -> Proofview.Goal.enter begin fun gl ->
479 let c = Proofview.Goal.concl gl in
480 let sigma = Proofview.Goal.sigma gl in
481 let env = Proofview.Goal.env gl in
482 let open EConstr in
483 match kind_of_type sigma c with
484 | AtomicType(hd, args) when
485 Array.length args >= 2 && is_app_evar sigma (Array.last args) &&
486 Ssrequality.ssr_is_setoid env sigma hd args
487 (* if the last condition above [ssr_is_setoid ...] holds
488 then [Corelib.Classes.RelationClasses] has been required *)
489 ||
490 (* if this is not the case, the tactic can still succeed
491 when the considered relation is [Corelib.Init.Logic.iff] *)
492 Ssrcommon.is_const_ref env sigma hd (Rocqlib.lib_ref "core.iff.type") &&
493 Array.length args = 2 && is_app_evar sigma args.(1) ->
494 protect_subgoal env sigma hd args
495 | _ ->
496 let t = Reductionops.whd_all env sigma c in
497 match kind_of_type sigma t with
498 | AtomicType(hd, args) when
499 Ssrcommon.is_ind_ref env sigma hd (Rocqlib.lib_ref "core.eq.type") &&
500 Array.length args = 3 && is_app_evar sigma args.(2) ->
501 protect_subgoal env sigma hd args
502 | _ ->
503 debug_ssr (fun () -> Pp.(str"under: stop:" ++ pr_econstr_env env sigma t));
504
505 Proofview.tclUNIT ()
506 end)
507 end
508 in
509 hnf' <*> Ssripats.tclIPATssr ipats <*> lock_eq ()
510
511 let pretty_rename evar_map term varnames =
512 let rec aux term vars =
513 try
514 match vars with
515 | [] -> term
516 | Names.Name.Anonymous :: varnames ->
517 let name, types, body = EConstr.destLambda evar_map term in
518 let res = aux body varnames in
519 EConstr.mkLambda (name, types, res)
520 | Names.Name.Name _ as name :: varnames ->
521 let { Context.binder_relevance = r }, types, body =
522 EConstr.destLambda evar_map term in
523 let res = aux body varnames in
524 EConstr.mkLambda (Context.make_annot name r, types, res)
525 with DestKO -> term
526 in
527 aux term varnames
528
529 let overtac = ssr_n_tac "over" ~-1
530
531 let check_numgoals ?(minus = 0) nh =
532 Proofview.numgoals >>= fun ng ->
533 if nh <> ng then
534 let errmsg =
535 str"Incorrect number of tactics" ++ spc() ++
536 str"(expected "++int (ng - minus)++str(String.plural ng " tactic") ++
537 str", was given "++ int (nh - minus)++str")."
538 in
539 CErrors.user_err errmsg
540 else
541 Proofview.tclUNIT ()
542
543 let undertac ?(pad_intro = false) ist ipats ((dir,_),_ as rule) hint =
544
545 (* total number of implied hints *)
546 let nh = List.length (snd hint) + (if hint = nullhint then 2 else 1) in
547
548 let varnames =
549 let rec aux acc = function
550 | IPatId id :: rest -> aux (Names.Name.Name id :: acc) rest
551 | IPatClear _ :: rest -> aux acc rest
552 | IPatSimpl _ :: rest -> aux acc rest
553 | IPatAnon (One _ | Drop) :: rest ->
554 aux (Names.Name.Anonymous :: acc) rest
555 | _ -> List.rev acc in
556 aux [] @@ match ipats with
557 | None -> []
558 | Some (IPatCase(Regular (l :: _)) :: _) -> l
559 | Some l -> l in
560
561 (* If we find a "=> [|]" we add 1 | to get "=> [||]" for the extra
562 * goal (the one that is left once we run over) *)
563 let ipats =
564 match ipats with
565 | None -> [IPatNoop]
566 | Some l when pad_intro -> (* typically, ipats = Some [IPatAnon All] *)
567 let new_l = ncons (nh - 1) l in
568 [IPatCase(Regular (new_l @ [[]]))]
569 | Some (IPatCase(Regular []) :: _ as ipats) -> ipats
570 (* Erik: is the previous line correct/useful? *)
571 | Some (IPatCase(Regular l) :: rest) -> IPatCase(Regular(l @ [[]])) :: rest
572 | Some (IPatCase(Block _) :: _ as l) -> l
573 | Some l -> [IPatCase(Regular [l;[]])] in
574
575 let map_redex env evar_map ~before:_ ~after:t =
576 debug_ssr (fun () -> Pp.(str"under vars: " ++ prlist Names.Name.print varnames));
577
578 let evar_map, ty = Typing.type_of env evar_map t in
579 let new_t = (* pretty-rename the bound variables *)
580 try begin match EConstr.destApp evar_map t with (f, ar) ->
581 let lam = Array.last ar in
582 debug_ssr(fun () -> Pp.(str"under: mapping:" ++
583 pr_econstr_env env evar_map lam));
584 let new_lam = pretty_rename evar_map lam varnames in
585 let new_ar, len1 = Array.copy ar, pred (Array.length ar) in
586 new_ar.(len1) <- new_lam;
587 EConstr.mkApp (f, new_ar)
588 end with
589 | DestKO ->
590 debug_ssr (fun () -> Pp.(str"under: cannot pretty-rename bound variables with destApp"));
591 t
592 in
593 debug_ssr (fun () -> Pp.(str"under: to:" ++ pr_econstr_env env evar_map new_t));
594 evar_map, new_t
595 in
596 let undertacs =
597 if hint = nohint then
598 Proofview.tclUNIT ()
599 else
600 let betaiota = Tactics.reduct_in_concl ~cast:false ~check:false
601 (Reductionops.nf_betaiota, DEFAULTcast)
602 in
603 (* Usefulness of check_numgoals: tclDISPATCH would be enough,
604 except for the error message w.r.t. the number of
605 provided/expected tactics, as the last one is implied *)
606 check_numgoals ~minus:1 nh <*>
607 Proofview.tclDISPATCH
608 ((List.map (function None -> overtac
609 | Some e -> ssrevaltac ist e <*>
610 overtac)
611 (if hint = nullhint then [None] else snd hint))
612 @ [betaiota])
613 in
614 let rew =
615 Ssrequality.ssrrewritetac ~under:true ~map_redex ist [rule]
616 in
617 rew <*> intro_lock ipats <*> undertacs