Haskell测试了mapM的不同实现

时间:2015-12-08 21:04:32

标签: haskell monads

我已实施序列' :: Monad m => [m a] - > m [a]低于

sequence' [] = return []
sequence' (m : ms)
  = m >>=
      \ a ->
        do as <- sequence' ms
           return (a : as)

我需要测试以下实现 MAPM&#39; :: Monad m =&gt; (a - &gt; m b) - &gt; [a] - &gt; m [b]

mapM'a f as = sequence' (map f as)

mapM'b f [] = return []
mapM'b f (a : as)
  = f a >>= \b -> mapM'b f as >>= \ bs -> return (b : bs)

mapM'f f [] = return []
mapM'f f (a : as) =
    do b <- f a
       bs <- mapM'f f as
       return (b : bs)

mapM'g f [] = return []
mapM'g f (a : as)
  = f a >>=
      \ b ->
        do bs <- mapM'g f as
           return (b : bs)

mapM'h f [] = return []
mapM'h f (a : as)
  = f a >>=
      \ b ->
      do bs <- mapM'h f as
         return (bs ++ [b])

请告诉我如何测试和验证mapM的上述实现 - 我应该调用哪些函数。 一些例子非常有用。

由于

1 个答案:

答案 0 :(得分:1)

您可能会发现在测试中有用的一类monad是各种仿函数的free monads。免费的monad往往对你在实现中可能犯的任何错误特别敏感。您可以使用任何合理灵活的基础仿函数(例如[])进行测试。

data Free f a = Pure a
              | Free (f (Free f a))

instance Functor f => Functor (Free f) where
  fmap f (Pure a) = Pure (f a)
  fmap f (Free xs) = Free (fmap (fmap f) xs)

instance Functor f => Applicative (Free f) where
  pure = Pure
  (<*>) = ap

instance Functor f => Monad (Free f) where
  Pure x >>= f = f x
  Free xs >>= f = Free (fmap (>>= f) xs)

instance (Eq (f (Free f a)), Eq a) => Eq (Free f a) where
  Pure a == Pure b = a == b
  Free fa == Free fb = fa == fb
  _ == _ = False