refactor: add config syntax and macro for boilerplate code

This commit is contained in:
Leonardo de Moura 2021-09-12 18:09:19 -07:00
parent 91001eef5a
commit 8c82302aca
3 changed files with 37 additions and 22 deletions

View file

@ -320,6 +320,9 @@ macro "admit" : tactic => `(exact sorry)
macro "sorry" : tactic => `(exact sorry)
macro "inferInstance" : tactic => `(exact inferInstance)
/- Optional configuration option for tactics -/
syntax config := ("(" &"config" " := " term ")")
syntax locationWildcard := "*"
syntax locationHyp := (colGt ident)+ ("⊢" <|> "|-")? -- TODO: delete
syntax locationTargets := (colGt ident)+ ("⊢" <|> "|-")?
@ -361,8 +364,8 @@ syntax simpPost := "↑"
syntax simpLemma := (simpPre <|> simpPost)? ("←" <|> "<-")? term
syntax simpErase := "-" ident
syntax simpStar := "*"
syntax (name := simp) "simp " ("(" &"config" " := " term ")")? (&"only ")? ("[" (simpStar <|> simpErase <|> simpLemma),* "]")? (location)? : tactic
syntax (name := simpAll) "simp_all " ("(" &"config" " := " term ")")? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? : tactic
syntax (name := simp) "simp " (config)? (&"only ")? ("[" (simpStar <|> simpErase <|> simpLemma),* "]")? (location)? : tactic
syntax (name := simpAll) "simp_all " (config)? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? : tactic
/--
Delta expand the given definition.

View file

@ -0,0 +1,24 @@
/-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.Tactic.Basic
import Lean.Elab.SyntheticMVars
namespace Lean.Elab.Tactic
open Meta
macro "declare_config_elab" elabName:ident type:ident : command =>
`(unsafe def evalUnsafe (e : Expr) : TermElabM $type :=
Term.evalExpr $type ``$type e
@[implementedBy evalUnsafe] constant eval (e : Expr) : TermElabM $type
def $elabName (optConfig : Syntax) : TermElabM $type := do
if optConfig.isNone then
return { : $type }
else
withoutModifyingState <| withLCtx {} {} <| Term.withSynthesize do
let c ← Term.elabTermEnsuringType optConfig[0][3] (Lean.mkConst ``$type)
eval (← instantiateMVars c)
)
end Lean.Elab.Tactic

View file

@ -4,39 +4,27 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.Simp
import Lean.Meta.Tactic.Replace
import Lean.Elab.BuiltinNotation
import Lean.Elab.Tactic.Basic
import Lean.Elab.Tactic.ElabTerm
import Lean.Elab.Tactic.Location
import Lean.Meta.Tactic.Replace
import Lean.Elab.BuiltinNotation
import Lean.Elab.Tactic.Config
namespace Lean.Elab.Tactic
open Meta
unsafe def evalSimpConfigUnsafe (e : Expr) : TermElabM Meta.Simp.Config :=
Term.evalExpr Meta.Simp.Config ``Meta.Simp.Config e
@[implementedBy evalSimpConfigUnsafe] constant evalSimpConfig (e : Expr) : TermElabM Meta.Simp.Config
unsafe def evalSimpConfigCtxUnsafe (e : Expr) : TermElabM Meta.Simp.ConfigCtx :=
Term.evalExpr Meta.Simp.ConfigCtx ``Meta.Simp.ConfigCtx e
@[implementedBy evalSimpConfigCtxUnsafe] constant evalSimpConfigCtx (e : Expr) : TermElabM Meta.Simp.ConfigCtx
declare_config_elab elabSimpConfigCore Meta.Simp.Config
declare_config_elab elabSimpConfigCtxCore Meta.Simp.ConfigCtx
/-
`optConfig` is of the form `("(" "config" ":=" term ")")?`
If `ctx == false`, the argument is assumed to have type `Meta.Simp.Config`, and `Meta.Simp.ConfigCtx` otherwise. -/
def elabSimpConfig (optConfig : Syntax) (ctx : Bool) : TermElabM Meta.Simp.Config := do
if optConfig.isNone then
if ctx then
return { : Meta.Simp.ConfigCtx }.toConfig
else
return {}
if ctx then
return (← elabSimpConfigCtxCore optConfig).toConfig
else
withoutModifyingState <| withLCtx {} {} <| Term.withSynthesize do
let c ← Term.elabTermEnsuringType optConfig[3] (Lean.mkConst (if ctx then ``Meta.Simp.ConfigCtx else ``Meta.Simp.Config))
if ctx then
return (← evalSimpConfigCtx (← instantiateMVars c)).toConfig
else
evalSimpConfig (← instantiateMVars c)
elabSimpConfigCore optConfig
private def addDeclToUnfoldOrLemma (lemmas : Meta.SimpLemmas) (e : Expr) (post : Bool) (inv : Bool) : MetaM Meta.SimpLemmas := do
if e.isConst then