| /- | |
| Copyright (c) 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Leonardo de Moura | |
| -/ | |
| module | |
| prelude | |
| public import Lean.Meta.Tactic.Grind.Action | |
| public import Lean.Meta.Tactic.Grind.Anchor | |
| import Lean.Meta.Tactic.Grind.Intro | |
| import Lean.Meta.Tactic.Grind.Util | |
| import Lean.Meta.Tactic.Grind.CasesMatch | |
| import Lean.Meta.Tactic.Grind.Internalize | |
| import Init.Data.List.MapIdx | |
| import Init.Grind.Util | |
| import Init.Omega | |
| public section | |
| namespace Lean.Meta.Grind | |
| inductive SplitStatus where | |
| | resolved | |
| | notReady | |
| | ready (numCases : Nat) (isRec := false) (tryPostpone := false) | |
| deriving Inhabited, BEq, Repr | |
| /-- Given `c`, the condition of an `if-then-else`, check whether we need to case-split on the `if-then-else` or not -/ | |
| private def checkIteCondStatus (c : Expr) : GoalM SplitStatus := do | |
| if (← isEqTrue c <||> isEqFalse c) then | |
| return .resolved | |
| else | |
| return .ready 2 | |
| /-- | |
| Given `e` of the form `a ∨ b`, check whether we are ready to case-split on `e`. | |
| That is, `e` is `True`, but neither `a` nor `b` is `True`." | |
| -/ | |
| private def checkDisjunctStatus (e a b : Expr) : GoalM SplitStatus := do | |
| if (← isEqTrue e) then | |
| if (← isEqTrue a <||> isEqTrue b) then | |
| return .resolved | |
| else | |
| return .ready 2 | |
| else if (← isEqFalse e) then | |
| return .resolved | |
| else | |
| return .notReady | |
| /-- | |
| Given `e` of the form `a ∧ b`, check whether we are ready to case-split on `e`. | |
| That is, `e` is `False`, but neither `a` nor `b` is `False`. | |
| -/ | |
| private def checkConjunctStatus (e a b : Expr) : GoalM SplitStatus := do | |
| if (← isEqTrue e) then | |
| return .resolved | |
| else if (← isEqFalse e) then | |
| if (← isEqFalse a <||> isEqFalse b) then | |
| return .resolved | |
| else | |
| return .ready 2 | |
| else | |
| return .notReady | |
| /-- | |
| Given `e` of the form `@Eq Prop a b`, check whether we are ready to case-split on `e`. | |
| There are two cases: | |
| 1- `e` is `True`, but neither both `a` and `b` are `True`, nor both `a` and `b` are `False`. | |
| 2- `e` is `False`, but neither `a` is `True` and `b` is `False`, nor `a` is `False` and `b` is `True`. | |
| -/ | |
| private def checkIffStatus (e a b : Expr) : GoalM SplitStatus := do | |
| if (← isEqTrue e) then | |
| if (← (isEqTrue a <&&> isEqTrue b) <||> (isEqFalse a <&&> isEqFalse b)) then | |
| return .resolved | |
| else | |
| return .ready 2 | |
| else if (← isEqFalse e) then | |
| if (← (isEqTrue a <&&> isEqFalse b) <||> (isEqFalse a <&&> isEqTrue b)) then | |
| return .resolved | |
| else | |
| return .ready 2 | |
| else | |
| return .notReady | |
| /-- Returns `true` if `c` is congruent to a case-split that was already performed. -/ | |
| private def isCongrToPrevSplit (c : Expr) : GoalM Bool := do | |
| unless c.isApp do return false | |
| (← get).split.resolved.foldM (init := false) fun flag { expr := c' } => do | |
| if flag then | |
| return true | |
| else | |
| return c'.isApp && (← get).isCongruent c c' | |
| private def checkDefaultSplitStatus (e : Expr) : GoalM SplitStatus := do | |
| match_expr e with | |
| | Or a b => checkDisjunctStatus e a b | |
| | And a b => checkConjunctStatus e a b | |
| | Eq _ a b => | |
| if isMorallyIff e then | |
| checkIffStatus e a b | |
| else | |
| return .ready 2 | |
| | _ => | |
| if isIte e || isDIte e then | |
| return (← checkIteCondStatus (e.getArg! 1)) | |
| if (← isResolvedCaseSplit e) then | |
| trace_goal[grind.debug.split] "split resolved: {e}" | |
| return .resolved | |
| if (← isCongrToPrevSplit e) then | |
| return .resolved | |
| if let some info := isMatcherAppCore? (← getEnv) e then | |
| return .ready info.numAlts | |
| if let .const declName .. := e.getAppFn then | |
| if let some info ← isInductivePredicate? declName then | |
| if (← isEqTrue e) then | |
| return .ready info.ctors.length info.isRec | |
| if e.isFVar then | |
| let type ← whnfD (← inferType e) | |
| let report : GoalM Unit := do | |
| reportIssue! "cannot perform case-split on {e}, unexpected type{indentExpr type}" | |
| let .const declName _ := type.getAppFn | report; return .resolved | |
| let .inductInfo info ← getConstInfo declName | report; return .resolved | |
| return .ready info.ctors.length info.isRec | |
| return .notReady | |
| def checkSplitInfoArgStatus (a b : Expr) (eq : Expr) : GoalM SplitStatus := do | |
| if (← isEqTrue eq <||> isEqFalse eq) then return .resolved | |
| let is := (← get).split.argPosMap[(a, b)]? |>.getD [] | |
| let mut j := a.getAppNumArgs | |
| let mut it_a := a | |
| let mut it_b := b | |
| repeat | |
| unless it_a.isApp && it_b.isApp do return .ready 2 | |
| j := j - 1 | |
| if j ∉ is then | |
| let arg_a := it_a.appArg! | |
| let arg_b := it_b.appArg! | |
| unless (← isEqv arg_a arg_b) do | |
| trace_goal[grind.split] "may be irrelevant\na: {a}\nb: {b}\neq: {eq}\narg_a: {arg_a}\narg_b: {arg_b}, gen: {← getGeneration eq}" | |
| /- | |
| We tried to return `.notReady` because we would not be able to derive a congruence, but | |
| `grind_ite.lean` breaks when this heuristic is used. TODO: understand better why. | |
| -/ | |
| return .ready 2 (tryPostpone := true) | |
| it_a := it_a.appFn! | |
| it_b := it_b.appFn! | |
| return .ready 2 | |
| private def checkForallStatus (imp : Expr) (h : imp.isForall) : GoalM SplitStatus := do | |
| if (← isEqTrue imp) then | |
| let p := imp.forallDomain h | |
| let q := imp.forallBody h | |
| if (← isEqTrue p <||> isEqFalse p) then | |
| return .resolved | |
| unless q.hasLooseBVars do | |
| if (← isEqTrue q <||> isEqFalse q) then | |
| return .resolved | |
| return .ready 2 | |
| else if (← isEqFalse imp) then | |
| return .resolved | |
| else | |
| return .notReady | |
| def checkSplitStatus (s : SplitInfo) : GoalM SplitStatus := do | |
| match s with | |
| | .default e _ => checkDefaultSplitStatus e | |
| | .imp e h _ => checkForallStatus e h | |
| | .arg a b _ eq _ => checkSplitInfoArgStatus a b eq | |
| private inductive SplitCandidate where | |
| | none | |
| | some (c : SplitInfo) (numCases : Nat) (isRec : Bool) (tryPostpone : Bool) | |
| /-- | |
| Returns `true`, if there are no anchor references restricting the search, | |
| or there is an anchor references `ref` s.t. `ref` matches `c`. | |
| -/ | |
| private def checkAnchorRefs (c : SplitInfo) : GrindM Bool := do | |
| let some anchorRefs ← getAnchorRefs | return true | |
| let anchor ← c.getAnchor | |
| return anchorRefs.any (·.matches anchor) | |
| /-- Returns the next case-split to be performed. It uses a very simple heuristic. -/ | |
| private def selectNextSplit? : GoalM SplitCandidate := do | |
| if (← isInconsistent) then return .none | |
| if (← checkMaxCaseSplit) then return .none | |
| go (← get).split.candidates .none [] | |
| where | |
| go (cs : List SplitInfo) (c? : SplitCandidate) (cs' : List SplitInfo) : GoalM SplitCandidate := do | |
| match cs with | |
| | [] => | |
| modify fun s => { s with split.candidates := cs'.reverse } | |
| if let .some .. := c? then | |
| -- Remark: we reset `numEmatch` after each case split. | |
| -- We should consider other strategies in the future. | |
| modify fun s => { s with ematch.num := 0 } | |
| return c? | |
| | c::cs => | |
| if !(← checkAnchorRefs c) then | |
| /- | |
| **Note**: `grind`s context contains anchor references restricting the | |
| case-splits that can be performed, and `c` does not matches any of | |
| the references provided. | |
| -/ | |
| go cs c? cs' | |
| else | |
| trace_goal[grind.debug.split] "checking: {c.getExpr}" | |
| match (← checkSplitStatus c) with | |
| | .notReady => go cs c? (c::cs') | |
| | .resolved => go cs c? cs' | |
| | .ready numCases isRec tryPostpone => | |
| if (← cheapCasesOnly) && numCases > 1 then | |
| go cs c? (c::cs') | |
| else match c? with | |
| | .none => go cs (.some c numCases isRec tryPostpone) cs' | |
| | .some c' numCases' _ tryPostpone' => | |
| let isBetter : GoalM Bool := do | |
| if tryPostpone' && !tryPostpone then | |
| return true | |
| else if tryPostpone && !tryPostpone' then | |
| return false | |
| else if numCases == 1 && !isRec && numCases' > 1 then | |
| return true | |
| /- | |
| **Note**: We used to use `getGeneration c.getExpr` instead of `c.getGeneration`. | |
| This was incorrect. The expression returned by `c.getExpr` may have not been internalized yet. | |
| -/ | |
| else if (← c.getGeneration) < (← c'.getGeneration) then | |
| return true | |
| return numCases < numCases' | |
| if (← isBetter) then | |
| go cs (.some c numCases isRec tryPostpone) (c'::cs') | |
| else | |
| go cs c? (c::cs') | |
| private def mkGrindEM (c : Expr) := | |
| mkApp (mkConst ``Lean.Grind.em) c | |
| /-- Constructs a major premise for the `cases` tactic used by `grind`. -/ | |
| private def mkCasesMajor (c : Expr) : GoalM Expr := do | |
| match_expr c with | |
| | And a b => return mkApp3 (mkConst ``Grind.or_of_and_eq_false) a b (← mkEqFalseProof c) | |
| | Eq _ a b => | |
| if isMorallyIff c then | |
| if (← isEqTrue c) then | |
| return mkApp3 (mkConst ``Grind.of_eq_eq_true) a b (← mkEqTrueProof c) | |
| else | |
| return mkApp3 (mkConst ``Grind.of_eq_eq_false) a b (← mkEqFalseProof c) | |
| else | |
| -- model-based theory combination split | |
| return mkGrindEM c | |
| | Not e => return mkGrindEM e | |
| | _ => | |
| if isIte c || isDIte c then | |
| return mkGrindEM (c.getArg! 1) | |
| else if (← isEqTrue c) then | |
| return mkOfEqTrueCore c (← mkEqTrueProof c) | |
| else | |
| return c | |
| private def casesWithTrace (mvarId : MVarId) (major : Expr) : GoalM (List MVarId) := do | |
| if (← getConfig).trace then | |
| if let .const declName _ := (← whnfD (← inferType major)).getAppFn then | |
| saveCases declName | |
| cases mvarId major | |
| structure SplitCandidateWithAnchor where | |
| c : SplitInfo | |
| numCases : Nat | |
| isRec : Bool | |
| e : Expr | |
| anchor : UInt64 | |
| instance : HasAnchor SplitCandidateWithAnchor where | |
| getAnchor e := e.anchor | |
| structure SplitCandidateAnchors where | |
| /-- Pairs `(anchor, split)` -/ | |
| candidates : Array SplitCandidateWithAnchor | |
| /-- Number of digits (≥ 4) sufficient for distinguishing anchors. We usually display only the first `numDigits`. -/ | |
| numDigits : Nat | |
| /-- | |
| Returns case-split candidates. Case-splits that are tagged as `.resolved` or `.notReady` are skipped. | |
| Applies additional `filter` if provided. | |
| If `candidates?` is provided, it is used instead of the candidates stored in the goal. | |
| -/ | |
| def getSplitCandidateAnchors (filter : Expr → GoalM Bool := fun _ => return true) | |
| (candidates? : Option (List SplitInfo) := none) : GoalM SplitCandidateAnchors := do | |
| let candidates ← match candidates? with | |
| | some candidates => pure candidates | |
| | none => pure (← get).split.candidates | |
| let candidates ← candidates.toArray.filterMapM fun c => do | |
| let e := c.getExpr | |
| let anchor ← c.getAnchor | |
| let status ← checkSplitStatus c | |
| -- **Note**: we ignore case-splits that are not ready or have already been resolved. | |
| -- We may consider adding an option for including "not-ready" splits in the future. | |
| match status with | |
| | .resolved | .notReady => return none | |
| | .ready numCases isRec _ => | |
| if (← filter e) then | |
| return some { e, c, numCases, isRec, anchor } | |
| else | |
| return none | |
| -- **TODO**: Add an option for including propositions that are only considered when using `+splitImp` | |
| -- **TODO**: Add an option for including terms whose type is an inductive predicate or type | |
| let numDigits := getNumDigitsForAnchors candidates | |
| return { candidates, numDigits } | |
| /-- | |
| Anchor reference for a case-split candidate in generated tactic scripts. | |
| `ordinal` is the 0-based position of the candidate among the (ready) candidates whose | |
| anchors share the same displayed prefix. Distinct candidates may have identical anchors | |
| (e.g., candidates that differ only in inaccessible variables), and the ordinal is used | |
| to disambiguate them. | |
| -/ | |
| structure SplitAnchorRefInfo where | |
| numDigits : Nat | |
| anchor : UInt64 | |
| ordinal : Nat | |
| /-- | |
| Computes the anchor reference information for the case-split candidate `c`. | |
| The candidate list must be the one stored in the goal before the case-split is selected and | |
| performed (i.e., before `selectNextSplit?` prunes the list and before `c` is marked as resolved), | |
| so that it matches the list inspected by the `cases` tactic when replaying the generated script. | |
| -/ | |
| def mkSplitAnchorRefInfo (c : SplitInfo) (candidates? : Option (List SplitInfo) := none) : GoalM SplitAnchorRefInfo := do | |
| let { candidates, numDigits } ← getSplitCandidateAnchors (candidates? := candidates?) | |
| let anchor ← c.getAnchor | |
| let shift := (64 - 4*numDigits).toUInt64 | |
| let mut ordinal := 0 | |
| for cand in candidates do | |
| if cand.anchor >>> shift == anchor >>> shift then | |
| if cand.c == c then | |
| return { numDigits, anchor, ordinal } | |
| ordinal := ordinal + 1 | |
| -- `c` should be in the candidate list; fall back to the first match. | |
| return { numDigits, anchor, ordinal := 0 } | |
| /-- | |
| Creates the anchor reference syntax for the result of `mkSplitAnchorRefInfo`. | |
| -/ | |
| def SplitAnchorRefInfo.toSyntax (info : SplitAnchorRefInfo) : CoreM (TSyntax `grind) := do | |
| let anchorStx ← mkAnchorSyntax info.numDigits info.anchor | |
| if info.ordinal == 0 then | |
| `(grind| cases $anchorStx:anchor) | |
| else | |
| `(grind| cases $anchorStx:anchor/$(quote (info.ordinal + 1)):num) | |
| namespace Action | |
| /-- | |
| Given a `mvarId` associated with a subgoal created by `splitCore`, inspects the | |
| proof term assigned to `mvarId` and tries to extract the proof of `False` that does not | |
| depend on hypotheses introduced in the subgoal. | |
| For example: suppose the subgoal is of the form `p → q → False` where `p` and `q` are new | |
| hypotheses introduced during case analysis. If the proof is of the form `fun _ _ => h`, returns | |
| `some h`. | |
| -/ | |
| private def getFalseProofEager? (mvarId : MVarId) : MetaM (Option Expr) := mvarId.withContext do | |
| let proof ← instantiateMVars (mkMVar mvarId) | |
| if proof.hasSyntheticSorry then | |
| /- **Note**: We do not perform non-chronological backtracking if the proof | |
| contains synthetic `sorry`. -/ | |
| return none | |
| else | |
| go proof | |
| where | |
| go (proof : Expr) : MetaM (Option Expr) := do | |
| match_expr proof with | |
| | False.elim _ p => return some p | |
| | False.casesOn _ p => return some p | |
| | id α p => if α.isFalse then return some p else return none | |
| | _ => | |
| /- | |
| **Note**: `intros` tactics may hide the `False` proof behind a `casesOn` | |
| For example: suppose the subgoal has a type of the form `p₁ → q₁ ∧ q₂ → p₂ → False` | |
| The proof will be of the form `fun _ h => h.casesOn (fun _ _ => hf)` where `hf` is the proof | |
| of `False` we are looking for. | |
| Non-chronological backtracking currently fails in this kind of example. | |
| -/ | |
| let .lam _ _ b _ := proof | return none | |
| if b.hasLooseBVars then return none | |
| go b | |
| /-- | |
| Similar to `getFalseProofEager?`, but without instantiating the proof, which would cost time | |
| proportional to the size of the proof at every enclosing case split. The proof of a subgoal that | |
| introduces hypotheses `xs` is usually of the form `fun xs => ?d xs`, where `?d` is delayed-assigned to | |
| `?p` abstracting `xs`. The body does not depend on `xs` if the summary of `?p` does not contain `xs`, | |
| see `getProofInfo`. The returned proof is not instantiated either. | |
| -/ | |
| private partial def getFalseProof? (mvarId : MVarId) : GrindM (Option Expr) := mvarId.withContext do | |
| let info ← getProofInfo (mkMVar mvarId) | |
| if info.hasSorry || info.hasUnassigned then | |
| /- **Note**: We do not perform non-chronological backtracking if the proof | |
| contains synthetic `sorry`, or if it is not complete and may still depend on `xs`. -/ | |
| return none | |
| go (mkMVar mvarId) | |
| where | |
| go (proof : Expr) : GrindM (Option Expr) := do | |
| let proof := proof.headBeta | |
| if let .mvar m := proof.getAppFn then | |
| if let some val ← getExprMVarAssignment? m then | |
| return (← go (val.beta proof.getAppArgs)) | |
| match_expr proof with | |
| | False.elim _ p => return some p | |
| | False.casesOn _ p => return some p | |
| | id α p => if α.isFalse then return some p else return none | |
| | _ => | |
| let .lam .. := proof | return none | |
| let n := proof.getNumHeadLambdas | |
| let b := lambdaBody n proof | |
| if !b.hasLooseBVars then return (← go b) | |
| let .mvar d := b.getAppFn | fallback | |
| let args := b.getAppArgs | |
| unless args.size == n && args.toList.zipIdx.all (fun (a, i) => a == mkBVar (n - 1 - i)) do | |
| return (← fallback) | |
| let some { fvars, mvarIdPending } ← getDelayedMVarAssignment? d | fallback | |
| unless fvars.size == n do return (← fallback) | |
| let info ← getProofInfo (mkMVar mvarIdPending) | |
| if fvars.any fun x => info.fvars.contains x.fvarId! then return none | |
| go (mkMVar mvarIdPending) | |
| fallback : GrindM (Option Expr) := getFalseProofEager? mvarId | |
| lambdaBody : Nat → Expr → Expr | |
| | n+1, .lam _ _ b _ => lambdaBody n b | |
| | _, e => e | |
| /-- Returns `true` if the given list can be compressed using `<;>` at `splitCore` -/ | |
| private def isCompressibleSeq (seq : List (TSyntax `grind)) : Bool := | |
| seq.all fun tac => match tac with | |
| | `(grind| next $_* => $_:grindSeq) => false | |
| | `(grind| · $_:grindSeq) => false | |
| | _ => true | |
| /-- | |
| Given `[t₁, ..., tₙ]`, returns `t₁ <;> ... <;> tₙ` | |
| -/ | |
| private def mkAndThenSeq (seq : List (TSyntax `grind)) : CoreM (TSyntax `grind) := do | |
| match seq with | |
| | [] => `(grind| done) | |
| | [tac] => return tac | |
| | tac :: seq => | |
| let seq ← mkAndThenSeq seq | |
| `(grind| $tac:grind <;> $seq:grind) | |
| private def mkCasesAndThen (cases : TSyntax `grind) (seq : List (TSyntax `grind)) : CoreM (TSyntax `grind) := do | |
| match seq with | |
| | [] => return cases | |
| | seq => | |
| let seq ← mkAndThenSeq seq | |
| `(grind| $cases:grind <;> $seq:grind) | |
| private def isCompressibleAlts (alts : Array (List (TSyntax `grind))) : Bool := | |
| if _ : alts.size > 0 then | |
| let alt := alts[0] | |
| isCompressibleSeq alt && alts.all (· == alt) | |
| else | |
| true | |
| def isSorryAlt (alt : List (TSyntax `grind)) : Bool := | |
| match alt with | |
| | [tac] => match tac with | |
| | `(grind| sorry) => true | |
| | _ => false | |
| | _ => false | |
| private def mkCasesResultSeq (cases : TSyntax `grind) (alts : Array (List (TSyntax `grind))) | |
| (compress : Bool) : CoreM (List (TSyntax `grind)) := do | |
| if compress && isCompressibleAlts alts then | |
| if alts.size > 0 then | |
| let firstAlt := alts[0]! | |
| if isSorryAlt firstAlt then | |
| /- | |
| **Note**: It is a bit pointless to return a script of the form `cases #<anchor> <;> sorry` | |
| -/ | |
| return firstAlt | |
| else | |
| return [(← mkCasesAndThen cases firstAlt)] | |
| else | |
| return [cases] | |
| else | |
| let seq ← if h : alts.size = 1 then | |
| pure alts[0] | |
| else | |
| alts.toList.mapM fun s => mkGrindNext s | |
| return cases :: seq | |
| /-- | |
| Performs a case-split using `c`. | |
| Remark: `numCases` and `isRec` are computed using `checkSplitStatus`. | |
| -/ | |
| def splitCore (c : SplitInfo) (numCases : Nat) (isRec : Bool) | |
| (stopAtFirstFailure : Bool) | |
| (compress : Bool) | |
| (candidates? : Option (List SplitInfo) := none) : Action := fun goal _ kp => withHoistedDecls goal do | |
| let traceEnabled := (← getConfig).trace | |
| let saved? ← saveStateIfTracing | |
| let goal₀ := goal | |
| let mvarId ← goal.mkAuxMVar | |
| let cExpr := c.getExpr | |
| let ((mvarIds, anchorInfo?), goal) ← GoalM.run goal do | |
| /- | |
| **Note**: The anchor reference must be computed before the case-split is performed, | |
| so that the candidate list matches the one inspected by the `cases` tactic when | |
| replaying the generated script. Callers that prune the candidate list during | |
| case-split selection (e.g., `splitNext` via `selectNextSplit?`) must provide the | |
| original list using `candidates?`. | |
| -/ | |
| let anchorInfo? ← if traceEnabled then | |
| pure (some (← mkSplitAnchorRefInfo c candidates?)) | |
| else | |
| pure none | |
| let gen ← getGeneration cExpr | |
| let genNew := if numCases > 1 || isRec then gen+1 else gen | |
| saveSplitDiagInfo cExpr genNew numCases c.source | |
| markCaseSplitAsResolved cExpr | |
| trace_goal[grind.split] "{cExpr}, generation: {gen}" | |
| let mvarIds ← if let .imp e h _ := c then | |
| casesWithTrace mvarId (mkGrindEM (e.forallDomain h)) | |
| else if (← isMatcherApp cExpr) then | |
| casesMatch mvarId cExpr | |
| else | |
| casesWithTrace mvarId (← mkCasesMajor cExpr) | |
| return (mvarIds, anchorInfo?) | |
| let numSubgoals := mvarIds.length | |
| /- | |
| **Branch counter heuristic**: The first case (`i = 0`) of a non-recursive split does not create | |
| a new branch. This leverages non-chronological backtracking (NCB): if the first case is solved | |
| using a proof that doesn't depend on the case hypothesis, we backtrack and close the original | |
| goal directly. In this scenario, the case-split was "free", it didn't contribute to the proof. | |
| By not counting it, we allow deeper exploration when case-splits turn out to be irrelevant. | |
| For recursive types or subsequent cases (`i > 0`), we always create a new branch since | |
| these represent genuine branches in the proof search. | |
| The branches are created when the case-split is performed, so that `numBranches` bounds the | |
| size of the search tree, including the cases that have not been explored yet. The cases skipped | |
| by NCB are removed again, see `closeByNCB`. | |
| -/ | |
| modifyThe State fun s => { s with numBranches := s.numBranches + if isRec then numSubgoals else numSubgoals - 1 } | |
| let goalStack := goal.goalStack.push (goal.mvarId, goal.toGoalState) | |
| let anchor ← goal.withContext <| c.getAnchor | |
| let subgoals := mvarIds.mapIdx fun i mvarId => | |
| { goal with | |
| mvarId, goalStack | |
| split.trace := { expr := cExpr, i, num := numSubgoals, source := c.source } :: goal.split.trace } | |
| |>.setIntroBase (mixHash anchor (hash i)) | |
| /- | |
| **Note**: Stuck goals are returned to the caller, and they may be processed after this split completes, | |
| e.g., by the next tactic in `grind => ...` mode. By then, the declarations hoisted to `goal` have been | |
| declared in its proof and erased (see `withHoistedDecls`). Thus, we truncate the goal stack of stuck | |
| goals to the one of `goal`, and they can only hoist declarations to the case splits enclosing `goal`. | |
| -/ | |
| let exitSplit (gs : List Goal) : List Goal := | |
| gs.map fun g => { g with goalStack := g.goalStack.extract 0 goal.goalStack.size } | |
| let mut seqNew : Array (List (TSyntax `grind)) := #[] | |
| let mut stuckNew : Array Goal := #[] | |
| /- | |
| The state for performing non-chronological backtracking (NCB) using the given proof and tactic | |
| sequence, if the case-split was kept even though NCB applies. | |
| -/ | |
| let mut ncb? : Option (SavedState × Nat × Expr × List (TSyntax `grind)) := none | |
| /- Closes `goal` using the proof `falseProof` of the `i`-th case, skipping the cases after it. -/ | |
| let closeByNCB (i : Nat) (falseProof : Expr) (seq : List (TSyntax `grind)) : GrindM ActionResult := do | |
| modifyThe State fun s => { s with numBranches := s.numBranches - (numSubgoals - 1 - i) } | |
| goal.mvarId.assignFalseProof (← goal.withContext <| mkHoistedDecls (← getHoistedDecls goal) falseProof (mkConst ``False)) | |
| return .closed seq | |
| for subgoal in subgoals, i in [:numSubgoals] do | |
| match (← kp subgoal) with | |
| | .stuck gs => | |
| if let some (s, j, falseProof, seq) := ncb? then | |
| -- Keeping the case-split failed, so we backtrack using the proof that does not need it. | |
| s.restore | |
| return (← closeByNCB j falseProof seq) | |
| if stopAtFirstFailure then | |
| /- | |
| **Note**: We don't need to assign `goal.mvarId` when `stopAtFirstFailure = true` | |
| because the caller will not be able to process the all failure/stuck goals anyway. | |
| -/ | |
| return .stuck (exitSplit gs) | |
| else | |
| stuckNew := stuckNew ++ exitSplit gs | |
| | .closed seq => | |
| if let some falseProof ← getFalseProof? subgoal.mvarId then | |
| /- | |
| **Note**: When tracing, NCB drops the case-split from the generated script only if `seq` | |
| still closes the original goal. It may not, even though the proof does not use the | |
| case-split: the case-split may have introduced terms used by E-matching, or changed the | |
| candidates that `seq` references using anchors. Otherwise, we keep the case-split. | |
| -/ | |
| if (← checkSeqAt saved? goal₀ seq) then | |
| return (← closeByNCB i falseProof seq) | |
| if ncb?.isNone then | |
| ncb? := some (← saveState, i, falseProof, seq) | |
| if !seq.isEmpty then | |
| seqNew := seqNew.push seq | |
| else if !seq.isEmpty then | |
| /- **Note**: if the sequence is empty, it means the user will never see this goal. -/ | |
| seqNew := seqNew.push seq | |
| let proof ← goal.withContext do mkHoistedDecls (← getHoistedDecls goal) (mkMVar mvarId) (← mvarId.getType) | |
| if (← goal.mvarId.getType).isFalse then | |
| /- **Note**: We add the marker to assist `getFalseExpr?` -/ | |
| goal.mvarId.assign (mkExpectedPropHint proof (mkConst ``False)) | |
| else | |
| goal.mvarId.assign proof | |
| if stuckNew.isEmpty then | |
| if let some anchorInfo := anchorInfo? then | |
| let cases ← anchorInfo.toSyntax | |
| return .closed (← mkCasesResultSeq cases seqNew compress) | |
| else | |
| return .closed [] | |
| else | |
| return .stuck stuckNew.toList | |
| /-- | |
| Selects a case-split from the list of candidates, performs the split and applies | |
| continuation to all subgoals. | |
| If a subgoal is solved without using new hypotheses, closes the original goal using this proof. That is, | |
| it performs non-chronological backtracking. | |
| If `stopsAtFirstFailure = true`, it stops the search as soon as the given continuation cannot solve a subgoal. | |
| If `compress = true`, then it uses `<;>` to generate the resulting tactic sequence if all subgoal sequences are | |
| identical. For example, suppose that the following sequence is generated with `compress = false` | |
| ``` | |
| cases #50fc | |
| next => lia | |
| next => lia | |
| ``` | |
| Then with `compress = true` it generates `cases #50fc <;> lia` | |
| -/ | |
| def splitNext (stopAtFirstFailure := true) (compress := true) : Action := fun goal kna kp => do | |
| /- | |
| **Note**: `selectNextSplit?` prunes the candidate list (e.g., it removes the selected | |
| candidate and resolved ones). We save the original list because it is needed for | |
| computing anchor reference information for the generated tactic script. | |
| -/ | |
| let candidates := goal.split.candidates | |
| let (r, goal) ← GoalM.run goal selectNextSplit? | |
| let .some c numCases isRec _ := r | |
| | kna goal | |
| let cExpr := c.getExpr | |
| let gen := goal.getGeneration cExpr | |
| let genNew := if numCases > 1 || isRec then gen+1 else gen | |
| let x : Action := splitCore c numCases isRec stopAtFirstFailure compress (candidates? := some candidates) >> intros genNew >> assertAll | |
| x goal kna kp | |
| end Action | |
| end Lean.Meta.Grind |