Get the latest tech news

The Algebra of Patterns (Extended Version)


Pattern matching is a popular feature in functional, imperative and object-oriented programming languages. Language designers should therefore invest effort in a good design for pattern matching. Most languages choose a first-match semantics for pattern matching; that is, clauses are tried in the order in which they appear in the program until the first one matches. As a consequence, the order in which the clauses appear cannot be arbitrarily changed, which results in a less declarative programming model. The declarative alternative to this is an order-independent semantics for pattern matching, which is not implemented in most programming languages since it requires more verbose patterns. The reason for this verbosity is that the syntax of patterns is usually not expressive enough to express the complement of a pattern. In this paper, we show a principled way to make order-independent pattern matching practical. Our solution consists of two parts: First, we introduce a boolean algebra of patterns which can express the complement of a pattern. Second, we introduce default clauses to pattern matches. These default clauses capture the essential idea of a fallthrough case without sacrificing the property of order-independence.

View a PDF of the paper titled The Algebra of Patterns (Extended Version), by David Binder and 1 other authors View PDFHTML (experimental) Abstract:Pattern matching is a popular feature in functional, imperative and object-oriented programming languages. These default clauses capture the essential idea of a fallthrough case without sacrificing the property of order-independence.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of patterns

patterns

Photo of algebra

algebra

Photo of extended version

extended version

Related news:

News photo

Mathematician solves algebra's oldest problem using intriguing number sequences

News photo

12-factor Agents: Patterns of reliable LLM applications

News photo

Life Altering PostgreSQL Patterns