Version française
Home     About     Download     Resources     Contact us    
Browse thread
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: -- (:)
From: Markus Mottl <markus.mottl@g...>
Subject: Re: [Caml-list] typing problem with sexplib and mutually recursive polymorphic types
On Tue, Mar 10, 2009 at 22:45, Yoann Padioleau <padator@wanadoo.fr> wrote:
> 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 ?

This has to do with recursive type definitions, not with generating
closures with _loc.  The following is similar and does not compile
either:

  let rec foo () = []
  and bar () = "asdf" :: foo ()
  and bla () = 42 :: foo ()

These definitions are clearly sound, but the type checker doesn't get
it.  Note, however, that the following works:

  let foo () = []
  let rec bar () = "asdf" :: foo ()
  and bla () = 42 :: foo ()

You can apply the same trick in your specific example by simply moving
the type definition of x1 out of the cycle with x2 and x3.

Cheers,
Markus

-- 
Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com