Debian Codegraph @

engine/proofview.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
12 (** This files defines the basic mechanism of proofs: the [proofview]
13 type is the state which tactics manipulate (a global state for
14 existential variables, together with the list of goals), and the type
15 ['a tactic] is the (abstract) type of tactics modifying the proof
16 state and returning a value of type ['a]. *)
17
18 open Pp
19 open Util
20 open Proofview_monad
21 open Context.Named.Declaration
22
23 (** Main state of tactics *)
24 type proofview = Proofview_monad.proofview
25
26 (* evar env
27 * proofs (under construction).
28 * statements that are being proved. *)
29 type entry = (Environ.named_context_val * EConstr.constr * EConstr.types) list
30
31 (** Returns a stylised view of a proofview for use by, for instance,
32 ide-s. *)
33 (* spiwack: the type of [proofview] will change as we push more
34 refined functions to ide-s. This would be better than spawning a
35 new nearly identical function every time. Hence the generic name. *)
36 (* In this version: returns the list of focused goals together with
37 the [evar_map] context. *)
38 let proofview p =
39 List.map drop_state p.comb , p.solution
40
41 let compact el ({ solution } as pv) =
42 let nf c = Evarutil.nf_evar solution c in
43 let nf0 c = EConstr.(to_constr ~abort_on_undefined_evars:false solution (of_constr c)) in
44 let nf_hyps hyps = Environ.map_named_val (fun d -> map_constr nf0 d) hyps in
45 let size = Evd.fold (fun _ _ i -> i+1) solution 0 in
46 let new_el = List.map (fun (hyps,t,ty) -> nf_hyps hyps, nf t, nf ty) el in
47 let pruned_solution = Evd.drop_all_defined solution in
48 let apply_subst_einfo _ ei = Evd.map_evar_info nf ei in
49 let new_solution = Evd.raw_map_undefined apply_subst_einfo pruned_solution in
50 let new_size = Evd.fold (fun _ _ i -> i+1) new_solution 0 in
51 Feedback.msg_info (Pp.str (Printf.sprintf "Evars: %d -> %d\n" size new_size));
52 new_el, { pv with solution = new_solution; }
53
54
55 (** {6 Starting and querying a proof view} *)
56
57 type telescope =
58 | TNil of Evd.evar_map
59 | TCons of Environ.env * Evd.evar_map * EConstr.types * (Evd.evar_map -> EConstr.constr -> telescope)
60
61 let map_telescope_evd f = function
62 | TNil sigma -> TNil (f sigma)
63 | TCons (env,sigma,ty,g) -> TCons(env,(f sigma),ty,g)
64
65 let dependent_init =
66 (* Goals don't have a source location. *)
67 let src = Loc.tag @@ Evar_kinds.GoalEvar in
68 (* Main routine *)
69 let rec aux = function
70 | TNil sigma -> [], { solution = sigma; comb = [] }
71 | TCons (env, sigma, typ, t) ->
72 let (sigma, econstr) = Evarutil.new_evar env sigma ~src ~typeclass_candidate:false typ in
73 let (gl, _) = EConstr.destEvar sigma econstr in
74 let ret, { solution = sol; comb = comb } = aux (t sigma econstr) in
75 let entry = (Environ.named_context_val env, econstr, typ) :: ret in
76 entry, { solution = sol; comb = with_empty_state gl :: comb }
77 in
78 fun t ->
79 let t = map_telescope_evd Evd.push_future_goals t in
80 let entry, v = aux t in
81 (* The created goal are not to be shelved. *)
82 let _goals, solution = Evd.pop_future_goals v.solution in
83 entry, { v with solution }
84
85 let init =
86 let rec aux sigma = function
87 | [] -> TNil sigma
88 | (env,g)::l -> TCons (env,sigma,g,(fun sigma _ -> aux sigma l))
89 in
90 fun sigma l -> dependent_init (aux sigma l)
91
92 let initial_goals initial = initial
93
94 let finished = function
95 | {comb = []} -> true
96 | _ -> false
97
98 let return { solution=defs } = defs
99
100 let return_constr { solution = defs } c = Evarutil.nf_evar defs c
101
102 let partial_proof entry pv = CList.map (return_constr pv) (CList.map pi2 entry)
103
104 (** {6 Normalizing evars} *)
105
106 let cleared_alias evd g =
107 let evk = drop_state g in
108 let state = get_state g in
109 Option.map (fun g -> goal_with_state g state) (Evarutil.advance evd evk)
110
111 (** [undefined defs l] is the list of goals in [l] which are still
112 unsolved (after advancing cleared goals). Note that order matters. *)
113 let undefined_evars defs l =
114 let fold evk (seen, ans as accu) = match Evarutil.advance defs evk with
115 | None -> accu
116 | Some evk ->
117 if Evar.Set.mem evk seen then accu
118 else (Evar.Set.add evk seen, evk :: ans)
119 in
120 snd @@ List.fold_right fold l (Evar.Set.empty, [])
121
122 let undefined defs l =
123 let fold gl (seen, ans as accu) = match cleared_alias defs gl with
124 | None -> accu
125 | Some gl ->
126 let evk = drop_state gl in
127 if Evar.Set.mem evk seen then accu
128 else (Evar.Set.add evk seen, gl :: ans)
129 in
130 snd @@ List.fold_right fold l (Evar.Set.empty, [])
131
132 (** {6 Focusing commands} *)
133
134 (** A [focus_context] represents the part of the proof view which has
135 been removed by a focusing action, it can be used to unfocus later
136 on. *)
137 (* First component is a reverse list of the goals which come before
138 and second component is the list of the goals which go after (in
139 the expected order). *)
140 type focus_context = goal_with_state list * goal_with_state list
141
142
143 (** Returns a stylised view of a focus_context for use by, for
144 instance, ide-s. *)
145 (* spiwack: the type of [focus_context] will change as we push more
146 refined functions to ide-s. This would be better than spawning a
147 new nearly identical function every time. Hence the generic name. *)
148 (* In this version: the goals in the context, as a "zipper" (the first
149 list is in reversed order). *)
150 let focus_context sigma (left,right) =
151 (undefined_evars sigma (List.map drop_state left), undefined_evars sigma (List.map drop_state right))
152
153 (** This (internal) function extracts a sublist between two indices,
154 and returns this sublist together with its context: if it returns
155 [(a,(b,c))] then [a] is the sublist and [(rev b) @ a @ c] is the
156 original list. The focused list has length [j-i-1] and contains
157 the goals from number [i] to number [j] (both included) the first
158 goal of the list being numbered [1]. [focus_sublist i j l] raises
159 [IndexOutOfRange] if [i > length l], or [j > length l] or [j <
160 i]. *)
161 let focus_sublist i j l =
162 let (left,sub_right) = CList.goto (i-1) l in
163 let (sub, right) =
164 try CList.chop (j-i+1) sub_right
165 with Failure _ -> raise CList.IndexOutOfRange
166 in
167 (sub, (left,right))
168
169 (** Inverse operation to the previous one. *)
170 let unfocus_sublist (left,right) s =
171 CList.rev_append left (s@right)
172
173
174 (** [focus i j] focuses a proofview on the goals from index [i] to
175 index [j] (inclusive, goals are indexed from [1]). I.e. goals
176 number [i] to [j] become the only focused goals of the returned
177 proofview. It returns the focused proofview, and a context for
178 the focus stack. *)
179 let focus i j sp =
180 let (new_comb, (left, right)) = focus_sublist i j sp.comb in
181 ( { sp with comb = new_comb } , (left, right) )
182
183 (* Returns [ev, Some n] if [n] is the index of evar [ev] with name [id] in the
184 list of currently focused goals, or [ev, None] if [ev] is shelved.
185 Raises [Not_found] if the evar does not exist. *)
186 let find_evar_in_pv id pv =
187 let ev = Evd.evar_key id pv.solution in
188 let comb = CList.map drop_state pv.comb in
189 try ev, Some (CList.index Evar.equal ev comb)
190 with Not_found -> ev, None
191
192 (** Unfocuses a proofview with respect to a context. *)
193 let unfocus (left, right) sp =
194 { sp with comb = undefined sp.solution (unfocus_sublist (left, right) sp.comb) }
195
196 let with_empty_state = Proofview_monad.with_empty_state
197 let drop_state = Proofview_monad.drop_state
198 let goal_with_state = Proofview_monad.goal_with_state
199
200 (** {6 The tactic monad} *)
201
202 (** - Tactics are objects which apply a transformation to all the
203 subgoals of the current view at the same time. By opposition to
204 the old vision of applying it to a single goal. It allows tactics
205 such as [shelve_unifiable], tactics to reorder the focused goals,
206 or global automation tactic for dependent subgoals (instantiating
207 an evar has influences on the other goals of the proof in
208 progress, not being able to take that into account causes the
209 current eauto tactic to fail on some instances where it could
210 succeed). Another benefit is that it is possible to write tactics
211 that can be executed even if there are no focused goals.
212 - Tactics form a monad ['a tactic], in a sense a tactic can be
213 seen as a function (without argument) which returns a value of
214 type 'a and modifies the environment (in our case: the view).
215 Tactics of course have arguments, but these are given at the
216 meta-level as OCaml functions. Most tactics in the sense we are
217 used to return [()], that is no really interesting values. But
218 some might pass information around. The tactics seen in Rocq's
219 Ltac are (for now at least) only [unit tactic], the return values
220 are kept for the OCaml toolkit. The operation or the monad are
221 [Proofview.tclUNIT] (which is the "return" of the tactic monad)
222 [Proofview.tclBIND] (which is the "bind") and [Proofview.tclTHEN]
223 (which is a specialized bind on unit-returning tactics).
224 - Tactics have support for full-backtracking. Tactics can be seen
225 having multiple success: if after returning the first success a
226 failure is encountered, the tactic can backtrack and use a second
227 success if available. The state is backtracked to its previous
228 value, except the non-logical state defined in the {!NonLogical}
229 module below.
230 *)
231 (* spiwack: as far as I'm aware this doesn't really relate to
232 F. Kirchner and C. Muñoz. *)
233
234 module Proof = Logical
235
236 (** type of tactics:
237
238 tactics can
239 - access the environment,
240 - report unsafe status, shelved goals and given up goals
241 - access and change the current [proofview]
242 - backtrack on previous changes of the proofview *)
243 type +'a tactic = 'a Proof.t
244
245 (** Applies a tactic to the current proofview. *)
246 let apply ~name ~poly env t sp =
247 let open Logic_monad in
248 NewProfile.profile "Proofview.apply" (fun () ->
249 let ans = Proof.repr (Proof.run t P.{trace=false; name; poly} (sp,env)) in
250 let ans = Logic_monad.NonLogical.run ans in
251 match ans with
252 | Nil (e, info) -> Exninfo.iraise (TacticFailure e, info)
253 | Cons ((r, (state, env), status, info), _) ->
254 r, state, env, status, Trace.to_tree info)
255 ()
256
257
258 (** {7 Monadic primitives} *)
259
260 (** Unit of the tactic monad. *)
261 let tclUNIT = Proof.return
262
263 (** Bind operation of the tactic monad. *)
264 let tclBIND = Proof.(>>=)
265
266 (** Interprets the ";" (semicolon) of Ltac. As a monadic operation,
267 it's a specialized "bind". *)
268 let tclTHEN = Proof.(>>)
269
270 (** [tclIGNORE t] has the same operational content as [t], but drops
271 the returned value. *)
272 let tclIGNORE = Proof.ignore
273
274 module Monad = Proof
275
276
277
278 (** {7 Failure and backtracking} *)
279
280
281 (** [tclZERO e] fails with exception [e]. It has no success. *)
282 let tclZERO ?(info=Exninfo.null) e =
283 if not (CErrors.noncritical e) then
284 CErrors.anomaly (Pp.str "tclZERO receiving critical error: " ++ CErrors.print e);
285 Proof.zero (e, info)
286
287 (** [tclOR t1 t2] behaves like [t1] as long as [t1] succeeds. Whenever
288 the successes of [t1] have been depleted and it failed with [e],
289 then it behaves as [t2 e]. In other words, [tclOR] inserts a
290 backtracking point. *)
291 let tclOR = Proof.plus
292
293 (** [tclORELSE t1 t2] is equal to [t1] if [t1] has at least one
294 success or [t2 e] if [t1] fails with [e]. It is analogous to
295 [try/with] handler of exception in that it is not a backtracking
296 point. *)
297 let tclORELSE t1 t2 =
298 let open Logic_monad in
299 let open Proof in
300 split t1 >>= function
301 | Nil e -> t2 e
302 | Cons (a,t1') -> plus (return a) t1'
303
304 (** [tclIFCATCH a s f] is a generalisation of {!tclORELSE}: if [a]
305 succeeds at least once then it behaves as [tclBIND a s] otherwise,
306 if [a] fails with [e], then it behaves as [f e]. *)
307 let tclIFCATCH a s f =
308 let open Logic_monad in
309 let open Proof in
310 split a >>= function
311 | Nil e -> f e
312 | Cons (x,a') -> plus (s x) (fun e -> (a' e) >>= fun x' -> (s x'))
313
314 (** [tclONCE t] behave like [t] except it has at most one success:
315 [tclONCE t] stops after the first success of [t]. If [t] fails
316 with [e], [tclONCE t] also fails with [e]. *)
317 let tclONCE = Proof.once
318
319 exception MoreThanOneSuccess
320 let _ = CErrors.register_handler begin function
321 | MoreThanOneSuccess ->
322 Some (Pp.str "This tactic has more than one success.")
323 | _ -> None
324 end
325
326 (** [tclEXACTLY_ONCE e t] succeeds as [t] if [t] has exactly one
327 success. Otherwise it fails. The tactic [t] is run until its first
328 success, then a failure with exception [e] is simulated. It [t]
329 yields another success, then [tclEXACTLY_ONCE e t] fails with
330 [MoreThanOneSuccess] (it is a user error). Otherwise,
331 [tclEXACTLY_ONCE e t] succeeds with the first success of
332 [t]. Notice that the choice of [e] is relevant, as the presence of
333 further successes may depend on [e] (see {!tclOR}). *)
334 let tclEXACTLY_ONCE e t =
335 let open Logic_monad in
336 let open Proof in
337 split t >>= function
338 | Nil (e, info) -> tclZERO ~info e
339 | Cons (x,k) ->
340 let info = Exninfo.null in
341 Proof.split (k (e, Exninfo.null)) >>= function
342 | Nil _ -> tclUNIT x
343 | _ -> tclZERO ~info MoreThanOneSuccess
344
345
346 (** [tclCASE t] wraps the {!Proofview_monad.Logical.split} primitive. *)
347 type 'a case =
348 | Fail of Exninfo.iexn
349 | Next of 'a * (Exninfo.iexn -> 'a tactic)
350 let tclCASE t =
351 let open Logic_monad in
352 let map = function
353 | Nil e -> Fail e
354 | Cons (x, t) -> Next (x, t)
355 in
356 Proof.map map (Proof.split t)
357
358 let tclBREAK = Proof.break
359
360
361
362 (** {7 Focusing tactics} *)
363
364 (** Represents a range selector as accepted by [tclFOCUSSELECTORLIST]. *)
365 type goal_range_selector =
366 | NthSelector of int
367 | RangeSelector of (int * int)
368 | IdSelector of Libnames.qualid
369
370 exception NoSuchGoals of int
371 exception CannotSelectShelvedAndFocused
372
373 let _ = CErrors.register_handler begin function
374 | NoSuchGoals n ->
375 Some (str "No such " ++ str (String.plural n "goal") ++ str ".")
376 | CannotSelectShelvedAndFocused ->
377 Some (str "Cannot simultaneously select shelved and unshelved goals.")
378 | _ -> None
379 end
380
381 (** [tclFOCUS ?nosuchgoal i j t] applies [t] in a context where
382 only the goals numbered [i] to [j] are focused (the rest of the goals
383 is restored at the end of the tactic). If the range [i]-[j] is not
384 valid, then it [tclFOCUS_gen nosuchgoal i j t] is [nosuchgoal]. *)
385 let tclFOCUS ?nosuchgoal i j t =
386 let nosuchgoal ~info = Option.default (tclZERO ~info (NoSuchGoals (j+1-i))) nosuchgoal in
387 let open Proof in
388 Pv.get >>= fun initial ->
389 try
390 let (focused,context) = focus i j initial in
391 Pv.set focused >>
392 t >>= fun result ->
393 Pv.modify (fun next -> unfocus context next) >>
394 return result
395 with CList.IndexOutOfRange as exn ->
396 let _, info = Exninfo.capture exn in
397 nosuchgoal ~info
398
399 let tclTRYFOCUS i j t = tclFOCUS ~nosuchgoal:(tclUNIT ()) i j t
400
401 (** Like {!tclFOCUS} but selects goals on the shelf, applies [t], and shelves
402 generated subgoals.
403 This method assumes that the list [evs] is a list of existing evars. *)
404 let tclFOCUSSHELF ?(nosuchgoal=tclZERO (NoSuchGoals 1)) evs t =
405 if CList.is_empty evs then nosuchgoal
406 else
407 let open Proof in
408 Comb.get >>= fun initial_comb ->
409 Comb.set (CList.map with_empty_state evs) >>
410 t >>= fun result ->
411 Comb.get >>= fun subgoals ->
412 Comb.set initial_comb >>
413 let subgoals = CList.filter_map (fun ev ->
414 let ev = drop_state ev in
415 (* If ev is still undefined, leave it on its original shelf *)
416 if (CList.mem_f Evar.equal ev evs) then None else Some ev)
417 subgoals
418 in
419 Pv.modify (fun pv -> { pv with solution = Evd.shelve pv.solution (undefined_evars pv.solution subgoals) }) >>
420 return result
421
422 let tclFOCUSLIST ?(nosuchgoal=tclZERO (NoSuchGoals 0)) l t =
423 let open Proof in
424 Comb.get >>= fun comb ->
425 let n = CList.length comb in
426 let ok (i, j) = 1 <= i && i <= j && j <= n in
427 if not (CList.for_all ok l) then nosuchgoal
428 else
429 match l with
430 | [] -> nosuchgoal
431 | (mi, _) :: _ ->
432 (* Get the left-most goal to focus. This goal won't move, and we
433 will then place all the other goals to focus to the right. *)
434 let mi = CList.fold_left (fun m (i, _) -> min m i) mi l in
435 (* [CList.goto] returns a zipper, so that
436 [(rev left) @ sub_right = comb]. *)
437 let left, sub_right = CList.goto (mi-1) comb in
438 let p x _ = CList.exists (fun (i, j) -> i <= x + mi && x + mi <= j) l in
439 let sub, right = CList.partitioni p sub_right in
440 let mj = mi - 1 + CList.length sub in
441 Comb.set (CList.rev_append left (sub @ right)) >>
442 tclFOCUS mi mj t
443
444 let tclFOCUSSELECTORLIST ?(nosuchgoal=tclZERO (NoSuchGoals 0)) l t =
445 let open Proof in
446 Pv.get >>= fun initial ->
447 try
448 let (ranges, shelved_evars) =
449 CList.partition_map (function
450 | NthSelector n -> Left (n, n)
451 | RangeSelector r -> Left r
452 | IdSelector id ->
453 match find_evar_in_pv id initial with
454 | ev, Some n -> Left (n, n) (* goal is focused with index n *)
455 | ev, None -> Right ev (* goal is shelved *)) l in
456 match CList.is_empty ranges, CList.is_empty shelved_evars with
457 | true, true -> nosuchgoal
458 | true, false -> tclFOCUSSHELF ~nosuchgoal shelved_evars t
459 | false, true -> tclFOCUSLIST ~nosuchgoal ranges t
460 | false, false -> tclZERO CannotSelectShelvedAndFocused
461 with Not_found -> nosuchgoal
462
463 (** Like {!tclFOCUS} but selects a single goal by name. *)
464 let tclFOCUSID ?(nosuchgoal=tclZERO (NoSuchGoals 1)) id t =
465 let open Proof in
466 Pv.get >>= fun initial ->
467 try
468 match find_evar_in_pv id initial with
469 | ev, Some n ->
470 (* Goal is under focus with index n *)
471 let (focused,context) = focus n n initial in
472 Pv.set focused >>
473 t >>= fun result ->
474 Pv.modify (fun next -> unfocus context next) >>
475 return result
476 | ev, None ->
477 (* Goal is shelved. *)
478 tclFOCUSSHELF ~nosuchgoal [ev] t
479 with Not_found -> nosuchgoal
480
481 (** {7 Dispatching on goals} *)
482
483 exception SizeMismatch of int*int
484 let _ = CErrors.register_handler begin function
485 | SizeMismatch (i,j) ->
486 let open Pp in
487 Some (
488 str"Incorrect number of goals" ++ spc() ++
489 str"(expected "++int i++str(String.plural i " tactic") ++ str", was given "++ int j++str").")
490 | _ -> None
491 end
492
493 (** A variant of [Monad.List.iter] where we iter over the focused list
494 of goals. The argument tactic is executed in a focus comprising
495 only of the current goal, a goal which has been solved by side
496 effect is skipped. The generated subgoals are concatenated in
497 order. *)
498 let iter_goal i =
499 let open Proof in
500 Comb.get >>= fun initial ->
501 Proof.List.fold_left begin fun (subgoals as cur) goal ->
502 Solution.get >>= fun step ->
503 match cleared_alias step goal with
504 | None -> return cur
505 | Some goal ->
506 Comb.set [goal] >>
507 i goal >>
508 Proof.map (fun comb -> comb :: subgoals) Comb.get
509 end [] initial >>= fun subgoals ->
510 Solution.get >>= fun evd ->
511 Comb.set CList.(undefined evd (flatten (rev subgoals)))
512
513 (** List iter but allocates a list of results *)
514 let map_goal i =
515 let rev = List.rev in (* hem... Proof masks List... *)
516 let open Proof in
517 Comb.get >>= fun initial ->
518 Proof.List.fold_left begin fun (acc, subgoals as cur) goal ->
519 Solution.get >>= fun step ->
520 match cleared_alias step goal with
521 | None -> return cur
522 | Some goal ->
523 Comb.set [goal] >>
524 i goal >>= fun res ->
525 Proof.map (fun comb -> comb :: subgoals) Comb.get >>= fun x ->
526 return (res :: acc, x)
527 end ([],[]) initial >>= fun (results_rev, subgoals) ->
528 Solution.get >>= fun evd ->
529 Comb.set CList.(undefined evd (flatten (rev subgoals))) >>
530 return (rev results_rev)
531
532 (** A variant of [Monad.List.fold_left2] where the first list is the
533 list of focused goals. The argument tactic is executed in a focus
534 comprising only of the current goal, a goal which has been solved
535 by side effect is skipped. The generated subgoals are concatenated
536 in order. *)
537 let fold_left2_goal i s l =
538 let open Proof in
539 Pv.get >>= fun initial ->
540 let err =
541 return () >>= fun () -> (* Delay the computation of list lengths. *)
542 tclZERO (SizeMismatch (CList.length initial.comb,CList.length l))
543 in
544 Proof.List.fold_left2 err begin fun ((r,subgoals) as cur) goal a ->
545 Solution.get >>= fun step ->
546 match cleared_alias step goal with
547 | None -> return cur
548 | Some goal ->
549 Comb.set [goal] >>
550 i goal a r >>= fun r ->
551 Proof.map (fun comb -> (r, comb :: subgoals)) Comb.get
552 end (s,[]) initial.comb l >>= fun (r,subgoals) ->
553 Solution.get >>= fun evd ->
554 Comb.set CList.(undefined evd (flatten (rev subgoals))) >>
555 return r
556
557 (** Dispatch tacticals are used to apply a different tactic to each
558 goal under focus. They come in two flavours: [tclDISPATCH] takes a
559 list of [unit tactic]-s and build a [unit tactic]. [tclDISPATCHL]
560 takes a list of ['a tactic] and returns an ['a list tactic].
561
562 They both work by applying each of the tactic in a focus
563 restricted to the corresponding goal (starting with the first
564 goal). In the case of [tclDISPATCHL], the tactic returns a list of
565 the same size as the argument list (of tactics), each element
566 being the result of the tactic executed in the corresponding goal.
567
568 When the length of the tactic list is not the number of goal,
569 raises [SizeMismatch (g,t)] where [g] is the number of available
570 goals, and [t] the number of tactics passed.
571
572 [tclDISPATCHGEN join tacs] generalises both functions as the
573 successive results of [tacs] are stored in reverse order in a
574 list, and [join] is used to convert the result into the expected
575 form. *)
576 let tclDISPATCHGEN0 join tacs =
577 match tacs with
578 | [] ->
579 begin
580 let open Proof in
581 Comb.get >>= function
582 | [] -> tclUNIT (join [])
583 | comb -> tclZERO (SizeMismatch (CList.length comb,0))
584 end
585 | [tac] ->
586 begin
587 let open Proof in
588 Pv.get >>= function
589 | { comb=[goal] ; solution } ->
590 begin match cleared_alias solution goal with
591 | None -> tclUNIT (join [])
592 | Some _ -> Proof.map (fun res -> join [res]) tac
593 end
594 | {comb} -> tclZERO (SizeMismatch(CList.length comb,1))
595 end
596 | _ ->
597 let iter _ t cur = Proof.map (fun y -> y :: cur) t in
598 let ans = fold_left2_goal iter [] tacs in
599 Proof.map join ans
600
601 let tclDISPATCHGEN join tacs =
602 let branch t = InfoL.tag (Info.DBranch) t in
603 let tacs = CList.map branch tacs in
604 InfoL.tag (Info.Dispatch) (tclDISPATCHGEN0 join tacs)
605
606 let tclDISPATCH tacs = tclDISPATCHGEN ignore tacs
607
608 let tclDISPATCHL tacs = tclDISPATCHGEN CList.rev tacs
609
610
611 (** [extend_to_list startxs rx endxs l] builds a list
612 [startxs @ [rx,...,rx] @ endxs] of the same length as [l]. Raises
613 [SizeMismatch] if [startxs @ endxs] is already longer than [l]. *)
614 let extend_to_list startxs rx endxs l =
615 (* spiwack: I use [l] essentially as a natural number *)
616 let rec duplicate acc = function
617 | [] -> acc
618 | _::rest -> duplicate (rx::acc) rest
619 in
620 let rec tail to_match rest =
621 match rest, to_match with
622 | [] , _::_ -> raise (SizeMismatch(0,0)) (* placeholder *)
623 | _::rest , _::to_match -> tail to_match rest
624 | _ , [] -> duplicate endxs rest
625 in
626 let rec copy pref rest =
627 match rest,pref with
628 | [] , _::_ -> raise (SizeMismatch(0,0)) (* placeholder *)
629 | _::rest, a::pref -> a::(copy pref rest)
630 | _ , [] -> tail endxs rest
631 in
632 copy startxs l
633
634 (** [tclEXTEND b r e] is a variant of {!tclDISPATCH}, where the [r]
635 tactic is "repeated" enough time such that every goal has a tactic
636 assigned to it ([b] is the list of tactics applied to the first
637 goals, [e] to the last goals, and [r] is applied to every goal in
638 between). *)
639 let tclEXTEND tacs1 rtac tacs2 =
640 let open Proof in
641 Comb.get >>= fun comb ->
642 try
643 let tacs = extend_to_list tacs1 rtac tacs2 comb in
644 tclDISPATCH tacs
645 with SizeMismatch _ ->
646 tclZERO (SizeMismatch(
647 CList.length comb,
648 (CList.length tacs1)+(CList.length tacs2)))
649 (* spiwack: failure occurs only when the number of goals is too
650 small. Hence we can assume that [rtac] is replicated 0 times for
651 any error message. *)
652
653 (** [tclEXTEND [] tac []]. *)
654 let tclINDEPENDENT tac =
655 let open Proof in
656 Pv.get >>= fun initial ->
657 match initial.comb with
658 | [] -> tclUNIT ()
659 | [_] -> tac
660 | _ ->
661 let tac = InfoL.tag (Info.DBranch) tac in
662 InfoL.tag (Info.Dispatch) (iter_goal (fun _ -> tac))
663
664 let tclINDEPENDENTL tac =
665 let open Proof in
666 Pv.get >>= fun initial ->
667 match initial.comb with
668 | [] -> tclUNIT []
669 | [_] -> tac >>= fun x -> return [x]
670 | _ ->
671 let tac = InfoL.tag (Info.DBranch) tac in
672 InfoL.tag (Info.Dispatch) (map_goal (fun _ -> tac))
673
674 (** {7 Goal manipulation} *)
675
676 (** Shelves all the goals under focus. *)
677 let shelve =
678 let open Proof in
679 Comb.get >>= fun initial ->
680 Comb.set [] >>
681 InfoL.leaf (Info.Tactic (fun () -> Pp.str"shelve")) >>
682 let initial = CList.map drop_state initial in
683 Pv.modify (fun pv -> { pv with solution = Evd.shelve pv.solution initial })
684
685 let shelve_goals l =
686 let open Proof in
687 Comb.get >>= fun initial ->
688 let comb = CList.filter (fun g -> not (CList.mem (drop_state g) l)) initial in
689 Comb.set comb >>
690 InfoL.leaf (Info.Tactic (fun () -> Pp.str"shelve_goals")) >>
691 Pv.modify (fun pv -> { pv with solution = Evd.shelve pv.solution l })
692
693 (** [depends_on sigma src tgt] checks whether the goal [src] appears
694 as an existential variable in the definition of the goal [tgt] in
695 [sigma]. *)
696 let depends_on sigma src tgt =
697 let evi = Evd.find_undefined sigma tgt in
698 Evar.Set.mem src (Evd.evars_of_filtered_evar_info sigma (Evarutil.nf_evar_info sigma evi))
699
700 let unifiable_delayed g l =
701 CList.exists (fun (tgt, lazy evs) -> not (Evar.equal g tgt) && Evar.Set.mem g evs) l
702
703 let free_evars sigma l =
704 let cache = Evarutil.create_undefined_evars_cache () in
705 let map ev =
706 (* Computes the set of evars appearing in the hypotheses, the conclusion or
707 the body of the evar_info [evi]. Note: since we want to use it on goals,
708 the body is actually supposed to be empty. *)
709 let EvarInfo evi = Evd.find sigma ev in
710 let fevs = lazy (Evarutil.filtered_undefined_evars_of_evar_info ~cache sigma evi) in
711 (ev, fevs)
712 in
713 List.map map l
714
715 let free_evars_with_state sigma l =
716 let cache = Evarutil.create_undefined_evars_cache () in
717 let map ev =
718 (* Computes the set of evars appearing in the hypotheses, the conclusion or
719 the body of the evar_info [evi]. Note: since we want to use it on goals,
720 the body is actually supposed to be empty. *)
721 let ev = drop_state ev in
722 let EvarInfo evi = Evd.find sigma ev in
723 let fevs = lazy (Evarutil.filtered_undefined_evars_of_evar_info ~cache sigma evi) in
724 (ev, fevs)
725 in
726 List.map map l
727
728
729 (** [unifiable sigma g l] checks whether [g] appears in another
730 subgoal of [l]. The list [l] may contain [g], but it does not
731 affect the result. *)
732 let unifiable_delayed_with_state sigma g l =
733 let g = drop_state g in
734 unifiable_delayed g l
735
736 let unifiable sigma g l =
737 let l = free_evars sigma l in
738 unifiable_delayed g l
739
740 (** [partition_unifiable sigma l] partitions [l] into a pair [(u,n)]
741 where [u] is composed of the unifiable goals, i.e. the goals on
742 whose definition other goals of [l] depend, and [n] are the
743 non-unifiable goals. *)
744 let partition_unifiable sigma l =
745 let fevs = free_evars_with_state sigma l in
746 CList.partition (fun g -> unifiable_delayed_with_state sigma g fevs) l
747
748 (** Shelves the unifiable goals under focus, i.e. the goals which
749 appear in other goals under focus (the unfocused goals are not
750 considered). *)
751 let shelve_unifiable_informative =
752 let open Proof in
753 Pv.get >>= fun initial ->
754 let (u,n) = partition_unifiable initial.solution initial.comb in
755 Comb.set n >>
756 InfoL.leaf (Info.Tactic (fun () -> Pp.str"shelve_unifiable")) >>
757 let u = CList.map drop_state u in
758 Pv.modify (fun pv -> { pv with solution = Evd.shelve pv.solution u }) >>
759 tclUNIT u
760
761 let shelve_unifiable =
762 let open Proof in
763 shelve_unifiable_informative >>= fun _ -> tclUNIT ()
764
765 (** [guard_no_unifiable] returns the list of unifiable goals if some
766 goals are unifiable (see {!shelve_unifiable}) in the current focus. *)
767 let guard_no_unifiable =
768 let open Proof in
769 Pv.get >>= fun initial ->
770 let (u,n) = partition_unifiable initial.solution initial.comb in
771 match u with
772 | [] -> tclUNIT None
773 | gls ->
774 let l = CList.map (fun g -> Evd.dependent_evar_ident (drop_state g) initial.solution) gls in
775 let l = CList.map (fun id -> Names.Name id) l in
776 tclUNIT (Some l)
777
778 (** [unshelve l p] moves all the goals in [l] from the shelf and put them at
779 the end of the focused goals of p, if they are still undefined after [advance] *)
780 let unshelve l p =
781 let solution = Evd.unshelve p.solution l in
782 let l = List.map with_empty_state l in
783 (* advance the goals in case of clear *)
784 let l = undefined p.solution l in
785 { comb = p.comb@l; solution }
786
787 let filter_shelf f pv =
788 { pv with solution = Evd.filter_shelf f pv.solution }
789
790 let mark_in_evm ~goal evd evars =
791 let evd =
792 if goal then
793 let mark evd content =
794 let EvarInfo info = Evd.find evd content in
795 let source = match Evd.evar_source info with
796 (* Two kinds for goal evars:
797 - GoalEvar (morally not dependent)
798 - VarInstance (morally dependent of some name).
799 This is a heuristic for naming these evars. *)
800 | loc, (Evar_kinds.QuestionMark { Evar_kinds.qm_name=Names.Name id} |
801 Evar_kinds.ImplicitArg (_,(_,id),_)) -> loc, Evar_kinds.VarInstance id
802 | _, (Evar_kinds.VarInstance _ | Evar_kinds.GoalEvar) as x -> x
803 | loc,_ -> loc,Evar_kinds.GoalEvar
804 in
805 Evd.update_source evd content source
806 in CList.fold_left mark evd evars
807 else evd
808 in
809 let tcs = Evd.get_typeclass_evars evd in
810 let evset = Evar.Set.of_list evars in
811 Evd.set_typeclass_evars evd (Evar.Set.diff tcs evset)
812
813 let with_shelf tac =
814 let open Proof in
815 Pv.get >>= fun pv ->
816 let { solution } = pv in
817 Pv.set { pv with solution = Evd.push_shelf @@ Evd.push_future_goals solution } >>
818 tac >>= fun ans ->
819 Pv.get >>= fun npv ->
820 let { solution = sigma } = npv in
821 let gls, sigma = Evd.pop_shelf sigma in
822 (* The pending future goals are necessarily coming from legacy tactics *)
823 (* and thus considered as to shelve, as in Proof.run_tactic *)
824 (* TODO: is it still relevant since the removal of the compat layer? *)
825 let fgl, sigma = Evd.pop_future_goals sigma in
826 (* Ensure we mark and return only unsolved goals *)
827 let gls' = CList.rev_append (Evd.FutureGoals.comb fgl) gls in
828 let gls' = undefined_evars sigma gls' in
829 let sigma = mark_in_evm ~goal:false sigma gls' in
830 let npv = { npv with solution = sigma } in
831 Pv.set npv >> tclUNIT (gls', ans)
832
833 (** [goodmod p m] computes the representative of [p] modulo [m] in the
834 interval [[0,m-1]].*)
835 let goodmod p m =
836 if m = 0 then 0 else
837 let p' = p mod m in
838 (* if [n] is negative [n mod l] is negative of absolute value less
839 than [l], so [(n mod l)+l] is the representative of [n] in the
840 interval [[0,l-1]].*)
841 if p' < 0 then p'+m else p'
842
843 let cycle n =
844 let open Proof in
845 InfoL.leaf (Info.Tactic (fun () -> Pp.(str"cycle "++int n))) >>
846 Comb.modify begin fun initial ->
847 let l = CList.length initial in
848 let n' = goodmod n l in
849 let (front,rear) = CList.chop n' initial in
850 rear@front
851 end
852
853 let swap i j =
854 let open Proof in
855 InfoL.leaf (Info.Tactic (fun () -> Pp.(hov 2 (str"swap"++spc()++int i++spc()++int j)))) >>
856 Comb.modify begin fun initial ->
857 let l = CList.length initial in
858 let i = if i>0 then i-1 else i and j = if j>0 then j-1 else j in
859 let i = goodmod i l and j = goodmod j l in
860 CList.map_i begin fun k x ->
861 match k with
862 | k when Int.equal k i -> CList.nth initial j
863 | k when Int.equal k j -> CList.nth initial i
864 | _ -> x
865 end 0 initial
866 end
867
868 let revgoals =
869 let open Proof in
870 InfoL.leaf (Info.Tactic (fun () -> Pp.str"revgoals")) >>
871 Comb.modify CList.rev
872
873 let numgoals =
874 let open Proof in
875 Comb.get >>= fun comb ->
876 return (CList.length comb)
877
878
879
880 (** {7 Access primitives} *)
881
882 let tclEVARMAP = Solution.get
883
884 let tclENV = Env.get
885
886
887
888 (** {7 Put-like primitives} *)
889
890 let mark_as_unsafe = Status.put false
891
892 (** Gives up on the goal under focus. Reports an unsafe status. Proofs
893 with given up goals cannot be closed. *)
894
895 let give_up evs pv =
896 let solution = List.fold_left (fun sigma ev -> Evd.give_up (drop_state ev) sigma) pv.solution evs in
897 { pv with solution }
898
899 let give_up =
900 let open Proof in
901 Comb.get >>= fun initial ->
902 Comb.set [] >>
903 mark_as_unsafe >>
904 InfoL.leaf (Info.Tactic (fun () -> Pp.str"give_up")) >>
905 Pv.modify (give_up initial)
906
907
908 (** {7 Control primitives} *)
909
910 module Progress = struct
911
912 let eq_constr evd extended_evd =
913 Evarutil.eq_constr_univs_test ~evd ~extended_evd
914
915 (** equality function on hypothesis contexts *)
916 let eq_named_context_val sigma1 sigma2 ctx1 ctx2 =
917 let r_eq _ _ = true (* ignore relevances *) in
918 let c1 = EConstr.named_context_of_val ctx1 and c2 = EConstr.named_context_of_val ctx2 in
919 let eq_named_declaration d1 d2 =
920 match d1, d2 with
921 | LocalAssum (i1,t1), LocalAssum (i2,t2) ->
922 Context.eq_annot Names.Id.equal r_eq i1 i2 && eq_constr sigma1 sigma2 t1 t2
923 | LocalDef (i1,c1,t1), LocalDef (i2,c2,t2) ->
924 Context.eq_annot Names.Id.equal r_eq i1 i2 && eq_constr sigma1 sigma2 c1 c2
925 && eq_constr sigma1 sigma2 t1 t2
926 | _ ->
927 false
928 in
929 (* NB: can't use List.equal because it shortcuts on physical equality *)
930 List.for_all2eq eq_named_declaration c1 c2
931
932 let eq_evar_body (type a1 a2) sigma1 sigma2 (b1 : a1 Evd.evar_body) (b2 : a2 Evd.evar_body) =
933 let open Evd in
934 match b1, b2 with
935 | Evar_empty, Evar_empty -> true
936 | Evar_defined t1, Evar_defined t2 -> eq_constr sigma1 sigma2 t1 t2
937 | _ -> false
938
939 let eq_evar_concl (type a1 a2) sigma1 sigma2 (e1 : a1 Evd.evar_info) (e2 : a2 Evd.evar_info) =
940 let open Evd in
941 match Evd.evar_body e1, Evd.evar_body e2 with
942 | Evar_empty, Evar_empty -> eq_constr sigma1 sigma2 (Evd.evar_concl e1) (Evd.evar_concl e2)
943 | Evar_defined _, Evar_defined _ -> true
944 | _ -> false
945
946 let eq_evar_info sigma1 sigma2 ei1 ei2 =
947 eq_evar_concl sigma1 sigma2 ei1 ei2 &&
948 eq_named_context_val sigma1 sigma2 (Evd.evar_hyps ei1) (Evd.evar_hyps ei2) &&
949 eq_evar_body sigma1 sigma2 (Evd.evar_body ei1) (Evd.evar_body ei2)
950
951 let fast_eq_evar_body (type a1 a2) (e1 : a1 Evd.evar_info) (e2 : a2 Evd.evar_info) =
952 let open Evd in
953 match Evd.evar_body e1, Evd.evar_body e2 with
954 | Evar_empty, Evar_empty -> true
955 | Evar_defined _, Evar_defined _ -> true
956 | _ -> false
957
958 let fast_eq_named_context_val ctx1 ctx2 =
959 let r_eq _ _ = true (* ignore relevances *) in
960 let c1 = EConstr.named_context_of_val ctx1 in
961 let c2 = EConstr.named_context_of_val ctx2 in
962 let eq_named_declaration d1 d2 = match d1, d2 with
963 | LocalAssum (i1, _), LocalAssum (i2, _) -> Context.eq_annot Names.Id.equal r_eq i1 i2
964 | LocalDef (i1, _, _), LocalDef (i2, _, _) -> Context.eq_annot Names.Id.equal r_eq i1 i2
965 | _ -> false
966 in
967 List.for_all2eq eq_named_declaration c1 c2
968
969 let fast_eq_evar_info ei1 ei2 =
970 fast_eq_evar_body ei1 ei2 &&
971 fast_eq_named_context_val (Evd.evar_hyps ei1) (Evd.evar_hyps ei2)
972
973 (** Equality function on goals *)
974 let goal_equal ~evd ~extended_evd evar extended_evar =
975 let EvarInfo evi = Evd.find evd evar in
976 let EvarInfo extended_evi = Evd.find extended_evd extended_evar in
977 if fast_eq_evar_info evi extended_evi then
978 eq_evar_info evd extended_evd evi extended_evi
979 else false
980
981 end
982
983 let tclPROGRESS t =
984 let open Proof in
985 Pv.get >>= fun initial ->
986 t >>= fun res ->
987 Pv.get >>= fun final ->
988 (* [*_test] test absence of progress. [quick_test] is approximate
989 whereas [exhaustive_test] is complete. *)
990 let quick_test =
991 initial.solution == final.solution && initial.comb == final.comb
992 in
993 let test =
994 quick_test ||
995 (CList.same_length initial.comb final.comb &&
996 Util.List.for_all2eq begin fun i f ->
997 Progress.goal_equal ~evd:initial.solution
998 ~extended_evd:final.solution (drop_state i) (drop_state f)
999 end initial.comb final.comb)
1000 in
1001 if not test then
1002 tclUNIT res
1003 else
1004 let info = Exninfo.reify () in
1005 tclZERO ~info (CErrors.UserError Pp.(str "Failed to progress."))
1006
1007 let () = CErrors.register_handler begin function
1008 | Logic_monad.Tac_Timeout ->
1009 Some (Pp.str "Tactic timeout!")
1010 | _ -> None
1011 end
1012
1013 let tclTIMEOUTF n t =
1014 let open Proof in
1015 (* spiwack: as one of the monad is a continuation passing monad, it
1016 doesn't force the computation to be threaded inside the underlying
1017 (IO) monad. Hence I force it myself by asking for the evaluation of
1018 a dummy value first, lest [timeout] be called when everything has
1019 already been computed. *)
1020 let t = Proof.lift (Logic_monad.NonLogical.return ()) >> t in
1021 Proof.get >>= fun initial ->
1022 Proof.current >>= fun envvar ->
1023 Proof.lift begin
1024 let open Logic_monad.NonLogical in
1025 timeout n (Proof.repr (Proof.run t envvar initial)) >>= fun r ->
1026 match r with
1027 | Error info -> return (Util.Inr (Logic_monad.Tac_Timeout, info))
1028 | Ok (Logic_monad.Nil e) -> return (Util.Inr e)
1029 | Ok (Logic_monad.Cons (r, _)) -> return (Util.Inl r)
1030 end >>= function
1031 | Util.Inl (res,s,m,i) ->
1032 Proof.set s >>
1033 Proof.put m >>
1034 Proof.update (fun _ -> i) >>
1035 return res
1036 | Util.Inr (e, info) -> tclZERO ~info e
1037
1038 let tclTIMEOUT n t = tclTIMEOUTF (float_of_int n) t
1039
1040 let tclTIME s t =
1041 let pr_time t1 t2 n msg =
1042 let msg =
1043 if n = 0 then
1044 str msg
1045 else
1046 str (msg ^ " after ") ++ int n ++ str (String.plural n " backtracking")
1047 in
1048 Feedback.msg_info(str "Tactic call" ++ pr_opt str s ++ str " ran for " ++
1049 System.fmt_time_difference t1 t2 ++ str " " ++ surround msg) in
1050 let rec aux n t =
1051 let open Proof in
1052 tclUNIT () >>= fun () ->
1053 let tstart = System.get_time() in
1054 Proof.split t >>= let open Logic_monad in function
1055 | Nil (e, info) ->
1056 begin
1057 let tend = System.get_time() in
1058 pr_time tstart tend n "failure";
1059 tclZERO ~info e
1060 end
1061 | Cons (x,k) ->
1062 let tend = System.get_time() in
1063 pr_time tstart tend n "success";
1064 tclOR (tclUNIT x) (fun e -> aux (n+1) (k e))
1065 in aux 0 t
1066
1067 let tclProofInfo =
1068 let open Proof in
1069 Logical.current >>= fun P.{name; poly} ->
1070 tclUNIT (name, poly)
1071
1072 (** {7 Unsafe primitives} *)
1073
1074 module Unsafe = struct
1075
1076 let (>>=) = tclBIND
1077
1078 let tclEVARS evd =
1079 Pv.modify (fun ps -> { ps with solution = evd })
1080
1081 let tclNEWGOALS ?(before = false) gls =
1082 Pv.modify begin fun step ->
1083 let gls = undefined step.solution gls in
1084 let comb = if before then gls @ step.comb else step.comb @ gls in
1085 { step with comb }
1086 end
1087
1088 let tclNEWSHELVED gls =
1089 Pv.modify begin fun step ->
1090 let gls = undefined_evars step.solution gls in
1091 { step with solution = Evd.shelve step.solution gls }
1092 end
1093
1094 let tclGETSHELF = tclEVARMAP >>= fun sigma -> tclUNIT @@ Evd.shelf sigma
1095
1096 let tclSETENV = Env.set
1097
1098 let tclGETGOALS = Comb.get
1099
1100 let tclSETGOALS = Comb.set
1101
1102 let tclEVARSADVANCE evd =
1103 Pv.modify (fun ps -> { solution = evd; comb = undefined evd ps.comb })
1104
1105 let tclEVARUNIVCONTEXT ctx =
1106 Pv.modify (fun ps -> { ps with solution = Evd.set_universe_context ps.solution ctx })
1107
1108 let push_future_goals p =
1109 { p with solution = Evd.push_future_goals p.solution }
1110
1111 let mark_as_goals evd content =
1112 mark_in_evm ~goal:true evd content
1113
1114 let advance = Evarutil.advance
1115
1116 let undefined = undefined
1117
1118 let mark_unresolvables evm evs =
1119 mark_in_evm ~goal:false evm evs
1120
1121 let mark_as_unresolvables p evs =
1122 { p with solution = mark_in_evm ~goal:false p.solution evs }
1123
1124 let update_sigma_univs ugraph pv =
1125 { pv with solution = Evd.update_sigma_univs ugraph pv.solution }
1126
1127 let purge_side_effects pv =
1128 let effs = Evd.eval_side_effects pv.solution in
1129 { pv with solution = Evd.set_side_effects Evd.empty_side_effects pv.solution }, effs
1130
1131 end
1132
1133 module UnsafeRepr = Proof.Unsafe
1134
1135 let (>>=) = tclBIND
1136
1137 (** {6 Goal-dependent tactics} *)
1138
1139 let catchable_exception = function
1140 | Logic_monad.Exception _ -> false
1141 | e -> CErrors.noncritical e
1142
1143
1144 module Goal = struct
1145
1146 type t = {
1147 env : Environ.env;
1148 sigma : Evd.evar_map;
1149 concl : EConstr.constr ;
1150 state : StateStore.t;
1151 self : Evar.t ; (* for compatibility with old-style definitions *)
1152 }
1153
1154 let state { state=state } = state
1155
1156 let env {env} = env
1157 let sigma {sigma} = sigma
1158 let hyps {env} = EConstr.named_context env
1159 let concl {concl} = concl
1160 let relevance {sigma; self} =
1161 Evd.evar_relevance (Evd.find_undefined sigma self)
1162
1163 let gmake_with info env sigma goal state =
1164 { env = Environ.reset_with_named_context (Evd.evar_filtered_hyps info) env ;
1165 sigma = sigma ;
1166 concl = Evd.evar_concl info;
1167 state = state ;
1168 self = goal }
1169
1170 let gmake env sigma goal =
1171 let state = get_state goal in
1172 let goal = drop_state goal in
1173 let info = Evd.find_undefined sigma goal in
1174 gmake_with info env sigma goal state
1175
1176 let enter f =
1177 let f gl = InfoL.tag (Info.DBranch) (f gl) in
1178 InfoL.tag (Info.Dispatch) begin
1179 iter_goal begin fun goal ->
1180 Env.get >>= fun env ->
1181 tclEVARMAP >>= fun sigma ->
1182 try f (gmake env sigma goal)
1183 with e when catchable_exception e ->
1184 let (e, info) = Exninfo.capture e in
1185 tclZERO ~info e
1186 end
1187 end
1188
1189 let enter_one ?(__LOC__=__LOC__) f =
1190 let open Proof in
1191 Comb.get >>= function
1192 | [goal] -> begin
1193 Env.get >>= fun env ->
1194 tclEVARMAP >>= fun sigma ->
1195 try f (gmake env sigma goal)
1196 with e when catchable_exception e ->
1197 let (e, info) = Exninfo.capture e in
1198 tclZERO ~info e
1199 end
1200 | _ ->
1201 CErrors.anomaly Pp.(str __LOC__ ++ str " enter_one")
1202
1203 let goals =
1204 Pv.get >>= fun step ->
1205 let sigma = step.solution in
1206 let map goal =
1207 match cleared_alias sigma goal with
1208 | None -> None (* ppedrot: Is this check really necessary? *)
1209 | Some goal ->
1210 let oinfo = Evd.find_undefined sigma (drop_state goal) in
1211 let gl =
1212 Env.get >>= fun env ->
1213 tclEVARMAP >>= fun sigma ->
1214 let state = get_state goal in
1215 let goal = drop_state goal in
1216 let EvarInfo info = Evd.find sigma goal in
1217 let goal = {
1218 env = Environ.reset_with_named_context (Evd.evar_filtered_hyps info) env ;
1219 sigma = sigma ;
1220 concl = Evd.evar_concl oinfo;
1221 state = state;
1222 self = goal;
1223 } in
1224 tclUNIT goal
1225 in
1226 Some gl
1227 in
1228 tclUNIT (CList.map_filter map step.comb)
1229
1230 let unsolved { self=self } =
1231 tclEVARMAP >>= fun sigma ->
1232 tclUNIT (not (Option.is_empty (Evarutil.advance sigma self)))
1233
1234 (* compatibility *)
1235 let goal { self=self } = self
1236
1237 end
1238
1239
1240
1241 (** {6 Trace} *)
1242
1243 module Trace = struct
1244
1245 let record_info_trace = InfoL.record_trace
1246
1247 let log m = InfoL.leaf (Info.Msg m)
1248 let name_tactic m t = InfoL.tag (Info.Tactic m) t
1249
1250 let pr_info env sigma ?(lvl=0) info =
1251 assert (lvl >= 0);
1252 Info.(print env sigma (collapse lvl info))
1253
1254 end
1255
1256
1257
1258 (** {6 Non-logical state} *)
1259
1260 module NonLogical = Logic_monad.NonLogical
1261
1262 let tclLIFT = Proof.lift
1263
1264 let tclCHECKINTERRUPT =
1265 tclLIFT (NonLogical.make Control.check_for_interrupt)
1266
1267 let wrap_exceptions f =
1268 try f ()
1269 with e when catchable_exception e ->
1270 let (e, info) = Exninfo.capture e in tclZERO ~info e
1271
1272 (** {7 Notations} *)
1273
1274 module Notations = struct
1275 let (>>=) = tclBIND
1276 let (<*>) = tclTHEN
1277 let (<+>) t1 t2 = tclOR t1 (fun _ -> t2)
1278 end