caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* ANN: pattern guards
@ 2007-06-29 14:19 Jeremy Yallop
  2007-06-29 18:26 ` [Caml-list] " skaller
  0 siblings, 1 reply; 7+ messages in thread
From: Jeremy Yallop @ 2007-06-29 14:19 UTC (permalink / raw)
  To: Caml List

I'm pleased to announce the initial release of `patterns', an OCaml
extension providing general-purposes additions to pattern matching.
This release includes a single feature: "pattern guards"; others will
be made available in the near future.

You can download patterns from

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

Pattern guards generalize "when"-guards to allow arbitrary pattern
matching with binding.  After each pattern in a match you can write
one or more binding phrases as follows:

   match e with
     | patt1 with p1 = e1
             ...
             with pn = en
          -> e
     | patt2 ...

The expressions e1 ... en are evaluated in turn and matched with
the corresponding patterns p1 ... pn until either a match fails (in
which case matching proceeds with patt2 etc.) or all matches
succeed.  Any variables bound in p1 ... pn are in scope within
subsequent guards and within e.

For example, given a function

    val lookup : 'a -> ('a * 'b) list -> 'b option

you might write the following

    let f env = function
     | Var x
          with Some v = lookup x env -> ... v ...

instead of the less elegant and less efficient

    let f env = function
     | Var x
          when mem_assoc x env -> ... assoc x env ...

Pattern guards and regular guards can be freely intermixed; for
example, you can write

   match e with
     | patt when c1
            with patt1 = e1
            when c2
            with patt2 = e2 -> e
     | ...

Pattern guards were proposed (for Haskell) in

    Martin Erwig and Simon Peyton Jones
    Pattern Guards and Transformational Patterns
    Haskell Workshop, 2000

See also: http://research.microsoft.com/~simonpj/Haskell/guards.html

Jeremy.


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

end of thread, other threads:[~2007-07-03  9:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-29 14:19 ANN: pattern guards Jeremy Yallop
2007-06-29 18:26 ` [Caml-list] " skaller
2007-06-29 18:56   ` Jeremy Yallop
2007-06-29 19:31     ` Brian Hurt
2007-06-30  4:09     ` skaller
2007-06-30  4:44       ` Arnaud Spiwack
2007-07-03  9:29         ` Jeremy Yallop

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).