-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 8 commits
d904907
05a9195
e5710e7
0dc4cb3
c9a2348
83ef0a8
432649e
456bfa6
da244f2
7ac7a05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||
|
@@ -13,11 +13,7 @@ partial def countConstructorArgs : Syntax → Nat | |||||
| Syntax.node _ ``Term.arrow #[_, _, tail] => 1 + (countConstructorArgs tail) | ||||||
| _ => 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I feel like having the argument generate a |
||||||
for ctor in view.ctors do | ||||||
trace[QPF] "mkConstructors\n{ctor.declName} : {ctor.type?}" | ||||||
let n_args := (ctor.type?.map countConstructorArgs).getD 0 | ||||||
|
@@ -34,18 +30,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 := nameGen ctor | ||||||
|
||||||
let cmd ← `( | ||||||
$(quote modifiers):declModifiers | ||||||
|
@@ -56,4 +49,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 := (mkIdent <| ·.declName.replacePrefix (←getCurrNamespace) .anonymous) | ||||||
|
||||||
mkConstructorsWithNameAndType view shape nameGen explicit | ||||||
|
||||||
end Data.Command |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please explain in the docstring what
Suggested change
|
||||||||||
:= 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) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The style is to have
Suggested change
|
||||||||||
|
||||||||||
/-- 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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function also has the wrong style, the (This is code you haven't touched in this PR, though, so feel free to ignore. Also, I don't remember if I wrote this, but it is very possible. I wrote a bunch of this stuff before I knew what the style should've been, so it's not necessarily good to just mimic the style of what's already there. Sorry 'bout that) |
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer the return type (presumably
CommandElabM Unit
?) to be explicit.Also, please write a docstring to explain what this does (and crucially, how it differs from
mkConstructors
).Finally, I like to limit lines to 100 chars wide, just like in Mathlib. This line feels like it might be too long (but I haven't checked, we really should set up a linter of some sort)