2

List 单子有return x = [x]. 那么为什么在下面的例子中结果不是[(["a", "b"], [2, 3])]呢?

> pairs a b = do { x <- a; y <- b; return (x, y)}
> pairs ["a", "b"] [2,3]
[("a",2),("a",3),("b",2),("b",3)]
4

2 回答 2

5

让我们首先分析和重写函数pairs

pairs a b = do { x <- a; y <- b; return (x, y)}

因此,这里我们有一个单子。我们do用作语法糖。但是编译器将其重写为:

pairs a b = a >>= (\x -> b >>= (\y -> return (x, y)))

或者以更规范的形式:

pairs a b = (>>=) a (\x -> (>>=) b (\y -> return (x, y)))

现在列表 monad 定义为:

instance Monad [] where
    return x = [x]
    (>>=) xs f = concatMap f xs

所以我们写了:

pairs a b = concatMap (\x -> concatMap (\y -> [(x, y)]) b) a

因此,我们将两个列表aand作为输入b,然后我们执行一个concatMapon awith 作为函数(\x -> concatMap (\y -> [(x, y)]) b)。在该函数中,我们使用 as function执行另一个concatMapb\y -> [(x, y)]

因此,如果我们对此进行评估,pairs ["a", "b"] [2,3]我们会得到:

   pairs ["a", "b"] [2,3]
-> concatMap (\x -> concatMap (\y -> [(x, y)]) [2,3]) ["a", "b"]
-> concatMap (\y -> [("a", y)]) [2,3] ++ concatMap (\y -> [("b", y)]) [2,3]
-> [("a", 2)] ++ [("a", 3)] ++ [("b", 2)] ++ [("b", 3)]
-> [("a", 2), ("a", 3), ("b", 2), ("b", 3)]
于 2018-02-24T10:18:04.930 回答
2

一般来说,

pairs a b = do { x <- a; y <- b; return (x, y) }  
          = do { x <- a;
                    do { y <- b; 
                            do { return (x, y) }}}

意味着,在伪代码中,

pairs( a, b) {  for x in a do: 
                     for y in b do: 
                                 yield( (x, y) ); 
             }

无论对于特定"for ... in ... do""yield"monad 是什么意思。更正式地说,它是

          = a  >>= (\x ->
                    do { y <- b;                          -- a >>= k  ===
                            do { return (x, y) }})        -- join (k <$> a)
          = join ( (<$> a)                                --  ( a       :: m a
                   (\x ->                                 --    k       ::   a ->   m b
                    do { y <- b;                          --    k <$> a :: m       (m b) )
                            do { return (x, y) }}) )      --            :: m          b

((<$>)fmap) 的别名。

对于Identitymonad, wherereturn a = Identity ajoin (Identity (Identity a)) = Identity a, 确实是

pairs( {Identity, a}, {Identity, b}) {  x = a; 
                                        y = b; 
                                        yield( {Identity, {Pair, x, y}} ); 
                                     }

但是对于列表 monad,"for"意味着foreach, 因为return x = [x]join xs = concat xs:

-- join :: m (m a) -> m a
-- join :: [] ([] a) -> [] a
-- join :: [[a]] -> [a]
join = concat

所以,

join    [ [a1, a2, a3, ...],
          [b1, b2, b3, ...],
           .....
          [z1, z2, z3, ...] ]
=
        [  a1, a2, a3, ... ,
           b1, b2, b3, ... ,
            .....
           z1, z2, z3, ...  ]

一元绑定满足ma >>= k = join (fmap k ma)where ma :: m a, k :: a -> m bfor a Monad m。因此对于列表,其中fmap = map,我们有ma >>= k = join (fmap k ma) = concat (map k ma) = concatMap k ma

m >>= k  =  [ a,        = join [ k a,   = join [ [ a1, a2, ... ],  =  [  a1, a2, ... ,
              b,                 k b,            [ b1, b2, ... ],        b1, b2, ... ,
              c,                 k c,            [ c1, c2, ... ],        c1, c2, ... ,
              d,                 k d,            [ d1, d2, ... ],        d1, d2, ... ,
              e,                 k e,            [ e1, e2, ... ],        e1, e2, ... ,
             ... ] >>= k         ...  ]          ...............  ]      ...........   ]

这正是嵌套循环所做的。因此

pairs ["a",                                   -- for x in ["a", "b"] do:
       "b"] [2, 3]                            --          for y in [2, 3] do:
=                                             --                     yield (x,y)
      ["a", 
       "b"] >>= (\x-> join (fmap (\y -> return (x,y)) [2, 3]) )
=
      ["a", 
       "b"] >>= (\x-> concat (map (\y -> [ (x,y) ]) [2, 3]) )
=
join [ "a"  &   (\x-> concat ((\y -> [ (x,y) ]) `map` [2, 3]) ),      -- x & f = f x
       "b"  &   (\x-> concat ((\y -> [ (x,y) ]) `map` [2, 3]) ) ]     
=
join [              concat ((\y -> [ ("a",y) ]) `map` [2, 3])  ,    
                    concat ((\y -> [ ("b",y) ]) `map` [2, 3])   ]     
=
join [ concat [ [("a", 2)], [("a", 3)] ] ,    -- for y in [2, 3] do: yield ("a",y)
       concat [ [("b", 2)], [("b", 3)] ] ]    -- for y in [2, 3] do: yield ("b",y)
=
join [        [  ("a", 2) ,  ("a", 3)  ] ,
              [  ("b", 2) ,  ("b", 3)  ] ]
=
     [           ("a", 2) ,  ("a", 3)    ,
                 ("b", 2) ,  ("b", 3)    ]

循环展开是嵌套循环所做的 ⁄嵌套计算是 Monad 的本质。

同样有趣的是注意到

 join                =                      =   [a1] ++            =  [a1] ++ join
  [ [ a1, a2, ... ],    [ a1, a2, ... ] ++          [a2, ... ] ++         [ [a2, ...],
    [ b1, b2, ... ],    [ b1, b2, ... ] ++     [ b1, b2, ... ] ++      [ b1, b2, ...],
    [ c1, c2, ... ],    [ c1, c2, ... ] ++     [ c1, c2, ... ] ++      [ c1, c2, ...],
    [ d1, d2, ... ],    [ d1, d2, ... ] ++     [ d1, d2, ... ] ++      [ d1, d2, ...],
    [ e1, e2, ... ],    [ e1, e2, ... ] ++     [ e1, e2, ... ] ++      [ e1, e2, ...],
    ............... ]   ...............        ...............         ..............  ]

这触及了“嵌套循环 ⁄ 产出”类比的核心。单子是高阶幺半群,“有什么问题?”

于 2018-02-28T13:16:53.863 回答