<div dir="ltr"><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">There is a real difficulty here with type-checking 'bar'. (And that difficulty is why 'foo' is also rejected.)</blockquote>
<div><br></div><div>Oh! Is the definition of 'foo' rejected in recent versions of GHC? My 7.4.1 installation doesn't complain. -- Conal<br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Mon, Jan 14, 2013 at 3:39 AM, Simon Peyton-Jones <span dir="ltr"><<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">| > > {-# LANGUAGE TypeFamilies #-}<br>
| > ><br>
| > > type family F a<br>
| > ><br>
| > > foo :: F a<br>
| > > foo = undefined<br>
| > ><br>
| > > bar :: F a<br>
| > > bar = foo<br>
<br>
</div>There is a real difficulty here with type-checking 'bar'. (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. So now we must find a type 'alpha' such that<br>
F a ~ F alpha<br>
Certainly alpha=1 is one solution, but there might be others. For example, suppose<br>
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. Then if we had<br>
instance C [b] where ...<br>
then the second solution (alpha=[a]) would work, but not the first. This can get arbitrarily complicated, and GHC's type inference does not "search" various solutions; it follows one path.<br>
<br>
The solution is to provide a way to fix alpha. For example,<br>
foo :: a -> F a<br>
is fine.<br>
<span class="HOEnZb"><font color="#888888"><br>
Simon<br>
</font></span><div class="HOEnZb"><div class="h5"><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 <<a href="mailto:conal@conal.net">conal@conal.net</a>> wrote:<br>
|<br>
| > 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>
| ><br>
| > Here's a simple example:<br>
| ><br>
| > > {-# LANGUAGE TypeFamilies #-}<br>
| > ><br>
| > > type family F a<br>
| > ><br>
| > > foo :: F a<br>
| > > foo = undefined<br>
| > ><br>
| > > bar :: F a<br>
| > > bar = foo<br>
| ><br>
| > The error message:<br>
| ><br>
| > Couldn't match type `F a' with `F a1'<br>
| > NB: `F' is a type function, and may not be injective<br>
| > In the expression: foo<br>
| > In an equation for `bar': bar = foo<br>
| ><br>
| > A terser (but perhaps subtler) example producing the same error:<br>
| ><br>
| > > baz :: F a<br>
| > > baz = baz<br>
| ><br>
| > Replacing `a` with a monotype (e.g., `Bool`) eliminates the error.<br>
| ><br>
| > 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>
| ><br>
| > Other insights welcome, as well as suggested work-arounds.<br>
| ><br>
| > I know about (injective) data families but don't want to lose the<br>
| convenience of type synonym families.<br>
| ><br>
| > Thanks, -- Conal<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><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><br>
</div></div></blockquote></div><br></div>