is there a way to convert an expression in Haskell language to a Tree like format and extract information from each node. Or Generate all possible trees for a given expression ? Ex: add 5 3 This is tree where, 3 and 5 are leaf nodes and "add" as the root. So what I need is to get decompose the above expression so that I can do further analysis on the expression. eg: * to know what are the values that I'm going to "add". * to know what is the operation that I'm performing over the given numbers (here "add", also it could be "subtraction", "multiplication" etc.) Really appreciate your help :)
Asked
Active
Viewed 121 times
0
-
possible duplicate of [Dynamic loading of Haskell abstract syntax expression](http://stackoverflow.com/questions/8881263/dynamic-loading-of-haskell-abstract-syntax-expression). In general, questions under the [ghc-api] and [template-haskell] tags might prove useful. – duplode Sep 14 '15 at 20:15
1 Answers
7
You could use Template Haskell:
Language.Haskell.TH> runQ [e|3 + 5|]
InfixE (Just (LitE (IntegerL 3))) (VarE GHC.Num.+) (Just (LitE (IntegerL 5)))
Or haskell-src-exts
:
Language.Haskell.Exts> parse "3 + 5" :: ParseResult Exp
ParseOk (InfixApp (Lit (Int 3)) (QVarOp (UnQual (Symbol "+"))) (Lit (Int 5)))

Daniel Wagner
- 145,880
- 9
- 220
- 380