Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#5676 closed feature request (fixed)

Typeclass instance function type declarations

Reported by: drb226 Owned by: simonpj
Priority: normal Milestone: 7.6.1
Component: Compiler (Type checker) Version:
Keywords: typeclass instance function type declaration Cc: fuzxxl@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Test Case: typecheck/should_compile/T5676
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


See discussion at

Basically, it would be nice to be able to do something like this:

instance Functor Maybe where
  fmap :: (a -> b) -> Maybe a -> Maybe b
  fmap = undefined

This currently produces a "misplaced type signature" error.

It seems like a simple enough extension (we could call it SuperfluousTypeclassSignatures, because "Superfluous" is a cool word). It might be a good idea for Haskell Prime, but let's test it out as a Glasgow extension first.

We (a few of us on #haskell irc right now) think it best that only the precisely correct type signatures be accepted (reject signatures that are too lose or too strong), and it would be nice if, given an incorrect type signature, a custom error message were displayed stating the correct type signature.

Change History (9)

comment:1 Changed 6 years ago by FUZxxl

Cc: fuzxxl@… added
Component: CompilerCompiler (Type checker)

Please implement this! This feature is great for new users and educative purposes as it allows to visualize the types of instance functions.

comment:2 Changed 6 years ago by simonpj

See also #5674, which proposes the same feature.

comment:3 Changed 6 years ago by simonpj

I've wanted this myself, so I've done it in a spare moment. I need to validate etc before pushing, and it's unclear whether it should be in 7.4, since we are in feature freeze.


comment:4 Changed 6 years ago by simonmar

Milestone: 7.6.1
Owner: set to simonpj

comment:5 Changed 6 years ago by simonpj

Simon, Ian: Actually, I have a validated patch now. Can I commit it? Or would it be better to set it aside until we've got 7.4 out?


comment:6 Changed 6 years ago by simonpj@…

commit 6353ae0ff672dfcf79fd9bca1d58ff6ec7e7f9e0

Author: Simon Peyton Jones <>
Date:   Mon Dec 12 00:32:06 2011 +0000

    Allow type signatures in instance decls (Trac #5676)
    This new feature-ette, enable with -XInstanceSigs, lets
    you give a type signature in an instance declaration:
       instance Eq Int where
         (==) :: Int -> Int -> Bool
         (==) = ...blah...
    Scoped type variables work too.

 compiler/main/DynFlags.hs         |    4 ++-
 compiler/rename/RnBinds.lhs       |    8 +++---
 compiler/rename/RnSource.lhs      |   38 ++++++++++++++++----------
 compiler/typecheck/TcBinds.lhs    |    9 +++---
 compiler/typecheck/TcInstDcls.lhs |   52 ++++++++++++++++++++++++++++++++----
 compiler/typecheck/TcSMonad.lhs   |    4 +-
 docs/users_guide/glasgow_exts.xml |   33 +++++++++++++++++++++++
 7 files changed, 115 insertions(+), 33 deletions(-)

comment:7 Changed 6 years ago by simonpj

difficulty: Unknown
Resolution: fixed
Status: newclosed
Test Case: typecheck/should_compile/T5676

OK I implemented this. Documentation in the user guide.


comment:8 Changed 6 years ago by simonpj

PS: too late for 7.4, so no merge, but it'll be in 7.6

comment:9 Changed 6 years ago by mgsloan1

Glad to see that there's a fix for this!

I ran into this recently. For now I will place the implementation elsewhere, but I wanted to mention an additional reason for this to be possible - ScopedTypeVariables. You can't get type variables in scope, to reference in where declarations unless you allow type signatures in instances.

Note: See TracTickets for help on using tickets.