caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <jeremy.yallop@ed.ac.uk>
To: caml-list@inria.fr
Subject: ANN: lazy patterns (patterns v 0.3)
Date: Thu, 25 Oct 2007 22:37:40 +0100	[thread overview]
Message-ID: <47210CA4.60707@ed.ac.uk> (raw)

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.


                 reply	other threads:[~2007-10-26  7:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47210CA4.60707@ed.ac.uk \
    --to=jeremy.yallop@ed.ac.uk \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).