<div dir="ltr"><div><div><div><div>Simon:<br><br></div>I would like to keep the constraints as much as possible. So if x :: C a => a then [x] :: C a => [a]. Also in the case of patterns if I have f 5 = 5, then the pattern 5 should have type Num a => a, but currently the type is stored without the Num context (using hsPatType). I don't to imitate the complete type checking process, but is there a way to retrieve these context information as well?<br><br></div>Robin:<br><br></div>The current solution uses the feature you mentioned. This is needed in order to extract the incompletely typed AST from the compiler. The tool then would perform some transformations on the syntax tree to correct the type errors, but for that it must be able to type subexpressions as well. The -fdefer-type-errors flag doesn't help to solve this problem, it only enables us to extract the AST.<br><br></div>Any advice on typing expressions using API functions and retrieving context information would help a great deal.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 5, 2018 at 5:25 PM, Robin Palotai <span dir="ltr"><<a href="mailto:palotai.robin@gmail.com" target="_blank">palotai.robin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I wondered if <a href="https://downloads.haskell.org/~ghc/7.8.1/docs/html/users_guide/defer-type-errors.html" target="_blank">https://downloads.haskell.org/<wbr>~ghc/7.8.1/docs/html/users_<wbr>guide/defer-type-errors.html</a> could help.<br><br></div>I tried to click around in the GHC 8.2 tree of <a href="http://stuff.codereview.me/ghc/#ghc/compiler/typecheck/TcErrors.hs?corpus=ghc-8.2.1-rc2&signature&line=120" target="_blank">http://stuff.codereview.me/<wbr>ghc/#ghc/compiler/typecheck/<wbr>TcErrors.hs?corpus=ghc-8.2.1-<wbr>rc2&signature&line=120</a>, but it seems deferring type errors just reports via a different means, and doesn't change the type-checking behavior. But correct me if I'm wrong.<br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">2018-03-05 17:11 GMT+01:00 Simon Peyton Jones via ghc-devs <span dir="ltr"><<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a>></span>:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">





<div link="blue" vlink="purple" lang="EN-GB">
<div class="m_-2977770364423459230m_272964852394899002WordSection1">
<p class="MsoNormal"><span style="font-size:12.0pt">Always cc ghc-devs!   Bottle-necking on me may well yield a slow response!   Or even Haskell-café.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt">What is the type of (\x -> [x,y])?   Where y is in scope with type y::a.   Presumably something like   a -> [a]?  Or is it forall a. a -> [a]?  And would your answer change if you had just (\x -> [x,x])?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt">Generalisation is tricky, and for terms with non-closed types it is hard to know what you need in your use-case.  A type like ‘a’ might be a very fine answer!<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt">A lot depends on precisely what you are trying to do.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt">Simon<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt"><u></u> <u></u></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> Peter Podlovics [mailto:<a href="mailto:peter.d.podlovics@gmail.com" target="_blank">peter.d.podlovics@gmai<wbr>l.com</a>]
<br>
<b>Sent:</b> 05 March 2018 14:54<br>
<b>To:</b> Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>><br>
<b>Subject:</b> Re: Type checking expressions<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:12.0pt;margin-left:0cm">
My main concern with that approach is that it might not give the correct type. For example the hsPatType function only gives unconstrained types, so it is incorrect for any numeric literal, since it gives "a" instead of "Num a => a".
<br>
<br>
So the question is whether it is possible to retrieve the context of the type variables as well. Also this problem may arise in the case of expressions as well, that is why I scrapped that approach and tried to type check the AST with the TcM monad directly,
 but without any success.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:12.0pt;margin-left:0cm">
Could you give me any leads on how to solve this problem?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
Thanks in advance,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
Peter<u></u><u></u></p>
</div>
</div><div><div class="m_-2977770364423459230h5">
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
<u></u> <u></u></p>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
On Mon, Mar 5, 2018 at 9:44 AM, Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">Peter</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:36.0pt">
 <u></u><u></u></p>
<p class="MsoNormal" style="margin-left:36.0pt">
My goal is to determine the type of every expression, pattern etc. in the<br>
syntax tree<span style="font-size:12.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">After type checking is complete, the syntax tree is liberally annotated with types.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">We do not yet have a function</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">            hsExprType :: HsExpr Id -> Type</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">but we do have</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">            TcHsTyn.hsPatType :: Pat GhcTc -> Type</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">and you or someone could readily make an equivalent for HsExpr.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">Most type errors are reported by adding an error constraint, but still returning an annotated tree.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">Some, I’m afraid, are still done in the old way, by throwing an exception – so you don’t get back an annotated tree in that case.  But they are relatively
 rare.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">Others must have wanted something like this…</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt">Simon</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><u></u><u></u></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> ghc-devs [mailto:<a href="mailto:ghc-devs-bounces@haskell.org" target="_blank">ghc-devs-bounces@haske<wbr>ll.org</a>]
<b>On Behalf Of </b>Peter Podlovics<br>
<b>Sent:</b> 02 March 2018 12:05<br>
<b>To:</b> <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
<b>Subject:</b> Fwd: Type checking expressions</span><u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Hello everyone,<br>
<br>
I would like to ask for some advice regarding the type checker part of GHC.<br>
My goal is to determine the type of every expression, pattern etc. in the<br>
syntax tree. Currently the compiler doesn't store this information, so I have<br>
to type check manually. One important aspect is that the program may be ill-typed,<br>
but I still want to extract as much information as possible.<br>
<br>
I tried using local type checking functions (eg.: tcInferSigma), but whenever<br>
I used it on an expression that had some "out-of-scope" names in it, it failed.<br>
<br>
> f xs = length xs<br>
<br>
The reason was that xs was not in the local environment.<br>
<br>
My question is: how could I provide the necessary local environment for these<br>
type checking functions? Also in the general case, is it possible to somehow<br>
annotate each expression with its type during the type checking?<br>
<br>
The motivation for this is that I want to implement a tool that automatically<br>
corrects ill-typed programs based heuristics. For that I need to know the types<br>
of certain AST nodes.<span style="color:#888888"><br>
<br>
<span class="m_-2977770364423459230m_272964852394899002m-7452985673952537534hoenzb">Peter Podlovics</span></span><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:6.0pt"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div></div></div>
</div>
</div>

<br></div></div>______________________________<wbr>_________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/ghc-devs</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>