From: Jeremy Yallop <yallop@gmail.com>
To: "Markus Weißmann" <markus.weissmann@in.tum.de>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Clever typing for client-server communication?
Date: Fri, 24 Jul 2015 21:25:17 +0100 [thread overview]
Message-ID: <CAAxsn=H62R8eooPkgGJwsQ6Mozi+HuLaE95UzOBbxLiF1fd0nw@mail.gmail.com> (raw)
In-Reply-To: <c44d210f0c2b3f58a45e95a8d81ddb2d@in.tum.de>
On 24 July 2015 at 12:01, Markus Weißmann <markus.weissmann@in.tum.de> wrote:
> but is there some clever way to only have the socket annotated with one type
> while keeping only one send and one recv function?
One way to achieve this is to add some structure to the 'server' and
'client' types, by turning your descriptions of the desired behaviour
into code. For example, you say
> There is a server and a client which can only send "client" (client to
> server) and "server" (server to client) messages.
which you might encode as follows:
type server = < src: server; dst: client; name: [`server] >
and client = < src: client; dst: server; name: [`client] >
These are object types, but there's no need to actually use object
values in your code. The object type syntax is simply a convenient
way to label the different components of the type so that they can be
retrieved later.
Once you have these types you might write type-level functions to
retrieve the components. Here's a function that retrieves the 'src'
component:
type 'a src = 's constraint 'a = < src: 's; .. >
This can be read as follows: src is a function that maps 'a to 's,
where 'a is an object type with a src key, and 's is the value of that
key.
Similarly, here's a function to retrieve the 'dst' component:
type 'a dst = 'd constraint 'a = < dst: 'd; .. >
> You can only send "client messages" on the "client socket" and "server
> messages" on the "server socket".
>
> val send : ('a, _) socket -> 'a message -> unit
>
> You can get these messages only on the respective other side.
>
> val recv : (_, 'b) socket -> 'b message
>
> Something in the spirit of this:
>
> type 'a socket
> val send : 'a socket -> 'a message -> unit
> val recv : [server socket -> client message | client socket -> server
> message]
You might then write
val send : 's socket -> 's src message -> unit
val recv : 's socket -> 's dst message
and OCaml will enforce the constraints you describe.
Kind regards,
Jeremy.
next prev parent reply other threads:[~2015-07-24 20:25 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-24 11:01 Markus Weißmann
2015-07-24 11:20 ` Nicolas Ojeda Bar
2015-07-24 18:41 ` Mikhail Mandrykin
2015-07-24 20:23 ` octachron
2015-07-24 20:25 ` Jeremy Yallop [this message]
2015-07-24 20:57 ` Török Edwin
2015-07-25 12:42 ` Oleg
2015-07-25 15:55 ` mandrykin
2015-08-08 21:39 ` Markus Mottl
2015-08-09 13:04 ` Jacques Garrigue
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAAxsn=H62R8eooPkgGJwsQ6Mozi+HuLaE95UzOBbxLiF1fd0nw@mail.gmail.com' \
--to=yallop@gmail.com \
--cc=caml-list@inria.fr \
--cc=markus.weissmann@in.tum.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).