caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Multi-matches & "when", also binding values in "when" clauses
@ 2001-04-03 12:54 Don Syme
  2001-04-03 13:00 ` Nicolas barnier
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Don Syme @ 2001-04-03 12:54 UTC (permalink / raw)
  To: caml-list


Some questions on pattern matching....

1. - Multi-matches & "when"

I've just discovered that one can write

match x with
  | C _   | D _ -> ...
  | E | F -> ...

to catch multiple patterns by single cases.  However, if you guard the
patterns with "when" clauses, you must guard all the patterns att once

match x with
  | C _   | D _ when !foo = 3 -> ...
  | E | F -> ...

Here the "when" clause covers matches against both C and D.  Is there
any good reason for this?  It seems reasonable to be able to write

match x with
  | C _  when !foo = 4 | D _ when !foo = 3 -> ...
  | E | F -> ...

Are there any plans for this?


2. - Multiple binding in patterns

It would sometimes be nice to bind variables twice in different
patterns, as in 

match x with
  | C y | D y -> ...
  | E | F -> ...

Are there any plans for this?


3. Binding values in "when" clauses

It would be very nice to be able to bind things in the "when" clauses
and have them visible on the r.h.s.

match x with
  | C y  in 
      let z = y + 4 in 
      when !foo = z -> f z
  | D _ -> ...


Are there any plans in the work for these sorts of extensions?
Cheers,
Don

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Multi-matches & "when", also binding values in "when"  clauses
  2001-04-03 12:54 [Caml-list] Multi-matches & "when", also binding values in "when" clauses Don Syme
@ 2001-04-03 13:00 ` Nicolas barnier
  2001-04-03 15:06 ` Luc Maranget
  2001-04-03 17:57 ` Pierre Weis
  2 siblings, 0 replies; 4+ messages in thread
From: Nicolas barnier @ 2001-04-03 13:00 UTC (permalink / raw)
  To: Don Syme; +Cc: caml-list

Don Syme wrote:
> 
> 2. - Multiple binding in patterns
> 
> It would sometimes be nice to bind variables twice in different
> patterns, as in
> 
> match x with
>   | C y | D y -> ...
>   | E | F -> ...
> 
> Are there any plans for this?

Allowed in 3.01.

-- Nicolas
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Multi-matches & "when", also binding values in "when" clauses
  2001-04-03 12:54 [Caml-list] Multi-matches & "when", also binding values in "when" clauses Don Syme
  2001-04-03 13:00 ` Nicolas barnier
@ 2001-04-03 15:06 ` Luc Maranget
  2001-04-03 17:57 ` Pierre Weis
  2 siblings, 0 replies; 4+ messages in thread
From: Luc Maranget @ 2001-04-03 15:06 UTC (permalink / raw)
  To: Don Syme; +Cc: caml-list

> 
> 
> Some questions on pattern matching....
> 
> 1. - Multi-matches & "when"
> 
> I've just discovered that one can write
> 
> match x with
>   | C _   | D _ -> ...
>   | E | F -> ...
> 
> to catch multiple patterns by single cases.  However, if you guard the
> patterns with "when" clauses, you must guard all the patterns att once
> 
> match x with
>   | C _   | D _ when !foo =3D 3 -> ...
>   | E | F -> ...
> 
> Here the "when" clause covers matches against both C and D.  Is there
> any good reason for this?  It seems reasonable to be able to write
> 
> match x with
>   | C _  when !foo =3D 4 | D _ when !foo =3D 3 -> ...
>   | E | F -> ...
> 
> Are there any plans for this?
No. For the moment, you can see
``when expr'' as part of  the r.h.s, not as part of the pattern.
Your idea is new to me.
The concrete syntax is misleading.



> 
> 
> 2. - Multiple binding in patterns
> 
> It would sometimes be nice to bind variables twice in different
> patterns, as in=20
> 
> match x with
>   | C y | D y -> ...
>   | E | F -> ...
> 
> Are there any plans for this?
This feature is available in version 3.01.


> 
> 
> 3. Binding values in "when" clauses
> 
> It would be very nice to be able to bind things in the "when" clauses
> and have them visible on the r.h.s.
> 
> match x with
>   | C y  in=20
>       let z =3D y + 4 in=20
>       when !foo =3D z -> f z
>   | D _ -> ...
> 
It cannot be done either.
At some point and for other purposes we considered a ``with''
construct in patterns.
``with x = expr'' binds x to expr and always match.
(This would achieve what you what, without the let ... in when ..
which I find rather exotic)
(with could also be used in or-patterns as in
``([x] | ([] with x=0))'' for instance 
I am unsure whether this would be very useful or not, considering that
implementation effort is non-neglectible.



A first problem with your constructs would be givng them
a proper semantics.
I see a simple solution :

Consider the variables bound in pat to be accessible in expr.
Semantics is quite clear btw
 - ``pat when expr'' matches when pat matches and expr is true
 - ``pat with x=expr'' matches when pat matches and add an extra binding
   As regards PM compilation, this looks feasible.
  Additionnaly the scope of the variables bound in pat includes expr.
  Compilation of these two constructs looks feasible.


A second, practical, problem would be that patterns syntax would now
include expressions, which would probably involve serious modifications
of all passes in the front end of the compiler (before PM
compilation).



Cheers, thank you for your interest in Caml

> Don

--Luc 
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Multi-matches & "when", also binding values in "when" clauses
  2001-04-03 12:54 [Caml-list] Multi-matches & "when", also binding values in "when" clauses Don Syme
  2001-04-03 13:00 ` Nicolas barnier
  2001-04-03 15:06 ` Luc Maranget
@ 2001-04-03 17:57 ` Pierre Weis
  2 siblings, 0 replies; 4+ messages in thread
