Skip to content

Spurious extra error message due to functional dependencies

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.

Trac metadata
Trac field Value
Version 8.0.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler (Type checker)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information