5 , ForeignFunctionInterface
8 {-# OPTIONS_GHC -funbox-strict-fields #-}
10 -- The -XOverlappingInstances flag allows the user to over-ride
11 -- the instances for Typeable given here. In particular, we provide an instance
12 -- instance ... => Typeable (s a)
13 -- But a user might want to say
14 -- instance ... => Typeable (MyType a b)
16 -----------------------------------------------------------------------------
18 -- Module : Data.Typeable
19 -- Copyright : (c) The University of Glasgow, CWI 2001--2004
20 -- License : BSD-style (see the file libraries/base/LICENSE)
22 -- Maintainer : libraries@haskell.org
23 -- Stability : experimental
24 -- Portability : portable
26 -- The 'Typeable' class reifies types to some extent by associating type
27 -- representations to types. These type representations can be compared,
28 -- and one can in turn define a type-safe cast operation. To this end,
29 -- an unsafe cast is guarded by a test for type (representation)
30 -- equivalence. The module "Data.Dynamic" uses Typeable for an
31 -- implementation of dynamics. The module "Data.Data" uses Typeable
32 -- and type-safe cast (but not dynamics) to support the \"Scrap your
33 -- boilerplate\" style of generic programming.
35 -----------------------------------------------------------------------------
40 -- * The Typeable class
41 Typeable( typeOf ), -- :: a -> TypeRep
44 cast, -- :: (Typeable a, Typeable b) => a -> Maybe b
45 gcast, -- a generalisation of cast
47 -- * Type representations
48 TypeRep, -- abstract, instance of: Eq, Show, Typeable
49 TyCon, -- abstract, instance of: Eq, Show, Typeable
52 -- * Construction of type representations
53 mkTyCon, -- :: String -> TyCon
54 mkTyConApp, -- :: TyCon -> [TypeRep] -> TypeRep
55 mkAppTy, -- :: TypeRep -> TypeRep -> TypeRep
56 mkFunTy, -- :: TypeRep -> TypeRep -> TypeRep
58 -- * Observation of type representations
59 splitTyConApp, -- :: TypeRep -> (TyCon, [TypeRep])
60 funResultTy, -- :: TypeRep -> TypeRep -> Maybe TypeRep
61 typeRepTyCon, -- :: TypeRep -> TyCon
62 typeRepArgs, -- :: TypeRep -> [TypeRep]
63 tyConString, -- :: TyCon -> String
64 typeRepKey, -- :: TypeRep -> IO Int
66 -- * The other Typeable classes
67 -- | /Note:/ The general instances are provided for GHC only.
68 Typeable1( typeOf1 ), -- :: t a -> TypeRep
69 Typeable2( typeOf2 ), -- :: t a b -> TypeRep
70 Typeable3( typeOf3 ), -- :: t a b c -> TypeRep
71 Typeable4( typeOf4 ), -- :: t a b c d -> TypeRep
72 Typeable5( typeOf5 ), -- :: t a b c d e -> TypeRep
73 Typeable6( typeOf6 ), -- :: t a b c d e f -> TypeRep
74 Typeable7( typeOf7 ), -- :: t a b c d e f g -> TypeRep
75 gcast1, -- :: ... => c (t a) -> Maybe (c (t' a))
76 gcast2, -- :: ... => c (t a b) -> Maybe (c (t' a b))
78 -- * Default instances
79 -- | /Note:/ These are not needed by GHC, for which these instances
80 -- are generated by general instance declarations.
81 typeOfDefault, -- :: (Typeable1 t, Typeable a) => t a -> TypeRep
82 typeOf1Default, -- :: (Typeable2 t, Typeable a) => t a b -> TypeRep
83 typeOf2Default, -- :: (Typeable3 t, Typeable a) => t a b c -> TypeRep
84 typeOf3Default, -- :: (Typeable4 t, Typeable a) => t a b c d -> TypeRep
85 typeOf4Default, -- :: (Typeable5 t, Typeable a) => t a b c d e -> TypeRep
86 typeOf5Default, -- :: (Typeable6 t, Typeable a) => t a b c d e f -> TypeRep
87 typeOf6Default -- :: (Typeable7 t, Typeable a) => t a b c d e f g -> TypeRep
91 import qualified Data.HashTable as HT
95 import Data.List( foldl, intersperse )
98 #ifdef __GLASGOW_HASKELL__
100 import GHC.Show (Show(..), ShowS,
101 shows, showString, showChar, showParen)
102 import GHC.Err (undefined)
103 import GHC.Num (Integer, (+))
104 import GHC.Real ( rem, Ratio )
105 import GHC.IORef (IORef,newIORef)
106 import GHC.IO (unsafePerformIO,mask_)
108 -- These imports are so we can define Typeable instances
109 -- It'd be better to give Typeable instances in the modules themselves
110 -- but they all have to be compiled before Typeable
114 import GHC.STRef ( STRef )
115 import GHC.Ptr ( Ptr, FunPtr )
116 import GHC.Stable ( StablePtr, newStablePtr, freeStablePtr,
117 deRefStablePtr, castStablePtrToPtr,
119 import GHC.Arr ( Array, STArray )
124 import Hugs.Prelude ( Key(..), TypeRep(..), TyCon(..), Ratio,
125 Handle, Ptr, FunPtr, ForeignPtr, StablePtr )
126 import Hugs.IORef ( IORef, newIORef, readIORef, writeIORef )
127 import Hugs.IOExts ( unsafePerformIO )
128 -- For the Typeable instance
129 import Hugs.Array ( Array )
131 import Hugs.ConcBase ( MVar )
135 import NHC.IOExtras (IOArray,IORef,newIORef,readIORef,writeIORef,unsafePerformIO)
138 -- For the Typeable instance
139 import NHC.FFI ( Ptr,FunPtr,StablePtr,ForeignPtr )
140 import Array ( Array )
143 #include "Typeable.h"
147 -------------------------------------------------------------
149 -- Type representations
151 -------------------------------------------------------------
153 -- | A concrete representation of a (monomorphic) type. 'TypeRep'
154 -- supports reasonably efficient equality.
155 data TypeRep = TypeRep !Key TyCon [TypeRep]
157 -- Compare keys for equality
158 instance Eq TypeRep where
159 (TypeRep k1 _ _) == (TypeRep k2 _ _) = k1 == k2
161 -- | An abstract representation of a type constructor. 'TyCon' objects can
162 -- be built using 'mkTyCon'.
163 data TyCon = TyCon !Key String
165 instance Eq TyCon where
166 (TyCon t1 _) == (TyCon t2 _) = t1 == t2
169 -- | Returns a unique integer associated with a 'TypeRep'. This can
170 -- be used for making a mapping with TypeReps
171 -- as the keys, for example. It is guaranteed that @t1 == t2@ if and only if
172 -- @typeRepKey t1 == typeRepKey t2@.
174 -- It is in the 'IO' monad because the actual value of the key may
175 -- vary from run to run of the program. You should only rely on
176 -- the equality property, not any actual key value. The relative ordering
177 -- of keys has no meaning either.
179 typeRepKey :: TypeRep -> IO Int
180 typeRepKey (TypeRep (Key i) _ _) = return i
183 -- let fTy = mkTyCon "Foo" in show (mkTyConApp (mkTyCon ",,")
186 -- returns "(Foo,Foo,Foo)"
188 -- The TypeRep Show instance promises to print tuple types
189 -- correctly. Tuple type constructors are specified by a
190 -- sequence of commas, e.g., (mkTyCon ",,,,") returns
191 -- the 5-tuple tycon.
193 ----------------- Construction --------------------
195 -- | Applies a type constructor to a sequence of types
196 mkTyConApp :: TyCon -> [TypeRep] -> TypeRep
197 mkTyConApp tc@(TyCon tc_k _) args
198 = TypeRep (appKeys tc_k arg_ks) tc args
200 arg_ks = [k | TypeRep k _ _ <- args]
202 -- | A special case of 'mkTyConApp', which applies the function
203 -- type constructor to a pair of types.
204 mkFunTy :: TypeRep -> TypeRep -> TypeRep
205 mkFunTy f a = mkTyConApp funTc [f,a]
207 -- | Splits a type constructor application
208 splitTyConApp :: TypeRep -> (TyCon,[TypeRep])
209 splitTyConApp (TypeRep _ tc trs) = (tc,trs)
211 -- | Applies a type to a function type. Returns: @'Just' u@ if the
212 -- first argument represents a function of type @t -> u@ and the
213 -- second argument represents a function of type @t@. Otherwise,
214 -- returns 'Nothing'.
215 funResultTy :: TypeRep -> TypeRep -> Maybe TypeRep
216 funResultTy trFun trArg
217 = case splitTyConApp trFun of
218 (tc, [t1,t2]) | tc == funTc && t1 == trArg -> Just t2
221 -- | Adds a TypeRep argument to a TypeRep.
222 mkAppTy :: TypeRep -> TypeRep -> TypeRep
223 mkAppTy (TypeRep tr_k tc trs) arg_tr
224 = let (TypeRep arg_k _ _) = arg_tr
225 in TypeRep (appKey tr_k arg_k) tc (trs++[arg_tr])
227 -- If we enforce the restriction that there is only one
228 -- @TyCon@ for a type & it is shared among all its uses,
229 -- we can map them onto Ints very simply. The benefit is,
230 -- of course, that @TyCon@s can then be compared efficiently.
232 -- Provided the implementor of other @Typeable@ instances
233 -- takes care of making all the @TyCon@s CAFs (toplevel constants),
236 -- If this constraint does turn out to be a sore thumb, changing
237 -- the Eq instance for TyCons is trivial.
239 -- | Builds a 'TyCon' object representing a type constructor. An
240 -- implementation of "Data.Typeable" should ensure that the following holds:
242 -- > mkTyCon "a" == mkTyCon "a"
245 mkTyCon :: String -- ^ the name of the type constructor (should be unique
246 -- in the program, so it might be wise to use the
247 -- fully qualified name).
248 -> TyCon -- ^ A unique 'TyCon' object
249 mkTyCon str = TyCon (mkTyConKey str) str
251 ----------------- Observation ---------------------
253 -- | Observe the type constructor of a type representation
254 typeRepTyCon :: TypeRep -> TyCon
255 typeRepTyCon (TypeRep _ tc _) = tc
257 -- | Observe the argument types of a type representation
258 typeRepArgs :: TypeRep -> [TypeRep]
259 typeRepArgs (TypeRep _ _ args) = args
261 -- | Observe string encoding of a type representation
262 tyConString :: TyCon -> String
263 tyConString (TyCon _ str) = str
265 ----------------- Showing TypeReps --------------------
267 instance Show TypeRep where
268 showsPrec p (TypeRep _ tycon tys) =
270 [] -> showsPrec p tycon
271 [x] | tycon == listTc -> showChar '[' . shows x . showChar ']'
272 [a,r] | tycon == funTc -> showParen (p > 8) $
276 xs | isTupleTyCon tycon -> showTuple xs
283 showsTypeRep :: TypeRep -> ShowS
286 instance Show TyCon where
287 showsPrec _ (TyCon _ s) = showString s
289 isTupleTyCon :: TyCon -> Bool
290 isTupleTyCon (TyCon _ ('(':',':_)) = True
291 isTupleTyCon _ = False
293 -- Some (Show.TypeRep) helpers:
295 showArgs :: Show a => [a] -> ShowS
297 showArgs [a] = showsPrec 10 a
298 showArgs (a:as) = showsPrec 10 a . showString " " . showArgs as
300 showTuple :: [TypeRep] -> ShowS
301 showTuple args = showChar '('
302 . (foldr (.) id $ intersperse (showChar ',')
303 $ map (showsPrec 10) args)
306 -------------------------------------------------------------
308 -- The Typeable class and friends
310 -------------------------------------------------------------
312 {- Note [Memoising typeOf]
313 ~~~~~~~~~~~~~~~~~~~~~~~~~~
314 IMPORTANT: we don't want to recalculate the type-rep once per
315 call to the dummy argument. This is what went wrong in Trac #3245
316 So we help GHC by manually keeping the 'rep' *outside* the value
319 typeOfDefault :: forall t a. (Typeable1 t, Typeable a) => t a -> TypeRep
320 typeOfDefault = \_ -> rep
322 rep = typeOf1 (undefined :: t a) `mkAppTy`
323 typeOf (undefined :: a)
325 Notice the crucial use of scoped type variables here!
328 -- | The class 'Typeable' allows a concrete representation of a type to
330 class Typeable a where
331 typeOf :: a -> TypeRep
332 -- ^ Takes a value of type @a@ and returns a concrete representation
333 -- of that type. The /value/ of the argument should be ignored by
334 -- any instance of 'Typeable', so that it is safe to pass 'undefined' as
337 -- | Variant for unary type constructors
338 class Typeable1 t where
339 typeOf1 :: t a -> TypeRep
341 #ifdef __GLASGOW_HASKELL__
342 -- | For defining a 'Typeable' instance from any 'Typeable1' instance.
343 typeOfDefault :: forall t a. (Typeable1 t, Typeable a) => t a -> TypeRep
344 typeOfDefault = \_ -> rep
346 rep = typeOf1 (undefined :: t a) `mkAppTy`
347 typeOf (undefined :: a)
348 -- Note [Memoising typeOf]
350 -- | For defining a 'Typeable' instance from any 'Typeable1' instance.
351 typeOfDefault :: (Typeable1 t, Typeable a) => t a -> TypeRep
352 typeOfDefault x = typeOf1 x `mkAppTy` typeOf (argType x)
358 -- | Variant for binary type constructors
359 class Typeable2 t where
360 typeOf2 :: t a b -> TypeRep
362 #ifdef __GLASGOW_HASKELL__
363 -- | For defining a 'Typeable1' instance from any 'Typeable2' instance.
364 typeOf1Default :: forall t a b. (Typeable2 t, Typeable a) => t a b -> TypeRep
365 typeOf1Default = \_ -> rep
367 rep = typeOf2 (undefined :: t a b) `mkAppTy`
368 typeOf (undefined :: a)
369 -- Note [Memoising typeOf]
371 -- | For defining a 'Typeable1' instance from any 'Typeable2' instance.
372 typeOf1Default :: (Typeable2 t, Typeable a) => t a b -> TypeRep
373 typeOf1Default x = typeOf2 x `mkAppTy` typeOf (argType x)
375 argType :: t a b -> a
379 -- | Variant for 3-ary type constructors
380 class Typeable3 t where
381 typeOf3 :: t a b c -> TypeRep
383 #ifdef __GLASGOW_HASKELL__
384 -- | For defining a 'Typeable2' instance from any 'Typeable3' instance.
385 typeOf2Default :: forall t a b c. (Typeable3 t, Typeable a) => t a b c -> TypeRep
386 typeOf2Default = \_ -> rep
388 rep = typeOf3 (undefined :: t a b c) `mkAppTy`
389 typeOf (undefined :: a)
390 -- Note [Memoising typeOf]
392 -- | For defining a 'Typeable2' instance from any 'Typeable3' instance.
393 typeOf2Default :: (Typeable3 t, Typeable a) => t a b c -> TypeRep
394 typeOf2Default x = typeOf3 x `mkAppTy` typeOf (argType x)
396 argType :: t a b c -> a
400 -- | Variant for 4-ary type constructors
401 class Typeable4 t where
402 typeOf4 :: t a b c d -> TypeRep
404 #ifdef __GLASGOW_HASKELL__
405 -- | For defining a 'Typeable3' instance from any 'Typeable4' instance.
406 typeOf3Default :: forall t a b c d. (Typeable4 t, Typeable a) => t a b c d -> TypeRep
407 typeOf3Default = \_ -> rep
409 rep = typeOf4 (undefined :: t a b c d) `mkAppTy`
410 typeOf (undefined :: a)
411 -- Note [Memoising typeOf]
413 -- | For defining a 'Typeable3' instance from any 'Typeable4' instance.
414 typeOf3Default :: (Typeable4 t, Typeable a) => t a b c d -> TypeRep
415 typeOf3Default x = typeOf4 x `mkAppTy` typeOf (argType x)
417 argType :: t a b c d -> a
421 -- | Variant for 5-ary type constructors
422 class Typeable5 t where
423 typeOf5 :: t a b c d e -> TypeRep
425 #ifdef __GLASGOW_HASKELL__
426 -- | For defining a 'Typeable4' instance from any 'Typeable5' instance.
427 typeOf4Default :: forall t a b c d e. (Typeable5 t, Typeable a) => t a b c d e -> TypeRep
428 typeOf4Default = \_ -> rep
430 rep = typeOf5 (undefined :: t a b c d e) `mkAppTy`
431 typeOf (undefined :: a)
432 -- Note [Memoising typeOf]
434 -- | For defining a 'Typeable4' instance from any 'Typeable5' instance.
435 typeOf4Default :: (Typeable5 t, Typeable a) => t a b c d e -> TypeRep
436 typeOf4Default x = typeOf5 x `mkAppTy` typeOf (argType x)
438 argType :: t a b c d e -> a
442 -- | Variant for 6-ary type constructors
443 class Typeable6 t where
444 typeOf6 :: t a b c d e f -> TypeRep
446 #ifdef __GLASGOW_HASKELL__
447 -- | For defining a 'Typeable5' instance from any 'Typeable6' instance.
448 typeOf5Default :: forall t a b c d e f. (Typeable6 t, Typeable a) => t a b c d e f -> TypeRep
449 typeOf5Default = \_ -> rep
451 rep = typeOf6 (undefined :: t a b c d e f) `mkAppTy`
452 typeOf (undefined :: a)
453 -- Note [Memoising typeOf]
455 -- | For defining a 'Typeable5' instance from any 'Typeable6' instance.
456 typeOf5Default :: (Typeable6 t, Typeable a) => t a b c d e f -> TypeRep
457 typeOf5Default x = typeOf6 x `mkAppTy` typeOf (argType x)
459 argType :: t a b c d e f -> a
463 -- | Variant for 7-ary type constructors
464 class Typeable7 t where
465 typeOf7 :: t a b c d e f g -> TypeRep
467 #ifdef __GLASGOW_HASKELL__
468 -- | For defining a 'Typeable6' instance from any 'Typeable7' instance.
469 typeOf6Default :: forall t a b c d e f g. (Typeable7 t, Typeable a) => t a b c d e f g -> TypeRep
470 typeOf6Default = \_ -> rep
472 rep = typeOf7 (undefined :: t a b c d e f g) `mkAppTy`
473 typeOf (undefined :: a)
474 -- Note [Memoising typeOf]
476 -- | For defining a 'Typeable6' instance from any 'Typeable7' instance.
477 typeOf6Default :: (Typeable7 t, Typeable a) => t a b c d e f g -> TypeRep
478 typeOf6Default x = typeOf7 x `mkAppTy` typeOf (argType x)
480 argType :: t a b c d e f g -> a
484 #ifdef __GLASGOW_HASKELL__
485 -- Given a @Typeable@/n/ instance for an /n/-ary type constructor,
486 -- define the instances for partial applications.
487 -- Programmers using non-GHC implementations must do this manually
488 -- for each type constructor.
489 -- (The INSTANCE_TYPEABLE/n/ macros in Typeable.h include this.)
491 -- | One Typeable instance for all Typeable1 instances
492 instance (Typeable1 s, Typeable a)
493 => Typeable (s a) where
494 typeOf = typeOfDefault
496 -- | One Typeable1 instance for all Typeable2 instances
497 instance (Typeable2 s, Typeable a)
498 => Typeable1 (s a) where
499 typeOf1 = typeOf1Default
501 -- | One Typeable2 instance for all Typeable3 instances
502 instance (Typeable3 s, Typeable a)
503 => Typeable2 (s a) where
504 typeOf2 = typeOf2Default
506 -- | One Typeable3 instance for all Typeable4 instances
507 instance (Typeable4 s, Typeable a)
508 => Typeable3 (s a) where
509 typeOf3 = typeOf3Default
511 -- | One Typeable4 instance for all Typeable5 instances
512 instance (Typeable5 s, Typeable a)
513 => Typeable4 (s a) where
514 typeOf4 = typeOf4Default
516 -- | One Typeable5 instance for all Typeable6 instances
517 instance (Typeable6 s, Typeable a)
518 => Typeable5 (s a) where
519 typeOf5 = typeOf5Default
521 -- | One Typeable6 instance for all Typeable7 instances
522 instance (Typeable7 s, Typeable a)
523 => Typeable6 (s a) where
524 typeOf6 = typeOf6Default
526 #endif /* __GLASGOW_HASKELL__ */
528 -------------------------------------------------------------
532 -------------------------------------------------------------
534 -- | The type-safe cast operation
535 cast :: (Typeable a, Typeable b) => a -> Maybe b
538 r = if typeOf x == typeOf (fromJust r)
539 then Just $ unsafeCoerce x
542 -- | A flexible variation parameterised in a type constructor
543 gcast :: (Typeable a, Typeable b) => c a -> Maybe (c b)
546 r = if typeOf (getArg x) == typeOf (getArg (fromJust r))
547 then Just $ unsafeCoerce x
553 gcast1 :: (Typeable1 t, Typeable1 t') => c (t a) -> Maybe (c (t' a))
556 r = if typeOf1 (getArg x) == typeOf1 (getArg (fromJust r))
557 then Just $ unsafeCoerce x
562 -- | Cast for * -> * -> *
563 gcast2 :: (Typeable2 t, Typeable2 t') => c (t a b) -> Maybe (c (t' a b))
566 r = if typeOf2 (getArg x) == typeOf2 (getArg (fromJust r))
567 then Just $ unsafeCoerce x
572 -------------------------------------------------------------
574 -- Instances of the Typeable classes for Prelude types
576 -------------------------------------------------------------
578 INSTANCE_TYPEABLE0((),unitTc,"()")
579 INSTANCE_TYPEABLE1([],listTc,"[]")
580 INSTANCE_TYPEABLE1(Maybe,maybeTc,"Maybe")
581 INSTANCE_TYPEABLE1(Ratio,ratioTc,"Ratio")
582 INSTANCE_TYPEABLE2((->),funTc,"->")
583 INSTANCE_TYPEABLE1(IO,ioTc,"IO")
585 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
586 -- Types defined in GHC.MVar
587 INSTANCE_TYPEABLE1(MVar,mvarTc,"MVar" )
590 INSTANCE_TYPEABLE2(Array,arrayTc,"Array")
591 INSTANCE_TYPEABLE2(IOArray,iOArrayTc,"IOArray")
593 #ifdef __GLASGOW_HASKELL__
594 -- Hugs has these too, but their Typeable<n> instances are defined
595 -- elsewhere to keep this module within Haskell 98.
596 -- This is important because every invocation of runhugs or ffihugs
597 -- uses this module via Data.Dynamic.
598 INSTANCE_TYPEABLE2(ST,stTc,"ST")
599 INSTANCE_TYPEABLE2(STRef,stRefTc,"STRef")
600 INSTANCE_TYPEABLE3(STArray,sTArrayTc,"STArray")
604 INSTANCE_TYPEABLE2((,),pairTc,"(,)")
605 INSTANCE_TYPEABLE3((,,),tup3Tc,"(,,)")
606 INSTANCE_TYPEABLE4((,,,),tup4Tc,"(,,,)")
607 INSTANCE_TYPEABLE5((,,,,),tup5Tc,"(,,,,)")
608 INSTANCE_TYPEABLE6((,,,,,),tup6Tc,"(,,,,,)")
609 INSTANCE_TYPEABLE7((,,,,,,),tup7Tc,"(,,,,,,)")
612 INSTANCE_TYPEABLE1(Ptr,ptrTc,"Ptr")
613 INSTANCE_TYPEABLE1(FunPtr,funPtrTc,"FunPtr")
614 #ifndef __GLASGOW_HASKELL__
615 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
617 INSTANCE_TYPEABLE1(StablePtr,stablePtrTc,"StablePtr")
618 INSTANCE_TYPEABLE1(IORef,iORefTc,"IORef")
620 -------------------------------------------------------
622 -- Generate Typeable instances for standard datatypes
624 -------------------------------------------------------
626 INSTANCE_TYPEABLE0(Bool,boolTc,"Bool")
627 INSTANCE_TYPEABLE0(Char,charTc,"Char")
628 INSTANCE_TYPEABLE0(Float,floatTc,"Float")
629 INSTANCE_TYPEABLE0(Double,doubleTc,"Double")
630 INSTANCE_TYPEABLE0(Int,intTc,"Int")
632 INSTANCE_TYPEABLE0(Word,wordTc,"Word" )
634 INSTANCE_TYPEABLE0(Integer,integerTc,"Integer")
635 INSTANCE_TYPEABLE0(Ordering,orderingTc,"Ordering")
636 #ifndef __GLASGOW_HASKELL__
637 INSTANCE_TYPEABLE0(Handle,handleTc,"Handle")
640 INSTANCE_TYPEABLE0(Int8,int8Tc,"Int8")
641 INSTANCE_TYPEABLE0(Int16,int16Tc,"Int16")
642 INSTANCE_TYPEABLE0(Int32,int32Tc,"Int32")
643 INSTANCE_TYPEABLE0(Int64,int64Tc,"Int64")
645 INSTANCE_TYPEABLE0(Word8,word8Tc,"Word8" )
646 INSTANCE_TYPEABLE0(Word16,word16Tc,"Word16")
647 INSTANCE_TYPEABLE0(Word32,word32Tc,"Word32")
648 INSTANCE_TYPEABLE0(Word64,word64Tc,"Word64")
650 INSTANCE_TYPEABLE0(TyCon,tyconTc,"TyCon")
651 INSTANCE_TYPEABLE0(TypeRep,typeRepTc,"TypeRep")
653 #ifdef __GLASGOW_HASKELL__
654 INSTANCE_TYPEABLE0(RealWorld,realWorldTc,"RealWorld")
657 ---------------------------------------------
661 ---------------------------------------------
664 newtype Key = Key Int deriving( Eq )
667 data KeyPr = KeyPr !Key !Key deriving( Eq )
669 hashKP :: KeyPr -> Int32
670 hashKP (KeyPr (Key k1) (Key k2)) = (HT.hashInt k1 + HT.hashInt k2) `rem` HT.prime
672 data Cache = Cache { next_key :: !(IORef Key), -- Not used by GHC (calls genSym instead)
673 tc_tbl :: !(HT.HashTable String Key),
674 ap_tbl :: !(HT.HashTable KeyPr Key) }
676 {-# NOINLINE cache #-}
677 #ifdef __GLASGOW_HASKELL__
678 foreign import ccall unsafe "RtsTypeable.h getOrSetTypeableStore"
679 getOrSetTypeableStore :: Ptr a -> IO (Ptr a)
683 cache = unsafePerformIO $ do
684 empty_tc_tbl <- HT.new (==) HT.hashString
685 empty_ap_tbl <- HT.new (==) hashKP
686 key_loc <- newIORef (Key 1)
687 let ret = Cache { next_key = key_loc,
688 tc_tbl = empty_tc_tbl,
689 ap_tbl = empty_ap_tbl }
690 #ifdef __GLASGOW_HASKELL__
692 stable_ref <- newStablePtr ret
693 let ref = castStablePtrToPtr stable_ref
694 ref2 <- getOrSetTypeableStore ref
696 then deRefStablePtr stable_ref
698 freeStablePtr stable_ref
700 (castPtrToStablePtr ref2)
705 newKey :: IORef Key -> IO Key
706 #ifdef __GLASGOW_HASKELL__
707 newKey _ = do i <- genSym; return (Key i)
709 newKey kloc = do { k@(Key i) <- readIORef kloc ;
710 writeIORef kloc (Key (i+1)) ;
714 #ifdef __GLASGOW_HASKELL__
715 foreign import ccall unsafe "genSymZh"
719 mkTyConKey :: String -> Key
721 = unsafePerformIO $ do
722 let Cache {next_key = kloc, tc_tbl = tbl} = cache
723 mb_k <- HT.lookup tbl str
726 Nothing -> do { k <- newKey kloc ;
727 HT.insert tbl str k ;
730 appKey :: Key -> Key -> Key
732 = unsafePerformIO $ do
733 let Cache {next_key = kloc, ap_tbl = tbl} = cache
734 mb_k <- HT.lookup tbl kpr
737 Nothing -> do { k <- newKey kloc ;
738 HT.insert tbl kpr k ;
743 appKeys :: Key -> [Key] -> Key
744 appKeys k ks = foldl appKey k ks