Haskell Pipes and Branching

后端 未结 2 1046
轮回少年
轮回少年 2021-01-02 05:51

Problem

I\'m attempting to implement a simple web server with Haskell and the Pipes library. I understand now that cyclic or diamond topologies aren\'t possible wi

相关标签:
2条回答
  • 2021-01-02 06:18

    I have not run your code, but I think I spotted a problem.

    routeRequest'' = runProxyK $ routeRequest''' <-< unitU
    

    routeRequest''' is requesting data from unitU which has nothing to supply, so it hangs.

    :t runProxy $ unitU >-> printD
    

    Will type check but nothing runs.

    It seems like the data is being sent to the wrong level of the monad transformer, data which is flowing into routeRequest should be flowing into routeRequest'''. The data flowing into the wrong level of the monad transformer is what is probably causing you to need to leave of the type signature to get everything to type check. With the type signature routeRequest is expecting a () coming from upstream and, I bet, with no type signature it is allowed to be polymorphic.

    In your definition of routeRequest you could "close the pipe", I think that is what it is called, by using unitD which would disallow your construction even when routeRequest''' does not have the type signature.

    0 讨论(0)
  • 2021-01-02 06:19

    I was wrong when I originally said you could not handle diamond topologies. I later discovered a sensible way to do this using an ArrowChoice-like interface and included the solution in pipes-3.2.0 in the form of the leftD and rightD combinators. I'll explain how it works:

    Instead of nesting proxy transformers, you wrap the result with a Left or Right

    routeRequest ::
        (Monad m, Proxy p)
        => () -> Pipe p HTTPRequest (Either HTTPRequest HTTPRequest) m r
    routeRequest () = runIdentityP $ forever $ do
        httpReq <- request ()
        let method = getMethod httpReq
        let (URI uri) = getURI httpReq
        respond $ case method of
          GET  -> Left  httpReq
          POST -> Right httpReq
    

    Then you can selectively apply each handler to each branch and then merge the branches:

    routeRequest >-> leftD handleGET >-> rightD handlePOST >-> mapD (either id id)
        :: (Monad m, Proxy p) => () -> Pipe p HTTPRequest ByteString IO r
    

    If you have more than two branches then you will have to nest Eithers, but that is just a limitation of how ArrowChoice works.

    0 讨论(0)
提交回复
热议问题