Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#13506 closed bug (fixed)

Spurious extra error message due to functional dependencies

Reported by: gelisam Owned by:
Priority: normal Milestone:
Component: Compiler (Type checker) Version: 8.0.2
Keywords: FunDeps Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect error/warning at compile-time Test Case: typecheck/should_fail/T13506
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

One call site is ill-typed, but GHC reports a type error at every call site, not just the problematic one. In the original code, the problem occurred with a very common function (Data.Lens.view), so the avalanche of error messages made it difficult to find the problematic call site.

{-# LANGUAGE FlexibleInstances, FunctionalDependencies, MultiParamTypeClasses #-}
module Bug where

class FunDep lista a | lista -> a
instance FunDep [a] a

singleton :: FunDep lista a => a -> lista
singleton _ = undefined

-- this error is expected:
--   Couldn't match type 'Char' with '()'
--   arising from a functional dependency between
--     constraint 'FunDep [Char] ()' arising from a use of 'singleton'
--     instance 'FunDep [a] a'
illTyped :: [Char]
illTyped = singleton ()

-- but this one is not:
--   Couldn't match type '()' with 'Char'
--     arising from a functional dependency between constraints:
--       'FunDep [Char] Char' arising from a use of 'singleton' (in 'wellTyped')
--       'FunDep [Char] ()' arising from a use of 'singleton' (in 'illTyped')
wellTyped :: [Char]
wellTyped = singleton 'a'

The spurious error disappears if illTyped is commented out or moved after wellTyped.

Change History (4)

comment:1 Changed 2 years ago by RyanGlScott

This error message has had an interesting history. In GHC 7.4.2 and earlier, you get the error message that you desire:

GHCi, version 7.4.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Bug              ( Bug.hs, interpreted )

Bug.hs:16:12:
    Couldn't match type `Char' with `()'
    When using functional dependencies to combine
      FunDep [a] a,
        arising from the dependency `lista -> a'
        in the instance declaration at Bug.hs:5:10
      FunDep [Char] (),
        arising from a use of `singleton' at Bug.hs:16:12-20
    In the expression: singleton ()
    In an equation for `illTyped': illTyped = singleton ()
Failed, modules loaded: none.

In GHC 7.6.3, it changed to the current behavior:

GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Bug              ( Bug.hs, interpreted )

Bug.hs:16:12:
    Couldn't match type `Char' with `()'
    When using functional dependencies to combine
      FunDep [a] a,
        arising from the dependency `lista -> a'
        in the instance declaration at Bug.hs:5:10
      FunDep [Char] (),
        arising from a use of `singleton' at Bug.hs:16:12-20
    In the expression: singleton ()
    In an equation for `illTyped': illTyped = singleton ()

Bug.hs:24:13:
    Couldn't match type `()' with `Char'
    When using functional dependencies to combine
      FunDep [Char] (),
        arising from a use of `singleton' at Bug.hs:16:12-20
      FunDep [Char] Char,
        arising from a use of `singleton' at Bug.hs:24:13-21
    In the expression: singleton 'a'
    In an equation for `wellTyped': wellTyped = singleton 'a'

But in GHC 7.8.4, it was briefly fixed again!

GHCi, version 7.8.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Bug              ( Bug.hs, interpreted )

Bug.hs:16:12:
    No instance for (FunDep [Char] ())
      arising from a use of ‘singleton’
    In the expression: singleton ()
    In an equation for ‘illTyped’: illTyped = singleton ()

But in GHC 7.10.3 and later, we get the current error message again.

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

In 48daaaf/ghc:

Don't report fundep wanted/wanted errors

This makes GHC drop derived FunDep errors when they
are come from wanted/wanted interactions.  Much along
the lines of "don't rewrite wanteds with wanteds".

See TcRnTypes Note [Dropping derived constraints]
and the new code in isDroppableDerivedLoc.

Fixes Trac #13506.

comment:3 Changed 2 years ago by simonpj

Resolution: fixed
Status: newclosed
Test Case: typecheck/should_fail/T13506

Excellent test case, thank you. Fixed!

Simon

comment:4 Changed 2 years ago by simonpj

Keywords: FunDeps added
Note: See TracTickets for help on using tickets.