kind
日本語では「種」と言う
読み方わからないが「しゅ」っぽい
型の型
型コンストラクタの型のようなもの
:k、:kindで調べられる
code: ghci
Prelude> :k Int
Int :: *
*(スター)は具体型を表す
kind周辺の用語の整理
kind注釈
様々なkind
*
具体型、データ型のkind
Intとか
->
関数のkind
Constraint
型制約種を表す
DataKindsを使って自前に作成もできる
MaybeやEitherは型コンストラクタで、具体型を引数にとって具体型になる
code:shell
Prelude> :k Maybe
Maybe :: * -> *
Prelude> :k Either
Either :: * -> * -> *
例えば、Maybeに型引数も指定した型のkindを調べると*(具体型)となる
code:shell
Prelude> :k Maybe String
Maybe String :: *
型引数を取る型は部分適用できる
Eitherに1つ型を指定すると、「型引数に具体型を1つ取って具体型になる型」になる
code:shell
Prelude> :k Either String
Either String :: * -> *
例
kindが*なものの例
具体型
値コンストラクタ
e.g. Int、String
型コンストラクタに完全に型を詰めたやつ
e.g. Either Int String、Maybe Bool
関数型
Int -> Int -> Bool
↓こういう定義のVoid型
code:hs
data Void
newtype Void = Void Void
値コンストラクタを持たない0引数型コンストラクタ
kindが*->*なものの例
Maybe、[]
kindが*->*->*なものの例
Either、2-タプル型、関数型(->)
関連するGHC拡張
TypeOperators
ConstraintKinds
DataKinds
型推論ならぬkind推論される
勝手にされる
逆に言えば、自分で指定できないので融通が効かないことがある ref ref2
参考
About kind system of Haskell (Part 1) - Haskell-jp
詳しい、最初に読むと良い
https://haskell.jp/blog/posts/2017/13-about-kind-system-part2.html
やばいmrsekut.icon
GeneralizedNewtypeDeriving
軽率多相
https://www.morrowm.com/posts/2022-07-19-kinds.html
https://ryanglscott.github.io/2020/01/05/five-benefits-to-using-standalonekindsignatures/
https://ryanglscott.github.io/2020/01/05/five-benefits-to-using-standalonekindsignatures/
https://downloads.haskell.org/~ghc/8.0.2/docs/html/users_guide/glasgow_exts.html#kind-polymorphism-and-type-in-type
https://zehnpaard.hatenablog.com/entry/2022/02/28/034206