[Haskell-cafe] Ambiguous types
Dušan Kolář
kolar at fit.vut.cz
Wed Aug 7 09:52:35 UTC 2019
Dear Café,
I'm trying to solve a couple of examples and exercises just for me. I've
come to the point, when I'm trying working code manipulating lists rewrite to work
on Foldable (etc.).
Nevertheless, I must be doing some mistake, overlooking something, as
simple code like this:
chkDup [] = False
chkDup (0:ns) = chkDup ns
chkDup (n:ns) = elem n ns || chkDup ns
which works fine, type-checks, can be used within other code, I'm trying to replace
with more generic, but probably less efficient and wrong code:
chkDup ns = fst $ foldr f (False,mempty) ns
where
f _ res@(True,_) = res
f v res@(_,vs) = if v==0 then (False, vs) else (elem v vs, pure v <> vs)
which does not even type-check.
Nevertheless, the error message is not too helpful, searching the Internet just
confirms it's wrong and that adding AllowAmbiguousTypes would not work. The
error message is:
helper.hs:49:1: error:
• Could not deduce (Foldable f0)
from the context: (Eq a, Num a, Foldable t, Foldable f,
Applicative f, Monoid (f a))
bound by the inferred type for ‘chkDup’:
forall a (t :: * -> *) (f :: * -> *).
(Eq a, Num a, Foldable t, Foldable f, Applicative f,
Monoid (f a)) =>
t a -> Bool
at helper.hs:(49,1)-(53,80)
The type variable ‘f0’ is ambiguous
• In the ambiguity check for the inferred type for ‘chkDup’
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
When checking the inferred type
chkDup :: forall a (t :: * -> *) (f :: * -> *).
(Eq a, Num a, Foldable t, Foldable f, Applicative f,
Monoid (f a)) =>
t a -> Bool
|
49 | chkDup ns =
| ^^^^^^^^^^^...
So is there a nicer and working way, how to change my simple example? Is there a
way, how to make it compile? Or is it useless to do that, original function is just
fine...
Best regards,
Dušan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190807/531a078a/attachment.html>
More information about the Haskell-Cafe
mailing list