snoyberg / mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers
153 stars 63 forks source link

Automatically derive Mono instances for monomophised newtypes using TH #113

Open bollu opened 7 years ago

bollu commented 7 years ago

Hey,

It would be nice to be able to automatically generate mono-* instances for a newtype wrapper or a data declaration that simply monomorphises a polymorphic type.

For example, we could easy do

newtype ListOfInt = ListOfInt [Int]

for which we can clearly write a MonoFunctor instance.

I've done this for my cellular-automata-diagrams library. I'm using template haskell, but I'd be interested in trying this out with the DeriveGeneric functionality as well.

Example use: it automatically instantiates the type family Element, derives MonoFunctor and MonoComonad here in Cyclic2d.hs

I'd be happy to add this facility and send a PR for all typeclasses in the library, since it was something I sorely wanted. Do you think this would be useful?

snoyberg commented 7 years ago

Does GeneralizedNewtypeDeriving not work here?

bollu commented 7 years ago

Wow,

  1. Did now know that GeneralizedNewtypeDeriving had enough power to do that. It requires you to define what the Element of the type family is, which the TH code does automatically.
  2. I was planning to allow this for data declarations as well, which GeneralizedNewtypeDeriving does not help with (nor does DeriveAnyClass)
snoyberg commented 7 years ago

OK, then this addition sounds good. Could you put it in a separate module though in case we need to conditionally exclude it for platforms lacking TH support?

bollu commented 7 years ago

Sure thing. I'll start by branching off and gradually adding instances.

On Sun, 23 Oct 2016 at 14:25 Michael Snoyman notifications@github.com wrote:

OK, then this addition sounds good. Could you put it in a separate module though in case we need to conditionally exclude it for platforms lacking TH support?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/snoyberg/mono-traversable/issues/113#issuecomment-255576841, or mute the thread https://github.com/notifications/unsubscribe-auth/ABncjQcV0uKHvgbfniq9oR6luIBOEnq-ks5q2yD4gaJpZM4KeDla .

Sending this from my phone, please excuse any typos!