2

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?

4
  • 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 package. Commented Feb 7, 2016 at 9: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 Commented Feb 7, 2016 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. Commented Feb 9, 2016 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. Commented Feb 11, 2016 at 17:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.