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=AWL autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id C7D28BC0A for ; Thu, 15 Feb 2007 04:55:00 +0100 (CET) Received: from ipmail02.adl2.internode.on.net (ipmail02.adl2.internode.on.net [203.16.214.141]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l1F3swgg003827 for ; Thu, 15 Feb 2007 04:54:59 +0100 Received: from ppp37-71.lns2.syd6.internode.on.net (HELO rosella) ([59.167.37.71]) by ipmail02.adl2.internode.on.net with ESMTP; 15 Feb 2007 14:24:35 +1030 X-IronPort-AV: i="4.14,172,1170595800"; d="scan'208"; a="85096882:sNHT3108467985" Subject: Re: [Caml-list] Patterns that evaluate From: skaller To: Jacques Carette Cc: Nathaniel Gray , OCaml In-Reply-To: <45D37AD0.3090002@mcmaster.ca> References: <45D23608.4030104@mcmaster.ca> <45D37AD0.3090002@mcmaster.ca> Content-Type: text/plain Date: Thu, 15 Feb 2007 14:53:37 +1100 Message-Id: <1171511618.13529.49.camel@rosella.wigram> Mime-Version: 1.0 X-Mailer: Evolution 2.6.1 Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 45D3D992.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; syntax:01 abstracted:01 bindings:01 ocaml:01 constructors:01 sourceforge:01 equality:01 lexical:01 wrote:01 wrote:01 imho:01 imho:01 constructor:01 caml-list:01 tail:01 On Wed, 2007-02-14 at 16:10 -0500, Jacques Carette wrote: > Nathaniel Gray wrote: > > I guess I'm not seeing it. How did you expect it to work? Is this > > what you mean: > > > > ... | ({st = Some v}, _) when v = e -> e2 > Yes. that is what I meant. > > > Is there some functionality that you're looking for that when clauses > > don't provide? > No, there is not. I was just (mistakenly) assuming that the "more > pleasant" syntax for this ``worked''. It is a common wish, but has many problems IMHO. First, it isn't very general. When clause has form: | ... x ... when P(x) -> which can express a lot more than merely one of the three equality operators. Indeed, P doesn't even have to be a known function -- it can be a function value. Second, the translation: | .. e .. -> to | .. x .. when x = e -> without extra lexical marks distinguishing pattern variables from local variables, and more generally patterns from expressions, would be extremely fragile: let h = 1 in match x with | h :: t -> ... The meaning of this matching appears to be to match a list starting with 1, binding t to the tail .. but it all depends on whether t is a symbol in the context of this fragment. The meaning would change if t were not in the context and a programmer introduced it: (* no t here *) .... let ... | h :: t -> Now the programmer adds a new global variable let t = [2] ... and it changes the meaning of the implementation of a function which one would expect was abstracted and localised. Thirdly it leaves open the issue of non-linear patterns: | x , x -> ... The first x binds to the first component of a tuple .. what does the second x do?? Easily solved with a when clause: | x, y when x = y -> ... Fourth, when clauses have a restriction that they do not allow introduction of extra bindings. I often need that: | C x | D with x = 1 -> ... x .... where I'm introducing a default for one branch. I also often match nasty patterns in many places and really wish I could name them: pattern p(x) = C x match e with | p(x) -> ... doesn't provide active patterns, but it would be quite useful. This can be done with a match composition already too: let p e = try match e with C x -> x with _ -> raise MatchFailure .. try let x = p e in ..with MatchFailure -> (* next case *) but it is messy (hard to compose). Jacques said: > Is there some functionality that you're looking for that when clauses > > don't provide? > No, there is not. and that's basically wrong. He's not telling the truth here :-> He IS looking for something more general, specifically | f a -> .. where f is pattern match variable. Ocaml currently requires f to be a constructor, that is, a constant. Any notion of advanced pattern matching would have to allow match variables to bind to constructors. IF we're going to extend pattern matching it should do this: http://www-staff.it.uts.edu.au/~cbj/Publications/pattern_calculus.ps IMHO of course.. :) -- John Skaller Felix, successor to C++: http://felix.sf.net