archives

Extensible Pattern Matching via a Lightweight Language

New draft of a paper by the F# team: Extensible Pattern Matching via a Lightweight Language Extension

By Don Syme, Gregory Neverov, James Margetson

Abstract
Pattern matching of algebraic data types (ADTs) is a standard feature in typed functional programming languages but it is well known that it interacts poorly with abstraction. While several partial solutions to this problem have been proposed, few have been implemented or used. This paper describes an extension to the .NET language F# called active patterns, which supports pattern matching over abstract representations of generic heterogeneous data such as XML and term structures, including where these are represented via object models in other .NET languages. Our design is the first to incorporate both ad hoc pattern matching functions for partial decompositions and “views” for total decompositions, and yet remains a simple and lightweight extension. We give a description of the language extension along with numerous motivating examples. Finally we describe how this feature would interact with other reasonable and related language extensions: existential types quantified at data discrimination tags, GADTs, and monadic generalizations of pattern matching.

Don Syme's blog introduces the paper:

  • How we combine total, partial and parameterized active patterns in one unified mechanism
  • Examples of using active patterns to match on LazyLists, Complex numbers, .NET Objects, strings using Regular Expressions, XML data, Data structures and F# Quotation values.