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
Given any tactic that takes a goal, and returns a sequence of alternative outcomes (each outcome consisting of a list of new subgoals), we can perform backtracking search by repeatedly applying the tactic.
Equations
- Lean.MVarId.firstContinuation results cont g = Nondet.firstM (results g) fun r => Lean.observing? (cont r)
Instances For
- 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 apply a lemma. 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 applying lemmas without changing goals. Failure will cause backtracking. (defaults tonone
) - suspend : Lean.MVarId → Lean.MetaM Bool
If
suspend g
, then we do not attempt to apply any further lemmas, 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 no lemmas apply. 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.
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.
Instances For
Equations
- Mathlib.Tactic.ppMVarIds gs = List.mapM (fun g => do let __do_lift ← Lean.MVarId.getType g Lean.Meta.ppExpr __do_lift) gs
Instances For
Attempts to solve the goals
, by recursively calling alternatives g
on each subgoal that appears.
alternatives
returns a nondeterministic list of goals
(this is essentially a lazy list of List MVarId
,
with the extra state required to backtrack in MetaM
).
backtrack
performs a backtracking search, attempting to close all subgoals.
Further flow control options are available via the Config
argument.
Equations
- Mathlib.Tactic.backtrack cfg trace alternatives goals = Mathlib.Tactic.backtrack.processIndependentGoals cfg trace alternatives goals goals goals