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

[Caml-list] ANN: lazy patterns (patterns v 0.3)

10 views
Skip to first unread message

Jeremy Yallop

unread,
Oct 26, 2007, 3:02:58 AM10/26/07
to caml...@inria.fr
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.

_______________________________________________
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

0 new messages