Debian Codegraph @

proofs/subproof.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
14 module NamedDecl = Context.Named.Declaration
15
16 (**********************************************************************)
17 (* Shortcut to build a term using tactics *)
18
19 let refine_by_tactic ~name ~poly env sigma ty tac =
20 (* Save the initial side-effects to restore them afterwards. *)
21 let eff = Evd.eval_side_effects sigma in
22 let old_len = Safe_typing.length_private @@ Evd.seff_private eff in
23 (* Save the existing goals *)
24 let sigma = Evd.push_future_goals sigma in
25 (* Start a proof *)
26 let prf = Proof.start ~name ~poly sigma [env, ty] in
27 let (prf, _, ()) =
28 try Proof.run_tactic env tac prf
29 with Logic_monad.TacticFailure e as src ->
30 (* Catch the inner error of the monad tactic *)
31 let (_, info) = Exninfo.capture src in
32 Exninfo.iraise (e, info)
33 in
34 (* Plug back the retrieved sigma *)
35 let Proof.{ goals; stack; sigma; entry } = Proof.data prf in
36 let () = assert (stack = []) in
37 let ans = match Proofview.initial_goals entry with
38 | [_, c, _] -> c
39 | _ -> assert false
40 in
41 let ans = EConstr.to_constr ~abort_on_undefined_evars:false sigma ans in
42 (* [neff] contains the freshly generated side-effects *)
43 let neff = Evd.seff_private @@ Evd.eval_side_effects sigma in
44 let new_len = Safe_typing.length_private neff in
45 let neff, _ = Safe_typing.pop_private neff (new_len - old_len) in
46 (* Reset the old side-effects *)
47 let sigma = Evd.set_side_effects eff sigma in
48 (* Restore former goals *)
49 let _goals, sigma = Evd.pop_future_goals sigma in
50 (* Push remaining goals as future_goals which is the only way we
51 have to inform the caller that there are goals to collect while
52 not being encapsulated in the monad *)
53 let sigma = List.fold_right Evd.declare_future_goal goals sigma in
54 (* Get rid of the fresh side-effects by internalizing them in the term
55 itself. Note that this is unsound, because the tactic may have solved
56 other goals that were already present during its invocation, so that
57 those goals rely on effects that are not present anymore. Hopefully,
58 this hack will work in most cases. *)
59 let (ans, uctx) = Safe_typing.inline_private_constants env ((ans, Univ.ContextSet.empty), neff) in
60 let sigma = Evd.merge_universe_context_set ~sideff:true UState.UnivRigid sigma uctx in
61 EConstr.of_constr ans, sigma
62
63 (* Abstract internals *)
64
65 exception OpenProof
66
67 let () = CErrors.register_handler begin function
68 | OpenProof ->
69 let open Pp in
70 Some (str "Attempt to save an incomplete proof.")
71 | _ -> None
72 end
73
74 let rec shrink ctx sign c t accu =
75 let open Constr in
76 let open Vars in
77 match ctx, sign with
78 | [], [] -> (c, t, accu)
79 | p :: ctx, decl :: sign ->
80 if noccurn 1 c && noccurn 1 t then
81 let c = subst1 mkProp c in
82 let t = subst1 mkProp t in
83 shrink ctx sign c t accu
84 else
85 let c = Term.mkLambda_or_LetIn p c in
86 let t = Term.mkProd_or_LetIn p t in
87 let accu = if Context.Rel.Declaration.is_local_assum p
88 then EConstr.mkVar (NamedDecl.get_id decl) :: accu
89 else accu
90 in
91 shrink ctx sign c t accu
92 | _ -> assert false
93
94 (* If [sign] is [x1:T1..xn:Tn], [c] is [fun x1:T1..xn:Tn => c']
95 and [t] is [forall x1:T1..xn:Tn, t'], returns a new [c'] and [t'],
96 where all non-dependent [xi] are removed, as well as a
97 restriction [args] of [x1..xn] such that [c' args] = [c x1..xn] *)
98 let shrink_entry sign body typ =
99 let (ctx, body, typ) = Term.decompose_lambda_prod_n_decls (List.length sign) body typ in
100 let (body, typ, args) = shrink ctx sign body typ [] in
101 body, typ, args
102
103 let build_constant_by_tactic ~name ~sigma ~env ~sign ~poly typ tac =
104 let pfenv = Environ.reset_with_named_context sign env in
105 let proof = Proof.start ~name ~poly sigma [pfenv, typ] in
106 let proof, status = Proof.solve env (Goal_select.select_nth 1) None tac proof in
107 let (body, typ, output_ustate) =
108 let Proof.{ entry; sigma = evd } = Proof.data proof in
109 let body, typ = match Proofview.initial_goals entry with
110 | [_, body, typ] -> body, typ
111 | _ -> assert false
112 in
113 let () = if not @@ Proof.is_done proof then raise OpenProof in
114 let evd = Evd.minimize_universes evd in
115 let to_constr c = match EConstr.to_constr_opt evd c with
116 | Some p -> p
117 | None -> raise OpenProof
118 in
119 let body = to_constr body in
120 let typ = to_constr typ in
121 (body, typ, Evd.ustate evd)
122 in
123 let univs =
124 let used_univs = Vars.universes_of_constr typ in
125 let used_univs = Vars.universes_of_constr body ~init:used_univs in
126 let uctx = UState.restrict output_ustate used_univs in
127 UState.check_univ_decl ~poly uctx UState.default_univ_decl
128 in
129 (* FIXME: return the locally introduced effects *)
130 let { Proof.sigma } = Proof.data proof in
131 let sigma = Evd.set_universe_context sigma output_ustate in
132 (univs, body, typ), status, sigma
133
134 let build_by_tactic env ~uctx ~poly ~typ tac =
135 let name = Id.of_string "temporary_proof" in
136 let sign = Environ.(val_of_named_context (named_context env)) in
137 let sigma = Evd.from_ctx uctx in
138 let (univs, body, typ), status, sigma = build_constant_by_tactic ~name ~env ~sigma ~sign ~poly typ tac in
139 let uctx = Evd.ustate sigma in
140 (* ignore side effect universes:
141 we don't reset the global env in this code path so the side effects are still present
142 cf #13271 and discussion in #18874
143 (but due to #13324 we still want to inline them) *)
144 let effs = Evd.seff_private @@ Evd.eval_side_effects sigma in
145 let body, ctx = Safe_typing.inline_private_constants env ((body, Univ.ContextSet.empty), effs) in
146 let _uctx = UState.merge_universe_context ~sideff:true Evd.univ_rigid uctx ctx in
147 body, typ, univs, status, uctx
148
149 let build_by_tactic_opt env ~uctx ~poly ~typ tac =
150 try Some (build_by_tactic env ~uctx ~poly ~typ tac)
151 with OpenProof -> None
152
153 let extract_monomorphic = function
154 | UState.Monomorphic_entry ctx ->
155 Entries.Monomorphic_entry, ctx
156 | UState.Polymorphic_entry uctx -> Entries.Polymorphic_entry uctx, Univ.ContextSet.empty
157
158 let declare_abstract ~name ~poly ~sign ~secsign ~opaque ~solve_tac env sigma concl =
159 let (const, safe, sigma') =
160 try build_constant_by_tactic ~name ~poly ~env ~sigma ~sign:secsign concl solve_tac
161 with Logic_monad.TacticFailure e as src ->
162 (* if the tactic [tac] fails, it reports a [TacticFailure e],
163 which is an error irrelevant to the proof system (in fact it
164 means that [e] comes from [tac] failing to yield enough
165 success). Hence it reraises [e]. *)
166 let (_, info) = Exninfo.capture src in
167 Exninfo.iraise (e, info)
168 in
169 let (univs, body, typ) = const in
170 let sigma = Evd.drop_new_defined ~original:sigma sigma' in
171 let body, typ, args = shrink_entry sign body typ in
172 let ts = Environ.oracle env in
173 let cst, effs =
174 (* No side-effects in the entry, they already exist in the ambient environment *)
175 let effs = Evd.eval_side_effects sigma in
176 let de, ctx =
177 let univ_entry, ctx = extract_monomorphic (fst univs) in
178 if not opaque then
179 Safe_typing.DefinitionEff { Entries.definition_entry_body = body;
180 definition_entry_secctx = None;
181 definition_entry_type = Some typ;
182 definition_entry_universes = univ_entry;
183 definition_entry_inline_code = false;
184 }, ctx
185 else
186 let secctx =
187 let env = Global.env () in
188 let hyps =
189 if List.is_empty (Environ.named_context env) then Id.Set.empty
190 else
191 let ids_typ = Environ.global_vars_set env typ in
192 let vars = Environ.global_vars_set env body in
193 Id.Set.union ids_typ vars
194 in
195 Environ.really_needed env hyps
196 in
197 Safe_typing.OpaqueEff { Entries.opaque_entry_body = body;
198 opaque_entry_secctx = secctx;
199 opaque_entry_type = typ;
200 opaque_entry_universes = univ_entry;
201 },
202 ctx
203 in
204 Evd.push_side_effects ~ts name de ctx effs
205 in
206 let sigma = Evd.emit_side_effects effs sigma in
207 let inst = match univs with
208 | UState.Monomorphic_entry _, _ -> UVars.Instance.empty
209 | UState.Polymorphic_entry uctx, _ -> UVars.UContext.instance uctx
210 in
211 let lem = EConstr.of_constr (Constr.mkConstU (cst, inst)) in
212 sigma, lem, args, safe