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 RAA26102; Tue, 3 Apr 2001 17:06:43 +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 RAA26103 for ; Tue, 3 Apr 2001 17:06:43 +0200 (MET DST) Received: from beaune.inria.fr (beaune.inria.fr [128.93.8.3]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f33F6gf15900 for ; Tue, 3 Apr 2001 17:06:42 +0200 (MET DST) Received: by beaune.inria.fr (8.8.8/1.1.22.3/14Sep99-0328PM) id RAA0000012691; Tue, 3 Apr 2001 17:06:42 +0200 (MET DST) From: Luc Maranget Message-Id: <200104031506.RAA0000012691@beaune.inria.fr> Subject: Re: [Caml-list] Multi-matches & "when", also binding values in "when" clauses To: dsyme@microsoft.com (Don Syme) Date: Tue, 3 Apr 2001 17:06:42 +0200 (MET DST) Cc: caml-list@inria.fr In-Reply-To: <0C682B70CE37BC4EADED9D375809768A48FD8B@red-msg-04.redmond.corp.microsoft.com> from "Don Syme" at avr 03, 2001 05:54:16 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 =3D 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 =3D 4 | D _ when !foo =3D 3 -> ... > | E | F -> ... > > Are there any plans for this? No. For the moment, you can see ``when expr'' as part of the r.h.s, not as part of the pattern. Your idea is new to me. The concrete syntax is misleading. > > > 2. - Multiple binding in patterns > > It would sometimes be nice to bind variables twice in different > patterns, as in=20 > > match x with > | C y | D y -> ... > | E | F -> ... > > Are there any plans for this? This feature is available in version 3.01. > > > 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=20 > let z =3D y + 4 in=20 > when !foo =3D z -> f z > | D _ -> ... > It cannot be done either. At some point and for other purposes we considered a ``with'' construct in patterns. ``with x = expr'' binds x to expr and always match. (This would achieve what you what, without the let ... in when .. which I find rather exotic) (with could also be used in or-patterns as in ``([x] | ([] with x=0))'' for instance I am unsure whether this would be very useful or not, considering that implementation effort is non-neglectible. A first problem with your constructs would be givng them a proper semantics. I see a simple solution : Consider the variables bound in pat to be accessible in expr. Semantics is quite clear btw - ``pat when expr'' matches when pat matches and expr is true - ``pat with x=expr'' matches when pat matches and add an extra binding As regards PM compilation, this looks feasible. Additionnaly the scope of the variables bound in pat includes expr. Compilation of these two constructs looks feasible. A second, practical, problem would be that patterns syntax would now include expressions, which would probably involve serious modifications of all passes in the front end of the compiler (before PM compilation). Cheers, thank you for your interest in Caml > Don --Luc ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr