I finally managed to reproduce with a sample code that contains non- function stuff and I will post the update in a different thread.
On Jan 6, 5:04 pm, DelPhiNus <[email protected]> wrote: > Great, I have been able to fix the problems! Now I fully aware that I > can use polymorphism everywhere. > Actually the biggest problem was to search the cause of the > compilation problem in my code... > > By the way, I have a crazy behavior at runtime with the pattern > matching of a tuple with variant and string. > I have not been able to reproduce with a simple code... But here is > how it looks like: > > type event = StartElement | EndElement | Text | EndOfDocument > > let read_section_while source test f a = > let rec handle_next source a = > match source with > | [] -> failwith "Unexpected end of source" > | hd :: tl -> > begin match hd with > | EndOfDocument, _ -> failwith "Unexpected end > of document" > | Text, _ -> > handle_next tl (f a "Text") > | StartElement, "titi" -> handle_next tl (f a > "Start titi") > | EndElement, "tata" -> handle_next tl (f a > "End tata") > | ev -> if test ev then handle_next tl a else a > end > in > handle_next source a > > let source = [ > (StartElement, "toto"); > (StartElement, "titi"); > (EndElement, "titi"); > (StartElement, "tata"); > (EndElement, "tata"); > (EndElement, "toto"); > (EndOfDocument, "dummy") > ];; > > read_section_while source ((<>) (EndElement, "toto")) (fun a x -> x :: > a) [] > > This works without any exception with the ocaml interpreter, but my > program throws exception "Unexpected end of document" (note: my > programs is built with ocamlopt). > > The same code works if I change just one line in my implementation: > | ev, name -> if test (ev, name) then handle_next tl > a else a > Instead of: > | ev -> if test ev then handle_next tl a else a > I have also tried this and it failed: > | (ev, name) as xml -> if test xml then handle_next > tl a else a > > This kind of problem has no sense to me and I am really worried about > that :( > The crazy thing is that the pattern doesn't even match today, I have > added Print instructions in the test function to check that. > Last time the code was working if I was modifying slightly the pattern > matching or the test function by extracting values from the tuple. > Are you aware of specific pattern matching that is likely to fail? > > Thanks > > On Jan 5, 11:27 pm, DelPhiNus <[email protected]> wrote: > > > > > > > > > Thanks. > > > Just to let you know. My problem was just about nesting and not about > > recursion. > > > The local polymorphic function was used in a match case. > > But I just checked the following code and it worked: > > let test str = > > let myfun f x = f x in > > match str with > > | "case1" -> string_of_int (myfun ((+) 1) 2) > > | "case2" -> List.hd ((fun x -> "e" :: []) []) > > | _ -> "default case" > > in test "case2" > > > Thankshttp://codepad.orgforproviding an online interpreter :) > > > So there might be something else wrong in my code. > > > On 5 jan, 23:03, Guillaume Yziquel <[email protected]> > > wrote: > > > > Le Thursday 05 Jan 2012 à 13:35:37 (-0800), DelPhiNus a écrit : > > > > > Actually last time I wrote pseudo-code without checking if it was > > > > correct... > > > > Yes the two other functions should be nested. > > > > > I don't know if something was wrong in my real code but somehow, the > > > > type of f x was inferred for the first use of this function, then I > > > > got an error later mentioning a type incompatibility showing the > > > > expected type that was inferred earlier. > > > > I think you should post a clear example so that I do not get confused > > > and do not get things wrong. > > > > > Regarding this: > > > > > Actually I am not very keen in writing a top-level function with all > > > > > necessary arguments each time I need to do some generic processing > > > > > that is specific to one master function. > > > > > I just mean that the language sucks if it provides the polymorphic > > > > capability only at the top level. > > > > No, the language provides a whole lot of possibilities both for > > > polymorphism and for recursion. You just have to pick the right one at > > > the right time. > > > > The "let ... in" construct allows you to use polymorphism inside nested > > > code. Typically, the fact that > > > > let _ = let f x = x in (f 1), (f []) > > > > typechecks properlies means that even though f is 'a -> 'a, the 'a type > > > gets instantiated separately each time in 'f 1' and 'f []'. So you do > > > have polymorphism. > > > > However, > > > > let rec f x = > > > let _ = f 1 in > > > let _ = f [] in > > > () > > > > will not typecheck, because inside the declaration body, f is assumed to > > > be 'a -> _ , and 'a gets unified with int and also with _ list. So this > > > doesn't typecheck. > > > > However, you have polymorphic recursion since recent versions of OCaml. > > > > type 'a t = A of 'a list t > > > let rec f : 'a. 'a t -> _ = fun (A x) -> f x > > > > does compile. As f's type is explicitely quantified, OCaml knows how to > > > typecheck properly the nested f as _ list t -> _. > > > > If you forget the 'a. prefix, it does not typecheck anymore: > > > > let rec f : 'a t -> _ = fun (A x) -> f x > > > > fails to typecheck miserably with the following error: > > > > Error: This expression has type 'a t -> 'b > > > but an expression was expected of type 'a list t -> 'b > > > > There's lots of way to make polymorphism and recursion nicely cohabit, > > > but they are not always the most obvious or syntactically light. > > > > > I will try your code tomorrow... and check if there was something > > > > wrong in my code. > > > > > Thanks > > > > -- > > > Guillaume Yziquelhttp://yziquel.homelinux.org -- 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
