Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for i = 0 to max_int do () done loop ? #2819

Closed
vicuna opened this issue Jun 28, 2001 · 2 comments
Closed

for i = 0 to max_int do () done loop ? #2819

vicuna opened this issue Jun 28, 2001 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Jun 28, 2001

Original bug ID: 415
Reporter: administrator
Status: closed
Resolution: fixed
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)
Parent of: #5999

Bug description

Full_Name: Cuihtlauac ALVARADO
Version: Objective Caml version 3.01
OS: Red Hat Linux 6.2, Kernel 2.2.19-6.2.7
Submission from: machine107.rd.francetelecom.fr (193.49.124.107)

This loops :

for i = 0 to max_int do
if i mod (max_int / 10) = 0 then Printf.printf "%i\n" i;
flush stdout
done;;

While that does not

let rec f i =
if i <> max_int then begin
if i mod (max_int / 10) = 0 then Printf.printf "%i\n" i;
flush stdout;
f (i + 1)
end in (f 0)

Am I missing something ?

@vicuna
Copy link
Author

vicuna commented Jun 28, 2001

Comment author: administrator

This loops :

for i = 0 to max_int do
if i mod (max_int / 10) = 0 then Printf.printf "%i\n" i;
flush stdout
done;;

While that does not

let rec f i =
if i <> max_int then begin
if i mod (max_int / 10) = 0 then Printf.printf "%i\n" i;
flush stdout;
f (i + 1)
end in (f 0)

Am I missing something ?

That's a known issue -- a rather amusing one, actually. Basically, the
"for" loop in the first example is compiled into:

    let i = ref 0 in
    while (!i <= max_int) do
      ...
      i := !i + 1
    done

and the test !i <= max_int is always true by definition of max_int.
Hence the infinite loop.

Notice that your second example is not equivalent to the first: it
iterates up to max_int - 1, not up to max_int, just like
"for i = 0 to max_int - 1 do done". (And the latter terminates
without problems.)

If you try to rewrite your second example so that it iterates up to
max_int included and stop, you might realize the difficulty of
correctly compiling "for" loops with max_int as upper bound...

I agree something should be done about this, but it's not completely
trivial to fix and doesn't seem too urgent.

Best regards,

  • Xavier Leroy

@vicuna
Copy link
Author

vicuna commented Nov 18, 2002

Comment author: administrator

Fixed 2002-11-18 by XL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant