Typeclassopediaの演習を行っています。の項にの関数Applicative
を書き、法則に従っているかどうかをチェックします。ZipList
pure
Applicative
私はチェックしました:
- 同一性法
- 交流法
- 合成法
しかし、「準同型」の法則をチェックしようとすると、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」法をチェックすると、MZipList
はpure
呼び出されません:
*Main> pure (*2) <*> pure 2
4
*Main> pure ((*2) 2)
4
*Main>
何故ですか?