Created
December 24, 2011 16:12
-
-
Save MnO2/1517679 to your computer and use it in GitHub Desktop.
Typeable and Dynamic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Dynamic | |
import Data.Maybe | |
hlist :: [Dynamic] | |
hlist = [toDyn "string", | |
toDyn (7::Int), | |
toDyn (pi :: Double), | |
toDyn 'x', | |
toDyn ((), Just "foo")] | |
dyn :: Dynamic | |
dyn = hlist !! 1 | |
v :: Int | |
v = case fromDynamic dyn of | |
Nothing -> error "Type mismatch" | |
Just x -> x | |
main = do putStrLn $ show v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Int | |
import Data.Word | |
import Data.Ord | |
import Data.Typeable | |
import Control.Monad.ST | |
import Control.Exception | |
import Foreign.C.Types | |
main = do | |
putStrLn $ show $ typeOf (True::Bool) | |
putStrLn $ show $ typeOf ('c'::Char) | |
putStrLn $ show $ typeOf (1.0::Double) | |
putStrLn $ show $ typeOf (1.0::Float) | |
putStrLn $ show $ typeOf (1::Int) | |
putStrLn $ show $ typeOf (1::Int8) | |
putStrLn $ show $ typeOf (1::Int16) | |
putStrLn $ show $ typeOf (1::Int32) | |
putStrLn $ show $ typeOf (1::Int64) | |
putStrLn $ show $ typeOf (1::Integer) | |
putStrLn $ show $ typeOf (undefined::Ordering) | |
putStrLn $ show $ typeOf (undefined::RealWorld) | |
putStrLn $ show $ typeOf (undefined::Word) | |
putStrLn $ show $ typeOf (undefined::Word8) | |
putStrLn $ show $ typeOf (undefined::Word16) | |
putStrLn $ show $ typeOf (undefined::Word32) | |
putStrLn $ show $ typeOf (undefined::Word64) | |
putStrLn $ show $ typeOf (undefined::()) | |
putStrLn $ show $ typeOf (undefined::TyCon) | |
putStrLn $ show $ typeOf (undefined::TypeRep) | |
putStrLn $ show $ typeOf (undefined::ArithException) | |
putStrLn $ show $ typeOf (undefined::ErrorCall) | |
putStrLn $ show $ typeOf (undefined::SomeException) | |
putStrLn $ show $ typeOf (undefined::IOException) | |
putStrLn $ show $ typeOf (undefined::CUIntMax) | |
putStrLn $ show $ typeOf (undefined::CIntMax) | |
putStrLn $ show $ typeOf (undefined::CUIntPtr) | |
putStrLn $ show $ typeOf (undefined::CIntPtr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Unsafe.Coerce | |
class Typeable a where | |
typeOf :: a -> TypeRep | |
data TypeRep = TR String [TypeRep] deriving (Eq, Show) | |
instance Typeable Int where | |
typeOf x = TR "Prelude.Int" [] | |
instance Typeable Bool where | |
typeOf x = TR "Prelude.Bool" [] | |
instance Typeable a => Typeable [a] where | |
typeOf x = TR "Prelude.List" [typeOf (get x)] | |
where | |
get :: [a] -> a | |
get = undefined | |
instance (Typeable a, Typeable b) => Typeable (a->b) where | |
typeOf f = TR "Prelude.->" [typeOf (getArg f), typeOf (getRes f)] | |
where getArg :: (a->b) -> a | |
getArg = undefined | |
getRes :: (a->b) -> b | |
getRes = undefined | |
cast :: (Typeable a, Typeable b) => a -> Maybe b | |
cast x = r | |
where | |
r = if typeOf x == typeOf (get r) | |
then Just (unsafeCoerce x) | |
else Nothing | |
get :: Maybe a -> a | |
get x = undefined | |
main = do | |
putStrLn $ show $ typeOf (1::Int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment