Converting Monad notation to Arrow notation

后端 未结 2 790
灰色年华
灰色年华 2021-02-04 14:01

I\'m trying to understand arrow notation, in particularly how it works with Monads. With Monads I can define the following:

f = (*2)
g = Just 5 >>= (return         


        
2条回答
  •  忘了有多久
    2021-02-04 14:29

    First we need an arrow with the same semantics as the Maybe monad. We could define it from scratch, but the easiest way is to wrap the Maybe monad into Kleisli:

    type MaybeArrow = Kleisli Maybe
    

    Then we'll also need a way how to run this monad to extract the result:

    runMaybeArrow :: MaybeArrow () a -> Maybe a
    runMaybeArrow = flip runKleisli ()
    

    Also it'll be handy to have a way how to create a constant arrow from a given value (which just ignores its input):

    val :: (Arrow a) => c -> a b c
    val = arr . const
    

    And finally, we get:

    g' = runMaybeArrow (val 5 >>> arr f)
    

提交回复
热议问题