Higher Kinded Types

114 views
Skip to first unread message

Joe San

unread,
Apr 18, 2017, 6:25:01 AM4/18/17
to scala-user

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

Oliver Ruebenacker

unread,
Apr 18, 2017, 9:19:20 AM4/18/17
to Joe San, scala-user

     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

--
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 scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



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

Jasper-M

unread,
Apr 18, 2017, 9:59:37 AM4/18/17
to scala-user
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:

Jasper Moeys

unread,
Apr 18, 2017, 11:20:59 AM4/18/17
to Joe San, scala...@googlegroups.com
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" <codein...@gmail.com>:
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]] 
--
Reply all
Reply to author
Forward
0 new messages