Type of an HsExpr

Simon Peyton-Jones simonpj at microsoft.com
Thu May 26 10:59:10 CEST 2011

| > tcRnExpr :: HscEnv
| >         -> InteractiveContext
| >         -> LHsExpr RdrName
| >         -> IO (Messages, Maybe Type)
| >
| > from TcRnDriver?
| This is pretty close to what I need. Unfortunately, I have
| LhsExpr Id not RdrName.

Just to be clear
  LHsExpr RdrName    is just after parsing
  LHsExpr Name       is just after the renamer
  LHsExpr Id         is just after the type checker

So if you have an LHsExpr Id you have a fully typechecked expression.

In principle, then, it should be straightforward to write a function
	lhsExprType :: LHsExpr Id -> Type

The only problem is that HsSyn is big: there are many constructors.  Nevertheless, it should be entirely straightforward. If someone wants to try, I'll gladly review.  I agree that it would be useful.  For example in an IDE you might want to select a sub-expression and see its type.

	Ranjit: if it'd be useful to you, you might be familiar enough by now...?

A hack is to desugar the expression to Core and use exprType, but that's very indirect and (as you observe) involves a monad for fresh names etc.

Let me know

| So, I guess the PostTcType is just not well defined within a ParsedModule.
| Thats why I use TypecheckedSource.

Correct.  As its name suggests PostTcType is only filled in post-typechecker.


More information about the Glasgow-haskell-users mailing list