Opened 8 years ago

Closed 6 years ago

#3592 closed bug (fixed)

Eta-contraction gives a rather bogus type error message

Reported by: guest Owned by: simonpj
Priority: normal Milestone: 7.4.1
Component: Compiler (Type checker) Version: 6.10.3
Keywords: Cc: thomas@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: typecheck/should_fail/T359
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Consider the following program:

module Main where

type T a = Show a => a

f :: T a -> String
f = show

Compiling this with GHC 6.10.3 and -fglasgow-exts gives:

[1 of 1] Compiling Main             ( Test.hs, interpreted )
ghc: panic! (the 'impossible' happened)
  (GHC version 6.10.3 for i386-apple-darwin):
	TcTyFuns.flattenType: unexpected PredType

Change History (7)

comment:1 Changed 8 years ago by simonpj

difficulty: Unknown
Summary: ghc: panic! on overloaded type synonymEta-contraction gives a rather bogus type error message

With the HEAD (and I think 6.12) we get

T3592.hs:7:4:
    Could not deduce (Show (T a)) from the context ()
      arising from a use of `show' at T3592.hs:7:4-7
    Possible fix:
      add (Show (T a)) to the context of the type signature for `f'
      or add an instance declaration for (Show (T a))
    In the expression: show
    In the definition of `f': f = show

which at least is not a crash. So that has already been fixed.

Your type signature means this

f :: forall a. (Show a => a) -> String

If we eta-expand the definition of f to

f x = show x

then we get the totally reasonable error message:

T3592.hs:7:11:
    Could not deduce (Show a) from the context ()
      arising from a use of `x' at T3592.hs:7:11
    Possible fix:
      add (Show a) to the context of the type signature for `f'
    In the first argument of `show', namely `x'
    In the expression: show x
    In the definition of `f': f x = show x

I think we should get the same error from the eta-contracted version, so I'm going to re-title this bug report, and put it in my list for the type inference overhaul.

Simon

comment:2 Changed 8 years ago by igloo

Milestone: 6.14.1

comment:3 Changed 7 years ago by igloo

Milestone: 7.0.17.0.2

comment:4 Changed 7 years ago by igloo

Milestone: 7.0.27.2.1

comment:5 Changed 6 years ago by igloo

Architecture: x86Unknown/Multiple
Component: CompilerCompiler (Type checker)
Milestone: 7.2.17.4.1
Operating System: MacOS XUnknown/Multiple
Owner: set to simonpj
Type of failure: None/Unknown

Simon, I guess the type inference overhaul has happened now, so might be worth having another look at this?

comment:6 Changed 6 years ago by simonpj

For this module

{-# LANGUAGE RankNTypes #-}
module T3592 where

type T a = Show a => a

f :: T a -> String
f = show

g :: T a -> String
g x = show x

We now get

T3592.hs:7:5:                           -- Eta contracted version
    No instance for (Show (T a))
      arising from a use of `show'
    Possible fix: add an instance declaration for (Show (T a))
    In the expression: show
    In an equation for `f': f = show

T3592.hs:10:12:                          -- Not eta contracted
    No instance for (Show a)
      arising from a use of `x'
    In the first argument of `show', namely `x'
    In the expression: show x
    In an equation for `g': g x = show x

So the error messages are slightly different, because of the implicit instantiation of 'x' when it is explicit, but I think both are reasonable. I think I'll declare this one done.

I'll add a test.

Simon

comment:7 Changed 6 years ago by simonpj

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