Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
5866d0d
Allow refinements of new types
odersky May 24, 2016
d30f441
Allow general recursion in refined types.
odersky Jun 29, 2016
5d03186
Better printing of skolems
odersky May 26, 2016
cdb4a1c
New type lambda scheme for hk types
odersky Jun 29, 2016
850dc6f
Introduce recursive types
odersky Jun 29, 2016
af43d32
Abstract type parameters out from type symbols
odersky Jun 29, 2016
aa7f66d
Disable checkInst in RecType
odersky Jun 29, 2016
08a0ea6
Add printing of type lambda trees
odersky Jun 29, 2016
178e90e
Remove special case in parser
odersky May 27, 2016
4bf43f8
Turn on new hk scheme
odersky Jun 29, 2016
5daae27
Multiple fixes
odersky Jun 29, 2016
e56bd1f
Beta-reduce when simplifying
odersky Jun 2, 2016
e61b80a
Refinement of cycle avoidance
odersky Jun 2, 2016
a7d61c0
Make etaExpandIfHk work for non-symbol type params
odersky Jun 2, 2016
d0f82a5
Don't map info of SkolemTypes in type maps
odersky Jun 3, 2016
c136af1
Fixes to BetaReduce and asMemberOf; add a second betaReduce
odersky Jun 29, 2016
ae1f248
Normalize RecTypes on creation to avoid cycles.
odersky Jun 4, 2016
939d9da
Add a second betaReduce
odersky Jun 4, 2016
c35f817
Adapt widenForMatchSelector to new HK scheme
odersky Jun 5, 2016
e36a36a
Fix typeParams for abstract types under completion
odersky Jun 5, 2016
6a7e466
Avoid accidental creation of hk types
odersky Jun 29, 2016
8e84fb0
Eta-expand unapplied types that have type parameters
odersky Jun 29, 2016
68e73e8
Restrict betaReduce to hk applications
odersky Jun 29, 2016
463e99a
Optionally, check kinds match for & and |
odersky Jun 7, 2016
0a5f839
Avoid infinite recursion when comparing recursive types.
odersky Jun 29, 2016
31af865
Fix condition for lambda abstracting in Namer
odersky Jun 29, 2016
5041e93
Make TypeAccumulators follow LazyRefs
odersky Jun 7, 2016
f1bf78b
Avoid creating dependent function types for closures
odersky Jun 8, 2016
830b724
Change tests
odersky Jun 29, 2016
6414f3b
Deal with nested findMember calls over the same RecType
odersky Jun 9, 2016
e749d83
Various tweaks
odersky Jun 29, 2016
d1f809f
Remove old hk scheme
odersky Jun 29, 2016
60d81f8
Start new, direct HK scheme
odersky Jun 29, 2016
3490e01
Make Constraint#bounds work for aliases
odersky Jun 14, 2016
f6efd99
Fix appliedTo and typeParams, and higher kinded subtyping tests
odersky Jun 29, 2016
97e84e6
Fix printing of type lambda trees and types
odersky Jun 18, 2016
73dd039
Various hk related fixes in types
odersky Jun 29, 2016
02ce995
Refactoring of PolyType and TypeLambda
odersky Jun 29, 2016
c28dd1b
Tweaks to appliedTo
odersky Jun 29, 2016
c1e27a0
Change underlying of HKApply
odersky Jun 29, 2016
98b466c
Ensure wildApprox maintains kinds
odersky Jun 20, 2016
0965e1a
Handle TypeLambdas in findMember
odersky Jun 20, 2016
65c26ba
Avoid cyclic errors when reading nsc.Global
odersky Jun 21, 2016
1e48758
Refactor handling of unpickled type params
odersky Jun 29, 2016
960ea75
Allow for HK types in widenForMatchSelector
odersky Jun 22, 2016
e0db04d
Drop bounds checking for type lambdas
odersky Jun 22, 2016
de5d8fe
Skip typeBounds when computing upperBound
odersky Jun 22, 2016
8805dd4
When comparing types revert eta-expansion as needed
odersky Jun 26, 2016
646bf97
Beta-reduce with wildcard arguments if this is safe.
odersky Jun 29, 2016
09f7ab1
Handle findMember for unreduced hk types.
odersky Jun 27, 2016
9a90e81
Handle WildcardType in liftToClasses
odersky Jun 29, 2016
dca1052
Don't make * types higher-kinded in avoid
odersky Jun 29, 2016
4093e13
Don't dealias when applying types
odersky Jun 29, 2016
bb59931
Fixes to hk comparisons
odersky Jun 29, 2016
dc5be65
Special case for TypeRefs in upperbound
odersky Jun 28, 2016
31ecad5
Drop assertion in TypeMap
odersky Jun 29, 2016
9d9965c
Fix parameter unpickling
odersky Jun 28, 2016
a23c1a4
Fixes for printing under -Ytest-pickler
odersky Jun 29, 2016
6bd7ba9
Remove refinement encoding of hk types
odersky Jun 29, 2016
ae360e9
Handle signatures over uninstantiated type variables
odersky Jun 29, 2016
6abde38
Get rid of BindingKind
odersky Jun 29, 2016
68abba1
Simplify appliedTo
odersky Jun 29, 2016
30e15ab
Cleanup of Signature matching
odersky Jun 30, 2016
b6a8bc7
Revert complications in asMemberOf
odersky Jun 30, 2016
32c0135
Refactorings of GenericTypes and underlying/upperbound
odersky Jun 30, 2016
34a068b
Drop Config.checkKinds
odersky Jun 30, 2016
beff8f8
Avoid orphan parameters in Constraint#replace
odersky Jul 2, 2016
eebb4b0
Fix bug in printing untyped New nodes.
odersky Jul 3, 2016
fd62c7b
Disallow higher-kinded types that simulate general existential types
odersky Jul 7, 2016
4693a78
Less eager removal of type parameters from constraint
odersky Jul 7, 2016
2ddb849
Unify aliases when taking a lub.
odersky Jul 4, 2016
c541ef9
Do the right thing for NoDenotation.mapInfo
odersky Jul 7, 2016
78b2672
Add a case where caching was missing.
odersky Jul 5, 2016
223705d
Refactor Typevar instantiation
odersky Jul 7, 2016
5e90215
Drop compareAliasedRefined
odersky Jul 8, 2016
540b38c
More tests
odersky Jul 9, 2016
a200695
Fix SI-2712
odersky Jul 11, 2016
6d7bc49
Address reviewers comments
odersky Jul 11, 2016
055726e
ExtractAPI: Do not miss value parameters of PolyTypes
smarter Jul 12, 2016
a6a142e
ExtractAPI: Add support for TypeLambdas
smarter Jul 12, 2016
1792c9e
ExtractAPI: Add support for RecType
smarter Jul 12, 2016
cdebd91
Allow definition of new types in refinements
odersky Jul 11, 2016
84a1a7a
Avoid dealiasing on type application
odersky Jul 12, 2016
c7f3b45
Drop restriction to 2nd order hk types
odersky Jul 12, 2016
7df0fa5
Enable test for fixed #1181: Typer should preserve aliases in TypeTrees
smarter Jul 12, 2016
1443fd4
Optimize hk comparisons
odersky Jul 12, 2016
f50cb20
Make rewritings of hk applications configurable
odersky Jul 13, 2016
0ff5354
Simplify variance computations
odersky Jul 13, 2016
18b3080
Allow hk parameters in lower bounds
odersky Jul 13, 2016
82fc27f
Fix bounds checking of hk applied typed
odersky Jul 14, 2016
a737b47
Add test case for hk bounds checking
odersky Jul 14, 2016
894c9fb
Bounds of type lambdas compare contravariantly
odersky Jul 15, 2016
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
Prev Previous commit
Next Next commit
Introduce recursive types
Map self-references in refinements to recursive types. This
commit does this for refinement types appearing in source.
We still have to do it for unpickled refinements.

