i-am-tom/higgledy

Deriving `Show`, `Eq` and other instances for newtypes over `HKD a f`.

Closed this issue · 3 comments

Hey, Tom! Thanks for the lib, I really like the idea :)

The issue

While trying to write an utility using higgledy and ran into the fact that I can't really derive instances easily for a newtype that wraps an HKD a f. Taking Show as an example:

data Wrapper a = Wrapped a | Unwrapped
  deriving (Generic, Eq, Show)
  
newtype Foo a = Foo (HKD a Wrapper)
  deriving (Generic)
  deriving newtype (Show)

If I try to derive Show for the type Foo like above I get the following error:

• Could not deduce (Data.Generic.HKD.Types.GShow 'True (GHKD_ Wrapper (Rep a)))

Have you come across this limitation before? How have you solved it?

Possible solution for specific instances

The issue for Show seems to be that higgledy doesn't export the Data.Generic.HKD.Types.GShow class so it is not possible to add it to the constraints of a standalone instance declaration like so:

deriving instance
  ( Data.Generic.HKD.Types.GShow 'True (GHKD_ Update (Rep a)),
    Generic a
  ) =>
  Show (Changeset a)

Could GShow be exported? It seems to be standard practice (see GFromJSON for example).

Ideal solution

However, the above is just a band-aid, since other instances cannot be derived either.

I wonder if there is a generalized way to do newtype deriving for instances such as Show, Eq, Semigroup and Monoid?

Hey! Sorry, missed this entirely. You should just be able to replace all that noise with a Show (HKD a Update) - you don't need to access the internals :) This is actually an unfortunate consequence of instance resolution, and not one I've ever really been able to fix... In short, GHC eagerly expands all constraints, so you end up with the thing it got stuck on, rather than the thing it had to solve in the first place. Still, I hope this works for you!

Oh, really? Let me dig up the code here and give it a try!

It works! Thanks for the answer!