From 939d9da26ee5992c17cd1fae0a501ed66a49fb95 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 4 Jun 2016 18:21:11 +0200 Subject: Add a second betaReduce The new one only reduces straight applications of type lambdas with definite arguments. It is called very early on appliedTo, and derivedRefinedType. The old one, now renamed to normalizeHkApply also handles wildcard arguments and can garbage collect general unneeded hk-refinements. It is called later, at various places. TODO: See what functionality of normalizeHkApply should go into betaReduce instead. Maybe we can even drop normalizeHkApply? However: need to be careful to maintain aliases for hk type inference. --- src/dotty/tools/dotc/core/Types.scala | 38 +++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/dotty/tools/dotc/core/Types.scala') diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index 403b49da6..2fa4f94c1 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -2131,9 +2131,43 @@ object Types { this } - def derivedRefinedType(parent: Type, refinedName: Name, refinedInfo: Type)(implicit ctx: Context): RefinedType = + def betaReduce(implicit ctx: Context): Type = refinedInfo match { + case TypeAlias(alias) => + def instantiate(rt: RecType) = new TypeMap { + def apply(t: Type) = t match { + case TypeRef(RecThis(`rt`), `refinedName`) => alias + case tp: TypeRef => + val pre1 = apply(tp.prefix) + if (pre1 ne tp.prefix) tp.newLikeThis(pre1) else tp + case _ => mapOver(t) + } + } + def substAlias(tp: Type): Type = tp.safeDealias match { + case tp @ RefinedType(p, rname, rinfo) if tp.isTypeParam => + if (rname == refinedName) p // check bounds? + else tp.derivedRefinedType(substAlias(p), rname, rinfo) + case tp: RecType => + val p1 = substAlias(tp.parent) + if (p1 ne tp.parent) tp.rebind(instantiate(tp)(p1)) + else tp + case _ => + tp + } + val reduced = substAlias(parent) + if (reduced ne parent) { + hk.println(i"REDUCE $this ----> ${reduced}") + reduced + } + else this + case _ => + this + } + + def derivedRefinedType(parent: Type, refinedName: Name, refinedInfo: Type)(implicit ctx: Context): Type = if ((parent eq this.parent) && (refinedName eq this.refinedName) && (refinedInfo eq this.refinedInfo)) this - else RefinedType(parent, refinedName, rt => refinedInfo.substRefinedThis(this, RefinedThis(rt))) + else + RefinedType(parent, refinedName, rt => refinedInfo.substRefinedThis(this, RefinedThis(rt))) + .betaReduce /** Add this refinement to `parent`, provided If `refinedName` is a member of `parent`. */ def wrapIfMember(parent: Type)(implicit ctx: Context): Type = -- cgit v1.2.3