Opened 6 years ago

Closed 5 years ago

#7268 closed bug (fixed)

Explicit type signatures for top level record pattern matches polymorphism fail

Reported by: TristanAllwood Owned by:
Priority: normal Milestone: 7.8.1
Component: Compiler (Type checker) Version: 7.4.1
Keywords: Cc: martijn@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Test Case: typecheck/should_compile/T7268
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Consider the following module, which declares a record with two fields (X), an instance of this record (x), and then (at the top level) pattern matches the fields of x to create two new functions.

{-# LANGUAGE RankNTypes #-}
module Foo where

data X
  = X { a :: forall a . a -> a , b :: Int -> Int }

x :: X
x = X { a = id, b = succ }

succ' :: Int -> Int

X { a = id', b = succ' } = x

If we ask ghci what the type of id' is, we get told:

:t id'
id' :: a -> a

However adding this type explicitly, gives the following:

    Couldn't match expected type `forall a. a -> a'
                with actual type `a -> a'
    In the pattern: X {a = id', b = succ'}
    In a pattern binding: X {a = id', b = succ'} = x
Failed, modules loaded: none.

This can happen in practice when using (e.g.) Parsec's TokenParser.

Change History (7)

comment:1 Changed 5 years ago by igloo

difficulty: Unknown
Milestone: 7.8.1
Owner: set to simonpj

Thanks for the report. Simon, could you take a look at what the expected behaviour is here, please?

comment:2 Changed 5 years ago by MartijnVanSteenbergen

Cc: martijn@… added

Some more info and examples can be found in #7891.

Notably, for the program above, an extra indirection makes it ok again to give a type signature:

id'' :: a -> a
id'' = id'

comment:3 Changed 5 years ago by simonpj@…

commit 0452021e726ab44f3866faacf7817ac116bb58db

Author: Simon Peyton Jones <>
Date:   Wed May 15 10:15:47 2013 +0100

    Fix typechecking of pattern bindings that have type signatures (Trac #7268)
    Pattern bindings are jolly tricky to typecheck, especially if there are
    also type signatures involved.  Trac #7268 pointed out that I'd got it
    wrong; this fixes it.  See Note [Typing patterns in pattern bindings] in TcPat.

 compiler/typecheck/TcBinds.lhs |   21 ++++------
 compiler/typecheck/TcPat.lhs   |   81 +++++++++++++++++++++------------------
 2 files changed, 52 insertions(+), 50 deletions(-)

comment:4 Changed 5 years ago by simonpj

Resolution: fixed
Status: newclosed
Test Case: typecheck/should_compile/T7268

Excellent point thank you. Pattern bindings are devilish tricky. Fixed now. I added a regression test for #7891 as well.


comment:5 Changed 5 years ago by MartijnVanSteenbergen

I'm really happy with this fix, thank you very much!

comment:6 Changed 5 years ago by MartijnVanSteenbergen

Owner: simonpj deleted
Resolution: fixed
Status: closednew

Hi Simon, looking at testcase T7891, wouldn't it be better to uncomment line 12 so that it becomes an actual regression test?

comment:7 Changed 5 years ago by simonpj

Resolution: fixed
Status: newclosed

I'm glad it's helpful. Thanks for pointing out the bug in the regression test...I've just pushed a fix.


Note: See TracTickets for help on using tickets.