Skip to content

too eager constraint simplification(?) for nullary type classes without existing instance

Summary

Type inference involving nullary classes without any instance is too eager.

Steps to reproduce

Consider the following interaction with ghci:

ghci> :set -XMultiParamTypeClasses 
ghci> class Foo where foo :: Int
ghci> :t foo -- expected: foo :: Foo => Int

<interactive>:1:1: error:
    • No instance for Foo arising from a use of ‘foo’
    • In the expression: foo
ghci> bar () = foo -- expected to work without type signature

<interactive>:4:10: error:
    • No instance for Foo arising from a use of ‘foo’
    • In the expression: foo
      In an equation for ‘bar’: bar () = foo
ghci> bar :: Foo => () -> Int; bar () = foo

or the following module which fails to compile:

{-# LANGUAGE MultiParamTypeClasses #-}

class Foo where foo :: Int

-- bar :: Foo => () -> Int
bar () = foo

{-
[1 of 2] Compiling Main             ( Foo.hs, Foo.o )

Foo.hs:6:10: error:
    • No instance for Foo arising from a use of ‘foo’
    • In the expression: foo
      In an equation for ‘bar’: bar () = foo
  |
6 | bar () = foo
  |          ^^^
-}

These examples start working once an instance for Foo is provided.

Expected behavior

I expected GHC to infer types of the shape Foo => ... rather than producing type errors.

Environment

  • GHC version used: 9.4.2
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information