Debian Codegraph @

tactics/abstract.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 Termops
13 open EConstr
14
15 module NamedDecl = Context.Named.Declaration
16
17 (* tactical to save as name a subproof such that the generalisation of
18 the current goal, abstracted with respect to the local signature,
19 is solved by tac *)
20
21 (** d1 is the section variable in the global context, d2 in the goal context *)
22 let interpretable_as_section_decl env sigma d1 d2 =
23 let open Context.Named.Declaration in
24 let e_eq_constr_univs sigma c1 c2 = match eq_constr_universes env sigma c1 c2 with
25 | None -> false
26 | Some cstr ->
27 try
28 let _sigma = Evd.add_constraints sigma cstr in
29 true
30 with UGraph.UniverseInconsistency _ | UState.UniversesDiffer -> false
31 in
32 match d2, d1 with
33 | LocalDef _, LocalAssum _ -> false
34 | LocalDef (_,b1,t1), LocalDef (_,b2,t2) ->
35 e_eq_constr_univs sigma b1 b2 && e_eq_constr_univs sigma t1 t2
36 | LocalAssum (_,t1), d2 -> e_eq_constr_univs sigma t1 (NamedDecl.get_type d2)
37
38 let name_op_to_name ~name_op ~name suffix =
39 match name_op with
40 | Some s -> s
41 | None -> Nameops.add_suffix name suffix
42
43 let cache_term_by_tactic_then ~opaque ~name_op ?(goal_type=None) tac tacK =
44 let open Tacticals in
45 let open Proofview.Notations in
46 Proofview.tclProofInfo [@ocaml.warning "-3"] >>= fun (name, poly) ->
47 let suffix = if opaque then "_subproof" else "_subterm" in
48 let name = name_op_to_name ~name_op ~name suffix in
49 Proofview.Goal.enter begin fun gl ->
50 let env = Proofview.Goal.env gl in
51 let sigma = Proofview.Goal.sigma gl in
52 (* XXX we cannot use [tclENV] here because toplevel callers may pass wrong
53 named contexts when nesting abstracts. See #5641. *)
54 let section_sign = Global.named_context_val () in
55 let goal_sign = Proofview.Goal.hyps gl in
56 let sign,secsign =
57 List.fold_right
58 (fun d (s1,s2) ->
59 let id = NamedDecl.get_id d in
60 if mem_named_context_val id section_sign &&
61 interpretable_as_section_decl env sigma (lookup_named_val id section_sign) d
62 then (s1,push_named_context_val d s2)
63 else (Context.Named.add d s1,s2))
64 goal_sign (Context.Named.empty, Environ.empty_named_context_val)
65 in
66 let bad id = match lookup_named_val id section_sign with
67 | (_ : named_declaration) -> true
68 | exception Not_found ->
69 Evd.seff_mem_label id (Evd.eval_side_effects sigma) ||
70 (* The local environment is OK when it comes to constants though,
71 including those defined by [tclABSTRACT]. *)
72 let kn = Lib.make_kn id in
73 let kn = Names.Constant.make1 kn in
74 Environ.mem_constant kn env
75 in
76 let name = Namegen.next_ident_away_from name bad in
77 let concl = match goal_type with
78 | None -> Proofview.Goal.concl gl
79 | Some ty -> ty
80 in
81 let concl = it_mkNamedProd_or_LetIn sigma concl sign in
82 let solve_tac = tclCOMPLETE
83 (Tactics.intros_mustbe_force (List.rev_map NamedDecl.get_id sign) <*>
84 tac)
85 in
86 let sigma, lem, args, safe =
87 Subproof.declare_abstract ~name ~poly ~sign ~secsign ~opaque ~solve_tac env sigma concl
88 in
89 let pose_tac = match name_op with
90 | None -> Proofview.tclUNIT ()
91 | Some id ->
92 if opaque then Tactics.pose_proof (Names.Name id) lem
93 else Tactics.pose_tac (Names.Name id) lem
94 in
95 let mark = if not safe then Proofview.mark_as_unsafe else tclIDTAC in
96 tclTHENLIST [
97 Proofview.Unsafe.tclEVARS sigma;
98 mark;
99 pose_tac;
100 tacK lem args;
101 ];
102 end
103
104 let abstract_subproof ~opaque tac =
105 cache_term_by_tactic_then ~opaque tac (fun lem args -> Tactics.exact_no_check (applist (lem, args)))
106
107 let tclABSTRACT ?(opaque=true) name_op tac =
108 abstract_subproof ~opaque ~name_op tac