How to turn LHExpr GhcPs into CoreExpr

Richard Eisenberg rae at
Wed Jan 22 09:36:29 UTC 2020

You'll need to run the expression through the whole pipeline.

1. Parsing
2. Renaming
3. Type-checking
  3a. Constraint generation
  3b. Constraint solving
  3c. Zonking
4. Desugaring

I don't have the exact calls you should use for these steps, but I can give you some pointers.

1. parseExpression
2. rnLExpr
3a. tcInferSigma
3b. simplifyInfer
3c. zonkTopLExpr
4. dsLExpr

You may want to examine tcRnExpr for a template on how to do steps 2 and 3. Note that this function drops the expression and continues only with the type, but the setup around constraint solving is likely what you want.

Another place to look for inspiration is in the pipeline that GHCi uses to process user-written expressions. hscParsedStmt and its caller, hscStmtWithLocation may also be helpful. These functions also compile the statement (which you don't want to do), but you can see where the desugared statement comes out.

I hope this helps move you in the right direction!

> On Jan 21, 2020, at 7:21 PM, Yiyun Liu <liuyiyun at> wrote:
> Hi ghc-devs,
> I've been trying to implementing a function with the signature:
> elaborateExpr :: GhcMonad m => String -> m CoreExpr
> It should take a string of the form:
> "\x y -> x <> y :: Semigroup a => a -> a -> a"
> and give back a core expression with the dictionaries filled in:
> \ ($dSem :: Semigroup a) (x :: a) (y :: a) -> (<>) $dSem x y
> The goal is to use the function to add elaboration support for liquidhaskell.
> I looked into the implementation of exprType <> and defined my own elaborateExpr <> similarly by calling desugarExpr <> on the expression (which has type LHsExpr GhcTcId) returned by tcInferSigma.
> GhcTcId is a synonym of GhcTc so the program I wrote typechecks, but it's definitely not right. The elaborateExpr function I defined would return something even when the expression doesn't typecheck, or occasionally give a runtime exception:
> ghc-elaboration-test: panic! (the 'impossible' happened)
>   (GHC version 8.6.5 for x86_64-unknown-linux):
>     dsEvBinds
> I must have broken some invariants somehow.
> What is the correct way of defining such a function (takes a string and returns a CoreExpr)? It appears to me that I should convert LHsExpr GhcPs into LHsExpr GhcTc first before calling deSugarExpr, but I don't know how.
> Thank you,
> - Yiyun
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the ghc-devs mailing list