| /- | |
| Copyright (c) 2024 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.Main | |
| public import Lean.Meta.Tactic.TryThis | |
| public import Lean.Elab.Tactic.Grind.Config | |
| public import Lean.LibrarySuggestions.Basic | |
| import Lean.Meta.Tactic.Grind.SimpUtil | |
| import Lean.Elab.Tactic.Grind.Param | |
| import Lean.Meta.Tactic.Grind.Finish | |
| import Lean.Meta.Tactic.Grind.CollectParams | |
| import Lean.Meta.Tactic.Grind.Parser | |
| public section | |
| namespace Lean.Elab.Tactic | |
| open Meta | |
| open Command Term in | |
| open Lean.Parser.Command.GrindCnstr in | |
| @[builtin_command_elab Lean.Parser.Command.grindPattern] | |
| def elabGrindPattern : CommandElab := fun stx => do | |
| match stx with | |
| | `(grind_pattern $[[ $attr?:ident ]]? $thmName:ident => $terms,* $[$cnstrs?:grindPatternCnstrs]?) => go attr? thmName terms cnstrs? .global | |
| | `(scoped grind_pattern $[[ $attr?:ident ]]? $thmName:ident => $terms,* $[$cnstrs?:grindPatternCnstrs]?) => go attr? thmName terms cnstrs? .scoped | |
| | `(local grind_pattern $[[ $attr?:ident ]]? $thmName:ident => $terms,* $[$cnstrs?:grindPatternCnstrs]?) => go attr? thmName terms cnstrs? .local | |
| | _ => throwUnsupportedSyntax | |
| where | |
| findLHS (xs : Array Expr) (lhs : Syntax) : TermElabM (LocalDecl × Nat) := do | |
| let lhsId := lhs.getId | |
| let mut i := 0 | |
| for x in xs do | |
| let xDecl ← x.fvarId!.getDecl | |
| if xDecl.userName == lhsId then | |
| return (xDecl, xs.size - i - 1) | |
| i := i + 1 | |
| throwErrorAt lhs "invalid constraint, `{lhsId}` is not local variable of the theorem" | |
| elabCnstrRHS (xs : Array Expr) (rhs : Syntax) (expectedType : Expr) : TermElabM Grind.CnstrRHS := do | |
| /- | |
| **Note**: We need better sanity checking here. | |
| We must check whether the type of `rhs` is type correct with respect to | |
| an arbitrary instantiation of `xs`. That is, we should use meta-variables | |
| in the check. It is incorrect to use `xDecl.type`. For example, suppose the | |
| type of `xDecl` is `α → β` where `α` and `β` are variables in `xs` occurring before | |
| `xDecl`, and `rhsExpr` is `some : ?m → ?m`. The types `α → β =?= ?m → ?m` are | |
| not definitionally equal, but `?α → ?β =?= ?m → ?m` are. | |
| -/ | |
| let rhsExpr ← Term.elabTerm rhs expectedType | |
| Term.synthesizeSyntheticMVars (postpone := .no) (ignoreStuckTC := true) | |
| let rhsExpr ← instantiateMVars rhsExpr | |
| if rhsExpr.hasSyntheticSorry then | |
| throwErrorAt rhs "invalid constraint, rhs contains a synthetic `sorry`" | |
| let rhsExpr := rhsExpr.eta | |
| let { paramNames := levelNames, mvars, expr := rhs } ← abstractMVars rhsExpr | |
| let numMVars := mvars.size | |
| let rhs := rhs.abstract xs | |
| return { levelNames, numMVars, expr := rhs } | |
| elabProp (xs : Array Expr) (term : Syntax) : TermElabM Expr := do | |
| let e ← Term.elabTermAndSynthesize term (Expr.sort 0) | |
| let e ← instantiateMVars e | |
| if e.hasSyntheticSorry then | |
| throwErrorAt term "invalid proposition, it contains a synthetic `sorry`" | |
| if e.hasMVar then | |
| throwErrorAt term "invalid proposition, it contains metavariables{indentExpr e}" | |
| return e.abstract xs | |
| elabNotDefEq (xs : Array Expr) (lhs rhs : Syntax) : TermElabM Grind.EMatchTheoremConstraint := do | |
| let (localDecl, lhsBVarIdx) ← findLHS xs lhs | |
| let rhs ← elabCnstrRHS xs rhs localDecl.type | |
| return .notDefEq lhsBVarIdx rhs | |
| elabDefEq (xs : Array Expr) (lhs rhs : Syntax) : TermElabM Grind.EMatchTheoremConstraint := do | |
| let (localDecl, lhsBVarIdx) ← findLHS xs lhs | |
| let rhs ← elabCnstrRHS xs rhs localDecl.type | |
| return .defEq lhsBVarIdx rhs | |
| elabCnstrs (xs : Array Expr) (cnstrs? : Option (TSyntax ``Parser.Command.grindPatternCnstrs)) | |
| : TermElabM (List (Grind.EMatchTheoremConstraint)) := do | |
| let some cnstrs := cnstrs? | return [] | |
| let cnstrs := cnstrs.raw[1].getArgs | |
| cnstrs.toList.mapM fun cnstr => do | |
| let kind := cnstr.getKind | |
| if kind == ``notDefEq then | |
| elabNotDefEq xs cnstr[0] cnstr[2] | |
| else if kind == ``defEq then | |
| elabDefEq xs cnstr[0] cnstr[2] | |
| else if kind == ``genLt then | |
| return .genLt cnstr[2].toNat | |
| else if kind == ``sizeLt then | |
| let (_, lhs) ← findLHS xs cnstr[1] | |
| return .sizeLt lhs cnstr[3].toNat | |
| else if kind == ``depthLt then | |
| let (_, lhs) ← findLHS xs cnstr[1] | |
| return .depthLt lhs cnstr[3].toNat | |
| else if kind == ``maxInsts then | |
| return .maxInsts cnstr[2].toNat | |
| else if kind == ``isValue then | |
| let (_, lhs) ← findLHS xs cnstr[1] | |
| return .isValue lhs false | |
| else if kind == ``isStrictValue then | |
| let (_, lhs) ← findLHS xs cnstr[1] | |
| return .isValue lhs true | |
| else if kind == ``notValue then | |
| let (_, lhs) ← findLHS xs cnstr[1] | |
| return .notValue lhs false | |
| else if kind == ``notStrictValue then | |
| let (_, lhs) ← findLHS xs cnstr[1] | |
| return .notValue lhs true | |
| else if kind == ``isGround then | |
| let (_, lhs) ← findLHS xs cnstr[1] | |
| return .isGround lhs | |
| else if kind == ``Parser.Command.GrindCnstr.check then | |
| return .check (← elabProp xs cnstr[1]) | |
| else if kind == ``Parser.Command.GrindCnstr.guard then | |
| return .guard (← elabProp xs cnstr[1]) | |
| else | |
| throwErrorAt cnstr "unexpected constraint" | |
| go (attrName? : Option (TSyntax `ident)) (thmName : TSyntax `ident) (terms : Syntax.TSepArray `term ",") | |
| (cnstrs? : Option (TSyntax ``Parser.Command.grindPatternCnstrs)) | |
| (kind : AttributeKind) : CommandElabM Unit := liftTermElabM do | |
| let attrName := if let some attrName := attrName? then attrName.getId else `grind | |
| let some ext ← Grind.getExtension? attrName | throwError "unknown `grind` attribute `{attrName}`" | |
| let declName ← realizeGlobalConstNoOverloadWithInfo thmName | |
| Linter.checkDeprecatedAt thmName declName | |
| let info ← getConstVal declName | |
| forallTelescope info.type fun xs _ => do | |
| let patterns ← terms.getElems.mapM fun term => do | |
| let pattern ← Term.elabTerm term none | |
| synthesizeSyntheticMVarsUsingDefault | |
| let pattern ← instantiateMVars pattern | |
| let pattern ← Grind.preprocessPattern pattern | |
| return pattern.abstract xs | |
| let cnstrs ← elabCnstrs xs cnstrs? | |
| ext.addEMatchTheorem declName xs.size patterns.toList .user kind cnstrs (minIndexable := false) | |
| open Command in | |
| @[builtin_command_elab Lean.Parser.resetGrindAttrs] | |
| def elabResetGrindAttrs : CommandElab := fun _ => liftTermElabM do | |
| -- Remark: we do not reset symbol priorities because we would have to then set | |
| -- `[grind symbol 0] Eq` after a `reset_grind_attr%` command. | |
| -- Grind.resetSymbolPrioExt | |
| modifyEnv fun env => Grind.grindExt.modifyState env fun ext => { ext with casesTypes := {}, inj := {}, ematch := {} } | |
| -- We don't reset homomorphism. | |
| -- modifyEnv fun env => Grind.homoExt.modifyState env fun _ => {} | |
| -- modifyEnv fun env => Grind.homoPredExt.modifyState env fun _ => {} | |
| -- modifyEnv fun env => Grind.homoSourceTypesExt.modifyState env fun _ => {} | |
| open Command Term in | |
| @[builtin_command_elab Lean.Parser.Command.initGrindNorm] | |
| def elabInitGrindNorm : CommandElab := fun stx => | |
| withExporting do -- should generate public aux decls | |
| match stx with | |
| | `(init_grind_norm $pre:ident* | $post*) => | |
| Command.liftTermElabM do | |
| let pre ← pre.mapM fun id => realizeGlobalConstNoOverloadWithInfo id | |
| let post ← post.mapM fun id => realizeGlobalConstNoOverloadWithInfo id | |
| -- Creates `Lean.Grind._simp_1` etc.. As we do not use this command in independent modules, | |
| -- there is no chance of name conflicts. | |
| withDeclNameForAuxNaming `Lean.Grind do | |
| Grind.registerNormTheorems pre post | |
| | _ => throwUnsupportedSyntax | |
| private def parseModifier (s : String) : CoreM Grind.AttrKind := do | |
| let stx := Parser.runParserCategory (← getEnv) `Lean.Parser.Attr.grindMod s | |
| match stx with | |
| | .ok stx => Grind.getAttrKindCore stx | |
| | _ => throwError "unexpected modifier {s}" | |
| open LibrarySuggestions in | |
| def elabGrindSuggestions | |
| (params : Grind.Params) (suggestions : Array Suggestion := #[]) : MetaM Grind.Params := do | |
| let mut params := params | |
| let mut added : Array Name := #[] | |
| for p in suggestions do | |
| let attr ← match p.flag with | |
| | some flag => parseModifier flag | |
| | none => pure <| .ematch (.default false) | |
| match attr with | |
| | .ematch kind => | |
| try | |
| params ← addEMatchTheorem params (mkIdent p.name) p.name kind false (warn := false) | |
| added := added.push p.name | |
| catch _ => pure () -- Don't worry if library suggestions gave bad theorems. | |
| | _ => | |
| -- We could actually support arbitrary grind modifiers, | |
| -- and call `processParam` rather than `addEMatchTheorem`, | |
| -- but this would require a larger refactor. | |
| -- Let's only do this if there is a prospect of a library suggestion engine supporting this. | |
| throwError "unexpected modifier {p.flag}" | |
| unless added.isEmpty do | |
| trace[grind.debug.suggestions] "{added}" | |
| return params | |
| /-- Add all definitions from the current file. -/ | |
| def elabGrindLocals (params : Grind.Params) : MetaM Grind.Params := do | |
| let env ← getEnv | |
| let mut params := params | |
| let mut added : Array Name := #[] | |
| for c in ← env.getLocalConstantInfos (skipTheoremSubDecls := true) do | |
| let name := c.name | |
| -- Filter similar to LibrarySuggestions.isDeniedPremise (but inlined to avoid dependency) | |
| -- Skip internal details, but allow private names (which are accessible from current module) | |
| if name.isInternalDetail && !isPrivateName name then continue | |
| if c.kind != .defn then continue | |
| if (← isInstanceReducible name) then continue | |
| try | |
| params ← addEMatchTheorem params (mkIdent name) name (.default false) false (warn := false) | |
| added := added.push name | |
| catch _ => pure () | |
| unless added.isEmpty do | |
| trace[grind.debug.locals] "{added}" | |
| return params | |
| def mkGrindParams | |
| (config : Grind.Config) (only : Bool) (ps : TSyntaxArray ``Parser.Tactic.grindParam) (mvarId : MVarId) | |
| (extensions? : Option Grind.ExtensionStateArray := none) (identity : Name := `grind) : | |
| TermElabM Grind.Params := do | |
| let params ← match extensions? with | |
| | some extensions => Grind.mkParams config extensions | |
| | none => if only then Grind.mkOnlyParams config else Grind.mkDefaultParams config | |
| let mut params ← elabGrindParams { params with identity } ps (lax := config.lax) (only := only) | |
| if config.suggestions then | |
| let lsConfig : LibrarySuggestions.Config := { caller := some "grind" } | |
| let lsConfig := match config.maxSuggestions with | |
| | some n => { lsConfig with maxSuggestions := n } | |
| | none => lsConfig | |
| params ← elabGrindSuggestions params (← LibrarySuggestions.select mvarId lsConfig) | |
| if config.locals then | |
| params ← elabGrindLocals params | |
| trace[grind.debug.inj] "{params.extensions[0]!.inj.getOrigins.map (·.pp)}" | |
| if params.anchorRefs?.isSome then | |
| /- | |
| **Note**: anchors are automatically computed in interactive mode where | |
| hygiene is turned on. So, we must disable hypotheses id cleanup since | |
| different ids will affect the anchor values. | |
| **TODO**: a more robust solution since users may use `grind +clean => finish?` | |
| -/ | |
| params := { params with config.clean := false } | |
| return params | |
| def checkTerminalAsSorry (mvarId : MVarId) : TacticM Bool := do | |
| if debug.terminalTacticsAsSorry.get (← getOptions) then | |
| mvarId.admit | |
| replaceMainGoal [] | |
| return true | |
| else | |
| return false | |
| def grind | |
| (mvarId : MVarId) (config : Grind.Config) | |
| (only : Bool) | |
| (ps : TSyntaxArray ``Parser.Tactic.grindParam) | |
| (seq? : Option (TSyntax `Lean.Parser.Tactic.Grind.grindSeq)) | |
| (extensions? : Option Grind.ExtensionStateArray := none) | |
| (head? : Option Syntax := none) | |
| (identity : Name := `grind) | |
| : TacticM Unit := do | |
| if (← checkTerminalAsSorry mvarId) then return () | |
| mvarId.withContext do | |
| let params ← mkGrindParams config only ps mvarId (extensions? := extensions?) (identity := identity) | |
| let params := if Grind.grind.unusedLemmaThreshold.get (← getOptions) > 0 then | |
| { params with config.markInstances := true } | |
| else params | |
| Grind.withProtectedMCtx config mvarId fun mvarId' => do | |
| let finalize (result : Grind.Result) : TacticM Unit := do | |
| if result.hasFailed then | |
| throwError (← result.toErrorMessageData) | |
| replaceMainGoal [] | |
| if let some seq := seq? then | |
| let (result, _) ← Grind.GrindTacticM.runAtGoal mvarId' params do | |
| -- Like `case x =>`, the tactic head `grind ... =>` gets an info node. It closes before | |
| -- `withProtectedMCtx` admits the goal on failure, so the goal view shows the remaining | |
| -- goals after an incomplete sequence. | |
| if let some head := head? then | |
| Grind.withTacticInfoContext head do | |
| Grind.evalGrindTactic seq | |
| else | |
| Grind.evalGrindTactic seq | |
| -- **Note**: We are returning only the first goal that could not be solved. | |
| let goal? := if let goal :: _ := (← get).goals then some goal else none | |
| let result ← Grind.liftGrindM <| Grind.mkResult params goal? | |
| if goal?.isNone then | |
| Grind.liftGrindM <| Grind.checkUnusedActivations mvarId' result.counters | |
| return result | |
| finalize result | |
| else | |
| let result ← Grind.main mvarId' params | |
| finalize result | |
| def evalGrindCore | |
| (ref : Syntax) | |
| (config : Grind.Config) | |
| (only : Option Syntax) | |
| (params? : Option (Syntax.TSepArray `Lean.Parser.Tactic.grindParam ",")) | |
| (seq? : Option (TSyntax `Lean.Parser.Tactic.Grind.grindSeq)) | |
| (extensions? : Option Grind.ExtensionStateArray := none) | |
| (head? : Option Syntax := none) | |
| (identity : Name := `grind) | |
| : TacticM Unit := do | |
| let only := only.isSome | |
| let params := if let some params := params? then params.getElems else #[] | |
| if Grind.grind.warning.get (← getOptions) then | |
| logWarningAt ref "The `grind` tactic is new and its behavior may change in the future. This project has used `set_option grind.warning true` to discourage its use." | |
| grind (← getMainGoal) config only params seq? (extensions? := extensions?) (head? := head?) (identity := identity) | |
| /-- Position for the `[..]` child syntax in the `grind` tactic. -/ | |
| def grindParamsPos := 3 | |
| /-- Position for the `only` child syntax in the `grind` tactic. -/ | |
| def grindOnlyPos := 2 | |
| def isGrindOnly (stx : TSyntax `tactic) : Bool := | |
| stx.raw.getKind == ``Parser.Tactic.grind && !stx.raw[grindOnlyPos].isNone | |
| def setGrindParams (stx : TSyntax `tactic) (params : Array Syntax) : TSyntax `tactic := | |
| if params.isEmpty then | |
| ⟨stx.raw.setArg grindParamsPos (mkNullNode)⟩ | |
| else | |
| let paramsStx := #[mkAtom "[", (mkAtom ",").mkSep params, mkAtom "]"] | |
| ⟨stx.raw.setArg grindParamsPos (mkNullNode paramsStx)⟩ | |
| def getGrindParams (stx : TSyntax `tactic) : Array Syntax := | |
| stx.raw[grindParamsPos][1].getSepArgs | |
| /-- Filter out `+suggestions` and `+locals` from the config syntax -/ | |
| def filterSuggestionsAndLocalsFromGrindConfig (config : TSyntax ``Lean.Parser.Tactic.optConfig) : | |
| TSyntax ``Lean.Parser.Tactic.optConfig := | |
| -- optConfig structure: (Tactic.optConfig [configItem1, configItem2, ...]) | |
| -- config.raw.getArgs returns #[null_node], so we need to filter the null node's children | |
| let nullNode := config.raw[0]! | |
| let configItems := nullNode.getArgs | |
| let filteredItems := configItems.filter fun configItem => | |
| -- Keep all items except +suggestions and +locals | |
| -- Structure: configItem -> posConfigItem -> ["+", ident] | |
| match configItem[0]? with | |
| | some posConfigItem => match posConfigItem[1]? with | |
| | some ident => | |
| let id := ident.getId.eraseMacroScopes | |
| !(posConfigItem.getKind == ``Lean.Parser.Tactic.posConfigItem && (id == `suggestions || id == `locals)) | |
| | none => true | |
| | none => true | |
| ⟨config.raw.setArg 0 (nullNode.setArgs filteredItems)⟩ | |
| private def elabGrindConfig' (config : TSyntax ``Lean.Parser.Tactic.optConfig) (interactive : Bool) : TacticM Grind.Config := do | |
| if interactive then | |
| elabGrindConfigInteractive config | |
| else | |
| elabGrindConfig config | |
| @[builtin_tactic Lean.Parser.Tactic.grind] def evalGrind : Tactic := fun stx => do | |
| -- Preserve this import in core; all others import `Init` anyway | |
| recordExtraModUse (isMeta := false) `Init.Grind.Tactics | |
| let `(tactic| grind%$tk $config:optConfig $[only%$only]? $[ [$params:grindParam,*] ]? $[=>%$arrow $seq:grindSeq]?) := stx | |
| | throwUnsupportedSyntax | |
| let interactive := seq.isSome | |
| let config ← elabGrindConfig' config interactive | |
| evalGrindCore stx config only params seq (head? := arrow.map (mkNullNode #[tk, ·])) | |
| @[builtin_tactic Lean.Parser.Tactic.sym] def evalSym : Tactic := fun stx => do | |
| recordExtraModUse (isMeta := false) `Init.Grind.Tactics | |
| let `(tactic| sym%$tk $config:optConfig $[only%$only]? $[ [$params:grindParam,*] ]? =>%$arrow $seq:grindSeq) := stx | |
| | throwUnsupportedSyntax | |
| let config ← elabGrindConfig' config true | |
| let only' := only.isSome | |
| let params := if let some params := params then params.getElems else #[] | |
| let mvarId ← getMainGoal | |
| if (← checkTerminalAsSorry mvarId) then return () | |
| mvarId.withContext do | |
| let params ← mkGrindParams config only' params mvarId (identity := `sym) | |
| Grind.withProtectedMCtx config mvarId fun mvarId' => do | |
| let (result, _) ← Grind.GrindTacticM.runAtGoal mvarId' params (sym := true) do | |
| -- closes before `withProtectedMCtx` admits the goal on failure, see `grind` | |
| Grind.withTacticInfoContext (mkNullNode #[tk, arrow]) do | |
| Grind.evalGrindTactic seq | |
| let goal? := if let goal :: _ := (← get).goals then some goal else none | |
| Grind.liftGrindM <| Grind.mkResult params goal? | |
| if result.hasFailed then | |
| throwError (← result.toErrorMessageData) | |
| /-- Appends to `ps` the `inj` parameters used by the proof assigned to `mvarId`. -/ | |
| private def addUsedInjParams (params : Grind.Params) (mvarId : MVarId) (ps : Array Grind.TParam) : | |
| MetaM (Array Grind.TParam) := do | |
| let injParams ← Grind.getUsedInjParams params (← instantiateMVars (mkMVar mvarId)) | |
| return ps ++ injParams.filter (!ps.contains ·) | |
| /-- | |
| Returns `true` if `tac` closes `mvarId` without errors. Explicit `sorry`s are allowed, since | |
| `grind?` uses them to mark where the search got stuck. Does not modify the state, including | |
| trace messages. | |
| -/ | |
| private def closesGoal (mvarId : MVarId) (tac : TSyntax `tactic) : TacticM Bool := do | |
| let traceState ← getTraceState | |
| let result ← withoutModifyingState do | |
| modifyThe Core.State fun s => { s with messages := {} } | |
| try | |
| let gs ← Term.withoutErrToSorry <| Tactic.run mvarId (evalTactic tac) | |
| return gs.isEmpty && !(← Core.getMessageLog).hasErrors | |
| && !(← instantiateMVars (mkMVar mvarId)).hasSyntheticSorry | |
| catch _ => | |
| return false | |
| setTraceState traceState | |
| return result | |
| /-- | |
| Removes the tactics in `tacs` that do not close `mvarId` at the saved state `s`, where `mvarId` | |
| must not be assigned yet. If `warn` is `true`, reports them in a warning. | |
| -/ | |
| def filterClosingTactics (s : Tactic.SavedState) (mvarId : MVarId) (tacs : Array (TSyntax `tactic)) | |
| (warn := true) : TacticM (Array (TSyntax `tactic)) := do | |
| let current ← saveState | |
| s.restore | |
| let mut ok := #[] | |
| let mut failed := #[] | |
| for tac in tacs do | |
| if (← closesGoal mvarId tac) then ok := ok.push tac else failed := failed.push tac | |
| current.restore | |
| if warn && !failed.isEmpty then | |
| let header := if failed.size == 1 then "generated tactic cannot close the goal" | |
| else "generated tactics cannot close the goal" | |
| logWarning <| failed.foldl (init := m!"{header}") fun msg tac => msg ++ indentD m!"{tac}" | |
| return ok | |
| def evalGrindTraceCore (stx : Syntax) (trace := true) (verbose := true) (useSorry := true) : TacticM (Array (TSyntax `tactic)) := withMainContext do | |
| let `(tactic| grind? $configStx:optConfig $[only%$only]? $[ [$params?:grindParam,*] ]?) := stx | |
| | throwUnsupportedSyntax | |
| let config ← elabGrindConfig configStx | |
| let config := { config with clean := false, trace, verbose, useSorry } | |
| let only := only.isSome | |
| let paramStxs := if let some params := params? then params.getElems else #[] | |
| let termParamStxs ← Grind.getPreservedParams paramStxs | |
| let mvarId ← getMainGoal | |
| let params ← mkGrindParams config only paramStxs mvarId | |
| Grind.withProtectedMCtx config mvarId fun mvarId' => do | |
| let (tacs, _) ← Grind.GrindTacticM.runAtGoal mvarId' params do | |
| let finish ← Grind.Action.mkFinish | |
| let goal :: goals ← Grind.getGoals | |
| | -- Goal was closed during initialization | |
| let configStx' := filterSuggestionsAndLocalsFromGrindConfig configStx | |
| let onlyParams ← addUsedInjParams params mvarId' termParamStxs | |
| if onlyParams.isEmpty then | |
| let tac ← `(tactic| grind $configStx':optConfig only) | |
| return #[tac] | |
| else | |
| let tac ← `(tactic| grind $configStx':optConfig only [$onlyParams,*]) | |
| return #[tac] | |
| Grind.liftGrindM do | |
| -- **Note**: If we get failures when using the first suggestion, we should test is using `saved` | |
| -- let saved ← saveState | |
| /- | |
| Asserting the parameters may split the goal (e.g., a fact whose type is marked | |
| `cases eager`), so there may be several goals. Each one gets its own script. | |
| -/ | |
| let mut seqs := #[] | |
| for goal in goal :: goals do | |
| match (← finish.run goal) with | |
| | .closed seq => seqs := seqs.push seq | |
| | .stuck gs => | |
| let goal :: _ := gs | throwError "`grind?` failed, but resulting goal is not available" | |
| let result ← Grind.mkResult params (some goal) | |
| throwError (← result.toErrorMessageData `grind?) | |
| let seq ← if h : seqs.size = 1 then pure seqs[0] else seqs.toList.mapM fun seq => Grind.Action.mkGrindNext seq | |
| let configStx' := filterSuggestionsAndLocalsFromGrindConfig configStx | |
| let tacs ← Grind.mkGrindOnlyTactics configStx' seq (← addUsedInjParams params mvarId' termParamStxs) config.ematch | |
| let seq := if Grind.grind.debug.dropSteps.get (← getOptions) then seq.dropLast else seq | |
| let seq := Grind.Action.mkGrindSeq seq | |
| /- | |
| **Note**: The script must carry the preserved parameters (e.g., types marked for | |
| case-splitting). The tactic was verified with these parameters active, and `cases` | |
| steps may fail without them. | |
| -/ | |
| let tac ← if termParamStxs.isEmpty then | |
| `(tactic| grind $configStx':optConfig => $seq:grindSeq) | |
| else | |
| `(tactic| grind $configStx':optConfig [$termParamStxs,*] => $seq:grindSeq) | |
| let tacs := tacs.push tac | |
| if Grind.grind.debug.badAnchors.get (← getOptions) then | |
| let badAnchor ← `(Parser.Tactic.anchor| #$(⟨mkNode `hexnum #[mkAtom "0000"]⟩)) | |
| return tacs.map fun tac => ⟨Id.run <| tac.raw.replaceM fun stx => | |
| return if stx.isOfKind ``Parser.Tactic.anchor then some badAnchor else none⟩ | |
| return tacs | |
| return tacs | |
| @[builtin_tactic Lean.Parser.Tactic.grindTrace] def evalGrindTrace : Tactic := fun stx => do | |
| let `(tactic| grind? $configStx:optConfig $[only%$only]? $[ [$params?:grindParam,*] ]?) := stx | |
| | throwUnsupportedSyntax | |
| let mvarId ← getMainGoal | |
| let saved ← saveState | |
| let tacs ← filterClosingTactics saved mvarId (← evalGrindTraceCore stx) | |
| let tacs ← if tacs.isEmpty then | |
| pure #[← `(tactic| grind $configStx:optConfig $[only%$only]? $[ [$params?:grindParam,*] ]?)] | |
| else | |
| pure tacs | |
| if tacs.size == 1 then | |
| Tactic.TryThis.addSuggestion stx { suggestion := .tsyntax tacs[0]! } | |
| else | |
| Tactic.TryThis.addSuggestions stx <| tacs.map fun tac => { suggestion := .tsyntax tac } | |
| @[builtin_tactic Lean.Parser.Tactic.lia] def evalLia : Tactic := fun stx => do | |
| let `(tactic| lia $config:optConfig $[ [$params:grindParam,*] ]?) := stx | throwUnsupportedSyntax | |
| let config ← elabCutsatConfig config | |
| let extensions ← Grind.getLiaExtensions | |
| evalGrindCore stx { config with } none params none (extensions? := some extensions) (identity := `lia) | |
| @[builtin_tactic Lean.Parser.Tactic.cutsat] def evalCutsat : Tactic := fun stx => do | |
| let `(tactic| cutsat $config:optConfig) := stx | throwUnsupportedSyntax | |
| -- Emit deprecation warning | |
| logWarning "`cutsat` has been deprecated, use `lia` instead" | |
| -- Emit a "try this:" suggestion to replace `cutsat` with `lia` | |
| let liaTac ← `(tactic| lia $config:optConfig) | |
| Tactic.TryThis.addSuggestion stx { suggestion := .tsyntax liaTac } | |
| -- Execute the same logic as lia | |
| let config ← elabCutsatConfig config | |
| let extensions ← Grind.getLiaExtensions | |
| evalGrindCore stx { config with } none none none (extensions? := some extensions) (identity := `lia) | |
| @[builtin_tactic Lean.Parser.Tactic.grind_order] def evalOrder : Tactic := fun stx => do | |
| let `(tactic| grind_order $config:optConfig) := stx | throwUnsupportedSyntax | |
| let config ← elabOrderConfig config | |
| evalGrindCore stx { config with } none none none (identity := `grind_order) | |
| @[builtin_tactic Lean.Parser.Tactic.grind_linarith] def evalLinarith : Tactic := fun stx => do | |
| let `(tactic| grind_linarith $config:optConfig) := stx | throwUnsupportedSyntax | |
| let config ← elabLinarithConfig config | |
| evalGrindCore stx { config with } none none none (identity := `grind_linarith) | |
| @[builtin_tactic Lean.Parser.Tactic.grobner] def evalGrobner : Tactic := fun stx => do | |
| let `(tactic| grobner $config:optConfig $[ [$params:grindParam,*] ]?) := stx | throwUnsupportedSyntax | |
| if params.isSome then | |
| -- Enable E-matching for the supplied lemmas without the `@[grind]` E-matching rules, and | |
| -- disable local-theorem E-matching as `lia` does. Other `@[grind]` data (e.g. `inj`) is kept. | |
| let config ← elabGrobnerConfig config (init := { ematch := ({} : Grind.Config).ematch, genLocal := 0 }) | |
| let ext := { (← Grind.getDefaultExtensionState) with ematch := {} } | |
| evalGrindCore stx { config with } none params none (extensions? := some #[ext]) (identity := `grobner) | |
| else | |
| let config ← elabGrobnerConfig config | |
| evalGrindCore stx { config with } none params none (identity := `grobner) | |
| end Lean.Elab.Tactic |