caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Unusual type declaration and Sexplib
@ 2009-02-22 20:59 Dario Teixeira
  2009-02-23 10:27 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Dario Teixeira @ 2009-02-22 20:59 UTC (permalink / raw)
  To: caml-list


Hi,

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:
sig
        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 =
struct
        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
end


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

[1] http://groups.google.com/group/fa.caml/browse_thread/thread/7552095ab859fb5f/9c4a6fd19812fbcc






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

* Re: [Caml-list] Unusual type declaration and Sexplib
  2009-02-22 20:59 Unusual type declaration and Sexplib Dario Teixeira
@ 2009-02-23 10:27 ` Jacques Garrigue
  2009-02-23 16:50   ` Dario Teixeira
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2009-02-23 10:27 UTC (permalink / raw)
  To: darioteixeira; +Cc: caml-list

From: Dario Teixeira <darioteixeira@yahoo.com>

> 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.

You program below is exactly equivalent to the following, without as.
Or si your real code something different?

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

        type t = private [< t foobar_t ]

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

        type t = t foobar_t

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

Jacques Garrigue

> module M:
> sig
>         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 =
> struct
>         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
> end


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

* Re: [Caml-list] Unusual type declaration and Sexplib
  2009-02-23 10:27 ` [Caml-list] " Jacques Garrigue
@ 2009-02-23 16:50   ` Dario Teixeira
  0 siblings, 0 replies; 3+ messages in thread
From: Dario Teixeira @ 2009-02-23 16:50 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list


Hi,

> You program below is exactly equivalent to the following,
> without as. Or is your real code something different?

You're right, the formulation without 'as' is simpler, and Sexplib handles
it just fine.  The real code adds a phantom type variable to t, and the
advantage of using 'as' was making the phantomness obvious:

type +'a t = private [< 'b node_t ] as 'b

versus

type +'a t = private [< 'a t node_t ]

But in this case, 'as' does raise more trouble than it's worth...
Anyway, issue solved -- thanks!

Best regards,
Dario Teixeira






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

end of thread, other threads:[~2009-02-23 16:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-22 20:59 Unusual type declaration and Sexplib Dario Teixeira
2009-02-23 10:27 ` [Caml-list] " Jacques Garrigue
2009-02-23 16:50   ` Dario Teixeira

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).