I ran across something I find curious while playing around with the Haskell interactive prompt (ghci). The following code run under ghci 7.0.4
[minBound..1]
throws the following exception:
<interactive>:1:12:
Ambiguous type variable `t0' in the constraints:
(Num t0) arising from the literal `1' at <interactive>:1:12
(Enum t0) arising from the arithmetic sequence `minBound .. 1'
at <interactive>:1:1-13
(Bounded t0) arising from a use of `minBound'
at <interactive>:1:2-9
Probable fix: add a type signature that fixes these type variable(s)
In the expression: 1
In the expression: [minBound .. 1]
In an equation for `it': it = [minBound .. 1]
I know that writing the above as [minBound..1 :: Int] would make it clear that ‘1’ here is meant to be an Int, but my question is, where does the ambiguity lie? ‘1’ could be interpreted as Int, Integer, Float or Double, but none of these except Int belong to the Bounded class. So is there another class that literal 1 could masquerade as? If not, then what?
Per the defaulting rules, a constrained type variable is tried to be resolved by defaulting, if
C a;adoesn’t appear as an argument to a type constructor in the constraint, andThe inferred type of the expression
[minBound .. 1]isso the defaulting rules apply. But for defaulting, only types listed in the default declaration of the module are considered – in the absence of a default declaration, the default default of
(Integer, Double)is assumed, i.e. to resolve a constrained ambiguous type variable, firstIntegeris tried, and if that doesn’t satisfy all constraints,Doubleis tried. If that doesn’t satisfy all constraints either, defaulting fails and the compilation fails with anambiguous type variableerror¹.In the case at hand, neither
IntegernorDoublesatisfy theBoundedconstraint, so defaulting fails.¹ In ghci, or with the
ExtendedDefaultRulesextension enabled, defaulting is also attempted if no numeric class is among the constraints butShowis, and the default default is extended by().