newtype-generics-0.5.3: A typeclass and set of functions for working with newtypes

Safe HaskellSafe
LanguageHaskell2010

Control.Newtype.Generics

Description

The Newtype typeclass and related functions. Primarily pulled from Conor McBride's Epigram work. Some examples:

>>> ala Sum foldMap [1,2,3,4]
10
>>> ala Endo foldMap [(+1), (+2), (subtract 1), (*2)] 3
8
>>> under2 Min (<>) 2 1
1
>>> over All not (All False)
All {getAll = True)

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. You can easily define new instances for your newtypes with the help of GHC.Generics

import GHC.Generics

(...)
newtype Example = Example Int
  deriving (Generic)

instance Newtype Example

This avoids the use of Template Haskell (TH) to get new instances.

Synopsis

Documentation

class Newtype n where #

As long as the type n is an instance of Generic, you can create an instance with just instance Newtype n

Minimal complete definition

Nothing

Associated Types

type O n :: * #

Methods

pack :: O n -> n #

pack :: (Generic n, GNewtype (Rep n), O n ~ GO (Rep n)) => O n -> n #

unpack :: n -> O n #

unpack :: (Generic n, GNewtype (Rep n), O n ~ GO (Rep n)) => n -> O n #

Instances
Newtype All # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O All :: Type #

Methods

pack :: O All -> All #

unpack :: All -> O All #

Newtype Any # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O Any :: Type #

Methods

pack :: O Any -> Any #

unpack :: Any -> O Any #

Newtype (Fixed a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Fixed a) :: Type #

Methods

pack :: O (Fixed a) -> Fixed a #

unpack :: Fixed a -> O (Fixed a) #

Newtype (Min a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Min a) :: Type #

Methods

pack :: O (Min a) -> Min a #

unpack :: Min a -> O (Min a) #

Newtype (Max a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Max a) :: Type #

Methods

pack :: O (Max a) -> Max a #

unpack :: Max a -> O (Max a) #

Newtype (First a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (First a) :: Type #

Methods

pack :: O (First a) -> First a #

unpack :: First a -> O (First a) #

Newtype (Last a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Last a) :: Type #

Methods

pack :: O (Last a) -> Last a #

unpack :: Last a -> O (Last a) #

Newtype (WrappedMonoid m) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (WrappedMonoid m) :: Type #

Newtype (Option a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Option a) :: Type #

Methods

pack :: O (Option a) -> Option a #

unpack :: Option a -> O (Option a) #

Newtype (ZipList a) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (ZipList a) :: Type #

Methods

pack :: O (ZipList a) -> ZipList a #

unpack :: ZipList a -> O (ZipList a) #

Newtype (Identity a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Identity a) :: Type #

Methods

pack :: O (Identity a) -> Identity a #

unpack :: Identity a -> O (Identity a) #

Newtype (First a) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (First a) :: Type #

Methods

pack :: O (First a) -> First a #

unpack :: First a -> O (First a) #

Newtype (Last a) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Last a) :: Type #

Methods

pack :: O (Last a) -> Last a #

unpack :: Last a -> O (Last a) #

Newtype (Dual a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Dual a) :: Type #

Methods

pack :: O (Dual a) -> Dual a #

unpack :: Dual a -> O (Dual a) #

Newtype (Endo a) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Endo a) :: Type #

Methods

pack :: O (Endo a) -> Endo a #

unpack :: Endo a -> O (Endo a) #

Newtype (Sum a) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Sum a) :: Type #

Methods

pack :: O (Sum a) -> Sum a #

unpack :: Sum a -> O (Sum a) #

Newtype (Product a) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Product a) :: Type #

Methods

pack :: O (Product a) -> Product a #

unpack :: Product a -> O (Product a) #

Newtype (Down a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Down a) :: Type #

Methods

pack :: O (Down a) -> Down a #

unpack :: Down a -> O (Down a) #

Newtype (WrappedMonad m a) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (WrappedMonad m a) :: Type #

Methods

pack :: O (WrappedMonad m a) -> WrappedMonad m a #

unpack :: WrappedMonad m a -> O (WrappedMonad m a) #

Newtype (ArrowMonad a b) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (ArrowMonad a b) :: Type #

Methods

pack :: O (ArrowMonad a b) -> ArrowMonad a b #

unpack :: ArrowMonad a b -> O (ArrowMonad a b) #

