module Data.Data (
module Data.Typeable,
Data(
gfoldl,
gunfold,
toConstr,
dataTypeOf,
dataCast1,
dataCast2,
gmapT,
gmapQ,
gmapQl,
gmapQr,
gmapQi,
gmapM,
gmapMp,
gmapMo
),
DataType,
mkDataType,
mkIntType,
mkFloatType,
mkCharType,
mkNoRepType,
dataTypeName,
DataRep(..),
dataTypeRep,
repConstr,
isAlgType,
dataTypeConstrs,
indexConstr,
maxConstrIndex,
isNorepType,
Constr,
ConIndex,
Fixity(..),
mkConstr,
mkIntegralConstr,
mkRealConstr,
mkCharConstr,
constrType,
ConstrRep(..),
constrRep,
constrFields,
constrFixity,
constrIndex,
showConstr,
readConstr,
tyconUQname,
tyconModule,
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.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 Text.Read( reads )
import Control.Applicative (WrappedArrow(..), WrappedMonad(..), ZipList(..))
import Data.Functor.Identity
import Data.Int
import Data.Type.Coercion
import Data.Word
import GHC.Real
import GHC.Ptr
import GHC.ForeignPtr
import Foreign.Ptr (IntPtr(..), WordPtr(..))
import GHC.Arr
import qualified GHC.Generics as Generics (Fixity(..))
import GHC.Generics hiding (Fixity(..))
class Typeable a => Data a where
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> a
-> c a
gfoldl _ z = z
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c a
toConstr :: a -> Constr
dataTypeOf :: a -> DataType
dataCast1 :: Typeable t
=> (forall d. Data d => c (t d))
-> Maybe (c a)
dataCast1 _ = Nothing
dataCast2 :: Typeable t
=> (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c a)
dataCast2 _ = Nothing
gmapT :: (forall b. Data b => b -> b) -> a -> a
gmapT f x0 = runIdentity (gfoldl k Identity x0)
where
k :: Data d => Identity (d->b) -> d -> Identity b
k (Identity c) x = Identity (c (f x))
gmapQl :: forall r r'. (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
gmapQl o r f = getConst . gfoldl k z
where
k :: Data d => Const r (d->b) -> d -> Const r b
k c x = Const $ (getConst c) `o` f x
z :: g -> Const r g
z _ = Const r
gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
gmapQr o r0 f x0 = unQr (gfoldl k (const (Qr id)) x0) r0
where
k :: Data d => Qr r (d->b) -> d -> Qr r b
k (Qr c) x = Qr (\r -> c (f x `o` r))
gmapQ :: (forall d. Data d => d -> u) -> a -> [u]
gmapQ f = gmapQr (:) [] f
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> a -> u
gmapQi i f x = case gfoldl k z x of { Qi _ q -> fromJust q }
where
k :: Data d => Qi u (d -> b) -> d -> Qi u b
k (Qi i' q) a = Qi (i' 1) (if i==i' then Just (f a) else q)
z :: g -> Qi q g
z _ = Qi 0 Nothing
gmapM :: forall m. Monad m => (forall d. Data d => d -> m d) -> a -> m a
gmapM f = gfoldl k return
where
k :: Data d => m (d -> b) -> d -> m b
k c x = do c' <- c
x' <- f x
return (c' x')
gmapMp :: forall m. MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a
gmapMp f x = unMp (gfoldl k z x) >>= \(x',b) ->
if b then return x' else mzero
where
z :: g -> Mp m g
z g = Mp (return (g,False))
k :: Data d => Mp m (d -> b) -> d -> Mp m b
k (Mp c) y
= Mp ( c >>= \(h, b) ->
(f y >>= \y' -> return (h y', True))
`mplus` return (h y, b)
)
gmapMo :: forall m. MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a
gmapMo f x = unMp (gfoldl k z x) >>= \(x',b) ->
if b then return x' else mzero
where
z :: g -> Mp m g
z g = Mp (return (g,False))
k :: Data d => Mp m (d -> b) -> d -> Mp m b
k (Mp c) y
= Mp ( c >>= \(h,b) -> if b
then return (h y, b)
else (f y >>= \y' -> return (h y',True))
`mplus` return (h y, b)
)
data Qi q a = Qi Int (Maybe q)
newtype Qr r a = Qr { unQr :: r -> r }
newtype Mp m x = Mp { unMp :: m (x, Bool) }
fromConstr :: Data a => Constr -> a
fromConstr = fromConstrB (errorWithoutStackTrace "Data.Data.fromConstr")
fromConstrB :: Data a
=> (forall d. Data d => d)
-> Constr
-> a
fromConstrB f = runIdentity . gunfold k z
where
k :: forall b r. Data b => Identity (b -> r) -> Identity r
k c = Identity (runIdentity c f)
z :: forall r. r -> Identity r
z = Identity
fromConstrM :: forall m a. (Monad m, Data a)
=> (forall d. Data d => m d)
-> Constr
-> m a
fromConstrM f = gunfold k z
where
k :: forall b r. Data b => m (b -> r) -> m r
k c = do { c' <- c; b <- f; return (c' b) }
z :: forall r. r -> m r
z = return
data DataType = DataType
{ tycon :: String
, datarep :: DataRep
}
deriving Show
data Constr = Constr
{ conrep :: ConstrRep
, constring :: String
, confields :: [String]
, confixity :: Fixity
, datatype :: DataType
}
instance Show Constr where
show = constring
instance Eq Constr where
c == c' = constrRep c == constrRep c'
data DataRep = AlgRep [Constr]
| IntRep
| FloatRep
| CharRep
| NoRep
deriving ( Eq
, Show
)
data ConstrRep = AlgConstr ConIndex
| IntConstr Integer
| FloatConstr Rational
| CharConstr Char
deriving ( Eq
, Show
)
type ConIndex = Int
data Fixity = Prefix
| Infix
deriving ( Eq
, Show
)
dataTypeName :: DataType -> String
dataTypeName = tycon
dataTypeRep :: DataType -> DataRep
dataTypeRep = datarep
constrType :: Constr -> DataType
constrType = datatype
constrRep :: Constr -> ConstrRep
constrRep = conrep
repConstr :: DataType -> ConstrRep -> Constr
repConstr dt cr =
case (dataTypeRep dt, cr) of
(AlgRep cs, AlgConstr i) -> cs !! (i1)
(IntRep, IntConstr i) -> mkIntegralConstr dt i
(FloatRep, FloatConstr f) -> mkRealConstr dt f
(CharRep, CharConstr c) -> mkCharConstr dt c
_ -> errorWithoutStackTrace "Data.Data.repConstr: The given ConstrRep does not fit to the given DataType."
mkDataType :: String -> [Constr] -> DataType
mkDataType str cs = DataType
{ tycon = str
, datarep = AlgRep cs
}
mkConstr :: DataType -> String -> [String] -> Fixity -> Constr
mkConstr dt str fields fix =
Constr
{ conrep = AlgConstr idx
, constring = str
, confields = fields
, confixity = fix
, datatype = dt
}
where
idx = head [ i | (c,i) <- dataTypeConstrs dt `zip` [1..],
showConstr c == str ]
dataTypeConstrs :: DataType -> [Constr]
dataTypeConstrs dt = case datarep dt of
(AlgRep cons) -> cons
_ -> errorWithoutStackTrace $ "Data.Data.dataTypeConstrs is not supported for "
dataTypeName dt
", as it is not an algebraic data type."
constrFields :: Constr -> [String]
constrFields = confields
constrFixity :: Constr -> Fixity
constrFixity = confixity
showConstr :: Constr -> String
showConstr = constring
readConstr :: DataType -> String -> Maybe Constr
readConstr dt str =
case dataTypeRep dt of
AlgRep cons -> idx cons
IntRep -> mkReadCon (\i -> (mkPrimCon dt str (IntConstr i)))
FloatRep -> mkReadCon ffloat
CharRep -> mkReadCon (\c -> (mkPrimCon dt str (CharConstr c)))
NoRep -> Nothing
where
mkReadCon :: Read t => (t -> Constr) -> Maybe Constr
mkReadCon f = case (reads str) of
[(t,"")] -> Just (f t)
_ -> Nothing
idx :: [Constr] -> Maybe Constr
idx cons = let fit = filter ((==) str . showConstr) cons
in if fit == []
then Nothing
else Just (head fit)
ffloat :: Double -> Constr
ffloat = mkPrimCon dt str . FloatConstr . toRational
isAlgType :: DataType -> Bool
isAlgType dt = case datarep dt of
(AlgRep _) -> True
_ -> False
indexConstr :: DataType -> ConIndex -> Constr
indexConstr dt idx = case datarep dt of
(AlgRep cs) -> cs !! (idx1)
_ -> errorWithoutStackTrace $ "Data.Data.indexConstr is not supported for "
dataTypeName dt
", as it is not an algebraic data type."
constrIndex :: Constr -> ConIndex
constrIndex con = case constrRep con of
(AlgConstr idx) -> idx
_ -> errorWithoutStackTrace $ "Data.Data.constrIndex is not supported for "
dataTypeName (constrType con)
", as it is not an algebraic data type."
maxConstrIndex :: DataType -> ConIndex
maxConstrIndex dt = case dataTypeRep dt of
AlgRep cs -> length cs
_ -> errorWithoutStackTrace $ "Data.Data.maxConstrIndex is not supported for "
dataTypeName dt
", as it is not an algebraic data type."
mkIntType :: String -> DataType
mkIntType = mkPrimType IntRep
mkFloatType :: String -> DataType
mkFloatType = mkPrimType FloatRep
mkCharType :: String -> DataType
mkCharType = mkPrimType CharRep
mkPrimType :: DataRep -> String -> DataType
mkPrimType dr str = DataType
{ tycon = str
, datarep = dr
}
mkPrimCon :: DataType -> String -> ConstrRep -> Constr
mkPrimCon dt str cr = Constr
{ datatype = dt
, conrep = cr
, constring = str
, confields = errorWithoutStackTrace "Data.Data.confields"
, confixity = errorWithoutStackTrace "Data.Data.confixity"
}
mkIntegralConstr :: (Integral a, Show a) => DataType -> a -> Constr
mkIntegralConstr dt i = case datarep dt of
IntRep -> mkPrimCon dt (show i) (IntConstr (toInteger i))
_ -> errorWithoutStackTrace $ "Data.Data.mkIntegralConstr is not supported for "
dataTypeName dt
", as it is not an Integral data type."
mkRealConstr :: (Real a, Show a) => DataType -> a -> Constr
mkRealConstr dt f = case datarep dt of
FloatRep -> mkPrimCon dt (show f) (FloatConstr (toRational f))
_ -> errorWithoutStackTrace $ "Data.Data.mkRealConstr is not supported for "
dataTypeName dt
", as it is not a Real data type."
mkCharConstr :: DataType -> Char -> Constr
mkCharConstr dt c = case datarep dt of
CharRep -> mkPrimCon dt (show c) (CharConstr c)
_ -> errorWithoutStackTrace $ "Data.Data.mkCharConstr is not supported for "
dataTypeName dt
", as it is not an Char data type."
mkNoRepType :: String -> DataType
mkNoRepType str = DataType
{ tycon = str
, datarep = NoRep
}
isNorepType :: DataType -> Bool
isNorepType dt = case datarep dt of
NoRep -> True
_ -> False
tyconUQname :: String -> String
tyconUQname x = let x' = dropWhile (not . (==) '.') x
in if x' == [] then x else tyconUQname (tail x')
tyconModule :: String -> String
tyconModule x = let (a,b) = break ((==) '.') x
in if b == ""
then b
else a tyconModule' (tail b)
where
tyconModule' y = let y' = tyconModule y
in if y' == "" then "" else ('.':y')
deriving instance Data Bool
charType :: DataType
charType = mkCharType "Prelude.Char"
instance Data Char where
toConstr x = mkCharConstr charType x
gunfold _ z c = case constrRep c of
(CharConstr x) -> z x
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Char."
dataTypeOf _ = charType
floatType :: DataType
floatType = mkFloatType "Prelude.Float"
instance Data Float where
toConstr = mkRealConstr floatType
gunfold _ z c = case constrRep c of
(FloatConstr x) -> z (realToFrac x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Float."
dataTypeOf _ = floatType
doubleType :: DataType
doubleType = mkFloatType "Prelude.Double"
instance Data Double where
toConstr = mkRealConstr doubleType
gunfold _ z c = case constrRep c of
(FloatConstr x) -> z (realToFrac x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Double."
dataTypeOf _ = doubleType
intType :: DataType
intType = mkIntType "Prelude.Int"
instance Data Int where
toConstr x = mkIntegralConstr intType x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Int."
dataTypeOf _ = intType
integerType :: DataType
integerType = mkIntType "Prelude.Integer"
instance Data Integer where
toConstr = mkIntegralConstr integerType
gunfold _ z c = case constrRep c of
(IntConstr x) -> z x
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Integer."
dataTypeOf _ = integerType
naturalType :: DataType
naturalType = mkIntType "Numeric.Natural.Natural"
instance Data Natural where
toConstr x = mkIntegralConstr naturalType x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Natural"
dataTypeOf _ = naturalType
int8Type :: DataType
int8Type = mkIntType "Data.Int.Int8"
instance Data Int8 where
toConstr x = mkIntegralConstr int8Type x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Int8."
dataTypeOf _ = int8Type
int16Type :: DataType
int16Type = mkIntType "Data.Int.Int16"
instance Data Int16 where
toConstr x = mkIntegralConstr int16Type x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Int16."
dataTypeOf _ = int16Type
int32Type :: DataType
int32Type = mkIntType "Data.Int.Int32"
instance Data Int32 where
toConstr x = mkIntegralConstr int32Type x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Int32."
dataTypeOf _ = int32Type
int64Type :: DataType
int64Type = mkIntType "Data.Int.Int64"
instance Data Int64 where
toConstr x = mkIntegralConstr int64Type x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Int64."
dataTypeOf _ = int64Type
wordType :: DataType
wordType = mkIntType "Data.Word.Word"
instance Data Word where
toConstr x = mkIntegralConstr wordType x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Word"
dataTypeOf _ = wordType
word8Type :: DataType
word8Type = mkIntType "Data.Word.Word8"
instance Data Word8 where
toConstr x = mkIntegralConstr word8Type x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Word8."
dataTypeOf _ = word8Type
word16Type :: DataType
word16Type = mkIntType "Data.Word.Word16"
instance Data Word16 where
toConstr x = mkIntegralConstr word16Type x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Word16."
dataTypeOf _ = word16Type
word32Type :: DataType
word32Type = mkIntType "Data.Word.Word32"
instance Data Word32 where
toConstr x = mkIntegralConstr word32Type x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Word32."
dataTypeOf _ = word32Type
word64Type :: DataType
word64Type = mkIntType "Data.Word.Word64"
instance Data Word64 where
toConstr x = mkIntegralConstr word64Type x
gunfold _ z c = case constrRep c of
(IntConstr x) -> z (fromIntegral x)
_ -> errorWithoutStackTrace $ "Data.Data.gunfold: Constructor " show c
" is not of type Word64."
dataTypeOf _ = word64Type
ratioConstr :: Constr
ratioConstr = mkConstr ratioDataType ":%" [] Infix
ratioDataType :: DataType
ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
instance (Data a, Integral a) => Data (Ratio a) where
gfoldl k z (a :% b) = z (%) `k` a `k` b
toConstr _ = ratioConstr
gunfold k z c | constrIndex c == 1 = k (k (z (%)))
gunfold _ _ _ = errorWithoutStackTrace "Data.Data.gunfold(Ratio)"
dataTypeOf _ = ratioDataType
nilConstr :: Constr
nilConstr = mkConstr listDataType "[]" [] Prefix
consConstr :: Constr
consConstr = mkConstr listDataType "(:)" [] Infix
listDataType :: DataType
listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
instance Data a => Data [a] where
gfoldl _ z [] = z []
gfoldl f z (x:xs) = z (:) `f` x `f` xs
toConstr [] = nilConstr
toConstr (_:_) = consConstr
gunfold k z c = case constrIndex c of
1 -> z []
2 -> k (k (z (:)))
_ -> errorWithoutStackTrace "Data.Data.gunfold(List)"
dataTypeOf _ = listDataType
dataCast1 f = gcast1 f
gmapT _ [] = []
gmapT f (x:xs) = (f x:f xs)
gmapQ _ [] = []
gmapQ f (x:xs) = [f x,f xs]
gmapM _ [] = return []
gmapM f (x:xs) = f x >>= \x' -> f xs >>= \xs' -> return (x':xs')
deriving instance (Typeable (a :: Type -> Type -> Type), Typeable b, Typeable c,
Data (a b c))
=> Data (WrappedArrow a b c)
deriving instance (Typeable (m :: Type -> Type), Typeable a, Data (m a))
=> Data (WrappedMonad m a)
deriving instance Data a => Data (ZipList a)
deriving instance Data a => Data (NonEmpty a)
deriving instance Data a => Data (Maybe a)
deriving instance Data Ordering
deriving instance (Data a, Data b) => Data (Either a b)
deriving instance Data ()
deriving instance (Data a, Data b) => Data (a,b)
deriving instance (Data a, Data b, Data c) => Data (a,b,c)
deriving instance (Data a, Data b, Data c, Data d)
=> Data (a,b,c,d)
deriving instance (Data a, Data b, Data c, Data d, Data e)
=> Data (a,b,c,d,e)
deriving instance (Data a, Data b, Data c, Data d, Data e, Data f)
=> Data (a,b,c,d,e,f)
deriving instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
=> Data (a,b,c,d,e,f,g)
instance Data a => Data (Ptr a) where
toConstr _ = errorWithoutStackTrace "Data.Data.toConstr(Ptr)"
gunfold _ _ = errorWithoutStackTrace "Data.Data.gunfold(Ptr)"
dataTypeOf _ = mkNoRepType "GHC.Ptr.Ptr"
dataCast1 x = gcast1 x
instance Data a => Data (ForeignPtr a) where
toConstr _ = errorWithoutStackTrace "Data.Data.toConstr(ForeignPtr)"
gunfold _ _ = errorWithoutStackTrace "Data.Data.gunfold(ForeignPtr)"
dataTypeOf _ = mkNoRepType "GHC.ForeignPtr.ForeignPtr"
dataCast1 x = gcast1 x
deriving instance Data IntPtr
deriving instance Data WordPtr
instance (Data a, Data b, Ix a) => Data (Array a b)
where
gfoldl f z a = z (listArray (bounds a)) `f` (elems a)
toConstr _ = errorWithoutStackTrace "Data.Data.toConstr(Array)"
gunfold _ _ = errorWithoutStackTrace "Data.Data.gunfold(Array)"
dataTypeOf _ = mkNoRepType "Data.Array.Array"
dataCast2 x = gcast2 x
deriving instance (Data t) => Data (Proxy t)
deriving instance (a ~ b, Data a) => Data (a :~: b)
deriving instance (Typeable i, Typeable j, Typeable a, Typeable b,
(a :: i) ~~ (b :: j))
=> Data (a :~~: b)
deriving instance (Coercible a b, Data a, Data b) => Data (Coercion a b)
deriving instance Data a => Data (Identity a)
deriving instance (Typeable k, Data a, Typeable (b :: k)) => Data (Const a b)
deriving instance Data Version
deriving instance Data a => Data (Dual a)
deriving instance Data All
deriving instance Data Any
deriving instance Data a => Data (Sum a)
deriving instance Data a => Data (Product a)
deriving instance Data a => Data (First a)
deriving instance Data a => Data (Last a)
deriving instance (Data (f a), Data a, Typeable f) => Data (Alt f a)
deriving instance (Data (f a), Data a, Typeable f) => Data (Ap f a)
deriving instance Data p => Data (U1 p)
deriving instance Data p => Data (Par1 p)
deriving instance (Data (f p), Typeable f, Data p) => Data (Rec1 f p)
deriving instance (Typeable i, Data p, Data c) => Data (K1 i c p)
deriving instance (Data p, Data (f p), Typeable c, Typeable i, Typeable f)
=> Data (M1 i c f p)
deriving instance (Typeable f, Typeable g, Data p, Data (f p), Data (g p))
=> Data ((f : : g) p)
deriving instance (Typeable (f :: Type -> Type), Typeable (g :: Type -> Type),
Data p, Data (f (g p)))
=> Data ((f :.: g) p)
deriving instance Data p => Data (V1 p)
deriving instance (Typeable f, Typeable g, Data p, Data (f p), Data (g p))
=> Data ((f :*: g) p)
deriving instance Data Generics.Fixity
deriving instance Data Associativity
deriving instance Data SourceUnpackedness
deriving instance Data SourceStrictness
deriving instance Data DecidedStrictness
deriving instance Data a => Data (Down a)