backtrack
#
A meta-tactic for running backtracking search, given a non-deterministic tactic
alternatives : MVarId → Nondet MetaM (List MVarId)
.
backtrack alternatives goals
will recursively try to solve all goals in goals
,
and the subgoals generated, backtracking as necessary.
In its default behaviour, it will either solve all goals, or fail.
A customisable suspend
hook in BacktrackConfig
allows suspend a goal (or subgoal),
so that it will be returned instead of processed further.
Other hooks proc
and discharge
(described in BacktrackConfig
) allow running other
tactics before alternatives
, or if all search branches from a given goal fail.
Currently only solveByElim
is implemented in terms of backtrack
.
Visualize an Except
using a checkmark or a cross.
Equations
- Except.emoji x = match x with | Except.error a => Lean.crossEmoji | Except.ok a => Lean.checkEmoji
Instances For
Run a monadic function on every element of a list, returning the list of elements on which the function fails, and the list of successful results.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Configuration structure to control the behaviour of backtrack
:
- control the maximum depth and behaviour (fail or return subgoals) at the maximum depth,
- and hooks allowing
- modifying intermediate goals before running the external tactic,
- 'suspending' goals, returning them in the result, and
- discharging subgoals if the external tactic fails.
- maxDepth : Nat
Maximum recursion depth.
- proc : List Lean.MVarId → List Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))
An arbitrary procedure which can be used to modify the list of goals before each attempt to generate alternatives. Called as
proc goals curr
, wheregoals
are the original goals forbacktracking
, andcurr
are the current goals. Returningsome l
will replace the current goals withl
and recurse (consuming one step of maximum depth). Returningnone
will proceed to generating alternative without changing goals. Failure will cause backtracking. (defaults tonone
) - suspend : Lean.MVarId → Lean.MetaM Bool
If
suspend g
, then we do not consider alternatives forg
, but returng
as a new subgoal. (defaults tofalse
) - discharge : Lean.MVarId → Lean.MetaM (Option (List Lean.MVarId))
discharge g
is called on goals for which there were no alternatives. Ifnone
we returng
as a new subgoal. Ifsome l
, we replaceg
byl
in the list of active goals, and recurse. If failure, we backtrack. (defaults to failure) - commitIndependentGoals : Bool
If we solve any "independent" goals, don't fail. See
Lean.MVarId.independent?
for the definition of independence.
Instances For
Attempts to solve the goals
, by recursively calling alternatives g
on each subgoal that appears.
alternatives
returns a nondeterministic list of new subgoals generated from a goal.
backtrack
performs a backtracking search, attempting to close all subgoals.
Further flow control options are available via the Config
argument.
Returns a list of subgoals which were "suspended" via the suspend
or discharge
hooks
in Config
. In the default configuration, backtrack
will either return an empty list or fail.
Equations
- Mathlib.Tactic.backtrack cfg trace alternatives goals = Mathlib.Tactic.backtrack.processIndependentGoals cfg trace alternatives goals goals goals
Instances For
A wrapper around run
, which works on "independent" goals separately first,
to reduce backtracking.
Pretty print a list of goals.
Equations
- Mathlib.Tactic.backtrack.ppMVarIds gs = List.mapM (fun (g : Lean.MVarId) => do let __do_lift ← Lean.MVarId.getType g Lean.Meta.ppExpr __do_lift) gs