generics-sop icon indicating copy to clipboard operation
generics-sop copied to clipboard

[feat] allow deriving via Generically

Open MangoIV opened this issue 2 years ago • 3 comments

  • [x] allow deriving via Generically as such

    data A = B | C 
      deriving stock GHC.Generic
      deriving Generic via Generically A
    
  • closes #153

  • [x] update on the documentation with a short section on deriving Generic image

  • [x] CI passes

MangoIV avatar Aug 30 '23 22:08 MangoIV

Somewhat unfortunately, datatypeInfo has the wrong type for being able to derive it via Generically:

class Generic a => HasDatatypeInfo a where
  -- | Type-level datatype info
  type DatatypeInfoOf a :: T.DatatypeInfo
  type DatatypeInfoOf a = GDatatypeInfoOf a

  -- | Term-level datatype info; by default, the term-level datatype info is produced
  -- from the type-level info.
  --
  datatypeInfo         :: proxy a -> DatatypeInfo (Code a)
  default datatypeInfo :: (GDatatypeInfo a, GCode a ~ Code a) => proxy a -> DatatypeInfo (Code a)
  datatypeInfo = gdatatypeInfo

The issue here is that because proxy is a type variable, we cannot assume that its parameter is representational or phantom, there are three solutions, all of which are breaking changes, the obvious one being specializing to Proxy or Proxy# which both have type role phantom on their parameter. Another option would be to specify a quantified Coercible constraint which would have the advantage to not fix the type but the disadvantage that it's basically incompatible with all ghc versions.

MangoIV avatar Aug 31 '23 13:08 MangoIV

another possibility without specifying the type and without quantified constraints is to directly have Coercible (proxy a) (proxy (Generically a)) which would hold for all the proxy types without role nominal and allow deriving via Generically, but this would nonetheless be a breaking change for people using proxies with a nominal parameter.

MangoIV avatar Aug 31 '23 13:08 MangoIV

oups, this is a duplicate of #159 ... perhaps we can merge these

MangoIV avatar Sep 01 '23 11:09 MangoIV