-
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 6 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 |
---|---|---|
|
@@ -924,6 +924,8 @@ object Parsers { | |
val tparams = typeParamClause(ParamOwner.TypeParam) | ||
if (in.token == TLARROW) | ||
atSpan(start, in.skipToken())(LambdaTypeTree(tparams, toplevelTyp())) | ||
else if (isIdent && in.name.toString == "->") | ||
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. Change syntax to ARROW, after #6558 is in. 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. Done. |
||
atSpan(start, in.skipToken())(PolyFunction(tparams, toplevelTyp())) | ||
else { accept(TLARROW); typ() } | ||
} | ||
else infixType() | ||
|
@@ -1323,6 +1325,11 @@ object Parsers { | |
atSpan(in.skipToken()) { Return(if (isExprIntro) expr() else EmptyTree, EmptyTree) } | ||
case FOR => | ||
forExpr() | ||
case LBRACKET => | ||
val start = in.offset | ||
val tparams = typeParamClause(ParamOwner.TypeParam) | ||
assert(isIdent && in.name.toString == "->", "Expected `->`") | ||
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 should be checked syntax, not an assert. 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. Done. |
||
atSpan(start, in.skipToken())(PolyFunction(tparams, expr())) | ||
case _ => | ||
if (isIdent(nme.inline) && !in.inModifierPosition() && in.lookaheadIn(canStartExpressionTokens)) { | ||
val start = in.skipToken() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,10 @@ class ElimErasedValueType extends MiniPhase with InfoTransformer { thisPhase => | |
val site = root.thisType | ||
val info1 = site.memberInfo(sym1) | ||
val info2 = site.memberInfo(sym2) | ||
if (!info1.matchesLoosely(info2)) | ||
if (!info1.matchesLoosely(info2) && | ||
!(sym1.name == nme.apply && | ||
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 did not understand why this additional condition was needed.To make it clearer, define it as an auxiliary def and add a doc comment? 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. Done. |
||
(sym1.owner.derivesFrom(defn.PolyFunctionClass) || | ||
sym2.owner.derivesFrom(defn.PolyFunctionClass)))) | ||
ctx.error(DoubleDefinition(sym1, sym2, root), root.sourcePos) | ||
} | ||
val earlyCtx = ctx.withPhase(ctx.elimRepeatedPhase.next) | ||
|
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" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -416,21 +416,28 @@ object Erasure { | |
*/ | ||
override def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = { | ||
|
||
val qual1 = typed(tree.qualifier, AnySelectionProto) | ||
|
||
def mapOwner(sym: Symbol): Symbol = { | ||
def recur(owner: Symbol): Symbol = | ||
if (defn.specialErasure.contains(owner)) { | ||
def recur(owner: Symbol): Symbol = { | ||
val owner = sym.maybeOwner | ||
if (!owner.exists) { | ||
// Hack for PolyFunction#apply | ||
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. Explain more, please 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. Done. Also simplified the logic in that area. |
||
qual1.tpe.widen.typeSymbol | ||
} else if (defn.specialErasure.contains(owner)) { | ||
assert(sym.isConstructor, s"${sym.showLocated}") | ||
defn.specialErasure(owner) | ||
} else if (defn.isSyntheticFunctionClass(owner)) | ||
defn.erasedFunctionClass(owner) | ||
else | ||
owner | ||
recur(sym.owner) | ||
} | ||
recur(sym.maybeOwner) | ||
} | ||
|
||
val origSym = tree.symbol | ||
val owner = mapOwner(origSym) | ||
val sym = if (owner eq origSym.owner) origSym else owner.info.decl(origSym.name).symbol | ||
val sym = if (owner eq origSym.maybeOwner) origSym else owner.info.decl(tree.name).symbol | ||
assert(sym.exists, origSym.showLocated) | ||
|
||
def select(qual: Tree, sym: Symbol): Tree = | ||
|
@@ -474,7 +481,7 @@ object Erasure { | |
} | ||
} | ||
|
||
checkNotErased(recur(typed(tree.qualifier, AnySelectionProto))) | ||
checkNotErased(recur(qual1)) | ||
} | ||
|
||
override def typedThis(tree: untpd.This)(implicit ctx: Context): Tree = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package scala | ||
|
||
/** Marker trait for polymorphic function types. | ||
* | ||
* This is the only trait that can be refined with a polymorphic method, | ||
* as long as that method is called `apply`, e.g.: | ||
* PolyFunction { def apply[T_1, ..., T_M](x_1: P_1, ..., x_N: P_N): R } | ||
* This type will be erased to FunctionN. | ||
*/ | ||
trait PolyFunction |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
object Test { | ||
def test1(f: [T <: AnyVal] -> List[T] => List[(T, T)]) = { | ||
f(List(1, 2, 3)) | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
val fun = [T <: AnyVal] -> (x: List[T]) => x.map(e => (e, e)) | ||
|
||
assert(test1(fun) == List((1, 1), (2, 2), (3, 3))) | ||
} | ||
} |
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.