caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* ANN: lazy patterns (patterns v 0.3)
@ 2007-10-25 21:37 Jeremy Yallop
  0 siblings, 0 replies; only message in thread
From: Jeremy Yallop @ 2007-10-25 21:37 UTC (permalink / raw)
  To: caml-list

I'm pleased to announce a new release of `patterns', an OCaml
extension providing general-purposes additions to pattern matching.
This release includes a new feature, "lazy patterns".

You can download patterns from

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

Lazy patterns extend OCaml pattern syntax with the keyword "lazy",
mirroring the use of lazy in expressions.  With this extension
patterns can be used to deconstruct lazy values; for example, you can
define a function that behaves like Lazy.force as follows

    let force (lazy x) = x

or, given a type of lazy lists in the "odd style":

    type 'a llist = Nil | Cons of 'a * lazy llist

you can write a lazy map function:

    let rec map f = function
     | Nil -> Nil
     | Cons (h, lazy t) -> Cons (f h, lazy (map f t))

You can use "lazy" anywhere you can use a constructor: in nested
patterns, or-patterns, patterns for "let", "function", "match",
"try/with", etc.  Lazy patterns can also be used with the other
extension provided in the current release, pattern guards: you can
write, for example,

    match v with
     | A (x, y) with lazy (z,w) = f x -> e

Paradoxically, lazy patterns make pattern-matching more eager, since
they force evaluation of delayed values.  However, no more forcing
than necessary (for some suitable definition thereof) will occur: the
following will return "true", for example.

    match lazy 3, lazy (assert false) with
     | lazy 2, lazy x -> false
     | lazy 3, _ -> true

Documentation for lazy patterns will be available soon.  Comments are
welcome, bug reports especially so.

A caveat: due to the translation used, lazy patterns can sometimes
give rise to spurious warnings.  For example, for the following
function

    function
       lazy (Some _) -> 1
     | lazy None     -> 0

OCaml gives the warning

    "Warning X: bad style, all clauses in this pattern-matching are 
guarded."

It is of course possible to avoid these warnings by using "-w x" or by
adding redundant match cases.

For information on pattern guards see the previous announcements

    v0.1: http://groups.google.com/group/fa.caml/msg/b0ec5324180bfeba
    v0.2: http://groups.google.com/group/fa.caml/msg/016e76d7a51559c8

and the documentation on the website

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

Jeremy.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2007-10-26  7:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-25 21:37 ANN: lazy patterns (patterns v 0.3) 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).