Type checking expressions

Simon Peyton Jones simonpj at microsoft.com
Mon Mar 5 16:11:46 UTC 2018


Always cc ghc-devs!   Bottle-necking on me may well yield a slow response!   Or even Haskell-café.

What is the type of (\x -> [x,y])?   Where y is in scope with type y::a.   Presumably something like   a -> [a]?  Or is it forall a. a -> [a]?  And would your answer change if you had just (\x -> [x,x])?

Generalisation is tricky, and for terms with non-closed types it is hard to know what you need in your use-case.  A type like ‘a’ might be a very fine answer!

A lot depends on precisely what you are trying to do.

Simon

From: Peter Podlovics [mailto:peter.d.podlovics at gmail.com]
Sent: 05 March 2018 14:54
To: Simon Peyton Jones <simonpj at microsoft.com>
Subject: Re: Type checking expressions

My main concern with that approach is that it might not give the correct type. For example the hsPatType function only gives unconstrained types, so it is incorrect for any numeric literal, since it gives "a" instead of "Num a => a".

So the question is whether it is possible to retrieve the context of the type variables as well. Also this problem may arise in the case of expressions as well, that is why I scrapped that approach and tried to type check the AST with the TcM monad directly, but without any success.
Could you give me any leads on how to solve this problem?
Thanks in advance,
Peter

On Mon, Mar 5, 2018 at 9:44 AM, Simon Peyton Jones <simonpj at microsoft.com<mailto:simonpj at microsoft.com>> wrote:
Peter

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…

Simon

From: ghc-devs [mailto:ghc-devs-bounces at haskell.org<mailto:ghc-devs-bounces at haskell.org>] On Behalf Of Peter Podlovics
Sent: 02 March 2018 12:05
To: ghc-devs at haskell.org<mailto: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/6bb32bc4/attachment-0001.html>


More information about the ghc-devs mailing list