HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulebase-4.20.2.0Haskell2010

Data.Functor.Classes

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
  readPrec     = readPrec1
  readListPrec = readListPrecDefault
instance (Show1 f, Show a) => Show (T f a) where showsPrec = showsPrec1
  • 8 classes
  • 28 values
  • Packagebase-4.20.2.0
  • Exports36
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceClasses.hs

Liftings of Prelude classes

0 declarations

For unary constructors

classclass (forall a. Eq a => Eq (f a)) => Eq1 (f :: Type -> Type) where
#

Lifting of the Eq class to unary type constructors.

Any instance should be subject to the following law that canonicity is preserved:

liftEq (==) = (==)

This class therefore represents the generalization of Eq by decomposing its main method into a canonical lifting on a canonical inner method, so that the lifting can be reused for other arguments than the canonical one.

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.

Instances17Eq1, …
  • Eq1 ComplexDefined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    eq1 (1 :+ 2) (1 :+ 2)True
    Example1 expression
    eq1 (1 :+ 2) (1 :+ 3)False
  • Eq1 NonEmptyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Eq1 IdentityDefined in base-4.20.2.0 · Data.Functor.Classes
  • Eq1 DownDefined in base-4.20.2.0 · Data.Functor.Classes
  • Eq1 MaybeDefined in base-4.20.2.0 · Data.Functor.Classes
  • Eq1 SoloDefined in base-4.20.2.0 · Data.Functor.Classes
  • Eq1 []Defined in base-4.20.2.0 · Data.Functor.Classes
  • Eq1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Eq a => Eq1 (Either a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • Eq a => Eq1 (Tuple2 a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • Eq a => Eq1 (Const a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Generic1 f, Eq1 (Rep1 f)) => Eq1 (Generically1 f)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Eq a, Eq b) => Eq1 (Tuple3 a b)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Eq1 f, Eq1 g) => Eq1 (Product f g)Defined in base-4.20.2.0 · Data.Functor.Product
  • (Eq1 f, Eq1 g) => Eq1 (Sum f g)Defined in base-4.20.2.0 · Data.Functor.Sum
  • (Eq a, Eq b, Eq c) => Eq1 (Tuple4 a b c)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Eq1 f, Eq1 g) => Eq1 (Compose f g)Defined in base-4.20.2.0 · Data.Functor.Compose
valueeq1 :: (Eq1 f, Eq a) => f a -> f a -> Bool
#

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

classclass (Eq1 f, forall a. Ord a => Ord (f a)) => Ord1 (f :: Type -> Type) where
#

Lifting of the Ord class to unary type constructors.

Any instance should be subject to the following law that canonicity is preserved:

liftCompare compare = compare

This class therefore represents the generalization of Ord by decomposing its main method into a canonical lifting on a canonical inner method, so that the lifting can be reused for other arguments than the canonical one.

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.

Instances16Ord1, …
  • Ord1 NonEmptyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord1 IdentityDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord1 DownDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord1 MaybeDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord1 SoloDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord1 []Defined in base-4.20.2.0 · Data.Functor.Classes
  • Ord1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord a => Ord1 (Either a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • Ord a => Ord1 (Tuple2 a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • Ord a => Ord1 (Const a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Generic1 f, Ord1 (Rep1 f)) => Ord1 (Generically1 f)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Ord a, Ord b) => Ord1 (Tuple3 a b)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Ord1 f, Ord1 g) => Ord1 (Product f g)Defined in base-4.20.2.0 · Data.Functor.Product
  • (Ord1 f, Ord1 g) => Ord1 (Sum f g)Defined in base-4.20.2.0 · Data.Functor.Sum
  • (Ord a, Ord b, Ord c) => Ord1 (Tuple4 a b c)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Ord1 f, Ord1 g) => Ord1 (Compose f g)Defined in base-4.20.2.0 · Data.Functor.Compose
classclass (forall a. Read a => Read (f a)) => Read1 (f :: Type -> Type) where
#

Lifting of the Read class to unary type constructors.

Any instance should be subject to the following laws that canonicity is preserved:

liftReadsPrec readsPrec readList = readsPrec

liftReadList readsPrec readList = readList

liftReadPrec readPrec readListPrec = readPrec

liftReadListPrec readPrec readListPrec = readListPrec

This class therefore represents the generalization of Read by decomposing it's methods into a canonical lifting on a canonical inner method, so that the lifting can be reused for other arguments than the canonical one.

Both liftReadsPrec and liftReadPrec exist to match the interface provided in the Read type class, but it is recommended to implement Read1 instances using liftReadPrec as opposed to liftReadsPrec, since the former is more efficient than the latter. For example:

instance Read1 T where
  liftReadPrec     = ...
  liftReadListPrec = liftReadListPrecDefault

For more information, refer to the documentation for the Read class.

Methods

Instances16Read1, …
  • Read1 ComplexDefined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    readPrec_to_S readPrec1 0 "(2 % 3) :+ (3 % 4)" :: [(Complex Rational, String)][(2 % 3 :+ 3 % 4,"")]
  • Read1 NonEmptyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read1 IdentityDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read1 DownDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read1 MaybeDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read1 SoloDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read1 []Defined in base-4.20.2.0 · Data.Functor.Classes
  • Read1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read a => Read1 (Either a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • Read a => Read1 (Tuple2 a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • Read a => Read1 (Const a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Read a, Read b) => Read1 (Tuple3 a b)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Read1 f, Read1 g) => Read1 (Product f g)Defined in base-4.20.2.0 · Data.Functor.Product
  • (Read1 f, Read1 g) => Read1 (Sum f g)Defined in base-4.20.2.0 · Data.Functor.Sum
  • (Read a, Read b, Read c) => Read1 (Tuple4 a b c)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Read1 f, Read1 g) => Read1 (Compose f g)Defined in base-4.20.2.0 · Data.Functor.Compose
