Ticket #5676 (closed feature request: fixed)

Opened 18 months ago

Last modified 15 months ago

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 Difficulty: Unknown
Test Case: typecheck/should_compile/T5676 Blocked By:
Blocking: Related Tickets:

Description

See discussion at  http://stackoverflow.com/questions/8367426/why-cant-one-put-type-signatures-in-instance-declarations-in-haskell

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

Changed 18 months ago by FUZxxl

  • cc fuzxxl@… added
  • component changed from Compiler to Compiler (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.

Changed 18 months ago by simonpj

See also #5674, which proposes the same feature.

Changed 18 months 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.

Simon

Changed 18 months ago by simonmar

  • owner set to simonpj
  • milestone set to 7.6.1

Changed 18 months 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?

Simon

Changed 18 months ago by simonpj@…

commit 6353ae0ff672dfcf79fd9bca1d58ff6ec7e7f9e0

Author: Simon Peyton Jones <simonpj@microsoft.com>
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(-)

Changed 18 months ago by simonpj

  • status changed from new to closed
  • difficulty set to Unknown
  • resolution set to fixed
  • testcase set to typecheck/should_compile/T5676

OK I implemented this. Documentation in the user guide.

Simon

Changed 18 months ago by simonpj

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

Changed 15 months 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.