Opened 7 years ago

Closed 3 years ago

#4921 closed feature request (fixed)

report ambiguous type variables more consistently

Reported by: Saizan Owned by:
Priority: low Milestone: 7.10.1
Component: Compiler (Type checker) Version: 7.0.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: typecheck/should_fail/T4921
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

{-# LANGUAGE MultiParamTypeClasses #-}                                                                                                                 
module Amb where                                                                                                                                       
                                                                                                                                                       
class C a b where                                                                                                                                      
    f :: (a,b)                                                                                                                                         
                                                                                                                                                       
instance C Int Char where                                                                                                                              
    f = undefined                                                                                                                                      
                                                                                                                                                       
{-                                                                                                                                                     
x = fst f                                                                                                                                              
/home/saizan/snippets/Amb.hs:7:8:                                                                                                                      
    Ambiguous type variables `a', `b' in the constraint:                                                                                               
      `C a b'                                                                                                                                          
        arising from a use of `f' at /home/saizan/snippets/Amb.hs:7:8                                                                                  
    Possible cause: the monomorphism restriction applied to the following:                                                                             
      x :: a (bound at /home/saizan/snippets/Amb.hs:7:0)                                                                                               
    Probable fix: give these definition(s) an explicit type signature                                                                                  
                  or use -XNoMonomorphismRestriction                                                                                                   
Failed, modules loaded: none.                                                                                                                          
-}                                                                                                                                                     
                                                                                                                                                       
{-                                                                                                                                                     
y = fst f :: Int                                                                                                                                       
                                                                                                                                                       
/home/saizan/snippets/Amb.hs:21:8:                                                                                                                     
    No instance for (C Int b)                                                                                                                          
      arising from a use of `f' at /home/saizan/snippets/Amb.hs:21:8                                                                                   
    Possible fix: add an instance declaration for (C Int b)                                                                                            
    In the first argument of `fst', namely `f'                                                                                                         
    In the expression: fst f :: Int                                                                                                                    
    In the definition of `y': y = fst f :: Int                                                                                                         
Failed, modules loaded: none.                                                                                                                          
-}

Both x and y have the same problem, there isn't enough type information to let the typechecker decide on an instance, so it seems they should produce similar error messages.

In particular, the error for y is quite confusing since it can be reasonably interpreted as saying there's no type b for which there's an instance C Int b, which in fact is not true, so i think explicitly mentioning the ambiguity like in the first message would help many to understand the problem better.

I can see though that an "instance C Int b" could make sense, more often than C a b, so maybe "Possible fix: add an instance declaration for (C Int b)" should be conserved, even if it still has the problem of expressing that the second argument needs to be a variable.

Change History (7)

comment:1 Changed 7 years ago by igloo

Component: CompilerCompiler (Type checker)
Milestone: 7.2.1

Thanks for the report.

comment:2 Changed 6 years ago by igloo

Milestone: 7.4.17.6.1
Priority: normallow

comment:3 Changed 5 years ago by igloo

Milestone: 7.6.17.6.2

comment:4 Changed 3 years ago by thoughtpolice

Milestone: 7.6.27.10.1

Moving to 7.10.1.

comment:5 Changed 3 years ago by thomie

difficulty: Unknown

This seems fixed, pending a regression test. The error messages for x and y are no longer completely different.

Ghc shows for y, since version 7.6.3: The type variable ‘b0’ is ambiguous. And since version 7.8.3 it doesn't show anymore: Possible fix: add an instance declaration for (C Int b).

Here is the full error message with HEAD:

$ ghc-7.9.20141125 test.hs
[1 of 1] Compiling Amb              ( test.hs, test.o )

test.hs:10:9:
    No instance for (C a0 b1) arising from a use of ‘f’
    The type variables ‘a0’, ‘b1’ are ambiguous
    Relevant bindings include x :: a0 (bound at test.hs:10:1)
    Note: there is a potential instance available:
      instance C Int Char -- Defined at test.hs:7:10
    In the first argument of ‘fst’, namely ‘f’
    In the expression: fst f
    In an equation for ‘x’: x = fst f

test.hs:11:9:
    No instance for (C Int b0) arising from a use of ‘f’
    The type variable ‘b0’ is ambiguous
    Note: there is a potential instance available:
      instance C Int Char -- Defined at test.hs:7:10
    In the first argument of ‘fst’, namely ‘f’
    In the expression: fst f :: Int
    In an equation for ‘y’: y = fst f :: Int

comment:6 Changed 3 years ago by Simon Peyton Jones <simonpj@…>

comment:7 Changed 3 years ago by simonpj

Resolution: fixed
Status: newclosed
Test Case: typecheck/should_fail/T4921
Note: See TracTickets for help on using tickets.