Opened 9 years ago

Closed 6 years ago

#2360 closed feature request (fixed)

More information in occurs check message

Reported by: ajd Owned by: chak
Priority: low Milestone: 7.4.1
Component: Compiler Version: 6.8.2
Keywords: Cc: claus, rwbarton
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

$ cat A.hs
module A
  where

foo :: [a] -> a
foo = id
$ ghc -c A.hs

A.hs:5:0:
    Occurs check: cannot construct the infinite type: a = [a]
    When generalising the type(s) for `foo'

It would be nice if GHC could give more information about where and how the error is being generated, for example which expressions are making it try to unify a and [a]. (Especially in more complicated functions, it can be difficult to track down the bug.)

Change History (13)

comment:1 Changed 9 years ago by claus

Cc: claus added

Perhaps the issue isn't specific to occurs check messages? See my comments on #1928.

Even in the trivial example given here, it would help to have the types for foo's rhs and foo's lhs/signature in the message (a -> a vs [a] -> a), to provide context for the type error.

comment:2 Changed 9 years ago by igloo

difficulty: Unknown
Milestone: 6.10 branch

comment:3 Changed 9 years ago by simonmar

Architecture: UnknownUnknown/Multiple

comment:4 Changed 9 years ago by simonmar

Operating System: UnknownUnknown/Multiple

comment:5 Changed 9 years ago by rwbarton

Cc: rwbarton added

Here are some more interesting cases. I'm specifically interested in whether the error message pinpoints the location of the error; I call those error messages good. My tests are under ghc-6.11.20090107.

Case 1: A good error message when there's no type signature

Prelude> let m x = x : x in m

<interactive>:1:14:
    Occurs check: cannot construct the infinite type: a = [a]
      Expected type: [a]
      Inferred type: a
    In the second argument of `(:)', namely `x'
    In the expression: x : x

Case 2: A bad error message when a polymorphic type signature is given

Prelude> let m :: a -> a; m x = x : x in m

<interactive>:1:17:
    Occurs check: cannot construct the infinite type: a = [a]
    When generalising the type(s) for `m'
    In the expression:
        let
          m :: a -> a
          m x = x : x
        in m
    In the definition of `it':
        it = let
               m :: a -> a
               m x = x : x
             in m

Case 3: A good error when a monomorphic type signature is given

Prelude> let m :: Int -> Int; m x = x : x in m

<interactive>:1:31:
    Couldn't match expected type `[Int]' against inferred type `Int'
    In the second argument of `(:)', namely `x'
    In the expression: x : x
    In the definition of `m': m x = x : x

The difference between cases 2 and 3 surprises me because I could mechanically turn case 2 into case 3 by substituting a fresh type constructor for each universally quantified type variable.

comment:6 Changed 9 years ago by simonpj

Milestone: 6.10 branch6.12 branch
Owner: set to chak

Quite right. We can definitely improve this. Older versions of GHC did better:

T2360.hs:4:10:
    Couldn't match expected type `[a]'
	   against inferred type `a' (a rigid variable)
      `a' is bound by the type signature for `m' at T2360.hs:3:5

Note to Manuel: the equality a ~ [a] is deferred, and then bleated about in TcTyFuns.checkOrientation. The latter is giving the wrong error. Let's fix this when the new solver is in place.

Simon

comment:7 Changed 8 years ago by chak

See also #3169

comment:8 Changed 7 years ago by igloo

Milestone: 6.12 branch6.12.3

comment:9 Changed 7 years ago by igloo

Milestone: 6.12.36.14.1
Priority: normallow

comment:10 Changed 7 years ago by igloo

Milestone: 7.0.17.0.2

comment:11 Changed 7 years ago by igloo

Milestone: 7.0.27.2.1

comment:12 Changed 6 years ago by igloo

Milestone: 7.2.17.4.1

comment:13 Changed 6 years ago by chak

Resolution: fixed
Status: newclosed
Type of failure: None/Unknown

The error message for this program is now back to

A.hs:5:7:
    Couldn't match type `a' with `[a]'
      `a' is a rigid type variable bound by
          the type signature for foo :: [a] -> a at A.hs:5:1
    Expected type: [a] -> a
      Actual type: [a] -> [a]
    In the expression: id
    In an equation for `foo': foo = id

This seems to implement simonpj's suggestion; so, I am closing this ticket.

Note: See TracTickets for help on using tickets.