Proposal: pattern synonyms

Niklas Broberg niklas.broberg at gmail.com
Fri Feb 17 06:13:19 EST 2006


On 2/16/06, Henrik Nilsson <nhn at cs.nott.ac.uk> wrote:
> Conor and I discussed this over lunch.
>
> Specifically, we talked about whether the right hand side of a pattern
> synonym would be any Haskell pattern (including "_", "~", possibly "!"),
> or restricted to the intersection between the patterns and terms, as
> Conor propose that pattern synonyms also be used for construction.
>
> By adopting some simple conventions, like replacing "_" by "undefined"
> when a synonym is used as a term for construction, it is clear that one
> can be a bit more liberal than a strict intersection between the pattern
> and current expression syntax.

I would speak against this. I like the idea of pattern synonyms, but I
think they should be just that - pattern synonyms, and not try to mix
in expressions as well. With the current (H98) pattern situation, it
*might* be possible to tweak things so that all patterns have an
expressional meaning (e.g. "_" as "undefined"), but 1) it would be
fairly construed in many cases, and 2) it would complicate making
extensions to the pattern matching facility.

(Shameless plug:) In particular I'm thinking about our extension HaRP
[1] that adds regular expressions to pattern matching over lists. The
ability to define pattern synonyms would be really useful in
conjunction with HaRP, but if those patterns are required to also have
an expressional meaning it would make things fairly complicated, not
to say impossible.

Instead I would like to propose an extension to the proposed extension
in another direction: Adding in (pattern) guards. Consider patterns
like

IsSpace x = x | isSpace x
Last x = xs | x <- last xs

This would in particular go well together with HaRP, where you in some
cases need guards to be inlined, e.g.

words [ (IsSpace _)*!, (/ xs@:(_*), (IsSpace _)*! /)* ] = xs

Btw, why not consider adding regular patterns a la HaRP to Haskell'? :-)

(Disclaimer: I'm not really serious, like Lennart I don't really feel
that any of this has any place in Haskell'. But as for the future
beyond that, I am serious.)

[1] http://www.cs.chalmers.se/~d00nibro/harp


More information about the Haskell-prime mailing list