classclass (forall a. Show a => Show (f a)) => Show1 (f :: Type -> Type) where
#

Lifting of the Show class to unary type constructors.

Any instance should be subject to the following laws that canonicity is preserved:

liftShowsPrec showsPrec showList = showsPrec

liftShowList showsPrec showList = showList

This class therefore represents the generalization of Show by decomposing it's methods into a canonical lifting on a canonical inner method, so that the lifting can be reused for other arguments than the canonical one.

Methods

Instances16Show1, …
  • Show1 ComplexDefined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    showsPrec1 0 (2 :+ 3) """2 :+ 3"
  • Show1 NonEmptyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show1 IdentityDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show1 DownDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show1 MaybeDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show1 SoloDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show1 []Defined in base-4.20.2.0 · Data.Functor.Classes
  • Show1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show a => Show1 (Either a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • Show a => Show1 (Tuple2 a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • Show a => Show1 (Const a)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Show a, Show b) => Show1 (Tuple3 a b)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Show1 f, Show1 g) => Show1 (Product f g)Defined in base-4.20.2.0 · Data.Functor.Product
  • (Show1 f, Show1 g) => Show1 (Sum f g)Defined in base-4.20.2.0 · Data.Functor.Sum
  • (Show a, Show b, Show c) => Show1 (Tuple4 a b c)Defined in base-4.20.2.0 · Data.Functor.Classes
  • (Show1 f, Show1 g) => Show1 (Compose f g)Defined in base-4.20.2.0 · Data.Functor.Compose

For binary constructors

classclass (forall a. Eq a => Eq1 (f a)) => Eq2 (f :: Type -> Type -> Type) 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.

Instances5Eq2
  • Eq2 EitherDefined in base-4.20.2.0 · Data.Functor.Classes
  • Eq2 Tuple2Defined in base-4.20.2.0 · Data.Functor.Classes
  • Eq2 ConstDefined in base-4.20.2.0 · Data.Functor.Classes
  • Eq a => Eq2 (Tuple3 a)Defined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    eq2 ('x', True, "str") ('x', True, "str")True
  • (Eq a, Eq b) => Eq2 (Tuple4 a b)Defined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    eq2 ('x', True, "str", 2) ('x', True, "str", 2 :: Int)True
valueeq2 :: (Eq2 f, Eq a, Eq b) => f a b -> f a b -> Bool
#

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

classclass (Eq2 f, forall a. Ord a => Ord1 (f a)) => Ord2 (f :: Type -> Type -> Type) 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.

Instances5Ord2
  • Ord2 EitherDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord2 Tuple2Defined in base-4.20.2.0 · Data.Functor.Classes
  • Ord2 ConstDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord a => Ord2 (Tuple3 a)Defined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    compare2 ('x', True, "aaa") ('x', True, "zzz")LT
  • (Ord a, Ord b) => Ord2 (Tuple4 a b)Defined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    compare2 ('x', True, "str", 2) ('x', True, "str", 3 :: Int)LT
classclass (forall a. Read a => Read1 (f a)) => Read2 (f :: Type -> Type -> Type) where
#

Lifting of the Read class to binary type constructors.

Both liftReadsPrec2 and liftReadPrec2 exist to match the interface provided in the Read type class, but it is recommended to implement Read2 instances using liftReadPrec2 as opposed to liftReadsPrec2, since the former is more efficient than the latter. For example:

instance Read2 T where
  liftReadPrec2     = ...
  liftReadListPrec2 = liftReadListPrec2Default

For more information, refer to the documentation for the Read class.

Methods

Instances5Read2
  • Read2 EitherDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read2 Tuple2Defined in base-4.20.2.0 · Data.Functor.Classes
  • Read2 ConstDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read a => Read2 (Tuple3 a)Defined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    readPrec_to_S readPrec2 0 "('x', True, 2)" :: [((Char, Bool, Int), String)][(('x',True,2),"")]
  • (Read a, Read b) => Read2 (Tuple4 a b)Defined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    readPrec_to_S readPrec2 0 "('x', True, 2, 4.5)" :: [((Char, Bool, Int, Double), String)][(('x',True,2,4.5),"")]
classclass (forall a. Show a => Show1 (f a)) => Show2 (f :: Type -> Type -> Type) where
#

Lifting of the Show class to binary type constructors.

Methods

Instances5Show2
  • Show2 EitherDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show2 Tuple2Defined in base-4.20.2.0 · Data.Functor.Classes
  • Show2 ConstDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show a => Show2 (Tuple3 a)Defined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    showsPrec2 0 ('x', True, 2 :: Int) """('x',True,2)"
  • (Show a, Show b) => Show2 (Tuple4 a b)Defined in base-4.20.2.0 · Data.Functor.Classes
    Example1 expression
    showsPrec2 0 ('x', True, 2 :: Int, 4.5 :: Double) """('x',True,2,4.5)"

Helper functions

8 declarations

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
    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 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