Skip to content

Ambiguous types and rejected type signatures

This ticket collects a bunch of examples where GHC

  • Rejects a function with a type signature but, if the type signature is removed, accepts the function and infers precisely the type that was originally specified.
  • Accepts type signatures that are utterly ambiguous; that is, the function could never be called, or only in the presence of bizarre instance declarations.
  • Rejects definitions that clearly have a unique typing.

Original example: the following programm does not compile:

{-# LANGUAGE TypeFamilies #-}

import Control.Monad
import Data.Maybe

class Bug s where
  type Depend s 
 
  next  :: s -> Depend s -> Maybe s
  start :: s
  
isValid :: (Bug s) => [Depend s] -> Bool
isValid ds = isJust $ foldM next start ds

Error:

GHCi, version 6.9.20071105: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( GHC-Bug.hs, interpreted )
Bug.hs:13:39:
    Couldn't match expected type `Depend a'
           against inferred type `Depend s'
      Expected type: [Depend a]
      Inferred type: [Depend s]
    In the third argument of `foldM', namely `ds'
    In the second argument of `($)', namely `foldM next start ds'
Failed, modules loaded: none.

When one elides the type signature the program compiles yielding the offending signature:

Prelude> :r
[1 of 1] Compiling Main             ( GHC-Bug.hs, interpreted )
Ok, modules loaded: Main.
*Main> :t isValid
isValid :: (Bug a) => [Depend a] -> Bool

Compiler version: 6.9.20071105

Best regards,

Harald Holtmann (haskell@zeuxis.de)

Edited by Simon Peyton Jones
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information