Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] ANN: patterns v0.4

29 views
Skip to first unread message

Jeremy Yallop

unread,
Jun 17, 2008, 7:21:05 AM6/17/08
to caml...@inria.fr
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.

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Nathaniel Gray

unread,
Jun 17, 2008, 5:38:04 PM6/17/08
to Jeremy Yallop, caml...@inria.fr
On Tue, Jun 17, 2008 at 4:20 AM, Jeremy Yallop <jeremy...@ed.ac.uk> wrote:
> I'm pleased to announce a new release of `patterns', an OCaml
> framework for writing extensions to pattern matching using Camlp4.

Ooh, very interesting! Have you looked at "active patterns" in F#?
They look really useful and I've been wanting to code them up in
camlp4 for a while now but haven't had the time. It sounds like your
framework could make that much easier.

Cheers,
-n8

--
>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- http://mojave.cs.caltech.edu -->

Jeremy Yallop

unread,
Jun 17, 2008, 5:58:47 PM6/17/08
to Nathaniel Gray, caml...@inria.fr
Nathaniel Gray wrote:
> On Tue, Jun 17, 2008 at 4:20 AM, Jeremy Yallop <jeremy...@ed.ac.uk> wrote:
>> I'm pleased to announce a new release of `patterns', an OCaml
>> framework for writing extensions to pattern matching using Camlp4.
>
> Ooh, very interesting! Have you looked at "active patterns" in F#?
> They look really useful and I've been wanting to code them up in
> camlp4 for a while now but haven't had the time. It sounds like your
> framework could make that much easier.

Yes, one of the reason for writing the framework was to be able to
implement F#-like active patterns. I think it should be reasonably
straightforward to do -- in fact, I'd expect design considerations to
take up more time than actual implementation work (although I say that
from the perspective of being already familiar with the "patterns"
framework, of course). If I remember rightly, there's a note at the end
of the ICFP07 active patterns paper about using polymorphic variants to
add active patterns in OCaml, which seems like it might be a good
starting point.

You might also be interested in the "views" feature of Martin Jambon's
"Micmatch", which is along the same lines as active patterns:

http://martin.jambon.free.fr/micmatch-manual.html#htoc10

Jeremy.

Richard Jones

unread,
Jun 20, 2008, 12:26:31 PM6/20/08
to Jeremy Yallop, Nathaniel Gray, caml...@inria.fr
On Tue, Jun 17, 2008 at 10:58:35PM +0100, Jeremy Yallop wrote:
> Nathaniel Gray wrote:
> >On Tue, Jun 17, 2008 at 4:20 AM, Jeremy Yallop <jeremy...@ed.ac.uk>
> >wrote:
> >>I'm pleased to announce a new release of `patterns', an OCaml
> >>framework for writing extensions to pattern matching using Camlp4.
> >
> >Ooh, very interesting! Have you looked at "active patterns" in F#?
> >They look really useful and I've been wanting to code them up in
> >camlp4 for a while now but haven't had the time. It sounds like your
> >framework could make that much easier.
>
> Yes, one of the reason for writing the framework was to be able to
> implement F#-like active patterns. I think it should be reasonably
> straightforward to do -- in fact, I'd expect design considerations to
> take up more time than actual implementation work (although I say that
> from the perspective of being already familiar with the "patterns"
> framework, of course). If I remember rightly, there's a note at the end
> of the ICFP07 active patterns paper about using polymorphic variants to
> add active patterns in OCaml, which seems like it might be a good
> starting point.

Can someone summarise active patterns for us? The MSDN site
containing the paper is down at the moment.

> You might also be interested in the "views" feature of Martin Jambon's
> "Micmatch", which is along the same lines as active patterns:
>
> http://martin.jambon.free.fr/micmatch-manual.html#htoc10

Is anyone working on upgrading micmatch to 3.10?

Rich.

--
Richard Jones
Red Hat

Martin Jambon

unread,
Jun 20, 2008, 1:00:27 PM6/20/08
to Richard Jones, Nathaniel Gray, Jeremy Yallop, caml...@inria.fr

I am, but I don't spend as much time on my personal projects than I used
to, so it goes slowly.

The dev page is at http://forge.ocamlcore.org/projects/micmatch/
There's a subversion repository. Although the code is currently unusable
you can see if there's some progress:

http://forge.ocamlcore.org/plugins/scmsvn/viewcvs.php/trunk/micmatch-redux/?root=micmatch


Martin

--
http://wink.com/profile/mjambon
http://mjambon.com/

Nathaniel Gray

unread,
Jun 24, 2008, 5:27:52 PM6/24/08
to Richard Jones, Jeremy Yallop, caml...@inria.fr
On Fri, Jun 20, 2008 at 9:26 AM, Richard Jones <ri...@annexia.org> wrote:
>
> Can someone summarise active patterns for us? The MSDN site
> containing the paper is down at the moment.

Sure. Active patterns are a lot like Wadler's "views", and thus are
similar to the stuff in micmatch. With active patterns you can invoke
functions (implicitly) within pattern matches and match the results,
which allows you to provide a "virtual" algebraic data structure for
any value you might want to match against. Here's an example from the
paper (using F# syntax):

open LazyList
let (|Cons|Nil|) l = if nonempty(l) then Cons(hd(l),tl(l)) else Nil

let rec pairSum xs =
match xs with
| Cons (x, Cons (y,ys)) -> consl (x+y) (lazy (pairSum ys))
| Cons (x, Nil ()) -> consl x (lazy nil)
| Nil () -> nil

This expands to:
let rec pairSum xs =
if nonempty xs then
let x, ys = hd xs, tl xs
if nonempty ys then
let y, zs = hd ys, tl ys
consl (x+y) (lazy (pairSum zs))
else consl x (lazy nil) )
else nil

There are other variations presented in the paper, including
parameterized patterns. These are nice for doing things like regular
expression matching. Again, from the paper:

let (|ParseRE|_|) re s =
let m = Regex(re).Match(s)
if m.Success
then Some [ for x in m.Groups -> x.Value ]
else None

let swap s =
match s with
| ParseRE "(\w+)-(\w+)" [l;r] -> r ^ "-" ^ l (* See below *)
| _ -> s

The matching syntax here is a bit confusing because it can be hard to
tell where parameters end and patterns begin. In the example above,
"(\w+)-(\w+)" is a parameter and [l;r] is a pattern. There's
definitely room for improvement over this syntax.

There are other variations and examples in the paper. I'd definitely
recommend reading it. If you still can't get it from the website I
can forward you a copy off-list.

Cheers,
-n8

--
>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- http://mojave.cs.caltech.edu -->

_______________________________________________

0 new messages