Int "Str...">

Convert string to type constructor in Haskell

Does anyone know if there is a function in Haskell that does something like this:

"Int" -> Int "String" -> String "Bool" -> Bool 

i.e., it takes a string representation of the name of the type constructor and converts it into the actual type constructor, both in the expression and in the template.

edit: My common goal is to simplify something like:

 transExp (Add exp1 exp2) vars = transExp exp1 vars ++ transExp exp2 vars ++ [IAdd] transExp (Sub exp1 exp2) vars = transExp exp1 vars ++ transExp exp2 vars ++ [ISub] 

One coincidence with the pattern, so basically convert Add or Sub to a string, add "I" to the foreground and convert it back to type.

+7
source share
4 answers

There is a much better way to reorganize your code here without any Haskell or shhenanigans templates, simply by joining your Add and Sub cases into one:

 data BinOp = Add | Sub | ... data Expr = ... | BinOp BinOp Expr Expr | ... transExp (BinOp op exp1 exp2) vars = transExp exp1 vars ++ transExp exp2 vars ++ [transOp op] ... transOp Add = IAdd transOp Sub = ISub 

Thus, we use the data type to directly express the fact that binary operators are interconnected and therefore have similar translations. You can still match the match on BinOp Add exp1 exp2 if you want to create a special case somewhere.

+10
source

In what context? There is a Haskell template and Data.Typeable , but for a really useful answer you need to provide more details.

+2
source

Well, here is the problem.

 "String" -> String 

This is gibberish in Haskell-land because "String" is a value, but String is a type. So you can try the following:

 String -> a 

It does not do what you want. You must learn to read type signatures, because if you cannot read tick signatures, you will find yourself bad in Haskell. The above type means: "Give me a string, and I can give you a value of whatever type you request." The prelude has a function in this signature, it is called error , which is not what you want.

It looks like you need something in this direction:

 String -> TypeRep 

Sorry, there is no such function. TypeRep does not instantiate the Read class.

What are you really trying to do here? If you tell us what you are actually trying to do, we can help you with this problem, and not try to help with this problem.

0
source

You cannot do this because strings are data and runtime types that must be fully resolved at compile time. The best you can probably do with your example is a helper function to eliminate some of the duplication:

 helper exp1 exp2 vars op = transExp exp1 vars ++ transExp exp2 vars ++ [op] transExp (Add exp1 exp2) vars = helper exp1 exp2 vars IAdd transExp (Sub exp1 exp2) vars = helper exp1 exp2 vars ISub 

But this may not be very useful in the long run, depending on how many cases you have.

In general, pattern matching is something that works against type structures, so it must be written in a compilation language for specific type constructors. This is the price you have to pay for having a truly reliable static type system.

0
source

All Articles