I’m glad to announce the availability of a draft paper on F# Active Patterns, co-authored with James Margetson and Gregory Neverov. I’ve included the draft as an attachment to this post. In the paper we cover
- 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.
F# active patterns are available for use in F# 1.9.1, with some limitations outlined in the paper and in the release notes. I’ll follow up with a post with lots of examples of the language feature in action, or you can work from the examples in the paper.
The abstract is below.
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.