caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Question
@ 2003-12-01 15:41 sebastien FURIC
  2003-12-01 17:48 ` Remi Vanicat
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: sebastien FURIC @ 2003-12-01 15:41 UTC (permalink / raw)
  To: OCaml Mailing list

 Hi,

 What do you think of the following code?

# type toto = Toto of int | Titi of string;;
type toto = Toto of int | Titi of string
# let test t t' = match t, t' with
  | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
-> "OK"
  | _ -> "KO";;
Characters 73-79:
Warning: this pattern is unused.
  | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
-> "OK"
                                          ^^^^^^
val test : toto -> toto -> string = <fun>
# test (Toto 0) (Toto 1);;
- : string = "KO"

 I was expecting "when" to be right distributive over "|". I find
OCaml's behaviour not very intuitive in such a situation. The correct
code is:

# let test t t' = match t, t' with
  | (Toto _ | Titi _), Toto x when x = 0 -> "OK"
  | Toto x, (Toto _ | Titi _) when x = 0 -> "OK"
  | _ -> "KO";;
val test : toto -> toto -> string = <fun>
# test (Toto 0) (Toto 1);;
- : string = "OK"

 Is there a good reason for this?

 Cheers,

 Sébastien.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Question
  2003-12-01 15:41 [Caml-list] Question sebastien FURIC
@ 2003-12-01 17:48 ` Remi Vanicat
  2003-12-01 18:04   ` sebastien FURIC
  2003-12-03 14:02 ` Damien Doligez
  2003-12-10 10:27 ` Pierre Weis
  2 siblings, 1 reply; 12+ messages in thread
From: Remi Vanicat @ 2003-12-01 17:48 UTC (permalink / raw)
  To: caml-list

"sebastien FURIC" <sebastien.furic@tni-valiosys.com> writes:

>  Hi,
>
>  What do you think of the following code?
>
> # type toto = Toto of int | Titi of string;;
> type toto = Toto of int | Titi of string
> # let test t t' = match t, t' with
>   | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
> -> "OK"
>   | _ -> "KO";;
> Characters 73-79:
> Warning: this pattern is unused.
>   | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
> -> "OK"
>                                           ^^^^^^

I can't answer to your question, but with your example, one can do a: 

# let test t t' = match t, t' with
  | ((Toto _ | Titi _), Toto 0
  | Toto 0, (Toto _ | Titi _)) when x = 0 -> "OK"
  | _ -> "KO";;

of course, this is not generalizable to any test.

-- 
Rémi Vanicat

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Question
  2003-12-01 17:48 ` Remi Vanicat
@ 2003-12-01 18:04   ` sebastien FURIC
  0 siblings, 0 replies; 12+ messages in thread
From: sebastien FURIC @ 2003-12-01 18:04 UTC (permalink / raw)
  To: OCaml Mailing list



Remi Vanicat a écrit :
> 
> "sebastien FURIC" <sebastien.furic@tni-valiosys.com> writes:
> 
> >  Hi,
> >
> >  What do you think of the following code?
> >
> > # type toto = Toto of int | Titi of string;;
> > type toto = Toto of int | Titi of string
> > # let test t t' = match t, t' with
> >   | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
> > -> "OK"
> >   | _ -> "KO";;
> > Characters 73-79:
> > Warning: this pattern is unused.
> >   | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
> > -> "OK"
> >                                           ^^^^^^
> 
> I can't answer to your question, but with your example, one can do a:
> 
> # let test t t' = match t, t' with
>   | ((Toto _ | Titi _), Toto 0
>   | Toto 0, (Toto _ | Titi _)) when x = 0 -> "OK"
>   | _ -> "KO";;
> 
> of course, this is not generalizable to any test.

 I could have even write:

# let test t t' = match t, t' with
  | _, Toto 0 | Toto 0, _ -> "OK"
  | _ -> "KO";;

 The point is that my example is extracted from a "serious" project
where I wrote something like:

| ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when f(x) = 0
-> ...

 and where type toto has more than 2 constructors.

 Cheers,

 Sébastien.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Question
  2003-12-01 15:41 [Caml-list] Question sebastien FURIC
  2003-12-01 17:48 ` Remi Vanicat
@ 2003-12-03 14:02 ` Damien Doligez
  2003-12-10 10:27 ` Pierre Weis
  2 siblings, 0 replies; 12+ messages in thread
From: Damien Doligez @ 2003-12-03 14:02 UTC (permalink / raw)
  To: OCaml Mailing list

On Monday, December 1, 2003, at 04:41 PM, sebastien FURIC wrote:

> # let test t t' = match t, t' with
>   | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
> -> "OK"
>   | _ -> "KO";;
> Characters 73-79:
> Warning: this pattern is unused.
>   | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
> -> "OK"
>                                           ^^^^^^
[...]

>  I was expecting "when" to be right distributive over "|".

That notion doesn't make sense because "when" is not a pattern operator.
If you "distribute" when over |, you get this:

  # let test t t' = match t, t' with
    | ((Toto _ | Titi _), Toto x when x = 0
       | Toto x, (Toto _ | Titi _) when x = 0)
      -> "OK"
    | _ -> "KO";;

which is nonsense, from O'Caml's point of view.

-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Question
  2003-12-01 15:41 [Caml-list] Question sebastien FURIC
  2003-12-01 17:48 ` Remi Vanicat
  2003-12-03 14:02 ` Damien Doligez
@ 2003-12-10 10:27 ` Pierre Weis
  2003-12-10 15:53   ` skaller
  2 siblings, 1 reply; 12+ messages in thread
From: Pierre Weis @ 2003-12-10 10:27 UTC (permalink / raw)
  To: sebastien FURIC; +Cc: caml-list

[...]
>  I was expecting "when" to be right distributive over "|". I find
> OCaml's behaviour not very intuitive in such a situation.
[...]

The when construct introduces a guard to a pattern matching
clause. This means that a when construct is global to the entire
pattern of the clause it appears in; it means in particular that no
when construct can be nested into a pattern and the question of its
distributivity wrt any other construct is pointless.

>  Is there a good reason for this?

Yes: simplicity, complexity invariants preservation for runtime
pattern analysis decisions even in the presence of when clauses,
easier understanding of the compiler warnings, simplicity and
well-definedness semantics (in particular the desired invariance wrt
the order of evaluation).

>  Cheers,
> 
>  Sébastien.

Best regards,

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
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Question
  2003-12-10 10:27 ` Pierre Weis
@ 2003-12-10 15:53   ` skaller
  2003-12-11  9:52     ` Luc Maranget
  0 siblings, 1 reply; 12+ messages in thread
From: skaller @ 2003-12-10 15:53 UTC (permalink / raw)
  To: Pierre Weis; +Cc: caml-list

On Wed, 2003-12-10 at 21:27, Pierre Weis wrote:
> [...]
> >  I was expecting "when" to be right distributive over "|". I find
> > OCaml's behaviour not very intuitive in such a situation.
> [...]
> 
> The when construct introduces a guard to a pattern matching
> clause. This means that a when construct is global to the entire
> pattern of the clause it appears in; it means in particular that no
> when construct can be nested into a pattern and the question of its
> distributivity wrt any other construct is pointless.
> 
> >  Is there a good reason for this?
> 
> Yes: simplicity, complexity invariants preservation for runtime
> pattern analysis decisions even in the presence of when clauses,
> easier understanding of the compiler warnings, simplicity and
> well-definedness semantics (in particular the desired invariance wrt
> the order of evaluation).

Whoa! Can you say that slower? Felix allows nested when clauses
in patterns (however it doesn't allow alternatives yet). 
The current implementation is unoptimised, 
I just evaluate the match of each pattern in turn until one matches.

One small difference .. the argument of a when clause is
an expression .. and felix expressions are purely functional
so there is no issue of order of evaluation changing anything.

So, what have I lost?


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Question
  2003-12-10 15:53   ` skaller
@ 2003-12-11  9:52     ` Luc Maranget
  2003-12-11 14:20       ` skaller
  0 siblings, 1 reply; 12+ messages in thread
From: Luc Maranget @ 2003-12-11  9:52 UTC (permalink / raw)
  To: skaller; +Cc: Pierre Weis, caml-list


