Type checking expressions

Simon Peyton Jones simonpj at microsoft.com
Mon Mar 5 08:44:10 UTC 2018


My goal is to determine the type of every expression, pattern etc. in the
syntax tree

After type checking is complete, the syntax tree is liberally annotated with types.

We do not yet have a function
            hsExprType :: HsExpr Id -> Type
but we do have
            TcHsTyn.hsPatType :: Pat GhcTc -> Type
and you or someone could readily make an equivalent for HsExpr.

Most type errors are reported by adding an error constraint, but still returning an annotated tree.
Some, I’m afraid, are still done in the old way, by throwing an exception – so you don’t get back an annotated tree in that case.  But they are relatively rare.

Others must have wanted something like this…


From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of Peter Podlovics
Sent: 02 March 2018 12:05
To: ghc-devs at haskell.org
Subject: Fwd: Type checking expressions

Hello everyone,

I would like to ask for some advice regarding the type checker part of GHC.
My goal is to determine the type of every expression, pattern etc. in the
syntax tree. Currently the compiler doesn't store this information, so I have
to type check manually. One important aspect is that the program may be ill-typed,
but I still want to extract as much information as possible.

I tried using local type checking functions (eg.: tcInferSigma), but whenever
I used it on an expression that had some "out-of-scope" names in it, it failed.

> f xs = length xs

The reason was that xs was not in the local environment.

My question is: how could I provide the necessary local environment for these
type checking functions? Also in the general case, is it possible to somehow
annotate each expression with its type during the type checking?

The motivation for this is that I want to implement a tool that automatically
corrects ill-typed programs based heuristics. For that I need to know the types
of certain AST nodes.

Peter Podlovics

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20180305/228616be/attachment.html>

More information about the ghc-devs mailing list