Data.Functor.Classes

transformers-0.5.2.0: Concrete functor and monad transformers

Copyright | (c) Ross Paterson 2013
License | BSD-style (see the file LICENSE)
Maintainer | R.Paterson@city.ac.uk
Stability | experimental
Portability | portable
Safe Haskell | Safe
Language | Haskell98

Data.Functor.Classes

Contents

- Liftings of Prelude classes
  - For unary constructors
  - For binary constructors
- Helper functions
  - Obsolete helpers

Description

Liftings of the Prelude classes `Eq`, `Ord`, `Read` and `Show` to unary and binary type constructors.

These classes are needed to express the constraints on arguments of transformers in portable Haskell. Thus for a new transformer `T`, one might write instances like

    instance (Eq1 f) => Eq1 (T f) where ...
    instance (Ord1 f) => Ord1 (T f) where ...
    instance (Read1 f) => Read1 (T f) where ...
    instance (Show1 f) => Show1 (T f) where ...

If these instances can be defined, defining instances of the base classes is mechanical:

    instance (Eq1 f, Eq a) => Eq (T f a) where (==) = eq1
    instance (Ord1 f, Ord a) => Ord (T f a) where compare = compare1
    instance (Read1 f, Read a) => Read (T f a) where readsPrec = readsPrec1
    instance (Show1 f, Show a) => Show (T f a) where showsPrec = showsPrec1

Synopsis

- class Eq1 f where
  - liftEq :: (a -> b -> Bool) -> f a -> f b -> Bool
- eq1 :: (Eq1 f, Eq a) => f a -> f a -> Bool
- class Eq1 f => Ord1 f where
  - liftCompare :: (a -> b -> Ordering) -> f a -> f b -> Ordering
- compare1 :: (Ord1 f, Ord a) => f a -> f a -> Ordering
- class Read1 f where
  - liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
  - liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
- readsPrec1 :: (Read1 f, Read a) => Int -> ReadS (f a)
- class Show1 f where
  - liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
  - liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
- showsPrec1 :: (Show1 f, Show a) => Int -> f a -> ShowS
- class Eq2 f where
  - liftEq2 :: (a -> b -> Bool) -> (c -> d -> Bool) -> f a c -> f b d -> Bool
- eq2 :: (Eq2 f, Eq a, Eq b) => f a b -> f a b -> Bool
- class Eq2 f => Ord2 f where
  - liftCompare2 :: (a -> b -> Ordering) -> (c -> d -> Ordering) -> f a c -> f b d -> Ordering
- compare2 :: (Ord2 f, Ord a, Ord b) => f a b -> f a b -> Ordering
- class Read2 f where
  - liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (f a b)
  - liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [f a b]
- readsPrec2 :: (Read2 f, Read a, Read b) => Int -> ReadS (f a b)
- class Show2 f where
  - liftShowsPrec2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> Int -> f a b -> ShowS
  - liftShowList2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> [f a b] -> ShowS
