caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] RFH: type / consistency problem
@ 2013-05-13  9:40 Goswin von Brederlow
  2013-05-13 12:11 ` Erkki Seppala
  0 siblings, 1 reply; 3+ messages in thread
From: Goswin von Brederlow @ 2013-05-13  9:40 UTC (permalink / raw)
  To: caml-list

Hi,

I have two C types like this:

   struct Request {
     uint32_t cmd_type;
     uint32_t size;
     uint8_t  data[];
   };

   struct Reply {
     uint32_t reply_type;
     uint32_t cmd_type; // the cmd_type of the request this reply is for
     uint32_t size;
     uint8_t  data[];
   };

Note: The C types are given and they do not have a request ID or
anything that can be matched to a reply, only the cmd_type. 2 requests
with the same cmd_type must not happen concurrently.

Now in the ocaml bindings this becomes maybe:

   type request = Foo of string | Bar of int | Baz
   type cmd_type = Foo | Bar | Baz
   type msg = Error of string | Buzz of float | Burr
   type reply = { cmd_type : cmd_type; msg : msg; }

Or should I rather keep track of request on the C side, match replies
to requests and use requests in the reply type like this?

   type reply = { req : request; msg : msg; }

On top of that there is a connection between request and reply
contents. A Foo request can get an Error or Buzz while Bar/Baz can get
Error or Burr. Anything else would be invalid. It would be nice if the
type system would prevent creating a Burr reply to a Foo request.


So here is my request for your ideas:

How do I match requests to replies with that? How do I ensure that
request and cmd_type remain in-sync over time when new variants get
added? I'm wondering if there is some magic that could be used to
ensure correct types? Maybe something with GADTs? Something that would
bind requests and replies together in some way to ensure corectness.
Other tricks?

MfG
	Goswin

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

end of thread, other threads:[~2013-05-13 13:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-13  9:40 [Caml-list] RFH: type / consistency problem Goswin von Brederlow
2013-05-13 12:11 ` Erkki Seppala
2013-05-13 13:09   ` Goswin von Brederlow

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