
Hello everyone,
On Thu, May 19, 2011 at 09:17, Simon Peyton-Jones
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
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. Thanks, Sven

Dear Sven,
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?
(disclaimer: I am _very_ new to this!) You may be able to extract those fields from the "ModGuts"obtained by compiling the file that you are traversing.
Another question: Why is dsExpr monadic? What side effects could there possibly be?
The desugaring process (of which dsExpr is a part) creates a bunch of distinct ("fresh") names. To do so it needs to tap into the Unique supply (see newUniqueId.) In addition, the DsM tracks some environments, source locations (getSrcSpanDs, putSrcSpanDs) etc... Ranjit.

| > 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. Simon
participants (3)
-
Ranjit Jhala
-
Simon Peyton-Jones
-
Sven Urbanski