Comments (5)
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.
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.
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.
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.
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)
- Composing tees HOT 1
- Some types are too general HOT 10
- Categorical documentation
- Make it easy to write stateful machines HOT 10
- Random tiny things HOT 1
- Consider hand-simplifying `<*>`
- On merging Plan with Machine HOT 11
- machines-0.6 requires distributive < 0.5 HOT 4
- Refl from base HOT 5
- Runar's talk link is broken
- local in PlanT's MonadReader instance only works up to first yield/awaits
- pass and listen in PlanT's MonadWriter instance are probably invalid HOT 2
- Cochoice instance for Mealy HOT 4
- More complex examples HOT 1
- Applicative and Monad instances for Mealy are inconsistent HOT 1
- noncanonical monoid instance for mooreT HOT 2
- Extract Moore and Mealy into a dedicated package HOT 1
- effectful `takingJusts` HOT 2
- Stricter `foldlT`?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from machines.