読者です 読者をやめる 読者になる 読者になる

IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

Type Classのメモ

Haskell

Type Classは以下の4つの観点から理解するとよい。

観点

(1) インターフェースが定められている。

(2) 満たすべき法則(law)がある。

(3) 上記を満たすインスタンスがある(抽象化の逆の具体化されたもの)。

(4) インターフェース以外に便利な操作(Operation)がある。

資料の場所

  • (1), (3)はGHCIの:iで確認できる。ドキュメントでも確認できる。
  • (3)はドキュメント'... should satisfy the following laws```と記載された部分で示されている。
  • (1)はドキュメント上でminimum complete definitionと示されている部分に相当する。
  • (1),(4)はドキュメント上でMethodsの節に記載されている。

functor

https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Functor.html

class Functor (f :: * -> *) where
  fmap :: (a -> b) -> f a -> f b
  (<$) :: a -> f b -> f a
  {-# MINIMAL fmap #-}
    -- Defined in ‘GHC.Base’
instance Functor (Either a) -- Defined in ‘Data.Either’
instance Functor (Magma i t b) -- Defined in ‘Control.Lens.Internal.Magma’
instance [safe] Functor (Level i) -- Defined in ‘Control.Lens.Internal.Level’
instance Functor (Indexed i a) -- Defined in ‘Control.Lens.Internal.Indexed’
instance Functor (Context a b) -- Defined in ‘Control.Lens.Internal.Context’
instance Functor (Bazaar1 p a b) -- Defined in ‘Control.Lens.Internal.Bazaar’
instance Functor (Bazaar p a b) -- Defined in ‘Control.Lens.Internal.Bazaar’
instance Functor (Const m) -- Defined in ‘Data.Functor.Const’
instance Functor Identity -- Defined in ‘Data.Functor.Identity’
instance Functor (ReifiedIndexedGetter i s) -- Defined in ‘Control.Lens.Reified’
instance Functor (ReifiedIndexedFold i s) -- Defined in ‘Control.Lens.Reified’
instance Functor (ReifiedGetter s) -- Defined in ‘Control.Lens.Reified’
instance Functor (ReifiedFold s) -- Defined in ‘Control.Lens.Reified’
instance Functor [] -- Defined in ‘GHC.Base’
instance Functor Maybe -- Defined in ‘GHC.Base’
instance Functor IO -- Defined in ‘GHC.Base’
instance Functor ((->) r) -- Defined in ‘GHC.Base’
instance Functor ((,) a) -- Defined in ‘GHC.Base’
Applicative

https://hackage.haskell.org/package/base-4.9.0.0/docs/Control-Applicative.html

class Functor f => Applicative (f :: * -> *) where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b
  (*>) :: f a -> f b -> f b
  (<*) :: f a -> f b -> f a
  {-# MINIMAL pure, (<*>) #-}
    -- Defined in ‘GHC.Base’
instance Applicative (Either e) -- Defined in ‘Data.Either’
instance Applicative (Indexed i a) -- Defined in ‘Control.Lens.Internal.Indexed’
instance Applicative (Bazaar p a b) -- Defined in ‘Control.Lens.Internal.Bazaar’
instance Monoid m => Applicative (Const m) -- Defined in ‘Data.Functor.Const’
instance Applicative Identity -- Defined in ‘Data.Functor.Identity’
instance Applicative (ReifiedGetter s) -- Defined in ‘Control.Lens.Reified’
instance Applicative (ReifiedFold s) -- Defined in ‘Control.Lens.Reified’
instance Applicative [] -- Defined in ‘GHC.Base’
instance Applicative Maybe -- Defined in ‘GHC.Base’
instance Applicative IO -- Defined in ‘GHC.Base’
instance Applicative ((->) a) -- Defined in ‘GHC.Base’
instance Monoid a => Applicative ((,) a) -- Defined in ‘GHC.Base’
Monad

https://hackage.haskell.org/package/base-4.9.0.0/docs/Control-Monad.html

class Applicative m => Monad (m :: * -> *) where
  (>>=) :: m a -> (a -> m b) -> m b
  (>>) :: m a -> m b -> m b
  return :: a -> m a
  fail :: String -> m a
  {-# MINIMAL (>>=) #-}
    -- Defined in ‘GHC.Base’
instance Monad (Either e) -- Defined in ‘Data.Either’
instance Monad (Indexed i a) -- Defined in ‘Control.Lens.Internal.Indexed’
instance Monad Identity -- Defined in ‘Data.Functor.Identity’
instance Monad (ReifiedGetter s) -- Defined in ‘Control.Lens.Reified’
instance Monad (ReifiedFold s) -- Defined in ‘Control.Lens.Reified’
instance Monad [] -- Defined in ‘GHC.Base’
instance Monad Maybe -- Defined in ‘GHC.Base’
instance Monad IO -- Defined in ‘GHC.Base’
instance Monad ((->) r) -- Defined in ‘GHC.Base’
instance Monoid a => Monad ((,) a) -- Defined in ‘GHC.Base’
Comonad

[https://hackage.haskell.org/package/comonad-1.1.0/docs/Control-Comonad.html

class Functor w => Comonad (w :: * -> *) where
  extract :: w a -> a
  duplicate :: w a -> w (w a)
  extend :: (w a -> b) -> w a -> w b
  {-# MINIMAL extract, (duplicate | extend) #-}
    -- Defined in ‘Control.Comonad’
instance [safe] Comonad Identity -- Defined in ‘Control.Comonad’
instance [safe] Monoid m => Comonad ((->) m) -- Defined in ‘Control.Comonad’
instance [safe] Comonad ((,) e) -- Defined in ‘Control.Comonad’
instance a ~ b => Comonad (Context a b) -- Defined in ‘Control.Lens.Internal.Context’
instance (a ~ b, Conjoined p) => Comonad (Bazaar1 p a b) -- Defined in ‘Control.Lens.Internal.Bazaar’
instance (a ~ b, Conjoined p) => Comonad (Bazaar p a b) -- Defined in ‘Control.Lens.Internal.Bazaar’
instance Monoid s => Comonad (ReifiedGetter s) -- Defined in ‘Control.Lens.Reified’