Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Benjamin Canou <benjamin.canou@gmail.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Strange performances
Date: Fri, 18 Jan 2008 17:55:14 +0100	[thread overview]
Message-ID: <1200675314.6345.32.camel@benjamin-laptop> (raw)
In-Reply-To: <20080118.181206.85503086.garrigue@math.nagoya-u.ac.jp>

  Hi,

Indeed, using runtime exceptions catching as a programming style may
lead to strange behaviours, but I think using the any pattern in the try
with construct was the real mistake.

So basically, to those who did not understand the problem :
This code works perfectly :

let list_of_string s =
  let rec list_of_string s i =
    try let e = s.[i] in e :: list_of_string s (succ i)
    with Invalid_argument "index out of bounds" -> []
  in list_of_string s 0

And if I had used a correct matching of exceptions in my original code :

let list_of_string s =
  let rec list_of_string s i =
    try s.[i] :: list_of_string s (succ i)
    with Invalid_argument "index out of bounds" -> []
  in list_of_string s 0

I would have noticed that the bug came from my carelessness about the
evaluation order (btw, thank you Jacques) :

Fatal error: exception Stack_overflow

In fact, the function calls itself recursively to construct the right
hand side of the list constructor until the stack is full.

With the any (_) pattern, the function returns [] for each call from the
one corresponding to the end of the string to the one causing the
overflow. So the result is correct, but it takes a time proportional to
the maximum size of the stack...
With a pattern matching the out of bounds exception, the stack overflow
is not caught and the programs exits abnormally right after the
overflow.

Jacques, if I remember well, the ocaml runtime is not able to detect
stack overflows in native code on all platforms, that's why you get a
segfault instead of a Stack overflow exception.

  Benjamin Canou.

Le vendredi 18 janvier 2008 à 18:12 +0900, Jacques Garrigue a écrit :
> From: "Till Varoquaux" <till.varoquaux@gmail.com>
> > On Jan 18, 2008 2:15 AM, Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> wrote:
> > ...
> > > By the way, on my machine your version doesn't even work in native
> > > code, I only get segfaults. This is allowed behaviour for
> > > out-of-bounds access.
> > 
> > Could you please clarify? This seems a little scary to me, I thought
> > segfaults where acceptable only when you used unsafe features (or ran
> > out of stack).
> 
> This is why I sent an erratum. The cause for the segfault was not the
> array access, but the stack overflow, which occured due to ocaml's
> peculiar evaluation order.
> Still, I maintain that intentionally raising and catching out-of-bound
> accesses is not good programming style...
> 
> Jacques Garrigue
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


  reply	other threads:[~2008-01-18 16:55 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-18  1:32 Benjamin Canou
2008-01-18  2:15 ` [Caml-list] " Jacques Garrigue
2008-01-18  2:28   ` Jacques Garrigue
2008-01-18  7:39   ` Till Varoquaux
2008-01-18  9:12     ` Jacques Garrigue
2008-01-18 16:55       ` Benjamin Canou [this message]
2008-01-18 17:05         ` Olivier Andrieu
2008-01-18 17:11           ` Jon Harrop
2008-01-18 17:43         ` Jon Harrop
2008-01-18 19:53           ` Benjamin Canou
2008-01-18 16:55       ` Edgar Friendly
2008-01-18 17:52         ` Kuba Ober
2008-01-18 17:56           ` Jon Harrop
2008-01-19  2:32         ` Jacques Garrigue
2008-01-24 22:52           ` Christophe Raffalli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1200675314.6345.32.camel@benjamin-laptop \
    --to=benjamin.canou@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox