| /- | |
| 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 Init.Grind.Lemmas | |
| public import Lean.Meta.Tactic.Grind.Action | |
| import Lean.Meta.Tactic.Apply | |
| import Lean.Meta.Tactic.Grind.Util | |
| import Lean.Meta.Tactic.Grind.CasesMatch | |
| import Lean.Meta.Tactic.Grind.Injection | |
| import Lean.Meta.Tactic.Grind.Core | |
| import Lean.Meta.Tactic.Grind.Simp | |
| import Lean.Meta.Tactic.Grind.MarkAccessible | |
| import Lean.Meta.Tactic.Grind.Anchor | |
| import Init.Data.List.MapIdx | |
| import Init.Grind.Util | |
| public section | |
| namespace Lean.Meta.Grind | |
| private inductive IntroResult where | |
| | done (goal : Goal) | |
| | newHyp (fvarId : FVarId) (goal : Goal) | |
| | newDepHyp (goal : Goal) | |
| | newLocal (fvarId : FVarId) (goal : Goal) | |
| deriving Inhabited | |
| /-- | |
| Returns `true` if `e` is marked with the `alreadyNorm` gadget. | |
| See `alreadyNorm` documentation for additional details | |
| -/ | |
| private def isAlreadyNorm? (e : Expr) : Option Expr := | |
| let_expr Lean.Grind.alreadyNorm c := e | none | |
| some c | |
| private def isSIntPatternLitValue (e : Expr) : Bool := | |
| match_expr e with | |
| | OfNat.ofNat type n _ => isSIntType type && n.consumeMData.isRawNatLit | |
| | Neg.neg type _ a => | |
| match_expr a with | |
| | OfNat.ofNat innerType n _ => | |
| type.consumeMData == innerType.consumeMData && isSIntType type && n.consumeMData.isRawNatLit | |
| | _ => false | |
| | _ => false | |
| where | |
| isSIntType (type : Expr) : Bool := | |
| let type := type.consumeMData | |
| type.isConstOf ``Int8 || type.isConstOf ``Int16 || type.isConstOf ``Int32 || type.isConstOf ``Int64 | |
| /-- Match conditions can also compare noncanonical signed literals using `normLitValue`. -/ | |
| private def isPatternLitValue (e : Expr) : MetaM Bool := do | |
| if e.hasLooseBVars || e.isTrue || e.isFalse then return false | |
| return (← isInterpreted e) || isSIntPatternLitValue e | |
| /-- | |
| Returns `true` if `e` is a pattern: a pattern variable (i.e., a loose bound variable), | |
| a literal supported by match-condition propagation, an offset `p + k`, or a constructor application | |
| whose fields are patterns. | |
| -/ | |
| private partial def isPattern (e : Expr) : MetaM Bool := do | |
| if e.getAppFn.isBVar then return true | |
| if (← isPatternLitValue e) then return true | |
| match_expr e with | |
| | HAdd.hAdd _ _ _ _ p k => | |
| -- Signed offsets require arithmetic normalization before matching. | |
| if isSIntPatternLitValue k then return false | |
| return (← isPatternLitValue k <&&> isPattern p) | |
| | _ => pure () | |
| let .const declName _ := e.getAppFn | return false | |
| let some (.ctorInfo ctor) := (← getEnv).find? declName | return false | |
| unless e.getAppNumArgs == ctor.numParams + ctor.numFields do return false | |
| let args := e.getAppArgs | |
| for i in ctor.numParams...args.size do | |
| unless (← isPattern args[i]!) do return false | |
| return true | |
| /-- | |
| Returns `true` if `isMatchCondCandidate e` holds and the right-hand sides of its equations are patterns. | |
| The `Grind.MatchCond` propagators cannot use a condition whose right-hand sides are not patterns, | |
| e.g., `∀ n, a = f n → False` (see `tryToProveFalse`). | |
| -/ | |
| private partial def isMatchCondHyp (e : Expr) : MetaM Bool := do | |
| unless isMatchCondCandidate e do return false | |
| go e | |
| where | |
| go (e : Expr) : MetaM Bool := do | |
| let .forallE _ d b _ := e | return true | |
| match_expr d with | |
| | Eq _ _ rhs => isPattern rhs <&&> go b | |
| | HEq _ _ _ rhs => isPattern rhs <&&> go b | |
| | _ => go b | |
| /-- | |
| Similar to `Grind.preprocess`, but does not simplify `e` if it looks like a | |
| `match`-expression condition (see `isMatchCondHyp`). | |
| We added this feature because it may be coming from external sources | |
| (e.g., manually applying an function induction principle before invoking `grind`). | |
| Other hypotheses are simplified as usual, and become available for E-matching (issue #15697). | |
| -/ | |
| def preprocessHypothesis (e : Expr) : GoalM Simp.Result := do | |
| if (← isMatchCondHyp e) then | |
| preprocess (markAsPreMatchCond e) | |
| else if let some c := isAlreadyNorm? e then | |
| let c ← shareCommon (← canon c) | |
| return { expr := c } | |
| else | |
| preprocess e | |
| /-- | |
| Helper function for `mkCleanName`. | |
| It ensures base name is a simple `Name` and does not have a `_<idx>` suffix | |
| -/ | |
| private def mkBaseName (name : Name) (type : Expr) : MetaM Name := do | |
| if let .str _ s := name then | |
| let pos := s.find '_' | |
| if h : pos.IsAtEnd then | |
| return Name.mkSimple s | |
| else | |
| let suffix := s.sliceFrom (pos.next h) | |
| unless suffix.isNat do | |
| return Name.mkSimple s | |
| let s := s.sliceTo pos | |
| unless s.isEmpty do | |
| return Name.mkSimple s.copy | |
| if (← isProp type) then return `h else return `x | |
| private def mkCleanName (name : Name) (type : Expr) : GoalM Name := do | |
| if (← getConfig).clean then | |
| let mut name := name | |
| if name.hasMacroScopes then | |
| name := name.eraseMacroScopes | |
| if name == `x || name == `a then | |
| if (← isProp type) then | |
| name := `h | |
| if (← get).clean.used.contains name then | |
| let base ← mkBaseName name type | |
| let mut i := if let some i := (← get).clean.next.find? base then i else 1 | |
| repeat | |
| name := base.appendIndexAfter i | |
| i := i + 1 | |
| unless (← get).clean.used.contains name do | |
| break | |
| modify fun s => { s with clean.next := s.clean.next.insert base i } | |
| modify fun s => { s with clean.used := s.clean.used.insert name } | |
| return name | |
| else if let some originalName := getOriginalName? name then | |
| return originalName | |
| else if name.hasMacroScopes then | |
| let name' := name.eraseMacroScopes | |
| if name' == `x || name' == `a then | |
| if (← isProp type) then | |
| return (← mkFreshUserName `h) | |
| return name | |
| else | |
| mkFreshUserName name | |
| private def intro1 : GoalM FVarId := do | |
| let target ← (← get).mvarId.getType | |
| let (name, type) ← match target with | |
| | .forallE n d .. => pure (n, d) | |
| | .letE n d .. => pure (n, d) | |
| | _ => throwError "`grind` internal error, binder expected" | |
| let name ← mkCleanName name type | |
| let (fvarId, mvarId) ← (← get).mvarId.intro name | |
| modify fun s => { s with mvarId } | |
| registerFVarAnchor fvarId | |
| return fvarId | |
| private partial def introNext (goal : Goal) (generation : Nat) : GrindM IntroResult := do | |
| Prod.fst <$> GoalM.run goal do | |
| let target ← (← get).mvarId.getType | |
| if target.isForall then | |
| let p := target.bindingDomain! | |
| if !(← isProp p) then | |
| let fvarId ← intro1 | |
| return .newLocal fvarId (← get) | |
| else | |
| let mvarId := (← get).mvarId | |
| let tag ← mvarId.getTag | |
| let qBase := target.bindingBody! | |
| let fvarId ← mkFreshFVarId | |
| registerFVarAnchor fvarId | |
| let fvar := mkFVar fvarId | |
| let r ← preprocessHypothesis p | |
| let lctx := (← getLCtx).mkLocalDecl fvarId (← mkCleanName target.bindingName! r.expr) r.expr target.bindingInfo! | |
| let mut localInsts ← getLocalInstances | |
| if let some className ← isClass? r.expr then | |
| localInsts := localInsts.push { className, fvar } | |
| match r.proof? with | |
| | some he => | |
| if target.isArrow then | |
| let q := qBase | |
| let u ← getLevel q | |
| let mvarNew ← mkFreshExprMVarAt lctx localInsts q .syntheticOpaque tag | |
| let mvarIdNew := mvarNew.mvarId! | |
| mvarIdNew.withContext do | |
| let h ← mkLambdaFVars #[fvar] mvarNew | |
| let hNew := mkAppN (mkConst ``Lean.Grind.intro_with_eq [u]) #[p, r.expr, q, he, h] | |
| mvarId.assign hNew | |
| return .newHyp fvarId { (← get) with mvarId := mvarIdNew } | |
| else | |
| let q := mkLambda target.bindingName! target.bindingInfo! p qBase | |
| let q' := qBase.instantiate1 (mkApp4 (mkConst ``Eq.mpr_prop) p r.expr he fvar) | |
| let u ← getLevel q' | |
| let mvarNew ← mkFreshExprMVarAt lctx localInsts q' .syntheticOpaque tag | |
| let mvarIdNew := mvarNew.mvarId! | |
| mvarIdNew.withContext do | |
| let h ← mkLambdaFVars #[fvar] mvarNew | |
| let hNew := mkAppN (mkConst ``Lean.Grind.intro_with_eq' [u]) #[p, r.expr, q, he, h] | |
| mvarId.assign hNew | |
| return .newHyp fvarId { (← get) with mvarId := mvarIdNew } | |
| | none => | |
| -- `p` and `p'` are definitionally equal | |
| let q := if target.isArrow then qBase else qBase.instantiate1 (mkFVar fvarId) | |
| let mvarNew ← mkFreshExprMVarAt lctx localInsts q .syntheticOpaque tag | |
| let mvarIdNew := mvarNew.mvarId! | |
| mvarIdNew.withContext do | |
| let h ← mkLambdaFVars #[mkFVar fvarId] mvarNew | |
| mvarId.assign h | |
| return .newHyp fvarId { (← get) with mvarId := mvarIdNew } | |
| else if target.isLet then | |
| if (← getConfig).zetaDelta then | |
| let targetNew := expandLet target #[] | |
| let mvarId := (← get).mvarId | |
| mvarId.withContext do | |
| let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew (← mvarId.getTag) | |
| mvarId.assign mvarNew | |
| introNext { (← get) with mvarId := mvarNew.mvarId! } generation | |
| else | |
| let fvarId ← intro1 | |
| (← get).mvarId.withContext do | |
| let localDecl ← fvarId.getDecl | |
| if (← isProp localDecl.type) then | |
| -- Add a non-dependent copy | |
| let mvarId ← (← get).mvarId.assert (← mkCleanName `h localDecl.type) localDecl.type (mkFVar fvarId) | |
| return .newDepHyp { (← get) with mvarId } | |
| else | |
| let v := (← fvarId.getDecl).value | |
| let r ← preprocessHypothesis v | |
| let x ← shareCommon (mkFVar fvarId) | |
| addNewEq x r.expr (← r.getProof) generation | |
| return .newLocal fvarId (← get) | |
| else | |
| return .done goal | |
| private def isEagerCasesCandidate (type : Expr) : GrindM Bool := do | |
| let .const declName _ := type.getAppFn | return false | |
| isEagerSplit declName | |
| /-- Returns `true` if `type` is an inductive type with at most one constructor. -/ | |
| private def isCheapInductive (type : Expr) : CoreM Bool := do | |
| let .const declName _ := type.getAppFn | return false | |
| let .inductInfo info ← getConstInfo declName | return false | |
| return info.numCtors <= 1 | |
| private def applyInjection? (goal : Goal) (fvarId : FVarId) : MetaM (Option Goal) := do | |
| if let some mvarId ← injection? goal.mvarId fvarId then | |
| return some { goal with mvarId } | |
| else | |
| return none | |
| private def exfalsoIfNotProp (goal : Goal) : MetaM Goal := goal.mvarId.withContext do | |
| if (← isProp (← goal.mvarId.getType)) then | |
| return goal | |
| else | |
| return { goal with mvarId := (← goal.mvarId.exfalso) } | |
| /-- | |
| Normalizes the target. A `[grind norm]` rule or a reducible definition may expose an implication | |
| or a universal quantifier that `introNext` then introduces like a syntactic one. | |
| -/ | |
| private def simpTarget (goal : Goal) : GrindM Goal := goal.mvarId.withContext do | |
| let target ← goal.mvarId.getType | |
| let r ← simpCore target | |
| if r.expr == target then return goal | |
| let mvarId ← match r.proof? with | |
| | some h => goal.mvarId.replaceTargetEq r.expr h | |
| | none => goal.mvarId.replaceTargetDefEq r.expr | |
| return { goal with mvarId } | |
| def Goal.lastDecl? (goal : Goal) : MetaM (Option LocalDecl) := do | |
| return (← goal.mvarId.getDecl).lctx.lastDecl | |
| namespace Action | |
| private def applyCases? (goal : Goal) (fvarId : FVarId) (generation : Nat) (kp : ActionCont) : GrindM (Option ActionResult) := goal.withContext do | |
| /- | |
| Remark: we used to use `whnfD`. This was a mistake, we don't want to unfold user-defined abstractions. | |
| Example: `a ∣ b` is defined as `∃ x, b = a * x` | |
| -/ | |
| let type ← whnf (← fvarId.getType) | |
| unless (← isEagerCasesCandidate type) do return none | |
| if (← cheapCasesOnly) then | |
| unless (← isCheapInductive type) do return none | |
| let .const declName _ := type.getAppFn | return none | |
| saveCases declName | |
| let goal ← if !isBuiltinEagerCases declName && (← isProp type) then | |
| /- | |
| `cases` only exposes the constructor fields. For a user-defined inductive predicate there is | |
| no propagator that recovers the proposition from its fields, so it must be asserted as well, | |
| e.g., `h : P` and `¬P` where `P` has field-less constructors. The split is performed right | |
| below, so it must not be selected again during the search. | |
| -/ | |
| GoalM.run' goal do | |
| addHypothesis fvarId generation | |
| markCaseSplitAsResolved (← fvarId.getType) | |
| else | |
| pure goal | |
| if goal.inconsistent then return some (← kp goal) | |
| let mvarIds ← cases goal.mvarId (mkFVar fvarId) | |
| let anchor ← getAnchor (mkFVar fvarId) | |
| -- This is a case split, so the E-matching round counter is reset as in `selectNextSplit?`. | |
| let subgoals := mvarIds.mapIdx fun i mvarId => | |
| { goal with mvarId, ematch.num := 0 }.setIntroBase (mixHash anchor (hash i)) | |
| let mut seqNew : Array (TSyntax `grind) := #[] | |
| let mut stuckNew : Array Goal := #[] | |
| for subgoal in subgoals do | |
| match (← kp subgoal) with | |
| | .stuck gs => stuckNew := stuckNew ++ gs | |
| | .closed seq => seqNew := seqNew ++ seq | |
| if stuckNew.isEmpty then | |
| return some (.closed seqNew.toList) | |
| else | |
| return some (.stuck stuckNew.toList) | |
| def intro (generation : Nat) : Action := fun goal kna kp => do | |
| if goal.inconsistent then return .closed [] | |
| let target ← goal.mvarId.getType | |
| if target.isFalse then | |
| kna goal | |
| else match (← introNext goal generation) with | |
| | .done goal => | |
| let goal ← exfalsoIfNotProp goal | |
| let goal ← simpTarget goal | |
| let target ← goal.mvarId.getType | |
| if target.isForall || target.isLet then | |
| kp goal | |
| else if let some mvarId ← goal.mvarId.byContra? then | |
| kp { goal with mvarId } | |
| else | |
| kp goal | |
| | .newDepHyp goal => | |
| kp goal | |
| | .newLocal fvarId goal => | |
| if let some result ← applyCases? goal fvarId generation kp then | |
| return result | |
| else | |
| kp goal | |
| | .newHyp fvarId goal => | |
| if let some goal ← applyInjection? goal fvarId then | |
| kp goal | |
| else if let some result ← applyCases? goal fvarId generation kp then | |
| return result | |
| else | |
| let goal ← GoalM.run' goal <| addHypothesis fvarId generation | |
| kp goal | |
| private def hugeNumber := 1000000 | |
| def intros (generation : Nat) : Action := | |
| ungroup >> (intro generation).loop hugeNumber >> group | |
| /-- Asserts a new fact `prop` with proof `proof` to the given `goal`. -/ | |
| private def assertAt (proof : Expr) (prop : Expr) (generation : Nat) : Action := fun goal kna kp => do | |
| if (← isEagerCasesCandidate prop) then | |
| let mvarId ← goal.mvarId.assert (← mkFreshUserName `h) prop proof | |
| let goal := { goal with mvarId }.setIntroBase (← goal.withContext <| getAnchor prop) | |
| intros generation goal kna kp | |
| else goal.withContext do | |
| let goal ← GoalM.run' goal do | |
| let r ← preprocess prop | |
| let prop' := r.expr | |
| let proof' := mkApp4 (mkConst ``Eq.mp [Level.zero]) prop r.expr (← r.getProof) proof | |
| add prop' proof' generation | |
| kp goal | |
| /-- | |
| Asserts next fact in the `goal` fact queue. | |
| Returns `true` if the queue was not empty and `false` otherwise. | |
| -/ | |
| def assertNext : Action := fun goal kna kp => do | |
| if goal.inconsistent then return .closed [] | |
| let some (fact, newRawFacts) := goal.newRawFacts.dequeue? | |
| | kna goal | |
| let goal := { goal with newRawFacts } | |
| withSplitSource fact.splitSource do | |
| withEmatchDiagSource fact.ematchDiagSource do | |
| assertAt fact.proof fact.prop fact.generation goal kna kp | |
| /-- | |
| Asserts all facts in the `goal` fact queue. | |
| Returns `true` if the queue was not empty and `false` otherwise. | |
| -/ | |
| def assertAll : Action := | |
| assertNext.loop hugeNumber | |
| end Action | |
| /- | |
| Creates an action that tries all solver extensions using `Action.andAlso`, | |
| then drains the `newRawFacts` queue via `assertAll`. | |
| The `assertAll` step is necessary because `processToDo` (called by `solverAction` on the | |
| `.propagated` path) drains the `toProcess` queue (equations, propositions, and propagations for the e-graph), | |
| but the resulting propagation cascade (e.g., congruence closure, or-propagation, | |
| `propagateForallPropDown`) can call `addNewRawFact`, which enqueues to the separate | |
| `newRawFacts` queue. Without this step, these raw facts are never asserted. See issue #12581. | |
| -/ | |
| def Solvers.mkAction : IO Action := do | |
| return (← Solvers.mkActionCore) >> Action.assertAll | |
| end Lean.Meta.Grind |