Version française
Home     About     Download     Resources     Contact us    
Browse thread
Patterns that evaluate
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Gerd Stolpmann <info@g...>
Subject: Re: [Caml-list] Patterns that evaluate
Am Mittwoch, den 14.02.2007, 12:20 -0600 schrieb Edgar Friendly:
> Jon Harrop wrote:
> > On Tuesday 13 February 2007 22:04, Jacques Carette wrote:
> >> I recently wrote some ocaml code which "worked", but not as I
> >> intended...  The test cases I tried worked, but I should have tested
> >> harder.  Apparently I was under the mistaken impression that OCaml's
> >> pattern-matching was more "first class"!  So I wrote (in part):
> >>
> >> let buildsimp cast e f1 f2 = fun e1 -> fun e2 -> match (e1,e2) with
> >>
> >>                                                  | ({st = Some e}, _) -> e2
> >>
> >> and I expected it to work.  Only a code review by a colleague 'found'
> >> this bug in my code.
> >>
> >> Question: would it be a difficult extension?  This seemed so "natural",
> >> I just "used" the feature before it was quite there yet ;-).
> > 
> > F# just introduced active patterns, which does what you want AFAIK. Of course, 
> > you must disambiguate that from the OCaml's current interpretation of the 
> > above (binding "e").
> > 
> The two options I see are:
> 1) noting a re-binding, and automatically testing against the value of 
> that previous binding
> 2) extra syntax (maybe || instead of | before active match constructs)
> 
> In the first case, there's backwards compatibility issues.  Wouldn't it 
> be useful to have the compiler warn on such uses, to make people aware 
> of rebindings performed in their code?

You are a bit quick. Before discussing syntax it is more important to
define the semantics of such patterns. I mean we have already three
predefined kinds of equality in O'Caml:

- ( == )
- ( = )
- (fun x y -> compare x y = 0)

I admit I do not prefer any one of them. So which equality should be
used to test whether the variable is equal to the matched part of the
value?

I guess this simple question is one of the reasons why such a feature is
not in the language up to now.

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------