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

Add the notion of a DeepThunk and generation for this #35

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 7 additions & 5 deletions Qpf/Macro/Data.lean
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ def isPolynomial (view : DataView) (F: Term) : CommandElabM (Option Term) := do
def mkType (view : DataView) (base : Term × Term × Term) : CommandElabM Unit := do
trace[QPF] "mkType"
let uncurriedIdent ← addSuffixToDeclIdent view.declId "Uncurried"
let baseIdent ← addSuffixToDeclIdent view.declId "Base"
let baseIdExt ← addSuffixToDeclIdent view.declId "Base"
let baseIdent ← addSuffixToDeclIdent baseIdExt "Uncurried"
let baseFunctorIdent ← addSuffixToDeclIdent baseIdent "instMvFunctor"
let baseQPFIdent ← addSuffixToDeclIdent baseIdent "instQPF"

Expand All @@ -430,6 +431,9 @@ def mkType (view : DataView) (base : Term × Term × Term) : CommandElabM Unit :
abbrev $baseIdent:ident $view.deadBinders:bracketedBinder* : TypeFun $(quote <| arity + 1) :=
$base

abbrev $baseIdExt $view.deadBinders:bracketedBinder*
:= TypeFun.curried $baseApplied

instance $baseFunctorIdent:ident : MvFunctor ($baseApplied) := $functor
instance $baseQPFIdent:ident : MvQPF ($baseApplied) := $q

Expand Down Expand Up @@ -476,9 +480,7 @@ def elabData : CommandElab := fun stx => do
mkType view base
mkConstructors view shape

if let .Data := view.command then
try genRecursors view
catch e => trace[QPF] (← e.toMessageData.toString)

try genRecursors view shape
catch e => trace[QPF] (← e.toMessageData.toString)

end Data.Command
21 changes: 15 additions & 6 deletions Qpf/Macro/Data/Constructors.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Qpf.Macro.Data.Replace
import Qpf.Macro.Data.RecForm
import Qpf.Macro.Data.View

open Lean Meta Elab Elab.Command
Expand All @@ -17,9 +18,13 @@ partial def countConstructorArgs : Syntax → Nat
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.
The binders param allows for adding constant binders to an expression.
-/
def mkConstructorsWithNameAndType (view : DataView) (shape : Name)
(nameGen : CtorView → Name) (ty : Term) : CommandElabM Unit := do
def mkConstructorsWithNameAndType
(view : DataView) (shape : Name)
(nameGen : CtorView → Name) (argTy retTy : Term)
(binders : TSyntaxArray ``Parser.Term.bracketedBinder)
: 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 @@ -36,8 +41,12 @@ def mkConstructorsWithNameAndType (view : DataView) (shape : Name)
else
`(fun $args:ident* => $pointConstructor ($shapeCtor $args:ident*))

let type : Term := TSyntax.mk <|
(ctor.type?.map fun type => Replace.replaceAllStx view.getExpectedType ty type).getD ty
let recType := view.getExpectedType
let forms := RecursionForm.extract ctor recType

let x := forms.map $ RecursionForm.replaceRec view.getExpectedType argTy
let type ← RecursionForm.toType retTy x

let modifiers : Modifiers := {
isNoncomputable := view.modifiers.isNoncomputable
attrs := #[{
Expand All @@ -48,7 +57,7 @@ def mkConstructorsWithNameAndType (view : DataView) (shape : Name)

let cmd ← `(
$(quote modifiers):declModifiers
def $declId:ident : $type := $body:term
def $declId:ident $binders*: $type := $body:term
)

trace[QPF] "mkConstructor.cmd = {cmd}"
Expand All @@ -62,6 +71,6 @@ def mkConstructors (view : DataView) (shape : Name) : CommandElabM Unit := do
let explicit ← view.getExplicitExpectedType
let nameGen := (·.declName.replacePrefix (←getCurrNamespace) .anonymous)

mkConstructorsWithNameAndType view shape nameGen explicit
mkConstructorsWithNameAndType view shape nameGen explicit explicit #[]

end Data.Command
Loading
Loading