transformers-0.4.3.0: Concrete functor and monad transformers

Copyright(c) Ross Paterson 2013
LicenseBSD-style (see the file LICENSE)
MaintainerR.Paterson@city.ac.uk
Stabilityexperimental
Portabilityportable
Safe HaskellSafe
LanguageHaskell98

Data.Functor.Classes

Contents

Description

Liftings of the Prelude classes Eq, Ord, Read and Show to unary 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) => Eq (T f a) where ...
instance (Ord1 f) => Ord (T f a) where ...
instance (Read1 f) => Read (T f a) where ...
instance (Show1 f) => Show (T f a) where ...

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

instance (Eq1 f) => Eq1 (T f) where eq1 = (==)
instance (Ord1 f) => Ord1 (T f) where compare1 = compare
instance (Read1 f) => Read1 (T f) where readsPrec1 = readsPrec
instance (Show1 f) => Show1 (T f) where showsPrec1 = showsPrec

Synopsis

Liftings of Prelude classes

class Eq1 f where

Lifting of the Eq class to unary type constructors.

Methods

eq1 :: Eq a => f a -> f a -> Bool

Instances

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

class Eq1 f => Ord1 f where

Lifting of the Ord class to unary type constructors.

Methods

compare1 :: Ord a => f a -> f a -> Ordering

Instances

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

class Read1 f where

Lifting of the Read class to unary type constructors.

Methods

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

Instances

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

class Show1 f where

Lifting of the Show class to unary type constructors.

Methods

showsPrec1 :: Show a => Int -> f a -> ShowS

Instances

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

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 (f a) (f a)

a standard Read instance may be defined as

instance (Read1 f, Read a) => Read (T f a) where
    readsPrec = readsData $
        readsUnary "Zero" Zero `mappend`
        readsUnary1 "One" One `mappend`
        readsBinary1 "Two" Two

and the corresponding Show instance as

instance (Show1 f, Show a) => Show (T f a) where
    showsPrec d (Zero x) = showsUnary "Zero" d x
    showsPrec d (One x) = showsUnary1 "One" d x
    showsPrec d (Two x y) = showsBinary1 "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.

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

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

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

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

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

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

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