- showsPrec2 :: (Show2 f, Show a, Show b) => Int -> f a b -> ShowS
- readsData :: (String -> ReadS a) -> Int -> ReadS a
- readsUnaryWith :: (Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
- readsBinaryWith :: (Int -> ReadS a) -> (Int -> ReadS b) -> String -> (a -> b -> t) -> String -> ReadS t
- showsUnaryWith :: (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
- showsBinaryWith :: (Int -> a -> ShowS) -> (Int -> b -> ShowS) -> String -> Int -> a -> b -> ShowS
- readsUnary :: Read a => String -> (a -> t) -> String -> ReadS t
- readsUnary1 :: (Read1 f, Read a) => String -> (f a -> t) -> String -> ReadS t
- readsBinary1 :: (Read1 f, Read1 g, Read a) => String -> (f a -> g a -> t) -> String -> ReadS t
- showsUnary :: Show a => String -> Int -> a -> ShowS
- showsUnary1 :: (Show1 f, Show a) => String -> Int -> f a -> ShowS
- showsBinary1 :: (Show1 f, Show1 g, Show a) => String -> Int -> f a -> g a -> ShowS

# Liftings of Prelude classes

## For unary constructors

class Eq1 f where

Lifting of the `Eq` class to unary type constructors.

Methods

liftEq :: (a -> b -> Bool) -> f a -> f b -> Bool

Lift an equality test through the type constructor.

The function will usually be applied to an equality function, but the more general type ensures that the implementation uses it to compare elements of the first container with elements of the second.

Instances

Eq1 [] |
Eq1 Identity |
Eq1 Maybe |
Eq a => Eq1 (Either a) |
Eq a => Eq1 ((,) a) |
Eq a => Eq1 (Const a) |
Eq1 f => Eq1 (Lift f) |
Eq1 m => Eq1 (ListT m) |
Eq1 m => Eq1 (MaybeT m) |
Eq a => Eq1 (Constant * a) |
(Eq e, Eq1 m) => Eq1 (ExceptT e m) |
(Eq e, Eq1 m) => Eq1 (ErrorT e m) |
Eq1 f => Eq1 (IdentityT * f) |
(Eq w, Eq1 m) => Eq1 (WriterT w m) |
(Eq w, Eq1 m) => Eq1 (WriterT w m) |
Eq1 f => Eq1 (Backwards * f) |
Eq1 f => Eq1 (Reverse * f) |
(Eq1 f, Eq1 g) => Eq1 (Product * f g) |
(Eq1 f, Eq1 g) => Eq1 (Sum * f g) |
(Eq1 f, Eq1 g) => Eq1 (Compose * * f g) |

eq1 :: (Eq1 f, Eq a) => f a -> f a -> Bool

Lift the standard `(`==`)` function through the type constructor.

class Eq1 f => Ord1 f where

Lifting of the `Ord` class to unary type constructors.

Methods

liftCompare :: (a -> b -> Ordering) -> f a -> f b -> Ordering

Lift a `compare` function through the type constructor.

The function will usually be applied to a comparison function, but the more general type ensures that the implementation uses it to compare elements of the first container with elements of the second.

Instances

Ord1 [] |
Ord1 Identity |
Ord1 Maybe |
Ord a => Ord1 (Either a) |
Ord a => Ord1 ((,) a) |
Ord a => Ord1 (Const a) |
Ord1 f => Ord1 (Lift f) |
Ord1 m => Ord1 (ListT m) |
Ord1 m => Ord1 (MaybeT m) |
Ord a => Ord1 (Constant * a) |
(Ord e, Ord1 m) => Ord1 (ExceptT e m) |
(Ord e, Ord1 m) => Ord1 (ErrorT e m) |
Ord1 f => Ord1 (IdentityT * f) |
(Ord w, Ord1 m) => Ord1 (WriterT w m) |
(Ord w, Ord1 m) => Ord1 (WriterT w m) |
Ord1 f => Ord1 (Backwards * f) |
Ord1 f => Ord1 (Reverse * f) |
(Ord1 f, Ord1 g) => Ord1 (Product * f g) |
(Ord1 f, Ord1 g) => Ord1 (Sum * f g) |
(Ord1 f, Ord1 g) => Ord1 (Compose * * f g) |

compare1 :: (Ord1 f, Ord a) => f a -> f a -> Ordering

Lift the standard `compare` function through the type constructor.

class Read1 f where

Lifting of the `Read` class to unary type constructors.

Minimal complete definition

liftReadsPrec

Methods

liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)

`readsPrec` function for an application of the type constructor based on `readsPrec` and `readList` functions for the argument type.

liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [f a]

`readList` function for an application of the type constructor based on `readsPrec` and `readList` functions for the argument type. The default implementation using standard list syntax is correct for most types.

Instances

Read1 [] |
Read1 Identity |
Read1 Maybe |
Read a => Read1 (Either a) |
Read a => Read1 ((,) a) |
Read a => Read1 (Const a) |
Read1 f => Read1 (Lift f) |
Read1 m => Read1 (ListT m) |
Read1 m => Read1 (MaybeT m) |
Read a => Read1 (Constant * a) |
(Read e, Read1 m) => Read1 (ExceptT e m) |
(Read e, Read1 m) => Read1 (ErrorT e m) |
Read1 f => Read1 (IdentityT * f) |
(Read w, Read1 m) => Read1 (WriterT w m) |
(Read w, Read1 m) => Read1 (WriterT w m) |
Read1 f => Read1 (Backwards * f) |
Read1 f => Read1 (Reverse * f) |
(Read1 f, Read1 g) => Read1 (Product * f g) |
(Read1 f, Read1 g) => Read1 (Sum * f g) |
(Read1 f, Read1 g) => Read1 (Compose * * f g) |

readsPrec1 :: (Read1 f, Read a) => Int -> ReadS (f a)

Lift the standard `readsPrec` and `readList` functions through the type constructor.

class Show1 f where

Lifting of the `Show` class to unary type constructors.

Minimal complete definition

liftShowsPrec

Methods

liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS

`showsPrec` function for an application of the type constructor based on `showsPrec` and `showList` functions for the argument type.

liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS

`showList` function for an application of the type constructor based on `showsPrec` and `showList` functions for the argument type. The default implementation using standard list syntax is correct for most types.

Instances

Show1 [] |
Show1 Identity |
Show1 Maybe |
Show a => Show1 (Either a) |
Show a => Show1 ((,) a) |
Show a => Show1 (Const a) |
Show1 f => Show1 (Lift f) |
Show1 m => Show1 (ListT m) |
Show1 m => Show1 (MaybeT m) |
Show a => Show1 (Constant * a) |
(Show e, Show1 m) => Show1 (ExceptT e m) |
(Show e, Show1 m) => Show1 (ErrorT e m) |
Show1 f => Show1 (IdentityT * f) |
(Show w, Show1 m) => Show1 (WriterT w m) |
(Show w, Show1 m) => Show1 (WriterT w m) |
Show1 f => Show1 (Backwards * f) |
Show1 f => Show1 (Reverse * f) |
(Show1 f, Show1 g) => Show1 (Product * f g) |
(Show1 f, Show1 g) => Show1 (Sum * f g) |
(Show1 f, Show1 g) => Show1 (Compose * * f g) |

showsPrec1 :: (Show1 f, Show a) => Int -> f a -> ShowS

Lift the standard `showsPrec` and `showList` functions through the type constructor.

## For binary constructors

class Eq2 f where

Lifting of the `Eq` class to binary type constructors.

Methods

liftEq2 :: (a -> b -> Bool) -> (c -> d -> Bool) -> f a c -> f b d -> Bool

Lift equality tests through the type constructor.

The function will usually be applied to equality functions, but the more general type ensures that the implementation uses them to compare elements of the first container with elements of the second.

Instances

Eq2 Either |
Eq2 (,) |
Eq2 Const |
Eq2 (Constant *) |

eq2 :: (Eq2 f, Eq a, Eq b) => f a b -> f a b -> Bool

Lift the standard `(`==`)` function through the type constructor.

class Eq2 f => Ord2 f where

Lifting of the `Ord` class to binary type constructors.

Methods

liftCompare2 :: (a -> b -> Ordering) -> (c -> d -> Ordering) -> f a c -> f b d -> Ordering

Lift `compare` functions through the type constructor.

The function will usually be applied to comparison functions, but the more general type ensures that the implementation uses them to compare elements of the first container with elements of the second.

Instances

Ord2 Either |
Ord2 (,) |
Ord2 Const |
Ord2 (Constant *) |

compare2 :: (Ord2 f, Ord a, Ord b) => f a b -> f a b -> Ordering

Lift the standard `compare` function through the type constructor.

class Read2 f where

Lifting of the `Read` class to binary type constructors.

Minimal complete definition

liftReadsPrec2

Methods

liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (f a b)

`readsPrec` function for an application of the type constructor based on `readsPrec` and `readList` functions for the argument types.

liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [f a b]

`readList` function for an application of the type constructor based on `readsPrec` and `readList` functions for the argument types. The default implementation using standard list syntax is correct for most types.

Instances

Read2 Either |
Read2 (,) |
Read2 Const |
Read2 (Constant *) |

readsPrec2 :: (Read2 f, Read a, Read b) => Int -> ReadS (f a b)

Lift the standard `readsPrec` function through the type constructor.

class Show2 f where

Lifting of the `Show` class to binary type constructors.

Minimal complete definition

liftShowsPrec2

Methods

liftShowsPrec2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> Int -> f a b -> ShowS

`showsPrec` function for an application of the type constructor based on `showsPrec` and `showList` functions for the argument types.

liftShowList2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> [f a b] -> ShowS

`showList` function for an application of the type constructor based on `showsPrec` and `showList` functions for the argument types. The default implementation using standard list syntax is correct for most types.

Instances

Show2 Either |
Show2 (,) |
Show2 Const |
Show2 (Constant *) |

showsPrec2 :: (Show2 f, Show a, Show b) => Int -> f a b -> ShowS

Lift the standard `showsPrec` function through the type constructor.

# Helper functions

These functions can be used to assemble `Read` and `Show` instances for new algebraic types. For example, given the definition

    data T f a = Zero a | One (f a) | Two a (f a)

a standard `Read1` instance may be defined as

    instance (Read1 f) => Read1 (T f) where
        liftReadsPrec rp rl = readsData $
            readsUnaryWith rp "Zero" Zero `mappend`
            readsUnaryWith (liftReadsPrec rp rl) "One" One `mappend`
            readsBinaryWith rp (liftReadsPrec rp rl) "Two" Two

and the corresponding `Show1` instance as

    instance (Show1 f) => Show1 (T f) where
        liftShowsPrec sp _ d (Zero x) =
            showsUnaryWith sp "Zero" d x
        liftShowsPrec sp sl d (One x) =
            showsUnaryWith (liftShowsPrec sp sl) "One" d x
        liftShowsPrec sp sl d (Two x y) =
            showsBinaryWith sp (liftShowsPrec sp sl) "Two" d x y

readsData :: (String -> ReadS a) -> Int -> ReadS a

``readsData` p d` is a parser for datatypes where each alternative begins with a data constructor. It parses the constructor and passes it to `p`. Parsers for various constructors can be constructed with `readsUnary`, `readsUnary1` and `readsBinary1`, and combined with `mappend` from the `Monoid` class.

readsUnaryWith :: (Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t

``readsUnaryWith` rp n c n'` matches the name of a unary data constructor and then parses its argument using `rp`.

readsBinaryWith :: (Int -> ReadS a) -> (Int -> ReadS b) -> String -> (a -> b -> t) -> String -> ReadS t

``readsBinaryWith` rp1 rp2 n c n'` matches the name of a binary data constructor and then parses its arguments using `rp1` and `rp2` respectively.

showsUnaryWith :: (Int -> a -> ShowS) -> String -> Int -> a -> ShowS

``showsUnaryWith` sp n d x` produces the string representation of a unary data constructor with name `n` and argument `x`, in precedence context `d`.

showsBinaryWith :: (Int -> a -> ShowS) -> (Int -> b -> ShowS) -> String -> Int -> a -> b -> ShowS

``showsBinaryWith` sp1 sp2 n d x y` produces the string representation of a binary data constructor with name `n` and arguments `x` and `y`, in precedence context `d`.

## Obsolete helpers

readsUnary :: Read a => String -> (a -> t) -> String -> ReadS t

Deprecated: Use readsUnaryWith to define liftReadsPrec

``readsUnary` n c n'` matches the name of a unary data constructor and then parses its argument using `readsPrec`.

readsUnary1 :: (Read1 f, Read a) => String -> (f a -> t) -> String -> ReadS t

Deprecated: Use readsUnaryWith to define liftReadsPrec

``readsUnary1` n c n'` matches the name of a unary data constructor and then parses its argument using `readsPrec1`.

readsBinary1 :: (Read1 f, Read1 g, Read a) => String -> (f a -> g a -> t) -> String -> ReadS t

Deprecated: Use readsBinaryWith to define liftReadsPrec

``readsBinary1` n c n'` matches the name of a binary data constructor and then parses its arguments using `readsPrec1`.

showsUnary :: Show a => String -> Int -> a -> ShowS

Deprecated: Use showsUnaryWith to define liftShowsPrec

``showsUnary` n d x` produces the string representation of a unary data constructor with name `n` and argument `x`, in precedence context `d`.

showsUnary1 :: (Show1 f, Show a) => String -> Int -> f a -> ShowS

Deprecated: Use showsUnaryWith to define liftShowsPrec

``showsUnary1` n d x` produces the string representation of a unary data constructor with name `n` and argument `x`, in precedence context `d`.

showsBinary1 :: (Show1 f, Show1 g, Show a) => String -> Int -> f a -> g a -> ShowS

Deprecated: Use showsBinaryWith to define liftShowsPrec

``showsBinary1` n d x y` produces the string representation of a binary data constructor with name `n` and arguments `x` and `y`, in precedence context `d`.