caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* typing problem with sexplib and mutually recursive polymorphic types
@ 2009-03-11  2:45 Yoann Padioleau
  2009-03-11  4:25 ` [Caml-list] " Markus Mottl
  0 siblings, 1 reply; 11+ messages in thread
From: Yoann Padioleau @ 2009-03-11  2:45 UTC (permalink / raw)
  To: Caml-list List


Hi, 

I tried a slightly modified version of sexplib by jane street 
on this simple file foo.ml:

    type 'a x1 = 'a * 'a
    and x2 = int x1
    and x3 = string x1
     (* with sexp *)

and succeeded to generate another file containing the sexp_of_xx code 
in a file foo2.ml:

    open Foo
    
    let rec x1_of_sexp__ =
      let _loc = "Xxx.x1"
      in
        fun _of_a ->
          function
          | Sexp.List ([ v1; v2 ]) ->
              let v1 = _of_a v1 and v2 = _of_a v2 in (v1, v2)
          | sexp -> Conv_error.tuple_of_size_n_expected _loc 2 sexp
    and x1_of_sexp _of_a sexp =
      try x1_of_sexp__ _of_a sexp
      with
      | Conv_error.No_variant_match ((msg, sexp)) -> Conv.of_sexp_error msg sexp
    and x2_of_sexp__ =
      let _loc = "Xxx.x2" in fun sexp -> x1_of_sexp Conv.int_of_sexp sexp
    and x2_of_sexp sexp =
      try x2_of_sexp__ sexp
      with
      | Conv_error.No_variant_match ((msg, sexp)) -> Conv.of_sexp_error msg sexp
    and x3_of_sexp__ =
      let _loc = "Xxx.x3" in fun sexp -> x1_of_sexp Conv.string_of_sexp sexp
    and x3_of_sexp sexp =
      try x3_of_sexp__ sexp
      with
      | Conv_error.No_variant_match ((msg, sexp)) -> Conv.of_sexp_error msg sexp
      
    let rec sexp_of_x1 _of_a (v1, v2) =
      let v1 = _of_a v1 and v2 = _of_a v2 in Sexp.List [ v1; v2 ]
    and sexp_of_x2 v = sexp_of_x1 Conv.sexp_of_int v
    and sexp_of_x3 v = sexp_of_x1 Conv.sexp_of_string v
  

But then the ocaml type system refuses to type this second file with the
error message: 
File "foo2.ml", line 22, characters 48-67:
This expression has type Sexp.t -> string but is here used with type
  Sexp.t -> int

Apparently the problem is that ocaml was not able to generalize
the type of x1_of_sexp and that it's first utilisation with an 'int'
force the x1_of_sexp to always return an 'int' instead of a 'string'
in the second case. Is there a way to rewrite this generated code
to avoid this typing problem ? Is it because of those 
 'let _loc = ... in fun ... ->"  that distrubs the ocaml type system ?



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2009-03-12  1:42 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-11  2:45 typing problem with sexplib and mutually recursive polymorphic types Yoann Padioleau
2009-03-11  4:25 ` [Caml-list] " Markus Mottl
2009-03-11  6:11   ` yoann padioleau
2009-03-11 14:20     ` Markus Mottl
2009-03-11 14:32       ` Yitzhak Mandelbaum
2009-03-11 14:44         ` Markus Mottl
2009-03-11 16:16           ` Damien Doligez
2009-03-11 16:43             ` Markus Mottl
2009-03-11 19:03               ` Till Varoquaux
2009-03-12  1:42               ` Jacques Garrigue
2009-03-11 23:08         ` Yoann Padioleau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).