From: Pierre Weis @ 2001-04-03 17:57 UTC (permalink / raw)
  To: Don Syme

> 
> Some questions on pattern matching....
> 
> 1. - Multi-matches & "when"
> 
> I've just discovered that one can write
> 
> match x with
>   | C _   | D _ -> ...
>   | E | F -> ...
> 
> to catch multiple patterns by single cases.  However, if you guard the
> patterns with "when" clauses, you must guard all the patterns att once
> 
> match x with
>   | C _   | D _ when !foo = 3 -> ...
>   | E | F -> ...
> 
> Here the "when" clause covers matches against both C and D.  Is there
> any good reason for this?  It seems reasonable to be able to write
> 
> match x with
>   | C _  when !foo = 4 | D _ when !foo = 3 -> ...
>   | E | F -> ...
> 
> Are there any plans for this?

No for more than one reason:
 - a trivial one: syntax ambiguity
 - more reasonable ones: when clauses introduce arbitrary
computations into pattern matching; thus limiting the number of guard
occurrences in the pattern of a clause seems to be good practice;
moreover the order of evaluation of multiple guards into a given
patterns will add further complexity in the semantics (and a lot of
indeterminism).

> 2. - Multiple binding in patterns
> 
> It would sometimes be nice to bind variables twice in different
> patterns, as in 
> 
> match x with
>   | C y | D y -> ...
>   | E | F -> ...
> 
> Are there any plans for this?

It's already available (again).

> 3. Binding values in "when" clauses
> 
> It would be very nice to be able to bind things in the "when" clauses
> and have them visible on the r.h.s.
> 
> match x with
>   | C y  in 
>       let z = y + 4 in 
>       when !foo = z -> f z
>   | D _ -> ...

As of the bindings inside guards, there are no problems: you can write

match x with
| C y when let z = y + 4 in !foo = z -> ...
| D _ -> ...

However the binding for z is lost when the corresponding expression is
selected.

(In your specific program, this indeed does not matter since z is equal
to !foo when calling f :) So that

| C y when let z = y + 4 in !foo = z -> f !foo

would just work perfectly!)

For a more general treatment of this kind of bindings inside guards,
I'm not sure it is worth the extra implementation efforts. (In many
case one can bind the variables outside the pattern matching, and in
case this is not possible, you can assign the local values to some
external references).

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/



-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2001-04-03 18:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-03 12:54 [Caml-list] Multi-matches & "when", also binding values in "when" clauses Don Syme
2001-04-03 13:00 ` Nicolas barnier
2001-04-03 15:06 ` Luc Maranget
2001-04-03 17:57 ` Pierre Weis

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