caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Jacques Carette <carette@mcmaster.ca>
Cc: Nathaniel Gray <n8gray@gmail.com>, OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] Patterns that evaluate
Date: Thu, 15 Feb 2007 14:53:37 +1100	[thread overview]
Message-ID: <1171511618.13529.49.camel@rosella.wigram> (raw)
In-Reply-To: <45D37AD0.3090002@mcmaster.ca>

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 <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


  reply	other threads:[~2007-02-15  3:55 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-13 22:04 Jacques Carette
2007-02-13 22:07 ` [Caml-list] " Jon Harrop
2007-02-14  0:10   ` Jacques Carette
2007-02-14 18:20   ` Edgar Friendly
2007-02-14 18:55     ` Gerd Stolpmann
2007-02-14 19:10       ` Denis Bueno
2007-02-14 19:11       ` Jacques Carette
2007-02-14 19:25         ` Gerd Stolpmann
2007-02-14 20:30           ` Edgar Friendly
2007-02-14 21:05       ` Jon Harrop
2007-02-14 21:33         ` Jacques Carette
2007-02-14 22:34   ` Martin Jambon
2007-02-15  0:26     ` Jacques Garrigue
2007-02-15  3:57       ` Jon Harrop
2007-02-15 22:43         ` Don Syme
2007-02-14 20:29 ` Nathaniel Gray
2007-02-14 21:10   ` Jacques Carette
2007-02-15  3:53     ` skaller [this message]
2007-02-15 13:41       ` Jacques Carette
2007-02-15 14:10         ` skaller
2007-02-15 20:43     ` Nathaniel Gray
2007-03-07 11:15       ` Oliver Bandel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1171511618.13529.49.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@inria.fr \
    --cc=carette@mcmaster.ca \
    --cc=n8gray@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).