Can anybody explain why haskell enforces explicit type signature in the following example and how to modify it to avoid need of the explicit declaration ?
import qualified Data.List as L
main = do
print $ length $ L.nub [1,1,2,3] -- 3, passed (Eq a, Num a) => [a]
print $ length $ L.nub [] -- ambiguous type error, passed (Eq a) => [a]
-- can help type system with explicit signature but how to avoid ?
print $ length $ L.nub ([] :: [Int])
Surprisingly the same code written interactively in ghci has no issue with ambiguity and does prints zero length:
λ> :m +Data.List
λ> print $ length $ nub []
0 -- ?? can you explain ??
Update:
It seems even same restricted as Data.List.nub
length function won't stop complaints about ambiguous type:
length' :: Eq a => [a] -> Int
length' [] = 0
length' (x:xs) = 1 + length' xs
main = do
print $ length' $ nub []
-- No instance for (Eq a0) arising from a use of ‘length'’
-- The type variable ‘a0’ is ambiguous
-XExtendedDefaultRules
) by default, see here, go to section 2.4.8. – n. 1.8e9-where's-my-share m.:show language
I getbase language is: Haskell2010 with the following modifiers: -XNoDatatypeContexts -XNondecreasingIndentation
? – David Unric