Thinking Functionally with Haskell勉強メモ: 第3章続 Type? Type Class?
Thinking Functionally with Haskellに”all numbers are instances of the type class Num”とあるが、”all number types are instances of the type class Num”の間違い?
— zehnpaard (@zehnpaard) 2018年3月8日
というわけで第3章を読み返していて腑に落ちない点が幾つか。
上記のツイートの通り、
In Haskell, all numbers are instances of the type class Num
と書いてあるのは間違いのように思えるのだが・・・
Expression X --(Is an instance of)--> Type Y
Type Y --(Is an instance of)--> Type Class Z
というような大まかな印象だったのだが、実際には
Type Y --(Implements the interface of)--> Type Class Z
Expression X --(Is an instance of)--> Type Y
Expression X --(Is an instance of)--> Type Class Z
なのか?
クラスベースの継承モデルなら後者がぴったりだが、正直TypeとType Classは概念的に別カテゴリな印象だったのであるexpressionがあるtype classのinstanceだというのは結構混乱する。
さらに
Prelude> x = 1 Prelude> :type x x :: Num t => t Prelude> x = 1 :: Int Prelude> :type x x :: Int
というのも不思議。1は明示的にtypeを指定されない限り、固有の型を持たず、「Num型クラスの何かの型」だと言われているようだ。うーむ。