Git Product home page Git Product logo

Comments (3)

vlopezj avatar vlopezj commented on August 28, 2024

This is a possible implementation (see commit).

instance (Alternative f, Monad w) => Monad (CofreeT f w) where
  return = CofreeT . return . (:< empty)
  {-# INLINE return #-}
  (CofreeT cx) >>= f = CofreeT $ do
    (a :< m) <- cx
    (b :< n) <- runCofreeT $ f a
    return $ b :< (n <|> fmap (>>= f) m)

instance (Alternative f, Applicative w) => Applicative (CofreeT f w) where
  pure = CofreeT . pure . (:< empty)
  {-# INLINE pure #-}
  (CofreeT wf) <*> aa@(CofreeT wa) = CofreeT $
    ( \(f :< t) ->
      \(a) ->
      let (b :< n) = bimap f (fmap f) a in
      b :< (n <|> fmap (<*> aa) t)) <$> wf <*> wa

Proofs of the relevant laws (not yet composition for Applicative) can be found here:

Requiring Alternative is perhaps too strong. The proofs are also valid for Functor.Plus in the "semigroupoids" package. The instances still work if (<|>) is replaced by (<!>) and
empty by zero.

from free.

ekmett avatar ekmett commented on August 28, 2024

Can you swap that around into a pull request?

I'm happy to go with the Alternative version for now, and if someone feels strongly, we can liberalize it later.

from free.

ekmett avatar ekmett commented on August 28, 2024

Merged. Closing.

from free.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.