Draft Paper on F# Active Patterns


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.

active-patterns-paper-draft.pdf

Comments (10)

  1. Stephan says:

    Nice paper. Is there a time horizon for the struct optimization mentioned in the implementation section? Is this something you’re currently working on? I’m just curious.

  2. dsyme says:

    Hi Stephan,

    Thanks!

    Re structs: I’d expect we’ll get to it at some point in the 2.0 release cycle (of which 1.9.1 is the first part).  In the meantime you can use structs to replace tuples as your active pattern return types, but you won’t get the nice tuple syntax (you use properties instead, like KeyValuePair in the .NET libraries). However there is no way to replace "option" or "choice" as the partial and discrimination pattern return types.

    Cheeers!

    don

  3. You may have heard of Active Patterns before – typically in conjunction with the words ‘awesome’ or ‘amazing’. Active Patterns are one of the more unique language features in F# and once you get a good feel for them one of the most powerful. This post

  4. You may have heard of Active Patterns before – typically in conjunction with the words ‘awesome’ or

  5. Last Tuesday I gave a talk to the .NET Developers Association entitled Language Oriented Programming

  6. Amar Nath Satrawala says:

    Can somebody comment that active patterns are similar to implicit cast operators available in e.g. C#. Both need static inference. Instead of return simple objects in the case of C#, in F# records of special types are returned.

    Is is a fundamental similarity here?