Getting a variables type as a string at runtime

Bit of a simple question but I'm struggling to find information on this via Google. I have a sum type, when the input is type A I want to process it, when it is type B I want to return MyError "Expected type A but actually got type B". However I also have C,D,E,F, etc which should also return similar errors so don't want to hardcode this string. Is there a function I can call on a variable to get the name of the data constructor that created it? I could create a typeName function but I want to avoid boiler plate if I can. For reference what I'm actually doing is parsing Java class files. The constant pool I've represented as a map of a sum ConstantPoolEntry type (the Java constant pool for some reason skips an index on some types so can't be a simple list). Certain Java class file entries have a reference to the constant pool that must be of a specific constant type so I want to report badly formed entries to the user. Thanks, Gareth

On Sun, Jan 12, 2014 at 02:04:40PM +0000, Gareth Morgan wrote:
I have a sum type, when the input is type A I want to process it, when it is type B I want to return MyError "Expected type A but actually got type B". However I also have C,D,E,F, etc which should also return similar errors so don't want to hardcode this string.
Would pattern matching be enough for you? i.e. (very crude): data Test = Alfa | Beta | Gamma deriving (Show) testAlfa Alfa = undefined -- put your process function here testAlfa o = error $ "wasn't expecting " ++ show o main = testAlfa Beta

Won't show include all the components? I wanted to include only the type
name.
I could probably get away with bundling all the contents into the error
string but it would be nice to know if there is a standard way to get type
names like this.
On Sun, Jan 12, 2014 at 2:33 PM, fa-ml
On Sun, Jan 12, 2014 at 02:04:40PM +0000, Gareth Morgan wrote:
I have a sum type, when the input is type A I want to process it, when it is type B I want to return MyError "Expected type A but actually got type B". However I also have C,D,E,F, etc which should also return similar errors so don't want to hardcode this string.
Would pattern matching be enough for you? i.e. (very crude):
data Test = Alfa | Beta | Gamma deriving (Show)
testAlfa Alfa = undefined -- put your process function here testAlfa o = error $ "wasn't expecting " ++ show o
main = testAlfa Beta
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan
Won't show include all the components? I wanted to include only the type name.
Types only exist at compile time normally. But if you add a Typeable constraint (see Data.Typeable and the DeriveDataTypeable extension) you can get type names at runtime. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Data.Typeable with Data.Data worked. Thank you.
Using Typeable alone was only giving me the type of the whole type.
Data.Data had the toConstr function needed to give me the particular
constructor name.
For reference if anyone is interested in the solution:
import Data.Typeable
import Data.Data
data A = A | B | C deriving (Typeable, Data)
typeName :: A -> String
typeName = show . toConstr
I needed to add DeriveDataTypeable extension to my cabal file
On Sun, Jan 12, 2014 at 2:53 PM, Brandon Allbery
On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan
wrote: Won't show include all the components? I wanted to include only the type name.
Types only exist at compile time normally. But if you add a Typeable constraint (see Data.Typeable and the DeriveDataTypeable extension) you can get type names at runtime.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

If you just wanted the constructor name, show would have worked perfectly.
On Sun Jan 12 2014 at 15:45:28, Gareth Morgan
Data.Typeable with Data.Data worked. Thank you.
Using Typeable alone was only giving me the type of the whole type. Data.Data had the toConstr function needed to give me the particular constructor name.
For reference if anyone is interested in the solution:
import Data.Typeable import Data.Data
data A = A | B | C deriving (Typeable, Data)
typeName :: A -> String typeName = show . toConstr
I needed to add DeriveDataTypeable extension to my cabal file
On Sun, Jan 12, 2014 at 2:53 PM, Brandon Allbery
wrote: On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan
wrote: Won't show include all the components? I wanted to include only the type name.
Types only exist at compile time normally. But if you add a Typeable constraint (see Data.Typeable and the DeriveDataTypeable extension) you can get type names at runtime.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
Benjamin Edwards
-
Brandon Allbery
-
fa-ml
-
Gareth Morgan