[scala] Unsoundness in overriding methods with higher-order type parameters?

View: New views
7 Messages — Rating Filter:   Alert me  

[scala] Unsoundness in overriding methods with higher-order type parameters?

by nikov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Consider the following code:

//////////////////////////////////////////////////
trait A {
  def f[T[_]](x : T[Int]) : T[Any]
}

class B extends A {
  def f[T[+_]](x : T[Int]) : T[Any] = x
}

class P[Y](var y : Y)
//////////////////////////////////////////////////

It accepted by the Scala 2.7.5 compiler without any errors. But is
seems unsound, because then we can write (new B:A).f[P](new
P[Int](1)), and nonvariant P[Int] is coerced to P[Any].

Thanks,
Vladimir

Re: [scala] Unsoundness in overriding methods with higher-order type parameters?

by James Iry-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Which version of Scala are you running?  In both 2.7.5 and 2.8.0.something I get

<console>:7: error: kinds of the type arguments (P) do not conform to the expected kinds of the type parameters (type T).
P's type parameters do not match type T's expected parameters: type Y is invariant, but type _ is declared covariant
       val palias = new B().f[P](new P[Int](1))

On Tue, Jun 16, 2009 at 6:27 AM, Vladimir Reshetnikov <v.reshetnikov@...> wrote:
Hi,

Consider the following code:

//////////////////////////////////////////////////
trait A {
 def f[T[_]](x : T[Int]) : T[Any]
}

class B extends A {
 def f[T[+_]](x : T[Int]) : T[Any] = x
}

class P[Y](var y : Y)
//////////////////////////////////////////////////

It accepted by the Scala 2.7.5 compiler without any errors. But is
seems unsound, because then we can write (new B:A).f[P](new
P[Int](1)), and nonvariant P[Int] is coerced to P[Any].

Thanks,
Vladimir


Re: [scala] Unsoundness in overriding methods with higher-order type parameters?

by James Iry-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Oops, missed the cast to an A.  Indeed that's a problem

trait A {
  def f[T[_]](x : T[Int]) : T[Any]
}

class B extends A {
  def f[T[+_]](x : T[Int]) : T[Any] = x
}

class P[Y](var y : Y)


val p = new P(1)

val palias = (new B():A).f[P](p)

palias.y = "hello"

scala> p.y
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer



On Tue, Jun 16, 2009 at 6:51 AM, James Iry <jamesiry@...> wrote:
Which version of Scala are you running?  In both 2.7.5 and 2.8.0.something I get

<console>:7: error: kinds of the type arguments (P) do not conform to the expected kinds of the type parameters (type T).
P's type parameters do not match type T's expected parameters: type Y is invariant, but type _ is declared covariant
       val palias = new B().f[P](new P[Int](1))


On Tue, Jun 16, 2009 at 6:27 AM, Vladimir Reshetnikov <v.reshetnikov@...> wrote:
Hi,

Consider the following code:

//////////////////////////////////////////////////
trait A {
 def f[T[_]](x : T[Int]) : T[Any]
}

class B extends A {
 def f[T[+_]](x : T[Int]) : T[Any] = x
}

class P[Y](var y : Y)
//////////////////////////////////////////////////

It accepted by the Scala 2.7.5 compiler without any errors. But is
seems unsound, because then we can write (new B:A).f[P](new
P[Int](1)), and nonvariant P[Int] is coerced to P[Any].

Thanks,
Vladimir



Re: [scala] Unsoundness in overriding methods with higher-order type parameters?

by nikov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I use Scala 2.7.5 final.

Vladimir

On 6/16/09, James Iry <jamesiry@...> wrote:

> Oops, missed the cast to an A.  Indeed that's a problem
>
> trait A {
>   def f[T[_]](x : T[Int]) : T[Any]
> }
>
> class B extends A {
>   def f[T[+_]](x : T[Int]) : T[Any] = x
> }
>
> class P[Y](var y : Y)
>
>
> val p = new P(1)
>
> val palias = (new B():A).f[P](p)
>
> palias.y = "hello"
>
> scala> p.y
> java.lang.ClassCastException: java.lang.String cannot be cast to
> java.lang.Integer
>
>
>
> On Tue, Jun 16, 2009 at 6:51 AM, James Iry <jamesiry@...> wrote:
>
>> Which version of Scala are you running?  In both 2.7.5 and
>> 2.8.0.something
>> I get
>>
>> <console>:7: error: kinds of the type arguments (P) do not conform to the
>> expected kinds of the type parameters (type T).
>> P's type parameters do not match type T's expected parameters: type Y is
>> invariant, but type _ is declared covariant
>>        val palias = new B().f[P](new P[Int](1))
>>
>>
>> On Tue, Jun 16, 2009 at 6:27 AM, Vladimir Reshetnikov <
>> v.reshetnikov@...> wrote:
>>
>>> Hi,
>>>
>>> Consider the following code:
>>>
>>> //////////////////////////////////////////////////
>>> trait A {
>>>  def f[T[_]](x : T[Int]) : T[Any]
>>> }
>>>
>>> class B extends A {
>>>  def f[T[+_]](x : T[Int]) : T[Any] = x
>>> }
>>>
>>> class P[Y](var y : Y)
>>> //////////////////////////////////////////////////
>>>
>>> It accepted by the Scala 2.7.5 compiler without any errors. But is
>>> seems unsound, because then we can write (new B:A).f[P](new
>>> P[Int](1)), and nonvariant P[Int] is coerced to P[Any].
>>>
>>> Thanks,
>>> Vladimir
>>>
>>
>>
>

