-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Proof of concept] Polymorphic function types #4672
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
Changes from all commits
951eaf4
0742175
e02b772
6df0adf
91134f7
bb8e5b5
956794e
860c96b
3c6c5a7
390071e
6db3ba4
f809ec4
251a7c4
4761564
6f8ed88
93d3c2a
6c9a5d3
112aa55
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package dotty.tools.dotc | ||
package transform | ||
|
||
import ast.{Trees, tpd} | ||
import core._, core.Decorators._ | ||
import MegaPhase._, Phases.Phase | ||
import Types._, Contexts._, Constants._, Names._, NameOps._, Flags._, DenotTransformers._ | ||
import SymDenotations._, Symbols._, StdNames._, Annotations._, Trees._, Scopes._, Denotations._ | ||
import TypeErasure.ErasedValueType, ValueClasses._ | ||
|
||
/** This phase rewrite PolyFunction subclasses to FunctionN subclasses | ||
* | ||
* class Foo extends PolyFunction { | ||
* def apply(x_1: P_1, ..., x_N: P_N): R = rhs | ||
* } | ||
* becomes: | ||
* class Foo extends FunctionN { | ||
* def apply(x_1: P_1, ..., x_N: P_N): R = rhs | ||
* } | ||
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. I wonder if we should allow writing such a class 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. Note that it can be really useful being able to extend/implement a polymorphic function type; it's one of the use cases I mention in https://github.com/lampepfl/dotty/issues/4670#issuecomment-397819801 – making polymorphic EDIT – typo: "type class" -> "case class" 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. Ah right, tho 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. @Blaisorblade AFAIK eta expansion never inserts |
||
*/ | ||
class ElimPolyFunction extends MiniPhase with DenotTransformer { | ||
|
||
import tpd._ | ||
|
||
override def phaseName: String = ElimPolyFunction.name | ||
|
||
override def runsAfter = Set(Erasure.name) | ||
|
||
override def changesParents: Boolean = true // Replaces PolyFunction by FunctionN | ||
|
||
override def transform(ref: SingleDenotation)(implicit ctx: Context) = ref match { | ||
case ref: ClassDenotation if ref.symbol != defn.PolyFunctionClass && ref.derivesFrom(defn.PolyFunctionClass) => | ||
val cinfo = ref.classInfo | ||
val newParent = functionTypeOfPoly(cinfo) | ||
val newParents = cinfo.classParents.map(parent => | ||
if (parent.typeSymbol == defn.PolyFunctionClass) | ||
newParent | ||
else | ||
parent | ||
) | ||
ref.copySymDenotation(info = cinfo.derivedClassInfo(classParents = newParents)) | ||
case _ => | ||
ref | ||
} | ||
|
||
def functionTypeOfPoly(cinfo: ClassInfo)(implicit ctx: Context): Type = { | ||
val applyMeth = cinfo.decls.lookup(nme.apply).info | ||
val arity = applyMeth.paramNamess.head.length | ||
defn.FunctionType(arity) | ||
} | ||
|
||
override def transformTemplate(tree: Template)(implicit ctx: Context): Tree = { | ||
val newParents = tree.parents.mapconserve(parent => | ||
if (parent.tpe.typeSymbol == defn.PolyFunctionClass) { | ||
val cinfo = tree.symbol.owner.asClass.classInfo | ||
tpd.TypeTree(functionTypeOfPoly(cinfo)) | ||
} | ||
else | ||
parent | ||
) | ||
cpy.Template(tree)(parents = newParents) | ||
} | ||
} | ||
|
||
object ElimPolyFunction { | ||
val name = "elimPolyFunction" | ||
} | ||
|
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.
This should follow the principle used elsewhere: The TypeRef is computed in the lazy val and the context-dependent symbol follows. This is to make sure that the system keeps functioning if Definition classes are edited and recompiled. If you deviate from this, you create confusion for others.
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.
Looking nearby, the pattern seems to be the same as here: the symbol is defined as a lazy val and the typeref as a def. Can you point me to an example which is arranged the way you want it?
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.
E.g.
But I meant to go over
Definitions
anyway, trying to avoid the duplication and make it safe by design. The problem with the lazy val pattern as you wrote it is that it would not work in interactive mode ifPolyFunction
was edited. Thenthe system would hang on to the first version computed instead of the edited ones. I agree that's a rather esoteric use case. So we can leave it for now.