This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Type alias over type constructors

4 replies
marius
Joined: 2008-08-31,
User offline. Last seen 3 years 19 weeks ago.
Hi,

The following doesn't seem to be possible:

trait OddFunctor[A] {
   type T

   def unit(v: A) : T[A]

   def fmap[B](f: A => B): T[B]
}

class SomeClass[T]( val v : T) extends OddFunctor[SomeClass] {

   type T = SomeClass

   def unit[B](v: B): T[B] = ...

   def fmap[B](f: A => B): T[B] = ...

}

Compiler complains that SomeClass takes type parameters.

Is there away to workaround this?

P.S. This isn't really the way I'd write Functors but rather using higher kinded types. This is just an exercise.

Thanks,
Marius
adriaanm
Joined: 2010-02-08,
User offline. Last seen 31 weeks 4 days ago.
Re: Type alias over type constructors


On Mon, Aug 8, 2011 at 11:04 AM, Marius Danciu <marius [dot] danciu [at] gmail [dot] com> wrote:
   type T = SomeClass
type T[x] = SomeClass[x]
this defines T as a type constructor of kind * -> *since we don't have kind annotations, when you simply write `type T = ...`, the kind * is inferred for this definition
our approach to defining types and type constructors avoids talking about kinds, like method definitions avoid talking about function types: think of the analogy between how you define methods by "prototypically" specifying the arguments they take, rather than giving the equivalent function type for the value:
def foo(x: Any): Any = ...  // a prototype for valid calls of foo: must look like foo(x) where x is an Anytype Foo[x] = SomeClass[x] // a prototype for creating a proper type (i.e., of kind *): it must look like Foo[x], where x is some proper type
val foo: Any => Any = ...  // Function-type corresponding to the method// type Foo : * -> * = SomeClass   // not valid Scala (we don't like to talk about kinds)
marius
Joined: 2008-08-31,
User offline. Last seen 3 years 19 weeks ago.
Re: Type alias over type constructors
I see, although having type T[x] = SomeClass[x] would cause another compile time error for

def unit(v: A) : T[A]

saying that T does not take type parameters. Which is normal I guess since T[x] is a proper type.

Thanks,
Marius

On Mon, Aug 8, 2011 at 12:53 PM, Adriaan Moors <adriaan [dot] moors [at] epfl [dot] ch> wrote:


On Mon, Aug 8, 2011 at 11:04 AM, Marius Danciu <marius [dot] danciu [at] gmail [dot] com> wrote:
   type T = SomeClass
type T[x] = SomeClass[x]
this defines T as a type constructor of kind * -> *since we don't have kind annotations, when you simply write `type T = ...`, the kind * is inferred for this definition
our approach to defining types and type constructors avoids talking about kinds, like method definitions avoid talking about function types: think of the analogy between how you define methods by "prototypically" specifying the arguments they take, rather than giving the equivalent function type for the value:
def foo(x: Any): Any = ...  // a prototype for valid calls of foo: must look like foo(x) where x is an Anytype Foo[x] = SomeClass[x] // a prototype for creating a proper type (i.e., of kind *): it must look like Foo[x], where x is some proper type
val foo: Any => Any = ...  // Function-type corresponding to the method// type Foo : * -> * = SomeClass   // not valid Scala (we don't like to talk about kinds)

adriaanm
Joined: 2010-02-08,
User offline. Last seen 31 weeks 4 days ago.
Re: Type alias over type constructors
that's probably because you also have a type parameter T in scope (which doesn't take type parameters), a type parameter takes precedence over a type member (since the latter can be accessed by selecting it on `this` explicitly, whereas the former cannot)

On Mon, Aug 8, 2011 at 11:58 AM, Marius Danciu <marius [dot] danciu [at] gmail [dot] com> wrote:
I see, although having type T[x] = SomeClass[x] would cause another compile time error for

def unit(v: A) : T[A]

saying that T does not take type parameters. Which is normal I guess since T[x] is a proper type.

Thanks,
Marius

On Mon, Aug 8, 2011 at 12:53 PM, Adriaan Moors <adriaan [dot] moors [at] epfl [dot] ch> wrote:


On Mon, Aug 8, 2011 at 11:04 AM, Marius Danciu <marius [dot] danciu [at] gmail [dot] com> wrote:
   type T = SomeClass
type T[x] = SomeClass[x]
this defines T as a type constructor of kind * -> *since we don't have kind annotations, when you simply write `type T = ...`, the kind * is inferred for this definition
our approach to defining types and type constructors avoids talking about kinds, like method definitions avoid talking about function types: think of the analogy between how you define methods by "prototypically" specifying the arguments they take, rather than giving the equivalent function type for the value:
def foo(x: Any): Any = ...  // a prototype for valid calls of foo: must look like foo(x) where x is an Anytype Foo[x] = SomeClass[x] // a prototype for creating a proper type (i.e., of kind *): it must look like Foo[x], where x is some proper type
val foo: Any => Any = ...  // Function-type corresponding to the method// type Foo : * -> * = SomeClass   // not valid Scala (we don't like to talk about kinds)


marius
Joined: 2008-08-31,
User offline. Last seen 3 years 19 weeks ago.
Re: Type alias over type constructors
Indeed this works:

trait OddFunctor[A] {
   type T[X]

   def unit[B](v: B) : T[B]

   def fmap[B](f: A => B): T[B]
}

class SomeClass[A]( val v : A) extends OddFunctor[A] {

   type T[X] = SomeClass[X]

   def unit[B](v: B): T[B] = new SomeClass(v)

   def fmap[B](f: A => B): T[B] = unit(f(v))

}

Thank you very much Adrian.

Marius

On Mon, Aug 8, 2011 at 1:51 PM, Adriaan Moors <adriaan [dot] moors [at] epfl [dot] ch> wrote:
that's probably because you also have a type parameter T in scope (which doesn't take type parameters), a type parameter takes precedence over a type member (since the latter can be accessed by selecting it on `this` explicitly, whereas the former cannot)

On Mon, Aug 8, 2011 at 11:58 AM, Marius Danciu <marius [dot] danciu [at] gmail [dot] com> wrote:
I see, although having type T[x] = SomeClass[x] would cause another compile time error for

def unit(v: A) : T[A]

saying that T does not take type parameters. Which is normal I guess since T[x] is a proper type.

Thanks,
Marius

On Mon, Aug 8, 2011 at 12:53 PM, Adriaan Moors <adriaan [dot] moors [at] epfl [dot] ch> wrote:


On Mon, Aug 8, 2011 at 11:04 AM, Marius Danciu <marius [dot] danciu [at] gmail [dot] com> wrote:
   type T = SomeClass
type T[x] = SomeClass[x]
this defines T as a type constructor of kind * -> *since we don't have kind annotations, when you simply write `type T = ...`, the kind * is inferred for this definition
our approach to defining types and type constructors avoids talking about kinds, like method definitions avoid talking about function types: think of the analogy between how you define methods by "prototypically" specifying the arguments they take, rather than giving the equivalent function type for the value:
def foo(x: Any): Any = ...  // a prototype for valid calls of foo: must look like foo(x) where x is an Anytype Foo[x] = SomeClass[x] // a prototype for creating a proper type (i.e., of kind *): it must look like Foo[x], where x is some proper type
val foo: Any => Any = ...  // Function-type corresponding to the method// type Foo : * -> * = SomeClass   // not valid Scala (we don't like to talk about kinds)



Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland