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

* Re: [Caml-list] ANN: patterns v0.4
  2008-06-17 11:20 ANN: patterns v0.4 Jeremy Yallop
@ 2008-06-17 21:37 ` Nathaniel Gray
  2008-06-17 21:58   ` Jeremy Yallop
  0 siblings, 1 reply; 6+ messages in thread
From: Nathaniel Gray @ 2008-06-17 21:37 UTC (permalink / raw)
  To: Jeremy Yallop; +Cc: caml-list

On Tue, Jun 17, 2008 at 4:20 AM, Jeremy Yallop <jeremy.yallop@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 -->


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

* Re: [Caml-list] ANN: patterns v0.4
  2008-06-17 21:37 ` [Caml-list] " Nathaniel Gray
@ 2008-06-17 21:58   ` Jeremy Yallop
  2008-06-20 16:26     ` Richard Jones
  0 siblings, 1 reply; 6+ messages in thread
From: Jeremy Yallop @ 2008-06-17 21:58 UTC (permalink / raw)
  To: Nathaniel Gray; +Cc: caml-list

Nathaniel Gray wrote:
> On Tue, Jun 17, 2008 at 4:20 AM, Jeremy Yallop <jeremy.yallop@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.


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

* Re: [Caml-list] ANN: patterns v0.4
  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
  0 siblings, 2 replies; 6+ messages in thread
From: Richard Jones @ 2008-06-20 16:26 UTC (permalink / raw)
  To: Jeremy Yallop; +Cc: Nathaniel Gray, caml-list

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.yallop@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


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

* Re: [Caml-list] ANN: patterns v0.4
  2008-06-20 16:26     ` Richard Jones
@ 2008-06-20 16:56       ` Martin Jambon
  2008-06-24 21:27       ` Nathaniel Gray
  1 sibling, 0 replies; 6+ messages in thread
From: Martin Jambon @ 2008-06-20 16:56 UTC (permalink / raw)
  To: Richard Jones; +Cc: Jeremy Yallop, Nathaniel Gray, caml-list

On Fri, 20 Jun 2008, Richard Jones wrote:

> 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.yallop@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?

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/


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

* Re: [Caml-list] ANN: patterns v0.4
  2008-06-20 16:26     ` Richard Jones
  2008-06-20 16:56       ` Martin Jambon
@ 2008-06-24 21:27       ` Nathaniel Gray
  1 sibling, 0 replies; 6+ messages in thread
From: Nathaniel Gray @ 2008-06-24 21:27 UTC (permalink / raw)
  To: Richard Jones; +Cc: Jeremy Yallop, caml-list

On Fri, Jun 20, 2008 at 9:26 AM, Richard Jones <rich@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 -->


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