git.iko.soy/typeable/ordered-containers
on master help sign in
Merge pull request #8 from RyanGlScott/data-instances
aad319fDaniel Wagner
Data/Map/Ordered.hs+35
@@ -1,3 +1,8 @@
1+{-# LANGUAGE CPP #-}
2+#if __GLASGOW_HASKELL__
3+{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
4+#endif
5+
1-- | An 'OMap' behaves much like a 'Map', with all the same asymptotics, but
6-- | An 'OMap' behaves much like a 'Map', with all the same asymptotics, but
2-- also remembers the order that keys were inserted.
7-- also remembers the order that keys were inserted.
3module Data.Map.Ordered
8module Data.Map.Ordered
@@ -34,6 +39,10 @@ import Data.Map.Util (Index, Tag, maxTag, minTag, nextHigherTag, nextLowerTag, r
34import Prelude hiding (filter, lookup, null)
39import Prelude hiding (filter, lookup, null)
35import qualified Data.Map as M
40import qualified Data.Map as M
36
41
42+#if __GLASGOW_HASKELL__
43+import Data.Data
44+#endif
45+
37data OMap k v = OMap !(Map k (Tag, v)) !(Map Tag (k, v))
46data OMap k v = OMap !(Map k (Tag, v)) !(Map Tag (k, v))
38
47
39-- | Values are produced in insertion order, not key order.
48-- | Values are produced in insertion order, not key order.
@@ -43,6 +52,32 @@ instance ( Ord k, Ord v) => Ord (OMap k v) where compare = compare `on`
43instance ( Show k, Show v) => Show (OMap k v) where showsPrec = showsPrecList assocs
52instance ( Show k, Show v) => Show (OMap k v) where showsPrec = showsPrecList assocs
44instance (Ord k, Read k, Read v) => Read (OMap k v) where readsPrec = readsPrecList fromList
53instance (Ord k, Read k, Read v) => Read (OMap k v) where readsPrec = readsPrecList fromList
45
54
55+#if __GLASGOW_HASKELL__
56+# if __GLASGOW_HASKELL__ >= 708
57+deriving instance Typeable OMap
58+# else
59+deriving instance Typeable2 OMap
60+# endif
61+
62+-- This instance preserves data abstraction at the cost of inefficiency.
63+-- We provide limited reflection services for the sake of data abstraction.
64+
65+instance (Data k, Data a, Ord k) => Data (OMap k a) where
66+ gfoldl f z m = z fromList `f` assocs m
67+ toConstr _ = fromListConstr
68+ gunfold k z c = case constrIndex c of
69+ 1 -> k (z fromList)
70+ _ -> error "gunfold"
71+ dataTypeOf _ = oMapDataType
72+ dataCast2 f = gcast2 f
73+
74+fromListConstr :: Constr
75+fromListConstr = mkConstr oMapDataType "fromList" [] Prefix
76+
77+oMapDataType :: DataType
78+oMapDataType = mkDataType "Data.Map.Ordered.Map" [fromListConstr]
79+#endif
80+
46infixr 5 <|, |< -- copy :
81infixr 5 <|, |< -- copy :
47infixl 5 >|, |>
82infixl 5 >|, |>
48
83
Data/Set/Ordered.hs+35
@@ -1,3 +1,8 @@
1+{-# LANGUAGE CPP #-}
2+#if __GLASGOW_HASKELL__
3+{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
4+#endif
5+
1-- | An 'OSet' behaves much like a 'Set', with all the same asymptotics, but
6-- | An 'OSet' behaves much like a 'Set', with all the same asymptotics, but
2-- also remembers the order that values were inserted.
7-- also remembers the order that values were inserted.
3module Data.Set.Ordered
8module Data.Set.Ordered
@@ -33,6 +38,10 @@ import Data.Set (Set) -- so the haddocks link to the right place
33import Prelude hiding (filter, foldr, lookup, null)
38import Prelude hiding (filter, foldr, lookup, null)
34import qualified Data.Map as M
39import qualified Data.Map as M
35
40
41+#if __GLASGOW_HASKELL__
42+import Data.Data
43+#endif
44+
36data OSet a = OSet !(Map a Tag) !(Map Tag a)
45data OSet a = OSet !(Map a Tag) !(Map Tag a)
37
46
38-- | Values appear in insertion order, not ascending order.
47-- | Values appear in insertion order, not ascending order.
@@ -42,6 +51,32 @@ instance Ord a => Ord (OSet a) where compare = compare `on` toList
42instance Show a => Show (OSet a) where showsPrec = showsPrecList toList
51instance Show a => Show (OSet a) where showsPrec = showsPrecList toList
43instance (Ord a, Read a) => Read (OSet a) where readsPrec = readsPrecList fromList
52instance (Ord a, Read a) => Read (OSet a) where readsPrec = readsPrecList fromList
44
53
54+#if __GLASGOW_HASKELL__
55+# if __GLASGOW_HASKELL__ >= 708
56+deriving instance Typeable OSet
57+# else
58+deriving instance Typeable1 OSet
59+# endif
60+
61+-- This instance preserves data abstraction at the cost of inefficiency.
62+-- We provide limited reflection services for the sake of data abstraction.
63+
64+instance (Data a, Ord a) => Data (OSet a) where
65+ gfoldl f z set = z fromList `f` (toList set)
66+ toConstr _ = fromListConstr
67+ gunfold k z c = case constrIndex c of
68+ 1 -> k (z fromList)
69+ _ -> error "gunfold"
70+ dataTypeOf _ = oSetDataType
71+ dataCast1 f = gcast1 f
72+
73+fromListConstr :: Constr
74+fromListConstr = mkConstr oSetDataType "fromList" [] Prefix
75+
76+oSetDataType :: DataType
77+oSetDataType = mkDataType "Data.Set.Ordered.Set" [fromListConstr]
78+#endif
79+
45infixr 5 <|, |< -- copy :
80infixr 5 <|, |< -- copy :
46infixl 5 >|, |>
81infixl 5 >|, |>
47infixr 6 <>|, |<> -- copy <>
82infixr 6 <>|, |<> -- copy <>