Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
ANN: lazy patterns (patterns v 0.3)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jeremy Yallop <jeremy.yallop@e...>
Subject: ANN: lazy patterns (patterns v 0.3)
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

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

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

OCaml gives the warning

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

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


and the documentation on the website