Fix warnings in Data.Generics.*
[ghc-base.git] / Data / Generics / Instances.hs
index 1157dc5..daa11bf 100644 (file)
@@ -6,7 +6,7 @@
 -- 
 -- 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
@@ -15,9 +15,8 @@
 --
 -----------------------------------------------------------------------------
 
-module Data.Generics.Instances 
-
-where
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+module Data.Generics.Instances where
 
 
 ------------------------------------------------------------------------------
@@ -32,34 +31,37 @@ import Data.Typeable
 import Data.Int              -- So we can give Data instance for Int8, ...
 import Data.Word             -- So we can give Data instance for Word8, ...
 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.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"
 
 
 ------------------------------------------------------------------------------
 --
---     Instances of the Data class for Prelude-like types.
---     We define top-level definitions for representations.
+--      Instances of the Data class for Prelude-like types.
+--      We define top-level definitions for representations.
 --
 ------------------------------------------------------------------------------
 
 
+falseConstr :: Constr
 falseConstr  = mkConstr boolDataType "False" [] Prefix
+trueConstr :: Constr
 trueConstr   = mkConstr boolDataType "True"  [] Prefix
-boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
 
+boolDataType :: DataType
+boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
 
 instance Data Bool where
   toConstr False = falseConstr
   toConstr True  = trueConstr
-  gunfold k z c  = case constrIndex c of
+  gunfold _ z c  = case constrIndex c of
                      1 -> z False
                      2 -> z True
                      _ -> error "gunfold"
@@ -68,12 +70,12 @@ instance Data Bool where
 
 ------------------------------------------------------------------------------
 
-
+charType :: DataType
 charType = mkStringType "Prelude.Char"
 
 instance Data Char where
   toConstr x = mkStringConstr charType [x]
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (StringConstr [x]) -> z x
                     _ -> error "gunfold"
   dataTypeOf _ = charType
@@ -81,12 +83,12 @@ instance Data Char where
 
 ------------------------------------------------------------------------------
 
