[Haskell-cafe] Ambiguous types
Paolino
paolo.veronelli at gmail.com
Wed Aug 7 11:05:39 UTC 2019
So about the type error, the second element of the tuple has no defined
type. You can fix it substituting (pure v) with [v] if you want a list
there.
Also
- chkDup ns = any (`elem` ns)
- use Set.member to reduce complexity
Best
On Wed, 7 Aug 2019 at 11:53, Dušan Kolář <kolar at fit.vut.cz> wrote:
> 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
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
--
Paolo Veronelli (paolo.veronelli at gmail.com)
*Functional developer @ global.de <http://global.de/>*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190807/e2c0c4ed/attachment.html>
More information about the Haskell-Cafe
mailing list