ekmett / contravariant

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

backend for deriving #54

Closed fommil closed 6 years ago

fommil commented 6 years ago

I don't know what your plans are for this library but in scalaz we are going to use this machinery to power the equivalent of the deriving keyword (it involves a lot of macros to handle arbitrary arity, nothing spectacularly complex, just a lot of machinary).

It would be good if Haskell itself had a deriving backend that used Decidable / Alternative... is this something you're planning on doing?

This is something I'd like to do, in order to break into contributing to Haskell. I will be looking to start in about 6 months on my own time... unless you get there first!

ekmett commented 6 years ago

Not quite sure how such a beast would look.

I do have deciding in Data.Functor.Contravariant.Generic that lets you write things like:

gcompare :: Deciding Ord a => a -> a -> Ordering
gcompare = getComparison $ deciding (Proxy :: Proxy Ord) (Comparison compare)

and

geq :: Deciding Eq a => a -> a -> Bool
geq = getEquivalence $ deciding (Proxy :: Proxy Eq) (Equivalence (==))

so you can just write Generic instances for your data types and use those to fill in compare and (==) respectively, though. That might serve as a starting point for you.

fommil commented 6 years ago

oh interesting, this Deciding may be the same as scalaz.Derives, which can be derived from Decidable or Alternative. The idea is that the typeclass author just writes a regular Decidable or Alternative and then some macros fill in the extension to N-arity.

Derives stops at arity N=4, but I have a generic N-arity version called Derivez which also has an interface that users can implement directly. It's a little more complex for the typeclass author, but it can lead to a big performance boost.

I need a second version of these typeclasses to support things with labels (names of products or coproducts) because (e.g. divide) composition doesn't hold when you introduce metadata like that.

fommil commented 6 years ago

I'm not sure it is the same as https://hackage.haskell.org/package/contravariant-1.4.1/docs/Data-Functor-Contravariant-Generic.html but we're getting into generic programming where the concepts may not translate directly. scalaz.Derives is effectively just an invariant parent of Alternative and Decidable. e.g. typical method

  def xcoproduct2[Z, A1, A2](a1: =>F[A1], a2: =>F[A2])(
    f: A1 \/ A2 => Z,
    g: Z => A1 \/ A2
  ): F[Z]