Skip to content

Commit

Permalink
refactor: genralize utils
Browse files Browse the repository at this point in the history
  • Loading branch information
William Sørensen committed Jul 25, 2024
1 parent d904907 commit 05a9195
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
25 changes: 14 additions & 11 deletions Qpf/Macro/Data/Constructors.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Qpf.Macro.Data.Replace
import Qpf.Macro.Data.View
import Qpf.Macro.NameUtils

open Lean Meta Elab.Command
open Lean Meta Elab Elab.Command
open PrettyPrinter (delab)

namespace Data.Command
Expand All @@ -15,11 +15,7 @@ partial def countConstructorArgs : Syntax → Nat
| _ => 0


open Elab
/--
Add convenient constructor functions to the environment
-/
def mkConstructors (view : DataView) (shape : Name) : CommandElabM Unit := do
def mkConstructorsWithNameAndType (view : DataView) (shape : Name) (nameGen : CtorView → Ident) (ty : Term) := do
for ctor in view.ctors do
trace[QPF] "mkConstructors\n{ctor.declName} : {ctor.type?}"
let n_args := (ctor.type?.map countConstructorArgs).getD 0
Expand All @@ -40,18 +36,15 @@ def mkConstructors (view : DataView) (shape : Name) : CommandElabM Unit := do
`(fun $args:ident* => $mk ($shapeCtor $args:ident*))
let body ← body

let explicit ← view.getExplicitExpectedType
let type : Term := TSyntax.mk <|
(ctor.type?.map fun type =>
Replace.replaceAllStx view.getExpectedType explicit type
).getD explicit
(ctor.type?.map fun type => Replace.replaceAllStx view.getExpectedType ty type).getD ty
let modifiers : Modifiers := {
isNoncomputable := view.modifiers.isNoncomputable
attrs := #[{
name := `matchPattern
}]
}
let declId := mkIdent <| Name.stripPrefix2 (←getCurrNamespace) ctor.declName
let declId := nameGen ctor

let cmd ← `(
$(quote modifiers):declModifiers
Expand All @@ -62,4 +55,14 @@ def mkConstructors (view : DataView) (shape : Name) : CommandElabM Unit := do
elabCommand cmd
return ()

/--
Add convenient constructor functions to the environment
-/
def mkConstructors (view : DataView) (shape : Name) : CommandElabM Unit := do
let explicit ← view.getExplicitExpectedType
let nameGen := (mkIdent <| Name.stripPrefix2 (←getCurrNamespace) ·.declName)

mkConstructorsWithNameAndType view shape nameGen explicit


end Data.Command
8 changes: 6 additions & 2 deletions Qpf/Macro/Data/View.lean
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,15 @@ def DataView.addDeclNameSuffix (view : DataView) (suffix : String) : DataView


/-- Returns the fully applied form of the type to be defined -/
def DataView.getExpectedType (view : DataView) : Term
:= Syntax.mkApp (mkIdent view.shortDeclName) (
def DataView.getExpectedTypeWithId (view : DataView) (id : Ident) : Term
:= Syntax.mkApp id (
(Macro.getBinderIdents view.binders.getArgs false)
)

/-- Returns the fully applied form of the type to be defined -/
def DataView.getExpectedType (view : DataView) : Term
:= view.getExpectedTypeWithId (mkIdent view.shortDeclName)

/-- Returns the fully applied, explicit (`@`) form of the type to be defined -/
def DataView.getExplicitExpectedType (view : DataView) : CommandElabM Term
:= let args := Macro.getBinderIdents view.binders.getArgs true
Expand Down

0 comments on commit 05a9195

Please sign in to comment.