From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id CDF0CBC6B for ; Sat, 30 Jun 2007 06:09:30 +0200 (CEST) Received: from ipmail03.adl2.internode.on.net (ipmail03.adl2.internode.on.net [203.16.214.135]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l5U49ST9028914 for ; Sat, 30 Jun 2007 06:09:29 +0200 X-IronPort-AV: E=Sophos;i="4.16,478,1175437800"; d="scan'208";a="109649812" Received: from ppp59-167-2-129.lns1.syd7.internode.on.net (HELO [192.168.1.201]) ([59.167.2.129]) by ipmail03.adl2.internode.on.net with ESMTP; 30 Jun 2007 13:39:26 +0930 Subject: Re: [Caml-list] ANN: pattern guards From: skaller To: Jeremy Yallop Cc: Caml List In-Reply-To: <468555F9.7090005@ed.ac.uk> References: <1183141562.5305.19.camel@rosella.wigram> <468555F9.7090005@ed.ac.uk> Content-Type: text/plain Date: Sat, 30 Jun 2007 14:09:23 +1000 Message-Id: <1183176563.8849.27.camel@rosella.wigram> Mime-Version: 1.0 X-Mailer: Evolution 2.10.1 Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 4685D778.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; 0100,:01 0100,:01 ocaml:01 bindings:01 syntax:01 syntax:01 'when':01 'when':01 conceptually:01 function':01 sourceforge:01 wrote:01 wrote:01 syntactic:01 precedence:01 On Fri, 2007-06-29 at 19:56 +0100, Jeremy Yallop wrote: > skaller wrote: > > On Fri, 2007-06-29 at 15:19 +0100, Jeremy Yallop wrote: > >> I'm pleased to announce the initial release of `patterns', an OCaml > >> extension providing general-purposes additions to pattern matching. > > > > I want to do this: > > > > match x with > > | Y x with a=x and b=x > > | X (y,z) with a=y and b=z > > -> f a b > > Interesting. Do you want 'z' to be in scope in the guards ("a=y" etc.) > but not in the expression ("f a b")? Or do you just generally want to > allow or-patterns where the branches have different bindings as long as > the expression only uses variables that are bound in every branch? Good question. I don't know. In theory, the idea is a 'change of variables' as in a coordinate transformation, so only the 'final' variables should be in scope, i.e. 'z' would not be in scope. In practice, a suitable syntax needs to be devised which is convenient to use: a common case would be: | X with x = 1 | Y x -> f x and it would be messy to have to write the identity change of variables in the second branch.. so I'm open to suggestions as to syntax. > > This won't work at the moment for two reasons: > > > > * I assume the precedence of 'with' is the same as 'when', > > which is not convenient > > Right: "with" scopes over an entire match-case, which might include > or-patterns, just as with "when". Which is a pain, you can't write: ( | X | Y x when f x | Z x when g x ) -> .... [Felix allows nested 'when' clauses but not alternatives .. the latter due to laziness on my part implementing it] > > * the variables in the basic patterns don't agree > > > > The whole point of the above is to switch all the branches > > to normalised variables. At the moment I have to write: > > > > match x with > > | Y x -> f x x > > | X (y,z) -> f y z > > Unless I'm mistaken you can write this as > > match x with > | Y (y as z) > | X (y,z) -> f y z > > Is there some more general case for which this won't work out? Of course! See above. Conceptually you need an arbitrary change of variables. For example: | Polar (r, theta) with z = polar r theta | Cartesian (x,y) with z = cartesian x y -> f z As far as I can see this is basically eta-expansion, known to dummies like me as a 'wrapper function', which for functions allows you call a function with one set of variables with a completely different set of variables by a standard change of variables The idea is basically that, but 'moved' to the other side of the -> sign in a pattern match. The above case can of course be written: | Polar (r, theta) -> let z = polar r theta in f z | Cartestian (x,y) -> let z = cartesian x y in f z but involves duplicating the call to f. BTW: I'm writing some basic Scheme at the moment and I'm struck by how much is lost, not having pattern matching -- yet of course it is almost all just sugar. BTW2: It also strikes me good syntactic design is a tradeoff between the tensions of avoiding duplication and gratuitous invention, retaining localisation (things should be defined near where they're used), and modularity (name anything complex). So for example simple anonymous functions are good (localisation), let/in is good (factor complexity but retain localisation) and C++ sucks (loss of localisation). -- John Skaller Felix, successor to C++: http://felix.sf.net