caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* ANN: patterns v0.4
@ 2008-06-17 11:20 Jeremy Yallop
  2008-06-17 21:37 ` [Caml-list] " Nathaniel Gray
  0 siblings, 1 reply; 6+ messages in thread
From: Jeremy Yallop @ 2008-06-17 11:20 UTC (permalink / raw)
  To: caml-list

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

     http://code.google.com/p/ocaml-patterns/

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
release:

    * Pattern-matching for lazy values

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

      http://code.google.com/p/ocaml-patterns/wiki/LazyPatterns

    * 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
      example,

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

      evaluates to

          4

    * 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
       extensions.

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.

Jeremy.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-06-24 21:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-17 11:20 ANN: patterns v0.4 Jeremy Yallop
2008-06-17 21:37 ` [Caml-list] " Nathaniel Gray
2008-06-17 21:58   ` Jeremy Yallop
2008-06-20 16:26     ` Richard Jones
2008-06-20 16:56       ` Martin Jambon
2008-06-24 21:27       ` Nathaniel Gray

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).