I cannot reproduce this behavior with ocaml 3.12.1 (all pairs of
lines printed are the same).
The lambda code from ocamlc for read_section_while I get is
below, but it looks fine to me. In particular, next and name are
called once prior to any matching code.
All preferences about specifying evaluation order aside, if Chris
King's version does not work as expected, then eta-value for
tuples is being broken, which seems pretty bad to me.
Can others reproduce this with recent versions or is it already fixed?
Cheers, Josh
read_section_while/1047
(function source/1048 test/1049 f/1050 a/1051
(letrec
(handle_next/1052
(function a/1053
(let
(match/1073 (apply next/1043 source/1048)
match/1074 (apply name/1041 0a))
(catch
(switch* match/1073
case int 0:
(if (caml_string_notequal match/1074 "titi") (exit 2)
(apply handle_next/1052
(apply f/1050 a/1053 "Start titi")))
case int 1:
(if (caml_string_notequal match/1074 "tata") (exit 2)
(apply handle_next/1052
(apply f/1050 a/1053 "End tata")))
case int 2:
(apply handle_next/1052 (apply f/1050 a/1053 "Text"))
case int 3:
(apply (field 1 (global Pervasives!))
"Unexpected end of document"))
with (2)
(let (foo/1056 (makeblock 0 match/1073 match/1074))
(seq (apply print_event/1039 (field 0 foo/1056))
(apply (field 29 (global Pervasives!))
(field 1 foo/1056))
(apply print_event/1039 match/1073)
(apply (field 29 (global Pervasives!)) match/1074)
(apply (field 30 (global Pervasives!)) 0a)
(if (apply test/1049 foo/1056)
(apply handle_next/1052 a/1053) a/1053)))))))
(apply handle_next/1052 a/1051)))
Chris King <colanderman@...> writes:
>
> This looks like a bug in OCaml (which is in my experience rare!), and
> an insidious one at that.
>
> I defined:
>
> let print_event = function
> | StartElement -> print_string "StartElement "
> | EndElement -> print_string "EndElement "
> | Text -> print_string "Text "
> | EndOfDocument -> print_string "EndOfDocument "
>
> and changed the failing case to:
>
> | (ev, name) as foo -> begin
> print_event (fst foo); print_endline (snd foo);
> print_event ev; print_endline name;
> print_newline ();
> if test foo then handle_next a else a
> end
>
> and look at the garbage OCaml prints out:
>
> StartElement toto
> StartElement toto
>
> StartElement titi
> EndElement titi
>
> EndOfDocument toto
> EndElement toto
>
> Exception: Failure "Unexpected end of source".
>
> (Each pair of lines should be identical.) This is in 3.11.2 on Ubuntu
> x86_64, and happens with both bytecode and compiled code.
>
> See if you can shrink down your example while maintaining this bug and
> then file a bug report: http://caml.inria.fr/mantis/my_view_page.php
>
> On Fri, Jan 6, 2012 at 11:41 AM, DelPhiNus <delphin.lecucq@...> wrote:
> > Considering the sample code below that contains non-functional stuff
> > that mimics a simple XmlScanner from my company.
> >
> > type event = StartElement | EndElement | Text | EndOfDocument
> >
> > let cur_name = ref None
> > let name () = match !cur_name with None -> failwith "None" | Some name
> > -> name
> > let next it =
> > match !it with
> > | [] -> failwith "Unexpected end of source"
> > | hd :: tl -> it := tl; cur_name := Some (snd hd); fst hd
> >
> > let read_section_while source test f a =
> > let rec handle_next a =
> > match next source, name () with
> > | EndOfDocument, _ -> failwith "Unexpected end of
document"
> > | Text, _ -> handle_next (f a
"Text")
> > | StartElement, "titi" -> handle_next (f a "Start titi")
> > | EndElement, "tata" -> handle_next (f a "End tata")
> > | xml -> if test xml then
handle_next a else a
> > in
> > handle_next a
> >
> > let source = [
> > (StartElement, "toto");
> > (StartElement, "titi");
> > (EndElement, "titi");
> > (StartElement, "tata");
> > (EndElement, "tata");
> > (EndElement, "toto");
> > (EndOfDocument, "dummy")
> > ];;
> >
> > read_section_while (ref source) ((<>) (EndElement, "toto")) (fun a x -
> >> x :: a) [];;
> >
> > ==== END SAMPLE ===
> >
> > I have an exception "Unexpected end of source" while the code was
> > supposed to stop normally on (EndElement, "toto")
> >
> > The same code works as expected if I just change slightly the default
> > pattern:
> > | ev, name -> if test (ev, name) then handle_next a else a
> > instead of:
> > | xml -> if test xml then handle_next a else a
> >
> > Again, the same code works if I make the tuple resulting from (next
> > it, name ()) just before the pattern matching:
> > let x = next source, name () in match next x with
> > instead of:
> > match next source, name () with
> >
> > From my point of view, I thought that there was no fundamental
> > difference between the code that works and the code that fails. Maybe
> > I missed something...
> >
> > I am using Ocaml 3.11.0 and I don't plan to update for the moment
> > since I am experiencing 3rd party library link problems as soon as
> > something change in the Ocaml+MinGW+Cygwin+FlexDLL toolchain.
--
You received this message because you are subscribed to the Google Groups
"ocaml-developer" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/ocaml-developer?hl=en
For other OCaml forums, see http://caml.inria.fr/resources/forums.en.html