caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re:  test with several issues
@ 1995-11-13 13:28 Valerie Menissier-Morain
  0 siblings, 0 replies; 3+ messages in thread
From: Valerie Menissier-Morain @ 1995-11-13 13:28 UTC (permalink / raw)
  To: caml-list, quercia


This is almost the ``when'' construction of Caml V3.1. In Caml V3.1, you 
should write:
when cond1 -> action1
   | cond2 -> action2
	   .
           .
           .
   | condn -> actionn
   | _ -> action_default

Vale'rie Me'nissier-Morain




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

* Re: test with several issues
  1995-11-11 13:08 quercia
@ 1995-11-13 19:59 ` Pierre Weis
  0 siblings, 0 replies; 3+ messages in thread
From: Pierre Weis @ 1995-11-13 19:59 UTC (permalink / raw)
  To: quercia; +Cc: caml-list


> *match* together with *when* enables to code nicely a test with several issues :
> match 0 with
>   _ when {condition-1} -> {action-1}
> | _ when {condition-2} -> {action-2}
> ...
> | _                    -> {default action}

I introduced the when construct in Caml V3.0 a long time ago (1990),
following exactly the same reasoning:

<<
1) Simple when:

Gards in patterns imply that you may write:

match true with
   _ when cond1 -> e1
|  _ when cond2 -> e2
|  ...
| _ -> e3

which is equivalent to

 if cond1 then e1 else
 if cond2 then e2 else
 ...
 else e3

In fact writing conditional with -> | is much more readable and
interaction between sequences and branches of consitional diseapear.
Thus we may introduce a when construct:

Syntax:
 when expr1 -> e1
    | expr2 -> e2
   ...
    | exprn -> en

Analogy with ``match true with ...'' suggests to admit
 | _ -> en
a equivalent to
 | true -> en

2) Extended when:

We may also extend this construct to multiple conditions:

Syntax:

 when expr1 { &| expr11 -> e11
              &| expr12 -> e12
              ...
              &| expr1n -> e1n}

 |    expr2 { &| expr21 -> e21
              ... }
 ...
 
 |    exprn { ... }

Se'mantixs:

 when expr1 &| expr11 -> e11
            &| expr12 -> e12
    | expr2 -> e2
    | _ -> en

is equivalent to:
 if expr1 then
    (if expr11 then e11
     else if expr12 then e12
     else if expr2 then e2 else en) else
 if expr2 then e2 else en

Now the compiler has something to do, since there is so code sharing
that you cannot write directly in Caml (except with functions).

Note:
1) When you use the when construct once, you use it all the time since it
is really clearer.

[...]
>>

Nothing to add to this old message, there is no problem to add it to
Caml Light, we simply have to decide to do it...

> ------------------------------ texte Francais --------------------------------
J'ai introduit la conditionnelle ily a tre`s longtemps en Caml V3.0 en
suivant le me^me raisonnement que le vo^tre. Je cite mes arguments de
l'e'poque (1990):
<<
1) When simple:

Les gardes ds le filtrage induisent la possibilite' d'e'crire des if
en cascades avec le ``truc'' suivant:

match true with
   _ when cond1 -> e1
|  _ when cond2 -> e2
|  ...
| _ -> e3

Evidemment equivalent a`:

 if cond1 then e1 else
 if cond2 then e2 else
 ...
 else e3

Il se trouve cependant que la construction syntaxique -> | est bien
plus lisible et bien plus tole'rante en ce qui concerne les se'quences
(qui apparraissent souvent ds les conditionnelles).

D'ou` l'ide'e d'abbreger un peu la construction ci-dessus avec une
construction ``when'':

Syntaxe:
 when expr1 -> e1
    | expr2 -> e2
   ...
    | exprn -> en

L'analogie avec ``match true with ...'' sugge`re en plus d'admettre
 | _ -> en
 comme e'quivalent a`
 | true -> en

2) Le when e'tendu:

On peut aussi e'tendre la construction when avec des conditions
multiples :

