1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Data.Dynamic
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : portable
12 -- $Id: Dynamic.hs,v 1.3 2001/07/03 14:13:32 simonmar Exp $
14 -- The Dynamic interface provides basic support for dynamic types.
16 -- Operations for injecting values of arbitrary type into
17 -- a dynamically typed value, Dynamic, are provided, together
18 -- with operations for converting dynamic values into a concrete
19 -- (monomorphic) type.
21 -- The Dynamic implementation provided is closely based on code
22 -- contained in Hugs library of the same name.
24 -----------------------------------------------------------------------------
29 Dynamic -- abstract, instance of: Show, Typeable
30 , toDyn -- :: Typeable a => a -> Dynamic
31 , fromDyn -- :: Typeable a => Dynamic -> a -> a
32 , fromDynamic -- :: Typeable a => Dynamic -> Maybe a
34 -- type representation
37 typeOf) -- :: a -> TypeRep
39 -- Dynamic defines Typeable instances for the following
40 -- Prelude types: [a], (), (a,b), (a,b,c), (a,b,c,d),
41 -- (a,b,c,d,e), (a->b), (Array a b), Bool, Char,
42 -- (Complex a), Double, (Either a b), Float, Handle,
43 -- Int, Integer, (IO a), (Maybe a), Ordering
45 , TypeRep -- abstract, instance of: Eq, Show, Typeable
46 , TyCon -- abstract, instance of: Eq, Show, Typeable
48 -- type representation constructors/operators:
49 , mkTyCon -- :: String -> TyCon
50 , mkAppTy -- :: TyCon -> [TypeRep] -> TypeRep
51 , mkFunTy -- :: TypeRep -> TypeRep -> TypeRep
52 , applyTy -- :: TypeRep -> TypeRep -> Maybe TypeRep
55 -- let fTy = mkTyCon "Foo" in show (mkAppTy (mkTyCon ",,")
58 -- returns "(Foo,Foo,Foo)"
60 -- The TypeRep Show instance promises to print tuple types
61 -- correctly. Tuple type constructors are specified by a
62 -- sequence of commas, e.g., (mkTyCon ",,,,") returns
70 #ifdef __GLASGOW_HASKELL__
80 #ifdef __GLASGOW_HASKELL__
81 import GHC.Prim ( unsafeCoerce# )
83 unsafeCoerce :: a -> b
84 unsafeCoerce = unsafeCoerce#
89 -- The dynamic type is represented by Dynamic, carrying
90 -- the dynamic value along with its type representation:
92 -- the instance just prints the type representation.
93 instance Show Dynamic where
94 showsPrec _ (Dynamic t _) =
99 -- Operations for going to and from Dynamic:
101 toDyn :: Typeable a => a -> Dynamic
102 toDyn v = Dynamic (typeOf v) (unsafeCoerce v)
104 fromDyn :: Typeable a => Dynamic -> a -> a
105 fromDyn (Dynamic t v) def
106 | typeOf def == t = unsafeCoerce v
109 fromDynamic :: Typeable a => Dynamic -> Maybe a
110 fromDynamic (Dynamic t v) =
111 case unsafeCoerce v of
112 r | t == typeOf r -> Just r
113 | otherwise -> Nothing
115 -- (Abstract) universal datatype:
117 instance Show TypeRep where
118 showsPrec p (App tycon tys) =
120 [] -> showsPrec p tycon
121 [x] | tycon == listTc -> showChar '[' . shows x . showChar ']'
123 | isTupleTyCon tycon -> showTuple tycon xs
130 showsPrec p (Fun f a) =
132 showsPrec 9 f . showString " -> " . showsPrec 8 a
134 -- To make it possible to convert values with user-defined types
135 -- into type Dynamic, we need a systematic way of getting
136 -- the type representation of an arbitrary type. A type
137 -- class provides just the ticket,
139 class Typeable a where
140 typeOf :: a -> TypeRep
142 -- NOTE: The argument to the overloaded `typeOf' is only
143 -- used to carry type information, and Typeable instances
144 -- should *never* *ever* look at its value.
146 isTupleTyCon :: TyCon -> Bool
147 isTupleTyCon (TyCon _ (',':_)) = True
148 isTupleTyCon _ = False
150 instance Show TyCon where
151 showsPrec _ (TyCon _ s) = showString s
153 -- If we enforce the restriction that there is only one
154 -- @TyCon@ for a type & it is shared among all its uses,
155 -- we can map them onto Ints very simply. The benefit is,
156 -- of course, that @TyCon@s can then be compared efficiently.
158 -- Provided the implementor of other @Typeable@ instances
159 -- takes care of making all the @TyCon@s CAFs (toplevel constants),
162 -- If this constraint does turn out to be a sore thumb, changing
163 -- the Eq instance for TyCons is trivial.
165 mkTyCon :: String -> TyCon
166 mkTyCon str = unsafePerformIO $ do
173 uni = unsafePerformIO ( newIORef 0 )
175 -- Some (Show.TypeRep) helpers:
177 showArgs :: Show a => [a] -> ShowS
179 showArgs [a] = showsPrec 10 a
180 showArgs (a:as) = showsPrec 10 a . showString " " . showArgs as
182 showTuple :: TyCon -> [TypeRep] -> ShowS
183 showTuple (TyCon _ str) args = showChar '(' . go str args
185 go [] [a] = showsPrec 10 a . showChar ')'
186 go _ [] = showChar ')' -- a failure condition, really.
187 go (',':xs) (a:as) = showsPrec 10 a . showChar ',' . go xs as
188 go _ _ = showChar ')'
191 mkAppTy :: TyCon -> [TypeRep] -> TypeRep
192 mkAppTy tyc args = App tyc args
194 mkFunTy :: TypeRep -> TypeRep -> TypeRep
195 mkFunTy f a = Fun f a
197 -- Auxillary functions
199 -- (f::(a->b)) `dynApply` (x::a) = (f a)::b
200 dynApply :: Dynamic -> Dynamic -> Maybe Dynamic
201 dynApply (Dynamic t1 f) (Dynamic t2 x) =
202 case applyTy t1 t2 of
203 Just t3 -> Just (Dynamic t3 ((unsafeCoerce f) x))
206 dynApp :: Dynamic -> Dynamic -> Dynamic
207 dynApp f x = case dynApply f x of
209 Nothing -> error ("Type error in dynamic application.\n" ++
210 "Can't apply function " ++ show f ++
211 " to argument " ++ show x)
213 applyTy :: TypeRep -> TypeRep -> Maybe TypeRep
214 applyTy (Fun t1 t2) t3
216 applyTy _ _ = Nothing
221 listTc = mkTyCon "[]"
223 instance Typeable a => Typeable [a] where
224 typeOf ls = mkAppTy listTc [typeOf ((undefined:: [a] -> a) ls)]
227 unitTc = mkTyCon "()"
229 instance Typeable () where
230 typeOf _ = mkAppTy unitTc []
235 instance (Typeable a, Typeable b) => Typeable (a,b) where
236 typeOf tu = mkAppTy tup2Tc [typeOf ((undefined :: (a,b) -> a) tu),
237 typeOf ((undefined :: (a,b) -> b) tu)]
240 tup3Tc = mkTyCon ",,"
242 instance ( Typeable a , Typeable b , Typeable c) => Typeable (a,b,c) where
243 typeOf tu = mkAppTy tup3Tc [typeOf ((undefined :: (a,b,c) -> a) tu),
244 typeOf ((undefined :: (a,b,c) -> b) tu),
245 typeOf ((undefined :: (a,b,c) -> c) tu)]
248 tup4Tc = mkTyCon ",,,"
250 instance ( Typeable a
253 , Typeable d) => Typeable (a,b,c,d) where
254 typeOf tu = mkAppTy tup4Tc [typeOf ((undefined :: (a,b,c,d) -> a) tu),
255 typeOf ((undefined :: (a,b,c,d) -> b) tu),
256 typeOf ((undefined :: (a,b,c,d) -> c) tu),
257 typeOf ((undefined :: (a,b,c,d) -> d) tu)]
260 tup5Tc = mkTyCon ",,,,"
262 instance ( Typeable a
266 , Typeable e) => Typeable (a,b,c,d,e) where
267 typeOf tu = mkAppTy tup5Tc [typeOf ((undefined :: (a,b,c,d,e) -> a) tu),
268 typeOf ((undefined :: (a,b,c,d,e) -> b) tu),
269 typeOf ((undefined :: (a,b,c,d,e) -> c) tu),
270 typeOf ((undefined :: (a,b,c,d,e) -> d) tu),
271 typeOf ((undefined :: (a,b,c,d,e) -> e) tu)]
273 instance (Typeable a, Typeable b) => Typeable (a -> b) where
274 typeOf f = mkFunTy (typeOf ((undefined :: (a -> b) -> a) f))
275 (typeOf ((undefined :: (a -> b) -> b) f))
277 INSTANCE_TYPEABLE0(Bool,boolTc,"Bool")
278 INSTANCE_TYPEABLE0(Char,charTc,"Char")
279 INSTANCE_TYPEABLE0(Float,floatTc,"Float")
280 INSTANCE_TYPEABLE0(Double,doubleTc,"Double")
281 INSTANCE_TYPEABLE0(Int,intTc,"Int")
282 INSTANCE_TYPEABLE0(Integer,integerTc,"Integer")
283 INSTANCE_TYPEABLE2(Either,eitherTc,"Either")
284 INSTANCE_TYPEABLE1(IO,ioTc,"IO")
285 INSTANCE_TYPEABLE1(Maybe,maybeTc,"Maybe")
286 INSTANCE_TYPEABLE0(Ordering,orderingTc,"Ordering")
288 INSTANCE_TYPEABLE0(TyCon,tyconTc,"TyCon")
289 INSTANCE_TYPEABLE0(TypeRep,typeRepTc,"TypeRep")
290 INSTANCE_TYPEABLE0(Dynamic,dynamicTc,"Dynamic")