Opened 21 months ago

Closed 5 months ago

#9479 closed feature request (fixed)

Report required constraints when reporting the type of a hole

Reported by: dominiquedevriese Owned by:
Priority: normal Milestone: 8.0.1
Component: Compiler (Type checker) Version: 7.8.3
Keywords: holes Cc: ekmett
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

For the following code

module Test where

test :: String
test = show _h

GHC currently reports:

Found hole ‘_h’ with type: a0
Where: ‘a0’ is an ambiguous type variable
Relevant bindings include
  test :: String (bound at /tmp/Test.hs:4:1)
In the first argument of ‘show’, namely ‘_h’
In the expression: show _h
In an equation for ‘test’: test = show _h

It correctly does not report the lack of a Show _a instance as a separate type error. However, it would be useful if the report containing the type of the hole would also contain the constraints that apply to its type. Something like:

Found hole ‘_h’ with type: a0
Where: ‘a0’ is an ambiguous type variable
Applicable constraints: Show a0
Relevant bindings include
  test :: String (bound at /tmp/Test.hs:4:1)
In the first argument of ‘show’, namely ‘_h’
In the expression: show _h
In an equation for ‘test’: test = show _h

I am explicitly *not* suggesting to report a type like Show a0 => a0 for the hole, because that might mistakenly suggest that we are looking for a value of type forall a0. Show a0 => a0, which we are not. A possible alternative is to use an imaginary exists type like exists a0. Show a0 => a0 but that's probably just even more confusing.

Change History (9)

comment:1 Changed 21 months ago by dominiquedevriese

comment:2 Changed 21 months ago by mentheta

How can I watch this ticket for activity? I am interested in future development related to this feature.

I'm sorry if this "meta question" is out of place, but I couldn't find a "watch button" either on this page, or for trac in general.

comment:3 Changed 21 months ago by goldfire

Open up the "Modify Ticket" option near the comment box and add your email to the "Cc" field.

comment:4 Changed 21 months ago by simonpj

I think this would be fairly easy to implement, and I talked to someone at ICFP who plans to do so. Happy to offer guidance.

Simon

comment:5 Changed 17 months ago by phadej

With -fdefer-type-errors GHC (both 7.8 and 7.10) tries a bit longer and finds the missing instance:

[1 of 1] Compiling Test             ( /Users/ogre/show.hs, /Users/ogre/show.o )

/Users/ogre/show.hs:4:8: Warning:
    No instance for (Show a0) arising from a use of ‘show’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
      instance Show Ordering -- Defined in ‘GHC.Show’
      instance Show Integer -- Defined in ‘GHC.Show’
      ...plus 22 others
    In the expression: show _h
    In an equation for ‘test’: test = show _h

/Users/ogre/show.hs:4:13: Warning:
    Found hole ‘_h’
      with type: a0
    Where: ‘a0’ is an ambiguous type variable
    Relevant bindings include
      test :: String (bound at /Users/ogre/show.hs:4:1)
    In the first argument of ‘show’, namely ‘_h’
    In the expression: show _h
    In an equation for ‘test’: test = show _h

So should we make holeReporter to try to find Dicts errors too, if -fdefer-type-errors is not set? IMHO the No instance error is what we need here.

comment:6 Changed 15 months ago by ekmett

  • Cc ekmett added

comment:7 Changed 7 months ago by dfeuer

  • Milestone set to 8.0.1
  • Priority changed from low to normal

comment:8 Changed 5 months ago by thomie

This is fixed in HEAD.

$ ghc-7.11.20151213 Test.hs
[1 of 1] Compiling Test             ( Test.hs, Test.o )

Test.hs:4:8: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘show’
      prevents the constraint ‘(Show a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Show Module -- Defined in ‘GHC.Show’
        instance Show Ordering -- Defined in ‘GHC.Show’
        instance Show TrName -- Defined in ‘GHC.Show’
        ...plus 25 others
        (use -fprint-potential-instances to see them all)
    • In the expression: show _h
      In an equation for ‘test’: test = show _h

Test.hs:4:13: error:
    • Found hole: _h :: a0
      Where: ‘a0’ is an ambiguous type variable
      Or perhaps ‘_h’ is mis-spelled, or not in scope
    • In the first argument of ‘show’, namely ‘_h’
      In the expression: show _h
      In an equation for ‘test’: test = show _h
    • Relevant bindings include test :: String (bound at Test.hs:4:1)

comment:9 Changed 5 months ago by thomie

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.