Syntaxe:

 when expr1 { &| expr11 -> e11
              &| expr12 -> e12
              ...
              &| expr1n -> e1n}

 |    expr2 { &| expr21 -> e21
              ... }
 ...
 
 |    exprn { ... }

Se'mantique:

 when expr1 &| expr11 -> e11
            &| expr12 -> e12
    | expr2 -> e2
    | _ -> en

Est e'quivalent a`:
 if expr1 then
    (if expr11 then e11
     else if expr12 then e12
     else if expr2 then e2 else en) else
 if expr2 then e2 else en

Chacun aura remarque' que le travail du compilateur est devenu non
nul, puisqu'il y a maintenant du partage de code non exprimable en
CAML sans recopie de source (ce qui cre'e une perte de lisibilite' et
des bugs potentiel lors de la maintenance du programme).

Remarques:

1) Apre`s avoir employe' le ``when'', il n'y a pas de doute que c'est plus
clair: c'est a` mon avis la construction de base, le if e'tant une
macro pour un when simplifie' (de la me^me facon que le ``let destructurant''
est un cas particulier de pattern matching)

[...]

>>

Y'a qu'a` le faire !

Pierre Weis
----------------------------------------------------------------------------
WWW Home Page: http://pauillac.inria.fr/~weis
Projet Cristal
INRIA, BP 105, F-78153 Le Chesnay Cedex (France)
E-mail: Pierre.Weis@inria.fr
Telephone: +33 1 39 63 55 98
Fax: +33 1 39 63 53 30
----------------------------------------------------------------------------




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

* test with several issues
@ 1995-11-11 13:08 quercia
  1995-11-13 19:59 ` Pierre Weis
  0 siblings, 1 reply; 3+ messages in thread
From: quercia @ 1995-11-11 13:08 UTC (permalink / raw)
  To: caml-list


I apologize for my poor english. Please, if you can read french skip the
30 following lines.

------------------------------ "English" text --------------------------------

*match* together with *when* enables to code nicely a test with several issues :

match 0 with
  _ when {condition-1} -> {action-1}
| _ when {condition-2} -> {action-2}
...
| _                    -> {default action}

This replaces the construct :

     if {condition-1} then {action-1}
else if {condition-2} then {action-2}
...
else                       {default action}

wich seems to me less readable. Annoyingly, one has to provide a value to
the match (0 here), but that value won't really be used.
Could you support my demand to INRIA so that the next releases of Caml-light
and Caml-special-light provide a nicer construct :

select
  when {condition-1} -> {action-1}
  when {condition-2} -> {action-2}
  ...
  otherwise          -> {default action}

like for example in PL/1 and in Rexx ?

------------------------------ texte Francais --------------------------------

*match* associe avec *when* permet de coder elegament un test a plusieurs
cas :

match 0 with
  _ when {condition-1} -> {action-1}
| _ when {condition-2} -> {action-2}
...
| _                    -> {action par defaut}

Ceci remplace la construction :

     if {condition-1} then {action-1}
else if {condition-2} then {action-2}
...
else                       {action par defaut}

qui me semble moins lisible. L'ennui est qu'il faut donner une
valeur a tester a match (ici 0) alors qu'on n'en fera rien.
Voulez-vous appuyer ma demande a l'INRIA pour que les prochaines versions
de Caml-light et Caml-special-light autorisent une construction encore plus
claire :

select
  when {condition-1} -> {action-1}
  when {condition-2} -> {action-2}
  ...
  otherwise          -> {action par defaut}

qu'on peut trouver par exemple en PL/1 et en Rexx ?

------------------------------------------------------------------------------
Michel Quercia,
Laboratoire d'informatique du lycee Carnot
16 bd Thiers
21000 Dijon
France
e-mail = quercia@u-bourgogne.fr
------------------------------------------------------------------------------




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

end of thread, other threads:[~1995-11-13 20:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-11-13 13:28 test with several issues Valerie Menissier-Morain
  -- strict thread matches above, loose matches on Subject: below --
1995-11-11 13:08 quercia
1995-11-13 19:59 ` 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).