<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">On 19 October 2016 at 17:00, Simon Peyton Jones <a href="http://mailto:simonpj@microsoft.com">simonpj@microsoft.com</a> wrote:</p>
<blockquote style="margin:1.2em 0px;border-left:4px solid rgb(221,221,221);padding:0px 1em;color:rgb(119,119,119);quotes:none">
<p style="margin:0px 0px 1.2em!important">I’m afraid I didn’t understand the issue in the link below.  It<br>speaks of “querying the type”, but I’m not sure what that means.  A<br>GHCi session perhaps?  Does this relate to the way GHCi displays<br>types?</p>
<p style="margin:0px 0px 1.2em!important">I’m a bit lost.  A from-the-beginning example, showing steps and<br>what the unexpected behaviour is would be helpful (to me anyway)</p>
</blockquote>
<p style="margin:0px 0px 1.2em!important">Sure. I’ll explain from top-level down:</p>
<ul style="margin:1.2em 0px;padding-left:2em">
<li style="margin:0.5em 0px"><p style="margin:0px 0px 1.2em!important;margin:0.5em 0px!important">In this case “querying the type” means running the <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">:type-at</code><br>command in Intero (which itself is a fork of GHCi’s codebase around<br>GHC 7.10):<br><a href="https://github.com/commercialhaskell/intero/blob/master/src/InteractiveUI.hs#L1693-L1713">https://github.com/commercialhaskell/intero/blob/master/src/InteractiveUI.hs#L1693-L1713</a><br>It accepts a file name, line-col to line-col span and prints the<br>type of that expression/pattern. As you can see in that function it<br>uses <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">printForUserModInfo</code> (from <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">GhcMonad</code>), similar to (scroll above)<br>the <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">printForUser</code> for GHCi’s regular <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">:type</code> command.</p>
</li>
<li style="margin:0.5em 0px"><p style="margin:0px 0px 1.2em!important;margin:0.5em 0px!important">Where does that info come from? When we load a module in Intero, we<br>perform an additional step of “collecting info” here:<br><a href="https://github.com/commercialhaskell/intero/blob/master/src/GhciInfo.hs#L73">https://github.com/commercialhaskell/intero/blob/master/src/GhciInfo.hs#L73</a><br>That info, for each node in the AST, is ultimately stored in a<br><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">SpanInfo</code>:<br><a href="https://github.com/commercialhaskell/intero/blob/master/src/GhciTypes.hs#L28-L39">https://github.com/commercialhaskell/intero/blob/master/src/GhciTypes.hs#L28-L39</a><br>Which we then use for <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">:type-at</code>.</p>
</li>
</ul>
<p style="margin:0px 0px 1.2em!important">So in summary we collect info from <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">tm_typechecked_source</code>, keep that<br>for later, and then when the user’s editor asks via e.g. <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">:type-at
X.hs 1 5 1 7</code> “what is the type of the thing at this point?” we use<br>GHC’s regular pretty printing function to print that type.</p>
<p style="margin:0px 0px 1.2em!important">That actually all works splendidly. For example, if we query</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-haskell" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248)"><span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">foo</span> g f = maybe g f
<span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">--  ^ here or   ^ here yields g :: b</span>
<span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">foo</span> g f = maybe g f
<span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">--    ^ here or   ^ here yields: f :: a -> b</span>
</code></pre>
<p style="margin:0px 0px 1.2em!important">The tricky part arises in this example:<br><a href="https://github.com/commercialhaskell/intero/issues/280#issuecomment-254784904">https://github.com/commercialhaskell/intero/issues/280#issuecomment-254784904</a></p>
<p style="margin:0px 0px 1.2em!important">Which is that we have two perfectly cromulent types from the AST that<br>are both <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">a</code> in isolation, but are actually different. They will have<br>different Unique values in their Name’s and come from different<br>implicit <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">forall</code>‘s. The question is what’s a good way to communicate<br>this to the user?</p>
<p style="margin:0px 0px 1.2em!important">This is partly a “user interface” question, and on the side a “given<br>an ideal UI, do we have the necessary info the GHC API?”</p>
<p style="margin:0px 0px 1.2em!important">If it helps, I could probably spend some time making an isolated<br>module that uses the GHC API to compile a file and then report these<br>types.</p>
<p style="margin:0px 0px 1.2em!important">Ciao!</p>
<div title="MDH:T24gMTkgT2N0b2JlciAyMDE2IGF0IDE3OjAwLCBTaW1vbiBQZXl0b24gSm9uZXMgJmx0O3NpbW9u
cGpAbWljcm9zb2Z0LmNvbSZndDsgd3JvdGU6PGJyPjxicj4mZ3Q7IEnigJltIGFmcmFpZCBJIGRp
ZG7igJl0IHVuZGVyc3RhbmQgdGhlIGlzc3VlIGluIHRoZSBsaW5rIGJlbG93LiDCoEl0PGJyPiZn
dDtzcGVha3Mgb2Yg4oCccXVlcnlpbmcgdGhlIHR5cGXigJ0sIGJ1dCBJ4oCZbSBub3Qgc3VyZSB3
aGF0IHRoYXQgbWVhbnMuIMKgQTxicj4mZ3Q7R0hDaSBzZXNzaW9uIHBlcmhhcHM/IMKgRG9lcyB0
aGlzIHJlbGF0ZSB0byB0aGUgd2F5IEdIQ2kgZGlzcGxheXM8YnI+Jmd0O3R5cGVzPzxicj4mZ3Q7
PGJyPiZndDsgSeKAmW0gYSBiaXQgbG9zdC4gwqBBIGZyb20tdGhlLWJlZ2lubmluZyBleGFtcGxl
LCBzaG93aW5nIHN0ZXBzIGFuZDxicj4mZ3Q7IHdoYXQgdGhlIHVuZXhwZWN0ZWQgYmVoYXZpb3Vy
IGlzIHdvdWxkIGJlIGhlbHBmdWwgKHRvIG1lIGFueXdheSk8YnI+PGJyPlN1cmUuIEknbGwgZXhw
bGFpbiBmcm9tIHRvcC1sZXZlbCBkb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