The problem is that type nil is equal to Parser [a] . So parse nil xs is of type Either ParseError [a] . Right [] usually of type Either l [a] ; comparing it with parse nil xs makes l be a ParseError , but the type in the list is still not completely bounded. Without any context, it remains completely polymorphic; that a not necessarily a member of a class of type Eq , and even if there is no way to find out which instance will be used to implement == , and therefore it is not valid to call == in these two terms.
In a realistic program, you are likely to be saved from this by the fact that you are using the result for something that would make such a specific event compatible with what you are using it for. This will probably be the specific type that the Eq implementation has.
When you talk about loading a module, I assume you mean the GHCI interpreter. GHCI adds some additional default rules. In particular, he will tend to use variables of type unconstrined type (which are not a type of top-level function) to () , so he does not have to complain about ambiguous type variables so often.
An interactive session in GHCi tends to encounter an ambiguous type variable much more often than realistic modules compiled in its entirety, since it should make small fragments mostly independently. GHCi has expanded the default rules to make them work much more often (although often it only delays the error until the next link when the user expects a different type, and the difference between GHCi and GHC often causes confusion).
Test fragments may experience a similar problem. If you are testing polymorphic functions, you often do not limit some of the types sufficient to deduce the type to work, as in real purposeful use of the function. But without the GHCi’s extended default rules, this problem manifests itself as an actual ambiguous type error when posting the problem, rather than masking it by arbitrary type selection.
To fix this, you just need to add a type annotation to fix the list type. Either declare the full type parse nil xs , or Right [] , just declare the type of the empty list literal on the right side. Sometihng how this should do the trick:
prop_nil :: [Char] -> Bool prop_nil xs = parse nil xs == Right ([] :: [Int])
source share