如何将Monad实例定义为具有多个值的类型?

ptk*_*ato 7 haskell

多个值我的意思是这样的:

data Foo a = Bar a | Baz a a
Run Code Online (Sandbox Code Playgroud)

我想不出一个明确的方式来定义>>=Baz:

instance Monad Foo where
    Bar x   >>= f = f x -- Great, that works perfectly!
    Baz x y >>= f = ??? -- What the heck do I even put here?
Run Code Online (Sandbox Code Playgroud)

Dan*_*ner 14

也许:

frst (Bar a) = a
frst (Baz a a') = a

scnd (Bar a) = a
scnd (Baz a a') = a'

instance Monad Foo where
    return = Bar
    Bar x >>= f = f x
    Baz x y >>= f = Baz (frst (f x)) (scnd (f y))
Run Code Online (Sandbox Code Playgroud)

这个定义的灵感来自(>>=)for 的定义(Bool ->).问我是否不清楚如何.

我们来检查一下法律." return单位"法律非常简单:

  return x >>= f
= Bar x >>= f
= f x

  m >>= return
= case m of
      Bar x -> return x
      Baz x y -> Baz (frst (return x)) (scnd (return y))
= case m of
      Bar x -> Bar x
      Baz x y -> Baz x y
= m
Run Code Online (Sandbox Code Playgroud)

我相信我也确信自己也是" (>>=)联合"法则,但我确信这个证据对任何人都是完全不可读的......我鼓励你自己尝试证明这一点,并将我的计算称为欺骗-sheet如果你卡住了.

  m >>= (\v -> f v >>= g)
= case m of
      Bar x -> (\v -> f v >>= g) x
      Baz x y -> Baz (frst ((\v -> f v >>= g) x))
                     (scnd ((\v -> f v >>= g) y))
= case m of
      Bar x -> f x >>= g
      Baz x y -> Baz (frst (f x >>= g)) (scnd (f y >>= g))
= case m of
      Bar x -> case f x of
          Bar y -> g y
          Baz a b -> Baz (frst (g a)) (scnd (g b))
      Baz x y -> Baz (frst l) (scnd r) where
          l = case f x of
                  Bar a -> g a
                  Baz a b -> Baz (frst (g a)) (scnd (g b))
          r = case f y of
                  Bar a -> g a
                  Baz a b -> Baz (frst (g a)) (scnd (g b))
= case m of
      Bar x -> case f x of
          Bar y -> g y
          Baz a b -> Baz (frst (g a)) (scnd (g b))
      Baz x y -> Baz (frst (g (frst (f x))))
                     (scnd (g (scnd (f y))))
= case m of
      Bar a -> case f a of
          Bar x -> g x
          Baz x y -> Baz (frst (g x)) (scnd (g y))
      Baz a b -> case Baz (frst (f a)) (scnd (f b)) of
          Bar x -> g x
          Baz x y -> Baz (frst (g x)) (scnd (g y))
= case v of
      Bar x -> g x
      Baz x y -> Baz (frst (g x)) (scnd (g y))
  where v = case m of
                Bar a -> f a
                Baz a b -> Baz (frst (f a)) (scnd (f b))
= case m >>= f of
      Bar x -> g x
      Baz x y -> Baz (frst (g x)) (scnd (g y))
= (m >>= f) >>= g
Run Code Online (Sandbox Code Playgroud)

编辑好吧,我决定写一个简短的解释,(Bool ->)即使没有人问,这是如何受到启发的.所以,回想一下:

instance Monad (e ->) where
    m >>= f = \e -> f (m e) e
Run Code Online (Sandbox Code Playgroud)

现在我们要定义

data Pair a = Pair a a
Run Code Online (Sandbox Code Playgroud)

并观察Bool -> a并且Pair a非常相似:

to :: Pair a -> (Bool -> a)
to (Pair false true) = \bool -> case bool of
    False -> false
    True  -> true

from :: (Bool -> a) -> Pair a
from f = Pair (f False) (f True)
Run Code Online (Sandbox Code Playgroud)

事实证明,from并且to是一个同构.换句话说:您可以将其Bool -> a视为"双元素容器".那么,如果我们尝试将(e ->)实例Monad转换为Pair类型会发生什么?它当然应该是可能的,因为它们是同构的.事实上,让我们从同构开始:

instance Monad Pair where
    return x = from (return x)
    m >>= f = from (to m >>= to . f)
Run Code Online (Sandbox Code Playgroud)

现在我们可以"转动曲柄":

  return x
= from (return x)
= from (\e -> x)
= Pair ((\e -> x) False) ((\e -> x) True)
= Pair x x
Run Code Online (Sandbox Code Playgroud)

和:

  m@(Pair false true) >>= f
= from (to m >>= to . f)
= from (\e -> (to . f) (to m e) e)
= from (\e -> to (f (to m e)) e)
= Pair (g False) (g True) where
      g = \e -> to (f (to m e)) e
= Pair (to (f (to m False)) False) (to (f (to m True)) True)
= Pair (case f (to m False) of Pair false true -> false)
       (case f (to m True ) of Pair false true -> true )
= Pair (case f false of Pair false true -> false)
       (case f true  of Pair false true -> true )
Run Code Online (Sandbox Code Playgroud)

因此,我们现在可以在不依赖于(Bool ->)复制和粘贴上述计算的第一行和最后一行的情况下重写实例:

frstPair (Pair false true) = false
scndPair (Pair false true) = true

instance Monad Pair where
    return x = Pair x x
    Pair false true >>= f = Pair (frstPair (f false)) (scndPair (f true))
Run Code Online (Sandbox Code Playgroud)

希望你能够认识到这与(>>=)我上面给出的定义有多么相似Foo.

编辑2另一个(不同!)monad是可能的.从基数检查同构类型的行为:

type Foo = Product Identity Maybe
Run Code Online (Sandbox Code Playgroud)

请参阅文档Product.写没有同构,它将是:

instance Monad Foo where
    return x = Baz x x
    Bar x >>= f = Bar (frst (f x))
    Baz x y >>= f = case f y of
        Bar a -> Bar (frst (f x))
        Baz a b -> Baz (frst (f x)) b
Run Code Online (Sandbox Code Playgroud)

从某种意义上说,当我添加更多monadic动作时,我的原始提议"扩展"结果数量 - 从Barin 开始return并将Bars不可撤销地转换Baz为绑定中的s - 而此实例"收缩"可能的结果数量添加更多monadic动作 - 从Bazin 开始并在s中不可逆转地returnBazs 转换为Bars.如果你问我,这是一个有趣的设计选择!它也让我想知道是否有另一个Monad实例Product可能(可能对所涉及的仿函数有不同的约束).