-4

I have the following snippet in my program where OCaml (3.12.1) seems to take both the branches.

let new_lst1 = 
  if List.length lst1 = 0 then tmp_lst1_1
  else tmp_lst1_2
in
let new_lst2 =
  if List.length lst2 = 0 then tmp_lst2_1
  else tmp_lst2_2
in
........

The program does not terminate and when examined in ocamldebug, I see that for new_lst2 both the branches are being taken (then followed by else branch). new_lst2 gets the wrong value resulting in a non terminating loop. This happens after executing the program in debugger for a while.

I haven't found anything by searching online for OCaml. I got around this by just rewriting each if-then-else into two if-then-elses. Like let lst = if cond then lst1 else lst2 can be rewritten to let sub_lst1 = if cond then lst1 else [] and let sub_lst2 = if not cond then lst2 else [] and let lst = sub_lst1@sub_lst2

But it would nice to get to the bottom of this perplexing issue. Has anyone else encountered similar problem? Any ideas on what might be happening?

Thanks.

1 Answers1

1

This would be a code generation error, which is really unlikely but not impossible. The way I would proceed would be by looking at the generated assembly code with the -S flag. If you can show erroneous code for the above snippet, it would be convincing.

Another approach would be to make a small, self-contained bit of code that exhibits the bad behavior, and show all the code. It's not possible to deduce anything from your snippet (or at least I can't).

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
  • I have posted self contained program showing a related problem where OCaml skips over a if-then branch altogether. See: http://stackoverflow.com/questions/39262731/possible-ocaml-code-generation-bug – user5754881 Sep 01 '16 at 04:31