newtype-generics

A typeclass and set of functions for working with newtypes, with generics support.

Version on this page:0.5.1
LTS Haskell 22.13:0.6.2@rev:2
Stackage Nightly 2023-12-26:0.6.2@rev:2
Latest on Hackage:0.6.2@rev:2

See all snapshots newtype-generics appears in

BSD-3-Clause licensed by Darius Jahandarie, Conor McBride, João Cristóvão
Maintained by Simon Jakobi
This version can be pinned in stack with:newtype-generics-0.5.1@sha256:8be6c626b856a834a1ef5a407a6822949c08888fcfde807c9254ac9d85094c06,1774

Module documentation for 0.5.1

Depends on 2 packages(full list with versions):
Used by 2 packages in nightly-2017-10-28(full list with versions):

newtype-generics

A typeclass and set of functions for working with newtypes. Fork of the code published by Darius Jahandarie here, with the addition of generics.

The ‘Newtype’ typeclass and related functions: op, ala, ala', under. Primarly pulled from Conor McBride’s Epigram work. Some examples:

-- foldMaps the list ala the Sum newtype. This results in 10.
ala Sum foldMap [1,2,3,4] 

-- foldMaps the list ala the Product newtype. This results in 24.
ala Product foldMap [1,2,3,4] 

-- foldMaps the list ala the Endo newtype. This results in 8.
ala Endo foldMap [(+1), (+2), (subtract 1), (*2)] 3 

NB: Data.Foldable.foldMap is a generalized mconcatMap which is a generalized concatMap.

This package includes Newtype instances for all the (non-GHC/foreign) newtypes in base (as seen in the examples). However, there are neat things you can do with this with /any/ newtype and you should definitely define your own ‘Newtype’ instances for the power of this library. For example, see ala Cont traverse, with the proper Newtype instance for Cont.

This could of course be eased with the addition of generics for version 0.3:

{-# LANGUAGE DeriveGeneric              #-}

import GHC.Generics
(...)
newtype Example = Example Int (deriving Generic)
instance Newtype Example

Changes

  • 0.5.1 - Add more instances from base
  • 0.5.0.1 - Compatibility with GHC 8.2.1
  • 0.5 - Relax types of underF and overF to allow different input & output funtors