Skip to content

Pattern synonyms

Lennart would like pattern synonyms. Something like

pattern con var1 … varN = pat

where ‘pattern` is a new keyword.

  • Perhaps there should be a way to give a type as well, so the con could be (con :: type).
  • The rhs is type checked as a usual pattern, i.e., in the global environment.
  • The pat should bind exactly var1 .. varN.
  • Recursive pattern synonyms are not allowed.

With con in scope it can be used like any other constructor in a pattern, and the semantics is simply by expansion.

It would have been very nice if con\ could be used in expressions as well, but I don’t see how that could work with view patterns. Perhaps view patterns could be extended to make them bidirectional.

My rationale for wanting pattern synonyms is that I sometimes have pattern matching with a lot of complex repetition in them. I’ve even resorted to using CPP in the past, and that just shows that Haskell is lacking some abstraction mechanism.

If pattern synonyms could be made to work in the presence of view pattern it would offer a mechanism for normal pattern matching on abstract types, since the abstract type could export some pattern synonyms and you’d not be able to tell of those were real constructors or not.

I’ve not tried implementing this, but I think SHE has something like it.

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