From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA30840; Tue, 3 Apr 2001 20:29:11 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA30804 for ; Tue, 3 Apr 2001 20:29:10 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f33IT8f21941 for ; Tue, 3 Apr 2001 20:29:08 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA30830 for caml-list@inria.fr; Tue, 3 Apr 2001 20:29:09 +0200 (MET DST) From: Pierre Weis Message-Id: <200104031829.UAA30830@pauillac.inria.fr> Subject: Re: [Caml-list] Multi-matches & "when", also binding values in "when" clauses In-Reply-To: <0C682B70CE37BC4EADED9D375809768A48FD8B@red-msg-04.redmond.corp.microsoft.com> from Don Syme at "Apr 3, 101 05:54:16 am" To: dsyme@microsoft.com (Don Syme) Date: Tue, 3 Apr 2001 19:57:44 +0200 (MET DST) X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > > Some questions on pattern matching.... > > 1. - Multi-matches & "when" > > I've just discovered that one can write > > match x with > | C _ | D _ -> ... > | E | F -> ... > > to catch multiple patterns by single cases. However, if you guard the > patterns with "when" clauses, you must guard all the patterns att once > > match x with > | C _ | D _ when !foo = 3 -> ... > | E | F -> ... > > Here the "when" clause covers matches against both C and D. Is there > any good reason for this? It seems reasonable to be able to write > > match x with > | C _ when !foo = 4 | D _ when !foo = 3 -> ... > | E | F -> ... > > Are there any plans for this? No for more than one reason: - a trivial one: syntax ambiguity - more reasonable ones: when clauses introduce arbitrary computations into pattern matching; thus limiting the number of guard occurrences in the pattern of a clause seems to be good practice; moreover the order of evaluation of multiple guards into a given patterns will add further complexity in the semantics (and a lot of indeterminism). > 2. - Multiple binding in patterns > > It would sometimes be nice to bind variables twice in different > patterns, as in > > match x with > | C y | D y -> ... > | E | F -> ... > > Are there any plans for this? It's already available (again). > 3. Binding values in "when" clauses > > It would be very nice to be able to bind things in the "when" clauses > and have them visible on the r.h.s. > > match x with > | C y in > let z = y + 4 in > when !foo = z -> f z > | D _ -> ... As of the bindings inside guards, there are no problems: you can write match x with | C y when let z = y + 4 in !foo = z -> ... | D _ -> ... However the binding for z is lost when the corresponding expression is selected. (In your specific program, this indeed does not matter since z is equal to !foo when calling f :) So that | C y when let z = y + 4 in !foo = z -> f !foo would just work perfectly!) For a more general treatment of this kind of bindings inside guards, I'm not sure it is worth the extra implementation efforts. (In many case one can bind the variables outside the pattern matching, and in case this is not possible, you can assign the local values to some external references). Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr