Skip to content

A variable should be best instantiation of pattern type (SI-6680 redux) #1870

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

Closed
S11001001 opened this issue Jan 2, 2017 · 2 comments
Closed

Comments

@S11001001
Copy link

Given

trait Stream[+A] 
case class Unfold[S,+A](s: S, f: S => Option[(A,S)]) extends Stream[A] 

You get this warning, and an associated unsound inferred type, as in SI-6680 “Unsoundness bug in pattern matcher when pattern reveals existentials”:

scala> def unbox[A](s: Stream[A]) = s match {case Unfold(s, f) => (s, f)} 
-- Warning: <console> ----------------------------------------------------------
7 |def unbox[A](s: Stream[A]) = s match {case Unfold(s, f) => (s, f)}
  |                                           ^^^^^^^^^^^^
  |           There is no best instantiation of pattern type Unfold[Any^, A^]
  |           that makes it a subtype of selector type Stream[A].
  |           Non-variant type variable S cannot be uniquely instantiated.
  |           (This would be an error under strict mode)
def unbox[A](s: Stream[A]): [A] => (s: Stream[A])(Any, Any => Option[(A, Any)])

But there is a ‘best’ choice, which is moreover sound, which is to introduce a fresh type variable, as you can write explicitly with type pattern variables.

scala> def unbox[A](s: Stream[A]) = s match {
  case u: Unfold[ts, A] =>
    u.s: ts  // the name chosen in this block
    (u.s, u.f)
} 
def unbox[A](s: Stream[A]): [A] => (s: Stream[A])(Any, _ => Option[(A, Any)])

This is based on 39c27b6.

/cc @pchiusano

@smarter
Copy link
Member

smarter commented Jan 2, 2017

I agree. I wrote down some thoughts on GADTs at https://gist.github.com/smarter/2e1c564c83bae58c65b4f3f041bfb15f but haven't had time to actually fix things yet. There was also some related discussions recently at https://gist.github.com/AleksanderBG/34ee1d9a9de3925800901f6ce7cb5c93

Before fixing this it would probably be wise to take care of #1754

@odersky
Copy link
Contributor

odersky commented Jan 24, 2018

This is fixed by #3889

@odersky odersky closed this as completed Jan 24, 2018
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants