blob: 75a3bbe03160f55708edbc2ba142d44f4b8ba7fb [file]
/-
Copyright (c) 2020 Microsoft Corporation. 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.Rewrite
public import Lean.Meta.Tactic.Replace
public import Lean.Elab.Tactic.Location
import Lean.Elab.ConfigEval
import Lean.Meta.Eqns
public section
namespace Lean.Elab.Tactic
open Meta
/--
Runs `Lean.MVarId.rewrite`, and also handles filtering out the old metavariables in the rewrite result.
This should be used from within `withSynthesize`.
Use `finishElabRewrite` once elaboration is complete to make final updates to `RewriteResult`.
-/
def elabRewrite (mvarId : MVarId) (e : Expr) (stx : Syntax)
(symm : Bool := false) (config := { : Rewrite.Config }) : TacticM RewriteResult := do
let mvarCounterSaved := (← getMCtx).mvarCounter
let thm ← elabTerm stx none true
if thm.hasSyntheticSorry then
throwAbortTactic
unless ← occursCheck mvarId thm do
throwErrorAt stx "Occurs check failed: Expression{indentExpr thm}\ncontains the goal {Expr.mvar mvarId}"
let r ← withInstancesTypeCheckNote e do
mvarId.rewrite e thm symm (config := config)
let mctx ← getMCtx
let mvarIds := r.mvarIds.filter fun mvarId => (mctx.getDecl mvarId |>.index) >= mvarCounterSaved
return { r with mvarIds }
/--
Makes new goals be synthetic opaque, to be done once elaboration of the rewrite theorem is complete.
Workaround note: we are only doing this for proof goals, not data goals,
since there are many downstream cases of tactic proofs that later assign data goals by unification.
-/
def finishElabRewrite (r : RewriteResult) : MetaM RewriteResult := do
let mvarIds ← r.mvarIds.filterM (not <$> ·.isAssigned)
mvarIds.forM fun newMVarId => newMVarId.withContext do
if ← Meta.isProp (← newMVarId.getType) then
newMVarId.setKind .syntheticOpaque
return { r with mvarIds }
def rewriteTarget (stx : Syntax) (symm : Bool) (config : Rewrite.Config := {}) : TacticM Unit := do
let r ← Term.withSynthesize <| withMainContext do
elabRewrite (← getMainGoal) (← getMainTarget) stx symm (config := config)
let r ← finishElabRewrite r
let mvarId' ← (← getMainGoal).replaceTargetEq r.eNew r.eqProof
replaceMainGoal (mvarId' :: r.mvarIds)
/-- Rewrites a local declaration and returns the replacement free variable. -/
def rewriteLocalDeclCore (stx : Syntax) (symm : Bool) (fvarId : FVarId)
(config : Rewrite.Config := {}) : TacticM FVarId := withMainContext do
-- Note: we cannot execute `replaceLocalDecl` inside `Term.withSynthesize`.
-- See issues #2711 and #2727.
let rwResult ← Term.withSynthesize <| withMainContext do
let localDecl ← fvarId.getDecl
elabRewrite (← getMainGoal) localDecl.type stx symm (config := config)
let rwResult ← finishElabRewrite rwResult
let replaceResult ← (← getMainGoal).replaceLocalDecl fvarId rwResult.eNew rwResult.eqProof
replaceMainGoal (replaceResult.mvarId :: rwResult.mvarIds)
return replaceResult.fvarId
def rewriteLocalDecl (stx : Syntax) (symm : Bool) (fvarId : FVarId) (config : Rewrite.Config := {}) :
TacticM Unit := do
discard <| rewriteLocalDeclCore stx symm fvarId config
/-- Folds a tactic action over a sequence of rewrite rules. -/
def foldRWRulesSeq (token : Syntax) (rwRulesSeqStx : Syntax) (init : α)
(x : α → (symm : Bool) → (term : Syntax) → TacticM α) : TacticM α := do
let lbrak := rwRulesSeqStx[0]
let rules := rwRulesSeqStx[1].getArgs
-- show initial state up to (incl.) `[`
withTacticInfoContext (mkNullNode #[token, lbrak]) (pure ())
let mut acc := init
let numRules := (rules.size + 1) / 2
for i in *...numRules do
let rule := rules[i * 2]!
let sep := rules.getD (i * 2 + 1) Syntax.missing
-- show rule state up to (incl.) next `,`
let acc' ← withTacticInfoContext (mkNullNode #[rule, sep]) do
-- show errors on rule
withRef rule do
let symm := !rule[0].isNone
let term := rule[1]
let processId (id : Syntax) : TacticM α := do
-- See if we can interpret `id` as a hypothesis first.
if (← withMainContext <| Term.isLocalIdent? id).isSome then
x acc symm term
else
-- Try to get equation theorems for `id`.
let declName ← try realizeGlobalConstNoOverload id catch _ => return (← x acc symm term)
let some eqThms ← getEqnsFor? declName | x acc symm term
let hint := if eqThms.size = 1 then m!"" else
.hint' m!"Try rewriting with `{Name.str declName unfoldThmSuffix}`"
let rec go : List Name → TacticM α
| [] => throwError m!"Failed to rewrite using equation theorems for `{.ofConstName declName}`" ++ hint
| eqThm::eqThms => (x acc symm (mkCIdentFrom id eqThm)) <|> go eqThms
discard <| Term.addTermInfo id (← mkConstWithFreshMVarLevels declName) (lctx? := ← getLCtx)
go eqThms.toList
match term with
| `($id:ident) => processId id
| `(@$id:ident) => processId id
| _ => x acc symm term
acc := acc'
return acc
def withRWRulesSeq (token : Syntax) (rwRulesSeqStx : Syntax)
(x : (symm : Bool) → (term : Syntax) → TacticM Unit) : TacticM Unit := do
discard <| foldRWRulesSeq token rwRulesSeqStx () fun _ symm term => x symm term
declare_config_elab elabRewriteConfig Rewrite.Config
@[builtin_tactic Lean.Parser.Tactic.rewriteSeq] def evalRewriteSeq : Tactic := fun stx => do
let cfg ← elabRewriteConfig stx[1]
let loc := expandOptLocation stx[3]
withRWRulesSeq stx[0] stx[2] fun symm term => do
withLocation loc
(rewriteLocalDecl term symm · cfg)
(rewriteTarget term symm cfg)
(throwTacticEx `rewrite · "Did not find an occurrence of the pattern in the current goal")
end Lean.Elab.Tactic