Re: [scala] Unsoundness in overriding methods with higher-order type parameters?

by Adriaan Moors-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Vladimir,

You're right. That's a bug :-(
Working on a fix now -- could you please file a ticket?

cheers
adriaan

On Tue, Jun 16, 2009 at 4:04 PM, Vladimir Reshetnikov <v.reshetnikov@...> wrote:
I use Scala 2.7.5 final.

Vladimir

On 6/16/09, James Iry <jamesiry@...> wrote:
> Oops, missed the cast to an A.  Indeed that's a problem
>
> trait A {
>   def f[T[_]](x : T[Int]) : T[Any]
> }
>
> class B extends A {
>   def f[T[+_]](x : T[Int]) : T[Any] = x
> }
>
> class P[Y](var y : Y)
>
>
> val p = new P(1)
>
> val palias = (new B():A).f[P](p)
>
> palias.y = "hello"
>
> scala> p.y
> java.lang.ClassCastException: java.lang.String cannot be cast to
> java.lang.Integer
>
>
>
> On Tue, Jun 16, 2009 at 6:51 AM, James Iry <jamesiry@...> wrote:
>
>> Which version of Scala are you running?  In both 2.7.5 and
>> 2.8.0.something
>> I get
>>
>> <console>:7: error: kinds of the type arguments (P) do not conform to the
>> expected kinds of the type parameters (type T).
>> P's type parameters do not match type T's expected parameters: type Y is
>> invariant, but type _ is declared covariant
>>        val palias = new B().f[P](new P[Int](1))
>>
>>
>> On Tue, Jun 16, 2009 at 6:27 AM, Vladimir Reshetnikov <
>> v.reshetnikov@...> wrote:
>>
>>> Hi,
>>>
>>> Consider the following code:
>>>
>>> //////////////////////////////////////////////////
>>> trait A {
>>>  def f[T[_]](x : T[Int]) : T[Any]
>>> }
>>>
>>> class B extends A {
>>>  def f[T[+_]](x : T[Int]) : T[Any] = x
>>> }
>>>
>>> class P[Y](var y : Y)
>>> //////////////////////////////////////////////////
>>>
>>> It accepted by the Scala 2.7.5 compiler without any errors. But is
>>> seems unsound, because then we can write (new B:A).f[P](new
>>> P[Int](1)), and nonvariant P[Int] is coerced to P[Any].
>>>
>>> Thanks,
>>> Vladimir
>>>
>>
>>
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm


Re: [scala] Unsoundness in overriding methods with higher-order type parameters?

by nikov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Adriaan,

Ticket #2066. Do not know your nickname in trac, so I did not reassigned it.

Vladimir

On 6/17/09, Adriaan Moors <adriaan.moors@...> wrote:

> Hi Vladimir,
> You're right. That's a bug :-(
> Working on a fix now -- could you please file a ticket?
>
> cheers
> adriaan
>
> On Tue, Jun 16, 2009 at 4:04 PM, Vladimir Reshetnikov <
> v.reshetnikov@...> wrote:
>
>> I use Scala 2.7.5 final.
>>
>> Vladimir
>>
>> On 6/16/09, James Iry <jamesiry@...> wrote:
>> > Oops, missed the cast to an A.  Indeed that's a problem
>> >
>> > trait A {
>> >   def f[T[_]](x : T[Int]) : T[Any]
>> > }
>> >
>> > class B extends A {
>> >   def f[T[+_]](x : T[Int]) : T[Any] = x
>> > }
>> >
>> > class P[Y](var y : Y)
>> >
>> >
>> > val p = new P(1)
>> >
>> > val palias = (new B():A).f[P](p)
>> >
>> > palias.y = "hello"
>> >
>> > scala> p.y
>> > java.lang.ClassCastException: java.lang.String cannot be cast to
>> > java.lang.Integer
>> >
>> >
>> >
>> > On Tue, Jun 16, 2009 at 6:51 AM, James Iry <jamesiry@...> wrote:
>> >
>> >> Which version of Scala are you running?  In both 2.7.5 and
>> >> 2.8.0.something
>> >> I get
>> >>
>> >> <console>:7: error: kinds of the type arguments (P) do not conform to
>> the
>> >> expected kinds of the type parameters (type T).
>> >> P's type parameters do not match type T's expected parameters: type Y
>> >> is
>> >> invariant, but type _ is declared covariant
>> >>        val palias = new B().f[P](new P[Int](1))
>> >>
>> >>
>> >> On Tue, Jun 16, 2009 at 6:27 AM, Vladimir Reshetnikov <
>> >> v.reshetnikov@...> wrote:
>> >>
>> >>> Hi,
>> >>>
>> >>> Consider the following code:
>> >>>
>> >>> //////////////////////////////////////////////////
>> >>> trait A {
>> >>>  def f[T[_]](x : T[Int]) : T[Any]
>> >>> }
>> >>>
>> >>> class B extends A {
>> >>>  def f[T[+_]](x : T[Int]) : T[Any] = x
>> >>> }
>> >>>
>> >>> class P[Y](var y : Y)
>> >>> //////////////////////////////////////////////////
>> >>>
>> >>> It accepted by the Scala 2.7.5 compiler without any errors. But is
>> >>> seems unsound, because then we can write (new B:A).f[P](new
>> >>> P[Int](1)), and nonvariant P[Int] is coerced to P[Any].
>> >>>
>> >>> Thanks,
>> >>> Vladimir
>> >>>
>> >>
>> >>
>> >
>>
>>
>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>>
>

Re: [scala] Unsoundness in overriding methods with higher-order type parameters?

by Adriaan Moors-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks! I reassigned it. (it's "moors" ;-))

adriaan

On Wed, Jun 17, 2009 at 4:19 PM, Vladimir Reshetnikov <v.reshetnikov@...> wrote:
Hi Adriaan,

Ticket #2066. Do not know your nickname in trac, so I did not reassigned it.

Vladimir

On 6/17/09, Adriaan Moors <adriaan.moors@...> wrote:
> Hi Vladimir,
> You're right. That's a bug :-(
> Working on a fix now -- could you please file a ticket?
>
> cheers
> adriaan
>
> On Tue, Jun 16, 2009 at 4:04 PM, Vladimir Reshetnikov <
> v.reshetnikov@...> wrote:
>
>> I use Scala 2.7.5 final.
>>
>> Vladimir
>>
>> On 6/16/09, James Iry <jamesiry@...> wrote:
>> > Oops, missed the cast to an A.  Indeed that's a problem
>> >
>> > trait A {
>> >   def f[T[_]](x : T[Int]) : T[Any]
>> > }
>> >
>> > class B extends A {
>> >   def f[T[+_]](x : T[Int]) : T[Any] = x
>> > }
>> >
>> > class P[Y](var y : Y)
>> >
>> >
>> > val p = new P(1)
>> >
>> > val palias = (new B():A).f[P](p)
>> >
>> > palias.y = "hello"
>> >
>> > scala> p.y
>> > java.lang.ClassCastException: java.lang.String cannot be cast to
>> > java.lang.Integer
>> >
>> >
>> >
>> > On Tue, Jun 16, 2009 at 6:51 AM, James Iry <jamesiry@...> wrote:
>> >
>> >> Which version of Scala are you running?  In both 2.7.5 and
>> >> 2.8.0.something
>> >> I get
>> >>
>> >> <console>:7: error: kinds of the type arguments (P) do not conform to
>> the
>> >> expected kinds of the type parameters (type T).
>> >> P's type parameters do not match type T's expected parameters: type Y
>> >> is
>> >> invariant, but type _ is declared covariant
>> >>        val palias = new B().f[P](new P[Int](1))
>> >>
>> >>
>> >> On Tue, Jun 16, 2009 at 6:27 AM, Vladimir Reshetnikov <
>> >> v.reshetnikov@...> wrote:
>> >>
>> >>> Hi,
>> >>>
>> >>> Consider the following code:
>> >>>
>> >>> //////////////////////////////////////////////////
>> >>> trait A {
>> >>>  def f[T[_]](x : T[Int]) : T[Any]
>> >>> }
>> >>>
>> >>> class B extends A {
>> >>>  def f[T[+_]](x : T[Int]) : T[Any] = x
>> >>> }
>> >>>
>> >>> class P[Y](var y : Y)
>> >>> //////////////////////////////////////////////////
>> >>>
>> >>> It accepted by the Scala 2.7.5 compiler without any errors. But is
>> >>> seems unsound, because then we can write (new B:A).f[P](new
>> >>> P[Int](1)), and nonvariant P[Int] is coerced to P[Any].
>> >>>
>> >>> Thanks,
>> >>> Vladimir
>> >>>
>> >>
>> >>
>> >
>>
>>
>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>>
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm