This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

typing problem with sexplib and mutually recursive polymorphic types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2009-03-11 (02:46) From: Yoann Padioleau Subject: typing problem with sexplib and mutually recursive polymorphic types
```
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 ?

```