Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Pierre Weis <weis@pauillac.inria.fr>
To: raffalli@cs.chalmers.se (Christophe Raffalli)
Cc: caml-list@pauillac.inria.fr
Subject: Re: Suggestions
Date: Thu, 14 Sep 1995 11:49:55 +0200 (MET DST)	[thread overview]
Message-ID: <199509140949.LAA12642@pauillac.inria.fr> (raw)
In-Reply-To: <199509131651.SAA24277@waldorf.cs.chalmers.se> from "Christophe Raffalli" at Sep 13, 95 06:51:38 pm

Continuing the ``where match'' saga ...

> the problem is that "when" is not a binder !

> The "where match" is in fact strictly more general than the when:
>   pat when exp ->
> is equivalent to
>   pat where match exp with true ->
> but the when is weaker because it does not bind any variable.

You're right. In my mind even your ``where'' construct is not general
enough. What you want is the so-called ``continue'' feature of Caml
V3.1: a way to ask the pattern matcher to get out from an already
selected clause (wherever you can be within its expression part).

The continue construct works as this: in a pattern matching like

 | pat -> expr
 | pat' -> expr'
 | ...

a ``continue'' statement within expr will exit from the computation of
expr, and the pattern matching will go on with the next clause
appropriate for pat (presumably pat' -> expr').

Using continue, you can freely mix computation of expr and exits to the rest of
the pattern matching, as in:
 | pat ->
    begin try 
     let x = ... in
     if x > 2 then continue else assoc x l
    with Not_found -> continue end

Furthermore ``continue'' subsumes the ``where match'' you proposed:

 | pat where match expr with pati -> ei

is equivalent to

 | pat -> (match expr with pati -> ei | _ -> continue)

(In fact the implemented ``continue'' statement is a bit more complex:
pattern matchings with continue are named so that you may exit to
another pattern matching from within a more nested one.)

In Caml Light, you may simulate a kind of continue statement, using
exception handling and functionality. This is a bit less efficient and
less readable than a built-in mechanism, but it is acceptable since it
is not so common to need ``continue''...

(
Here is the encoding trick:
Suppose you have a clause pat -> expr that needs a continue statement,
inside a given pattern matching:

 | ...
 | pat -> expr
 | pati -> ei

Then define a Continue exception and an auxiliary function for the
rest of the pattern:

exception Continue;;

let rest = function
 | pati -> ei

Use ``raise Continue'' when you need a continue statement and rewrite
your code as:

 | ...
 | pat as p ->
    begin try expr
    with Continue -> rest p end
 | x -> rest x
)

Pierre Weis
----------------------------------------------------------------------------
WWW Home Page: http://pauillac.inria.fr/~weis
Projet Cristal
INRIA, BP 105, F-78153 Le Chesnay Cedex (France)
E-mail: Pierre.Weis@inria.fr
Telephone: +33 1 39 63 55 98
Fax: +33 1 39 63 53 30
----------------------------------------------------------------------------




  reply	other threads:[~1995-09-14  9:50 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-09-12  9:27 Release 1.06 of Caml Special Light Xavier Leroy
1995-09-13  8:15 ` Suggestions Christophe Raffalli
1995-09-13 15:11   ` Suggestions Pierre Weis
1995-09-13 16:51     ` Suggestions Christophe Raffalli
1995-09-14  9:49       ` Pierre Weis [this message]
1999-01-27  1:18 Suggestions Miles Egan
1999-01-27 18:48 ` Suggestions Jon Moore
1999-01-28 10:50   ` Suggestions Xavier Leroy

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=199509140949.LAA12642@pauillac.inria.fr \
    --to=weis@pauillac.inria.fr \
    --cc=caml-list@pauillac.inria.fr \
    --cc=raffalli@cs.chalmers.se \
    /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