Scala Higher Kind Generics

Generics of a Higher Kind | Lambda the Ultimate 経由。

ScalaでできてHaskellでできないことがあげられている:

class Monad m where
  (>>=) :: m a -> (a -> m b) -> m b

data (Ord a) => Set a = ...

instance Monad Set where
-- (>>=) :: Set a -> (a -> Set b) -> Set b

Scalaでは

trait BoundedMonad[A <: Bound[A], M[X <: Bound[X]], Bound[X]] {
   def >>= [B <: Bound[B]](f: A ) M[B]): M[B]
}

trait Set[T <: Ord[T]]

implicit def SetIsBoundedMonad[T <: Ord[T]](s: Set[T])
   : BoundedMonad[T, Set, Ord] = ...

と制約を型メンバー間に課するタイプのMonadが書けるようになっている。

タイトルのKindとは、型(Type)の(構造の)種類のことで、その違いで(処理的に)区別するときの概念。
SetA[T

  • Kind = *(Type, Type) | Kind -> Kind
    • *(Type, Type): 上限型下限型で示されたKind。サブタイプ関係でその間にある型すべてならそのKindになっている
    • * = *(Nothing, Any): 上限がAny,下限がNothing.つまり型なら全部このKind
    • *(T) = *(Nothing, T): 上限がT。つまりTのサブタイプすべて。
    • Kind -> Kind: 左のKindをもとに生成されるKind。Type Classのような型を指す。
      • NumericList[t \<: Number] : *(Number) -> *、つまりInt\