<div>A few issues, you can remove the overlapping instances by using a newtype wrapper to disambiguate which instance you want.</div><div><br></div><div>A little alarm bell goes off in my head whenever I read &#39;instance Foo a&#39;.</div>
<div><br></div><div>newtype Wrapped a = Wrapped a</div><div><br></div><div>instance Target Foo where ...</div><div><br></div><div>instance Enumerated a =&gt; Target (Wrapped a) where ...</div><div><br></div><div>is probably a better idea over all.</div>
<div><br></div><div>As for the use of constructors. your problem is that it is ambiguous which constructors you want to take the list of.</div><div><br></div><div><span class="Apple-style-span" style="border-collapse: collapse;">Try replacing constructors with a local variable. The choice of constructors then will be fixed by the use of the argument from the Just branch and the monomorphism restriction constraining you so that the list you take the length of has the same type.</span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></div><div><span class="Apple-style-span" style="border-collapse: collapse; ">&gt; instance (Enumerated a) =&gt; Target a where<br>&gt;     convert n | n `elem` [0..len-1] = Just $ cons !! n<br>
&gt;               | otherwise = Nothing<br>&gt;         where </span></div><div><span class="Apple-style-span" style="border-collapse: collapse; ">&gt;            len = length cons</span></div><div><span class="Apple-style-span" style="border-collapse: collapse;">&gt;            cons = constructors      </span></div>
<div><br></div><div>You could also do it with some sort of </div><div><br></div><div>asListOf :: [a] -&gt; a -&gt; [a]</div><div>asListOf = const</div><div><br></div><div>convert n = result where</div><div>    ...</div><div>
    len = length cons </div><div>    ...</div><div>    cons = constructors `asListOf` result</div><div><br></div><div>if relying on the MR makes you feel dirty.</div><div><br></div><div>-Edward</div><div><br></div><div><div>
<div class="gmail_quote">On Thu, Sep 17, 2009 at 9:40 AM, Andy Gimblett <span dir="ltr">&lt;<a href="mailto:haskell@gimbo.org.uk">haskell@gimbo.org.uk</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi all.  This email is in literate Haskell; you should be able to load<br>
it into ghci and verify what I&#39;m saying (nb: it won&#39;t compile without<br>
alteration: see below).<br>
<br>
I&#39;m trying to do something which may anyway be stupid / not the best<br>
approach to what I&#39;m trying to achieve; however, it&#39;s not working and<br>
I can&#39;t see why not.  So I&#39;m asking for help on two fronts:<br>
<br>
1) Why is this failing?<br>
<br>
2) Maybe more usefully, how should I actually be doing this?  It seems<br>
   an ugly approach; a voice in my head is saying &quot;scrap your<br>
   boilerplate&quot;, but I&#39;ve no idea yet if that&#39;s actually applicable<br>
   here; should I look at it?<br>
<br>
On with the show...<br>
<br>
I need these for &quot;subclass&quot; stuff later on...<br>
<br>
&gt; {-# LANGUAGE FlexibleInstances #-}<br>
&gt; {-# LANGUAGE OverlappingInstances #-}<br>
&gt; {-# LANGUAGE UndecidableInstances #-}<br>
<br>
&gt; module Ambig where<br>
<br>
I wish to define a number of algebraic data types with the ability to<br>
turn Int values into instances of those types.  So I define a<br>
typeclass saying this is possible.  I use Maybe so I can encode the<br>
existence of out-of-range Int values, which will vary from target type<br>
to target type.<br>
<br>
&gt; class Target a where<br>
&gt;     convert :: Int -&gt; Maybe a<br>
<br>
E.g. here&#39;s a type Foo which only wants values between 1 and 10:<br>
<br>
&gt; data Foo = Foo Int deriving (Show)<br>
&gt; instance Target Foo where<br>
&gt;     convert n | n `elem` [1..10] = Just $ Foo n<br>
&gt;               | otherwise = Nothing<br>
<br>
(That&#39;s a simple example; some are rather more complex.  How to do<br>
this isn&#39;t what I&#39;m asking about, really.)<br>
<br>
So we have, for example:<br>
<br>
*Ambig&gt; (convert 1) :: Maybe Foo<br>
Just (Foo 1)<br>
*Ambig&gt; (convert 11) :: Maybe Foo<br>
Nothing<br>
<br>
Now, some of those algebraic data type types happen to be<br>
enumerations; in this case, my idea is to list the constructors, with<br>
the rule that each constructor&#39;s position in the list is the Int which<br>
gets converted into that constructor.<br>
<br>
&gt; class Enumerated a where<br>
&gt;     constructors :: [a]<br>
<br>
E.g. here&#39;s a type Bar with three constructors:<br>
<br>
&gt; data Bar = X | Y | Z deriving (Show)<br>
&gt; instance Enumerated Bar where<br>
&gt;     constructors = [X, Y, Z]<br>
<br>
(This is certainly ugly.  Any suggestions?)<br>
<br>
Now we get to the crux.  If a type is an instance of Enumerated, it<br>
should also be a Target, because we should be able to convert from Int<br>
just by list lookup.  But we include a bounds check, naturally...<br>
<br>
&gt; instance (Enumerated a) =&gt; Target a where<br>
&gt;     convert n | n `elem` [0..len-1] = Just $ constructors !! n<br>
&gt;               | otherwise = Nothing<br>
&gt;         where len = length constructors<br>
<br>
So I would _hope_ that then, e.g., we&#39;d have:<br>
<br>
*Ambig&gt; (convert 0) :: Maybe Bar<br>
Just X<br>
*Ambig&gt; (convert 1) :: Maybe Bar<br>
Just Y<br>
*Ambig&gt; (convert 3) :: Maybe Bar<br>
Nothing<br>
<br>
Sadly, this function doesn&#39;t compile, dying with an &quot;Ambiguous type<br>
variable&quot; error:<br>
<br>
Ambig.lhs:75:29:<br>
    Ambiguous type variable `a&#39; in the constraint:<br>
      `Enumerated a&#39;<br>
        arising from a use of `constructors&#39; at Ambig.lhs:74:29-40<br>
    Probable fix: add a type signature that fixes these type variable(s)<br>
<br>
If we replace &quot;length constructors&quot; with &quot;3&quot; (say), it compiles (but<br>
is useless).  Adding a type signature doesn&#39;t help: it&#39;s &quot;misplaced&quot;<br>
in that context.  If I break it out of the instance declaration so I<br>
can add one, I still get the same problem:<br>
<br>
&gt; convert&#39; :: (Enumerated a, Target a) =&gt; Int -&gt; Maybe a<br>
&gt; convert&#39; n | n `elem` [0..len-1] = Just $ constructors !! n<br>
&gt;            | otherwise = Nothing<br>
&gt;     where len = length constructors<br>
<br>
I guess I see roughly what&#39;s going on; the question is &quot;which<br>
constructors instance is meant?&quot;, right?  In the &quot;Just&quot; part it&#39;s OK,<br>
because it can be inferred from the function&#39;s return type (right?).<br>
But in the guard we don&#39;t have that help, so it could be any<br>
Enumerated instance?<br>
<br>
Any advice appreciated!  Particularly if this is just a dumb approach.<br>
For context, this is related to deserialisation of binary data (they&#39;ll<br>
actually be Word8&#39;s, not Int&#39;s) into a variety of data structures.<br>
<br>
Hmmm, maybe I should just be using Data.Binary...<br>
<br>
Many thanks,<br>
<br>
-Andy<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br></div></div>