Newtype (WrappedArrow a b c) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (WrappedArrow a b c) :: Type #

Methods

pack :: O (WrappedArrow a b c) -> WrappedArrow a b c #

unpack :: WrappedArrow a b c -> O (WrappedArrow a b c) #

Newtype (Kleisli m a b) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Kleisli m a b) :: Type #

Methods

pack :: O (Kleisli m a b) -> Kleisli m a b #

unpack :: Kleisli m a b -> O (Kleisli m a b) #

Newtype (Const a x) # 
Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Const a x) :: Type #

Methods

pack :: O (Const a x) -> Const a x #

unpack :: Const a x -> O (Const a x) #

Newtype (Alt f a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Alt f a) :: Type #

Methods

pack :: O (Alt f a) -> Alt f a #

unpack :: Alt f a -> O (Alt f a) #

Newtype (Compose f g a) #

Since: 0.5.1

Instance details

Defined in Control.Newtype.Generics

Associated Types

type O (Compose f g a) :: Type #

Methods

pack :: O (Compose f g a) -> Compose f g a #

unpack :: Compose f g a -> O (Compose f g a) #

op :: (Newtype n, o ~ O n) => (o -> n) -> n -> o #

This function serves two purposes:

  1. Giving you the unpack of a newtype without you needing to remember the name.
  2. Showing that the first parameter is completely ignored on the value level, meaning the only reason you pass in the constructor is to provide type information. Typeclasses sure are neat.
>>> op Identity (Identity 3)
3

ala :: (Newtype n, Newtype n', o' ~ O n', o ~ O n) => (o -> n) -> ((o -> n) -> b -> n') -> b -> o' #

The workhorse of the package. Given a "packer" and a "higher order function" (hof), it handles the packing and unpacking, and just sends you back a regular old function, with the type varying based on the hof you passed.

The reason for the signature of the hof is due to ala not caring about structure. To illustrate why this is important, consider this alternative implementation of under2:

under2 :: (Newtype n, Newtype n', o' ~ O n', o ~ O n)
       => (o -> n) -> (n -> n -> n') -> (o -> o -> o')
under2' pa f o0 o1 = ala pa (\p -> uncurry f . bimap p p) (o0, o1)

Being handed the "packer", the hof may apply it in any structure of its choosing – in this case a tuple.

>>> ala Sum foldMap [1,2,3,4]
10

ala' :: (Newtype n, Newtype n', o' ~ O n', o ~ O n) => (o -> n) -> ((a -> n) -> b -> n') -> (a -> o) -> b -> o' #

This is the original function seen in Conor McBride's work. The way it differs from the ala function in this package, is that it provides an extra hook into the "packer" passed to the hof. However, this normally ends up being id, so ala wraps this function and passes id as the final parameter by default. If you want the convenience of being able to hook right into the hof, you may use this function.

>>> ala' Sum foldMap length ["hello", "world"]
10
>>> ala' First foldMap (readMaybe @Int) ["x", "42", "1"]
Just 42

under :: (Newtype n, Newtype n', o' ~ O n', o ~ O n) => (o -> n) -> (n -> n') -> o -> o' #

A very simple operation involving running the function 'under' the newtype.

>>> under Product (stimes 3) 3
27

over :: (Newtype n, Newtype n', o' ~ O n', o ~ O n) => (o -> n) -> (o -> o') -> n -> n' #

The opposite of under. I.e., take a function which works on the underlying types, and switch it to a function that works on the newtypes.

>>> over All not (All False)
All {getAll = True}

under2 :: (Newtype n, Newtype n', o' ~ O n', o ~ O n) => (o -> n) -> (n -> n -> n') -> o -> o -> o' #

Lower a binary function to operate on the underlying values.

>>> under2 Any (<>) True False
True

Since: 0.5.2

over2 :: (Newtype n, Newtype n', o' ~ O n', o ~ O n) => (o -> n) -> (o -> o -> o') -> n -> n -> n' #

The opposite of under2.

Since: 0.5.2

underF :: (Newtype n, Newtype n', o' ~ O n', o ~ O n, Functor f, Functor g) => (o -> n) -> (f n -> g n') -> f o -> g o' #

under lifted into a Functor.

overF :: (Newtype n, Newtype n', o' ~ O n', o ~ O n, Functor f, Functor g) => (o -> n) -> (f o -> g o') -> f n -> g n' #

over lifted into a Functor.