Version française
Home     About     Download     Resources     Contact us    

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

Browse thread
Unusual type declaration and Sexplib
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Dario Teixeira <darioteixeira@y...>
Subject: Unusual type declaration and Sexplib


I'm having some trouble serialising via Sexplib a data structure defined
recursively.  Consider module M defined below; note how type foobar_t includes
a "with sexp" declaration, telling the Sexplib syntax extension to create
(de)serialisers automatically.  However, type t cannot rely on that automatism,
because type declarations with the "as" operator are not fully supported.
Therefore, I need to create the (de)serialisers for this type manually.

module M:
        type 'a foobar_t =
                [ `Foo of int
                | `Bar of 'a list
                ] with sexp

        type t = private [< 'a foobar_t ] as 'a

        val foo: int -> t
        val bar: t list -> t
end =
        type 'a foobar_t =
                [ `Foo of int
                | `Bar of 'a list
                ] with sexp

        type t = 'a foobar_t as 'a

        let foo x = `Foo x
        let bar x = `Bar x

So basically the problem is creating the functions with the following signatures:

val sexp_of_t: t -> Sexplib.Sexp.t
val t_of_sexp: Sexplib.Sexp.t -> t

Using as base the automatically created functions whose signatures are as follows:

val sexp_of_foobar_t: ('a -> Sexplib.Sexp.t) -> 'a foobar_t -> Sexplib.Sexp.t
val foobar_t_of_sexp: (Sexplib.Sexp.t -> 'a) -> Sexplib.Sexp.t -> 'a foobar_t

But in practice, I'm having trouble seeing how these later functions can
be used.  The problem lies in their recursive definition: how would I
break it?  (And yes, I realise that the definition of type t is unusual,
but it's extremely convenient for avoiding a lot of explicit annotations
and coercions [1]).

Thanks for your help!
Best regards,
Dario Teixeira