{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeOperators #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Data
-- Copyright   :  (c) The University of Glasgow, CWI 2001--2004
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  stable
-- Portability :  non-portable (local universal quantification)
--
-- This module provides the 'Data' class with its primitives for
-- generic programming, along with instances for many datatypes. It
-- corresponds to a merge between the previous "Data.Generics.Basics"
-- and almost all of "Data.Generics.Instances". The instances that are
-- not present in this module were moved to the
-- @Data.Generics.Instances@ module in the @syb@ package.
--
-- \"Scrap your boilerplate\" --- Generic programming in Haskell.  See
-- <https://wiki.haskell.org/Research_papers/Generics#Scrap_your_boilerplate.21>.
--
-----------------------------------------------------------------------------

module Data.Data (

        -- * Module Data.Typeable re-exported for convenience
        module Data.Typeable,

        -- * The Data class for processing constructor applications
        Data(
                gfoldl,
                gunfold,
                toConstr,
                dataTypeOf,
                dataCast1,      -- mediate types and unary type constructors
                dataCast2,      -- mediate types and binary type constructors
                -- Generic maps defined in terms of gfoldl
                gmapT,
                gmapQ,
                gmapQl,
                gmapQr,
                gmapQi,
                gmapM,
                gmapMp,
                gmapMo
            ),

        -- * Datatype representations
        DataType,       -- abstract
        -- ** Constructors
        mkDataType,
        mkIntType,
        mkFloatType,
        mkCharType,
        mkNoRepType,
        -- ** Observers
        dataTypeName,
        DataRep(..),
        dataTypeRep,
        -- ** Convenience functions
        repConstr,
        isAlgType,
        dataTypeConstrs,
        indexConstr,
        maxConstrIndex,
        isNorepType,

        -- * Data constructor representations
        Constr,         -- abstract
        ConIndex,       -- alias for Int, start at 1
        Fixity(..),
        -- ** Constructors
        mkConstr,
        mkConstrTag,
        mkIntegralConstr,
        mkRealConstr,
        mkCharConstr,
        -- ** Observers
        constrType,
        ConstrRep(..),
        constrRep,
        constrFields,
        constrFixity,
        -- ** Convenience function: algebraic data types
        constrIndex,
        -- ** From strings to constructors and vice versa: all data types
        showConstr,
        readConstr,

        -- * Convenience functions: take type constructors apart
        tyconUQname,
        tyconModule,

        -- * Generic operations defined in terms of 'gunfold'
        fromConstr,
        fromConstrB,
        fromConstrM

  ) where


------------------------------------------------------------------------------

import Data.Functor.Const
import Data.Either
import Data.Eq
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.List (findIndex)
import Data.Typeable
import Data.Version( Version(..) )
import GHC.Base hiding (Any, IntRep, FloatRep)
import GHC.List
import GHC.Num
import GHC.Read
import GHC.Show
import GHC.Tuple (Solo (..))
import Text.Read( reads )

-- Imports for the instances
import Control.Applicative (WrappedArrow(..), WrappedMonad(..), ZipList(..))
       -- So we can give them Data instances
import Data.Functor.Identity -- So we can give Data instance for Identity
import Data.Int              -- So we can give Data instance for Int8, ...
import Data.Type.Coercion
import Data.Word             -- So we can give Data instance for Word8, ...
import GHC.Real              -- So we can give Data instance for Ratio
--import GHC.IOBase            -- So we can give Data instance for IO, Handle
import GHC.Ptr               -- So we can give Data instance for Ptr
import Foreign.C.ConstPtr    -- So we can give Data instance for ConstPtr
import GHC.ForeignPtr        -- So we can give Data instance for ForeignPtr
import Foreign.Ptr (IntPtr(..), WordPtr(..))
                             -- So we can give Data instance for IntPtr and WordPtr
--import GHC.Stable            -- So we can give Data instance for StablePtr
--import GHC.ST                -- So we can give Data instance for ST
--import GHC.Conc              -- So we can give Data instance for MVar & Co.
import GHC.Arr               -- So we can give Data instance for Array
import qualified GHC.Generics as Generics (Fixity(..))
import GHC.Generics hiding (Fixity(..))
                             -- So we can give Data instance for U1, V1, ...

------------------------------------------------------------------------------
--
--      The Data class
--
------------------------------------------------------------------------------

{- |
The 'Data' class comprehends a fundamental primitive 'gfoldl' for
folding over constructor applications, say terms. This primitive can
be instantiated in several ways to map over the immediate subterms
of a term; see the @gmap@ combinators later in this class.  Indeed, a
generic programmer does not necessarily need to use the ingenious gfoldl
primitive but rather the intuitive @gmap@ combinators.  The 'gfoldl'
primitive is completed by means to query top-level constructors, to
turn constructor representations into proper terms, and to list all
possible datatype constructors.  This completion allows us to serve
generic programming scenarios like read, show, equality, term generation.

The combinators 'gmapT', 'gmapQ', 'gmapM', etc are all provided with
default definitions in terms of 'gfoldl', leaving open the opportunity
to provide datatype-specific definitions.
(The inclusion of the @gmap@ combinators as members of class 'Data'
allows the programmer or the compiler to derive specialised, and maybe
more efficient code per datatype.  /Note/: 'gfoldl' is more higher-order
than the @gmap@ combinators.  This is subject to ongoing benchmarking
experiments.  It might turn out that the @gmap@ combinators will be
moved out of the class 'Data'.)

Conceptually, the definition of the @gmap@ combinators in terms of the
primitive 'gfoldl' requires the identification of the 'gfoldl' function
arguments.  Technically, we also need to identify the type constructor
@c@ for the construction of the result type from the folded term type.

In the definition of @gmapQ@/x/ combinators, we use phantom type
constructors for the @c@ in the type of 'gfoldl' because the result type
of a query does not involve the (polymorphic) type of the term argument.
In the definition of 'gmapQl' we simply use the plain constant type
constructor because 'gfoldl' is left-associative anyway and so it is
readily suited to fold a left-associative binary operation over the
immediate subterms.  In the definition of gmapQr, extra effort is
needed. We use a higher-order accumulation trick to mediate between
left-associative constructor application vs. right-associative binary
operation (e.g., @(:)@).  When the query is meant to compute a value
of type @r@, then the result type within generic folding is @r -> r@.
So the result of folding is a function to which we finally pass the
right unit.

With the @-XDeriveDataTypeable@ option, GHC can generate instances of the
'Data' class automatically.  For example, given the declaration

> data T a b = C1 a b | C2 deriving (Typeable, Data)

GHC will generate an instance that is equivalent to

> instance (Data a, Data b) => Data (T a b) where
>     gfoldl k z (C1 a b) = z C1 `k` a `k` b
>     gfoldl k z C2       = z C2
>
>     gunfold k z c = case constrIndex c of
>                         1 -> k (k (z C1))
>                         2 -> z C2
>
>     toConstr (C1 _ _) = con_C1
>     toConstr C2       = con_C2
>
>     dataTypeOf _ = ty_T
>
> con_C1 = mkConstr ty_T "C1" [] Prefix
> con_C2 = mkConstr ty_T "C2" [] Prefix
> ty_T   = mkDataType "Module.T" [con_C1, con_C2]

This is suitable for datatypes that are exported transparently.

-}

class Typeable a => Data a where

  -- | Left-associative fold operation for constructor applications.
  --
  -- The type of 'gfoldl' is a headache, but operationally it is a simple
  -- generalisation of a list fold.
  --
  -- The default definition for 'gfoldl' is @'const' 'id'@, which is
  -- suitable for abstract datatypes with no substructures.
  gfoldl  :: (forall d b. Data d => c (d -> b) -> d -> c b)
                -- ^ defines how nonempty constructor applications are
                -- folded.  It takes the folded tail of the constructor
                -- application and its head, i.e., an immediate subterm,
                -- and combines them in some way.
          -> (forall g. g -> c g)
                -- ^ defines how the empty constructor application is
                -- folded, like the neutral \/ start element for list
                -- folding.
          -> a
                -- ^ structure to be folded.
          -> c a
                -- ^ result, with a type defined in terms of @a@, but
                -- variability is achieved by means of type constructor
                -- @c@ for the construction of the actual result type.

  -- See the 'Data' instances in this file for an illustration of 'gfoldl'.

  gfoldl forall d b. Data d => c (d -> b) -> d -> c b
_ forall g. g -> c g
z = a -> c a
forall g. g -> c g
z

  -- | Unfolding constructor applications
  gunfold :: (forall b r. Data b => c (b -> r) -> c r)
          -> (forall r. r -> c r)
          -> Constr
          -> c a

  -- | Obtaining the constructor from a given datum.
  -- For proper terms, this is meant to be the top-level constructor.
  -- Primitive datatypes are here viewed as potentially infinite sets of
  -- values (i.e., constructors).
  toConstr   :: a -> Constr


  -- | The outer type constructor of the type
  dataTypeOf  :: a -> DataType



------------------------------------------------------------------------------
--
-- Mediate types and type constructors
--
------------------------------------------------------------------------------

  -- | Mediate types and unary type constructors.
  --
  -- In 'Data' instances of the form
  --
  -- @
  --     instance (Data a, ...) => Data (T a)
  -- @
  --
  -- 'dataCast1' should be defined as 'gcast1'.
  --
  -- The default definition is @'const' 'Nothing'@, which is appropriate
  -- for instances of other forms.
  dataCast1 :: Typeable t
            => (forall d. Data d => c (t d))
            -> Maybe (c a)
  dataCast1 forall d. Data d => c (t d)
_ = Maybe (c a)
forall a. Maybe a
Nothing

  -- | Mediate types and binary type constructors.
  --
  -- In 'Data' instances of the form
  --
  -- @
  --     instance (Data a, Data b, ...) => Data (T a b)
  -- @
  --
  -- 'dataCast2' should be defined as 'gcast2'.
  --
  -- The default definition is @'const' 'Nothing'@, which is appropriate
  -- for instances of other forms.
  dataCast2 :: Typeable t
            => (forall d e. (Data d, Data e) => c (t d e))
            -> Maybe (c a)
  dataCast2 forall d e. (Data d, Data e) => c (t d e)
_ = Maybe (c a)
forall a. Maybe a
Nothing



------------------------------------------------------------------------------
--
--      Typical generic maps defined in terms of gfoldl
--
------------------------------------------------------------------------------


  -- | A generic transformation that maps over the immediate subterms
  --
  -- The default definition instantiates the type constructor @c@ in the
  -- type of 'gfoldl' to an identity datatype constructor, using the
  -- isomorphism pair as injection and projection.
  gmapT :: (forall b. Data b => b -> b) -> a -> a

  -- Use the Identity datatype constructor
  -- to instantiate the type constructor c in the type of gfoldl,
  -- and perform injections Identity and projections runIdentity accordingly.
  --
  gmapT forall b. Data b => b -> b
f a
x0 = Identity a -> a
forall a. Identity a -> a
runIdentity ((forall d b. Data d => Identity (d -> b) -> d -> Identity b)
-> (forall g. g -> Identity g) -> a -> Identity a
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
gfoldl Identity (d -> b) -> d -> Identity b
forall d b. Data d => Identity (d -> b) -> d -> Identity b
k g -> Identity g
forall g. g -> Identity g
Identity a
x0)
    where
      k :: Data d => Identity (d->b) -> d -> Identity b
      k :: forall d b. Data d => Identity (d -> b) -> d -> Identity b
k (Identity d -> b
c) d
x = b -> Identity b
forall g. g -> Identity g
Identity (d -> b
c (d -> d
forall b. Data b => b -> b
f d
x))


  -- | A generic query with a left-associative binary operator
  gmapQl :: forall r r'. (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
  gmapQl r -> r' -> r
o r
r forall d. Data d => d -> r'
f = Const r a -> r
forall {k} a (b :: k). Const a b -> a
getConst (Const r a -> r) -> (a -> Const r a) -> a -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall d b. Data d => Const r (d -> b) -> d -> Const r b)
-> (forall g. g -> Const r g) -> a -> Const r a
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
gfoldl Const r (d -> b) -> d -> Const r b
forall d b. Data d => Const r (d -> b) -> d -> Const r b
k g -> Const r g
forall g. g -> Const r g
z
    where
      k :: Data d => Const r (d->b) -> d -> Const r b
      k :: forall d b. Data d => Const r (d -> b) -> d -> Const r b
