Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
ANN: pattern guards
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-06-29 (18:53)
From: Jeremy Yallop <jeremy.yallop@e...>
Subject: Re: [Caml-list] ANN: pattern guards
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?

> 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".

> * 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?