Commit ba2e2014 authored by Simon Peyton Jones's avatar Simon Peyton Jones
Browse files

Do type-class defaulting even if there are insoluble constraints

The argument in Trac #9033 is very compelling: we should not report 20
errors, fix one, and have the other 19 disappear.  They were spurious
in the first place.

The fix was easy; do type-class defaulting uncondionally, rather than
only if there are no insoluble constraints.

See Note [When to do type-class defaulting] in TcSimplify.

Error messages generally improve, especially tc211 which actually
had an example of precisely this phenomenon.
parent 0960a378
......@@ -95,10 +95,9 @@ simpl_top wanteds
try_class_defaulting :: WantedConstraints -> TcS WantedConstraints
try_class_defaulting wc
| isEmptyWC wc || insolubleWC wc
= return wc -- Don't do type-class defaulting if there are insolubles
-- Doing so is not going to solve the insolubles
| otherwise
| isEmptyWC wc
= return wc
| otherwise -- See Note [When to do type-class defaulting]
= do { something_happened <- applyDefaultingRules (approximateWC wc)
-- See Note [Top-level Defaulting Plan]
; if something_happened
......@@ -107,6 +106,33 @@ simpl_top wanteds
else return wc }
\end{code}
Note [When to do type-class defaulting]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In GHC 7.6 and 7.8.2, we did type-class defaulting only if insolubleWC
was false, on the grounds that defaulting can't help solve insoluble
constraints. But if we *don't* do defaulting we may report a whole
lot of errors that would be solved by defaulting; these errors are
quite spurious because fixing the single insoluble error means that
defaulting happens again, which makes all the other errors go away.
This is jolly confusing: Trac #9033.
So it seems better to always do type-class defaulting.
However, always doing defaulting does mean that we'll do it in
situations like this (Trac #5934):
run :: (forall s. GenST s) -> Int
run = fromInteger 0
We don't unify the return type of fromInteger with the given function
type, because the latter involves foralls. So we're left with
(Num alpha, alpha ~ (forall s. GenST s) -> Int)
Now we do defaulting, get alpha := Integer, and report that we can't
match Integer with (forall s. GenST s) -> Int. That's not totally
stupid, but perhaps a little strange.
Another potential alternative would be to suppress *all* non-insoluble
errors if there are *any* insoluble errors, anywhere, but that seems
too drastic.
Note [Must simplify after defaulting]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We may have a deeply buried constraint
......
T5934.hs:12:7:
Cannot instantiate unification variable ‘a0
with a type involving foralls:
(forall s. Gen (PrimState (ST s))) -> Int
Perhaps you want ImpredicativeTypes
Couldn't match type ‘Integer
with ‘(forall s. Gen (PrimState (ST s))) -> Int’
Expected type: Integer -> (forall s. GenST s) -> Int
Actual type: Integer -> Integer
In the expression: 0
In an equation for ‘run’: run = 0
tc211.hs:15:22:
Couldn't match type ‘forall a6. a6 -> a6’ with ‘a -> a’
Expected type: [a -> a]
Actual type: [forall a. a -> a]
In the first argument of ‘head’, namely ‘foo’
In the first argument of ‘(:) ::
(forall a. a -> a)
-> [forall a. a -> a] -> [forall a. a -> a]’, namely
‘(head foo)’
tc211.hs:48:19:
Could not deduce (Num a2) arising from the literal ‘3’
from the context (Num a)
bound by the inferred type of
h1 :: Num a => (forall a1. a1 -> a1) -> a
at tc211.hs:(47,1)-(49,9)
The type variable ‘a2’ is ambiguous
Relevant bindings include
y :: Pair a2 (Pair a3 b1) (bound at tc211.hs:48:10)
Note: there are several potential instances:
instance Num Double -- Defined in ‘GHC.Float’
instance Num Float -- Defined in ‘GHC.Float’
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus three others
In the first argument of ‘g’, namely ‘3’
In the first argument of ‘P’, namely ‘(g 3)’
In the expression: P (g 3) (g (P 3 4))
tc211.hs:48:28:
Could not deduce (Num a3) arising from the literal ‘3’
from the context (Num a)
bound by the inferred type of
h1 :: Num a => (forall a1. a1 -> a1) -> a
at tc211.hs:(47,1)-(49,9)
The type variable ‘a3’ is ambiguous
Relevant bindings include
y :: Pair a2 (Pair a3 b1) (bound at tc211.hs:48:10)
Note: there are several potential instances:
instance Num Double -- Defined in ‘GHC.Float’
instance Num Float -- Defined in ‘GHC.Float’
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus three others
In the first argument of ‘P’, namely ‘3’
In the first argument of ‘g’, namely ‘(P 3 4)’
In the second argument of ‘P’, namely ‘(g (P 3 4))’
tc211.hs:48:30:
Could not deduce (Num b1) arising from the literal ‘4’
from the context (Num a)
bound by the inferred type of
h1 :: Num a => (forall a1. a1 -> a1) -> a
at tc211.hs:(47,1)-(49,9)
The type variable ‘b1’ is ambiguous
Relevant bindings include
y :: Pair a2 (Pair a3 b1) (bound at tc211.hs:48:10)
Note: there are several potential instances:
instance Num Double -- Defined in ‘GHC.Float’
instance Num Float -- Defined in ‘GHC.Float’
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus three others
In the second argument of ‘P’, namely ‘4’
In the first argument of ‘g’, namely ‘(P 3 4)’
In the second argument of ‘P’, namely ‘(g (P 3 4))’
tc211.hs:70:9:
Couldn't match type ‘forall a7. a7 -> a7’ with ‘a6 -> a6’
Expected type: List (forall a. a -> a)
-> (forall a. a -> a) -> a6 -> a6
Actual type: List (forall a. a -> a)
-> (forall a. a -> a) -> forall a. a -> a
In the expression:
foo2 ::
List (forall a. a -> a) -> (forall a. a -> a) -> (forall a. a -> a)
In the expression:
(foo2 ::
List (forall a. a -> a)
-> (forall a. a -> a) -> (forall a. a -> a))
xs1 (\ x -> x)
tc211.hs:15:22:
Couldn't match type ‘forall a1. a1 -> a1’ with ‘a -> a’
Expected type: [a -> a]
Actual type: [forall a. a -> a]
In the first argument of ‘head’, namely ‘foo’
In the first argument of ‘(:) ::
(forall a. a -> a)
-> [forall a. a -> a] -> [forall a. a -> a]’, namely
‘(head foo)’
tc211.hs:70:9:
Couldn't match type ‘forall a2. a2 -> a2’ with ‘a1 -> a1’
Expected type: List (forall a. a -> a)
-> (forall a. a -> a) -> a1 -> a1
Actual type: List (forall a. a -> a)
-> (forall a. a -> a) -> forall a. a -> a
In the expression:
foo2 ::
List (forall a. a -> a) -> (forall a. a -> a) -> (forall a. a -> a)
In the expression:
(foo2 ::
List (forall a. a -> a)
-> (forall a. a -> a) -> (forall a. a -> a))
xs1 (\ x -> x)
T8603.hs:29:17:
Couldn't match type ‘(->) [a0]’ with ‘[t1]’
Expected type: [t1] -> StateT s RV t0
Actual type: t2 ((->) [a0]) (StateT s RV t0)
Couldn't match type ‘(->) [a0]’ with ‘[Integer]’
Expected type: [Integer] -> StateT s RV t0
Actual type: t1 ((->) [a0]) (StateT s RV t0)
The function ‘lift’ is applied to two arguments,
but its type ‘([a0] -> StateT s RV t0)
-> t2 ((->) [a0]) (StateT s RV t0)’
-> t1 ((->) [a0]) (StateT s RV t0)’
has only one
In a stmt of a 'do' block: prize <- lift uniform [1, 2, 3]
In the expression:
......
module T9030 where
bad :: Bool
bad = ()
square :: Integral i => i -> i
square x = x^2
T9033.hs:4:7:
Couldn't match expected type ‘Bool’ with actual type ‘()’
In the expression: ()
In an equation for ‘bad’: bad = ()
......@@ -331,3 +331,4 @@ test('T8570', extra_clean(['T85570a.o', 'T8570a.hi','T85570b.o', 'T8570b.hi']),
test('T8603', normal, compile_fail, [''])
test('T8806', normal, compile_fail, [''])
test('T8912', normal, compile_fail, [''])
test('T9033', normal, compile_fail, [''])
mc24.hs:10:31:
Couldn't match type ‘[a0]’ with ‘a -> a1’
Expected type: (a -> a1) -> [a] -> t [a]
Actual type: [a0] -> [a0]
Possible cause: ‘take’ is applied to too many arguments
In the expression: take 2
In a stmt of a monad comprehension: then group by x using take 2
mc24.hs:10:31:
Couldn't match type ‘[a0]’ with ‘a -> Integer’
Expected type: (a -> Integer) -> [a] -> t [a]
Actual type: [a0] -> [a0]
Possible cause: ‘take’ is applied to too many arguments
In the expression: take 2
In a stmt of a monad comprehension: then group by x using take 2
tcfail004.hs:3:9:
Couldn't match expected type ‘(t, t3)’
with actual type ‘(t0, t1, t2)’
Relevant bindings include
f :: t (bound at tcfail004.hs:3:2)
g :: t3 (bound at tcfail004.hs:3:4)
In the expression: (1, 2, 3)
In a pattern binding: (f, g) = (1, 2, 3)
tcfail004.hs:3:9:
Couldn't match expected type ‘(t, t1)’
with actual type ‘(Integer, Integer, Integer)’
Relevant bindings include
f :: t (bound at tcfail004.hs:3:2)
g :: t1 (bound at tcfail004.hs:3:4)
In the expression: (1, 2, 3)
In a pattern binding: (f, g) = (1, 2, 3)
tcfail005.hs:3:9:
Couldn't match expected type ‘[t]’ with actual type ‘(t0, Char)’
Relevant bindings include
h :: t (bound at tcfail005.hs:3:2)
i :: [t] (bound at tcfail005.hs:3:4)
In the expression: (1, 'a')
In a pattern binding: (h : i) = (1, 'a')
tcfail005.hs:3:9:
Couldn't match expected type ‘[t]’
with actual type ‘(Integer, Char)’
Relevant bindings include
h :: t (bound at tcfail005.hs:3:2)
i :: [t] (bound at tcfail005.hs:3:4)
In the expression: (1, 'a')
In a pattern binding: (h : i) = (1, 'a')
tcfail140.hs:10:7:
Couldn't match expected type ‘a0 -> t’ with actual type ‘Int’
Relevant bindings include bar :: t (bound at tcfail140.hs:10:1)
The function ‘f’ is applied to two arguments,
but its type ‘Int -> Int’ has only one
In the expression: f 3 9
In an equation for ‘bar’: bar = f 3 9
tcfail140.hs:12:10:
Couldn't match expected type ‘a1 -> t1’ with actual type ‘Int’
Relevant bindings include
rot :: t -> t1 (bound at tcfail140.hs:12:1)
The operator ‘f’ takes two arguments,
but its type ‘Int -> Int’ has only one
In the expression: 3 `f` 4
In an equation for ‘rot’: rot xs = 3 `f` 4
tcfail140.hs:14:15:
Couldn't match expected type ‘a -> b’ with actual type ‘Int’
Relevant bindings include
xs :: [a] (bound at tcfail140.hs:14:5)
bot :: [a] -> [b] (bound at tcfail140.hs:14:1)
The operator ‘f’ takes two arguments,
but its type ‘Int -> Int’ has only one
In the first argument of ‘map’, namely ‘(3 `f`)’
In the expression: map (3 `f`) xs
tcfail140.hs:16:8:
Constructor ‘Just’ should have 1 argument, but has been given none
In the pattern: Just
In the expression: (\ Just x -> x) :: Maybe a -> a
In the expression: ((\ Just x -> x) :: Maybe a -> a) (Just 1)
tcfail140.hs:19:1:
Couldn't match expected type ‘t0 -> Bool’ with actual type ‘Int’
The equation(s) for ‘g’ have two arguments,
but its type ‘Int -> Int’ has only one
tcfail140.hs:10:7:
Couldn't match expected type ‘Integer -> t’ with actual type ‘Int’
Relevant bindings include bar :: t (bound at tcfail140.hs:10:1)
The function ‘f’ is applied to two arguments,
but its type ‘Int -> Int’ has only one
In the expression: f 3 9
In an equation for ‘bar’: bar = f 3 9
tcfail140.hs:12:10:
Couldn't match expected type ‘Integer -> t1’ with actual type ‘Int’
Relevant bindings include
rot :: t -> t1 (bound at tcfail140.hs:12:1)
The operator ‘f’ takes two arguments,
but its type ‘Int -> Int’ has only one
In the expression: 3 `f` 4
In an equation for ‘rot’: rot xs = 3 `f` 4
tcfail140.hs:14:15:
Couldn't match expected type ‘a -> b’ with actual type ‘Int’
Relevant bindings include
xs :: [a] (bound at tcfail140.hs:14:5)
bot :: [a] -> [b] (bound at tcfail140.hs:14:1)
The operator ‘f’ takes two arguments,
but its type ‘Int -> Int’ has only one
In the first argument of ‘map’, namely ‘(3 `f`)’
In the expression: map (3 `f`) xs
tcfail140.hs:16:8:
Constructor ‘Just’ should have 1 argument, but has been given none
In the pattern: Just
In the expression: (\ Just x -> x) :: Maybe a -> a
In the expression: ((\ Just x -> x) :: Maybe a -> a) (Just 1)
tcfail140.hs:19:1:
Couldn't match expected type ‘t0 -> Bool’ with actual type ‘Int’
The equation(s) for ‘g’ have two arguments,
but its type ‘Int -> Int’ has only one
tcfail189.hs:10:31:
Couldn't match type ‘[a0]’ with ‘a -> a1’
Expected type: (a -> a1) -> [a] -> [[a]]
Actual type: [a0] -> [a0]
Possible cause: ‘take’ is applied to too many arguments
In the expression: take 2
In a stmt of a list comprehension: then group by x using take 2
tcfail189.hs:10:31:
Couldn't match type ‘[a0]’ with ‘a -> Integer’
Expected type: (a -> Integer) -> [a] -> [[a]]
Actual type: [a0] -> [a0]
Possible cause: ‘take’ is applied to too many arguments
In the expression: take 2
In a stmt of a list comprehension: then group by x using take 2
......@@ -7,9 +7,9 @@ tcfail206.hs:5:5:
In an equation for ‘a’: a = (, True)
tcfail206.hs:8:5:
Couldn't match type ‘(t0, Int)’ with ‘Bool -> (Int, Bool)’
Couldn't match type ‘(Integer, Int)’ with ‘Bool -> (Int, Bool)’
Expected type: Int -> Bool -> (Int, Bool)
Actual type: Int -> (t0, Int)
Actual type: Int -> (Integer, Int)
In the expression: (1,)
In an equation for ‘b’: b = (1,)
......@@ -32,9 +32,10 @@ tcfail206.hs:14:5:
In an equation for ‘d’: d = (# , True #)
tcfail206.hs:17:5:
Couldn't match type ‘(# a0, Int #)’ with ‘Bool -> (# Int, Bool #)’
Couldn't match type ‘(# Integer, Int #)’
with ‘Bool -> (# Int, Bool #)’
Expected type: Int -> Bool -> (# Int, Bool #)
Actual type: Int -> (# a0, Int #)
Actual type: Int -> (# Integer, Int #)
In the expression: (# 1, #)
In an equation for ‘e’: e = (# 1, #)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment