Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
ANN: patterns v0.4
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jeremy Yallop <jeremy.yallop@e...>
Subject: ANN: patterns v0.4
I'm pleased to announce a new release of `patterns', an OCaml
framework for writing extensions to pattern matching using Camlp4.

You can download `patterns' from

Previous releases provided specific extensions to pattern matching
("pattern guards" and "lazy patterns").  From this release onwards
"patterns" is not itself an extension, but a means for writing
extensions to pattern matching.  Some examples are provided with the

    * Pattern-matching for lazy values

      As in previous releases, but now available as an application of
      the framework rather than a hardcoded extension.

    * Conjunctive patterns

      Conjunctive patterns (as found in F#) generalise "as"-patterns.
      In standard OCaml the syntax `patt as var' may be used to bind a
      value simultaneously to both a pattern and a variable; with
      conjunctive patterns the syntax `patt & patt' may be used to bind
      a value simultaneously to two patterns.  For example,

          let ((`A a, b) & (c, `B d)) = (`A 3, `B 4) in (a,b,c,d)

      evaluates to

         (3, `B 4, `A 3, 4)

    * "Object patterns"

      Object patterns bind the results of calling an object's methods
      to other patterns during a pattern match.  This makes it more
      convenient to use objects as structurally-typed records.  The
      notation mirrors that in Jacques Garrigue's pa_oo extension.  For

          let {| x = x; y = _ |} =
             object method x = 3 method y = 4 method z = 5 end
             x + 1

      evaluates to


    * Negative patterns.

      Matching with negative patterns succeeds if the value does not
      match the pattern given.  For example,

         let nonzero = function
            | ~0 -> true
            | _  -> false
         in (nonzero 4, nonzero 0)

      evaluates to

         (true, false)

    * N+K patterns

      The infamous n+k patterns (as found in Haskell) offer a
      "Peano-number" view of integers.  Matching an integer value v
      against `patt+k' (where k is an integer literal) succeeds,
      binding patt to v-k, if v>=k.  For example

         let pred = function
            | x+1 -> x
            | 0   -> 0
         in (pred 10, pred 0)

      evaluates to

         (9, 0)

Pattern guards are gone for now.  I intend to restore them in a future
release, implemented as an application of the framework.

The "patterns" framework has the following features:

     * it makes it easy to write extensions to deep pattern matching,
       otherwise an arduous task.  For example, the entire
       implementation of lazy patterns is just a few lines of code.

     * it works with original and revised syntax.

     * pattern-matching extensions written using the framework extend
       patterns in every context in which patterns can be used:
       function, match, try/with, let, object, etc.

     * the extensions that use the framework may be used in any
       combination: for example, you can choose to use negative and n+k
       patterns in your program without loading any of the other

Users of previous versions may notice additional improvements.  For
example, "patterns" no longer modifies the OCaml grammar, so it should
coexist more happily with other extensions.

Feedback, including bug reports and patches, are very welcome.