Suppose I define a multi-parameter type class:
{-# LANGUAGE MultiParamTypeClasses, AllowAmbiguousTypes, FlexibleContexts, FlexibleInstances #-}
class Table a b c where
decrement :: a -> a
evalutate :: a -> b -> c
Then I define a function that uses decrement
, for simplicity:
d = decrement
When I try to load this in ghci
(version 8.6.3):
• Could not deduce (Table a b0 c0)
arising from a use of ‘decrement’
from the context: Table a b c
bound by the type signature for:
d :: forall a b c. Table a b c => a -> a
at Thing.hs:13:1-28
The type variables ‘b0’, ‘c0’ are ambiguous
Relevant bindings include d :: a -> a (bound at Thing.hs:14:1)
These potential instance exist:
instance Table (DummyTable a b) a b
This is confusing to me because the type of d
is exactly the type of decrement
, which is denoted in the class declaration.
I thought of the following workaround:
data Table a b = Table (a -> b) ((Table a b) -> (Table a b))
But this seems notationally inconvenient, and I also just wanted to know why I was getting this error message in the first place.
Table
would not compile. GHC issued a message sayingAllowAmbiguousTypes
might help. So I see you switched it on. Terrible idea, and GHC should never have mentioned it. Functional Dependencies, per @typedfern's answer is a much better approach. Grr. I am trying to fix GHC to stop making ridiculous suggestions -- esp for newbies. – WellbalancedAllowAmbiguousTypes
is perfectly fine if you are then prepared to useTypeApplications
to disambiguate. I prefer that to using proxies. OTOH, in this specific case, it is not the right solution, and the GHC suggestion is not appropriate. Here, fundeps (or type families) should be used. – IntercontinentalTypeApplications
probably haveAllowAmbiguousTypes
switched on. So the message should be appropriate for people who make a mistake in their method sigs. It should mention all the possible approaches/corrections, not favour an advanced feature (which it doesn't actually name) and keep otherwise silent. This is the type astronauts taking over the hen-house. – Wellbalanced