Hi,

This is not exactly what you asked, but you can use GADTs to constrain the phantom type (_, _) socket:

type client
type server

type (_, _) socket =
  | ClientServer : mysocket -> (client, server) socket
  | ServerClient : mysocket -> (server, client) socket

Best wishes,
Nicolas


On Fri, Jul 24, 2015 at 1:01 PM, Markus Weißmann <markus.weissmann@in.tum.de> wrote:
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/


--
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs