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

Modulesyb-0.7.3Haskell98

Data.Generics.Aliases

This module provides a number of declarations for typical generic function types, corresponding type case, and others.

  • 12 types
  • 29 values
  • Packagesyb-0.7.3
  • Exports41
  • LanguageHaskell98
  • LicenceBSD-3-Clause
  • SourceAliases.hs

Combinators which create generic functions via cast

0 declarations

Other programming languages sometimes provide an operator instanceof which can check whether an expression is an instance of a given type. This operator allows programmers to implement a function f :: forall a. a -> a which exhibits a different behaviour depending on whether a Bool or a Char is passed. In Haskell this is not the case: A function with type forall a. a -> a can only be the identity function or a function which loops indefinitely or throws an exception. That is, it must implement exactly the same behaviour for any type at which it is used. But sometimes it is very useful to have a function which can accept (almost) any type and exhibit a different behaviour for different types. Haskell provides this functionality with the Typeable typeclass, whose instances can be automatically derived by GHC for almost all types. This typeclass allows the definition of a functon cast which has type forall a b. (Typeable a, Typeable b) => a -> Maybe b. The cast function allows to implement a polymorphic function with different behaviour at different types:

Example1 expression
cast True :: Maybe BoolJust True
Example1 expression
cast True :: Maybe IntNothing

This section provides combinators which make use of cast internally to provide various polymorphic functions with type-specific behaviour.

Transformations

valuemkT
  1. :: (Typeable a, Typeable b)
  2. => (b -> b)

    The type-specific transformation

  3. -> a

    The argument we try to cast to type b

  4. -> a
#

Extend the identity function with a type-specific transformation. The function created by mkT ext behaves like the identity function on all arguments which cannot be cast to type b, and like the function ext otherwise. The name mkT is short for "make transformation".

Examples
Example1 expression
mkT not TrueFalse
Example1 expression
mkT not 'a''a'
valueextT
  1. :: (Typeable a, Typeable b)
  2. => (a -> a)

    The transformation we want to extend

  3. -> (b -> b)

    The type-specific transformation

  4. -> a

    The argument we try to cast to type b

  5. -> a
#

Extend a generic transformation by a type-specific transformation. The function created by extT def ext behaves like the generic transformation def if its argument cannot be cast to the type b, and like the type-specific transformation ext otherwise. The name extT is short for "extend transformation".

Examples
Example1 expression
extT id not TrueFalse
Example1 expression
extT id not 'a''a'

Queries

valuemkQ
  1. :: (Typeable a, Typeable b)
  2. => r

    The default result

  3. -> (b -> r)

    The transformation to apply if the cast is successful

  4. -> a

    The argument we try to cast to type b

  5. -> r
#

The function created by mkQ def f returns the default result def if its argument cannot be cast to type b, otherwise it returns the result of applying f to its argument. The name mkQ is short for "make query".

Examples
Example1 expression
mkQ "default" (show :: Bool -> String) True"True"
Example1 expression
mkQ "default" (show :: Bool -> String) ()"default"
valueextQ
  1. :: (Typeable a, Typeable b)
  2. => (a -> r)

    The query we want to extend

  3. -> (b -> r)

    The type-specific query

  4. -> a

    The argument we try to cast to type b

  5. -> r
#

Extend a generic query by a type-specific query. The function created by extQ def ext behaves like the generic query def if its argument cannot be cast to the type b, and like the type-specific query ext otherwise. The name extQ is short for "extend query".

Examples
Example1 expression
extQ (const True) not TrueFalse
Example1 expression
extQ (const True) not 'a'True

Monadic transformations

valuemkM
  1. :: (Monad m, Typeable a, Typeable b)
  2. => (b -> m b)

    The type-specific monadic transformation

  3. -> a

    The argument we try to cast to type b

  4. -> m a
#

Extend the default monadic action pure :: Monad m => a -> m a by a type-specific monadic action. The function created by mkM act behaves like pure if its argument cannot be cast to type b, and like the monadic action act otherwise. The name mkM is short for "make monadic transformation".

Examples
Example1 expression
mkM (\x -> [x, not x]) True[True,False]
Example1 expression
mkM (\x -> [x, not x]) (5 :: Int)[5]
valueextM
  1. :: (Monad m, Typeable a, Typeable b)
  2. => (a -> m a)

    The monadic transformation we want to extend

  3. -> (b -> m b)

    The type-specific monadic transformation

  4. -> a

    The argument we try to cast to type b

  5. -> m a
