Type ambiguity with numbers
haskell, types
Solution
Per the defaulting rules, a constrained type variable is tried to be resolved by defaulting, if
- all constraints have the form `C a`; `a` doesn't appear as an argument to a type constructor in the constraint, and
- at least one of the classes involved is a numeric class,and
- all of the classes are defined in the Prelude or the standard libraries.
The inferred type of the expression `[minBound .. 1]` is
[minBound .. 1] :: (Num a, Enum a, Bounded a) => [a]
so 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, first `Integer` is tried, and if that doesn't satisfy all constraints, `Double` is tried. If that doesn't satisfy all constraints either, defaulting fails and the compilation fails with an `ambiguous type variable` error¹.
In the case at hand, neither `Integer` nor `Double` satisfy the `Bounded` constraint, so defaulting fails.
¹ In ghci, or with the `ExtendedDefaultRules` extension enabled, defaulting is also attempted if no numeric class is among the constraints but `Show` is, and the default default is extended by `()`.
Problem
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?