Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize utility functions previously used for a single purpose #34

Merged
merged 10 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions Qpf/Macro/Data/Constructors.lean
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Qpf.Macro.Data.Replace
import Qpf.Macro.Data.View

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

namespace Data.Command
Expand All @@ -13,11 +13,13 @@ partial def countConstructorArgs : Syntax → Nat
| Syntax.node _ ``Term.arrow #[_, _, tail] => 1 + (countConstructorArgs tail)
| _ => 0

open Elab
/--
Add convenient constructor functions to the environment
Add definitions for constructors
that are generic across two input types shape and name.
Additionally we allow the user to control how names are generated.
-/
def mkConstructors (view : DataView) (shape : Name) : CommandElabM Unit := do
def mkConstructorsWithNameAndType (view : DataView) (shape : Name)
(nameGen : CtorView → Name) (ty : Term) : CommandElabM Unit := 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 @@ -34,18 +36,15 @@ def mkConstructors (view : DataView) (shape : Name) : CommandElabM Unit := do
else
`(fun $args:ident* => $pointConstructor ($shapeCtor $args:ident*))

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 <| ctor.declName.replacePrefix (←getCurrNamespace) .anonymous
let declId := mkIdent $ nameGen ctor

let cmd ← `(
$(quote modifiers):declModifiers
Expand All @@ -56,4 +55,13 @@ 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 := (·.declName.replacePrefix (←getCurrNamespace) .anonymous)

mkConstructorsWithNameAndType view shape nameGen explicit

end Data.Command
14 changes: 10 additions & 4 deletions Qpf/Macro/Data/View.lean
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,17 @@ def DataView.addDeclNameSuffix (view : DataView) (suffix : String) : DataView
{ view with declName, declId, shortDeclName }


/--
Returns the fully applied form of the type to be defined.
The `name` parameter allows the user to control what the const at the bottom of the type is.
This lets the user get types that have the same parameters such as the DeepThunk types.
-/
def DataView.getExpectedTypeWithName (view : DataView) (name : Name) : Term :=
Macro.getBinderIdents view.binders.getArgs false |> Syntax.mkApp (mkIdent name)

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

/-- Returns the fully applied, explicit (`@`) form of the type to be defined -/
def DataView.getExplicitExpectedType (view : DataView) : CommandElabM Term
Expand Down
Loading