Debian Codegraph @

proofs/proof.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 (* Module defining the last essential tiles of interactive proofs.
12 A proof deals with the focusing commands (including the braces and bullets),
13 the shelf (see the [shelve] tactic) and given up goal (see the [give_up]
14 tactic). A proof is made of the following:
15 - Proofview: a proof is primarily the data of the current view.
16 That which is shown to the user (as a remainder, a proofview
17 is mainly the logical state of the proof, together with the
18 currently focused goals).
19 - Focus: a proof has a focus stack: the top of the stack contains
20 the context in which to unfocus the current view to a view focused
21 with the rest of the stack.
22 In addition, this contains, for each of the focus context, a
23 "focus kind" and a "focus condition" (in practice, and for modularity,
24 the focus kind is actually stored inside the condition). To unfocus, one
25 needs to know the focus kind, and the condition (for instance "no condition" or
26 the proof under focused must be complete) must be met.
27 - Given up goals: as long as there is a given up goal, the proof is not completed.
28 Given up goals cannot be retrieved, the user must go back where the tactic
29 [give_up] was run and solve the goal there.
30 *)
31
32 open Util
33
34 module FocusKind = Dyn.Make()
35
36 type 'a focus_kind = 'a FocusKind.tag
37 type reason = NotThisWay | AlreadyNoFocus
38 type unfocusable =
39 | Cannot of reason
40 | Loose
41 | Strict
42 type 'a focus_condition =
43 | CondNo of bool * 'a focus_kind
44 | CondDone of bool * 'a focus_kind
45 | CondEndStack of 'a focus_kind (* loose_end is false here *)
46
47 let new_focus_kind = FocusKind.create
48
49 (* To be authorized to unfocus one must meet the condition prescribed by
50 the action which focused.*)
51 (* spiwack: we could consider having a list of authorized focus_kind instead
52 of just one, if anyone needs it *)
53
54 exception CannotUnfocusThisWay
55
56 (* Cannot focus on non-existing subgoals *)
57 exception NoSuchGoals of int * int
58
59 exception NoSuchGoal of Libnames.qualid option
60
61 exception FullyUnfocused
62
63 let _ = CErrors.register_handler begin function
64 | CannotUnfocusThisWay ->
65 Some (Pp.str "This proof is focused, but cannot be unfocused this way")
66 | NoSuchGoals (i,j) when Int.equal i j ->
67 Some Pp.(str "[Focus] No such goal (" ++ int i ++ str").")
68 | NoSuchGoals (i,j) ->
69 Some Pp.(str "[Focus] Not every goal in range ["++ int i ++ str","++int j++str"] exist.")
70 | NoSuchGoal (Some id) ->
71 Some Pp.(str "[Focus] No such goal: " ++ Libnames.pr_qualid id ++ str ".")
72 | NoSuchGoal None ->
73 Some Pp.(str "[Focus] No such goal.")
74 | FullyUnfocused ->
75 Some (Pp.str "The proof is not focused")
76 | _ -> None
77 end
78
79 let check_cond_kind c k =
80 let kind_of_cond = function
81 | CondNo (_,k) | CondDone(_,k) | CondEndStack k -> k in
82 FocusKind.eq (kind_of_cond c) k
83
84 let equal_kind c k = match FocusKind.eq c k with
85 | None -> false
86 | Some _ -> true
87
88 let test_cond c k1 pw =
89 match c with
90 | CondNo(_, k) when equal_kind k k1 -> Strict
91 | CondNo(true, _) -> Loose
92 | CondNo(false, _) -> Cannot NotThisWay
93 | CondDone(_, k) when equal_kind k k1 && Proofview.finished pw -> Strict
94 | CondDone(true, _) -> Loose
95 | CondDone(false, _) -> Cannot NotThisWay
96 | CondEndStack k when equal_kind k k1 -> Strict
97 | CondEndStack _ -> Cannot AlreadyNoFocus
98
99 let no_cond ?(loose_end=false) k = CondNo (loose_end, k)
100 let done_cond ?(loose_end=false) k = CondDone (loose_end,k)
101
102 type focus_element = FocusElt : 'a focus_condition * 'a * Proofview.focus_context -> focus_element
103
104 (* Subpart of the type of proofs. It contains the parts of the proof which
105 are under control of the undo mechanism *)
106 type t =
107 { proofview: Proofview.proofview
108 (** Current focused proofview *)
109 ; entry : Proofview.entry
110 (** Entry for the proofview *)
111 ; focus_stack: focus_element list
112 (** History of the focusings, provides information on how to unfocus
113 the proof and the extra information stored while focusing. The
114 list is empty when the proof is fully unfocused. *)
115 ; name : Names.Id.t
116 (** the name of the theorem whose proof is being constructed *)
117 ; poly : PolyFlags.t
118 (** Universe polymorphism *)
119 ; typing_flags : Declarations.typing_flags option
120 }
121
122 (*** General proof functions ***)
123
124 let proof p =
125 let (goals,sigma) = Proofview.proofview p.proofview in
126 (* spiwack: beware, the bottom of the stack is used by [Proof]
127 internally, and should not be exposed. *)
128 let rec map_minus_one f = function
129 | [] -> assert false
130 | [_] -> []
131 | a::l -> f a :: (map_minus_one f l)
132 in
133 let map (FocusElt (_, _, c)) = Proofview.focus_context sigma c in
134 let stack = map_minus_one map p.focus_stack in
135 (goals,stack,sigma)
136
137 let rec unroll_focus pv = function
138 | FocusElt (_,_,ctx)::stk -> unroll_focus (Proofview.unfocus ctx pv) stk
139 | [] -> pv
140
141 (* spiwack: a proof is considered completed even if its still focused, if the focus
142 doesn't hide any goal.
143 Unfocusing is handled in {!return}. *)
144 let is_done p =
145 Proofview.finished p.proofview &&
146 Proofview.finished (unroll_focus p.proofview p.focus_stack)
147
148 (* Returns the list of partial proofs to initial goals *)
149 let partial_proof p = Proofview.partial_proof p.entry p.proofview
150
151 (*** The following functions implement the basic internal mechanisms
152 of proofs, they are not meant to be exported in the .mli ***)
153
154 (* An auxiliary function to push a {!focus_context} on the focus stack. *)
155 let push_focus cond inf context pr =
156 { pr with focus_stack = FocusElt(cond,inf,context)::pr.focus_stack }
157
158 type any_focus_condition = AnyFocusCond : 'a focus_condition -> any_focus_condition
159
160 (* An auxiliary function to read the kind of the next focusing step *)
161 let cond_of_focus pr =
162 match pr.focus_stack with
163 | FocusElt (cond,_,_)::_ -> AnyFocusCond cond
164 | _ -> raise FullyUnfocused
165
166 (* An auxiliary function to pop and read the last {!Proofview.focus_context}
167 on the focus stack. *)
168 let pop_focus pr =
169 match pr.focus_stack with
170 | focus::other_focuses ->
171 { pr with focus_stack = other_focuses }, focus
172 | _ ->
173 raise FullyUnfocused
174
175 (* This function focuses the proof [pr] between indices [i] and [j] *)
176 let _focus cond inf i j pr =
177 let focused, context = Proofview.focus i j pr.proofview in
178 let pr = push_focus cond inf context pr in
179 { pr with proofview = focused }
180
181 (* This function unfocuses the proof [pr], it raises [FullyUnfocused],
182 if the proof is already fully unfocused.
183 This function does not care about the condition of the current focus. *)
184 let _unfocus pr =
185 let pr, FocusElt (_,_,fc) = pop_focus pr in
186 { pr with proofview = Proofview.unfocus fc pr.proofview }
187
188 (* Focus command (focuses on the [i]th subgoal) *)
189 (* spiwack: there could also, easily be a focus-on-a-range tactic, is there
190 a need for it? *)
191 let focus cond inf i pr =
192 try _focus cond inf i i pr
193 with CList.IndexOutOfRange -> raise (NoSuchGoals (i,i))
194
195 (* Focus on the goal named id *)
196 let focus_id cond inf id pr =
197 let (focused_goals, evar_map) = Proofview.proofview pr.proofview in
198 begin match try Some (Evd.evar_key id evar_map) with Not_found -> None with
199 | Some ev ->
200 begin match CList.index_opt Evar.equal ev focused_goals with
201 | Some i ->
202 (* goal is already under focus *)
203 _focus cond inf i i pr
204 | None ->
205 if Evd.mem_shelf ev evar_map then
206 (* goal is on the shelf, put it in focus *)
207 let proofview = Proofview.unshelve [ev] pr.proofview in
208 let pr = { pr with proofview } in
209 let (focused_goals, _) = Proofview.proofview pr.proofview in
210 let i =
211 (* Now we know that this will succeed *)
212 try CList.index Evar.equal ev focused_goals
213 with Not_found -> assert false
214 in
215 _focus cond inf i i pr
216 else
217 raise CannotUnfocusThisWay
218 end
219 | None ->
220 raise (NoSuchGoal (Some id))
221 end
222
223 let rec unfocus kind pr () =
224 let AnyFocusCond cond = cond_of_focus pr in
225 match test_cond cond kind pr.proofview with
226 | Cannot NotThisWay -> raise CannotUnfocusThisWay
227 | Cannot AlreadyNoFocus -> raise FullyUnfocused
228 | Strict ->
229 let pr = _unfocus pr in
230 pr
231 | Loose ->
232 begin try
233 let pr = _unfocus pr in
234 unfocus kind pr ()
235 with FullyUnfocused -> raise CannotUnfocusThisWay
236 end
237
238 exception NoSuchFocus
239 (* no handler: should not be allowed to reach toplevel. *)
240 let rec get_in_focus_stack : type a. a focus_kind -> _ -> a = fun kind stack ->
241 match stack with
242 | FocusElt (cond,inf,_)::stack ->
243 begin match check_cond_kind cond kind with
244 | Some Refl -> inf
245 | None -> get_in_focus_stack kind stack
246 end
247 | [] -> raise NoSuchFocus
248 let get_at_focus kind pr =
249 get_in_focus_stack kind pr.focus_stack
250
251 let is_last_focus kind pr =
252 let FocusElt (cond,_,_) = List.hd pr.focus_stack in
253 Option.has_some (check_cond_kind cond kind)
254
255 let no_focused_goal p =
256 Proofview.finished p.proofview
257
258 let rec maximal_unfocus k p =
259 if no_focused_goal p then
260 try maximal_unfocus k (unfocus k p ())
261 with FullyUnfocused | CannotUnfocusThisWay -> p
262 else p
263
264 (*** Proof Creation/Termination ***)
265
266 (* [end_of_stack] is unfocused by return to close every loose focus. *)
267 let end_of_stack_kind = new_focus_kind "end_of_stack"
268 let end_of_stack = CondEndStack end_of_stack_kind
269
270 let unfocused = is_last_focus end_of_stack_kind
271
272 let unfocus_all p = unfocus end_of_stack_kind p ()
273
274 let start ~name ~poly ?typing_flags sigma goals =
275 let entry, proofview = Proofview.init sigma goals in
276 let pr =
277 { proofview
278 ; entry
279 ; focus_stack = []
280 ; name
281 ; poly
282 ; typing_flags
283 } in
284 _focus end_of_stack () 1 (List.length goals) pr
285
286 let dependent_start ~name ~poly ?typing_flags goals =
287 let entry, proofview = Proofview.dependent_init goals in
288 let pr =
289 { proofview
290 ; entry
291 ; focus_stack = []
292 ; name
293 ; poly
294 ; typing_flags
295 } in
296 let number_of_goals = List.length (Proofview.initial_goals pr.entry) in
297 _focus end_of_stack () 1 number_of_goals pr
298
299 let compact p =
300 let entry, proofview = Proofview.compact p.entry p.proofview in
301 { p with proofview; entry }
302
303 let update_sigma_univs ugraph p =
304 let proofview = Proofview.Unsafe.update_sigma_univs ugraph p.proofview in
305 { p with proofview }
306
307 (*** Function manipulation proof extra informations ***)
308
309 (*** Tactics ***)
310
311 let run_tactic env tac pr =
312 let open Proofview.Notations in
313 let tac =
314 (* include the future goals in the shelf *)
315 Proofview.with_shelf tac >>= fun (shelf, v) ->
316 Proofview.Unsafe.tclNEWSHELVED shelf <*>
317 Proofview.tclUNIT v
318 in
319 let { name; poly; proofview } = pr in
320 let (result,proofview,env,status,info_trace) =
321 Proofview.apply ~name ~poly env tac proofview
322 in
323 let sigma = Proofview.return proofview in
324 (* cleanup any shelved goals that got defined
325 (this is only useful for goals that were already in the shelf,
326 tclNEWSHELVED filters out defined goals instead of adding them)
327 XXX should we be doing something advance-aware like tclNEWSHELVED? *)
328 let proofview = Proofview.filter_shelf (Evd.is_undefined sigma) proofview in
329 { pr with proofview },(env,status,info_trace),result
330
331 (*** Commands ***)
332
333 (* Remove all the goals from the shelf and adds them at the end of the
334 focused goals. *)
335 let unshelve p =
336 let sigma = Proofview.return p.proofview in
337 let shelf = Evd.shelf sigma in
338 let proofview = Proofview.unshelve shelf p.proofview in
339 { p with proofview }
340
341 let background_subgoals p =
342 let it, _ = Proofview.proofview (unroll_focus p.proofview p.focus_stack) in
343 it
344
345 let all_goals p =
346 let add gs set =
347 List.fold_left (fun s g -> Evar.Set.add g s) set gs in
348 let (goals,stack,sigma) = proof p in
349 let set = add goals Evar.Set.empty in
350 let set = List.fold_left (fun s gs -> let (g1, g2) = gs in add g1 (add g2 set)) set stack in
351 let set = add (Evd.shelf sigma) set in
352 let set = Evar.Set.union (Evd.given_up sigma) set in
353 let bgoals = background_subgoals p in
354 add bgoals set
355
356 type data =
357 { sigma : Evd.evar_map
358 (** A representation of the evar_map [EJGA wouldn't it better to just return the proofview?] *)
359 ; goals : Evar.t list
360 (** Focused goals *)
361 ; entry : Proofview.entry
362 (** Entry for the proofview *)
363 ; stack : (Evar.t list * Evar.t list) list
364 (** A representation of the focus stack *)
365 ; name : Names.Id.t
366 (** The name of the theorem whose proof is being constructed *)
367 ; poly : PolyFlags.t
368 (** Universe polymorphism *)
369 }
370
371 let data { proofview; focus_stack; entry; name; poly } =
372 let goals, sigma = Proofview.proofview proofview in
373 (* spiwack: beware, the bottom of the stack is used by [Proof]
374 internally, and should not be exposed. *)
375 let rec map_minus_one f = function
376 | [] -> assert false
377 | [_] -> []
378 | a::l -> f a :: (map_minus_one f l)
379 in
380 let map (FocusElt (_, _, c)) = Proofview.focus_context sigma c in
381 let stack = map_minus_one map focus_stack in
382 { sigma; goals; entry; stack; name; poly }
383
384 let pr_goal e = Pp.(str "GOAL:" ++ int (Evar.repr e))
385
386 let goal_uid e = string_of_int (Evar.repr e)
387
388 let pr_proof p =
389 let { goals=fg_goals; stack=bg_goals; sigma } = data p in
390 let entry = Proofview.partial_proof p.entry p.proofview in
391 Pp.(
392 let pr_goal_list = prlist_with_sep spc pr_goal in
393 let rec aux acc = function
394 | [] -> acc
395 | (before,after)::stack ->
396 aux (pr_goal_list before ++ spc () ++ str "{" ++ acc ++ str "}" ++ spc () ++
397 pr_goal_list after) stack in
398 str "[" ++ str "focus structure: " ++
399 aux (pr_goal_list fg_goals) bg_goals ++ str ";" ++ spc () ++
400 str "shelved: " ++ pr_goal_list (Evd.shelf sigma) ++ str ";" ++ spc () ++
401 str "given up: " ++ pr_goal_list (Evar.Set.elements @@ Evd.given_up sigma) ++ spc() ++
402 str "partial proof: " ++ prlist_with_sep spc (Termops.Internal.print_constr_env (Global.env()) sigma) entry ++
403 str "]"
404 )
405
406 let { Goptions.get = use_unification_heuristics } =
407 Goptions.declare_bool_option_and_ref
408 ~key:["Solve";"Unification";"Constraints"]
409 ~value:true
410 ()
411
412 exception SuggestNoSuchGoals of int * t
413
414 exception FailedConstraints of exn
415
416 let () = CErrors.register_handler (function
417 | FailedConstraints e ->
418 Some Pp.(CErrors.print e ++ spc() ++
419 hov 1
420 (str "(while solving unification constraints," ++ spc() ++
421 str "see flag \"Solve Unification Constraints\")"))
422 | _ -> None)
423
424 let solve_constraints =
425 Proofview.tclOR Refine.solve_constraints
426 (fun (e,info) -> Proofview.tclZERO ~info (FailedConstraints e))
427
428 let solve ?with_end_tac env gi info_lvl tac pr =
429 let tac = match with_end_tac with
430 | None -> tac
431 | Some etac -> Proofview.tclTHEN tac etac in
432 let tac = match info_lvl with
433 | None -> tac
434 | Some _ -> Proofview.Trace.record_info_trace tac
435 in
436 let nosuchgoal =
437 let info = Exninfo.reify () in
438 Proofview.tclZERO ~info (SuggestNoSuchGoals (1,pr))
439 in
440 let tac = Goal_select.tclSELECT ~nosuchgoal gi tac in
441 let tac =
442 if use_unification_heuristics () then
443 Proofview.tclTHEN tac solve_constraints
444 else tac
445 in
446 let env = Environ.update_typing_flags ?typing_flags:pr.typing_flags env in
447 let (p, (env, status, info), ()) = run_tactic env tac pr in
448 let sigma = (data p).sigma in
449 let () =
450 match info_lvl with
451 | None -> ()
452 | Some i -> Feedback.msg_info (Pp.hov 0 (Proofview.Trace.pr_info env sigma ~lvl:i info))
453 in
454 (p, status)
455
456 let get_goal_context_gen pf i =
457 let { sigma; goals } = data pf in
458 let goal = try List.nth goals (i-1) with Failure _ -> raise (NoSuchGoal None) in
459 let env = Evd.evar_filtered_env (Global.env ()) (Evd.find_undefined sigma goal) in
460 (sigma, env)
461
462 let get_proof_context p =
463 try get_goal_context_gen p 1
464 with
465 | NoSuchGoal _ ->
466 (* No more focused goals *)
467 let { sigma } = data p in
468 sigma, Global.env ()
469
470 let purge_side_effects p =
471 let proofview, eff = Proofview.Unsafe.purge_side_effects p.proofview in
472 { p with proofview }, eff