X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FDynamic.hs;h=4c449882508edd5beb4dd41a459b532116468f34;hb=f7a485978f04e84b086f1974b88887cc72d832d0;hp=fe3d6275f6d7a9b3643959a790b8b7a6e3ba93e1;hpb=5545727d5a6a1fc6d5d00f32a92a8fdf0fb7ca77;p=ghc-base.git diff --git a/Data/Dynamic.hs b/Data/Dynamic.hs index fe3d627..4c44988 100644 --- a/Data/Dynamic.hs +++ b/Data/Dynamic.hs @@ -1,16 +1,14 @@ {-# OPTIONS -fno-implicit-prelude #-} ----------------------------------------------------------------------------- --- +-- | -- Module : Data.Dynamic -- Copyright : (c) The University of Glasgow 2001 --- License : BSD-style (see the file libraries/core/LICENSE) +-- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : experimental -- Portability : portable -- --- $Id: Dynamic.hs,v 1.2 2001/07/03 11:37:49 simonmar Exp $ --- -- The Dynamic interface provides basic support for dynamic types. -- -- Operations for injecting values of arbitrary type into @@ -64,20 +62,23 @@ module Data.Dynamic ) where +import Data.Maybe +import Data.Either +import Data.Int +import Data.Word +import Foreign.Ptr +import Foreign.StablePtr + #ifdef __GLASGOW_HASKELL__ import GHC.Base -import GHC.Maybe import GHC.Show import GHC.Err import GHC.Num import GHC.Float import GHC.IOBase -import GHC.Dynamic #endif #ifdef __GLASGOW_HASKELL__ -import GHC.Prim ( unsafeCoerce# ) - unsafeCoerce :: a -> b unsafeCoerce = unsafeCoerce# #endif @@ -87,30 +88,22 @@ unsafeCoerce = unsafeCoerce# -- The dynamic type is represented by Dynamic, carrying -- the dynamic value along with its type representation: --- the instance just prints the type representation. +data Dynamic = Dynamic TypeRep Obj + instance Show Dynamic where + -- the instance just prints the type representation. showsPrec _ (Dynamic t _) = showString "<<" . showsPrec 0 t . showString ">>" --- Operations for going to and from Dynamic: - -toDyn :: Typeable a => a -> Dynamic -toDyn v = Dynamic (typeOf v) (unsafeCoerce v) - -fromDyn :: Typeable a => Dynamic -> a -> a -fromDyn (Dynamic t v) def - | typeOf def == t = unsafeCoerce v - | otherwise = def - -fromDynamic :: Typeable a => Dynamic -> Maybe a -fromDynamic (Dynamic t v) = - case unsafeCoerce v of - r | t == typeOf r -> Just r - | otherwise -> Nothing +data Obj = Obj + -- dummy type to hold the dynamically typed value. --- (Abstract) universal datatype: +data TypeRep + = App TyCon [TypeRep] + | Fun TypeRep TypeRep + deriving ( Eq ) instance Show TypeRep where showsPrec p (App tycon tys) = @@ -129,6 +122,31 @@ instance Show TypeRep where showParen (p > 8) $ showsPrec 9 f . showString " -> " . showsPrec 8 a +-- type constructors are +data TyCon = TyCon Int String + +instance Eq TyCon where + (TyCon t1 _) == (TyCon t2 _) = t1 == t2 + +instance Show TyCon where + showsPrec _ (TyCon _ s) = showString s + +-- Operations for going to and from Dynamic: + +toDyn :: Typeable a => a -> Dynamic +toDyn v = Dynamic (typeOf v) (unsafeCoerce v) + +fromDyn :: Typeable a => Dynamic -> a -> a +fromDyn (Dynamic t v) def + | typeOf def == t = unsafeCoerce v + | otherwise = def + +fromDynamic :: Typeable a => Dynamic -> Maybe a +fromDynamic (Dynamic t v) = + case unsafeCoerce v of + r | t == typeOf r -> Just r + | otherwise -> Nothing + -- To make it possible to convert values with user-defined types -- into type Dynamic, we need a systematic way of getting -- the type representation of an arbitrary type. A type @@ -145,9 +163,6 @@ isTupleTyCon :: TyCon -> Bool isTupleTyCon (TyCon _ (',':_)) = True isTupleTyCon _ = False -instance Show TyCon where - showsPrec _ (TyCon _ s) = showString s - -- If we enforce the restriction that there is only one -- @TyCon@ for a type & it is shared among all its uses, -- we can map them onto Ints very simply. The benefit is, @@ -282,6 +297,19 @@ INSTANCE_TYPEABLE2(Either,eitherTc,"Either") INSTANCE_TYPEABLE1(IO,ioTc,"IO") INSTANCE_TYPEABLE1(Maybe,maybeTc,"Maybe") INSTANCE_TYPEABLE0(Ordering,orderingTc,"Ordering") +INSTANCE_TYPEABLE0(Handle,handleTc,"Handle") +INSTANCE_TYPEABLE1(Ptr,ptrTc,"Ptr") +INSTANCE_TYPEABLE1(StablePtr,stablePtrTc,"StablePtr") + +INSTANCE_TYPEABLE0(Int8,int8Tc, "Int8") +INSTANCE_TYPEABLE0(Int16,int16Tc,"Int16") +INSTANCE_TYPEABLE0(Int32,int32Tc,"Int32") +INSTANCE_TYPEABLE0(Int64,int64Tc,"Int64") + +INSTANCE_TYPEABLE0(Word8,word8Tc, "Word8" ) +INSTANCE_TYPEABLE0(Word16,word16Tc,"Word16") +INSTANCE_TYPEABLE0(Word32,word32Tc,"Word32") +INSTANCE_TYPEABLE0(Word64,word64Tc,"Word64") INSTANCE_TYPEABLE0(TyCon,tyconTc,"TyCon") INSTANCE_TYPEABLE0(TypeRep,typeRepTc,"TypeRep")