caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Strange syntax behavior
@ 2004-06-01 17:13 John Goerzen
  2004-06-01 17:30 ` Jon Harrop
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: John Goerzen @ 2004-06-01 17:13 UTC (permalink / raw)
  To: caml-list

Hello,

I have found some mysterious bugs in a couple of my programs.  Here is a
test case to illustrate what is going on:

# let p = print_endline;;      
# if true then p "yes" else p "no"; p "done";;
yes
done
- : unit = ()
# try p "test" with Not_found -> p "exc"; p "done";;
test
- : unit = ()
# try raise Not_found with Not_found -> p "exc"; p "done";;
exc
done
- : unit = ()
# try p "test" with Not_found -> begin p "exc"; end; p "done";;
test
- : unit = ()

In the case of if...then...else, the else clause appears to consume only
the first statement following.  With try..with, the with clause appears
to consume everything it possibly can, despite even attempts to stop
that with a begin..end clause.

Is this a bug or a feature?  If a feature, why is this so?  the
try..with behavior seems highly misleading.

-- John

-------------------
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] 7+ messages in thread

* Re: [Caml-list] Strange syntax behavior
  2004-06-01 17:13 [Caml-list] Strange syntax behavior John Goerzen
@ 2004-06-01 17:30 ` Jon Harrop
  2004-06-01 17:40 ` Kenneth Knowles
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Jon Harrop @ 2004-06-01 17:30 UTC (permalink / raw)
  To: caml-list


> In the case of if...then...else, the else clause appears to consume only
> the first statement following.

Yes, it is looking for an expression:

if true then
  p "yes"
else
  p "no";
p "done";;

You may have meant:

if true then
  p "yes"
else
  begin
    p "no";
    p "done"
  end;;

> With try..with, the with clause appears 
> to consume everything it possibly can, despite even attempts to stop
> that with a begin..end clause.

Yes, it is looking for pattern matches.

try
  p "test"
with
  Not_found ->
    begin
      p "exc";
    end;
    p "done";;

You probably meant:

begin
  try
    p "test"
  with
    Not_found ->
      p "exc";
end;
p "done";;

> Is this a bug or a feature?  If a feature, why is this so?  the
> try..with behavior seems highly misleading.

I got a feeling for how these sorts of things work by looking at the 
indentation style (in emacs). It's fairly obvious that pattern matches 
(including "try ... with ..." as well as just "match ... with ...") need 
either brackets or "begin ... end" to nest them as, without this, the 
compiler couldn't tell which pattern was matching which match.

Cheers,
Jon.

-------------------
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] 7+ messages in thread

* Re: [Caml-list] Strange syntax behavior
  2004-06-01 17:13 [Caml-list] Strange syntax behavior John Goerzen
  2004-06-01 17:30 ` Jon Harrop
@ 2004-06-01 17:40 ` Kenneth Knowles
  2004-06-01 17:44 ` Christophe TROESTLER
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Kenneth Knowles @ 2004-06-01 17:40 UTC (permalink / raw)
  To: John Goerzen; +Cc: caml-list

On Tue, Jun 01, 2004 at 12:13:54PM -0500, John Goerzen wrote:
> In the case of if...then...else, the else clause appears to consume only
> the first statement following.  With try..with, the with clause appears
> to consume everything it possibly can, despite even attempts to stop
> that with a begin..end clause.

You'll need (try raise Not_found with Not_found -> p "exc"); p "done";;

I'd guess the reason the precedence is such is to require as few parentheses as
possible.  Most uses of try or match are not in a sequence of expressions, while
many if-then-else expressions are.  Essentially, matching constructs and if
constructs are not analogous.   Examples with clear indentation:

	if true then
		p "yes"
	else
		p "no";
	p "done"

	if true then
		p "yes"
	else (
		p "no";
		p "done"
	)

	try raise Not_found with
		| Not_found ->
			p "exc";
			p "done"


	(try raise Not_found with
		| Not_found -> p "exec");
	p "done"


Kenn

-------------------
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] 7+ messages in thread

* Re: [Caml-list] Strange syntax behavior
  2004-06-01 17:13 [Caml-list] Strange syntax behavior John Goerzen
  2004-06-01 17:30 ` Jon Harrop
  2004-06-01 17:40 ` Kenneth Knowles
@ 2004-06-01 17:44 ` Christophe TROESTLER
  2004-06-01 17:55 ` skaller
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Christophe TROESTLER @ 2004-06-01 17:44 UTC (permalink / raw)
  To: jgoerzen; +Cc: caml-list

On Tue, 1 Jun 2004, John Goerzen <jgoerzen@complete.org> wrote:
>
> In the case of if...then...else, the else clause appears to consume
> only the first statement following.  With try..with, the with clause
> appears to consume everything it possibly can,

You can think "with ... -> ..." almost as "fun ... -> ...", so for the
latter you certainly expect all that follows to be part of the body of
the function.  This is also the same for the "match ... with ... ->
..." close.

