| 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 |
Require Import Ltac2.Init. |
| 12 |
Require Ltac2.Message. |
| 13 |
|
| 14 |
(** Panic *) |
| 15 |
|
| 16 |
Ltac2 @ external throw : exn -> 'a := "rocq-runtime.plugins.ltac2" "throw". |
| 17 |
(** Fatal exception throwing. This does not induce backtracking. *) |
| 18 |
|
| 19 |
(** Generic backtracking control *) |
| 20 |
|
| 21 |
Ltac2 @ external zero : exn -> 'a := "rocq-runtime.plugins.ltac2" "zero". |
| 22 |
(** [zero e] raises the exception e, passing control to the current backtracking continuation. *) |
| 23 |
|
| 24 |
Ltac2 @ external plus : (unit -> 'a) -> (exn -> 'a) -> 'a := "rocq-runtime.plugins.ltac2" "plus". |
| 25 |
(** Backtracking point, cf Backtracking section of the ltac2 refman. *) |
| 26 |
|
| 27 |
Ltac2 @ external once : (unit -> 'a) -> 'a := "rocq-runtime.plugins.ltac2" "once". |
| 28 |
(** [once t] behaves like [t], except it has at most one success: [once t] |
| 29 |
stops after the first success of [t]. If [t] fails with [e], |
| 30 |
[once t] also fails with [e]. If [once t; t'] fails on [t'], then [once t] will not backtrack and evaluate [t] for another value, and will instead fail. *) |
| 31 |
|
| 32 |
Ltac2 @ external case : (unit -> 'a) -> ('a * (exn -> 'a)) result := "rocq-runtime.plugins.ltac2" "case". |
| 33 |
(** [case] is the most general primitive to control backtracking: |
| 34 |
- If [t ()] would fail with [e], [case t] returns [Err e]. |
| 35 |
- If [t ()] would succeed and evaluate to [v] then [case t] returns [Val (v, h)], |
| 36 |
where [h] is the continuation to execute in case of subsequent failure. |
| 37 |
[case] reifies a backtracking computation into an inspectable value, it allows the programmer to make explicit the effects which are normally implicit (i.e., they do not appear in the type system). |
| 38 |
*) |
| 39 |
|
| 40 |
Ltac2 once_plus (run : unit -> 'a) (handle : exn -> 'a) : 'a := |
| 41 |
once (fun () => plus run handle). |
| 42 |
(** [once_plus run handle] is [once] applied to [plus run handle]. *) |
| 43 |
|
| 44 |
(** Proof state manipulation *) |
| 45 |
|
| 46 |
Ltac2 @ external numgoals : unit -> int := "rocq-runtime.plugins.ltac2" "numgoals". |
| 47 |
(** Return the number of goals currently focused. *) |
| 48 |
|
| 49 |
Ltac2 @ external dispatch : (unit -> unit) list -> unit := "rocq-runtime.plugins.ltac2" "dispatch". |
| 50 |
(** The dispatch tactical is used to apply a different tactic to each |
| 51 |
goal under focus. It works by applying each of the tactics in a focus |
| 52 |
restricted to the corresponding goal (starting with the first |
| 53 |
goal). When the length of the tactic list is not equal to the |
| 54 |
number of focused goals, raises Internal err. *) |
| 55 |
|
| 56 |
Ltac2 @ external extend : (unit -> unit) list -> (unit -> unit) -> (unit -> unit) list -> unit := "rocq-runtime.plugins.ltac2" "extend". |
| 57 |
(** extend is a more flexible variant of dispatch, where the second argument |
| 58 |
tactic is "repeated" enough times such that every goal has a tactic |
| 59 |
assigned to it. [extend b r e] applies the tactics in [b] to the first |
| 60 |
[length b] goals, the tactics in [r] to the last [length r] goals, and |
| 61 |
[e] to all tactics in between. Raises Internal err if [length b + length r] |
| 62 |
is greater than the number of goals under focus.*) |
| 63 |
|
| 64 |
Ltac2 @ external enter : (unit -> unit) -> unit := "rocq-runtime.plugins.ltac2" "enter". |
| 65 |
(** [enter] takes a single tactic [t] and applies [t] in each goal under focus independently. *) |
| 66 |
|
| 67 |
Ltac2 @ external focus : int -> int -> (unit -> 'a) -> 'a := "rocq-runtime.plugins.ltac2" "focus". |
| 68 |
(** [focus i j tac] focuses a proofview on the goals from index [i] to |
| 69 |
index [j] (inclusive, goals are indexed from [1]) and runs |
| 70 |
t with those goals under focus, i.e. goals |
| 71 |
number [i] to [j] become the only focused goals during the execution |
| 72 |
of tac. When [focus] returns, the present focus is restored. If the range [i - j] is invalid, fails with a backtrackable "no such goal" error. *) |
| 73 |
|
| 74 |
Ltac2 @ external cycle : int -> unit := "rocq-runtime.plugins.ltac2" "cycle". |
| 75 |
(** If [n] is positive, [cycle n] puts the [n] first goal last. If [n] |
| 76 |
is negative, then it puts the [n] last goals first.*) |
| 77 |
|
| 78 |
Ltac2 @ external shelve : unit -> unit := "rocq-runtime.plugins.ltac2" "shelve". |
| 79 |
(** Shelve all goals under focus. The goals are placed on the shelf for later use, or to be solved by side-effects.*) |
| 80 |
|
| 81 |
Ltac2 @ external shelve_unifiable : unit -> unit := "rocq-runtime.plugins.ltac2" "shelve_unifiable". |
| 82 |
(** Shelves the unifiable goals under focus, i.e. the goals which |
| 83 |
appear in other goals under focus (the unfocused goals are not |
| 84 |
considered). *) |
| 85 |
|
| 86 |
Ltac2 @ external new_goal : evar -> unit := "rocq-runtime.plugins.ltac2" "new_goal". |
| 87 |
(** Adds the given evar to the list of goals as the last one. If it is |
| 88 |
already defined in the current state, don't do anything. Panics if the |
| 89 |
evar is not in the current state. *) |
| 90 |
|
| 91 |
Ltac2 @ external unshelve : (unit -> 'a) -> 'a := "rocq-runtime.plugins.ltac2" "unshelve". |
| 92 |
(** Runs the closure, then unshelves existential variables added to the |
| 93 |
shelf by its execution, prepending them to the current goal. |
| 94 |
Returns the value produced by the closure. *) |
| 95 |
|
| 96 |
Ltac2 @ external progress : (unit -> 'a) -> 'a := "rocq-runtime.plugins.ltac2" "progress". |
| 97 |
(** [progress t] checks the state of the proof after [t]. It it is |
| 98 |
identical to the state before, then [progress t] fails, otherwise |
| 99 |
it succeeds like [t]. *) |
| 100 |
|
| 101 |
(** Goal inspection *) |
| 102 |
|
| 103 |
Ltac2 @ external goal : unit -> constr := "rocq-runtime.plugins.ltac2" "goal". |
| 104 |
(** Panics if there is not exactly one goal under focus. Otherwise returns |
| 105 |
the conclusion of this goal. *) |
| 106 |
|
| 107 |
Ltac2 @ external hyp : ident -> constr := "rocq-runtime.plugins.ltac2" "hyp". |
| 108 |
(** Panics if there is more than one goal under focus. If there is no |
| 109 |
goal under focus, looks for the section variable with the given name. |
| 110 |
If there is one, looks for the hypothesis with the given name. *) |
| 111 |
|
| 112 |
Ltac2 @ external hyp_value : ident -> constr option := "rocq-runtime.plugins.ltac2" "hyp_value". |
| 113 |
(** Panics if there is more than one goal under focus. If there is no |
| 114 |
goal under focus, looks for the section variable with the given |
| 115 |
name and return its value ("v" in "H := v") if there is one. If |
| 116 |
there is one, looks for the hypothesis with the given name and |
| 117 |
return its value if there is one. *) |
| 118 |
|
| 119 |
Ltac2 @ external hyps : unit -> (ident * constr option * constr) list := "rocq-runtime.plugins.ltac2" "hyps". |
| 120 |
(** Panics if there is more than one goal under focus. If there is no |
| 121 |
goal under focus, returns the list of section variables. |
| 122 |
If there is one, returns the list of hypotheses. In both cases, the |
| 123 |
list is ordered with rightmost values being last introduced. *) |
| 124 |
|
| 125 |
(** Refinement *) |
| 126 |
|
| 127 |
Ltac2 @ external refine : (unit -> constr) -> unit := "rocq-runtime.plugins.ltac2" "refine". |
| 128 |
(** [refine t] computes the type of the term [t ()] and unifies the |
| 129 |
type with the current goal. All unification variables produced while computing [t ()] not |
| 130 |
solved by this unification process are added as new goals. *) |
| 131 |
|
| 132 |
(** Evars *) |
| 133 |
|
| 134 |
Ltac2 @ external with_holes : (unit -> 'a) -> ('a -> 'b) -> 'b := "rocq-runtime.plugins.ltac2" "with_holes". |
| 135 |
(** [with_holes x f] evaluates [x], then apply [f] to the result, and fails if |
| 136 |
all evars generated by the call to [x] have not been solved when [f] |
| 137 |
returns. *) |
| 138 |
|
| 139 |
(** Misc *) |
| 140 |
|
| 141 |
Ltac2 @ external time : string option -> (unit -> 'a) -> 'a := "rocq-runtime.plugins.ltac2" "time". |
| 142 |
(** Displays the time taken by a tactic to evaluate. *) |
| 143 |
|
| 144 |
Ltac2 @ external abstract : ident option -> (unit -> unit) -> unit := "rocq-runtime.plugins.ltac2" "abstract". |
| 145 |
(** Abstract a subgoal. *) |
| 146 |
|
| 147 |
Ltac2 @ external check_interrupt : unit -> unit := "rocq-runtime.plugins.ltac2" "check_interrupt". |
| 148 |
(** For internal use. *) |
| 149 |
|
| 150 |
(** Assertions throwing exceptions and short form throws *) |
| 151 |
|
| 152 |
Ltac2 throw_invalid_argument (msg : string) := |
| 153 |
Control.throw (Invalid_argument (Some (Message.of_string msg))). |
| 154 |
|
| 155 |
Ltac2 throw_out_of_bounds (msg : string) := |
| 156 |
Control.throw (Out_of_bounds (Some (Message.of_string msg))). |
| 157 |
|
| 158 |
Ltac2 assert_valid_argument (msg : string) (test : bool) := |
| 159 |
match test with |
| 160 |
| true => () |
| 161 |
| false => throw_invalid_argument msg |
| 162 |
end. |
| 163 |
|
| 164 |
Ltac2 assert_bounds (msg : string) (test : bool) := |
| 165 |
match test with |
| 166 |
| true => () |
| 167 |
| false => throw_out_of_bounds msg |
| 168 |
end. |
| 169 |
|
| 170 |
Ltac2 assert_true b := |
| 171 |
if b then () else throw Assertion_failure. |
| 172 |
|
| 173 |
Ltac2 assert_false b := |
| 174 |
if b then throw Assertion_failure else (). |
| 175 |
|
| 176 |
(** Short form backtracks *) |
| 177 |
|
| 178 |
Ltac2 backtrack_tactic_failure (msg : string) := |
| 179 |
Control.zero (Tactic_failure (Some (Message.of_string msg))). |
| 180 |
|
| 181 |
(** Backtraces. *) |
| 182 |
|
| 183 |
Ltac2 @external current_exninfo : unit -> exninfo |
| 184 |
:= "rocq-runtime.plugins.ltac2" "current_exninfo". |
| 185 |
|
| 186 |
(** [throw_bt info e] is similar to [throw e], but raises [e] with the |
| 187 |
backtrace represented by [info]. *) |
| 188 |
Ltac2 @ external throw_bt : exn -> exninfo -> 'a := |
| 189 |
"rocq-runtime.plugins.ltac2" "throw_bt". |
| 190 |
|
| 191 |
(** [zero_bt info e] is similar to [zero e], but raises [e] with the |
| 192 |
backtrace represented by [info]. *) |
| 193 |
Ltac2 @ external zero_bt : exn -> exninfo -> 'a := |
| 194 |
"rocq-runtime.plugins.ltac2" "zero_bt". |
| 195 |
|
| 196 |
(** [plus_bt run handle] is similar to [plus run handle] (up to the type |
| 197 |
missmatch for [handle]), but it calls [handle] with an extra argument |
| 198 |
representing the backtrace at the point of the exception. The [handle] |
| 199 |
function can thus decide to re-attach that backtrace when using the |
| 200 |
[throw_bt] or [zero_bt] functions. *) |
| 201 |
Ltac2 @ external plus_bt : (unit -> 'a) -> (exn -> exninfo -> 'a) -> 'a := |
| 202 |
"rocq-runtime.plugins.ltac2" "plus_bt". |
| 203 |
|
| 204 |
(** [once_plus_bt run handle] is a non-backtracking variant of [once_plus] |
| 205 |
that has backtrace support similar to that of [plus_bt]. *) |
| 206 |
Ltac2 once_plus_bt (run : unit -> 'a) (handle : exn -> exninfo -> 'a) : 'a := |
| 207 |
once (fun _ => plus_bt run handle). |
| 208 |
|
| 209 |
Ltac2 @ external clear_err_info : err -> err := |
| 210 |
"rocq-runtime.plugins.ltac2" "clear_err_info". |
| 211 |
|
| 212 |
Ltac2 clear_exn_info (e : exn) : exn := |
| 213 |
match e with |
| 214 |
| Init.Internal err => Init.Internal (clear_err_info err) |
| 215 |
| e => e |
| 216 |
end. |
| 217 |
|
| 218 |
(** Timeout. *) |
| 219 |
|
| 220 |
(** [timeout t thunk] calls [thunk ()] with a timeout of [t] seconds. *) |
| 221 |
Ltac2 @ external timeout : int -> (unit -> 'a) -> 'a := |
| 222 |
"rocq-runtime.plugins.ltac2" "timeout". |
| 223 |
|
| 224 |
(** [timeoutf t thunk] calls [thunk ()] with a timeout of [t] seconds. *) |
| 225 |
Ltac2 @ external timeoutf : float -> (unit -> 'a) -> 'a := |
| 226 |
"rocq-runtime.plugins.ltac2" "timeoutf". |
| 227 |
|
| 228 |
(** Error printing *) |
| 229 |
|
| 230 |
(** Print internal errors. *) |
| 231 |
Ltac2 @external print_err : err -> message |
| 232 |
:= "rocq-runtime.plugins.ltac2" "print_err". |
| 233 |
|
| 234 |
(** Print exceptions as errors. Used by the runtime when printing uncaught errors. |
| 235 |
Extensible by mutation, see uses below. |
| 236 |
|
| 237 |
IMPORTANT: when called for printing uncaught errors, it is run in an empty state |
| 238 |
(no goals, empty evar map). |
| 239 |
|
| 240 |
Also note that the "Internal" branch is not used when printing |
| 241 |
uncaught errors as Internal exceptions are not considered as Ltac2 |
| 242 |
errors. *) |
| 243 |
Ltac2 mutable print_exn : exn -> message option := fun e => |
| 244 |
match e with |
| 245 |
| Internal e => Some (print_err e) |
| 246 |
| _ => None |
| 247 |
end. |
| 248 |
|
| 249 |
#[global] |
| 250 |
Ltac2 Set print_exn as print_other := fun e => |
| 251 |
match e with |
| 252 |
| Tactic_failure (Some msg) => Some msg |
| 253 |
| _ => print_other e |
| 254 |
end. |