1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Data.Dynamic
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : portable
12 -- The Dynamic interface provides basic support for dynamic types.
14 -- Operations for injecting values of arbitrary type into
15 -- a dynamically typed value, Dynamic, are provided, together
16 -- with operations for converting dynamic values into a concrete
17 -- (monomorphic) type.
19 -----------------------------------------------------------------------------
24 -- Module Data.Typeable re-exported for convenience
27 -- * The @Dynamic@ type
28 Dynamic, -- abstract, instance of: Show, Typeable
30 -- * Converting to and from @Dynamic@
31 toDyn, -- :: Typeable a => a -> Dynamic
32 fromDyn, -- :: Typeable a => Dynamic -> a -> a
33 fromDynamic, -- :: Typeable a => Dynamic -> Maybe a
35 -- * Applying functions of dynamic type
45 #ifdef __GLASGOW_HASKELL__
59 #ifdef __GLASGOW_HASKELL__
60 unsafeCoerce :: a -> b
61 unsafeCoerce = unsafeCoerce#
65 import NonStdUnsafeCoerce (unsafeCoerce)
66 import NHC.IOExtras (IORef,newIORef,readIORef,writeIORef,unsafePerformIO)
71 -------------------------------------------------------------
75 -------------------------------------------------------------
78 A value of type 'Dynamic' is an object encapsulated together with its type.
80 A 'Dynamic' may only represent a monomorphic value; an attempt to
81 create a value of type 'Dynamic' from a polymorphically-typed
82 expression will result in an ambiguity error (see 'toDyn').
84 'Show'ing a value of type 'Dynamic' returns a pretty-printed representation
85 of the object\'s type; useful for debugging.
88 data Dynamic = Dynamic TypeRep Obj
91 INSTANCE_TYPEABLE0(Dynamic,dynamicTc,"Dynamic")
93 instance Show Dynamic where
94 -- the instance just prints the type representation.
95 showsPrec _ (Dynamic t _) =
100 #ifdef __GLASGOW_HASKELL__
101 type Obj = forall a . a
102 -- Dummy type to hold the dynamically typed value.
104 -- In GHC's new eval/apply execution model this type must
105 -- be polymorphic. It can't be a constructor, because then
106 -- GHC will use the constructor convention when evaluating it,
107 -- and this will go wrong if the object is really a function. On
108 -- the other hand, if we use a polymorphic type, GHC will use
109 -- a fallback convention for evaluating it that works for all types.
110 -- (using a function type here would also work).
111 #elif !defined(__HUGS__)
115 -- | Converts an arbitrary value into an object of type 'Dynamic'.
117 -- The type of the object must be an instance of 'Typeable', which
118 -- ensures that only monomorphically-typed objects may be converted to
119 -- 'Dynamic'. To convert a polymorphic object into 'Dynamic', give it
120 -- a monomorphic type signature. For example:
122 -- > toDyn (id :: Int -> Int)
124 toDyn :: Typeable a => a -> Dynamic
125 toDyn v = Dynamic (typeOf v) (unsafeCoerce v)
127 -- | Converts a 'Dynamic' object back into an ordinary Haskell value of
128 -- the correct type. See also 'fromDynamic'.
129 fromDyn :: Typeable a
130 => Dynamic -- ^ the dynamically-typed object
131 -> a -- ^ a default value
132 -> a -- ^ returns: the value of the first argument, if
133 -- it has the correct type, otherwise the value of
134 -- the second argument.
135 fromDyn (Dynamic t v) def
136 | typeOf def == t = unsafeCoerce v
139 -- | Converts a 'Dynamic' object back into an ordinary Haskell value of
140 -- the correct type. See also 'fromDyn'.
143 => Dynamic -- ^ the dynamically-typed object
144 -> Maybe a -- ^ returns: @'Just' a@, if the dynamically-typed
145 -- object has the correct type (and @a@ is its value),
146 -- or 'Nothing' otherwise.
147 fromDynamic (Dynamic t v) =
148 case unsafeCoerce v of
149 r | t == typeOf r -> Just r
150 | otherwise -> Nothing
152 -- (f::(a->b)) `dynApply` (x::a) = (f a)::b
153 dynApply :: Dynamic -> Dynamic -> Maybe Dynamic
154 dynApply (Dynamic t1 f) (Dynamic t2 x) =
155 case funResultTy t1 t2 of
156 Just t3 -> Just (Dynamic t3 ((unsafeCoerce f) x))
159 dynApp :: Dynamic -> Dynamic -> Dynamic
160 dynApp f x = case dynApply f x of
162 Nothing -> error ("Type error in dynamic application.\n" ++
163 "Can't apply function " ++ show f ++
164 " to argument " ++ show x)