ekmett / contravariant

Haskell 98 contravariant functors
http://hackage.haskell.org/package/contravariant
Other
73 stars 24 forks source link

Add Free Divisible functor #43

Open paf31 opened 6 years ago

paf31 commented 6 years ago

You can construct the free Divisible functor (free relative to the forgetful functor which only remembers the Contravariant instance) using contravariant Day convolution, just like you can construct the free Applicative using covariant Day convolution:

-- Free f ~ Coproduct Proxy (Day f (Free f))
data Free f a
  = Conquer
  | forall b c. Divide (a -> (b, c)) (f b) (Free f c)

So you can list out the primitive ways you would like to be able to consume values using some Contravariant functor, and then construct a Divisible functor from that. To consume an a using Free f a, you split a into a big tuple, and for each component of that tuple, either consume it using an f, or simply use conquer.

Maybe it's worth adding here?

I have a prototype implementation in PureScript here for reference.