How to turn LHExpr GhcPs into CoreExpr

Yiyun Liu liuyiyun at terpmail.umd.edu
Tue Jan 21 19:21:38 UTC 2020


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 
<https://hackage.haskell.org/package/ghc-8.6.5/docs/CoreUtils.html#v:exprType> 
and defined my own elaborateExpr 
<https://github.com/yiyunliu/ghc-elaboration-test/blob/30b3307469df15789cf1a323bed96ca42e84aeb5/src/Main.hs#L58> 
similarly by calling desugarExpr 
<https://hackage.haskell.org/package/ghc-8.6.5/docs/Desugar.html#v: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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20200121/376dedbc/attachment.html>


More information about the ghc-devs mailing list