diff options
author | RyanGlScott <ryan.gl.scott@gmail.com> | 2015-10-03 19:21:37 +0200 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2015-10-03 20:03:15 +0200 |
commit | 6cde981a8788b225819be28659caddc35b77972d (patch) | |
tree | f78cd8be5a0549a654e523345bbde48a80493120 /docs | |
parent | a96f1acc59f425062e6192b4cd2a19e1ef987f4a (diff) | |
download | haskell-6cde981a8788b225819be28659caddc35b77972d.tar.gz |
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: https://phabricator.haskell.org/D1239
GHC Trac Issues: #10868
Diffstat (limited to 'docs')
-rw-r--r-- | docs/users_guide/7.12.1-notes.rst | 3 | ||||
-rw-r--r-- | docs/users_guide/glasgow_exts.rst | 42 |
2 files changed, 45 insertions, 0 deletions
diff --git a/docs/users_guide/7.12.1-notes.rst b/docs/users_guide/7.12.1-notes.rst index 188daa9217..dc87c59c9e 100644 --- a/docs/users_guide/7.12.1-notes.rst +++ b/docs/users_guide/7.12.1-notes.rst @@ -74,6 +74,9 @@ Language - Due to a :ghc-ticket:`security issue <10826>`, Safe Haskell now forbids annotations in programs marked as ``-XSafe``. +- Generic instances can be derived for data types whose constructors have + arguments with certain unlifted types. See :ref:`generic-programming` for + more details. Compiler ~~~~~~~~ diff --git a/docs/users_guide/glasgow_exts.rst b/docs/users_guide/glasgow_exts.rst index e2dd28e9f7..bc9e0233f3 100644 --- a/docs/users_guide/glasgow_exts.rst +++ b/docs/users_guide/glasgow_exts.rst @@ -12004,6 +12004,48 @@ we show generic serialization: Typically this class will not be exported, as it only makes sense to have instances for the representation types. +Unlifted representation types +----------------------------- + +The data family ``URec`` is provided to enable generic programming over +datatypes with certain unlifted arguments. There are six instances corresponding +to common unlifted types: :: + + data family URec a p + + data instance URec (Ptr ()) p = UAddr { uAddr# :: Addr# } + data instance URec Char p = UChar { uChar# :: Char# } + data instance URec Double p = UDouble { uDouble# :: Double# } + data instance URec Int p = UInt { uInt# :: Int# } + data instance URec Float p = UFloat { uFloat# :: Float# } + data instance URec Word p = UWord { uWord# :: Word# } + +Six type synonyms are provided for convenience: :: + + type UAddr = URec (Ptr ()) + type UChar = URec Char + type UDouble = URec Double + type UFloat = URec Float + type UInt = URec Int + type UWord = URec Word + +As an example, this data declaration: :: + + data IntHash = IntHash Int# + deriving Generic + +results in the following ``Generic`` instance: :: + + instance Generic IntHash where + type Rep IntHash = + D1 D1IntHash + (C1 C1_0IntHash + (S1 NoSelector UInt)) + +A user could provide, for example, a ``GSerialize UInt`` instance so that a +``Serialize IntHash`` instance could be easily defined in terms of +``GSerialize``. + Generic defaults ---------------- |