<div dir="ltr">Hello Conal,<div><br></div><div style>The issue with your example is that it is ambiguous, so GHC can't figure out how to instantiate the use of `foo`. It might be easier to see why this is if you write it in this form:</div>
<div style><br></div><div style>> foo :: (F a ~ b) => b</div><div style>> foo = ...</div><div style><br></div><div style>Now, we can see that only `b` appears on the RHS of the `=>`, so there is really no way for GHC to figure out what is the intended value for `a`. Replacing `a` with a concrete type (such as `Bool`) eliminates the problem, because now GHC does not need to come up with a value for `a`. Another way to eliminate the ambiguity would be if `F` was injective---then we'd know that `b` uniquely determines `a` so again there would be no ambiguity.</div>
<div style><br></div><div style>If `F` is not injective, however, the only workaround would be to write the type in such a way that the function arguments appear in the signature directly (e.g., something like 'a -> F a' would be ok).</div>
<div style><br></div><div style>-Iavor</div><div style><br></div><div style><br></div><div style><br></div><div style><br></div><div style><br></div><div style><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Sun, Jan 13, 2013 at 11:10 AM, Conal Elliott <span dir="ltr"><<a href="mailto:conal@conal.net" target="_blank">conal@conal.net</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">I sometimes run into trouble with lack of injectivity for type families. I'm trying to understand what's at the heart of these difficulties and whether I can avoid them. Also, whether some of the 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 and then compare with the given one? Or is there an issue even with type *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 convenience of type synonym families.<br><br>Thanks, -- Conal<br><br></div>
<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></blockquote></div><br></div>