#

Extend a generic monadic transformation by a type-specific case. The function created by extM def ext behaves like the monadic transformation def if its argument cannot be cast to type b, and like the monadic transformation ext otherwise. The name extM is short for "extend monadic transformation".

Examples
Example1 expression
extM (\x -> [x,x])(\x -> [not x, x]) True[False,True]
Example1 expression
extM (\x -> [x,x])(\x -> [not x, x]) (5 :: Int)[5,5]

MonadPlus transformations

valuemkMp
  1. :: (MonadPlus m, Typeable a, Typeable b)
  2. => (b -> m b)

    The type-specific MonadPlus action

  3. -> a

    The argument we try to cast to type b

  4. -> m a
#

Extend the default MonadPlus action const mzero by a type-specific MonadPlus action. The function created by mkMp act behaves like const mzero if its argument cannot be cast to type b, and like the monadic action act otherwise. The name mkMp is short for "make MonadPlus transformation".

Examples
Example1 expression
mkMp (\x -> Just (not x)) TrueJust False
Example1 expression
mkMp (\x -> Just (not x)) 'a'Nothing
valueextMp
  1. :: (MonadPlus m, Typeable a, Typeable b)
  2. => (a -> m a)

    The MonadPlus transformation we want to extend

  3. -> (b -> m b)

    The type-specific MonadPlus transformation

  4. -> a

    The argument we try to cast to type b

  5. -> m a
#

Extend a generic MonadPlus transformation by a type-specific case. The function created by extMp def ext behaves like MonadPlus transformation def if its argument cannot be cast to type b, and like the transformation ext otherwise. Note that extMp behaves exactly like extM. The name extMp is short for "extend MonadPlus transformation".

Examples
Example1 expression
extMp (\x -> [x,x])(\x -> [not x, x]) True[False,True]
Example1 expression
extMp (\x -> [x,x])(\x -> [not x, x]) (5 :: Int)[5,5]

Readers

valuemkR
  1. :: (MonadPlus m, Typeable a, Typeable b)
  2. => m b

    The type-specific reader

  3. -> m a
#

Make a generic reader from a type-specific case. The function created by mkR f behaves like the reader f if an expression of type a can be cast to type b, and like the expression mzero otherwise. The name mkR is short for "make reader".

Examples
Example1 expression
mkR (Just True) :: Maybe BoolJust True
Example1 expression
mkR (Just True) :: Maybe IntNothing
valueextR
  1. :: (Monad m, Typeable a, Typeable b)
  2. => m a

    The generic reader we want to extend

  3. -> m b

    The type-specific reader

  4. -> m a
#

Extend a generic reader by a type-specific case. The reader created by extR def ext behaves like the reader def if expressions of type b cannot be cast to type a, and like the reader ext otherwise. The name extR is short for "extend reader".

Examples
Example1 expression
extR (Just True) (Just 'a')Just True
Example1 expression
extR (Just True) (Just False)Just False

Builders

valueextB
  1. :: (Typeable a, Typeable b)
  2. => a

    The default result

  3. -> b

    The argument we try to cast to type a

  4. -> a
#

Extend a generic builder by a type-specific case. The builder created by extB def ext returns def if ext cannot be cast to type a, and like ext otherwise. The name extB is short for "extend builder".

Examples
Example1 expression
extB True 'a'True
Example1 expression
extB True FalseFalse

Other

valueext0 :: (Typeable a, Typeable b) => c a -> c b -> c a
#

Flexible type extension

Examples
Example1 expression
ext0 [1 :: Int, 2, 3] [True, False] :: [Int][1,2,3]
Example1 expression
ext0 [1 :: Int, 2, 3] [4 :: Int, 5, 6] :: [Int][4,5,6]

Types for generic functions

0 declarations

Transformations

typetype GenericT = forall a. Data a => a -> a
#

Generic transformations, i.e., take an "a" and return an "a"

newtypenewtype GenericT'
#

The type synonym GenericT has a polymorphic type, and can therefore not appear in places where monomorphic types are expected, for example in a list. The newtype GenericT' wraps GenericT in a newtype to lift this restriction.

Constructors

Queries

