-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve type inference for functions like fold (#18780)
When calling a fold with an accumulator like `Nil` or `List()` one used to have add an explicit type ascription. This is now no longer necessary. When instantiating type variables that occur invariantly in the expected type of a lambda, we now replace covariant occurrences of `Nothing` in the (possibly widened) instance type of the type variable with fresh type variables. In the case of fold, the accumulator determines the instance type of a type variable that appears both in the parameter list and in the result type of the closure, which makes it invariant. So the accumulator type is improved in the way described above. The idea is that a fresh type variable in such places is always better than Nothing. For module values such as `Nil` we widen to `List[<fresh var>]`. This does possibly cause a new type error if the fold really wanted a `Nil` instance. But that case seems very rare, so it looks like a good bet in general to do the widening.
- Loading branch information
Showing
8 changed files
with
177 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- [E007] Type Mismatch Error: tests/neg/foldinf-ill-kinded.scala:9:16 ------------------------------------------------- | ||
9 | ys.combine(x) // error | ||
| ^^^^^^^^^^^^^ | ||
| Found: Foo[List] | ||
| Required: Foo[Nothing] | ||
| | ||
| longer explanation available when compiling with `-explain` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Foo[+T[_]]: | ||
def combine[T1[x] >: T[x]](x: T1[Int]): Foo[T1] = new Foo | ||
object Foo: | ||
def empty: Foo[Nothing] = new Foo | ||
|
||
object X: | ||
def test(xs: List[List[Int]]): Unit = | ||
xs.foldLeft(Foo.empty)((ys, x) => | ||
ys.combine(x) // error | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
object Test: | ||
extension [A](xs: List[A]) | ||
def foldl[B](acc: B)(f: (A, B) => B): B = ??? | ||
|
||
val xs = List(1, 2, 3) | ||
|
||
val _ = xs.foldl(List())((y, ys) => y :: ys) | ||
|
||
val _ = xs.foldl(Nil)((y, ys) => y :: ys) | ||
|
||
def partition[a](xs: List[a], pred: a => Boolean): Tuple2[List[a], List[a]] = { | ||
xs.foldRight/*[Tuple2[List[a], List[a]]]*/((List(), List())) { | ||
(x, p) => if (pred (x)) (x :: p._1, p._2) else (p._1, x :: p._2) | ||
} | ||
} | ||
|
||
def snoc[A](xs: List[A], x: A) = x :: xs | ||
|
||
def reverse[A](xs: List[A]) = | ||
xs.foldLeft(Nil)(snoc) | ||
|
||
def reverse2[A](xs: List[A]) = | ||
xs.foldLeft(List())(snoc) | ||
|
||
val ys: Seq[Int] = xs | ||
ys.foldLeft(Seq())((ys, y) => y +: ys) | ||
ys.foldLeft(Nil)((ys, y) => y +: ys) | ||
|
||
def dup[A](xs: List[A]) = | ||
xs.foldRight(Nil)((x, xs) => x :: x :: xs) | ||
|
||
def toSet[A](xs: Seq[A]) = | ||
xs.foldLeft(Set.empty)(_ + _) |