[project @ 2005-01-19 22:33:32 by ralf]
[ghc.git] / libraries / base / Data / Typeable.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Data.Typeable
5 -- Copyright : (c) The University of Glasgow, CWI 2001--2004
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 'Typeable' class reifies types to some extent by associating type
13 -- representations to types. These type representations can be compared,
14 -- and one can in turn define a type-safe cast operation. To this end,
15 -- an unsafe cast is guarded by a test for type (representation)
16 -- equivalence. The module "Data.Dynamic" uses Typeable for an
17 -- implementation of dynamics. The module "Data.Generics" uses Typeable
18 -- and type-safe cast (but not dynamics) to support the \"Scrap your
19 -- boilerplate\" style of generic programming.
20 --
21 -- Note, only relevant if you use dynamic linking. If you have a program
22 -- that is statically linked with Data.Typeable, and then dynamically link
23 -- a program that also uses Data.Typeable, you'll get two copies of the module.
24 -- That's fine, but behind the scenes, the module uses a mutable variable to
25 -- allocate unique Ids to type constructors. So in the situation described,
26 -- there'll be two separate Id allocators, which aren't comparable to each other.
27 -- This can lead to chaos. (It's a bug that we will fix.) None of
28 -- this matters if you aren't using dynamic linking.
29 --
30 -----------------------------------------------------------------------------
31
32 module Data.Typeable
33 (
34
35 -- * The Typeable class
36 Typeable( typeOf ), -- :: a -> TypeRep
37
38 -- * Type-safe cast
39 cast, -- :: (Typeable a, Typeable b) => a -> Maybe b
40 gcast, -- a generalisation of cast
41
42 -- * Type representations
43 TypeRep, -- abstract, instance of: Eq, Show, Typeable
44 TyCon, -- abstract, instance of: Eq, Show, Typeable
45
46 -- * Construction of type representations
47 mkTyCon, -- :: String -> TyCon
48 mkTyConApp, -- :: TyCon -> [TypeRep] -> TypeRep
49 mkAppTy, -- :: TypeRep -> TypeRep -> TypeRep
50 mkFunTy, -- :: TypeRep -> TypeRep -> TypeRep
51
52 -- * Observation of type representations
53 splitTyConApp, -- :: TypeRep -> (TyCon, [TypeRep])
54 funResultTy, -- :: TypeRep -> TypeRep -> Maybe TypeRep
55 typeRepTyCon, -- :: TypeRep -> TyCon
56 typeRepArgs, -- :: TypeRep -> [TypeRep]
57 tyConString, -- :: TyCon -> String
58
59 -- * The other Typeable classes
60 -- | /Note:/ The general instances are provided for GHC only.
61 Typeable1( typeOf1 ), -- :: t a -> TypeRep
62 Typeable2( typeOf2 ), -- :: t a b -> TypeRep
63 Typeable3( typeOf3 ), -- :: t a b c -> TypeRep
64 Typeable4( typeOf4 ), -- :: t a b c d -> TypeRep
65 Typeable5( typeOf5 ), -- :: t a b c d e -> TypeRep
66 Typeable6( typeOf6 ), -- :: t a b c d e f -> TypeRep
67 Typeable7( typeOf7 ), -- :: t a b c d e f g -> TypeRep
68 gcast1, -- :: ... => c (t a) -> Maybe (c (t' a))
69 gcast2, -- :: ... => c (t a b) -> Maybe (c (t' a b))
70
71 -- * Default instances
72 -- | /Note:/ These are not needed by GHC, for which these instances
73 -- are generated by general instance declarations.
74 typeOfDefault, -- :: (Typeable1 t, Typeable a) => t a -> TypeRep
75 typeOf1Default, -- :: (Typeable2 t, Typeable a) => t a b -> TypeRep
76 typeOf2Default, -- :: (Typeable3 t, Typeable a) => t a b c -> TypeRep
77 typeOf3Default, -- :: (Typeable4 t, Typeable a) => t a b c d -> TypeRep
78 typeOf4Default, -- :: (Typeable5 t, Typeable a) => t a b c d e -> TypeRep
79 typeOf5Default, -- :: (Typeable6 t, Typeable a) => t a b c d e f -> TypeRep
80 typeOf6Default -- :: (Typeable7 t, Typeable a) => t a b c d e f g -> TypeRep
81
82 ) where
83
84 import qualified Data.HashTable as HT
85 import Data.Maybe
86 import Data.Either
87 import Data.Int
88 import Data.Word
89 import Data.List( foldl )
90
91 #ifdef __GLASGOW_HASKELL__
92 import GHC.Base
93 import GHC.Show
94 import GHC.Err
95 import GHC.Num
96 import GHC.Float
97 import GHC.Real( rem, Ratio )
98 import GHC.IOBase
99 import GHC.ST -- So we can give Typeable instance for ST
100 import GHC.Ptr -- So we can give Typeable instance for Ptr
101 import GHC.ForeignPtr -- So we can give Typeable instance for ForeignPtr
102 import GHC.Stable -- So we can give Typeable instance for StablePtr
103 #endif
104
105 #ifdef __HUGS__
106 import Hugs.Prelude
107 import Hugs.IO
108 import Hugs.IORef
109 import Hugs.IOExts
110 #endif
111
112 #ifdef __GLASGOW_HASKELL__
113 unsafeCoerce :: a -> b
114 unsafeCoerce = unsafeCoerce#
115 #endif
116
117 #ifdef __NHC__
118 import NonStdUnsafeCoerce (unsafeCoerce)
119 import NHC.IOExtras (IORef,newIORef,readIORef,writeIORef,unsafePerformIO)
120 import IO (Handle)
121 import Ratio (Ratio)
122 import NHC.FFI (Ptr,StablePtr)
123 #else
124 #endif
125
126 #include "Typeable.h"
127
128 #ifndef __HUGS__
129
130 -------------------------------------------------------------
131 --
132 -- Type representations
133 --
134 -------------------------------------------------------------
135
136 -- | A concrete representation of a (monomorphic) type. 'TypeRep'
137 -- supports reasonably efficient equality.
138 data TypeRep = TypeRep !Key TyCon [TypeRep]
139
140 -- Compare keys for equality
141 instance Eq TypeRep where
142 (TypeRep k1 _ _) == (TypeRep k2 _ _) = k1 == k2
143
144 -- | An abstract representation of a type constructor. 'TyCon' objects can
145 -- be built using 'mkTyCon'.
146 data TyCon = TyCon !Key String
147
148 instance Eq TyCon where
149 (TyCon t1 _) == (TyCon t2 _) = t1 == t2
150
151 #endif
152
153 --
154 -- let fTy = mkTyCon "Foo" in show (mkTyConApp (mkTyCon ",,")
155 -- [fTy,fTy,fTy])
156 --
157 -- returns "(Foo,Foo,Foo)"
158 --
159 -- The TypeRep Show instance promises to print tuple types
160 -- correctly. Tuple type constructors are specified by a
161 -- sequence of commas, e.g., (mkTyCon ",,,,") returns
162 -- the 5-tuple tycon.
163
164 ----------------- Construction --------------------
165
166 -- | Applies a type constructor to a sequence of types
167 mkTyConApp :: TyCon -> [TypeRep] -> TypeRep
168 mkTyConApp tc@(TyCon tc_k _) args
169 = TypeRep (appKeys tc_k arg_ks) tc args
170 where
171 arg_ks = [k | TypeRep k _ _ <- args]
172
173 -- | A special case of 'mkTyConApp', which applies the function
174 -- type constructor to a pair of types.
175 mkFunTy :: TypeRep -> TypeRep -> TypeRep
176 mkFunTy f a = mkTyConApp funTc [f,a]
177
178 -- | Splits a type constructor application
179 splitTyConApp :: TypeRep -> (TyCon,[TypeRep])
180 splitTyConApp (TypeRep _ tc trs) = (tc,trs)
181
182 -- | Applies a type to a function type. Returns: @'Just' u@ if the
183 -- first argument represents a function of type @t -> u@ and the
184 -- second argument represents a function of type @t@. Otherwise,
185 -- returns 'Nothing'.
186 funResultTy :: TypeRep -> TypeRep -> Maybe TypeRep
187 funResultTy trFun trArg
188 = case splitTyConApp trFun of
189 (tc, [t1,t2]) | tc == funTc && t1 == trArg -> Just t2
190 _ -> Nothing
191
192 -- | Adds a TypeRep argument to a TypeRep.
193 mkAppTy :: TypeRep -> TypeRep -> TypeRep
194 mkAppTy (TypeRep tr_k tc trs) arg_tr
195 = let (TypeRep arg_k _ _) = arg_tr
196 in TypeRep (appKey tr_k arg_k) tc (trs++[arg_tr])
197
198 -- If we enforce the restriction that there is only one
199 -- @TyCon@ for a type & it is shared among all its uses,
200 -- we can map them onto Ints very simply. The benefit is,
201 -- of course, that @TyCon@s can then be compared efficiently.
202
203 -- Provided the implementor of other @Typeable@ instances
204 -- takes care of making all the @TyCon@s CAFs (toplevel constants),
205 -- this will work.
206
207 -- If this constraint does turn out to be a sore thumb, changing
208 -- the Eq instance for TyCons is trivial.
209
210 -- | Builds a 'TyCon' object representing a type constructor. An
211 -- implementation of "Data.Typeable" should ensure that the following holds:
212 --
213 -- > mkTyCon "a" == mkTyCon "a"
214 --
215
216 mkTyCon :: String -- ^ the name of the type constructor (should be unique
217 -- in the program, so it might be wise to use the
218 -- fully qualified name).
219 -> TyCon -- ^ A unique 'TyCon' object
220 mkTyCon str = TyCon (mkTyConKey str) str
221
222 ----------------- Observation ---------------------
223
224 -- | Observe the type constructor of a type representation
225 typeRepTyCon :: TypeRep -> TyCon
226 typeRepTyCon (TypeRep _ tc _) = tc
227
228 -- | Observe the argument types of a type representation
229 typeRepArgs :: TypeRep -> [TypeRep]
230 typeRepArgs (TypeRep _ _ args) = args
231
232 -- | Observe string encoding of a type representation
233 tyConString :: TyCon -> String
234 tyConString (TyCon _ str) = str
235
236 ----------------- Showing TypeReps --------------------
237
238 instance Show TypeRep where
239 showsPrec p (TypeRep _ tycon tys) =
240 case tys of
241 [] -> showsPrec p tycon
242 [x] | tycon == listTc -> showChar '[' . shows x . showChar ']'
243 [a,r] | tycon == funTc -> showParen (p > 8) $
244 showsPrec 9 a .
245 showString " -> " .
246 showsPrec 8 r
247 xs | isTupleTyCon tycon -> showTuple tycon xs
248 | otherwise ->
249 showParen (p > 9) $
250 showsPrec p tycon .
251 showChar ' ' .
252 showArgs tys
253
254 instance Show TyCon where
255 showsPrec _ (TyCon _ s) = showString s
256
257 isTupleTyCon :: TyCon -> Bool
258 isTupleTyCon (TyCon _ (',':_)) = True
259 isTupleTyCon _ = False
260
261 -- Some (Show.TypeRep) helpers:
262
263 showArgs :: Show a => [a] -> ShowS
264 showArgs [] = id
265 showArgs [a] = showsPrec 10 a
266 showArgs (a:as) = showsPrec 10 a . showString " " . showArgs as
267
268 showTuple :: TyCon -> [TypeRep] -> ShowS
269 showTuple (TyCon _ str) args = showChar '(' . go str args
270 where
271 go [] [a] = showsPrec 10 a . showChar ')'
272 go _ [] = showChar ')' -- a failure condition, really.
273 go (',':xs) (a:as) = showsPrec 10 a . showChar ',' . go xs as
274 go _ _ = showChar ')'
275
276 -------------------------------------------------------------
277 --
278 -- The Typeable class and friends
279 --
280 -------------------------------------------------------------
281
282 -- | The class 'Typeable' allows a concrete representation of a type to
283 -- be calculated.
284 class Typeable a where
285 typeOf :: a -> TypeRep
286 -- ^ Takes a value of type @a@ and returns a concrete representation
287 -- of that type. The /value/ of the argument should be ignored by
288 -- any instance of 'Typeable', so that it is safe to pass 'undefined' as
289 -- the argument.
290
291 -- | Variant for unary type constructors
292 class Typeable1 t where
293 typeOf1 :: t a -> TypeRep
294
295 -- | For defining a 'Typeable' instance from any 'Typeable1' instance.
296 typeOfDefault :: (Typeable1 t, Typeable a) => t a -> TypeRep
297 typeOfDefault x = typeOf1 x `mkAppTy` typeOf (argType x)
298 where
299 argType :: t a -> a
300 argType = undefined
301
302 -- | Variant for binary type constructors
303 class Typeable2 t where
304 typeOf2 :: t a b -> TypeRep
305
306 -- | For defining a 'Typeable1' instance from any 'Typeable2' instance.
307 typeOf1Default :: (Typeable2 t, Typeable a) => t a b -> TypeRep
308 typeOf1Default x = typeOf2 x `mkAppTy` typeOf (argType x)
309 where
310 argType :: t a b -> a
311 argType = undefined
312
313 -- | Variant for 3-ary type constructors
314 class Typeable3 t where
315 typeOf3 :: t a b c -> TypeRep
316
317 -- | For defining a 'Typeable2' instance from any 'Typeable3' instance.
318 typeOf2Default :: (Typeable3 t, Typeable a) => t a b c -> TypeRep
319 typeOf2Default x = typeOf3 x `mkAppTy` typeOf (argType x)
320 where
321 argType :: t a b c -> a
322 argType = undefined
323
324 -- | Variant for 4-ary type constructors
325 class Typeable4 t where
326 typeOf4 :: t a b c d -> TypeRep
327
328 -- | For defining a 'Typeable3' instance from any 'Typeable4' instance.
329 typeOf3Default :: (Typeable4 t, Typeable a) => t a b c d -> TypeRep
330 typeOf3Default x = typeOf4 x `mkAppTy` typeOf (argType x)
331 where
332 argType :: t a b c d -> a
333 argType = undefined
334
335 -- | Variant for 5-ary type constructors
336 class Typeable5 t where
337 typeOf5 :: t a b c d e -> TypeRep
338
339 -- | For defining a 'Typeable4' instance from any 'Typeable5' instance.
340 typeOf4Default :: (Typeable5 t, Typeable a) => t a b c d e -> TypeRep
341 typeOf4Default x = typeOf5 x `mkAppTy` typeOf (argType x)
342 where
343 argType :: t a b c d e -> a
344 argType = undefined
345
346 -- | Variant for 6-ary type constructors
347 class Typeable6 t where
348 typeOf6 :: t a b c d e f -> TypeRep
349
350 -- | For defining a 'Typeable5' instance from any 'Typeable6' instance.
351 typeOf5Default :: (Typeable6 t, Typeable a) => t a b c d e f -> TypeRep
352 typeOf5Default x = typeOf6 x `mkAppTy` typeOf (argType x)
353 where
354 argType :: t a b c d e f -> a
355 argType = undefined
356
357 -- | Variant for 7-ary type constructors
358 class Typeable7 t where
359 typeOf7 :: t a b c d e f g -> TypeRep
360
361 -- | For defining a 'Typeable6' instance from any 'Typeable7' instance.
362 typeOf6Default :: (Typeable7 t, Typeable a) => t a b c d e f g -> TypeRep
363 typeOf6Default x = typeOf7 x `mkAppTy` typeOf (argType x)
364 where
365 argType :: t a b c d e f g -> a
366 argType = undefined
367
368 #ifdef __GLASGOW_HASKELL__
369 -- Given a @Typeable@/n/ instance for an /n/-ary type constructor,
370 -- define the instances for partial applications.
371 -- Programmers using non-GHC implementations must do this manually
372 -- for each type constructor.
373 -- (The INSTANCE_TYPEABLE/n/ macros in Typeable.h include this.)
374
375 -- | One Typeable instance for all Typeable1 instances
376 instance (Typeable1 s, Typeable a)
377 => Typeable (s a) where
378 typeOf = typeOfDefault
379
380 -- | One Typeable1 instance for all Typeable2 instances
381 instance (Typeable2 s, Typeable a)
382 => Typeable1 (s a) where
383 typeOf1 = typeOf1Default
384
385 -- | One Typeable2 instance for all Typeable3 instances
386 instance (Typeable3 s, Typeable a)
387 => Typeable2 (s a) where
388 typeOf2 = typeOf2Default
389
390 -- | One Typeable3 instance for all Typeable4 instances
391 instance (Typeable4 s, Typeable a)
392 => Typeable3 (s a) where
393 typeOf3 = typeOf3Default
394
395 -- | One Typeable4 instance for all Typeable5 instances
396 instance (Typeable5 s, Typeable a)
397 => Typeable4 (s a) where
398 typeOf4 = typeOf4Default
399
400 -- | One Typeable5 instance for all Typeable6 instances
401 instance (Typeable6 s, Typeable a)
402 => Typeable5 (s a) where
403 typeOf5 = typeOf5Default
404
405 -- | One Typeable6 instance for all Typeable7 instances
406 instance (Typeable7 s, Typeable a)
407 => Typeable6 (s a) where
408 typeOf6 = typeOf6Default
409
410 #endif /* __GLASGOW_HASKELL__ */
411
412 -------------------------------------------------------------
413 --
414 -- Type-safe cast
415 --
416 -------------------------------------------------------------
417
418 -- | The type-safe cast operation
419 cast :: (Typeable a, Typeable b) => a -> Maybe b
420 cast x = r
421 where
422 r = if typeOf x == typeOf (fromJust r)
423 then Just $ unsafeCoerce x
424 else Nothing
425
426 -- | A flexible variation parameterised in a type constructor
427 gcast :: (Typeable a, Typeable b) => c a -> Maybe (c b)
428 gcast x = r
429 where
430 r = if typeOf (getArg x) == typeOf (getArg (fromJust r))
431 then Just $ unsafeCoerce x
432 else Nothing
433 getArg :: c x -> x
434 getArg = undefined
435
436 -- | Cast for * -> *
437 gcast1 :: (Typeable1 t, Typeable1 t') => c (t a) -> Maybe (c (t' a))
438 gcast1 x = r
439 where
440 r = if typeOf1 (getArg x) == typeOf1 (getArg (fromJust r))
441 then Just $ unsafeCoerce x
442 else Nothing
443 getArg :: c x -> x
444 getArg = undefined
445
446 -- | Cast for * -> * -> *
447 gcast2 :: (Typeable2 t, Typeable2 t') => c (t a b) -> Maybe (c (t' a b))
448 gcast2 x = r
449 where
450 r = if typeOf2 (getArg x) == typeOf2 (getArg (fromJust r))
451 then Just $ unsafeCoerce x
452 else Nothing
453 getArg :: c x -> x
454 getArg = undefined
455
456 -------------------------------------------------------------
457 --
458 -- Instances of the Typeable classes for Prelude types
459 --
460 -------------------------------------------------------------
461
462 INSTANCE_TYPEABLE1([],listTc,"[]")
463 INSTANCE_TYPEABLE1(Maybe,maybeTc,"Maybe")
464 INSTANCE_TYPEABLE1(Ratio,ratioTc,"Ratio")
465 INSTANCE_TYPEABLE2(Either,eitherTc,"Either")
466 INSTANCE_TYPEABLE2((->),funTc,"->")
467 INSTANCE_TYPEABLE1(IO,ioTc,"IO")
468 #ifdef __GLASGOW_HASKELL__
469 INSTANCE_TYPEABLE2(ST,stTc,"ST")
470 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
471 #endif
472 INSTANCE_TYPEABLE0((),unitTc,"()")
473 #ifndef __NHC__
474 INSTANCE_TYPEABLE2((,),pairTc,",")
475 INSTANCE_TYPEABLE3((,,),tup3Tc,",,")
476
477 tup4Tc :: TyCon
478 tup4Tc = mkTyCon ",,,"
479
480 instance Typeable4 (,,,) where
481 typeOf4 tu = mkTyConApp tup4Tc []
482
483 tup5Tc :: TyCon
484 tup5Tc = mkTyCon ",,,,"
485
486 instance Typeable5 (,,,,) where
487 typeOf5 tu = mkTyConApp tup5Tc []
488
489 tup6Tc :: TyCon
490 tup6Tc = mkTyCon ",,,,,"
491
492 instance Typeable6 (,,,,,) where
493 typeOf6 tu = mkTyConApp tup6Tc []
494
495 tup7Tc :: TyCon
496 tup7Tc = mkTyCon ",,,,,,"
497
498 instance Typeable7 (,,,,,,) where
499 typeOf7 tu = mkTyConApp tup7Tc []
500
501 #endif /* __NHC__ */
502 INSTANCE_TYPEABLE1(Ptr,ptrTc,"Ptr")
503 INSTANCE_TYPEABLE1(StablePtr,stableptrTc,"StablePtr")
504 INSTANCE_TYPEABLE1(IORef,iorefTc,"IORef")
505
506 -------------------------------------------------------
507 --
508 -- Generate Typeable instances for standard datatypes
509 --
510 -------------------------------------------------------
511
512 INSTANCE_TYPEABLE0(Bool,boolTc,"Bool")
513 INSTANCE_TYPEABLE0(Char,charTc,"Char")
514 INSTANCE_TYPEABLE0(Float,floatTc,"Float")
515 INSTANCE_TYPEABLE0(Double,doubleTc,"Double")
516 INSTANCE_TYPEABLE0(Int,intTc,"Int")
517 #ifndef __NHC__
518 INSTANCE_TYPEABLE0(Word,wordTc,"Word" )
519 #endif
520 INSTANCE_TYPEABLE0(Integer,integerTc,"Integer")
521 INSTANCE_TYPEABLE0(Ordering,orderingTc,"Ordering")
522 INSTANCE_TYPEABLE0(Handle,handleTc,"Handle")
523
524 INSTANCE_TYPEABLE0(Int8,int8Tc,"Int8")
525 INSTANCE_TYPEABLE0(Int16,int16Tc,"Int16")
526 INSTANCE_TYPEABLE0(Int32,int32Tc,"Int32")
527 INSTANCE_TYPEABLE0(Int64,int64Tc,"Int64")
528
529 INSTANCE_TYPEABLE0(Word8,word8Tc,"Word8" )
530 INSTANCE_TYPEABLE0(Word16,word16Tc,"Word16")
531 INSTANCE_TYPEABLE0(Word32,word32Tc,"Word32")
532 INSTANCE_TYPEABLE0(Word64,word64Tc,"Word64")
533
534 INSTANCE_TYPEABLE0(TyCon,tyconTc,"TyCon")
535 INSTANCE_TYPEABLE0(TypeRep,typeRepTc,"TypeRep")
536
537 #ifdef __GLASGOW_HASKELL__
538 INSTANCE_TYPEABLE0(RealWorld,realWorldTc,"RealWorld")
539 #endif
540
541 ---------------------------------------------
542 --
543 -- Internals
544 --
545 ---------------------------------------------
546
547 #ifndef __HUGS__
548 newtype Key = Key Int deriving( Eq )
549 #endif
550
551 data KeyPr = KeyPr !Key !Key deriving( Eq )
552
553 hashKP :: KeyPr -> Int32
554 hashKP (KeyPr (Key k1) (Key k2)) = (HT.hashInt k1 + HT.hashInt k2) `rem` HT.prime
555
556 data Cache = Cache { next_key :: !(IORef Key),
557 tc_tbl :: !(HT.HashTable String Key),
558 ap_tbl :: !(HT.HashTable KeyPr Key) }
559
560 {-# NOINLINE cache #-}
561 cache :: Cache
562 cache = unsafePerformIO $ do
563 empty_tc_tbl <- HT.new (==) HT.hashString
564 empty_ap_tbl <- HT.new (==) hashKP
565 key_loc <- newIORef (Key 1)
566 return (Cache { next_key = key_loc,
567 tc_tbl = empty_tc_tbl,
568 ap_tbl = empty_ap_tbl })
569
570 newKey :: IORef Key -> IO Key
571 #ifdef __GLASGOW_HASKELL__
572 newKey kloc = do i <- genSym; return (Key i)
573 #else
574 newKey kloc = do { k@(Key i) <- readIORef kloc ;
575 writeIORef kloc (Key (i+1)) ;
576 return k }
577 #endif
578
579 #ifdef __GLASGOW_HASKELL__
580 -- In GHC we use the RTS's genSym function to get a new unique,
581 -- because in GHCi we might have two copies of the Data.Typeable
582 -- library running (one in the compiler and one in the running
583 -- program), and we need to make sure they don't share any keys.
584 --
585 -- This is really a hack. A better solution would be to centralise the
586 -- whole mutable state used by this module, i.e. both hashtables. But
587 -- the current solution solves the immediate problem, which is that
588 -- dynamics generated in one world with one type were erroneously
589 -- being recognised by the other world as having a different type.
590 foreign import ccall unsafe "genSymZh"
591 genSym :: IO Int
592 #endif
593
594 mkTyConKey :: String -> Key
595 mkTyConKey str
596 = unsafePerformIO $ do
597 let Cache {next_key = kloc, tc_tbl = tbl} = cache
598 mb_k <- HT.lookup tbl str
599 case mb_k of
600 Just k -> return k
601 Nothing -> do { k <- newKey kloc ;
602 HT.insert tbl str k ;
603 return k }
604
605 appKey :: Key -> Key -> Key
606 appKey k1 k2
607 = unsafePerformIO $ do
608 let Cache {next_key = kloc, ap_tbl = tbl} = cache
609 mb_k <- HT.lookup tbl kpr
610 case mb_k of
611 Just k -> return k
612 Nothing -> do { k <- newKey kloc ;
613 HT.insert tbl kpr k ;
614 return k }
615 where
616 kpr = KeyPr k1 k2
617
618 appKeys :: Key -> [Key] -> Key
619 appKeys k ks = foldl appKey k ks