> despite even attempts to stop that with a begin..end clause.

Try this:

  (try p "test" with Not_found -> p "exc"); p "done"

or

  begin
    try p "test"
    with Not_found -> p "exc"
  end;
  p "done"

ChriS

-------------------
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] 7+ messages in thread

* Re: [Caml-list] Strange syntax behavior
  2004-06-01 17:13 [Caml-list] Strange syntax behavior John Goerzen
                   ` (2 preceding siblings ...)
  2004-06-01 17:44 ` Christophe TROESTLER
@ 2004-06-01 17:55 ` skaller
  2004-06-02  8:31 ` Hendrik Tews
  2004-06-02  8:35 ` Richard Jones
  5 siblings, 0 replies; 7+ messages in thread
From: skaller @ 2004-06-01 17:55 UTC (permalink / raw)
  To: John Goerzen; +Cc: caml-list

On Wed, 2004-06-02 at 03:13, John Goerzen wrote:

> Is this a bug or a feature?  If a feature, why is this so?  the
> try..with behavior seems highly misleading.

This is a consequence of a lot of the syntactic elements
being prefix form, that is, having no trailing terminator:
if/then/else and try/with and let/in and match/with
are all like this, whilst while/do/done and for/do/done 
are terminated.

Prefix forms have to have a definite precedence.
No choice is always what you want.

Felix uses closed constructions mainly so you have to write:
if/then/else/endif and match .. endmatch etc.
[Except i had to allow let/in .. :]

Whilst this is more obvious than the Ocaml syntax,
it does make heavily nested expressions more verbose.

Summary: a fairly arbitrary choice. Use begin/end
around try/with if in doubt. You will get used to it.
However it is a bit of an obstacle to learning Ocaml:
after, what, 8 years Ocaml programming I still have
no idea what the precedences of everything are.

And I have no idea how to read C type declarations
after 20+ years .. I just use typedefs .. it was 
interesting to write a parser/emitter for them
recently, I actually almost learned the C rules..

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
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] 7+ messages in thread

* Re: [Caml-list] Strange syntax behavior
  2004-06-01 17:13 [Caml-list] Strange syntax behavior John Goerzen
                   ` (3 preceding siblings ...)
  2004-06-01 17:55 ` skaller
@ 2004-06-02  8:31 ` Hendrik Tews
  2004-06-02  8:35 ` Richard Jones
  5 siblings, 0 replies; 7+ messages in thread
From: Hendrik Tews @ 2004-06-02  8:31 UTC (permalink / raw)
  To: caml-list

John Goerzen <jgoerzen@complete.org> writes:

   [...]
   In the case of if...then...else, the else clause appears to consume only
   the first statement following.  With try..with, the with clause appears
   to consume everything it possibly can, despite even attempts to stop
   that with a begin..end clause.
   
   Is this a bug or a feature?  

It's definitely a feature, even documented! See the preverence
table just before Section 6.7.1 in
http://caml.inria.fr/ocaml/htmlman/manual015.html

Bye,

Hendrik

-------------------
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] 7+ messages in thread

* Re: [Caml-list] Strange syntax behavior
  2004-06-01 17:13 [Caml-list] Strange syntax behavior John Goerzen
                   ` (4 preceding siblings ...)
  2004-06-02  8:31 ` Hendrik Tews
@ 2004-06-02  8:35 ` Richard Jones
  5 siblings, 0 replies; 7+ messages in thread
From: Richard Jones @ 2004-06-02  8:35 UTC (permalink / raw)
  To: John Goerzen; +Cc: caml-list

[Question already adequately answered by everyone else.  Just to add ...]

I use tuareg-mode in emacs, with the configuration below.  It does a
pretty good job of indenting code to show the logical structure.

(setq auto-mode-alist (cons '("\\.ml\\w?" . tuareg-mode) auto-mode-alist))
(autoload 'tuareg-mode "tuareg" "Major mode for editing Caml code" t)
(autoload 'camldebug "camldebug" "Run the Caml debugger" t)
(if (and (boundp 'window-system) window-system)
    (require 'font-lock))
(add-hook 'tuareg-mode-hook
          '(lambda ()
             (setq tuareg-in-indent 0)
                                        ; no indentation after `in' keywords
             ))

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
MONOLITH is an advanced framework for writing web applications in C, easier
than using Perl & Java, much faster and smaller, reusable widget-based arch,
database-backed, discussion, chat, calendaring:
http://www.annexia.org/freeware/monolith/

-------------------
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] 7+ messages in thread

end of thread, other threads:[~2004-06-02  8:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-01 17:13 [Caml-list] Strange syntax behavior John Goerzen
2004-06-01 17:30 ` Jon Harrop
2004-06-01 17:40 ` Kenneth Knowles
2004-06-01 17:44 ` Christophe TROESTLER
2004-06-01 17:55 ` skaller
2004-06-02  8:31 ` Hendrik Tews
2004-06-02  8:35 ` Richard Jones

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