Data.Functor.Classes (original) (raw)

Description

Liftings of the Prelude classes [Eq](Data-Eq.html#t:Eq "Data.Eq"), [Ord](Data-Ord.html#t:Ord "Data.Ord"), [Read](Text-Read.html#t:Read "Text.Read") and [Show](Text-Show.html#t:Show "Text.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 readPrec = readPrec1 readListPrec = readListPrecDefault instance (Show1 f, Show a) => Show (T f a) where showsPrec = showsPrec1

Since: 4.9.0.0

Liftings of Prelude classesFor unary constructors

class Eq1 f where Source #

Lifting of the [Eq](Data-Eq.html#t:Eq "Data.Eq") class to unary type constructors.

Since: 4.9.0.0

Methods

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

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.

Since: 4.9.0.0

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

Lift the standard (`[==](Data-Eq.html#v:-61--61- "Data.Eq")`) function through the type constructor.

Since: 4.9.0.0

class Eq1 f => Ord1 f where Source #

Lifting of the [Ord](Data-Ord.html#t:Ord "Data.Ord") class to unary type constructors.

Since: 4.9.0.0

Methods

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

Lift a [compare](Data-Ord.html#v:compare "Data.Ord") 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.

Since: 4.9.0.0

class Show1 f where Source #

Lifting of the [Show](Text-Show.html#t:Show "Text.Show") class to unary type constructors.

Since: 4.9.0.0

For binary constructors

class Eq2 f where Source #

Lifting of the [Eq](Data-Eq.html#t:Eq "Data.Eq") class to binary type constructors.

Since: 4.9.0.0

Methods

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

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.

Since: 4.9.0.0

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

Lift the standard (`[==](Data-Eq.html#v:-61--61- "Data.Eq")`) function through the type constructor.

Since: 4.9.0.0

class Eq2 f => Ord2 f where Source #

Lifting of the [Ord](Data-Ord.html#t:Ord "Data.Ord") class to binary type constructors.

Since: 4.9.0.0

Methods

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

Lift [compare](Data-Ord.html#v:compare "Data.Ord") 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.

Since: 4.9.0.0

class Show2 f where Source #

Lifting of the [Show](Text-Show.html#t:Show "Text.Show") class to binary type constructors.

Since: 4.9.0.0

Helper functions

These functions can be used to assemble [Read](Text-Read.html#t:Read "Text.Read") and [Show](Text-Show.html#t:Show "Text.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](Data-Functor-Classes.html#t:Read1 "Data.Functor.Classes") instance may be defined as

instance (Read1 f) => Read1 (T f) where liftReadPrec rp rl = readData $ readUnaryWith rp "Zero" Zero <|> readUnaryWith (liftReadPrec rp rl) "One" One <|> readBinaryWith rp (liftReadPrec rp rl) "Two" Two liftReadListPrec = liftReadListPrecDefault

and the corresponding [Show1](Data-Functor-Classes.html#t:Show1 "Data.Functor.Classes") 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

Obsolete helpers

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

Deprecated: Use showsUnaryWith to define liftShowsPrec

`[showsUnary](Data-Functor-Classes.html#v:showsUnary "Data.Functor.Classes")` n d x produces the string representation of a unary data constructor with name n and argument x, in precedence context d.

Since: 4.9.0.0

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

Deprecated: Use showsUnaryWith to define liftShowsPrec

`[showsUnary1](Data-Functor-Classes.html#v:showsUnary1 "Data.Functor.Classes")` n d x produces the string representation of a unary data constructor with name n and argument x, in precedence context d.

Since: 4.9.0.0

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

Deprecated: Use showsBinaryWith to define liftShowsPrec

`[showsBinary1](Data-Functor-Classes.html#v:showsBinary1 "Data.Functor.Classes")` 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.

Since: 4.9.0.0