k Const r (d -> b)
c d
x = r -> Const r b
forall {k} a (b :: k). a -> Const a b
Const (r -> Const r b) -> r -> Const r b
forall a b. (a -> b) -> a -> b
$ (Const r (d -> b) -> r
forall {k} a (b :: k). Const a b -> a
getConst Const r (d -> b)
c) r -> r' -> r
`o` d -> r'
forall d. Data d => d -> r'
f d
x
      z :: g -> Const r g
      z :: forall g. g -> Const r g
z g
_   = r -> Const r g
forall {k} a (b :: k). a -> Const a b
Const r
r

  -- | A generic query with a right-associative binary operator
  gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
  gmapQr r' -> r -> r
o r
r0 forall d. Data d => d -> r'
f a
x0 = Qr r a -> r -> r
forall {k} r (a :: k). Qr r a -> r -> r
unQr ((forall d b. Data d => Qr r (d -> b) -> d -> Qr r b)
-> (forall g. g -> Qr r g) -> a -> Qr r a
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
gfoldl Qr r (d -> b) -> d -> Qr r b
forall d b. Data d => Qr r (d -> b) -> d -> Qr r b
k (Qr r g -> g -> Qr r g
forall a b. a -> b -> a
const ((r -> r) -> Qr r g
forall {k} r (a :: k). (r -> r) -> Qr r a
Qr r -> r
forall a. a -> a
id)) a
x0) r
r0
    where
      k :: Data d => Qr r (d->b) -> d -> Qr r b
      k :: forall d b. Data d => Qr r (d -> b) -> d -> Qr r b
k (Qr r -> r
c) d
x = (r -> r) -> Qr r b
forall {k} r (a :: k). (r -> r) -> Qr r a
Qr (\r
r -> r -> r
c (d -> r'
forall d. Data d => d -> r'
f d
x r' -> r -> r
`o` r
r))


  -- | A generic query that processes the immediate subterms and returns a list
  -- of results.  The list is given in the same order as originally specified
  -- in the declaration of the data constructors.
  gmapQ :: (forall d. Data d => d -> u) -> a -> [u]
  gmapQ forall d. Data d => d -> u
f = (u -> [u] -> [u])
-> [u] -> (forall d. Data d => d -> u) -> a -> [u]
forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
gmapQr (:) [] d -> u
forall d. Data d => d -> u
f


  -- | A generic query that processes one child by index (zero-based)
  gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> a -> u
  gmapQi Int
i forall d. Data d => d -> u
f a
x = case (forall d b. Data d => Qi u (d -> b) -> d -> Qi u b)
-> (forall g. g -> Qi u g) -> a -> Qi u a
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
gfoldl Qi u (d -> b) -> d -> Qi u b
forall d b. Data d => Qi u (d -> b) -> d -> Qi u b
k g -> Qi u g
forall g. g -> Qi u g
forall g q. g -> Qi q g
z a
x of { Qi Int
_ Maybe u
q -> Maybe u -> u
forall a. HasCallStack => Maybe a -> a
fromJust Maybe u
q }
    where
      k :: Data d => Qi u (d -> b) -> d -> Qi u b
      k :: forall d b. Data d => Qi u (d -> b) -> d -> Qi u b
k (Qi Int
i' Maybe u
q) d
a = Int -> Maybe u -> Qi u b
forall {k} q (a :: k). Int -> Maybe q -> Qi q a
Qi (Int
i'Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (if Int
iInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
i' then u -> Maybe u
forall a. a -> Maybe a
Just (d -> u
forall d. Data d => d -> u
f d
a) else Maybe u
q)
      z :: g -> Qi q g
      z :: forall g q. g -> Qi q g
z g
_           = Int -> Maybe q -> Qi q g
forall {k} q (a :: k). Int -> Maybe q -> Qi q a
Qi Int
0 Maybe q
forall a. Maybe a
Nothing


  -- | A generic monadic transformation that maps over the immediate subterms
  --
  -- The default definition instantiates the type constructor @c@ in
  -- the type of 'gfoldl' to the monad datatype constructor, defining
  -- injection and projection using 'return' and '>>='.
  gmapM :: forall m. Monad m => (forall d. Data d => d -> m d) -> a -> m a

  -- Use immediately the monad datatype constructor
  -- to instantiate the type constructor c in the type of gfoldl,
  -- so injection and projection is done by return and >>=.
  --
  gmapM forall d. Data d => d -> m d
f = (forall d b. Data d => m (d -> b) -> d -> m b)
-> (forall g. g -> m g) -> a -> m a
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
gfoldl m (d -> b) -> d -> m b
forall d b. Data d => m (d -> b) -> d -> m b
k g -> m g
forall g. g -> m g
forall (m :: * -> *) a. Monad m => a -> m a
return
    where
      k :: Data d => m (d -> b) -> d -> m b
      k :: forall d b. Data d => m (d -> b) -> d -> m b
k m (d -> b)
c d
x = do d -> b
c' <- m (d -> b)
c
                 d
x' <- d -> m d
forall d. Data d => d -> m d
f d
x
                 b -> m b
forall g. g -> m g
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> b
c' d
x')


  -- | Transformation of at least one immediate subterm does not fail
  gmapMp :: forall m. MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a

{-

The type constructor that we use here simply keeps track of the fact
if we already succeeded for an immediate subterm; see Mp below. To
this end, we couple the monadic computation with a Boolean.

-}

  gmapMp forall d. Data d => d -> m d
f a
x = Mp m a -> m (a, Bool)
forall (m :: * -> *) x. Mp m x -> m (x, Bool)
unMp ((forall d b. Data d => Mp m (d -> b) -> d -> Mp m b)
-> (forall g. g -> Mp m g) -> a -> Mp m a
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
gfoldl Mp m (d -> b) -> d -> Mp m b
forall d b. Data d => Mp m (d -> b) -> d -> Mp m b
k g -> Mp m g
forall g. g -> Mp m g
z a
x) m (a, Bool) -> ((a, Bool) -> m a) -> m a
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(a
x',Bool
b) ->
                if Bool
