Type of an HsExpr

Sven Urbanski svenurbanski at gmail.com
Mon May 23 16:51:29 CEST 2011

Hello everyone,

On Thu, May 19, 2011 at 09:17, Simon Peyton-Jones <simonpj at microsoft.com> wrote:
> Maybe you want
> 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.

A little detail: I am traversing the LHsExprs inside a "TypecheckedSource".
Since I mostly only need the HsExprs, I could also use a "ParsedModule".
I have tried switching to ParsedModule. But whenever I use the PostTcType from
a MatchGroup (within a FunBind) I get:
 panic! (the 'impossible' happened)
  (GHC version 6.12.3 for i386-unknown-linux):
        Evaluated the place holder for a PostTcType

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

On Thu, May 19, 2011 at 17:06, Ranjit Jhala <jhala at cs.ucsd.edu> wrote:
>  dsExpr :: HsExpr Id -> DsM CoreExpr
> which "desugars" the source-level HsExpr into the CoreExpr.

Again, pretty close to what I need. The Monad is a little unexpected.

I wrote a simple function:

hsExpr2TypeDo :: HsExpr Id -> DsM Type
hsExpr2TypeDo hsexpr = do
  let ioCoreE = dsExpr hsexpr
  x <- ioCoreE
  return (exprType x)

(One could write that a little shorter, but that's not the point)

My problem was, that I eventually need the Type from the DsMonad.
Obviously, I cant just get the value out of a monad (that's against
the idea of monads).

So I was looking for a function that transforms: DsM a -> IO a.

I found:
initDs :: HscEnv -> Module -> GlobalRdrEnv -> TypeEnv -> DsM a -> IO
(Messages, Maybe a)

1) For the HscEnv I use the result of getSession
2) Module is the result of ms_mod (getModSummary m))

However for the other parameters, I use empty values:

3) emptyGlobalRdrEnv
4) emptyTypeEnv

This actually works (at least for the simple cases I have tested).
But it seems wrong to me to pass two empty environments.
Can I somehow obtain valid environments?

Another question: Why is dsExpr monadic?
What side effects could there possibly be?

Thanks for all the answers, I'm sorry that I answer so late,
I wanted to try everything out before responding.


More information about the Glasgow-haskell-users mailing list