haskell - typeOf with type constructors of kind *->* / printing type of value from within program -
consider following :
module main data tree = emptytree | node (tree a) (tree a) deriving (show, read, eq) data container b = container{contfield :: b a} deriving (show) result = container {contfield = node 'a' emptytree emptytree} main = print result
if load ghci, following type of result
:
*main> :t result result :: container char tree
how can print type container char tree
within program? trying adapt solution given @ haskell — typerep concrete type instance got stuck because not find way use typeof
in conjunction type constructors of kind * -> *
[edit] : of methods in post have been deprecated in ghc 7.8.1 release notes version 7.8.1 :
typeable poly-kinded, making typeable1, typeable2, etc., obsolete, deprecated, , relegated data.oldtypeable. furthermore, user-written instances of typeable disallowed: use deriving or new extension -xautoderivetypeable, create typeable instances every datatype declared in module.
one possibility create typeable
instance yourself. struggled bit creating tycon
container
, maybe there better way how it:
{-# language explicitforall #-} {-# language scopedtypevariables #-} {-# language derivedatatypeable #-} module main import data.dynamic import data.typeable data tree = emptytree | node (tree a) (tree a) deriving (show, read, eq, typeable) -- copy representation of type constructor -- existing representation copytycon :: typeable => -> string -> tycon copytycon x = mktycon3 (tyconpackage tc) (tyconmodule tc) tc = typereptycon (typeof x) data dummy = dummy -- package/module names container deriving (typeable) data container b = container { contfield :: b } deriving (show) instance (typeable a, typeable1 f) => typeable (container f) typeof (container x) = mktyconapp (copytycon dummy "container") [typeof (undefined :: a), typeof1 x] result = container { contfield = node 'a' emptytree emptytree } main = print $ typeof result print result
take grain of salt, i'm not experienced typeable
.
Comments
Post a Comment