1

Typeclassopediaの演習を行っています。の項にの関数Applicativeを書き、法則に従っているかどうかをチェックします。ZipListpureApplicative

私はチェックしました:

  • 同一性法
  • 交流法
  • 合成法

しかし、「準同型」の法則をチェックしようとすると、GHCi が結果を として得ていないことがわかりましたMZipList

pureこれは、Applicative型クラスに を指定するのを忘れたためだと思います。pure関数<*>をすぐに実行せずに実行するにはどうすればよいApplicativeですか?</p>

MZipList定義とクラス インスタンスは次のとおりです。

newtype MZipList a = MZipList { getZipList :: [a] }
    deriving (Show)

instance Functor MZipList where
  fmap gs x = pure gs <*> x

instance Applicative MZipList where
  pure a= MZipList (repeat a)
  (MZipList gs) <*> (MZipList xs) = MZipList (zipWith ($) gs xs)

たとえば、「インターチェンジ」法を確認すると、次のようになります。

*Main> (MZipList [(*2),(*3),(*4)]) <*> pure (2)
MZipList {getZipList = [4,6,8]}
*Main> pure ($ 2) <*> (MZipList [(*2),(*3),(*4)])
MZipList {getZipList = [4,6,8]}

しかし、「Homomorphism」法をチェックすると、MZipListpure呼び出されません:

*Main> pure (*2) <*> pure 2
4
*Main>  pure ((*2) 2)
4
*Main>

何故ですか?

4

1 に答える 1