Haskell管道:理解“平等”的逻辑

时间:2015-12-06 01:11:46

标签: haskell pipe

Pipes tutorial,它说:

 -- s :: Monad m =>      Producer a m ()  -- i.e. 'P.stdinLn'
 -- f :: Monad m => a -> Producer b m ()  -- i.e. 'duplicate'
 -- g :: Monad m => b -> Producer c m ()  -- i.e. '(lift . putStrLn)'    
 for (for s f) g = for s (\x -> for (f x) g)

加上这个:

 (~>) :: Monad m
      => (a -> Producer b m ())
      -> (b -> Producer c m ())
      -> (a -> Producer c m ())
 (f ~> g) x = for (f x) g

等于:

 f :: Monad m => a -> Producer b m ()
 g :: Monad m => b -> Producer c m ()
 h :: Monad m => c -> Producer d m ()
 (f ~> g) ~> h = f ~> (g ~> h)

对我来说毫无意义。请有人澄清一下吗?

1 个答案:

答案 0 :(得分:2)

从评论中得到提示,我已经弄明白了。谢谢大家。

首先:

  (f ~> g) x =       for (f x) g
  (f ~> g)   = \x -> for (f x) g
  (~>) f g   = \x -> for (f x) g

因此:

        for (for s f) g, s = e x
= \x -> for (for (e x) f) g
= \x -> for ((e ~> f) x) g
= (e ~> f) ~> g

        for s     (\x -> for (f x) g), s = e x
= \x -> for (e x) (\x -> for (f x) g)
= \x -> for (e x) (f ~> g)
= e ~> (f ~> g)