caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Clever typing for client-server communication?
@ 2015-07-24 11:01 Markus Weißmann
  2015-07-24 11:20 ` Nicolas Ojeda Bar
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Markus Weißmann @ 2015-07-24 11:01 UTC (permalink / raw)
  To: caml-list

Hello OCaml list,

I'm trying to do something clever regarding the interface for a 
communication library:
There is a server and a client which can only send "client" (client to 
server) and "server" (server to client) messages.
The current idea is to use a phantom type to annotate the socket as 
either being "client to server" or "server to client";

type client
type server

type ('a, 'b) socket
type 'a message

I've got a bunch of functions that only work on either "client 
messages", "server messages" or some on both. Something like:

val p1 : 'a message -> int
val p2 : server message -> float
val p3 : client message -> char

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

but is there some clever way to only have the socket annotated with one 
type while keeping only one send and one recv function?
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]

there is no "(client, client) socket" or "(server, server) socket";


regards
-Markus

PS: Under the hood its basically

type 'a message = bytes
type ('a, 'b) socket = mysocket

But the underlying system allows me to add filters that guarantee me 
the aforementioned properties of send/receive.

regards
Markus

-- 
Markus Weißmann, M.Sc.
Technische Universität München
Institut für Informatik
Boltzmannstr. 3
D-85748 Garching
Germany
http://wwwknoll.in.tum.de/


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

end of thread, other threads:[~2015-08-09 13:04 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-24 11:01 [Caml-list] Clever typing for client-server communication? 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
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

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