Skip to content

Allow smart constructors their own types

GHC currently offers one-way pattern synonyms allowing general deconstruction with nice syntax. And it offers bidirectional pattern synonyms, which toss in construction functions that start with capital letters. Unfortunately, it forces the construction and deconstruction sides to share a type. This is sometimes rather unfortunate. The documentation itself points out the limitation with regard to matching numeric literals (where deconstruction requires Eq). Another example is Edward Yang's NF type, whose smart constructor (but not deconstructor) requires NFData. I think the easiest fix is to allow a type signature in the where clause of a pattern synonym definition.

pattern X :: Eq a => a -> Foo
pattern X q <- ... where
  X :: X a => a -> Foo
  X a = ...

Ideally, I'd like to offer the other one-way (constructor without pattern) as well, but I care less about that. }}}

Trac metadata
Trac field Value
Version 8.0.1
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information