b then a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x' else m a
forall a. m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
    where
      z :: g -> Mp m g
      z :: forall g. g -> Mp m g
z g
g = m (g, Bool) -> Mp m g
forall (m :: * -> *) x. m (x, Bool) -> Mp m x
Mp ((g, Bool) -> m (g, Bool)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (g
g,Bool
False))
      k :: Data d => Mp m (d -> b) -> d -> Mp m b
      k :: forall d b. Data d => Mp m (d -> b) -> d -> Mp m b
k (Mp m (d -> b, Bool)
c) d
y
        = m (b, Bool) -> Mp m b
forall (m :: * -> *) x. m (x, Bool) -> Mp m x
Mp ( m (d -> b, Bool)
c m (d -> b, Bool) -> ((d -> b, Bool) -> m (b, Bool)) -> m (b, Bool)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(d -> b
h, Bool
b) ->
                 (d -> m d
forall d. Data d => d -> m d
f d
y m d -> (d -> m (b, Bool)) -> m (b, Bool)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \d
y' -> (b, Bool) -> m (b, Bool)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> b
h d
y', Bool
True))
                 m (b, Bool) -> m (b, Bool) -> m (b, Bool)
forall a. m a -> m a -> m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` (b, Bool) -> m (b, Bool)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> b
h d
y, Bool
b)
             )

  -- | Transformation of one immediate subterm with success
  gmapMo :: forall m. MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a

{-

We use the same pairing trick as for gmapMp,
i.e., we use an extra Bool component to keep track of the
fact whether an immediate subterm was processed successfully.
However, we cut of mapping over subterms once a first subterm
was transformed successfully.

-}

  gmapMo forall d. Data d => d -> m d
f a
x = Mp m a -> m (a, Bool)
forall (m :: * -> *) x. Mp m x -> m (x, Bool)
unMp ((forall d b. Data d => Mp m (d -> b) -> d -> Mp m b)
-> (forall g. g -> Mp m g) -> a -> Mp m a
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a
gfoldl Mp m (d -> b) -> d -> Mp m b
forall d b. Data d => Mp m (d -> b) -> d -> Mp m b
k g -> Mp m g
forall g. g -> Mp m g
z a
x) m (a, Bool) -> ((a, Bool) -> m a) -> m a
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(a
x',Bool
b) ->
                if Bool
b then a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x' else m a
forall a. m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
    where
      z :: g -> Mp m g
      z :: forall g. g -> Mp m g
z g
g = m (g, Bool) -> Mp m g
forall (m :: * -> *) x. m (x, Bool) -> Mp m x
Mp ((g, Bool) -> m (g, Bool)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (g
g,Bool
False))
      k :: Data d => Mp m (d -> b) -> d -> Mp m b
      k :: forall d b. Data d => Mp m (d -> b) -> d -> Mp m b
k (Mp m (d -> b, Bool)
c) d
y
        = m (b, Bool) -> Mp m b
forall (m :: * -> *) x. m (x, Bool) -> Mp m x
Mp ( m (d -> b, Bool)
c m (d -> b, Bool) -> ((d -> b, Bool) -> m (b, Bool)) -> m (b, Bool)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(d -> b
h,Bool
b) -> if Bool
b
                        then (b, Bool) -> m (b, Bool)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> b
h d
y, Bool
b)
                        else (d -> m d
forall d. Data d => d -> m d
f d
y m d -> (d -> m (b, Bool)) -> m (b, Bool)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \d
y' -> (b, Bool) -> m (b, Bool)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> b
h d
y',Bool
True))
                             m (b, Bool) -> m (b, Bool) -> m (b, Bool)
forall a. m a -> m a -> m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` (b, Bool) -> m (b, Bool)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> b
h d
y, Bool
b)
             )


-- | Type constructor for adding counters to queries
data Qi q a = Qi Int (Maybe q)


-- | The type constructor used in definition of gmapQr
newtype Qr r a = Qr { forall {k} r (a :: k). Qr r a -> r -> r
unQr  :: r -> r }


-- | The type constructor used in definition of gmapMp
newtype Mp m x = Mp { forall (m :: * -> *) x. Mp m x -> m (x, Bool)
unMp :: m (x, Bool) }



------------------------------------------------------------------------------
--
--      Generic unfolding
--
------------------------------------------------------------------------------


-- | Build a term skeleton
fromConstr :: Data a => Constr -> a
fromConstr :: forall a. Data a => Constr -> a
fromConstr = (forall d. Data d => d) -> Constr -> a
forall a. Data a => (forall d. Data d => d) -> Constr -> a
fromConstrB (String -> d
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.fromConstr")


-- | Build a term and use a generic function for subterms
fromConstrB :: Data a
            => (forall d. Data d => d)
            -> Constr
            -> a
fromConstrB :: forall a. Data a => (forall d. Data d => d) -> Constr -> a
fromConstrB forall d. Data d => d
f = Identity a -> a
forall a. Identity a -> a
runIdentity (Identity a -> a) -> (Constr -> Identity a) -> Constr -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall b r. Data b => Identity (b -> r) -> Identity r)
-> (forall g. g -> Identity g) -> Constr -> Identity a
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a
gunfold Identity (b -> r) -> Identity r
forall b r. Data b => Identity (b -> r) -> Identity r
k r -> Identity r
forall g. g -> Identity g
z
 where
  k :: forall b r. Data b => Identity (b -> r) -> Identity r
  k :: forall b r. Data b => Identity (b -> r) -> Identity r
k Identity (b -> r)
c = r -> Identity r
forall g. g -> Identity g
Identity (Identity (b -> r) -> b -> r
forall a. Identity a -> a
runIdentity Identity (b -> r)
c b
forall d. Data d => d
f)

  z :: forall r. r -> Identity r
  z :: forall g. g -> Identity g
z = r -> Identity r
forall g. g -> Identity g
Identity


-- | Monadic variation on 'fromConstrB'
fromConstrM :: forall m a. (Monad m, Data a)
            => (forall d. Data d => m d)
            -> Constr
            -> m a
fromConstrM :: forall (m :: * -> *) a.
(Monad m, Data a) =>
(forall d. Data d => m d) -> Constr -> m a
fromConstrM forall d. Data d => m d
f = (forall b r. Data b => m (b -> r) -> m r)
-> (forall r. r -> m r) -> Constr -> m a
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a
gunfold m (b -> r) -> m r
forall b r. Data b => m (b -> r) -> m r
k r -> m r
forall r. r -> m r
z
 where
  k :: forall b r. Data b => m (b -> r) -> m r
  k :: forall b r. Data b => m (b -> r) -> m r
k m (b -> r)
c = do { b -> r
c' <- m (b -> r)
c; b
b <- m b
forall d. Data d => m d
f; r -> m r
forall r. r -> m r
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> r
c' b
b) }

  z :: forall r. r -> m r
  z :: forall r. r -> m r
z = r -> m r
forall r. r -> m r
forall (m :: * -> *) a. Monad m => a -> m a
return



------------------------------------------------------------------------------
--
--      Datatype and constructor representations
--
------------------------------------------------------------------------------


--
-- | Representation of datatypes.
-- A package of constructor representations with names of type and module.
--
data DataType = DataType
                        { DataType -> String
tycon   :: String
                        , DataType -> DataRep
datarep :: DataRep
                        }

              deriving Int -> DataType -> ShowS
[DataType] -> ShowS
DataType -> String
(Int -> DataType -> ShowS)
-> (DataType -> String) -> ([DataType] -> ShowS) -> Show DataType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DataType -> ShowS
showsPrec :: Int -> DataType -> ShowS
$cshow :: DataType -> String
show :: DataType -> String
$cshowList :: [DataType] -> ShowS
showList :: [DataType] -> ShowS
Show -- ^ @since 4.0.0.0

-- | Representation of constructors. Note that equality on constructors
-- with different types may not work -- i.e. the constructors for 'False' and
-- 'Nothing' may compare equal.
data Constr = Constr
                        { Constr -> ConstrRep
conrep    :: ConstrRep
                        , Constr -> String
constring :: String
                        , Constr -> [String]
confields :: [String] -- for AlgRep only
                        , Constr -> Fixity
confixity :: Fixity   -- for AlgRep only
                        , Constr -> DataType
datatype  :: DataType
                        }

-- | @since 4.0.0.0
instance Show Constr where
 show :: Constr -> String
show = Constr -> String
constring


-- | Equality of constructors
--
-- @since 4.0.0.0
instance Eq Constr where
  Constr
c == :: Constr -> Constr -> Bool
== Constr
c' = Constr -> ConstrRep
constrRep Constr
c ConstrRep -> ConstrRep -> Bool
forall a. Eq a => a -> a -> Bool
== Constr -> ConstrRep
constrRep Constr
c'


