Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0000415OCamlOCaml generalpublic2001-06-28 14:312013-04-25 16:56
Reporteradministrator 
Assigned To 
PrioritynormalSeverityminorReproducibilityalways
StatusclosedResolutionfixed 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0000415: for i = 0 to max_int do () done loop ?
DescriptionFull_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 ?

TagsNo tags attached.
Attached Files

- Relationships
parent of 0005999acknowledged "for" loop not entirely optimal? 
Not all the children of this issue are yet resolved or closed.

-  Notes
(0000600)
administrator (administrator)
2001-06-28 14:48

> 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

(0000601)
administrator (administrator)
2002-11-18 11:51

Fixed 2002-11-18 by XL.


- Issue History
Date Modified Username Field Change
2005-11-18 10:13 administrator New Issue
2013-04-25 16:56 xleroy Relationship added parent of 0005999


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker