<div dir="ltr"><div><div>What happens if type 'a' is both an instance EnumTag and StrTag at the same time? Then which instance does it choose? You may know that can't happen in your code, but there's no guarantee that someone using your library or that some import won't bring such a type into scope.<br><br></div>Because of this ambiguity, during type checking haskell ignores class contexts and merely looks at the instance head (Read a), and says hey there are two instances 'Read a', they are overlapping.<br><br></div>As to what to do about it, I'm not sure. But I don't think I would be trying to get different read instances based on whatever typeclasses happen to be in scope for that type.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 1, 2017 at 6:57 AM, Baa <span dir="ltr"><<a href="mailto:aquagnu@gmail.com" target="_blank">aquagnu@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello, List!<br>
<br>
I got error:<br>
<br>
Duplicate instance declarations:<br>
instance [overlap ok] EnumTag a => Read a<br>
-- Defined at /XXX/intero/intero2932Xpa-<wbr>TEMP.hs:110:27<br>
instance [overlap ok] StrTag a => Read a<br>
-- Defined at /XXX/intero/intero2932Xpa-<wbr>TEMP.hs:121:27 (intero)<br>
<br>
For this code:<br>
<br>
class (Show a, Enum a) => EnumTag a where<br>
anyEnum :: a<br>
<br>
instance {-# OVERLAPS #-} EnumTag a => Read a where<br>
readPrec = RP.lift P.skipSpaces >> expectEnum<br>
instance {-# OVERLAPS #-} EnumTag a => Eq a where<br>
a == b | a == anyEnum || b == anyEnum = True<br>
| otherwise = fromEnum a == fromEnum b<br>
<br>
class StrTag a where<br>
anyStr :: a<br>
tagPrefix :: a -> String -- ^ should be constant<br>
toStr :: String -> a<br>
<br>
instance {-# OVERLAPS #-} StrTag a => Read a where<br>
readPrec = parens $ do<br>
RP.lift P.skipSpaces<br>
(RP.lift $ expectShown anyStr) <++ RP.lift g<br>
where g = do<br>
Just s@(_:_) <- L.stripPrefix tagPrefix <$> expectTag<br>
return $ toStr s<br>
<br>
Why does it happen? `Read a` in 1st instance is valid only when a is<br>
`EnumTag`, in 2nd one - is valid only when a is `StrTag`.<br>
<br>
How can I fix this error and to create "default" instances for `EnumTag`<br>
and to `StrTag`, so client code will "inherit" those functionality<br>
(`Read`) simple, only with instantiation of `EnumTag` or `StrTag` ?<br>
<br>
Sure, if I comment `instance ... StrTag a` then all work fine, but I need 2 specialized `Read`s (and `Eq`s too :)<br>
<br>
===<br>
Best regards, Paul<br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>