List monad在这个例子中是如何工作的?

时间:2018-02-24 08:28:47

标签: list haskell monads

List monad有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)]

2 个答案:

答案 0 :(得分:5)

让我们首先分析并重写函数pairs a b = do { x <- a; y <- b; return (x, y)}

do

这里我们有一个monad。我们使用pairs a b = a >>= (\x -> b >>= (\y -> return (x, y))) 作为语法糖。但编译器将其重写为:

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

或者以更规范的形式:

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

现在列表monad被定义为:

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

所以我们写了:

a

因此,我们将两个列表bconcatMap作为输入,我们在a上使用函数(\x -> concatMap (\y -> [(x, y)]) b)执行concatMap。在该函数中,我们使用函数b\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)] 对此进行评估,我们会得到:

{{1}}

答案 1 :(得分:1)

一般来说,

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)的别名。

对于Identity monad,return 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, ...  ]

Monadic bind满足ma >>= k = join (fmap k ma) ma :: m a, k :: a -> m b 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, ...],
    ............... ]   ...............        ...............         ..............  ]

这是&#34;嵌套循环/产量&#34;的核心。比喻。 Monads是高阶幺半群,&#34;问题是什么?&#34;