From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id IAA25818 for caml-redistribution; Mon, 10 May 1999 08:30:31 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA09277 for ; Sat, 8 May 1999 00:28:34 +0200 (MET DST) Received: from mail4.microsoft.com (mail4.microsoft.com [131.107.3.122]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id AAA06010 for ; Sat, 8 May 1999 00:28:32 +0200 (MET DST) Received: by mail4.microsoft.com with Internet Mail Service (5.5.2524.0) id ; Fri, 7 May 1999 15:28:33 -0700 Message-ID: <783D93998201D311B0CF00805FEAA07B7E8DA2@RED-MSG-42> From: Manuel Fahndrich To: "'caml-list@inria.fr'" Subject: stream pattern [< pat=exp >] and extensions to ordinary pattern m atching Date: Fri, 7 May 1999 15:28:29 -0700 X-Mailer: Internet Mail Service (5.5.2524.0) Sender: weis Hi, I just discovered the stream parser pattern [< pat = exp >] and I was wondering why this kind of pattern is not allowed in regular patterns? It is a very useful construct. For example: Suppose I have some type ast for abstract syntax type ast = Constant of constant | Binary of oper * ast * ast | ... where the type "constant" is not a datatype. There is however a function refine : constant -> con where type con = String of string | Int of int | ... In order to use pattern matching to detect a string constant, I have to use the following two auxiliary functions (which can be defined with what is given): isString : constant -> bool getString : constant -> string Then I can write: match a with Constant c when isString c -> ... getString c ... However, this is really not what I want, since I essentially test twice if c is a string (i.e. getString needs an error case), and furthermore, my operation 'refine' from above may be expensive. Now, in a paper ICFP'97 "Statically Checkable Pattern Abstraction", me and a colleague have explored more expressive patterns in the context of ML. It seems to me that the stream patterns in OCAML would allow me to do what we described in this paper, except for the fact that they can only appear in stream parsers. Suppose I could write pat = exp in the regular pattern language, with the semantics that the value being matched is first passed to the function exp, and the result is matched against pat: Then I can write the above code as follows: I write a different auxilary function to test whether a constant is a string that returns the string in case it is one. let isString2 c = match refine c with String s -> Some s | _ -> None Then my match becomes: match a with Constant (Some s = isString2) -> .... s .... | ... which is exactly what I want. I believe that with such an extension to OCAML, the pattern language we explored in the ICFP paper could be expressed completely (and more). In our paper, we used syntax that swaps the arguments and returns to pattern abstractions, so we could write the above as match a with Constant (isString2(s)) -> ... s ... Note that in this case the success/failure of the pattern is implicit, whereas in the above case I used an option to encode it. Any pattern abstraction could be encoded in this form, where the pattern bindings become the argument to Some. ... Just a random thought, but it would be neat. -Manuel Fahndrich