| /- | |
| 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.Elab.Tactic.Grind.Basic | |
| import Lean.Meta.Tactic.TryThis | |
| import Lean.Meta.Tactic.Grind.Solve | |
| import Lean.Meta.Tactic.Grind.Arith.Cutsat.Search | |
| import Lean.Meta.Tactic.Grind.Arith.Linear.Search | |
| import Lean.Meta.Tactic.Grind.Arith.CommRing.EqCnstr | |
| import Lean.Meta.Tactic.Grind.AC.Eq | |
| import Lean.Meta.Tactic.Grind.EMatch | |
| import Lean.Meta.Tactic.Grind.PP | |
| import Lean.Meta.Tactic.Grind.Internalize | |
| import Lean.Meta.Tactic.Grind.Intro | |
| import Lean.Meta.Tactic.Grind.Split | |
| import Lean.Meta.Tactic.Grind.Arith.CommRing.PP | |
| import Lean.Meta.Tactic.Grind.Arith.Linear.PP | |
| import Lean.Meta.Tactic.Grind.AC.PP | |
| import Lean.Meta.Tactic.ExposeNames | |
| import Lean.Elab.Tactic.RenameInaccessibles | |
| import Lean.Elab.Tactic.Grind.Anchor | |
| import Lean.Elab.Tactic.Grind.ShowState | |
| import Lean.Elab.Tactic.Grind.Config | |
| import Lean.Elab.Tactic.Grind.Param | |
| namespace Lean.Elab.Tactic.Grind | |
| def showStateAt (ref : Syntax) (filter? : Option (TSyntax `grind_filter)) : GrindTacticM Unit := do | |
| if let goal :: _ := (← getGoals) then | |
| withRef ref <| goal.withContext do | |
| let filter ← elabFilter filter? | |
| showState filter (isSilent := true) | |
| else | |
| logAt ref (severity := .information) (isSilent := true) "no grind state" | |
| def evalSepTactics (stx : Syntax) : GrindTacticM Unit := do | |
| for arg in stx.getArgs, i in *...stx.getArgs.size do | |
| if i % 2 == 0 then | |
| match arg with | |
| | `(Parser.Tactic.Grind.grindStep| $tac:grind) => evalGrindTactic tac | |
| | `(Parser.Tactic.Grind.grindStep| $tac:grind | $[$filter?]?) => | |
| showStateAt arg filter? | |
| evalGrindTactic tac | |
| showStateAt arg[1] filter? | |
| | _ => throwUnsupportedSyntax | |
| else | |
| saveTacticInfoForToken arg | |
| @[builtin_grind_tactic grindSeq1Indented] | |
| def evalGrindSeq1Indented : GrindTactic := fun stx => | |
| evalSepTactics stx[0] | |
| @[builtin_grind_tactic grindSeqBracketed] | |
| def evalGrindSeqBracketed : GrindTactic := fun stx => do | |
| let initInfo ← mkInitialTacticInfo stx[0] | |
| withRef stx[2] <| closeUsingOrAdmit do | |
| -- save state before/after entering focus on `{` | |
| withInfoContext (pure ()) initInfo | |
| evalSepTactics stx[1] | |
| @[builtin_grind_tactic grindSeq] | |
| def evalGrindSeq : GrindTactic := fun stx => | |
| evalGrindTactic stx[0] | |
| @[builtin_grind_tactic Parser.Tactic.Grind.«done»] def evalDone : GrindTactic := fun _ => | |
| done | |
| @[builtin_grind_tactic Parser.Tactic.Grind.«sorry»] def evalSorry : GrindTactic := fun _ => do | |
| let goal ← getMainGoal | |
| goal.mvarId.admit | |
| replaceMainGoal [] | |
| @[builtin_grind_tactic skip] def evalSkip : GrindTactic := fun _ => | |
| return () | |
| @[builtin_grind_tactic showGoals] def evalShowGoals : GrindTactic := fun _ => do | |
| let goals ← getUnsolvedGoalMVarIds | |
| addRawTrace (goalsToMessageData goals) | |
| @[builtin_grind_tactic paren] def evalParen : GrindTactic := fun stx => | |
| evalGrindTactic stx[1] | |
| open Meta Lean.Meta.Grind | |
| @[builtin_grind_tactic finish] def evalFinish : GrindTactic := fun stx => withMainContext do | |
| let `(grind| finish $[$configItems]* $[only%$only]? $[[$params?,*]]?) := stx | throwUnsupportedSyntax | |
| withConfigItems configItems do | |
| let params := params?.getD {} | |
| withParams (← read).params params only.isSome do | |
| let goal ← getMainGoal | |
| if let some goal ← liftGrindM <| solve goal then | |
| let params := (← read).params | |
| let result ← liftGrindM do mkResult params (some goal) | |
| throwError (← result.toErrorMessageData `finish) | |
| else | |
| replaceMainGoal [] | |
| /-- | |
| Helper function to executing "check" tactics that return a flag indicating | |
| whether they made progress or not. | |
| If the goal is not inconsistent and progress has been made, | |
| `pp?` is executed to produce an info message. | |
| -/ | |
| def evalCheck (tacticName : Name) (k : GoalM Bool) | |
| (pp? : Goal → Sym.SymM (Option MessageData)) : GrindTacticM Unit := do | |
| /- In sym mode, introduce remaining binders + by-contradiction + internalize | |
| so that satellite solvers (lia, ring, linarith) see all hypotheses. | |
| This matches the behavior of these tactics in default tactic mode | |
| where `lia` can close `x > 1 → x + y + z > 0` directly. -/ | |
| if (← read).sym then | |
| match (← liftActionCore <| Action.intros 0 >> Action.assertAll) with | |
| | .closed => return () -- closed the goal | |
| | .subgoals => pure () -- continue | |
| let recover := (← read).recover | |
| liftGoalM do | |
| let progress ← k | |
| unless progress do | |
| throwError "`{tacticName}` failed" | |
| processToDo | |
| unless (← Grind.getConfig).verbose do return () | |
| unless recover do return () | |
| if (← get).inconsistent then return () | |
| let some msg ← pp? (← get) | return () | |
| logInfo msg | |
| @[builtin_grind_tactic lia] def evalLIA : GrindTactic := fun _ => | |
| evalCheck `lia Arith.Cutsat.check fun goal => Arith.Cutsat.pp? goal | |
| @[builtin_grind_tactic linarith] def evalLinarith : GrindTactic := fun _ => do | |
| evalCheck `linarith Arith.Linear.check fun goal => Arith.Linear.pp? goal | |
| @[builtin_grind_tactic ring] def evalRing : GrindTactic := fun _ => do | |
| evalCheck `ring Arith.CommRing.check' fun goal => do Arith.CommRing.pp? goal (← Sym.Arith.getArithState).rings | |
| @[builtin_grind_tactic ac] def evalAC : GrindTactic := fun _ => do | |
| evalCheck `ac AC.check' fun goal => AC.pp? goal | |
| def logTheoremAnchor (proof : Expr) : TermElabM Unit := do | |
| let stx ← getRef | |
| Term.addTermInfo' stx proof | |
| def ematchThms (only : Bool) (thms : Array EMatchTheorem) : GrindTacticM Unit := do | |
| -- **TODO**: add `instantiate` action that takes `thms` | |
| let progress ← liftGoalM do | |
| let thms ← thms.mapM (preprocessTheorem · 0) | |
| if only then ematchOnly thms else ematch thms | |
| unless progress do | |
| throwError "`instantiate` tactic failed to instantiate new facts, use `show_patterns` to see active theorems and their patterns." | |
| liftAction Action.assertAll | |
| @[builtin_grind_tactic instantiate] def evalInstantiate : GrindTactic := fun stx => withMainContext do | |
| let `(grind| instantiate $[ only%$only ]? $[ approx ]? $[ [ $[$thmRefs?:thm],* ] ]?) := stx | throwUnsupportedSyntax | |
| let goal ← getMainGoal | |
| let only := only.isSome | |
| let initThms ← if only then goal.getActiveMatchEqTheorems else pure #[] | |
| let mut thms := initThms | |
| if let some thmRefs := thmRefs? then | |
| for thmRef in thmRefs do | |
| match thmRef with | |
| -- **Note**: Delete `namespace` modifier. We should use a custom `grind` attribute for this. | |
| | `(Parser.Tactic.Grind.thm| namespace $ns:ident) => | |
| let namespaceName := ns.getId | |
| let scopedThms ← Grind.grindExt.getEMatchTheoremsForNamespace namespaceName | |
| thms := thms ++ scopedThms | |
| | `(Parser.Tactic.Grind.thm| #$anchor:hexnum) => thms := thms ++ (← withRef thmRef <| elabLocalEMatchTheorem anchor) | |
| | `(Parser.Tactic.Grind.thm| $[$mod?:grindMod]? $id:ident) => thms := thms ++ (← withRef thmRef <| elabThm mod? id false) | |
| | `(Parser.Tactic.Grind.thm| ! $[$mod?:grindMod]? $id:ident) => thms := thms ++ (← withRef thmRef <| elabThm mod? id true) | |
| | _ => throwErrorAt thmRef "unexpected theorem reference" | |
| ematchThms only thms | |
| where | |
| collectThms (anchorRef : AnchorRef) (thms : PArray EMatchTheorem) : StateT (Array EMatchTheorem) GrindTacticM Unit := do | |
| let mut found : Std.HashSet Expr := {} | |
| for thm in thms do | |
| -- **Note**: `anchors` are cached using pointer addresses, if this is a performance issue, we should | |
| -- cache the theorem types. | |
| let type ← inferType thm.proof | |
| let anchor ← liftGrindM <| getAnchor type | |
| if anchorRef.matches anchor then | |
| -- **Note**: We display the anchor term at most once. | |
| unless found.contains type do | |
| logTheoremAnchor thm.proof | |
| found := found.insert type | |
| modify (·.push thm) | |
| elabLocalEMatchTheorem (anchor : TSyntax `hexnum) : GrindTacticM (Array EMatchTheorem) := withRef anchor do | |
| let anchorRef ← elabAnchorRef anchor | |
| let goal ← getMainGoal | |
| let thms ← StateT.run' (s := #[]) do | |
| collectThms anchorRef goal.ematch.thms | |
| collectThms anchorRef goal.ematch.newThms | |
| get | |
| if thms.isEmpty then | |
| throwError "no local theorems" | |
| return thms | |
| ensureNoMinIndexable (minIndexable : Bool) : MetaM Unit := do | |
| if minIndexable then | |
| throwError "redundant modifier `!` in `grind` parameter" | |
| elabEMatchTheorem (declName : Name) (kind : Grind.EMatchTheoremKind) (minIndexable : Bool) : GrindTacticM (Array EMatchTheorem) := do | |
| let params := (← read).params | |
| let info ← getAsyncConstInfo declName | |
| match info.kind with | |
| | .thm | .axiom | .ctor => | |
| match kind with | |
| | .eqBoth gen => | |
| ensureNoMinIndexable minIndexable | |
| let thm₁ ← Grind.mkEMatchTheoremForDecl declName (.eqLhs gen) params.symPrios | |
| let thm₂ ← Grind.mkEMatchTheoremForDecl declName (.eqRhs gen) params.symPrios | |
| return #[thm₁, thm₂] | |
| | _ => | |
| if kind matches .eqLhs _ | .eqRhs _ then | |
| ensureNoMinIndexable minIndexable | |
| let thm ← Grind.mkEMatchTheoremForDecl declName kind params.symPrios (minIndexable := minIndexable) | |
| return #[thm] | |
| | .defn => | |
| if (← isReducible declName) then | |
| throwError "`{.ofConstName declName}` is a reducible definition, `grind` automatically unfolds them" | |
| if !kind.isEqLhs && !kind.isDefault then | |
| throwError "invalid `grind` parameter, `{.ofConstName declName}` is a definition, the only acceptable (and redundant) modifier is '='" | |
| ensureNoMinIndexable minIndexable | |
| let some thms ← Grind.mkEMatchEqTheoremsForDef? declName | |
| | throwError "failed to generate equation theorems for `{.ofConstName declName}`" | |
| return thms | |
| | _ => | |
| throwError "invalid `grind` parameter, `{.ofConstName declName}` is not a theorem, definition, or inductive type" | |
| elabThm | |
| (mod? : Option (TSyntax `Lean.Parser.Attr.grindMod)) | |
| (id : TSyntax `ident) | |
| (minIndexable : Bool) : GrindTacticM (Array EMatchTheorem) := do | |
| let declName ← realizeGlobalConstNoOverloadWithInfo id | |
| -- Definitions are unfolded, acting on occurrences that are already in the goal. | |
| unless (← isDefn? declName).isSome do | |
| Linter.checkDeprecatedAt id declName | |
| let kind ← if let some mod := mod? then Grind.getAttrKindCore mod else pure .infer | |
| match kind with | |
| | .ematch .user => | |
| ensureNoMinIndexable minIndexable | |
| let params := (← read).params | |
| let thms := params.extensions.find (.decl declName) | |
| let thms := thms.filter fun thm => thm.kind == .user | |
| if thms.isEmpty then | |
| throwError "invalid use of `usr` modifier, `{.ofConstName declName}` does not have patterns specified with the command `grind_pattern`" | |
| return thms.toArray | |
| | .ematch kind => | |
| elabEMatchTheorem declName kind minIndexable | |
| | .infer => | |
| let goal ← getMainGoal | |
| -- Registered theorems are reused only if they match the requested `!` modifier. | |
| let thms := goal.ematch.thmMap.find (.decl declName) |>.filter (·.minIndexable == minIndexable) | |
| if thms.isEmpty then | |
| elabEMatchTheorem declName (.default false) minIndexable | |
| else | |
| return thms.toArray | |
| | .cases _ | .intro | .inj | .ext | .symbol _ | .funCC | .norm .. | .unfold | .homo .. | .homoPred => | |
| throwError "invalid modifier" | |
| def logAnchor (c : SplitInfo) : TermElabM Unit := do | |
| let e := c.getExpr | |
| let stx ← getRef | |
| if e.isFVar || e.isConst then | |
| /- | |
| **Note**: When `e` is a constant or free variable, the hover displays `e` | |
| -/ | |
| Term.addTermInfo' stx e | |
| else if (← isType e) then | |
| /- | |
| **Note**: If `e` is a type, we create a fake `sorry` to force `e` to be displayed | |
| when we hover over the anchor. | |
| We wrap the `sorry` with `id` to ensure the hover will not display `sorry : e` | |
| -/ | |
| let e ← mkSorry e (synthetic := false) | |
| let e ← mkId e | |
| Term.addTermInfo' stx e | |
| else | |
| /- | |
| **Note**: `e` and its type are displayed when hovering over the anchor. | |
| -/ | |
| Term.addTermInfo' stx e (isDisplayableTerm := True) | |
| def split? (c : SplitInfo) : Action := fun goal kna kp => do | |
| let (.ready numCases isRec _, goal) ← GoalM.run goal (checkSplitStatus c) | |
| | throwError "`cases` tactic failed, case-split is not ready{indentExpr c.getExpr}" | |
| let gen := goal.getGeneration c.getExpr | |
| let genNew := if numCases > 1 || isRec then gen+1 else gen | |
| let a : Action := | |
| Action.splitCore c numCases isRec (stopAtFirstFailure := false) (compress := false) >> Action.intros genNew >> Action.assertAll | |
| a goal kna kp | |
| /-- | |
| Drains the pending raw facts (e.g., queued by `internalize_all` in `sym` mode) before a | |
| case-split tactic, so that split candidates are inspected in a fully propagated state and | |
| the queued facts are not preprocessed once per subgoal created by the split. | |
| Returns `true` if draining closed the goal. | |
| -/ | |
| def drainRawFacts : GrindTacticM Bool := do | |
| match (← liftActionCore Action.assertAll) with | |
| | .closed => return true | |
| | .subgoals => return false | |
| @[builtin_grind_tactic cases] def evalCases : GrindTactic := fun stx => do | |
| let (anchor, ordinal) ← match stx with | |
| | `(grind| cases #$anchor:hexnum) => pure ((anchor : TSyntax `hexnum), 1) | |
| | `(grind| cases #$anchor:hexnum/$i:num) => | |
| if i.getNat == 0 then | |
| throwErrorAt i "invalid anchor ordinal, it must be ≥ 1" | |
| pure (anchor, i.getNat) | |
| | _ => throwUnsupportedSyntax | |
| if (← drainRawFacts) then return () | |
| withMainContext do | |
| let anchorRef ← elabAnchorRef anchor | |
| let c? ← liftGoalM do | |
| let mut remaining := ordinal | |
| let mut firstMatch? := none | |
| for c in (← get).split.candidates do | |
| let a ← c.getAnchor | |
| if anchorRef.matches a then | |
| if firstMatch?.isNone then | |
| firstMatch? := some c | |
| if (← checkSplitStatus c) matches .ready .. then | |
| remaining := remaining - 1 | |
| if remaining == 0 then | |
| return some c | |
| if ordinal == 1 then | |
| /- | |
| **Note**: Fall back to a candidate that is not ready so that `split?` can | |
| produce a more informative error message. | |
| -/ | |
| return firstMatch? | |
| else | |
| return none | |
| let some c := c? | throwError "`cases` tactic failed, invalid anchor" | |
| let goal ← getMainGoal | |
| goal.withContext <| withRef anchor <| logAnchor c | |
| liftAction <| split? c | |
| def mkCasesSuggestions (candidates : Array SplitCandidateWithAnchor) (numDigits : Nat) : MetaM (Array Tactic.TryThis.Suggestion) := do | |
| let shift := (64 - 4*numDigits).toUInt64 | |
| let mut counts : Std.HashMap UInt64 Nat := {} | |
| let mut suggestions := #[] | |
| for { anchor, e, .. } in candidates do | |
| let anchorStx ← mkAnchorSyntax numDigits anchor | |
| /- | |
| **Note**: Distinct candidates may have identical anchors (e.g., they differ only in | |
| inaccessible variables). We use ordinal references (e.g., `#a56e/2`) to disambiguate them. | |
| -/ | |
| let anchorPrefix := anchor >>> shift | |
| let ordinal := counts.getD anchorPrefix 0 | |
| counts := counts.insert anchorPrefix (ordinal + 1) | |
| let tac ← if ordinal == 0 then | |
| `(grind| cases $anchorStx:anchor) | |
| else | |
| `(grind| cases $anchorStx:anchor/$(quote (ordinal + 1)):num) | |
| let msg ← addMessageContext m!"{tac} for{indentExpr e}" | |
| suggestions := suggestions.push { | |
| suggestion := .tsyntax tac | |
| messageData? := some msg | |
| } | |
| return suggestions | |
| @[builtin_grind_tactic casesTrace] def evalCasesTrace : GrindTactic := fun stx => do | |
| let `(grind| cases? $[$filter?]?) := stx | throwUnsupportedSyntax | |
| if (← drainRawFacts) then return () | |
| withMainContext do | |
| let filter ← elabFilter filter? | |
| let { candidates, numDigits } ← liftGoalM <| getSplitCandidateAnchors filter.eval | |
| let suggestions ← mkCasesSuggestions candidates numDigits | |
| Tactic.TryThis.addSuggestions stx suggestions | |
| return () | |
| @[builtin_grind_tactic casesNext] def evalCasesNext : GrindTactic := fun _ => do | |
| /- | |
| **Note**: We cannot use `Action.assertAll >> Action.splitNext` here: `assertAll` is | |
| always applicable, and `>>` treats the second action's "not applicable" as a skip, so | |
| the composition would never fail. `cases_next` must fail when there is no split | |
| candidate (see `grind_finish_trace.lean`); `repeat`/`first` idioms rely on it. | |
| -/ | |
| if (← drainRawFacts) then return () | |
| liftAction (Action.splitNext (stopAtFirstFailure := false)) | |
| @[builtin_grind_tactic Parser.Tactic.Grind.focus] def evalFocus : GrindTactic := fun stx => do | |
| let mkInfo ← mkInitialTacticInfo stx[0] | |
| focus do | |
| -- show focused state on `focus` | |
| withInfoContext (pure ()) mkInfo | |
| evalGrindTactic stx[1] | |
| @[builtin_grind_tactic allGoals] def evalAllGoals : GrindTactic := fun stx => do | |
| let goals ← getGoals | |
| let mut goalsNew := #[] | |
| let mut abort := false | |
| for goal in goals do | |
| unless (← goal.mvarId.isAssigned) do | |
| setGoals [goal] | |
| let saved ← saveState | |
| abort ← Grind.tryCatch | |
| (do | |
| evalGrindTactic stx[1] | |
| pure abort) | |
| (fun ex => do | |
| if (← read).recover then | |
| logException ex | |
| let msgLog ← Core.getMessageLog | |
| saved.restore | |
| Core.setMessageLog msgLog | |
| admitGoal goal.mvarId | |
| pure true | |
| else | |
| throw ex) | |
| goalsNew := goalsNew ++ (← getUnsolvedGoals) | |
| if abort then | |
| throwAbortTactic | |
| setGoals goalsNew.toList | |
| @[builtin_grind_tactic withAnnotateState] def evalWithAnnotateState : GrindTactic := fun stx => | |
| withTacticInfoContext stx[1] do | |
| evalGrindTactic stx[2] | |
| @[builtin_grind_tactic anyGoals] def evalAnyGoals : GrindTactic := fun stx => do | |
| let goals ← getGoals | |
| let mut goalsNew := #[] | |
| let mut succeeded := false | |
| for goal in goals do | |
| unless (← goal.mvarId.isAssigned) do | |
| setGoals [goal] | |
| try | |
| evalGrindTactic stx[1] | |
| goalsNew := goalsNew ++ (← getUnsolvedGoals) | |
| succeeded := true | |
| catch _ => | |
| goalsNew := goalsNew.push goal | |
| unless succeeded do | |
| throwError "Tactic failed on all goals:{indentD stx[1]}" | |
| setGoals goalsNew.toList | |
| public def renameInaccessibles (mvarId : MVarId) (hs : TSyntaxArray ``binderIdent) : GrindTacticM MVarId := do | |
| let mvarId ← Tactic.renameInaccessibles mvarId hs | |
| unless hs.isEmpty do liftGrindM <| resetAnchors | |
| return mvarId | |
| @[builtin_grind_tactic «next»] def evalNext : GrindTactic := fun stx => do | |
| let `(grind| next%$nextTk $hs* =>%$arr $seq:grindSeq) := stx | throwUnsupportedSyntax | |
| let goal :: goals ← getUnsolvedGoals | throwNoGoalsToBeSolved | |
| -- **Note**: `renameInaccessibles` resets cached anchors. | |
| let mvarId ← renameInaccessibles goal.mvarId hs | |
| let goal := { goal with mvarId } | |
| setGoals [goal] | |
| goal.mvarId.setTag Name.anonymous | |
| withCaseRef arr seq <| closeUsingOrAdmit <| withTacticInfoContext (mkNullNode #[nextTk, arr]) <| | |
| evalGrindTactic stx[3] | |
| setGoals goals | |
| /-- | |
| Searches `goals` for one whose case tag matches `tag`, using the same heuristic as the | |
| standard `case` tactic: prefer an exact match, then a suffix match, then a prefix match. | |
| -/ | |
| def findGrindTag? (goals : List Goal) (tag : Name) : GrindTacticM (Option Goal) := do | |
| let byName (p : Name → Name → Bool) : GrindTacticM (Option Goal) := | |
| goals.findM? fun g => return p tag (← g.mvarId.getDecl).userName.eraseMacroScopes | |
| if let some g ← byName (· == ·) then return some g | |
| if let some g ← byName (·.isSuffixOf ·) then return some g | |
| byName (·.isPrefixOf ·) | |
| /-- | |
| Returns the goal selected by the case tag `tag` together with the remaining goals. | |
| If `tag` is a hole (`_`), the main goal is selected. | |
| -/ | |
| def getCaseGoal (tag : TSyntax ``binderIdent) : GrindTacticM (Goal × List Goal) := do | |
| let gs ← getUnsolvedGoals | |
| if let `(binderIdent| $tagId:ident) := tag then | |
| let tagId := tagId.getId.eraseMacroScopes | |
| let some g ← findGrindTag? gs tagId | notFound gs tagId | |
| return (g, gs.filter (·.mvarId != g.mvarId)) | |
| else | |
| let g ← getMainGoal | |
| return (g, gs.filter (·.mvarId != g.mvarId)) | |
| where | |
| notFound {α} (available : List Goal) (tag : Name) : GrindTacticM α := do | |
| let names := (← available.mapM fun g => return (← g.mvarId.getDecl).userName) | |
| |>.filter (· != .anonymous) |>.eraseDups | |
| let hint := match names with | |
| | [] => m!"There are no cases to select." | |
| | [name] => m!"The only available case tag is `{name}`." | |
| | names => m!"Available tags: {MessageData.joinSep (names.map (m!"`{·}`")) ", "}" | |
| throwError "Case tag `{tag}` not found.\n{hint}" | |
| @[builtin_grind_tactic «case»] def evalCase : GrindTactic := fun stx => do | |
| let `(grind| case%$caseTk $[$tags $hss*]|* =>%$arr $seq:grindSeq) := stx | throwUnsupportedSyntax | |
| for tag in tags, hs in hss do | |
| let (goal, goals) ← getCaseGoal tag | |
| -- **Note**: `renameInaccessibles` resets cached anchors. | |
| let mvarId ← renameInaccessibles goal.mvarId hs | |
| let goal := { goal with mvarId } | |
| setGoals [goal] | |
| goal.mvarId.setTag Name.anonymous | |
| withCaseRef arr seq <| closeUsingOrAdmit <| withTacticInfoContext (mkNullNode #[caseTk, arr]) <| | |
| evalGrindTactic seq | |
| setGoals goals | |
| @[builtin_grind_tactic nestedTacticCore] def evalNestedTactic : GrindTactic := fun stx => do | |
| let `(grind| tactic%$tacticTk =>%$arr $seq:tacticSeq) := stx | throwUnsupportedSyntax | |
| let goal ← getMainGoal | |
| let recover := (← read).recover | |
| discard <| Tactic.run goal.mvarId <| withCaseRef arr seq <| Tactic.closeUsingOrAdmit | |
| <| Tactic.withTacticInfoContext (mkNullNode #[tacticTk, arr]) | |
| <| Tactic.withRecover recover <| evalTactic seq | |
| replaceMainGoal [] | |
| @[builtin_grind_tactic «first»] partial def evalFirst : GrindTactic := fun stx => do | |
| let `(grind| first $[($s:grindSeq)]*) := stx | throwUnsupportedSyntax | |
| loop s 0 | |
| where | |
| loop (s : Array (TSyntax ``Parser.Tactic.Grind.grindSeq)) (i : Nat) := | |
| if i == s.size - 1 then | |
| evalGrindTactic s[i]! | |
| else | |
| evalGrindTactic s[i]! <|> loop s (i+1) | |
| @[builtin_grind_tactic failIfSuccess] def evalFailIfSuccess : GrindTactic := fun stx => | |
| Term.withoutErrToSorry <| withoutRecover do | |
| let tactic := stx[1] | |
| if (← try evalGrindTactic tactic; pure true catch _ => pure false) then | |
| throwError "The tactic provided to `fail_if_success` succeeded but was expected to fail:{indentD stx[1]}" | |
| @[builtin_grind_tactic «fail»] def evalFail : GrindTactic := fun stx => do | |
| let goals ← getGoals | |
| let goalsMsg := MessageData.joinSep (goals.map (MessageData.ofGoal ·.mvarId)) m!"\n\n" | |
| match stx with | |
| | `(grind| fail) => throwError "Failed: `fail` tactic was invoked\n{goalsMsg}" | |
| | `(grind| fail $msg:str) => throwError "{msg.getString}\n{goalsMsg}" | |
| | _ => throwUnsupportedSyntax | |
| @[builtin_grind_tactic «renameI»] def evalRenameInaccessibles : GrindTactic := fun stx => do | |
| let `(grind| rename_i $hs*) := stx | throwUnsupportedSyntax | |
| let goal ← getMainGoal | |
| let mvarId ← renameInaccessibles goal.mvarId hs | |
| replaceMainGoal [{ goal with mvarId }] | |
| @[builtin_grind_tactic exposeNames] def evalExposeNames : GrindTactic := fun _ => do | |
| let goal ← getMainGoal | |
| let mvarId ← goal.mvarId.exposeNames | |
| liftGrindM <| resetAnchors | |
| replaceMainGoal [{ goal with mvarId }] | |
| @[builtin_grind_tactic setOption] def elabSetOption : GrindTactic := fun stx => do | |
| let (options, decl) ← Elab.elabSetOption stx[1] stx[3] | |
| withRef stx[1] <| Elab.checkDeprecatedOption (stx[1].getId.eraseMacroScopes) decl | |
| withOptions (fun _ => options) do evalGrindTactic stx[5] | |
| @[builtin_grind_tactic setConfig] def elabSetConfig : GrindTactic := fun stx => do | |
| let `(grind| set_config $[$items:configItem]* in $seq:grindSeq) := stx | throwUnsupportedSyntax | |
| withConfigItems items do evalGrindTactic seq | |
| @[builtin_grind_tactic mbtc] def elabMBTC : GrindTactic := fun _ => do | |
| liftGoalM do | |
| discard <| Solvers.check | |
| if (← get).inconsistent then | |
| return () | |
| let progress ← Solvers.mbtc | |
| unless progress do | |
| throwError "`mbtc` tactic failed to generate new candidate case-splits." | |
| end Lean.Elab.Tactic.Grind |