<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi ghc-devs,</p>
<p>I've been trying to implementing a function with the signature:</p>
<p>elaborateExpr :: GhcMonad m => String -> m CoreExpr</p>
<p>It should take a string of the form:</p>
<p>"\x y -> x <> y :: Semigroup a => a -> a -> a"</p>
<p>and give back a core expression with the dictionaries filled in:<br>
</p>
<p>\ ($dSem :: Semigroup a) (x :: a) (y :: a) -> (<>) $dSem
x y<br>
</p>
<p><br>
</p>
<p>The goal is to use the function to add elaboration support for
liquidhaskell.</p>
<p>I looked into the implementation of <a moz-do-not-send="true"
href="https://hackage.haskell.org/package/ghc-8.6.5/docs/CoreUtils.html#v:exprType">exprType</a>
and defined my own <a moz-do-not-send="true"
href="https://github.com/yiyunliu/ghc-elaboration-test/blob/30b3307469df15789cf1a323bed96ca42e84aeb5/src/Main.hs#L58">elaborateExpr</a>
similarly by calling <a moz-do-not-send="true"
href="https://hackage.haskell.org/package/ghc-8.6.5/docs/Desugar.html#v:deSugarExpr">desugarExpr</a>
on the expression (which has type LHsExpr GhcTcId) returned by
tcInferSigma.</p>
<p>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:</p>
<p>ghc-elaboration-test: panic! (the 'impossible' happened)<br>
(GHC version 8.6.5 for x86_64-unknown-linux):<br>
dsEvBinds</p>
<p>I must have broken some invariants somehow.</p>
<p>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.</p>
<p>Thank you,</p>
<p>- Yiyun<br>
</p>
</body>
</html>