-
+floatType :: DataType
 floatType = mkFloatType "Prelude.Float"
 
 instance Data Float where
   toConstr x = mkFloatConstr floatType (realToFrac x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (FloatConstr x) -> z (realToFrac x)
                     _ -> error "gunfold"
   dataTypeOf _ = floatType
@@ -94,12 +96,12 @@ instance Data Float where
 
 ------------------------------------------------------------------------------
 
-
+doubleType :: DataType
 doubleType = mkFloatType "Prelude.Double"
 
 instance Data Double where
   toConstr = mkFloatConstr floatType
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (FloatConstr x) -> z x
                     _ -> error "gunfold"
   dataTypeOf _ = doubleType
@@ -107,12 +109,12 @@ instance Data Double where
 
 ------------------------------------------------------------------------------
 
-
+intType :: DataType
 intType = mkIntType "Prelude.Int"
 
 instance Data Int where
   toConstr x = mkIntConstr intType (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = intType
@@ -120,12 +122,12 @@ instance Data Int where
 
 ------------------------------------------------------------------------------
 
-
+integerType :: DataType
 integerType = mkIntType "Prelude.Integer"
 
 instance Data Integer where
   toConstr = mkIntConstr integerType
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z x
                     _ -> error "gunfold"
   dataTypeOf _ = integerType
@@ -133,12 +135,12 @@ instance Data Integer where
 
 ------------------------------------------------------------------------------
 
-
+int8Type :: DataType
 int8Type = mkIntType "Data.Int.Int8"
 
 instance Data Int8 where
   toConstr x = mkIntConstr int8Type (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = int8Type
@@ -146,12 +148,12 @@ instance Data Int8 where
 
 ------------------------------------------------------------------------------
 
-
+int16Type :: DataType
 int16Type = mkIntType "Data.Int.Int16"
 
 instance Data Int16 where
   toConstr x = mkIntConstr int16Type (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = int16Type
@@ -159,12 +161,12 @@ instance Data Int16 where
 
 ------------------------------------------------------------------------------
 
-
+int32Type :: DataType
 int32Type = mkIntType "Data.Int.Int32"
 
 instance Data Int32 where
   toConstr x = mkIntConstr int32Type (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = int32Type
@@ -172,12 +174,12 @@ instance Data Int32 where
 
 ------------------------------------------------------------------------------
 
-
+int64Type :: DataType
 int64Type = mkIntType "Data.Int.Int64"
 
 instance Data Int64 where
   toConstr x = mkIntConstr int64Type (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = int64Type
@@ -185,12 +187,12 @@ instance Data Int64 where
 
 ------------------------------------------------------------------------------
 
-
+wordType :: DataType
 wordType = mkIntType "Data.Word.Word"
 
 instance Data Word where
   toConstr x = mkIntConstr wordType (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = wordType
@@ -198,12 +200,12 @@ instance Data Word where
 
 ------------------------------------------------------------------------------
 
-
+word8Type :: DataType
 word8Type = mkIntType "Data.Word.Word8"
 
 instance Data Word8 where
   toConstr x = mkIntConstr word8Type (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = word8Type
@@ -211,12 +213,12 @@ instance Data Word8 where
 
 ------------------------------------------------------------------------------
 
-
+word16Type :: DataType
 word16Type = mkIntType "Data.Word.Word16"
 
 instance Data Word16 where
   toConstr x = mkIntConstr word16Type (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = word16Type
@@ -224,12 +226,12 @@ instance Data Word16 where
 
 ------------------------------------------------------------------------------
 
-
+word32Type :: DataType
 word32Type = mkIntType "Data.Word.Word32"
 
 instance Data Word32 where
   toConstr x = mkIntConstr word32Type (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = word32Type
@@ -237,12 +239,12 @@ instance Data Word32 where
 
 ------------------------------------------------------------------------------
 
-
+word64Type :: DataType
 word64Type = mkIntType "Data.Word.Word64"
 
 instance Data Word64 where
   toConstr x = mkIntConstr word64Type (fromIntegral x)
-  gunfold k z c = case constrRep c of
+  gunfold _ z c = case constrRep c of
                     (IntConstr x) -> z (fromIntegral x)
                     _ -> error "gunfold"
   dataTypeOf _ = word64Type
@@ -250,8 +252,10 @@ instance Data Word64 where
 
 ------------------------------------------------------------------------------
 
-
+ratioConstr :: Constr
 ratioConstr = mkConstr ratioDataType ":%" [] Infix
+
+ratioDataType :: DataType
 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
 
 instance (Data a, Integral a) => Data (Ratio a) where
@@ -263,13 +267,16 @@ instance (Data a, Integral a) => Data (Ratio a) where
 
 ------------------------------------------------------------------------------
 
-
+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 f z []     = z []
+  gfoldl _ z []     = z []
   gfoldl f z (x:xs) = z (:) `f` x `f` xs
   toConstr []    = nilConstr
   toConstr (_:_) = consConstr
@@ -278,29 +285,32 @@ instance Data a => Data [a] where
                     2 -> k (k (z (:)))
                     _ -> error "gunfold"
   dataTypeOf _ = listDataType
-  dataCast1    = gcast1
+  dataCast1 f  = gcast1 f
 
 --
 -- The gmaps are given as an illustration.
 -- This shows that the gmaps for lists are different from list maps.
 --
-  gmapT  f   []     = []
+  gmapT  _   []     = []
   gmapT  f   (x:xs) = (f x:f xs)
-  gmapQ  f   []     = []
+  gmapQ  _   []     = []
   gmapQ  f   (x:xs) = [f x,f xs]
-  gmapM  f   []     = return []
+  gmapM  _   []     = return []
   gmapM  f   (x:xs) = f x >>= \x' -> f xs >>= \xs' -> return (x':xs')
 
 
 ------------------------------------------------------------------------------
 
-
+nothingConstr :: Constr
 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
+justConstr :: Constr
 justConstr    = mkConstr maybeDataType "Just"    [] Prefix
+
+maybeDataType :: DataType
 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
 
 instance Data a => Data (Maybe a) where
-  gfoldl f z Nothing  = z Nothing
+  gfoldl _ z Nothing  = z Nothing
   gfoldl f z (Just x) = z Just `f` x
   toConstr Nothing  = nothingConstr
   toConstr (Just _) = justConstr
@@ -309,25 +319,29 @@ instance Data a => Data (Maybe a) where
                     2 -> k (z Just)
                     _ -> error "gunfold"
   dataTypeOf _ = maybeDataType
-  dataCast1    = gcast1
+  dataCast1 f  = gcast1 f
 
 
 ------------------------------------------------------------------------------
 
-
+ltConstr :: Constr
 ltConstr         = mkConstr orderingDataType "LT" [] Prefix
+eqConstr :: Constr
 eqConstr         = mkConstr orderingDataType "EQ" [] Prefix
+gtConstr :: Constr
 gtConstr         = mkConstr orderingDataType "GT" [] Prefix
+
+orderingDataType :: DataType
 orderingDataType = mkDataType "Prelude.Ordering" [ltConstr,eqConstr,gtConstr]
 
 instance Data Ordering where
-  gfoldl f z LT  = z LT
-  gfoldl f z EQ  = z EQ
-  gfoldl f z GT  = z GT
+  gfoldl _ z LT  = z LT
+  gfoldl _ z EQ  = z EQ
+  gfoldl _ z GT  = z GT
   toConstr LT  = ltConstr
   toConstr EQ  = eqConstr
   toConstr GT  = gtConstr
-  gunfold k z c = case constrIndex c of
+  gunfold _ z c = case constrIndex c of
                     1 -> z LT
                     2 -> z EQ
                     3 -> z GT
@@ -337,9 +351,13 @@ instance Data Ordering where
 
 ------------------------------------------------------------------------------
 
-
+leftConstr :: Constr
 leftConstr     = mkConstr eitherDataType "Left"  [] Prefix
+
+rightConstr :: Constr
 rightConstr    = mkConstr eitherDataType "Right" [] Prefix
+
+eitherDataType :: DataType
 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
 
 instance (Data a, Data b) => Data (Either a b) where
@@ -352,7 +370,7 @@ instance (Data a, Data b) => Data (Either a b) where
                     2 -> k (z Right)
                     _ -> error "gunfold"
   dataTypeOf _ = eitherDataType
-  dataCast2    = gcast2
+  dataCast2 f  = gcast2 f
 
 
 ------------------------------------------------------------------------------
@@ -366,46 +384,52 @@ instance (Data a, Data b) => Data (a -> b) where
   toConstr _   = error "toConstr"
   gunfold _ _  = error "gunfold"
   dataTypeOf _ = mkNorepType "Prelude.(->)"
-  dataCast2    = gcast2
+  dataCast2 f  = gcast2 f
 
 
 ------------------------------------------------------------------------------
 
-
+tuple0Constr :: Constr
 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
+
+tuple0DataType :: DataType
 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
 
 instance Data () where
-  toConstr _    = tuple0Constr
-  gunfold k z c | constrIndex c == 1 = z ()  
+  toConstr ()   = tuple0Constr
+  gunfold _ z c | constrIndex c == 1 = z ()
   gunfold _ _ _ = error "gunfold"
   dataTypeOf _  = tuple0DataType
 
 
 ------------------------------------------------------------------------------
 
-
+tuple2Constr :: Constr
 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
+
+tuple2DataType :: DataType
 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
 
 instance (Data a, Data b) => Data (a,b) where
   gfoldl f z (a,b) = z (,) `f` a `f` b
-  toConstr _    = tuple2Constr
+  toConstr (_,_) = tuple2Constr
   gunfold k z c | constrIndex c == 1 = k (k (z (,)))
   gunfold _ _ _ = error "gunfold"
   dataTypeOf _  = tuple2DataType
-  dataCast2     = gcast2
+  dataCast2 f   = gcast2 f
 
 
 ------------------------------------------------------------------------------
 
-
+tuple3Constr :: Constr
 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
+
+tuple3DataType :: DataType
 tuple3DataType = mkDataType "Prelude.(,)" [tuple3Constr]
 
 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
+  toConstr (_,_,_) = tuple3Constr
   gunfold k z c | constrIndex c == 1 = k (k (k (z (,,))))
   gunfold _ _ _ = error "gunfold"
   dataTypeOf _  = tuple3DataType
@@ -413,14 +437,16 @@ instance (Data a, Data b, Data c) => Data (a,b,c) where
 
 ------------------------------------------------------------------------------
 
-
+tuple4Constr :: Constr
 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
+
+tuple4DataType :: DataType
 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
 
 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
+  toConstr (_,_,_,_) = tuple4Constr
   gunfold k z c = case constrIndex c of
                     1 -> k (k (k (k (z (,,,)))))
                     _ -> error "gunfold"
@@ -429,14 +455,16 @@ instance (Data a, Data b, Data c, Data d)
 
 ------------------------------------------------------------------------------
 
-
+tuple5Constr :: Constr
 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
+
+tuple5DataType :: DataType
 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
 
 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
+  toConstr (_,_,_,_,_) = tuple5Constr
   gunfold k z c = case constrIndex c of
                     1 -> k (k (k (k (k (z (,,,,))))))
                     _ -> error "gunfold"
@@ -445,14 +473,16 @@ instance (Data a, Data b, Data c, Data d, Data e)
 
 ------------------------------------------------------------------------------
 
-
+tuple6Constr :: Constr
 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
+
+tuple6DataType :: DataType
 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
 
 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
+  toConstr (_,_,_,_,_,_) = tuple6Constr
   gunfold k z c = case constrIndex c of
                     1 -> k (k (k (k (k (k (z (,,,,,)))))))
                     _ -> error "gunfold"
@@ -461,15 +491,17 @@ instance (Data a, Data b, Data c, Data d, Data e, Data f)
 
 ------------------------------------------------------------------------------
 
-
+tuple7Constr :: Constr
 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
+
+tuple7DataType :: DataType
 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
 
 instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
          => 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
+  toConstr  (_,_,_,_,_,_,_) = tuple7Constr
   gunfold k z c = case constrIndex c of
                     1 -> k (k (k (k (k (k (k (z (,,,,,,))))))))
                     _ -> error "gunfold"
@@ -605,3 +637,12 @@ instance Typeable a => Data (STM a) where
 
 
 ------------------------------------------------------------------------------
+-- 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"
+