-- | Public representation of datatypes
data DataRep = AlgRep [Constr]
             | IntRep
             | FloatRep
             | CharRep
             | NoRep

            deriving ( DataRep -> DataRep -> Bool
(DataRep -> DataRep -> Bool)
-> (DataRep -> DataRep -> Bool) -> Eq DataRep
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DataRep -> DataRep -> Bool
== :: DataRep -> DataRep -> Bool
$c/= :: DataRep -> DataRep -> Bool
/= :: DataRep -> DataRep -> Bool
Eq   -- ^ @since 4.0.0.0
                     , Int -> DataRep -> ShowS
[DataRep] -> ShowS
DataRep -> String
(Int -> DataRep -> ShowS)
-> (DataRep -> String) -> ([DataRep] -> ShowS) -> Show DataRep
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DataRep -> ShowS
showsPrec :: Int -> DataRep -> ShowS
$cshow :: DataRep -> String
show :: DataRep -> String
$cshowList :: [DataRep] -> ShowS
showList :: [DataRep] -> ShowS
Show -- ^ @since 4.0.0.0
                     )
-- The list of constructors could be an array, a balanced tree, or others.


-- | Public representation of constructors
data ConstrRep = AlgConstr    ConIndex
               | IntConstr    Integer
               | FloatConstr  Rational
               | CharConstr   Char

               deriving ( ConstrRep -> ConstrRep -> Bool
(ConstrRep -> ConstrRep -> Bool)
-> (ConstrRep -> ConstrRep -> Bool) -> Eq ConstrRep
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConstrRep -> ConstrRep -> Bool
== :: ConstrRep -> ConstrRep -> Bool
$c/= :: ConstrRep -> ConstrRep -> Bool
/= :: ConstrRep -> ConstrRep -> Bool
Eq   -- ^ @since 4.0.0.0
                        , Int -> ConstrRep -> ShowS
[ConstrRep] -> ShowS
ConstrRep -> String
(Int -> ConstrRep -> ShowS)
-> (ConstrRep -> String)
-> ([ConstrRep] -> ShowS)
-> Show ConstrRep
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConstrRep -> ShowS
showsPrec :: Int -> ConstrRep -> ShowS
$cshow :: ConstrRep -> String
show :: ConstrRep -> String
$cshowList :: [ConstrRep] -> ShowS
showList :: [ConstrRep] -> ShowS
Show -- ^ @since 4.0.0.0
                        )


-- | Unique index for datatype constructors,
-- counting from 1 in the order they are given in the program text.
type ConIndex = Int


-- | Fixity of constructors
data Fixity = Prefix
            | Infix     -- Later: add associativity and precedence

            deriving ( Fixity -> Fixity -> Bool
(Fixity -> Fixity -> Bool)
-> (Fixity -> Fixity -> Bool) -> Eq Fixity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Fixity -> Fixity -> Bool
== :: Fixity -> Fixity -> Bool
$c/= :: Fixity -> Fixity -> Bool
/= :: Fixity -> Fixity -> Bool
Eq   -- ^ @since 4.0.0.0
                     , Int -> Fixity -> ShowS
[Fixity] -> ShowS
Fixity -> String
(Int -> Fixity -> ShowS)
-> (Fixity -> String) -> ([Fixity] -> ShowS) -> Show Fixity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Fixity -> ShowS
showsPrec :: Int -> Fixity -> ShowS
$cshow :: Fixity -> String
show :: Fixity -> String
$cshowList :: [Fixity] -> ShowS
showList :: [Fixity] -> ShowS
Show -- ^ @since 4.0.0.0
                     )


------------------------------------------------------------------------------
--
--      Observers for datatype representations
--
------------------------------------------------------------------------------


-- | Gets the type constructor including the module
dataTypeName :: DataType -> String
dataTypeName :: DataType -> String
dataTypeName = DataType -> String
tycon



-- | Gets the public presentation of a datatype
dataTypeRep :: DataType -> DataRep
dataTypeRep :: DataType -> DataRep
dataTypeRep = DataType -> DataRep
datarep


-- | Gets the datatype of a constructor
constrType :: Constr -> DataType
constrType :: Constr -> DataType
constrType = Constr -> DataType
datatype


-- | Gets the public presentation of constructors
constrRep :: Constr -> ConstrRep
constrRep :: Constr -> ConstrRep
constrRep = Constr -> ConstrRep
conrep


