-
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
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) type of the accumulator with fresh type variables. 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
6 changed files
with
155 additions
and
48 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,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)(_ + _) |