<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 12 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Verdana;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.hoenzb
        {mso-style-name:hoenzb;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Verdana","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal" style="margin-left:36.0pt">Oh! Is the definition of 'foo' rejected in recent versions of GHC? My 7.4.1 installation doesn't complain.&nbsp;&nbsp; -- Conal<o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;;color:#1F497D">Yes, it is rejected.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;;color:#1F497D"><br>
Simon<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></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 #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">From:</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"> conal.elliott@gmail.com [mailto:conal.elliott@gmail.com]
<b>On Behalf Of </b>Conal Elliott<br>
<b>Sent:</b> 14 January 2013 20:52<br>
<b>To:</b> Simon Peyton-Jones<br>
<b>Cc:</b> Richard Eisenberg; glasgow-haskell-users@haskell.org; Haskell Cafe<br>
<b>Subject:</b> Re: Advice on type families and non-injectivity?<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal">There is a real difficulty here with type-checking 'bar'. &nbsp;(And that difficulty is why 'foo' is also rejected.)<o:p></o:p></p>
</blockquote>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Oh! Is the definition of 'foo' rejected in recent versions of GHC? My 7.4.1 installation doesn't complain.&nbsp;&nbsp; -- Conal<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><o:p>&nbsp;</o:p></p>
<div>
<p class="MsoNormal">On Mon, Jan 14, 2013 at 3:39 AM, Simon Peyton-Jones &lt;<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>&gt; wrote:<o:p></o:p></p>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">| &gt; &gt; {-# LANGUAGE TypeFamilies #-}<br>
| &gt; &gt;<br>
| &gt; &gt; type family F a<br>
| &gt; &gt;<br>
| &gt; &gt; foo :: F a<br>
| &gt; &gt; foo = undefined<br>
| &gt; &gt;<br>
| &gt; &gt; bar :: F a<br>
| &gt; &gt; bar = foo<o:p></o:p></p>
</div>
<p class="MsoNormal">There is a real difficulty here with type-checking 'bar'. &nbsp;(And that difficulty is why 'foo' is also rejected.)<br>
<br>
Namely, when typechecking 'bar', we must instantiate foo with an unknown type, say alpha. &nbsp;So now we must find a type 'alpha' such that<br>
&nbsp; &nbsp; &nbsp; &nbsp; F a ~ F alpha<br>
Certainly alpha=1 is one solution, but there might be others. &nbsp;For example, suppose<br>
&nbsp; &nbsp; &nbsp; &nbsp; type instance F [b] = F b<br>
Then alpha=[a] would also be a solution.<br>
<br>
In this particular case any solution will do, but suppose there was an addition constraint (C alpha) arising from the right hand side, where C is a class. &nbsp;Then if we had<br>
&nbsp; &nbsp; &nbsp; &nbsp; instance C [b] where ...<br>
then the second solution (alpha=[a]) would work, but not the first. &nbsp;This can get arbitrarily complicated, and GHC's type inference does not &quot;search&quot; various solutions; it follows one path.<br>
<br>
The solution is to provide a way to fix alpha. For example,<br>
&nbsp; &nbsp; &nbsp; &nbsp; foo :: a -&gt; F a<br>
is fine.<br>
<span style="color:#888888"><br>
<span class="hoenzb">Simon</span></span><o:p></o:p></p>
<div>
<div>
<p class="MsoNormal"><br>
<br>
| -----Original Message-----<br>
| From: <a href="mailto:glasgow-haskell-users-bounces@haskell.org">glasgow-haskell-users-bounces@haskell.org</a> [mailto:<a href="mailto:glasgow-haskell-">glasgow-haskell-</a><br>
| <a href="mailto:users-bounces@haskell.org">users-bounces@haskell.org</a>] On Behalf Of Richard Eisenberg<br>
| Sent: 14 January 2013 03:47<br>
| To: Conal Elliott<br>
| Cc: <a href="mailto:glasgow-haskell-users@haskell.org">glasgow-haskell-users@haskell.org</a>; Haskell Cafe<br>
| Subject: Re: Advice on type families and non-injectivity?<br>
|<br>
| Hi Conal,<br>
|<br>
| I agree that your initial example is a little puzzling, and I'm glad<br>
| that the new ambiguity checker prevents both definitions, not just one.<br>
|<br>
| However, your initial question seems broader than just this example. I<br>
| have run into this problem (wanting injective type functions) several<br>
| times myself, and have been successful at finding workarounds. But, I<br>
| can't think of any unifying principle or solid advice. If you can post<br>
| more information about your problem, perhaps I or others can contribute.<br>
|<br>
| For what it's worth, the desire for injective type functions has been<br>
| entered as ticket #6018 in the GHC Trac, but I see you're already on the<br>
| cc: list. I believe Simon PJ has given serious thought to implementing<br>
| this feature and may have even put in some very basic code toward this<br>
| end.<br>
|<br>
| Richard<br>
|<br>
| On Jan 13, 2013, at 2:10 PM, Conal Elliott &lt;<a href="mailto:conal@conal.net">conal@conal.net</a>&gt; wrote:<br>
|<br>
| &gt; I sometimes run into trouble with lack of injectivity for type<br>
| families. I'm trying to understand what's at the heart of these<br>
| difficulties and whether I can avoid them. Also, whether some of the<br>
| obstacles could be overcome with simple improvements to GHC.<br>
| &gt;<br>
| &gt; Here's a simple example:<br>
| &gt;<br>
| &gt; &gt; {-# LANGUAGE TypeFamilies #-}<br>
| &gt; &gt;<br>
| &gt; &gt; type family F a<br>
| &gt; &gt;<br>
| &gt; &gt; foo :: F a<br>
| &gt; &gt; foo = undefined<br>
| &gt; &gt;<br>
| &gt; &gt; bar :: F a<br>
| &gt; &gt; bar = foo<br>
| &gt;<br>
| &gt; The error message:<br>
| &gt;<br>
| &gt; &nbsp; &nbsp; Couldn't match type `F a' with `F a1'<br>
| &gt; &nbsp; &nbsp; NB: `F' is a type function, and may not be injective<br>
| &gt; &nbsp; &nbsp; In the expression: foo<br>
| &gt; &nbsp; &nbsp; In an equation for `bar': bar = foo<br>
| &gt;<br>
| &gt; A terser (but perhaps subtler) example producing the same error:<br>
| &gt;<br>
| &gt; &gt; baz :: F a<br>
| &gt; &gt; baz = baz<br>
| &gt;<br>
| &gt; Replacing `a` with a monotype (e.g., `Bool`) eliminates the error.<br>
| &gt;<br>
| &gt; Does the difficulty here have to do with trying to *infer* the type<br>
| and then compare with the given one? Or is there an issue even with type<br>
| *checking* in such cases?<br>
| &gt;<br>
| &gt; Other insights welcome, as well as suggested work-arounds.<br>
| &gt;<br>
| &gt; I know about (injective) data families but don't want to lose the<br>
| convenience of type synonym families.<br>
| &gt;<br>
| &gt; Thanks, &nbsp;-- Conal<br>
| &gt;<br>
| &gt; _______________________________________________<br>
| &gt; Glasgow-haskell-users mailing list<br>
| &gt; <a href="mailto:Glasgow-haskell-users@haskell.org">Glasgow-haskell-users@haskell.org</a><br>
| &gt; <a href="http://www.haskell.org/mailman/listinfo/glasgow-haskell-users" target="_blank">
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users</a><br>
|<br>
|<br>
| _______________________________________________<br>
| Glasgow-haskell-users mailing list<br>
| <a href="mailto:Glasgow-haskell-users@haskell.org">Glasgow-haskell-users@haskell.org</a><br>
| <a href="http://www.haskell.org/mailman/listinfo/glasgow-haskell-users" target="_blank">
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users</a><o:p></o:p></p>
</div>
</div>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</div>
</div>
</body>
</html>