[Haskell-cafe] Ambiguous types

Dušan Kolář kolar at fit.vut.cz
Wed Aug 7 12:38:12 UTC 2019

Thanks all for the answers, and no, they are not what I was asking for...

Not providing type signatures, yes, no type signatures provided, GHC should derive 
them on its own. My question was, why it is not able to do that and how force GHC 
to do that. Without extra type info...

The original type signature was simple: chkDup :: (Eq a, Num a) => [a] -> Bool
I would be quite happy to have something similar, just for [] to have some generic 
type - auto-magically :-)

And no, my intention was not to make it a bit faster for larger inputs, just make 
clear and clean code without extra type signatures, easy to read and simple to 
understand. Real-life input has 36 six members at most.

And no, I don't want the general code to be specific for lists, thus no extra type 
signatures or changes to make it more list of Ints.

So thank you once again, unfortunately, my conclusion is that writing generic code 
is not always beneficial - neither for reading nor for generality...

Thank you once again,


On středa 7. srpna 2019 13:21:27 CEST Juan Casanova wrote:
> PS: I didn't even spend much time thinking how I would probably
> implement chkDup differently even for the generic case that you want
> to do. As in, intuition tells me you don't really need an applicative
> monoid internally to do what you're trying to do. But, as I said,
> haven't thought it thoroughly, and with the code I gave you, you get
> what you were trying to do and it's not "wrong". Maybe just overkill.
> Quoting Juan Casanova <juan.casanova at ed.ac.uk> on Wed, 07 Aug 2019
> 12:17:27 +0100:
> > To build up on Paolino's answer, and maybe, if you're like me, give
> > you a better intuition as to why your code was incorrect, but
> > leading to essentially the same answer he gave.
> > 
> > First, you didn't provide us with the type signatures for chkDup and
> > f, but I worked with the following ones to start with:
> > 
> > chkDup :: forall a (t :: * -> *) (f :: * -> *). (Eq a, Num a,
> > Foldable t, Foldable f, Applicative f, Monoid (f a)) => t a -> Bool
> > 
> > f :: (Num a, Foldable t, Semigroup (t a), Applicative t, Eq a) => a
> > -> (Bool, t a) -> (Bool, t a)
> > 
> > This gives me the same error you get even if I define chkDup _ =
> > undefined.
> > 
> > The problem is that you are providing class constraints for the type
> > f, which does not appear at all in the signature of the function (t
> > a -> Bool), and this makes the typechecker get super confused. Now,
> > I know why you thought this was necessary: precisely because of what
> > Paolino pointed out: mempty has an undefined type, and you tried to
> > fix this by saying "This mempty is of type f, which I don't care for
> > the type, just that it is a Semigroup and Applicative". But this is
> > a mistake. So, if you just remove the f altogether from the class
> > constraints of the chkDup type signature, and leave it like this:
> > 
> > chkDup :: forall a (t :: * -> *) (f :: * -> *). (Eq a, Num a,
> > Foldable t) => t a -> Bool
> > 
> > You still get an error, but now you get the actual error message
> > that you're looking for: "Could not deduce Foldable t0 arising from
> > a use of f".
> > 
> > The right way to fix this is how Paolino said: Specify what Foldable
> > you're going to use (recommended: list) (so, use [] instead of
> > mempty). The point is, since the type f is not part of the input NOR
> > the output of the function chkDup, and instead it is just an
> > internal tool that you use to compute chkDup, there's no reason to
> > want to be generic about it, there's no advantage in that. The
> > advantage in genericity using typeclasses is: 1. If it's in the
> > input, you allow the user of the function to provide you with any
> > type that fulfills the class. 2. You make it very clear that
> > everything you do with that parameter is related to its typeclass
> > instance, as there is no way to inspect the actual type. 3. If it's
> > in the output, you don't let users of the function make any
> > assumption of what that type might look like, other than its
> > typeclass instance.
> > 
> > But in an internal tool that doesn't come from outside and isn't
> > output by your function, there's no reason to be generic, just use
> > lists. The only reason I could see being argued for wanting to be
> > generic would be if you'd like the user to provide you with hints as
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190807/e85c1f38/attachment.html>

More information about the Haskell-Cafe mailing list