以下の内容はhttps://hiratara.hatenadiary.jp/entry/20131115/1384531042より取得しました。


関手圏からHaskへの関手

こんな感じでいいのかな。

class HFunctor hf where
  hfmap :: (Functor f, Functor f') =>
           (forall t . f t -> f' t) -> hf f -> hf f'

以下は、型bを固定した時に定義できる関手F_bで、Functor fを型f bに移すような関手の定義。

data FunctorB b g = FunctorB (g b) deriving (Eq, Show)

instance HFunctor (FunctorB b) where
  hfmap nat (FunctorB x) = FunctorB (nat x)

例えば、以下のようなMaybe関手からList関手への自然変換。

maybeToList :: Maybe a -> [a]
maybeToList Nothing = []
maybeToList (Just x) = [x]

hfmapを使えばきちんとMaybe Intから[Int]への関数(Haskの射)に移る。

*Main> hfmap maybeToList (FunctorB (Just 10))
FunctorB [10]

昔はhackageに同名のクラスが上がっていたようだが、こちらは多分関手圏から関手圏への関手。今は消されている。




以上の内容はhttps://hiratara.hatenadiary.jp/entry/20131115/1384531042より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14