typetype GenericQ r = forall a. Data a => a -> r
#

Generic queries of type "r", i.e., take any "a" and return an "r"

newtypenewtype GenericQ' r
#

The type synonym GenericQ has a polymorphic type, and can therefore not appear in places where monomorphic types are expected, for example in a list. The newtype GenericQ' wraps GenericQ in a newtype to lift this restriction.

Constructors

Monadic transformations

typetype GenericM (m :: Type -> Type) = forall a. Data a => a -> m a
#

Generic monadic transformations, i.e., take an "a" and compute an "a"

newtypenewtype GenericM' (m :: Type -> Type)
#

The type synonym GenericM has a polymorphic type, and can therefore not appear in places where monomorphic types are expected, for example in a list. The newtype GenericM' wraps GenericM in a newtype to lift this restriction.

Constructors

Readers

typetype GenericR (m :: Type -> Type) = forall a. Data a => m a
#

Generic readers, say monadic builders, i.e., produce an "a" with the help of a monad "m".

Builders

typetype GenericB = forall a. Data a => a
#

Generic builders i.e., produce an "a".

Other

typetype Generic (c :: Type -> Type) = forall a. Data a => a -> c a
#

The general scheme underlying generic functions assumed by gfoldl; there are isomorphisms such as GenericT = Generic T.

Ingredients of generic functions

1 declaration
valueorElse :: Maybe a -> Maybe a -> Maybe a
#

Left-biased choice on maybes

Examples
Example1 expression
orElse Nothing NothingNothing
Example1 expression
orElse Nothing (Just 'a')Just 'a'
Example1 expression
orElse (Just 'a') NothingJust 'a'
Example1 expression
orElse (Just 'a') (Just 'b')Just 'a'

Function combinators on generic functions

4 declarations

Type extension for unary type constructors

6 declarations
valueext1 :: (Data a, Typeable t) => c a -> (forall d. Data d => c (t d)) -> c a
#

Flexible type extension

valueext1T
  1. :: (Data d, Typeable t)
  2. => forall e. Data e => e -> e
  3. -> forall f. Data f => t f -> t f
  4. -> d
  5. -> d
#

Type extension of transformations for unary type constructors

valueext1M
  1. :: (Monad m, Data d, Typeable t)
  2. => forall e. Data e => e -> m e
  3. -> forall f. Data f => t f -> m (t f)
  4. -> d
  5. -> m d
#

Type extension of monadic transformations for type constructors

valueext1Q
  1. :: (Data d, Typeable t)
  2. => d -> q
  3. -> forall e. Data e => t e -> q
  4. -> d
  5. -> q
#

Type extension of queries for type constructors

valueext1R
  1. :: (Monad m, Data d, Typeable t)
  2. => m d
  3. -> forall e. Data e => m (t e)
  4. -> m d
#

Type extension of readers for type constructors

valueext1B :: (Data a, Typeable t) => a -> (forall b. Data b => t b) -> a
#

Type extension of builders for type constructors

Type extension for binary type constructors

6 declarations
valueext2T
  1. :: (Data d, Typeable t)
  2. => forall e. Data e => e -> e
  3. -> forall d1 d2. (Data d1, Data d2) => t d1 d2 -> t d1 d2
  4. -> d
  5. -> d
#

Type extension of transformations for unary type constructors

valueext2M
  1. :: (Monad m, Data d, Typeable t)
  2. => forall e. Data e => e -> m e
  3. -> forall d1 d2. (Data d1, Data d2) => t d1 d2 -> m (t d1 d2)
  4. -> d
  5. -> m d
#

Type extension of monadic transformations for type constructors

valueext2Q
  1. :: (Data d, Typeable t)
  2. => d -> q
  3. -> forall d1 d2. (Data d1, Data d2) => t d1 d2 -> q
  4. -> d
  5. -> q
#

Type extension of queries for type constructors

valueext2R
  1. :: (Monad m, Data d, Typeable t)
  2. => m d
  3. -> forall d1 d2. (Data d1, Data d2) => m (t d1 d2)
  4. -> m d
#

Type extension of readers for type constructors

valueext2B
  1. :: (Data a, Typeable t)
  2. => a
  3. -> forall d1 d2. (Data d1, Data d2) => t d1 d2
  4. -> a
#

Type extension of builders for type constructors