> > The when construct introduces a guard to a pattern matching
> > clause. This means that a when construct is global to the entire
> > pattern of the clause it appears in; it means in particular that no
> > when construct can be nested into a pattern and the question of its
> > distributivity wrt any other construct is pointless.
> > 
> > >  Is there a good reason for this?
> > 
> > Yes: simplicity, complexity invariants preservation for runtime
> > pattern analysis decisions even in the presence of when clauses,
> > easier understanding of the compiler warnings, simplicity and
> > well-definedness semantics (in particular the desired invariance wrt
> > the order of evaluation).
> 
> Whoa! Can you say that slower? Felix allows nested when clauses
> in patterns (however it doesn't allow alternatives yet). 
> The current implementation is unoptimised, 
> I just evaluate the match of each pattern in turn until one matches.
> 
> One small difference .. the argument of a when clause is
> an expression .. and felix expressions are purely functional
> so there is no issue of order of evaluation changing anything.
> 
> So, what have I lost?

In some sense order of evaluation may not be an issue
(at worst it can be left unspecified!).
That said.

  When clauses inside patterns would for sure much complicate optimized
compilation and maybe diagnostics. So basically you loose optimised
compilation and simple diagnostics.


  Moreover at the moment the AST types for patterns and expression are
not mutually recursive. Expressions depend on patterns but not the other way
round.

As far as I know, The compiler take advantage of this in many places,
with code that more or less looks as follows

let rec treat_pat = blabala
;;

let rec treat_expr = blabla ... treat_pat ...
;;


Making patterns and expressions mutually dependant, would perhaps,
break some untold assumption, hidden invariant etc.

To conclude adopting the Felix way in Ocaml is by no mean a trivial
change and benefits are unclear, how many programs do realy use this
feature ?


Cheers,
-- 
Luc Maranget

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Question
  2003-12-11  9:52     ` Luc Maranget
@ 2003-12-11 14:20       ` skaller
  2003-12-11 16:56         ` Luc Maranget
  0 siblings, 1 reply; 12+ messages in thread
From: skaller @ 2003-12-11 14:20 UTC (permalink / raw)
  To: Luc Maranget; +Cc: Pierre Weis, caml-list

On Thu, 2003-12-11 at 20:52, Luc Maranget wrote:

> To conclude adopting the Felix way in Ocaml is by no mean a trivial
> change and benefits are unclear, how many programs do realy use this
> feature ?

Well, none in Ocaml because it isn't present :-)
I have occasionally wanted this, but there is always
a workaround.

Basically, I think it would be useful in the following
situation:

	match x with
	| A
	| (B (j,k) when j=k) ->
	| B (j,k) ->

Without a nested when clause, you could code

	let handler () = ... in
	match x with
	| A -> handler ()
	| B (j,k) when j = k -> handler ()
	| B (j,k) -> ...

delocalising the handler. (I have matches several pages long,
so this is annoying).

Also useful would be:

	match x with
	| A i
	| B (i,k) when i = k -> ... i ..

though I can't see a way to generalise that. 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Question
  2003-12-11 14:20       ` skaller
@ 2003-12-11 16:56         ` Luc Maranget
  0 siblings, 0 replies; 12+ messages in thread
From: Luc Maranget @ 2003-12-11 16:56 UTC (permalink / raw)
  To: skaller; +Cc: Luc Maranget, Pierre Weis, caml-list

> On Thu, 2003-12-11 at 20:52, Luc Maranget wrote:
> 
> > To conclude adopting the Felix way in Ocaml is by no mean a trivial
> > change and benefits are unclear, how many programs do realy use this
> > feature ?
> 
> Well, none in Ocaml because it isn't present :-)
> I have occasionally wanted this, but there is always
> a workaround.
> 
> Basically, I think it would be useful in the following
> situation:
> 
> 	match x with
> 	| A
> 	| (B (j,k) when j=k) -> e1
> 	| B (j,k) -> e2
Hum, I think that you assume this is correct 
provided j and k are not present in e1.

However, this code does not follow the current rules of
bindings in or-pattern: j and k are bound only in the right argument of
the or pattern.

Since using j and k in ``when j=k'' seems legitimate, this means that
the rules of bindings patterns also need to be changed...


> 
> 	match x with
> 	| A i
> 	| B (i,k) when i = k -> ... i ..
> 
Same binding problem, even more clear.
the scoping rules for variable bound in patterns become more complicated.



-- 
Luc Maranget

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] question
  2005-06-08 10:06 question  dsingh01
@ 2005-06-08 15:13 ` Damien Bobillot
  0 siblings, 0 replies; 12+ messages in thread
From: Damien Bobillot @ 2005-06-08 15:13 UTC (permalink / raw)
  To: dsingh01; +Cc: caml-list


[-- Attachment #1.1: Type: text/plain, Size: 551 bytes --]


Le 8 juin 05 à 12:06, dsingh01 a écrit :

> j'ai envoye un mail a
> ocaml_beginners@yahoogroups.com
> pour qu'on m'aide a debugger des fonctions et je voudrais savoir  
> quand j'aurais une reponse.
> merci de me repondre

Le temps que quelqu'un le fasse : les mailings listes ne sont pas des  
hotlines, les réponses ne viennent que lorsque quelqu'un décide d'en  
faire une.

Par ailleurs, n'étant pas abonné à ocaml_beginners@yahoogroups.com  
comme de nombreuses personnes ici, je ne pourrais pas t'aider

-- 
Damien Bobillot


[-- Attachment #1.2: Type: text/html, Size: 1361 bytes --]

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 2375 bytes --]

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

* Re: [Caml-list] Question
  2005-05-23  6:54 Question  dsingh01
  2005-05-23  7:40 ` [Caml-list] Question Remi Vanicat
@ 2005-05-23 12:21 ` Jacques Carette
  1 sibling, 0 replies; 12+ messages in thread
From: Jacques Carette @ 2005-05-23 12:21 UTC (permalink / raw)
  To:  dsingh01, caml-list

Je ne peux pas aider directement, mais il est quand meme bon de noter que le packetage 'combstruct' pour le logiciel 
Maple, fait tout ceci et meme beaucoup plus.  Il peut etre de l'utiliser comme point de comparaison.  Puisque ce 
logicial (combstruct) est issu du projet ALGO a l'INRIA, il devrait etre possible d'obtenir de l'aide de ca cote 
aussi.

Jacques

" dsingh01" <" dsingh01"@etudiant.univ-mlv.fr> wrote:
> Bonjour;
> pour recevoir une aide pour la réalisation d'un projet de caml,j'ai envoyé un mail à cette adresse 
> ocaml_beginners@yahoogroups.com <mailto:ocaml_beginners@yahoogroups.com>
> mais je n'ai pas eu de réponse .
> Pouvez-vous faire en sorte que je recoive une réponse ou alors pouvez-vous m'aidez pour le projet que voici:
> 


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

* Re: [Caml-list] Question
  2005-05-23  6:54 Question  dsingh01
@ 2005-05-23  7:40 ` Remi Vanicat
  2005-05-23 12:21 ` Jacques Carette
  1 sibling, 0 replies; 12+ messages in thread
From: Remi Vanicat @ 2005-05-23  7:40 UTC (permalink / raw)
  To: dsingh01; +Cc: caml-list

Le 23/05/05, dsingh01"@etudiant.univ-mlv.fr  dsingh01<"> a écrit :
>  Bonjour;
>  pour recevoir une aide pour la réalisation d'un projet de caml,j'ai envoyé
> un mail à cette adresse ocaml_beginners@yahoogroups.com
>  mais je n'ai pas eu de réponse .

ocaml_begginers est une liste qu'on ne peut acceder que s'y on est abboner. Voir
http://groups.yahoo.com/group/ocaml_beginners

>  Pouvez-vous faire en sorte que je recoive une réponse ou alors pouvez-vous
> m'aidez pour le projet que voici:
>  

Le projet que l'on vous propose m'a l'aire détailler. N'esperez pas
qu'on le résolve pour vous, essayez un peu de le faire tout seul. Et
si vos avez des questions plus spécifique, posez les, mais pas avant
d'avoir relut votre cours et réfléchis un peu.

In english: a student ask us to do its homework, I reaply that it
should do it by himself.


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

end of thread, other threads:[~2005-06-08 15:19 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-01 15:41 [Caml-list] Question sebastien FURIC
2003-12-01 17:48 ` Remi Vanicat
2003-12-01 18:04   ` sebastien FURIC
2003-12-03 14:02 ` Damien Doligez
2003-12-10 10:27 ` Pierre Weis
2003-12-10 15:53   ` skaller
2003-12-11  9:52     ` Luc Maranget
2003-12-11 14:20       ` skaller
2003-12-11 16:56         ` Luc Maranget
2005-05-23  6:54 Question  dsingh01
2005-05-23  7:40 ` [Caml-list] Question Remi Vanicat
2005-05-23 12:21 ` Jacques Carette
2005-06-08 10:06 question  dsingh01
2005-06-08 15:13 ` [Caml-list] question Damien Bobillot

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