Lysxia / generic-data

Generic data types in Haskell, utilities for GHC.Generics
https://hackage.haskell.org/package/generic-data
MIT License
44 stars 9 forks source link

Surgery with derivingVia #13

Closed Drezil closed 5 years ago

Drezil commented 5 years ago

We can derive many things using derivingVia and this package.

Is it possible to derive surgically altered things with deriving via? I could not get it compiling..

i.e. instead of

data T a = T { unT :: a }

instance (Show a) => Show (T a) where
  showsPrec n = gshowsPrec n . derecordify . toData

something like

data T a = T { unT :: a }
  deriving Show via Derecordify T a

latter yields the error

error:
    • Couldn't match representation of type ‘T a’
                               with that of ‘Derecordify T a’
        arising from the coercion of the method ‘showsPrec’
          from type ‘Int -> Derecordify T a -> ShowS’
            to type ‘Int -> T a -> ShowS’
    • When deriving the instance for (Show (T a))
    |
xxx |   deriving Show via Derecordify T a
    |            ^^^^

Is it just a missing instance Coercible (Derecordify a) a and Coercible a (Derecordify a)? Or is this harder to fix?

Lysxia commented 5 years ago

It's a bit more involved, we need to add a newtype for this, but nothing too challenging. It would look like

data T a = T { unT :: a }
  deriving Show via Surgery Derecordify (T a)

for some newtype Surgery and after making Derecordify a defunctionalized symbol (to be able to reuse its name).

Lysxia commented 5 years ago

How's #14 look?

Drezil commented 5 years ago

Cool! Thank you!

Lysxia commented 5 years ago

I just released 0.6.0.0!