Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#10868 closed feature request (fixed)

Make GHC generics capable of handling unboxed types

Reported by: RyanGlScott Owned by: RyanGlScott
Priority: normal Milestone: 8.0.1
Component: Compiler Version: 7.10.2
Keywords: Generics Cc: dreixel, kosmikus
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #8468 Differential Rev(s): Phab:D1239
Wiki Page:


Currently, GHC generics will not accept any unboxed constructor arguments:

$ ghci
GHCi, version 7.10.2:  :? for help
λ> :set -XMagicHash -XDeriveGeneric
λ> :m + GHC.Generics GHC.Exts
λ> data IntHash = IntHash Int# deriving Generic

    Can't make a derived instance of ‘Generic IntHash’:
      IntHash must not have unlifted or polymorphic arguments
    In the data declaration for ‘IntHash’

This makes GHC generics strictly less powerful than deriving clauses in some scenarios, as typeclasses like Eq, Ord, and Show are capable of special-casing certain unboxed types:

λ> :set -ddump-deriv
λ> data IntHash = IntHash Int# deriving Show

==================== Derived instances ====================
Derived instances:
  instance GHC.Show.Show Ghci1.IntHash where
    GHC.Show.showsPrec a_a1OJ (Ghci1.IntHash b1_a1OK)
      = GHC.Show.showParen
          ((a_a1OJ GHC.Classes.>= 11))
             (GHC.Show.showString "IntHash ")
             (GHC.Show.showsPrec 11 (GHC.Types.I# b1_a1OK)))
    GHC.Show.showList = GHC.Show.showList__ (GHC.Show.showsPrec 0)

As discussed on the GHC devs mailing list previously, Andres suggested adding new generic representation types to mark where Int#, Char#, etc. are used (suggesting the name UInt to represent Int#).

Change History (5)

comment:1 Changed 4 years ago by RyanGlScott

Differential Rev(s): Phab:D1239

comment:2 Changed 4 years ago by Ben Gamari <ben@…>

In 6cde981/ghc:

Make GHC generics capable of handling unboxed types

This adds a data family (`URec`) and six data family instances (`UAddr`,
`UChar`, `UDouble`, `UFloat`, `UInt`, and `UWord`) which a `deriving
Generic(1)` clause will generate if it sees `Addr#`, `Char#`, `Double#`,
`Float#`, `Int#`, or `Word#`, respectively. The programmer can then
provide instances for these data family instances to provide custom
implementations for unboxed types, similar to how derived `Eq`, `Ord`,
and `Show` instances currently special-case unboxed types.

Fixes #10868.

Test Plan: ./validate

Reviewers: goldfire, dreixel, bgamari, austin, hvr, kosmikus

Reviewed By: dreixel, kosmikus

Subscribers: simonpj, thomie

Differential Revision:

GHC Trac Issues: #10868

comment:3 Changed 4 years ago by bgamari

Resolution: fixed
Status: newclosed

comment:4 Changed 4 years ago by thomie

Milestone: 8.0.1

comment:5 Changed 4 years ago by RyanGlScott

Keywords: Generics added
Note: See TracTickets for help on using tickets.