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