<div dir="ltr">Hello Simon,<div><br></div><div>I think that the consistent choice would be "(A) like pattern signatures". I think this would ensure that all types in patterns handle variables in the same way. It would be confusing to me if the following two examples ended up doing different things:</div><div><font face="monospace"><br></font></div><div><font face="monospace">h1 (Nothing :: Maybe [p]) = ...</font></div><div><font face="monospace">h2 (Nothing @[p]) = ...</font></div><div><br></div><div>-Iavor</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Nov 13, 2020 at 3:30 AM Simon Peyton Jones via ghc-steering-committee <<a href="mailto:ghc-steering-committee@haskell.org">ghc-steering-committee@haskell.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div lang="EN-GB" style="overflow-wrap: break-word;">
<div class="gmail-m_-259128659515729648WordSection1">
<p class="MsoNormal">Dear Steering Committee<u></u><u></u></p>
<p class="MsoNormal">You may remember that we approved the <a href="https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0126-type-applications-in-patterns.rst" target="_blank">
Type Applications in Patterns</a> proposal, some time ago. Cale has been implementing it (see
<a href="https://gitlab.haskell.org/ghc/ghc/-/issues/11350" target="_blank">ticket 11350</a> and <a href="https://gitlab.haskell.org/ghc/ghc/-/merge_requests/2464" target="_blank">
MR 2464</a>). It’s nearly done.<u></u><u></u></p>
<p class="MsoNormal">But in doing so, one design choice came up that we did not discuss much, and I’d like to consult you.<u></u><u></u></p>
<p class="MsoNormal">Consider first the <i>existing</i> pattern-signature mechanism (not the new feature):<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code">data T a where<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code">MkT :: forall a b. a -> b -> T a<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code"><u></u> <u></u></p>
<p class="gmail-m_-259128659515729648Code">f1 :: forall p. T [p] -> blah<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code">f1 (MkT (x :: a) pi) = blah<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code"><u></u> <u></u></p>
<p class="gmail-m_-259128659515729648Code">f2 :: forall p. T [p] -> blah<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code">f2 (MkT (x :: p) pi) = blah<u></u><u></u></p>
<p class="MsoNormal">In f1, the pattern (x :: a) brings ‘a’ into scope, binding it to the type [p]. But in f2, since p is already in scope, the pattern (x :: p) does not bring anything new into scope. Instead it requires that x have type p, but actually it
has type [p], so f2 is rejected.<u></u><u></u></p>
<p class="MsoNormal">Notice that a pattern signature brings a new variable into scope only if it isn’t already in scope. Notice how this differs from the treatment of term variables; the ‘pi’ in the pattern brings ‘pi’ into scope unconditionally, shadowing
the existing ‘pi’ (from the Prelude).<u></u><u></u></p>
<p class="MsoNormal">OK, now let’s look at the new feature. Consider<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code">g1 :: forall p. T [p] -> blah<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code">g1 (MkT @a x y) = blah<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code"><u></u> <u></u></p>
<p class="gmail-m_-259128659515729648Code">g2 :: forall p. T [p] -> blah<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code">g2 (MkT @p x pi) = blah<u></u><u></u></p>
<p class="gmail-m_-259128659515729648Code"><u></u> <u></u></p>
<p class="MsoNormal"><b>Question</b>: should the variables free in these type arguments be treated like pattern signatures, or like term variables?<u></u><u></u></p>
<ol style="margin-top:0cm" start="1" type="A">
<li class="gmail-m_-259128659515729648MsoListParagraph" style="margin-left:0cm"><b>Like pattern signatures</b>. In this design, in g1, ‘a’ is not in scope, so this brings it into scope, bound to [p]. But in g2, ‘p’ is in scope, so this is rejected (because
MkT is instantiated at [p] not p.<u></u><u></u></li><li class="gmail-m_-259128659515729648MsoListParagraph" style="margin-left:0cm"><b>Like term variables</b>. In this design, all the variables free in type patterns are fresh, and brought into scope. In g2, a new ‘p’ is brought into scope, shadowing the existing
‘p’; indeed the new ‘p’ is bound to [old_p].<u></u><u></u></li></ol>
<p class="MsoNormal">The original paper, and hence the accepted proposal, adopts (A). But Cale likes (B). Indeed John Ericson wrote a
<a href="https://github.com/ghc-proposals/ghc-proposals/pull/291" target="_blank">Proposal 291: simplify scoping for type applications in pattens</a> to advocate this change. (The proposal is not easy to understand, but advocating (B) is its payload.<u></u><u></u></p>
<p class="MsoNormal">This is not a terribly big deal, but it would be good to settle it. <u></u><u></u></p>
<p class="MsoNormal">The right place to have debate is on <a href="https://github.com/ghc-proposals/ghc-proposals/pull/291" target="_blank">
Proposal 291</a>. This email is just to alert you to it, and ask for your opinions.<u></u><u></u></p>
<p class="MsoNormal">Simon<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
_______________________________________________<br>
ghc-steering-committee mailing list<br>
<a href="mailto:ghc-steering-committee@haskell.org" target="_blank">ghc-steering-committee@haskell.org</a><br>
<a href="https://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-steering-committee" rel="noreferrer" target="_blank">https://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-steering-committee</a><br>
</blockquote></div>