Test apply-equiv got moved to pending because it simulates
the old higher-kinded type encoding in source, which relies
on the old representation in terms of self-referential refinement
types. The plan is not to adapt this encoding to the new
representation, but to replace it with a different encoding
that makes critical use of the added power of recursive types.

Use recursive types also when unpickling from Scala 2.x.

Add mapInfo method to Denotations.
  • Loading branch information
odersky committed Jul 11, 2016
commit 850dc6f2fb3b6228f2586ce0790621e80f664afe
10 changes: 9 additions & 1 deletion src/dotty/tools/dotc/core/Denotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ object Denotations {
/** Is this denotation different from NoDenotation or an ErrorDenotation? */
def exists: Boolean = true

/** A denotation with the info of this denotation transformed using `f` */
def mapInfo(f: Type => Type)(implicit ctx: Context): Denotation

/** If this denotation does not exist, fallback to alternative */
final def orElse(that: => Denotation) = if (this.exists) this else that

Expand Down Expand Up @@ -242,7 +245,7 @@ object Denotations {
}
else if (exists && !qualifies(symbol)) NoDenotation
else asSingleDenotation
}
}

/** Form a denotation by conjoining with denotation `that`.
*
Expand Down Expand Up @@ -456,6 +459,8 @@ object Denotations {
else if (!d2.exists) d1
else derivedMultiDenotation(d1, d2)
}
def mapInfo(f: Type => Type)(implicit ctx: Context): Denotation =
derivedMultiDenotation(denot1.mapInfo(f), denot2.mapInfo(f))
def derivedMultiDenotation(d1: Denotation, d2: Denotation) =
if ((d1 eq denot1) && (d2 eq denot2)) this else MultiDenotation(d1, d2)
override def toString = alternatives.mkString(" <and> ")
Expand Down Expand Up @@ -488,6 +493,9 @@ object Denotations {
if ((symbol eq this.symbol) && (info eq this.info)) this
else newLikeThis(symbol, info)

def mapInfo(f: Type => Type)(implicit ctx: Context): SingleDenotation =
derivedSingleDenotation(symbol, f(info))

def orElse(that: => SingleDenotation) = if (this.exists) this else that

def altsWith(p: Symbol => Boolean): List[SingleDenotation] =
Expand Down
22 changes: 22 additions & 0 deletions src/dotty/tools/dotc/core/Substituters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@ trait Substituters { this: Context =>
.mapOver(tp)
}

final def substRecThis(tp: Type, from: Type, to: Type, theMap: SubstRecThisMap): Type =
tp match {
case tp @ RecThis(binder) =>
if (binder eq from) to else tp
case tp: NamedType =>
if (tp.currentSymbol.isStatic) tp
else tp.derivedSelect(substRecThis(tp.prefix, from, to, theMap))
case _: ThisType | _: BoundType | NoPrefix =>
tp
case tp: RefinedType =>
tp.derivedRefinedType(substRecThis(tp.parent, from, to, theMap), tp.refinedName, substRecThis(tp.refinedInfo, from, to, theMap))
case tp: TypeAlias =>
tp.derivedTypeAlias(substRecThis(tp.alias, from, to, theMap))
case _ =>
(if (theMap != null) theMap else new SubstRecThisMap(from, to))
.mapOver(tp)
}

final def substParam(tp: Type, from: ParamType, to: Type, theMap: SubstParamMap): Type =
tp match {
case tp: BoundType =>
Expand Down Expand Up @@ -270,6 +288,10 @@ trait Substituters { this: Context =>
def apply(tp: Type): Type = substRefinedThis(tp, from, to, this)
}

final class SubstRecThisMap(from: Type, to: Type) extends DeepTypeMap {
def apply(tp: Type): Type = substRecThis(tp, from, to, this)
}

final class SubstParamMap(from: ParamType, to: Type) extends DeepTypeMap {
def apply(tp: Type) = substParam(tp, from, to, this)
}
Expand Down
3 changes: 2 additions & 1 deletion src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1121,10 +1121,11 @@ object SymDenotations {

def debugString = toString + "#" + symbol.id // !!! DEBUG

def hasSkolems(tp: Type): Boolean = tp match {
def hasSkolems(tp: Type): Boolean = tp match {
case tp: SkolemType => true
case tp: NamedType => hasSkolems(tp.prefix)
case tp: RefinedType => hasSkolems(tp.parent) || hasSkolems(tp.refinedInfo)
case tp: RecType => hasSkolems(tp.parent)
case tp: PolyType => tp.paramBounds.exists(hasSkolems) || hasSkolems(tp.resType)
case tp: MethodType => tp.paramTypes.exists(hasSkolems) || hasSkolems(tp.resType)
case tp: ExprType => hasSkolems(tp.resType)
Expand Down
40 changes: 32 additions & 8 deletions src/dotty/tools/dotc/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@ object TypeApplicationsNewHK {
import TypeApplications._

object TypeLambda {
def apply(argBindingFns: List[RefinedType => TypeBounds],
bodyFn: RefinedType => Type)(implicit ctx: Context): Type = {
def apply(argBindingFns: List[RecType => TypeBounds],
bodyFn: RecType => Type)(implicit ctx: Context): Type = {
val argNames = argBindingFns.indices.toList.map(tpnme.hkArg)
RefinedType.recursive(bodyFn, argNames, argBindingFns)
var idx = 0
RecType.closeOver(rt =>
(bodyFn(rt) /: argBindingFns) { (parent, argBindingFn) =>
val res = RefinedType(parent, tpnme.hkArg(idx), argBindingFn(rt))
idx += 1
res
})
}

def unapply(tp: Type)(implicit ctx: Context): Option[(List[TypeBounds], Type)] = {
def decompose(t: Type, acc: List[TypeBounds]): (List[TypeBounds], Type) = t match {
case t @ RefinedType(p, rname, rinfo: TypeBounds)
if rname.isHkArgName && rinfo.isBinding =>
decompose(p, rinfo.bounds :: acc)
case t: RecType =>
decompose(t.parent, acc)
case _ =>
(acc, t)
}
Expand Down Expand Up @@ -78,13 +86,13 @@ object TypeApplications {
* [v1 X1: B1, ..., vn Xn: Bn] -> T
* ==>
* ([X_i := this.$hk_i] T) { type v_i $hk_i: (new)B_i }
*
*
* [X] -> List[X]
*
*
* List { type List$A = this.$hk_0 } { type $hk_0 }
*
*
* [X] -> X
*
*
* mu(this) this.$hk_0 & { type $hk_0 }
*/
object TypeLambda {
Expand Down Expand Up @@ -212,6 +220,10 @@ object TypeApplications {
def argRefs(rt: RefinedType, n: Int)(implicit ctx: Context) =
List.range(0, n).map(i => RefinedThis(rt).select(tpnme.hkArg(i)))

/** The references `<rt>.this.$hk0, ..., <rt>.this.$hk<n-1>`. */
def argRefs(rt: RecType, n: Int)(implicit ctx: Context) =
List.range(0, n).map(i => RecThis(rt).select(tpnme.hkArg(i)))

/** Merge `tp1` and `tp2` under a common lambda, combining them with `op`.
* @param tparams1 The type parameters of `tp1`
* @param tparams2 The type parameters of `tp2`
Expand Down Expand Up @@ -400,7 +412,7 @@ class TypeApplications(val self: Type) extends AnyVal {
* but without forcing anything.
*/
def classNotLambda(implicit ctx: Context): Boolean = self.stripTypeVar match {
case self: RefinedType =>
case self: RefinedOrRecType =>
self.parent.classNotLambda
case self: TypeRef =>
self.denot.exists && {
Expand Down Expand Up @@ -428,6 +440,14 @@ class TypeApplications(val self: Type) extends AnyVal {
new ctx.SafeSubstMap(tparams, argRefs(rt, tparams.length))
.apply(self).asInstanceOf[T]

/** Replace references to type parameters with references to hk arguments `this.$hk_i`
* Care is needed not to cause cyclic reference errors, hence `SafeSubstMap`.
*/
def recursify[T <: Type](tparams: List[Symbol])(implicit ctx: Context): RecType => T =
(rt: RecType) =>
new ctx.SafeSubstMap(tparams, argRefs(rt, tparams.length))
.apply(self).asInstanceOf[T]

/** Lambda abstract `self` with given type parameters. Examples:
*
* type T[X] = U becomes type T = [X] -> U
Expand Down Expand Up @@ -546,6 +566,8 @@ class TypeApplications(val self: Type) extends AnyVal {
arg.prefix.select(boundLambda)
case arg: RefinedType =>
arg.derivedRefinedType(adaptArg(arg.parent), arg.refinedName, arg.refinedInfo)
case arg: RecType =>
arg.derivedRecType(adaptArg(arg.parent))
case arg @ TypeAlias(alias) =>
arg.derivedTypeAlias(adaptArg(alias))
case arg @ TypeBounds(lo, hi) =>
Expand Down Expand Up @@ -814,6 +836,8 @@ class TypeApplications(val self: Type) extends AnyVal {
}
case tp: RefinedType =>
recur(tp.refinedInfo) || recur(tp.parent)
case tp: RecType =>
recur(tp.parent)
case tp: TypeBounds =>
recur(tp.lo) || recur(tp.hi)
case tp: AnnotatedType =>
Expand Down
31 changes: 29 additions & 2 deletions src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
isSubRefinements(tp1w.asInstanceOf[RefinedType], tp2, skipped2)
}
compareRefined
case tp2: RecType =>
val tp1stable = ensureStableSingleton(tp1)
isSubType(fixRecs(tp1stable, tp1stable.widenExpr), tp2.substRecThis(tp2, tp1stable))
case OrType(tp21, tp22) =>
// Rewrite T1 <: (T211 & T212) | T22 to T1 <: (T211 | T22) and T1 <: (T212 | T22)
// and analogously for T1 <: T21 | (T221 & T222)
Expand Down Expand Up @@ -465,7 +468,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
case _ =>
def isNullable(tp: Type): Boolean = tp.dealias match {
case tp: TypeRef => tp.symbol.isNullableClass
case tp: RefinedType => isNullable(tp.parent)
case tp: RefinedOrRecType => isNullable(tp.parent)
case AndType(tp1, tp2) => isNullable(tp1) && isNullable(tp2)
case OrType(tp1, tp2) => isNullable(tp1) || isNullable(tp2)
case _ => false
Expand Down Expand Up @@ -494,6 +497,8 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
isNewSubType(tp1.parent, tp2) ||
compareHkLambda(tp1, tp2, inOrder = true) ||
compareAliasedRefined(tp1, tp2, inOrder = true)
case tp1: RecType =>
isNewSubType(tp1.parent, tp2)
case AndType(tp11, tp12) =>
// Rewrite (T111 | T112) & T12 <: T2 to (T111 & T12) <: T2 and (T112 | T12) <: T2
// and analogously for T11 & (T121 | T122) & T12 <: T2
Expand Down Expand Up @@ -642,6 +647,25 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
}
}

/** Replace any top-level recursive type `{ z => T }` in `tp` with
* `[z := anchor]T`.
*/
private def fixRecs(anchor: SingletonType, tp: Type): Type = {
def fix(tp: Type): Type = tp.stripTypeVar match {
case tp: RecType => fix(tp.parent).substRecThis(tp, anchor)
case tp @ RefinedType(parent, rname, rinfo) => tp.derivedRefinedType(fix(parent), rname, rinfo)
case tp: PolyParam => fixOrElse(bounds(tp).hi, tp)
case tp: TypeProxy => fixOrElse(tp.underlying, tp)
case tp: AndOrType => tp.derivedAndOrType(fix(tp.tp1), fix(tp.tp2))
case tp => tp
}
def fixOrElse(tp: Type, fallback: Type) = {
val tp1 = fix(tp)
if (tp1 ne tp) tp1 else fallback
}
fix(tp)
}

/** The symbol referred to in the refinement of `rt` */
private def refinedSymbol(rt: RefinedType) = rt.parent.member(rt.refinedName).symbol

Expand Down Expand Up @@ -772,7 +796,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {

/** A type has been covered previously in subtype checking if it
* is some combination of TypeRefs that point to classes, where the
* combiners are RefinedTypes, AndTypes or AnnotatedTypes.
* combiners are RefinedTypes, RecTypes, AndTypes or AnnotatedTypes.
* One exception: Refinements referring to basetype args are never considered
* to be already covered. This is necessary because such refined types might
* still need to be compared with a compareAliasRefined.
Expand All @@ -781,6 +805,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
case tp: TypeRef => tp.symbol.isClass && tp.symbol != NothingClass && tp.symbol != NullClass
case tp: ProtoType => false
case tp: RefinedType => isCovered(tp.parent) && !refinedSymbol(tp).is(BaseTypeArg)
case tp: RecType => isCovered(tp.parent)
case tp: AnnotatedType => isCovered(tp.underlying)
case AndType(tp1, tp2) => isCovered(tp1) && isCovered(tp2)
case _ => false
Expand Down Expand Up @@ -1118,6 +1143,8 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
case _ =>
NoType
}
case tp1: RecType =>
tp1.rebind(distributeAnd(tp1.parent, tp2))
case tp1: TypeBounds =>
tp2 match {
case tp2: TypeBounds => tp1 & tp2
Expand Down
23 changes: 22 additions & 1 deletion src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,16 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
}
case _ =>
}

tp1 match {
case tp1: RecType =>
tp1.rebind(approximateOr(tp1.parent, tp2))
case tp1: TypeProxy if !isClassRef(tp1) =>
approximateUnion(next(tp1) | tp2)
case _ =>
tp2 match {
case tp2: RecType =>
tp2.rebind(approximateOr(tp1, tp2.parent))
case tp2: TypeProxy if !isClassRef(tp2) =>
approximateUnion(tp1 | next(tp2))
case _ =>
Expand All @@ -252,16 +257,32 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
if (ctx.featureEnabled(defn.LanguageModuleClass, nme.keepUnions)) tp
else tp match {
case tp: OrType =>
approximateOr(tp.tp1, tp.tp2)
approximateOr(tp.tp1, tp.tp2) // Maybe refactor using liftToRec?
case tp @ AndType(tp1, tp2) =>
tp derived_& (approximateUnion(tp1), approximateUnion(tp2))
case tp: RefinedType =>
tp.derivedRefinedType(approximateUnion(tp.parent), tp.refinedName, tp.refinedInfo)
case tp: RecType =>
tp.rebind(approximateUnion(tp.parent))
case _ =>
tp
}
}

/** Not currently needed:
*
def liftToRec(f: (Type, Type) => Type)(tp1: Type, tp2: Type)(implicit ctx: Context) = {
def f2(tp1: Type, tp2: Type): Type = tp2 match {
case tp2: RecType => tp2.rebind(f(tp1, tp2.parent))
case _ => f(tp1, tp2)
}
tp1 match {
case tp1: RecType => tp1.rebind(f2(tp1.parent, tp2))
case _ => f2(tp1, tp2)
}
}
*/

private def enterArgBinding(formal: Symbol, info: Type, cls: ClassSymbol, decls: Scope) = {
val lazyInfo = new LazyType { // needed so we do not force `formal`.
def complete(denot: SymDenotation)(implicit ctx: Context): Unit = {
Expand Down
Loading