Git Product home page Git Product logo

Comments (5)

Zemyla avatar Zemyla commented on July 30, 2024

You may actually want an Adjunction. I've been experimenting with something like:

data Mealy a b where
  Mealy :: (Applicative u, Adjunction f u) => f (u (a -> f b)) -> Mealy a b

This allows you to do things like define arr as follows:

arr = (coerce :: (Identity (Identity (a -> Identity b)) -> Mealy a b) -> (a -> b) -> Mealy a b) Mealy

The Applicative instance is required because (a) Distributive really should have Applicative as a superclass, and (b) so Choice can be defined fairly easily:

instance Choice Mealy where
  left' (Mealy fu) = Mealy $ fmap (liftA2 (\fr fl -> either (fmap Left . fl) (fmap Right . fr)) $ distribute unit) fu
  right' (Mealy fu) = Mealy $ fmap (liftA2 (\fl fr -> either (fmap Left . fl) (fmap Right . fr)) $ distribute unit) fu

You don't technically need Applicative, though, as if you have an Adjunction, you can define a liftA2 analogue:

liftAdj2 :: Adjunction f u => (a -> b -> c) -> u a -> u b -> u c
liftAdj2 f ua ub = leftAdjunct (\fab -> f (rightAdjunct fst fab) (rightAdjunct snd fab)) (ua, ub)

But as mentioned before, every sensible Distributive/Representable instance should have an Applicative instance as well.

from machines.

ekmett avatar ekmett commented on July 30, 2024

NB: Adjunction f g is equivalent to Representable g.

It isn't until you allow adjunctions to spread to other categories than just Hask -> Hask that anything extra is gained.

from machines.

ekmett avatar ekmett commented on July 30, 2024

Using Representable instead means we can unpack the f = (,) (Rep g) in the Mealy constructor. Adjunction holds out hope that it might be something exotic, which shrinks the Identity case by 8 bytes in exchange for making the common case have to follow a pointer indirection.

from machines.

ekmett avatar ekmett commented on July 30, 2024

Interestingly it is possible to implement Applicative using Distributive, it's just messy and requires partial functions under the hood. I do agree that it should have at least that as a superclass, and if Compose was a nicer cultural fit, I'd want Monad as well, but alas.

from machines.

treeowl avatar treeowl commented on July 30, 2024

You mean with

fs <*> xs = (\(Pair (Left f) (Right x)) <$>
                     distribute (Pair (fmap Left fs) (fmap Right xs))

or something like that? I guess it uses parametricity to ensure totality.
Pretty horrible all right. The unsafeCoerce version is even uglier, but presumably more efficient.

from machines.

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.