-- | Look up a constructor by its representation
repConstr :: DataType -> ConstrRep -> Constr
repConstr :: DataType -> ConstrRep -> Constr
repConstr DataType
dt ConstrRep
cr =
      case (DataType -> DataRep
dataTypeRep DataType
dt, ConstrRep
cr) of
        (AlgRep [Constr]
cs, AlgConstr Int
i)      -> [Constr]
cs [Constr] -> Int -> Constr
forall a. HasCallStack => [a] -> Int -> a
!! (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
        (DataRep
IntRep,    IntConstr Integer
i)      -> DataType -> Integer -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
dt Integer
i
        (DataRep
FloatRep,  FloatConstr Rational
f)    -> DataType -> Rational -> Constr
forall a. (Real a, Show a) => DataType -> a -> Constr
mkRealConstr DataType
dt Rational
f
        (DataRep
CharRep,   CharConstr Char
c)     -> DataType -> Char -> Constr
mkCharConstr DataType
dt Char
c
        (DataRep, ConstrRep)
_ -> String -> Constr
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.repConstr: The given ConstrRep does not fit to the given DataType."



------------------------------------------------------------------------------
--
--      Representations of algebraic data types
--
------------------------------------------------------------------------------


-- | Constructs an algebraic datatype
mkDataType :: String -> [Constr] -> DataType
mkDataType :: String -> [Constr] -> DataType
mkDataType String
str [Constr]
cs = DataType
                        { tycon :: String
tycon   = String
str
                        , datarep :: DataRep
datarep = [Constr] -> DataRep
AlgRep [Constr]
cs
                        }

-- | Constructs a constructor
--
-- @since 4.16.0.0
mkConstrTag :: DataType -> String -> Int -> [String] -> Fixity -> Constr
mkConstrTag :: DataType -> String -> Int -> [String] -> Fixity -> Constr
mkConstrTag DataType
dt String
str Int
idx [String]
fields Fixity
fix =
        Constr
                { conrep :: ConstrRep
conrep    = Int -> ConstrRep
AlgConstr Int
idx
                , constring :: String
constring = String
str
                , confields :: [String]
confields = [String]
fields
                , confixity :: Fixity
confixity = Fixity
fix
                , datatype :: DataType
datatype  = DataType
dt
                }

-- | Constructs a constructor
mkConstr :: DataType -> String -> [String] -> Fixity -> Constr
mkConstr :: DataType -> String -> [String] -> Fixity -> Constr
mkConstr DataType
dt String
str [String]
fields Fixity
fix = DataType -> String -> Int -> [String] -> Fixity -> Constr
mkConstrTag DataType
dt String
str Int
idx [String]
fields Fixity
fix
  where
    idx :: Int
idx = case (Constr -> Bool) -> [Constr] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex (\Constr
c -> Constr -> String
showConstr Constr
c String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
str) (DataType -> [Constr]
dataTypeConstrs DataType
dt) of
            Just Int
i  -> Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 -- ConTag starts at 1
            Maybe Int
Nothing -> String -> Int
forall a. String -> a
errorWithoutStackTrace (String -> Int) -> String -> Int
forall a b. (a -> b) -> a -> b
$
                        String
"Data.Data.mkConstr: couldn't find constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
str


-- | Gets the constructors of an algebraic datatype
dataTypeConstrs :: DataType -> [Constr]
dataTypeConstrs :: DataType -> [Constr]
dataTypeConstrs DataType
dt = case DataType -> DataRep
datarep DataType
dt of
                        (AlgRep [Constr]
cons) -> [Constr]
cons
                        DataRep
_ -> String -> [Constr]
forall a. String -> a
errorWithoutStackTrace (String -> [Constr]) -> String -> [Constr]
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.dataTypeConstrs is not supported for "
                                    String -> ShowS
forall a. [a] -> [a] -> [a]
++ DataType -> String
dataTypeName DataType
dt String -> ShowS
forall a. [a] -> [a] -> [a]
++
                                    String
", as it is not an algebraic data type."


-- | Gets the field labels of a constructor.  The list of labels
-- is returned in the same order as they were given in the original
-- constructor declaration.
constrFields :: Constr -> [String]
constrFields :: Constr -> [String]
constrFields = Constr -> [String]
confields


-- | Gets the fixity of a constructor
constrFixity :: Constr -> Fixity
constrFixity :: Constr -> Fixity
constrFixity = Constr -> Fixity
confixity



------------------------------------------------------------------------------
--
--      From strings to constr's and vice versa: all data types
--
------------------------------------------------------------------------------


-- | Gets the string for a constructor
showConstr :: Constr -> String
showConstr :: Constr -> String
showConstr = Constr -> String
constring


-- | Lookup a constructor via a string
readConstr :: DataType -> String -> Maybe Constr
readConstr :: DataType -> String -> Maybe Constr
readConstr DataType
dt String
str =
      case DataType -> DataRep
dataTypeRep DataType
dt of
        AlgRep [Constr]
cons -> [Constr] -> Maybe Constr
idx [Constr]
cons
        DataRep
IntRep      -> (Integer -> Constr) -> Maybe Constr
forall t. Read t => (t -> Constr) -> Maybe Constr
mkReadCon (\Integer
i -> (DataType -> String -> ConstrRep -> Constr
mkPrimCon DataType
dt String
str (Integer -> ConstrRep
IntConstr Integer
i)))
        DataRep
FloatRep    -> (Double -> Constr) -> Maybe Constr
forall t. Read t => (t -> Constr) -> Maybe Constr
mkReadCon Double -> Constr
ffloat
        DataRep
CharRep     -> (Char -> Constr) -> Maybe Constr
forall t. Read t => (t -> Constr) -> Maybe Constr
mkReadCon (\Char
c -> (DataType -> String -> ConstrRep -> Constr
mkPrimCon DataType
dt String
str (Char -> ConstrRep
CharConstr Char
c)))
        DataRep
NoRep       -> Maybe Constr
forall a. Maybe a
Nothing
  where

    -- Read a value and build a constructor
    mkReadCon :: Read t => (t -> Constr) -> Maybe Constr
    mkReadCon :: forall t. Read t => (t -> Constr) -> Maybe Constr
mkReadCon t -> Constr
f = case (ReadS t
forall a. Read a => ReadS a
reads String
str) of
                    [(t
t,String
"")] -> Constr -> Maybe Constr
forall a. a -> Maybe a
Just (t -> Constr
f t
t)
                    [(t, String)]
_ -> Maybe Constr
forall a. Maybe a
Nothing

    -- Traverse list of algebraic datatype constructors
    idx :: [Constr] -> Maybe Constr
    idx :: [Constr] -> Maybe Constr
idx [Constr]
cons = case (Constr -> Bool) -> [Constr] -> [Constr]
forall a. (a -> Bool) -> [a] -> [a]
filter (String -> String -> Bool
forall a. Eq a => a -> a -> Bool
(==) String
str (String -> Bool) -> (Constr -> String) -> Constr -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Constr -> String
showConstr) [Constr]
cons of
                [] -> Maybe Constr
forall a. Maybe a
Nothing
                Constr
hd : [Constr]
_ -> Constr -> Maybe Constr
forall a. a -> Maybe a
Just Constr
hd

    ffloat :: Double -> Constr
    ffloat :: Double -> Constr
ffloat =  DataType -> String -> ConstrRep -> Constr
mkPrimCon DataType
dt String
str (ConstrRep -> Constr) -> (Double -> ConstrRep) -> Double -> Constr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> ConstrRep
FloatConstr (Rational -> ConstrRep)
-> (Double -> Rational) -> Double -> ConstrRep
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Rational
forall a. Real a => a -> Rational
toRational

------------------------------------------------------------------------------
--
--      Convenience functions: algebraic data types
--
------------------------------------------------------------------------------


-- | Test for an algebraic type
isAlgType :: DataType -> Bool
isAlgType :: DataType -> Bool
isAlgType DataType
dt = case DataType -> DataRep
datarep DataType
dt of
                 (AlgRep [Constr]
_) -> Bool
True
                 DataRep
_ -> Bool
False


-- | Gets the constructor for an index (algebraic datatypes only)
indexConstr :: DataType -> ConIndex -> Constr
indexConstr :: DataType -> Int -> Constr
indexConstr DataType
dt Int
idx = case DataType -> DataRep
datarep DataType
dt of
                        (AlgRep [Constr]
cs) -> [Constr]
cs [Constr] -> Int -> Constr
forall a. HasCallStack => [a] -> Int -> a
!! (Int
idxInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
                        DataRep
_           -> String -> Constr
forall a. String -> a
errorWithoutStackTrace (String -> Constr) -> String -> Constr
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.indexConstr is not supported for "
                                               String -> ShowS
forall a. [a] -> [a] -> [a]
++ DataType -> String
dataTypeName DataType
dt String -> ShowS
forall a. [a] -> [a] -> [a]
++
                                               String
", as it is not an algebraic data type."


-- | Gets the index of a constructor (algebraic datatypes only)
constrIndex :: Constr -> ConIndex
constrIndex :: Constr -> Int
constrIndex Constr
con = case Constr -> ConstrRep
constrRep Constr
con of
                    (AlgConstr Int
idx) -> Int
idx
                    ConstrRep
_ -> String -> Int
forall a. String -> a
errorWithoutStackTrace (String -> Int) -> String -> Int
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.constrIndex is not supported for "
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ DataType -> String
dataTypeName (Constr -> DataType
constrType Constr
con) String -> ShowS
forall a. [a] -> [a] -> [a]
++
                                 String
", as it is not an algebraic data type."


-- | Gets the maximum constructor index of an algebraic datatype
maxConstrIndex :: DataType -> ConIndex
maxConstrIndex :: DataType -> Int
maxConstrIndex DataType
dt = case DataType -> DataRep
dataTypeRep DataType
dt of
                        AlgRep [Constr]
cs -> [Constr] -> Int
forall a. [a] -> Int
length [Constr]
cs
                        DataRep
_            -> String -> Int
forall a. String -> a
errorWithoutStackTrace (String -> Int) -> String -> Int
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.maxConstrIndex is not supported for "
                                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ DataType -> String
dataTypeName DataType
dt String -> ShowS
forall a. [a] -> [a] -> [a]
++
                                                 String
", as it is not an algebraic data type."



------------------------------------------------------------------------------
--
--      Representation of primitive types
--
------------------------------------------------------------------------------


-- | Constructs the 'Int' type
mkIntType :: String -> DataType
mkIntType :: String -> DataType
mkIntType = DataRep -> String -> DataType
mkPrimType DataRep
IntRep


-- | Constructs the 'Float' type
mkFloatType :: String -> DataType
mkFloatType :: String -> DataType
mkFloatType = DataRep -> String -> DataType
mkPrimType DataRep
FloatRep


-- | Constructs the 'Char' type
mkCharType :: String -> DataType
mkCharType :: String -> DataType
mkCharType = DataRep -> String -> DataType
mkPrimType DataRep
CharRep


-- | Helper for 'mkIntType', 'mkFloatType'
mkPrimType :: DataRep -> String -> DataType
mkPrimType :: DataRep -> String -> DataType
mkPrimType DataRep
dr String
str = DataType
                        { tycon :: String
tycon   = String
str
                        , datarep :: DataRep
datarep = DataRep
dr
                        }


-- Makes a constructor for primitive types
mkPrimCon :: DataType -> String -> ConstrRep -> Constr
mkPrimCon :: DataType -> String -> ConstrRep -> Constr
mkPrimCon DataType
dt String
str ConstrRep
cr = Constr
                        { datatype :: DataType
datatype  = DataType
dt
                        , conrep :: ConstrRep
conrep    = ConstrRep
cr
                        , constring :: String
constring = String
str
                        , confields :: [String]
confields = String -> [String]
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.confields"
                        , confixity :: Fixity
confixity = String -> Fixity
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.confixity"
                        }

mkIntegralConstr :: (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr :: forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
dt a
i = case DataType -> DataRep
datarep DataType
dt of
                  DataRep
IntRep -> DataType -> String -> ConstrRep -> Constr
mkPrimCon DataType
dt (a -> String
forall a. Show a => a -> String
show a
i) (Integer -> ConstrRep
IntConstr (a -> Integer
forall a. Integral a => a -> Integer
toInteger  a
i))
                  DataRep
_ -> String -> Constr
forall a. String -> a
errorWithoutStackTrace (String -> Constr) -> String -> Constr
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.mkIntegralConstr is not supported for "
                               String -> ShowS
forall a. [a] -> [a] -> [a]
++ DataType -> String
dataTypeName DataType
dt String -> ShowS
forall a. [a] -> [a] -> [a]
++
                               String
", as it is not an Integral data type."

mkRealConstr :: (Real a, Show a) => DataType -> a -> Constr
mkRealConstr :: forall a. (Real a, Show a) => DataType -> a -> Constr
mkRealConstr DataType
dt a
f = case DataType -> DataRep
datarep DataType
dt of
                    DataRep
FloatRep -> DataType -> String -> ConstrRep -> Constr
mkPrimCon DataType
dt (a -> String
forall a. Show a => a -> String
show a
f) (Rational -> ConstrRep
FloatConstr (a -> Rational
forall a. Real a => a -> Rational
toRational a
f))
                    DataRep
_ -> String -> Constr
forall a. String -> a
errorWithoutStackTrace (String -> Constr) -> String -> Constr
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.mkRealConstr is not supported for "
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ DataType -> String
dataTypeName DataType
dt String -> ShowS
forall a. [a] -> [a] -> [a]
++
                                 String
", as it is not a Real data type."

-- | Makes a constructor for 'Char'.
mkCharConstr :: DataType -> Char -> Constr
mkCharConstr :: DataType -> Char -> Constr
mkCharConstr DataType
dt Char
c = case DataType -> DataRep
datarep DataType
dt of
                   DataRep
CharRep -> DataType -> String -> ConstrRep -> Constr
mkPrimCon DataType
dt (Char -> String
forall a. Show a => a -> String
show Char
c) (Char -> ConstrRep
CharConstr Char
c)
                   DataRep
_ -> String -> Constr
forall a. String -> a
errorWithoutStackTrace (String -> Constr) -> String -> Constr
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.mkCharConstr is not supported for "
                                String -> ShowS
forall a. [a] -> [a] -> [a]
++ DataType -> String
dataTypeName DataType
dt String -> ShowS
forall a. [a] -> [a] -> [a]
++
                                String
", as it is not an Char data type."


------------------------------------------------------------------------------
--
--      Non-representations for non-representable types
--
------------------------------------------------------------------------------


-- | Constructs a non-representation for a non-representable type
mkNoRepType :: String -> DataType
mkNoRepType :: String -> DataType
mkNoRepType String
str = DataType
                        { tycon :: String
tycon   = String
str
                        , datarep :: DataRep
datarep = DataRep
NoRep
                        }

-- | Test for a non-representable type
isNorepType :: DataType -> Bool
isNorepType :: DataType -> Bool
isNorepType DataType
dt = case DataType -> DataRep
datarep DataType
dt of
                   DataRep
NoRep -> Bool
True
                   DataRep
_ -> Bool
False



------------------------------------------------------------------------------
--
--      Convenience for qualified type constructors
--
------------------------------------------------------------------------------


-- | Gets the unqualified type constructor:
-- drop *.*.*... before name
--
tyconUQname :: String -> String
tyconUQname :: ShowS
tyconUQname String
x = case (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
(==) Char
'.') String
x of
                  [] -> String
x
                  Char
_ : String
tl -> ShowS
tyconUQname String
tl


-- | Gets the module of a type constructor:
-- take *.*.*... before name
tyconModule :: String -> String
tyconModule :: ShowS
tyconModule String
x = case (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
(==) Char
'.') String
x of
                  (String
_, String
"") -> String
""
                  (String
a, Char
_ : String
tl) -> String
a String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
tyconModule' String
tl
  where
    tyconModule' :: ShowS
tyconModule' String
y = let y' :: String
y' = ShowS
tyconModule String
y
                      in if String
y' String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"" then String
"" else (Char
'.'Char -> ShowS
forall a. a -> [a] -> [a]
:String
y')




------------------------------------------------------------------------------
------------------------------------------------------------------------------
--
--      Instances of the Data class for Prelude-like types.
--      We define top-level definitions for representations.
--
------------------------------------------------------------------------------

-- | @since 4.0.0.0
deriving instance Data Bool

------------------------------------------------------------------------------

charType :: DataType
charType :: DataType
charType = String -> DataType
mkCharType String
"Prelude.Char"

-- | @since 4.0.0.0
instance Data Char where
  toConstr :: Char -> Constr
toConstr Char
x = DataType -> Char -> Constr
mkCharConstr DataType
charType Char
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Char
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (CharConstr Char
x) -> Char -> c Char
forall r. r -> c r
z Char
x
                    ConstrRep
_ -> String -> c Char
forall a. String -> a
errorWithoutStackTrace (String -> c Char) -> String -> c Char
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Char."
  dataTypeOf :: Char -> DataType
dataTypeOf Char
_ = DataType
charType


------------------------------------------------------------------------------

floatType :: DataType
floatType :: DataType
floatType = String -> DataType
mkFloatType String
"Prelude.Float"

-- | @since 4.0.0.0
instance Data Float where
  toConstr :: Float -> Constr
toConstr = DataType -> Float -> Constr
forall a. (Real a, Show a) => DataType -> a -> Constr
mkRealConstr DataType
floatType
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Float
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (FloatConstr Rational
x) -> Float -> c Float
forall r. r -> c r
z (Rational -> Float
forall a b. (Real a, Fractional b) => a -> b
realToFrac Rational
x)
                    ConstrRep
_ -> String -> c Float
forall a. String -> a
errorWithoutStackTrace (String -> c Float) -> String -> c Float
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Float."
  dataTypeOf :: Float -> DataType
dataTypeOf Float
_ = DataType
floatType


------------------------------------------------------------------------------

doubleType :: DataType
doubleType :: DataType
doubleType = String -> DataType
mkFloatType String
"Prelude.Double"

-- | @since 4.0.0.0
instance Data Double where
  toConstr :: Double -> Constr
toConstr = DataType -> Double -> Constr
forall a. (Real a, Show a) => DataType -> a -> Constr
mkRealConstr DataType
doubleType
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Double
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (FloatConstr Rational
x) -> Double -> c Double
forall r. r -> c r
z (Rational -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac Rational
x)
                    ConstrRep
_ -> String -> c Double
forall a. String -> a
errorWithoutStackTrace (String -> c Double) -> String -> c Double
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Double."
  dataTypeOf :: Double -> DataType
dataTypeOf Double
_ = DataType
doubleType


------------------------------------------------------------------------------

intType :: DataType
intType :: DataType
intType = String -> DataType
mkIntType String
"Prelude.Int"

-- | @since 4.0.0.0
instance Data Int where
  toConstr :: Int -> Constr
toConstr Int
x = DataType -> Int -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
intType Int
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Int
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Int -> c Int
forall r. r -> c r
z (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Int
forall a. String -> a
errorWithoutStackTrace (String -> c Int) -> String -> c Int
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Int."
  dataTypeOf :: Int -> DataType
dataTypeOf Int
_ = DataType
intType


------------------------------------------------------------------------------

integerType :: DataType
integerType :: DataType
integerType = String -> DataType
mkIntType String
"Prelude.Integer"

-- | @since 4.0.0.0
instance Data Integer where
  toConstr :: Integer -> Constr
toConstr = DataType -> Integer -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
integerType
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Integer
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Integer -> c Integer
forall r. r -> c r
z Integer
x
                    ConstrRep
_ -> String -> c Integer
forall a. String -> a
errorWithoutStackTrace (String -> c Integer) -> String -> c Integer
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Integer."
  dataTypeOf :: Integer -> DataType
dataTypeOf Integer
_ = DataType
integerType


------------------------------------------------------------------------------

-- This follows the same style as the other integral 'Data' instances
-- defined in "Data.Data"
naturalType :: DataType
naturalType :: DataType
naturalType = String -> DataType
mkIntType String
"Numeric.Natural.Natural"

-- | @since 4.8.0.0
instance Data Natural where
  toConstr :: Natural -> Constr
toConstr Natural
x = DataType -> Natural -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
naturalType Natural
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Natural
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Natural -> c Natural
forall r. r -> c r
z (Integer -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Natural
forall a. String -> a
errorWithoutStackTrace (String -> c Natural) -> String -> c Natural
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Natural"
  dataTypeOf :: Natural -> DataType
dataTypeOf Natural
_ = DataType
naturalType


------------------------------------------------------------------------------

int8Type :: DataType
int8Type :: DataType
int8Type = String -> DataType
mkIntType String
"Data.Int.Int8"

-- | @since 4.0.0.0
instance Data Int8 where
  toConstr :: Int8 -> Constr
toConstr Int8
x = DataType -> Int8 -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
int8Type Int8
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Int8
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Int8 -> c Int8
forall r. r -> c r
z (Integer -> Int8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Int8
forall a. String -> a
errorWithoutStackTrace (String -> c Int8) -> String -> c Int8
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Int8."
  dataTypeOf :: Int8 -> DataType
dataTypeOf Int8
_ = DataType
int8Type


------------------------------------------------------------------------------

int16Type :: DataType
int16Type :: DataType
int16Type = String -> DataType
mkIntType String
"Data.Int.Int16"

-- | @since 4.0.0.0
instance Data Int16 where
  toConstr :: Int16 -> Constr
toConstr Int16
x = DataType -> Int16 -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
int16Type Int16
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Int16
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Int16 -> c Int16
forall r. r -> c r
z (Integer -> Int16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Int16
forall a. String -> a
errorWithoutStackTrace (String -> c Int16) -> String -> c Int16
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Int16."
  dataTypeOf :: Int16 -> DataType
dataTypeOf Int16
_ = DataType
int16Type


------------------------------------------------------------------------------

int32Type :: DataType
int32Type :: DataType
int32Type = String -> DataType
mkIntType String
"Data.Int.Int32"

-- | @since 4.0.0.0
instance Data Int32 where
  toConstr :: Int32 -> Constr
toConstr Int32
x = DataType -> Int32 -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
int32Type Int32
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Int32
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Int32 -> c Int32
forall r. r -> c r
z (Integer -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Int32
forall a. String -> a
errorWithoutStackTrace (String -> c Int32) -> String -> c Int32
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Int32."
  dataTypeOf :: Int32 -> DataType
dataTypeOf Int32
_ = DataType
int32Type


------------------------------------------------------------------------------

int64Type :: DataType
int64Type :: DataType
int64Type = String -> DataType
mkIntType String
"Data.Int.Int64"

-- | @since 4.0.0.0
instance Data Int64 where
  toConstr :: Int64 -> Constr
toConstr Int64
x = DataType -> Int64 -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
int64Type Int64
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Int64
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Int64 -> c Int64
forall r. r -> c r
z (Integer -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Int64
forall a. String -> a
errorWithoutStackTrace (String -> c Int64) -> String -> c Int64
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Int64."
  dataTypeOf :: Int64 -> DataType
dataTypeOf Int64
_ = DataType
int64Type


------------------------------------------------------------------------------

wordType :: DataType
wordType :: DataType
wordType = String -> DataType
mkIntType String
"Data.Word.Word"

-- | @since 4.0.0.0
instance Data Word where
  toConstr :: Word -> Constr
toConstr Word
x = DataType -> Word -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
wordType Word
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Word
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Word -> c Word
forall r. r -> c r
z (Integer -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Word
forall a. String -> a
errorWithoutStackTrace (String -> c Word) -> String -> c Word
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Word"
  dataTypeOf :: Word -> DataType
dataTypeOf Word
_ = DataType
wordType


------------------------------------------------------------------------------

word8Type :: DataType
word8Type :: DataType
word8Type = String -> DataType
mkIntType String
"Data.Word.Word8"

-- | @since 4.0.0.0
instance Data Word8 where
  toConstr :: Word8 -> Constr
toConstr Word8
x = DataType -> Word8 -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
word8Type Word8
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Word8
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Word8 -> c Word8
forall r. r -> c r
z (Integer -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Word8
forall a. String -> a
errorWithoutStackTrace (String -> c Word8) -> String -> c Word8
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Word8."
  dataTypeOf :: Word8 -> DataType
dataTypeOf Word8
_ = DataType
word8Type


------------------------------------------------------------------------------

word16Type :: DataType
word16Type :: DataType
word16Type = String -> DataType
mkIntType String
"Data.Word.Word16"

-- | @since 4.0.0.0
instance Data Word16 where
  toConstr :: Word16 -> Constr
toConstr Word16
x = DataType -> Word16 -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
word16Type Word16
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Word16
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Word16 -> c Word16
forall r. r -> c r
z (Integer -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Word16
forall a. String -> a
errorWithoutStackTrace (String -> c Word16) -> String -> c Word16
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Word16."
  dataTypeOf :: Word16 -> DataType
dataTypeOf Word16
_ = DataType
word16Type


------------------------------------------------------------------------------

word32Type :: DataType
word32Type :: DataType
word32Type = String -> DataType
mkIntType String
"Data.Word.Word32"

-- | @since 4.0.0.0
instance Data Word32 where
  toConstr :: Word32 -> Constr
toConstr Word32
x = DataType -> Word32 -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
word32Type Word32
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Word32
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Word32 -> c Word32
forall r. r -> c r
z (Integer -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Word32
forall a. String -> a
errorWithoutStackTrace (String -> c Word32) -> String -> c Word32
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Word32."
  dataTypeOf :: Word32 -> DataType
dataTypeOf Word32
_ = DataType
word32Type


------------------------------------------------------------------------------

word64Type :: DataType
word64Type :: DataType
word64Type = String -> DataType
mkIntType String
"Data.Word.Word64"

-- | @since 4.0.0.0
instance Data Word64 where
  toConstr :: Word64 -> Constr
toConstr Word64
x = DataType -> Word64 -> Constr
forall a. (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr DataType
word64Type Word64
x
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Word64
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
z Constr
c = case Constr -> ConstrRep
constrRep Constr
c of
                    (IntConstr Integer
x) -> Word64 -> c Word64
forall r. r -> c r
z (Integer -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
                    ConstrRep
_ -> String -> c Word64
forall a. String -> a
errorWithoutStackTrace (String -> c Word64) -> String -> c Word64
forall a b. (a -> b) -> a -> b
$ String
"Data.Data.gunfold: Constructor " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Constr -> String
forall a. Show a => a -> String
show Constr
c
                                 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not of type Word64."
  dataTypeOf :: Word64 -> DataType
dataTypeOf Word64
_ = DataType
word64Type


------------------------------------------------------------------------------

ratioConstr :: Constr
ratioConstr :: Constr
ratioConstr = DataType -> String -> [String] -> Fixity -> Constr
mkConstr DataType
ratioDataType String
":%" [] Fixity
Infix

ratioDataType :: DataType
ratioDataType :: DataType
ratioDataType = String -> [Constr] -> DataType
mkDataType String
"GHC.Real.Ratio" [Constr
ratioConstr]

-- NB: This Data instance intentionally uses the (%) smart constructor instead
-- of the internal (:%) constructor to preserve the invariant that a Ratio
-- value is reduced to normal form. See #10011.

-- | @since 4.0.0.0
instance (Data a, Integral a) => Data (Ratio a) where
  gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Ratio a -> c (Ratio a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
k forall g. g -> c g
z (a
a :% a
b) = (a -> a -> Ratio a) -> c (a -> a -> Ratio a)
forall g. g -> c g
z a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
(%) c (a -> a -> Ratio a) -> a -> c (a -> Ratio a)
forall d b. Data d => c (d -> b) -> d -> c b
`k` a
a c (a -> Ratio a) -> a -> c (Ratio a)
forall d b. Data d => c (d -> b) -> d -> c b
`k` a
b
  toConstr :: Ratio a -> Constr
toConstr Ratio a
_ = Constr
ratioConstr
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Ratio a)
gunfold forall b r. Data b => c (b -> r) -> c r
k forall r. r -> c r
z Constr
c | Constr -> Int
constrIndex Constr
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = c (a -> Ratio a) -> c (Ratio a)
forall b r. Data b => c (b -> r) -> c r
k (c (a -> a -> Ratio a) -> c (a -> Ratio a)
forall b r. Data b => c (b -> r) -> c r
k ((a -> a -> Ratio a) -> c (a -> a -> Ratio a)
forall r. r -> c r
z a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
(%)))
  gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
_ Constr
_ = String -> c (Ratio a)
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.gunfold(Ratio)"
  dataTypeOf :: Ratio a -> DataType
dataTypeOf Ratio a
_  = DataType
ratioDataType


------------------------------------------------------------------------------

nilConstr :: Constr
nilConstr :: Constr
nilConstr    = DataType -> String -> [String] -> Fixity -> Constr
mkConstr DataType
listDataType String
"[]" [] Fixity
Prefix
consConstr :: Constr
consConstr :: Constr
consConstr   = DataType -> String -> [String] -> Fixity -> Constr
mkConstr DataType
listDataType String
"(:)" [] Fixity
Infix

listDataType :: DataType
listDataType :: DataType
listDataType = String -> [Constr] -> DataType
mkDataType String
"Prelude.[]" [Constr
nilConstr,Constr
consConstr]

-- | For historical reasons, the constructor name used for @(:)@ is
-- @"(:)"@. In a derived instance, it would be @":"@.
--
-- @since 4.0.0.0
instance Data a => Data [a] where
  gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> [a] -> c [a]
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
_ forall g. g -> c g
z []     = [a] -> c [a]
forall g. g -> c g
z []
  gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z (a
x:[a]
xs) = (a -> [a] -> [a]) -> c (a -> [a] -> [a])
forall g. g -> c g
z (:) c (a -> [a] -> [a]) -> a -> c ([a] -> [a])
forall d b. Data d => c (d -> b) -> d -> c b
`f` a
x c ([a] -> [a]) -> [a] -> c [a]
forall d b. Data d => c (d -> b) -> d -> c b
`f` [a]
xs
  toConstr :: [a] -> Constr
toConstr []    = Constr
nilConstr
  toConstr (a
_:[a]
_) = Constr
consConstr
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c [a]
gunfold forall b r. Data b => c (b -> r) -> c r
k forall r. r -> c r
z Constr
c = case Constr -> Int
constrIndex Constr
c of
                    Int
1 -> [a] -> c [a]
forall r. r -> c r
z []
                    Int
2 -> c ([a] -> [a]) -> c [a]
forall b r. Data b => c (b -> r) -> c r
k (c (a -> [a] -> [a]) -> c ([a] -> [a])
forall b r. Data b => c (b -> r) -> c r
k ((a -> [a] -> [a]) -> c (a -> [a] -> [a])
forall r. r -> c r
z (:)))
                    Int
_ -> String -> c [a]
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.gunfold(List)"
  dataTypeOf :: [a] -> DataType
dataTypeOf [a]
_ = DataType
listDataType
  dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c [a])
dataCast1 forall d. Data d => c (t d)
f  = c (t a) -> Maybe (c [a])
forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
       (a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 c (t a)
forall d. Data d => c (t d)
f

--
-- The gmaps are given as an illustration.
-- This shows that the gmaps for lists are different from list maps.
--
  gmapT :: (forall b. Data b => b -> b) -> [a] -> [a]
gmapT  forall b. Data b => b -> b
_   []     = []
  gmapT  forall b. Data b => b -> b
f   (a
x:[a]
xs) = (a -> a
forall b. Data b => b -> b
f a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a] -> [a]
forall b. Data b => b -> b
f [a]
xs)
  gmapQ :: forall u. (forall d. Data d => d -> u) -> [a] -> [u]
gmapQ  forall d. Data d => d -> u
_   []     = []
  gmapQ  forall d. Data d => d -> u
f   (a
x:[a]
xs) = [a -> u
forall d. Data d => d -> u
f a
x,[a] -> u
forall d. Data d => d -> u
f [a]
xs]
  gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> [a] -> m [a]
gmapM  forall d. Data d => d -> m d
_   []     = [a] -> m [a]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return []
  gmapM  forall d. Data d => d -> m d
f   (a
x:[a]
xs) = a -> m a
forall d. Data d => d -> m d
f a
x m a -> (a -> m [a]) -> m [a]
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
x' -> [a] -> m [a]
forall d. Data d => d -> m d
f [a]
xs m [a] -> ([a] -> m [a]) -> m [a]
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[a]
xs' -> [a] -> m [a]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x'a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs')


------------------------------------------------------------------------------

-- | @since 4.14.0.0
deriving instance (Typeable (a :: Type -> Type -> Type), Typeable b, Typeable c,
                   Data (a b c))
         => Data (WrappedArrow a b c)

-- | @since 4.14.0.0
deriving instance (Typeable (m :: Type -> Type), Typeable a, Data (m a))
         => Data (WrappedMonad m a)

-- | @since 4.14.0.0
deriving instance Data a => Data (ZipList a)

-- | @since 4.9.0.0
deriving instance Data a => Data (NonEmpty a)

-- | @since 4.0.0.0
deriving instance Data a => Data (Maybe a)

-- | @since 4.0.0.0
deriving instance Data Ordering

-- | @since 4.0.0.0
deriving instance (Data a, Data b) => Data (Either a b)

-- | @since 4.8.0.0
deriving instance Data Void

-- | @since 4.0.0.0
deriving instance Data ()

-- | @since 4.15
deriving instance Data a => Data (Solo a)

-- | @since 4.0.0.0
deriving instance (Data a, Data b) => Data (a,b)

-- | @since 4.0.0.0
deriving instance (Data a, Data b, Data c) => Data (a,b,c)

-- | @since 4.0.0.0
deriving instance (Data a, Data b, Data c, Data d)
         => Data (a,b,c,d)

-- | @since 4.0.0.0
deriving instance (Data a, Data b, Data c, Data d, Data e)
         => Data (a,b,c,d,e)

-- | @since 4.0.0.0
deriving instance (Data a, Data b, Data c, Data d, Data e, Data f)
         => Data (a,b,c,d,e,f)

-- | @since 4.0.0.0
deriving instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
         => Data (a,b,c,d,e,f,g)

------------------------------------------------------------------------------

-- | @since 4.8.0.0
instance Data a => Data (Ptr a) where
  toConstr :: Ptr a -> Constr
toConstr Ptr a
_   = String -> Constr
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.toConstr(Ptr)"
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Ptr a)
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
_  = String -> Constr -> c (Ptr a)
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.gunfold(Ptr)"
  dataTypeOf :: Ptr a -> DataType
dataTypeOf Ptr a
_ = String -> DataType
mkNoRepType String
"GHC.Ptr.Ptr"
  dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Ptr a))
dataCast1 forall d. Data d => c (t d)
x  = c (t a) -> Maybe (c (Ptr a))
forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
       (a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 c (t a)
forall d. Data d => c (t d)
x

-- | @since 4.18.0.0
deriving instance Data a => Data (ConstPtr a)

------------------------------------------------------------------------------

-- | @since 4.8.0.0
instance Data a => Data (ForeignPtr a) where
  toConstr :: ForeignPtr a -> Constr
toConstr ForeignPtr a
_   = String -> Constr
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.toConstr(ForeignPtr)"
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (ForeignPtr a)
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
_  = String -> Constr -> c (ForeignPtr a)
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.gunfold(ForeignPtr)"
  dataTypeOf :: ForeignPtr a -> DataType
dataTypeOf ForeignPtr a
_ = String -> DataType
mkNoRepType String
"GHC.ForeignPtr.ForeignPtr"
  dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (ForeignPtr a))
dataCast1 forall d. Data d => c (t d)
x  = c (t a) -> Maybe (c (ForeignPtr a))
forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
       (a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 c (t a)
forall d. Data d => c (t d)
x

-- | @since 4.11.0.0
deriving instance Data IntPtr

-- | @since 4.11.0.0
deriving instance Data WordPtr

------------------------------------------------------------------------------
-- The Data instance for Array preserves data abstraction at the cost of
-- inefficiency. We omit reflection services for the sake of data abstraction.
-- | @since 4.8.0.0
instance (Data a, Data b, Ix a) => Data (Array a b)
 where
  gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Array a b -> c (Array a b)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z Array a b
a = ([b] -> Array a b) -> c ([b] -> Array a b)
forall g. g -> c g
z ((a, a) -> [b] -> Array a b
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Array a b -> (a, a)
forall i e. Array i e -> (i, i)
bounds Array a b
a)) c ([b] -> Array a b) -> [b] -> c (Array a b)
forall d b. Data d => c (d -> b) -> d -> c b
`f` (Array a b -> [b]
forall i e. Array i e -> [e]
elems Array a b
a)
  toConstr :: Array a b -> Constr
toConstr Array a b
_   = String -> Constr
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.toConstr(Array)"
  gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Array a b)
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
_  = String -> Constr -> c (Array a b)
forall a. String -> a
errorWithoutStackTrace String
"Data.Data.gunfold(Array)"
  dataTypeOf :: Array a b -> DataType
dataTypeOf Array a b
_ = String -> DataType
mkNoRepType String
"Data.Array.Array"
  dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Array a b))
dataCast2 forall d e. (Data d, Data e) => c (t d e)
x  = c (t a b) -> Maybe (c (Array a b))
forall {k1} {k2} {k3} (c :: k1 -> *) (t :: k2 -> k3 -> k1)
       (t' :: k2 -> k3 -> k1) (a :: k2) (b :: k3).
(Typeable t, Typeable t') =>
c (t a b) -> Maybe (c (t' a b))
gcast2 c (t a b)
forall d e. (Data d, Data e) => c (t d e)
x

----------------------------------------------------------------------------
-- Data instance for Proxy

-- | @since 4.7.0.0
deriving instance (Data t) => Data (Proxy t)

-- | @since 4.7.0.0
deriving instance (a ~ b, Data a) => Data (a :~: b)

-- | @since 4.10.0.0
deriving instance (Typeable i, Typeable j, Typeable a, Typeable b,
                    (a :: i) ~~ (b :: j))
    => Data (a :~~: b)

-- | @since 4.7.0.0
deriving instance (Coercible a b, Data a, Data b) => Data (Coercion a b)

-- | @since 4.9.0.0
deriving instance Data a => Data (Identity a)

-- | @since 4.10.0.0
deriving instance (Typeable k, Data a, Typeable (b :: k)) => Data (Const a b)

-- | @since 4.7.0.0
deriving instance Data Version

----------------------------------------------------------------------------
-- Data instances for Data.Monoid wrappers

-- | @since 4.8.0.0
deriving instance Data a => Data (Dual a)

-- | @since 4.8.0.0
deriving instance Data All

-- | @since 4.8.0.0
deriving instance Data Any

-- | @since 4.8.0.0
deriving instance Data a => Data (Sum a)

-- | @since 4.8.0.0
deriving instance Data a => Data (Product a)

-- | @since 4.8.0.0
deriving instance Data a => Data (First a)

-- | @since 4.8.0.0
deriving instance Data a => Data (Last a)

-- | @since 4.8.0.0
deriving instance (Data (f a), Data a, Typeable f) => Data (Alt f a)

-- | @since 4.12.0.0
deriving instance (Data (f a), Data a, Typeable f) => Data (Ap f a)

----------------------------------------------------------------------------
-- Data instances for GHC.Generics representations

-- | @since 4.9.0.0
deriving instance Data p => Data (U1 p)

-- | @since 4.9.0.0
deriving instance Data p => Data (Par1 p)

-- | @since 4.9.0.0
deriving instance (Data (f p), Typeable f, Data p) => Data (Rec1 f p)

-- | @since 4.9.0.0
deriving instance (Typeable i, Data p, Data c) => Data (K1 i c p)

-- | @since 4.9.0.0
deriving instance (Data p, Data (f p), Typeable c, Typeable i, Typeable f)
    => Data (M1 i c f p)

-- | @since 4.9.0.0
deriving instance (Typeable f, Typeable g, Data p, Data (f p), Data (g p))
    => Data ((f :+: g) p)

-- | @since 4.9.0.0
deriving instance (Typeable (f :: Type -> Type), Typeable (g :: Type -> Type),
          Data p, Data (f (g p)))
    => Data ((f :.: g) p)

-- | @since 4.9.0.0
deriving instance Data p => Data (V1 p)

-- | @since 4.9.0.0
deriving instance (Typeable f, Typeable g, Data p, Data (f p), Data (g p))
    => Data ((f :*: g) p)

-- | @since 4.9.0.0
deriving instance Data Generics.Fixity

-- | @since 4.9.0.0
deriving instance Data Associativity

-- | @since 4.9.0.0
deriving instance Data SourceUnpackedness

-- | @since 4.9.0.0
deriving instance Data SourceStrictness

-- | @since 4.9.0.0
deriving instance Data DecidedStrictness

----------------------------------------------------------------------------
-- Data instances for Data.Ord

-- | @since 4.12.0.0
deriving instance Data a => Data (Down a)