--
-- Maintainer : libraries@haskell.org
-- Stability : experimental
--- Portability : non-portable
+-- Portability : non-portable (uses Data.Generics.Basics)
--
-- \"Scrap your boilerplate\" --- Generic programming in Haskell
-- See <http://www.cs.vu.nl/boilerplate/>. The present module
-- instantiates the class Data for Prelude-like datatypes.
+-- (This module does not export anything. It really just defines instances.)
--
-----------------------------------------------------------------------------
module Data.Generics.Instances
+
where
import GHC.Real( Ratio(..) ) -- 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 GHC.ForeignPtr -- So we can give Data instance for ForeignPtr
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
#include "Typeable.h"
trueConstr = mkConstr boolDataType "True" [] Prefix
boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
+
instance Data Bool where
toConstr False = falseConstr
toConstr True = trueConstr
- fromConstr c = case constrIndex c of
- 1 -> False
- 2 -> True
- _ -> error "fromConstr"
+ gunfold k z c = case constrIndex c of
+ 1 -> z False
+ 2 -> z True
+ _ -> error "gunfold"
dataTypeOf _ = boolDataType
instance Data Char where
toConstr x = mkStringConstr charType [x]
- fromConstr con = case constrRep con of
- (StringConstr [x]) -> x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (StringConstr [x]) -> z x
+ _ -> error "gunfold"
dataTypeOf _ = charType
instance Data Float where
toConstr x = mkFloatConstr floatType (realToFrac x)
- fromConstr con = case constrRep con of
- (FloatConstr x) -> realToFrac x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (FloatConstr x) -> z (realToFrac x)
+ _ -> error "gunfold"
dataTypeOf _ = floatType
instance Data Double where
toConstr = mkFloatConstr floatType
- fromConstr con = case constrRep con of
- (FloatConstr x) -> x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (FloatConstr x) -> z x
+ _ -> error "gunfold"
dataTypeOf _ = doubleType
instance Data Int where
toConstr x = mkIntConstr intType (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = intType
instance Data Integer where
toConstr = mkIntConstr integerType
- fromConstr con = case constrRep con of
- (IntConstr x) -> x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z x
+ _ -> error "gunfold"
dataTypeOf _ = integerType
instance Data Int8 where
toConstr x = mkIntConstr int8Type (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = int8Type
instance Data Int16 where
toConstr x = mkIntConstr int16Type (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = int16Type
instance Data Int32 where
toConstr x = mkIntConstr int32Type (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = int32Type
instance Data Int64 where
toConstr x = mkIntConstr int64Type (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = int64Type
instance Data Word where
toConstr x = mkIntConstr wordType (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = wordType
instance Data Word8 where
toConstr x = mkIntConstr word8Type (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = word8Type
instance Data Word16 where
toConstr x = mkIntConstr word16Type (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = word16Type
instance Data Word32 where
toConstr x = mkIntConstr word32Type (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = word32Type
instance Data Word64 where
toConstr x = mkIntConstr word64Type (fromIntegral x)
- fromConstr con = case constrRep con of
- (IntConstr x) -> fromIntegral x
- _ -> error "fromConstr"
+ gunfold k z c = case constrRep c of
+ (IntConstr x) -> z (fromIntegral x)
+ _ -> error "gunfold"
dataTypeOf _ = word64Type
instance (Data a, Integral a) => Data (Ratio a) where
toConstr _ = ratioConstr
- fromConstr c | constrIndex c == 1 = undefined :% undefined
- fromConstr _ = error "fromConstr"
- dataTypeOf _ = ratioDataType
+ gunfold k z c | constrIndex c == 1 = k (k (z (:%)))
+ gunfold _ _ _ = error "gunfold"
+ dataTypeOf _ = ratioDataType
------------------------------------------------------------------------------
-nilConstr = mkConstr listDataType "[]" [] Prefix
+nilConstr = mkConstr listDataType "[]" [] Prefix
consConstr = mkConstr listDataType "(:)" [] Infix
listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
gfoldl f z (x:xs) = z (:) `f` x `f` xs
toConstr [] = nilConstr
toConstr (_:_) = consConstr
- fromConstr c = case constrIndex c of
- 1 -> []
- 2 -> undefined:undefined
- _ -> error "fromConstr"
+ gunfold k z c = case constrIndex c of
+ 1 -> z []
+ 2 -> k (k (z (:)))
+ _ -> error "gunfold"
dataTypeOf _ = listDataType
- dataCast1 = gcast1
+ dataCast1 f = gcast1 f
--
-- The gmaps are given as an illustration.
gfoldl f z (Just x) = z Just `f` x
toConstr Nothing = nothingConstr
toConstr (Just _) = justConstr
- fromConstr c = case constrIndex c of
- 1 -> Nothing
- 2 -> Just undefined
- _ -> error "fromConstr"
+ gunfold k z c = case constrIndex c of
+ 1 -> z Nothing
+ 2 -> k (z Just)
+ _ -> error "gunfold"
dataTypeOf _ = maybeDataType
- dataCast1 = gcast1
+ dataCast1 f = gcast1 f
------------------------------------------------------------------------------
toConstr LT = ltConstr
toConstr EQ = eqConstr
toConstr GT = gtConstr
- fromConstr c = case constrIndex c of
- 1 -> LT
- 2 -> EQ
- 3 -> GT
- _ -> error "fromConstr"
+ gunfold k z c = case constrIndex c of
+ 1 -> z LT
+ 2 -> z EQ
+ 3 -> z GT
+ _ -> error "gunfold"
dataTypeOf _ = orderingDataType
gfoldl f z (Right a) = z Right `f` a
toConstr (Left _) = leftConstr
toConstr (Right _) = rightConstr
- fromConstr c = case constrIndex c of
- 1 -> Left undefined
- 2 -> Right undefined
- _ -> error "fromConstr"
+ gunfold k z c = case constrIndex c of
+ 1 -> k (z Left)
+ 2 -> k (z Right)
+ _ -> error "gunfold"
dataTypeOf _ = eitherDataType
- dataCast2 = gcast2
+ dataCast2 f = gcast2 f
------------------------------------------------------------------------------
instance (Data a, Data b) => Data (a -> b) where
toConstr _ = error "toConstr"
- fromConstr _ = error "fromConstr"
+ gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "Prelude.(->)"
- dataCast2 = gcast2
+ dataCast2 f = gcast2 f
------------------------------------------------------------------------------
tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
instance Data () where
- toConstr _ = tuple0Constr
- fromConstr c | constrIndex c == 1 = ()
- fromConstr _ = error "fromConstr"
- dataTypeOf _ = tuple0DataType
+ toConstr () = tuple0Constr
+ gunfold k z c | constrIndex c == 1 = z ()
+ gunfold _ _ _ = error "gunfold"
+ dataTypeOf _ = tuple0DataType
------------------------------------------------------------------------------
instance (Data a, Data b) => Data (a,b) where
gfoldl f z (a,b) = z (,) `f` a `f` b
- toConstr _ = tuple2Constr
- fromConstr c | constrIndex c == 1 = (undefined,undefined)
- fromConstr _ = error "fromConstr"
- dataTypeOf _ = tuple2DataType
- dataCast2 = gcast2
+ toConstr (a,b) = tuple2Constr
+ gunfold k z c | constrIndex c == 1 = k (k (z (,)))
+ gunfold _ _ _ = error "gunfold"
+ dataTypeOf _ = tuple2DataType
+ dataCast2 f = gcast2 f
------------------------------------------------------------------------------
instance (Data a, Data b, Data c) => Data (a,b,c) where
gfoldl f z (a,b,c) = z (,,) `f` a `f` b `f` c
- toConstr _ = tuple3Constr
- fromConstr c | constrIndex c == 1 = (undefined,undefined,undefined)
- fromConstr _ = error "fromConstr"
- dataTypeOf _ = tuple3DataType
+ toConstr (a,b,c) = tuple3Constr
+ gunfold k z c | constrIndex c == 1 = k (k (k (z (,,))))
+ gunfold _ _ _ = error "gunfold"
+ dataTypeOf _ = tuple3DataType
------------------------------------------------------------------------------
instance (Data a, Data b, Data c, Data d)
=> Data (a,b,c,d) where
gfoldl f z (a,b,c,d) = z (,,,) `f` a `f` b `f` c `f` d
- toConstr _ = tuple4Constr
- fromConstr c = case constrIndex c of
- 1 -> (undefined,undefined,undefined,undefined)
- _ -> error "fromConstr"
+ toConstr (a,b,c,d) = tuple4Constr
+ gunfold k z c = case constrIndex c of
+ 1 -> k (k (k (k (z (,,,)))))
+ _ -> error "gunfold"
dataTypeOf _ = tuple4DataType
instance (Data a, Data b, Data c, Data d, Data e)
=> Data (a,b,c,d,e) where
gfoldl f z (a,b,c,d,e) = z (,,,,) `f` a `f` b `f` c `f` d `f` e
- toConstr _ = tuple5Constr
- fromConstr c = case constrIndex c of
- 1 -> (undefined,undefined,undefined,undefined,undefined)
- _ -> error "fromConstr"
+ toConstr (a,b,c,d,e) = tuple5Constr
+ gunfold k z c = case constrIndex c of
+ 1 -> k (k (k (k (k (z (,,,,))))))
+ _ -> error "gunfold"
dataTypeOf _ = tuple5DataType
instance (Data a, Data b, Data c, Data d, Data e, Data f)
=> Data (a,b,c,d,e,f) where
gfoldl f z (a,b,c,d,e,f') = z (,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f'
- toConstr _ = tuple6Constr
- fromConstr c =
- case constrIndex c of
- 1 -> (undefined,undefined,undefined,undefined,undefined,undefined)
- _ -> error "fromConstr"
+ toConstr (a,b,c,d,e,f) = tuple6Constr
+ gunfold k z c = case constrIndex c of
+ 1 -> k (k (k (k (k (k (z (,,,,,)))))))
+ _ -> error "gunfold"
dataTypeOf _ = tuple6DataType
=> Data (a,b,c,d,e,f,g) where
gfoldl f z (a,b,c,d,e,f',g) =
z (,,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f' `f` g
- toConstr _ = tuple7Constr
- fromConstr c = case constrIndex c of
- 1 -> (undefined,undefined,undefined,undefined,undefined,undefined,undefined)
- _ -> error "fromConstr"
+ toConstr (a,b,c,d,e,f,g) = tuple7Constr
+ gunfold k z c = case constrIndex c of
+ 1 -> k (k (k (k (k (k (k (z (,,,,,,))))))))
+ _ -> error "gunfold"
dataTypeOf _ = tuple7DataType
instance Data TypeRep where
toConstr _ = error "toConstr"
- fromConstr _ = error "fromConstr"
+ gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "Data.Typeable.TypeRep"
instance Data TyCon where
toConstr _ = error "toConstr"
- fromConstr _ = error "fromConstr"
+ gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "Data.Typeable.TyCon"
instance Data DataType where
toConstr _ = error "toConstr"
- fromConstr _ = error "fromConstr"
+ gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "Data.Generics.Basics.DataType"
instance Typeable a => Data (IO a) where
toConstr _ = error "toConstr"
- fromConstr _ = error "fromConstr"
+ gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "GHC.IOBase.IO"
instance Data Handle where
toConstr _ = error "toConstr"
- fromConstr _ = error "fromConstr"
+ gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "GHC.IOBase.Handle"
instance Typeable a => Data (Ptr a) where
toConstr _ = error "toConstr"
- fromConstr _ = error "fromConstr"
+ gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
instance Typeable a => Data (StablePtr a) where
toConstr _ = error "toConstr"
- fromConstr _ = error "fromConstr"
+ gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "GHC.Stable.StablePtr"
instance Typeable a => Data (IORef a) where
toConstr _ = error "toConstr"
- fromConstr _ = error "fromConstr"
+ gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNorepType "GHC.IOBase.IORef"
------------------------------------------------------------------------------
+
+
+instance Typeable a => Data (ForeignPtr a) where
+ toConstr _ = error "toConstr"
+ gunfold _ _ = error "gunfold"
+ dataTypeOf _ = mkNorepType "GHC.ForeignPtr.ForeignPtr"
+
+
+------------------------------------------------------------------------------
+
+
+instance (Typeable s, Typeable a) => Data (ST s a) where
+ toConstr _ = error "toConstr"
+ gunfold _ _ = error "gunfold"
+ dataTypeOf _ = mkNorepType "GHC.ST.ST"
+
+
+------------------------------------------------------------------------------
+
+
+instance Data ThreadId where
+ toConstr _ = error "toConstr"
+ gunfold _ _ = error "gunfold"
+ dataTypeOf _ = mkNorepType "GHC.Conc.ThreadId"
+
+
+------------------------------------------------------------------------------
+
+
+instance Typeable a => Data (TVar a) where
+ toConstr _ = error "toConstr"
+ gunfold _ _ = error "gunfold"
+ dataTypeOf _ = mkNorepType "GHC.Conc.TVar"
+
+
+------------------------------------------------------------------------------
+
+
+instance Typeable a => Data (MVar a) where
+ toConstr _ = error "toConstr"
+ gunfold _ _ = error "gunfold"
+ dataTypeOf _ = mkNorepType "GHC.Conc.MVar"
+
+
+------------------------------------------------------------------------------
+
+
+instance Typeable a => Data (STM a) where
+ toConstr _ = error "toConstr"
+ gunfold _ _ = error "gunfold"
+ dataTypeOf _ = mkNorepType "GHC.Conc.STM"
+
+
+------------------------------------------------------------------------------
+-- The Data instance for Array preserves data abstraction at the cost of inefficiency.
+-- We omit reflection services for the sake of data abstraction.
+instance (Typeable a, Data b, Ix a) => Data (Array a b)
+ where
+ gfoldl f z a = z (listArray (bounds a)) `f` (elems a)
+ toConstr _ = error "toConstr"
+ gunfold _ _ = error "gunfold"
+ dataTypeOf _ = mkNorepType "Data.Array.Array"
+