考虑与(Bool ->)
monad同构的monad:
data Pair a = P a a
instance Functor Pair where
fmap f (P x y) = P (f x) (f y)
instance Monad Pair where
return x = P x x
P a b >>= f = P x y
where P x _ = f a
P _ y = f b
并用Maybe
monad组成:
newtype Bad a = B (Maybe (Pair a))
我声称那Bad
不可能是单子。
部分证明:
只有一种定义fmap
满足的方法fmap id = id
:
instance Functor Bad where
fmap f (B x) = B $ fmap (fmap f) x
回忆一下单子法则:
(1) join (return x) = x
(2) join (fmap return x) = x
(3) join (join x) = join (fmap join x)
对于的定义return x
,我们有两个选择:B Nothing
或B (Just (P x x))
。显然,为了有希望x
从(1)和(2)返回,我们不能丢掉它x
,因此我们必须选择第二个选项。
return' :: a -> Bad a
return' x = B (Just (P x x))
离开join
。由于只有少数可能的输入,因此我们可以为每个输入一个案例:
join :: Bad (Bad a) -> Bad a
(A) join (B Nothing) = ???
(B) join (B (Just (P (B Nothing) (B Nothing)))) = ???
(C) join (B (Just (P (B (Just (P x1 x2))) (B Nothing)))) = ???
(D) join (B (Just (P (B Nothing) (B (Just (P x1 x2)))))) = ???
(E) join (B (Just (P (B (Just (P x1 x2))) (B (Just (P x3 x4)))))) = ???
由于输出具有类型Bad a
,唯一的选择是B Nothing
或B (Just (P y1 y2))
其中y1
,y2
必须从选择x1 ... x4
。
在情况(A)和(B)中,我们没有类型的值a
,因此B Nothing
在两种情况下我们都必须返回。
情况(E)由(1)和(2)单子法确定:
join (return' (B (Just (P y1 y2))))
=
join (B (Just (P (B (Just (P y1 y2))) (B (Just (P y1 y2))))))
=
B (Just (P y1 y2))
为了返回B (Just (P y1 y2))
情况(E),这意味着我们必须y1
从x1
或中进行选择x3
,并y2
从x2
或中进行选择x4
。
join (fmap return' (B (Just (P y1 y2))))
=
join (B (Just (P (return y1) (return y2))))
=
join (B (Just (P (B (Just (P y1 y1))) (B (Just (P y2 y2))))))
=
B (Just (P y1 y2))
同样,这表示我们必须y1
从x1
或中进行选择x2
,并y2
从x3
或中进行选择x4
。结合两者,我们确定(E)的右侧必须为B (Just (P x1 x4))
。
到目前为止,一切都很好,但是当您尝试在(C)和(D)的右侧填写时,问题就来了。
每种都有5个可能的右侧,并且所有组合都不起作用。我对此还没有一个很好的论据,但是我有一个可以详尽测试所有组合的程序:
{-# LANGUAGE ImpredicativeTypes, ScopedTypeVariables #-}
import Control.Monad (guard)
data Pair a = P a a
deriving (Eq, Show)
instance Functor Pair where
fmap f (P x y) = P (f x) (f y)
instance Monad Pair where
return x = P x x
P a b >>= f = P x y
where P x _ = f a
P _ y = f b
newtype Bad a = B (Maybe (Pair a))
deriving (Eq, Show)
instance Functor Bad where
fmap f (B x) = B $ fmap (fmap f) x
unit :: a -> Bad a
unit x = B (Just (P x x))
joins :: Integer
joins = sum $ do
let ways = [ \_ _ -> B Nothing
, \a b -> B (Just (P a a))
, \a b -> B (Just (P a b))
, \a b -> B (Just (P b a))
, \a b -> B (Just (P b b)) ] :: [forall a. a -> a -> Bad a]
c3 :: forall a. a -> a -> Bad a <- ways
c4 :: forall a. a -> a -> Bad a <- ways
let join :: forall a. Bad (Bad a) -> Bad a
join (B Nothing) = B Nothing
join (B (Just (P (B Nothing) (B Nothing)))) = B Nothing
join (B (Just (P (B (Just (P x1 x2))) (B Nothing)))) = c3 x1 x2
join (B (Just (P (B Nothing) (B (Just (P x3 x4)))))) = c4 x3 x4
join (B (Just (P (B (Just (P x1 x2))) (B (Just (P x3 x4)))))) = B (Just (P x1 x4))
guard $ all (\x -> join (unit x) == x) bad1
guard $ all (\x -> join (fmap unit x) == x) bad1
guard $ all (\x -> join (join x) == join (fmap join x)) bad3
return 1
main = putStrLn $ show joins ++ " combinations work."
bad1 :: [Bad Int]
bad1 = map fst (bad1' 1)
bad3 :: [Bad (Bad (Bad Int))]
bad3 = map fst (bad3' 1)
bad1' :: Int -> [(Bad Int, Int)]
bad1' n = [(B Nothing, n), (B (Just (P n (n+1))), n+2)]
bad2' :: Int -> [(Bad (Bad Int), Int)]
bad2' n = (B Nothing, n) : do
(x, n') <- bad1' n
(y, n'') <- bad1' n'
return (B (Just (P x y)), n'')
bad3' :: Int -> [(Bad (Bad (Bad Int)), Int)]
bad3' n = (B Nothing, n) : do
(x, n') <- bad2' n
(y, n'') <- bad2' n'
return (B (Just (P x y)), n'')
join
两个单子的组成中一般的。但这并没有导致任何具体的例子。