How do I use the Church encoding for Free Monads?

前端 未结 4 769
别跟我提以往
别跟我提以往 2021-02-08 10:49

I\'ve been using the Free datatype in Control.Monad.Free from the free package. Now I\'m trying to convert it to use F in

4条回答
  •  忘了有多久
    2021-02-08 11:13

    Your

    matchF
      :: Functor f
      => (a -> r)
      -> (f (F f a) -> r)
      -> F f a
      -> r
    

    looks like the Scott-encoded Free monad. The Church-encoded version is just

    matchF
      :: Functor f
      => (a -> r)
      -> (f r -> r)
      -> F f a
      -> r
    matchF kp kf f = runF f kp kf
    

    Here are Church- and Scott-encoded lists for comparison:

    newtype Church a = Church { runChurch :: forall r. (a -> r       -> r) -> r -> r }
    newtype Scott  a = Scott  { runScott  :: forall r. (a -> Scott a -> r) -> r -> r }
    

提交回复
热议问题