Reputation: 2967
Consider the below:
class Test m a where
t :: Int -> m a
instance Test [] Int where
t i = [i]
instance Test Maybe Int where
t i | i == 0 = Nothing
| otherwise = Just i
main = do
print $ t (22 :: Int) --Error!
This will throw with the following error:
Ambiguous type variables ‘m0’, ‘a0’ arising from a use of ‘print’
prevents the constraint ‘(Show (m0 a0))’ from being solved.
This is due to the fact the compiler has no way of identifying what instance of m a
to use. How can I explicitly state this?
Upvotes: 1
Views: 58
Reputation: 116139
Annotate the full call to t
:
print (t 22 :: Maybe Int)
or annotate t
itself
print $ (t :: Int -> Maybe Int) 22
As a more advanced alternative, with the proper extensions on, one can pass the type level arguments explicitly
print $ t @Maybe @Int 22
depending on the class at hand, this may save you to type a very long annotation.
Upvotes: 8