Proposal: pattern synonyms

Conor McBride ctm at
Thu Feb 16 04:51:10 EST 2006

Hi folks

Having spent the last week hacking, I've found a common irritation, so I 
have a proposal to mitigate it. Of course, this issue may not bug anyone 
else, so I'm a little hesitant. In pessimal pedagogical order, the 
proposal: pattern synonyms. I want to be allowed to write stuff like

  P x y z = C x (y, [z])

defining a capitalised symbol with parameters used linearly in a pattern 
on the right-hand side. Pattern synonyms should be fully applied and 
expand macro-style wherever patterns or terms appear, hence should not 
give rise to cycles, just like type synonyms.

It seems (and is) trivial. Why would it help? Well, it would help me, 
because I often write more complex types than strictly necessary to 
represent data in order to expose its structure and thus gain free 
equipment. For example, taking

newtype Comp f g x = Comp (f (g x))  -- applicative if f and g are
newtype Prod f g x = Prod (f x, g x) -- applicative if f and g are
newtype Const x y = Const y -- applicative if x is a monoid
newtype Id x = Id x -- trivially applicative
newtype Any = Any Bool  -- equipped with the disjunctive monoid structure

all of which I keep lying around anyway,

I might well write

type MyEffect = Comp Maybe (Prod (Const Any) Id)

which is the same as

type MyEffect' x = Maybe (Bool, x)

except that

  (1) MyEffect :: * -> * is an applicative functor without further ado 
(because it is composed from suitable components), hence I get lots of 
free kit for traversing data with it. (If you're interested, we use this 
to update terms where some variables have been changed and others have 
been deleted entirely: we want to know the new term if it exists, and 
whether it differs from the old one.) MyEffect' is meaningless.
  (2) Pattern matching on MyEffect x is much more annoying than on 
MyEffect' x.

I want to write

Bang :: MyEffect x
Bang = Comp Nothing

Bing :: x -> MyEffect x
Bing x = Comp (Just (Prod (Any True), Id x))

Dull :: x -> MyEffect x
Dull x = Comp (Just (Prod (Any False), Id x))

and have done with it.

Does this happen to anyone else? Would pattern synonyms help?

Thought I'd ask, anyway.

All the best


More information about the Haskell-prime mailing list