[project @ 2006-01-06 15:51:23 by simonpj]
[haskell-directory.git] / Data / Generics / Instances.hs
index 1157dc5..75de715 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
@@ -38,6 +38,7 @@ 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"
 
@@ -278,7 +279,7 @@ 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.
@@ -309,7 +310,7 @@ instance Data a => Data (Maybe a) where
                     2 -> k (z Just)
                     _ -> error "gunfold"
   dataTypeOf _ = maybeDataType
-  dataCast1    = gcast1
+  dataCast1 f  = gcast1 f
 
 
 ------------------------------------------------------------------------------
@@ -352,7 +353,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,7 +367,7 @@ instance (Data a, Data b) => Data (a -> b) where
   toConstr _   = error "toConstr"
   gunfold _ _  = error "gunfold"
   dataTypeOf _ = mkNorepType "Prelude.(->)"
-  dataCast2    = gcast2
+  dataCast2 f  = gcast2 f
 
 
 ------------------------------------------------------------------------------
@@ -376,7 +377,7 @@ tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
 
 instance Data () where
-  toConstr _    = tuple0Constr
+  toConstr ()   = tuple0Constr
   gunfold k z c | constrIndex c == 1 = z ()  
   gunfold _ _ _ = error "gunfold"
   dataTypeOf _  = tuple0DataType
@@ -390,11 +391,11 @@ 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 (a,b) = tuple2Constr
   gunfold k z c | constrIndex c == 1 = k (k (z (,)))
   gunfold _ _ _ = error "gunfold"
   dataTypeOf _  = tuple2DataType
-  dataCast2     = gcast2
+  dataCast2 f   = gcast2 f
 
 
 ------------------------------------------------------------------------------
@@ -405,7 +406,7 @@ 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 (a,b,c) = tuple3Constr
   gunfold k z c | constrIndex c == 1 = k (k (k (z (,,))))
   gunfold _ _ _ = error "gunfold"
   dataTypeOf _  = tuple3DataType
@@ -420,7 +421,7 @@ 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 (a,b,c,d) = tuple4Constr
   gunfold k z c = case constrIndex c of
                     1 -> k (k (k (k (z (,,,)))))
                     _ -> error "gunfold"
@@ -436,7 +437,7 @@ 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 (a,b,c,d,e) = tuple5Constr
   gunfold k z c = case constrIndex c of
                     1 -> k (k (k (k (k (z (,,,,))))))
                     _ -> error "gunfold"
@@ -452,7 +453,7 @@ 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 (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"
@@ -469,7 +470,7 @@ 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  (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"
@@ -605,3 +606,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"
+