generic-data: Utilities for GHC.Generics

[ deriving, library, mit, other ] [ Propose Tags ]

This package provides common functions on generic types. See README.


[Skip to Readme]
Versions [faq] 0.1.0.0, 0.1.1.0, 0.2.0.0, 0.3.0.0, 0.4.0.0, 0.5.0.0, 0.6.0.0
Change log CHANGELOG.md
Dependencies base (>=4.9 && <5), base-orphans (>=0.8), contravariant, show-combinators [details]
License MIT
Copyright 2018 Li-yao Xia
Author Li-yao Xia
Maintainer lysxia@gmail.com
Category Other
Home page https://github.com/Lysxia/generic-data#readme
Source repo head: git clone https://github.com/Lysxia/generic-data
Uploaded by lyxia at Sun Dec 30 21:22:21 UTC 2018
Distributions LTSHaskell:0.3.0.0, NixOS:0.6.0.0, Stackage:0.6.0.0
Downloads 716 total (156 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2018-12-30 [all 1 reports]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for generic-data-0.4.0.0

[back to package description]

Generic data types in Haskell Hackage Build Status

Utilities for GHC.Generics.

Generic deriving for standard classes

{-# LANGUAGE DeriveGeneric #-}

-- base
import Data.Semigroup (Semigroup(..))
import GHC.Generics

-- generic-data
import Generic.Data (gmappend, Generically(..))
import Generic.Data.Orphans ()

data Foo a = Bar [a] [a] deriving Generic

instance Semigroup (Foo a) where
  (<>) = gmappend

-- also with some additional extensions --

{-# LANGUAGE
    DerivingStrategies,
    DerivingVia #-}  -- since GHC 8.6.1

data Foo a = Bar [a] [a]
  deriving Generic
  deriving Semigroup via (Generically (Foo a))

-- This example can be found in test/example.hs

Supported classes that GHC currently can't derive: Semigroup, Monoid, Applicative, Alternative, Eq1, Ord1, Show1.

Other classes from base are also supported, even though GHC can already derive them:

  • Eq, Ord, Enum, Bounded, Show (standard);
  • Functor, Foldable, Traversable (via extensions, DeriveFunctor, etc.).

(Read is currently not implemented.)

To derive type classes outside of the standard library, it might be worth taking a look at one-liner.

Type metadata

Extract type names, constructor names, number and arities of constructors, etc..

Type surgery

generic-data offers simple operations on generic representations.

More surgeries can be found in generic-data-surgery.

{-# LANGUAGE DeriveGeneric #-}

import GHC.Generic

import Generic.Data (gshowsPrec)
import Generic.Data.Microsurgery (unrecordify)

newtype T = T { unT :: Int } deriving Generic

-- Naively deriving Show would result in this being shown:
--
-- show (T 3) = "T {unT = 3}"
--
-- But instead, with a simple surgery, unrecordify, we can forget T was
-- declared as a record:
--
-- show (T 3) = "T 3"

instance Show T where
  showsPrec n = gshowsPrec n . unrecordify . toData

-- This example can be found in test/microsurgery.hs

Related links

generic-data aims to subsume generic deriving features of the following packages:

  • semigroups: generic Semigroup, Monoid, but with a heavy dependency footprint.
  • transformers-compat: generic Eq1, Ord1, Show1.
  • generic-deriving: doesn't derive the classes in base (defines clones of these classes as a toy example); has Template Haskell code to derive Generic.

Here are other relevant links.


Internal module policy

Modules under Generic.Data.Internal are not subject to any versioning policy. Breaking changes may apply to them at any time.

If something in those modules seems useful, please report it or create a pull request to export it from an external module.


All contributions are welcome. Open an issue or a pull request on Github!