From: Jeremy Yallop <jeremy.yallop@ed.ac.uk>
To: caml-list@inria.fr
Subject: ANN: patterns v0.4
Date: Tue, 17 Jun 2008 12:20:51 +0100 [thread overview]
Message-ID: <48579E13.8070706@ed.ac.uk> (raw)
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.
next reply other threads:[~2008-06-17 11:20 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-06-17 11:20 Jeremy Yallop [this message]
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
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=48579E13.8070706@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