I am trying to parse Haskell source code and generate a decision tree to analyze different paths Haskell programs can take.
haskell-src-exts
gives a reasonable representation, but does not have any type information associated with it.
Does GHC or some other tool provide that functionality?

- 38,665
- 7
- 99
- 204

- 638
- 4
- 18
-
If you mean to get the type information of any expression (top-level or not) in a haskell source file, you may want to check [`ghc-mod`](http://hackage.haskell.org/package/ghc-mod) package. – zakyggaps Feb 07 '16 at 09:13
-
I want to generate a tree from program source code to analyze the potential paths the program can take. Simple text based parsing is not sufficient for my usecase and need type information as well at nodes of the tree i am going to generate – ankitrokdeonsns Feb 07 '16 at 10:09
-
Do you need to do it on the source level? It'd be much easier to implement this on Core, where nested pattern matching is already flattened out. – Cactus Feb 09 '16 at 12:45
-
I would prefer it at a higher level than core for my use case. I can work with the simplified version obtained from ghc -ddump-simple as well. – ankitrokdeonsns Feb 11 '16 at 17:17
1 Answers
There's no tool except GHC that is particularly adept at typechecking Haskell source at the moment. A haskell-type-exts
was in development to match src-exts
, but it was never completed.
So you can use a reasonable wrapper to the GHC API, such as hint, and invoke it on the subexpressions you want to check using its type inference api.
This is a rather painful approach, but I can't think of much better. If you're only interested in working on haskell-like code as an exercise, you could instead import the PureScript
compiler as a library, and then you'll be able to get a fully type annotated syntax tree in a more reasonable way.
Alternately, you can try to navigate the thicket of the GHC api itself to get fully typechecked source...
If you choose to go that route, this answer may get you started.