Faster filterF, filterNotByte
[packages/old-time.git] / Data / Dynamic.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Data.Dynamic
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
7 --
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : portable
11 --
12 -- The Dynamic interface provides basic support for dynamic types.
13 --
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.
18 --
19 -----------------------------------------------------------------------------
20
21 module Data.Dynamic
22 (
23
24 -- Module Data.Typeable re-exported for convenience
25 module Data.Typeable,
26
27 -- * The @Dynamic@ type
28 Dynamic, -- abstract, instance of: Show, Typeable
29
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
34
35 -- * Applying functions of dynamic type
36 dynApply,
37 dynApp,
38 dynTypeRep
39
40 ) where
41
42
43 import Data.Typeable
44 import Data.Maybe
45
46 #ifdef __GLASGOW_HASKELL__
47 import GHC.Base
48 import GHC.Show
49 import GHC.Err
50 import GHC.Num
51 #endif
52
53 #ifdef __HUGS__
54 import Hugs.Prelude
55 import Hugs.IO
56 import Hugs.IORef
57 import Hugs.IOExts
58 #endif
59
60 #ifdef __GLASGOW_HASKELL__
61 unsafeCoerce :: a -> b
62 unsafeCoerce = unsafeCoerce#
63 #endif
64
65 #ifdef __NHC__
66 import NonStdUnsafeCoerce (unsafeCoerce)
67 import NHC.IOExtras (IORef,newIORef,readIORef,writeIORef,unsafePerformIO)
68 #endif
69
70 #include "Typeable.h"
71
72 -------------------------------------------------------------
73 --
74 -- The type Dynamic
75 --
76 -------------------------------------------------------------
77
78 {-|
79 A value of type 'Dynamic' is an object encapsulated together with its type.
80
81 A 'Dynamic' may only represent a monomorphic value; an attempt to
82 create a value of type 'Dynamic' from a polymorphically-typed
83 expression will result in an ambiguity error (see 'toDyn').
84
85 'Show'ing a value of type 'Dynamic' returns a pretty-printed representation
86 of the object\'s type; useful for debugging.
87 -}
88 #ifndef __HUGS__
89 data Dynamic = Dynamic TypeRep Obj
90 #endif
91
92 INSTANCE_TYPEABLE0(Dynamic,dynamicTc,"Dynamic")
93
94 instance Show Dynamic where
95 -- the instance just prints the type representation.
96 showsPrec _ (Dynamic t _) =
97 showString "<<" .
98 showsPrec 0 t .
99 showString ">>"
100
101 #ifdef __GLASGOW_HASKELL__
102 type Obj = forall a . a
103 -- Dummy type to hold the dynamically typed value.
104 --
105 -- In GHC's new eval/apply execution model this type must
106 -- be polymorphic. It can't be a constructor, because then
107 -- GHC will use the constructor convention when evaluating it,
108 -- and this will go wrong if the object is really a function. On
109 -- the other hand, if we use a polymorphic type, GHC will use
110 -- a fallback convention for evaluating it that works for all types.
111 -- (using a function type here would also work).
112 #elif !defined(__HUGS__)
113 data Obj = Obj
114 #endif
115
116 -- | Converts an arbitrary value into an object of type 'Dynamic'.
117 --
118 -- The type of the object must be an instance of 'Typeable', which
119 -- ensures that only monomorphically-typed objects may be converted to
120 -- 'Dynamic'. To convert a polymorphic object into 'Dynamic', give it
121 -- a monomorphic type signature. For example:
122 --
123 -- > toDyn (id :: Int -> Int)
124 --
125 toDyn :: Typeable a => a -> Dynamic
126 toDyn v = Dynamic (typeOf v) (unsafeCoerce v)
127
128 -- | Converts a 'Dynamic' object back into an ordinary Haskell value of
129 -- the correct type. See also 'fromDynamic'.
130 fromDyn :: Typeable a
131 => Dynamic -- ^ the dynamically-typed object
132 -> a -- ^ a default value
133 -> a -- ^ returns: the value of the first argument, if
134 -- it has the correct type, otherwise the value of
135 -- the second argument.
136 fromDyn (Dynamic t v) def
137 | typeOf def == t = unsafeCoerce v
138 | otherwise = def
139
140 -- | Converts a 'Dynamic' object back into an ordinary Haskell value of
141 -- the correct type. See also 'fromDyn'.
142 fromDynamic
143 :: Typeable a
144 => Dynamic -- ^ the dynamically-typed object
145 -> Maybe a -- ^ returns: @'Just' a@, if the dynamically-typed
146 -- object has the correct type (and @a@ is its value),
147 -- or 'Nothing' otherwise.
148 fromDynamic (Dynamic t v) =
149 case unsafeCoerce v of
150 r | t == typeOf r -> Just r
151 | otherwise -> Nothing
152
153 -- (f::(a->b)) `dynApply` (x::a) = (f a)::b
154 dynApply :: Dynamic -> Dynamic -> Maybe Dynamic
155 dynApply (Dynamic t1 f) (Dynamic t2 x) =
156 case funResultTy t1 t2 of
157 Just t3 -> Just (Dynamic t3 ((unsafeCoerce f) x))
158 Nothing -> Nothing
159
160 dynApp :: Dynamic -> Dynamic -> Dynamic
161 dynApp f x = case dynApply f x of
162 Just r -> r
163 Nothing -> error ("Type error in dynamic application.\n" ++
164 "Can't apply function " ++ show f ++
165 " to argument " ++ show x)
166
167 dynTypeRep :: Dynamic -> TypeRep
168 dynTypeRep (Dynamic tr _) = tr