Quantcast

Higher Kinded Types

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Higher Kinded Types

Joe San

Dear Scala users,

I'm reading through the Functional Programming in Scala book and in the Monoids chapter, they talk about a Monoid interface that looks like this:

trait Monoid[A] {
 def op(a1: A, a2: A): A
 def zero: A
}

Later on, they define specific Monoid instances by extending this interface. For example.,

val intMonoid = new Monoid[Int] {
  ...
}

val listMonoid = new Monoid[List[Int]] {
  ...
}

A couple more pages that I read through this chapter 10, I come across 'higher kinded types (HKT)' which according to the book is any type that it self is a type that can take other types.

trait Foldable[F[_]] {
 ...
 ...
}

So the trait Foldable is according to the book a higher kinded type. My question is, the Monoid[A] to me is also fits the 'higher kinded type' definition as it can take a List[A]

def listMonoid[A] = new Monoid[List[A]] {
  ...
  ...
}

So is my listMonoid function an implementation of the Monoid[A] HKT?

Thanks,

Joe

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Higher Kinded Types

Oliver Ruebenacker

     Hello,

  A type declaration like A[_] is a higher-kinded type, because it is a function B => A[B] from type to type, whereas A[B] is simply a type expression, not a function.

     Best, Oliver

On Tue, Apr 18, 2017 at 6:24 AM, Joe San <[hidden email]> wrote:

Dear Scala users,

I'm reading through the Functional Programming in Scala book and in the Monoids chapter, they talk about a Monoid interface that looks like this:

trait Monoid[A] {
 def op(a1: A, a2: A): A
 def zero: A
}

Later on, they define specific Monoid instances by extending this interface. For example.,

val intMonoid = new Monoid[Int] {
  ...
}

val listMonoid = new Monoid[List[Int]] {
  ...
}

A couple more pages that I read through this chapter 10, I come across 'higher kinded types (HKT)' which according to the book is any type that it self is a type that can take other types.

trait Foldable[F[_]] {
 ...
 ...
}

So the trait Foldable is according to the book a higher kinded type. My question is, the Monoid[A] to me is also fits the 'higher kinded type' definition as it can take a List[A]

def listMonoid[A] = new Monoid[List[A]] {
  ...
  ...
}

So is my listMonoid function an implementation of the Monoid[A] HKT?

Thanks,

Joe

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Higher Kinded Types

Jasper-M
In reply to this post by Joe San
Hi Joe,

This stackoverflow answer really explains HKTs very well.
Monoid is a type constructor that accepts proper types. Foldable is a type constructor that accepts another type constructor. 
That makes Foldable a HKT, and Monoid not. Whether you can create a Monoid[List[A]] for any proper type A or not doesn't really matter. You cannot pass a type constructor to the Monoid type constructor thus it is not a HKT.

Kind regards,
Jasper

Op dinsdag 18 april 2017 12:25:01 UTC+2 schreef Joe San:

Dear Scala users,

I'm reading through the Functional Programming in Scala book and in the Monoids chapter, they talk about a Monoid interface that looks like this:

trait Monoid[A] {
 def op(a1: A, a2: A): A
 def zero: A
}

Later on, they define specific Monoid instances by extending this interface. For example.,

val intMonoid = new Monoid[Int] {
  ...
}

val listMonoid = new Monoid[List[Int]] {
  ...
}

A couple more pages that I read through this chapter 10, I come across 'higher kinded types (HKT)' which according to the book is any type that it self is a type that can take other types.

trait Foldable[F[_]] {
 ...
 ...
}

So the trait Foldable is according to the book a higher kinded type. My question is, the Monoid[A] to me is also fits the 'higher kinded type' definition as it can take a List[A]

def listMonoid[A] = new Monoid[List[A]] {
  ...
  ...
}

So is my listMonoid function an implementation of the Monoid[A] HKT?

Thanks,

Joe

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Higher Kinded Types

Jasper-M
The listmonoid method has the method type [A]Monoid[List[A]]. Meaning that from any type A it can create a value of type Monoid[List[A]]. But the List[A] that you pass to Monoid is still a proper type, not a type constructor. The property of being higher kinded is not really ascribed to methods (value level) but to type constructors (type level). Maybe it would be possible to say of a method type that it's higher kinded, but listmonoid still isn't. I imagine a method type [F[_]]Monoid[F[Int]] could be considered higher kinded. 
I'm also no expert on type theory.

Op 18 apr. 2017 16:50 schreef "Joe San" <[hidden email]>:
So in my listMonoid function, I'm still passing in a type constructor where A => List[A] so this qualifies as a HKT or?

def listMonoid[A] = new Monoid[List[A]] 

On Tue, Apr 18, 2017 at 3:59 PM, Jasper-M <[hidden email]> wrote:
Hi Joe,

This stackoverflow answer really explains HKTs very well.
Monoid is a type constructor that accepts proper types. Foldable is a type constructor that accepts another type constructor. 
That makes Foldable a HKT, and Monoid not. Whether you can create a Monoid[List[A]] for any proper type A or not doesn't really matter. You cannot pass a type constructor to the Monoid type constructor thus it is not a HKT.

Kind regards,
Jasper

Op dinsdag 18 april 2017 12:25:01 UTC+2 schreef Joe San:

Dear Scala users,

I'm reading through the Functional Programming in Scala book and in the Monoids chapter, they talk about a Monoid interface that looks like this:

trait Monoid[A] {
 def op(a1: A, a2: A): A
 def zero: A
}

Later on, they define specific Monoid instances by extending this interface. For example.,

val intMonoid = new Monoid[Int] {
  ...
}

val listMonoid = new Monoid[List[Int]] {
  ...
}

A couple more pages that I read through this chapter 10, I come across 'higher kinded types (HKT)' which according to the book is any type that it self is a type that can take other types.

trait Foldable[F[_]] {
 ...
 ...
}

So the trait Foldable is according to the book a higher kinded type. My question is, the Monoid[A] to me is also fits the 'higher kinded type' definition as it can take a List[A]

def listMonoid[A] = new Monoid[List[A]] {
  ...
  ...
}

So is my listMonoid function an implementation of the Monoid[A] HKT?

Thanks,

Joe

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].

For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...