Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: octachron <octa@polychoron.fr>
To: "Markus Weißmann" <markus.weissmann@in.tum.de>, caml-list@inria.fr
Subject: Re: [Caml-list] Clever typing for client-server communication?
Date: Fri, 24 Jul 2015 22:23:35 +0200	[thread overview]
Message-ID: <55B29EC7.2070202@polychoron.fr> (raw)
In-Reply-To: <c44d210f0c2b3f58a45e95a8d81ddb2d@in.tum.de>

Hello,
> 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";
>

I see one way to do this, but I am not sure I would call it a clever way.

The idea is to try to encode (-1),(1) and (~-) at the type level. With 
this encoding, it is possible
to define send and recv as

type 'a socket
val send : 'n socket -> 'n message -> unit
val recv : 'n socket -> '-n message

A working encoding would be:

type head
type tail
type 'a socket = ... (constraint  'a = 'b * 'c)
type 'a message = ... (constraint  'a = 'b * 'c)

where you can use either gadt or abstract value to enforce that 'a is 
only head*tail ( = 1 ) or tail*head ( = -1 ).

Then -( 'a * 'b ) can be translated to ('b *' a), i.e.

val send : 'n socket -> 'n message -> unit
val recv: ('a * 'b) socket -> ('b * 'a) message

With this encoding, you only need to decompose the type parameter of 
socket or message when you need to flip it. However, I am not sure that 
this slim advantage is worth the added complexity (and one could argue 
that I added a type parameter to message rather than removed one from 
socket).

Regards,
octachron.




  parent reply	other threads:[~2015-07-24 20:24 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 [this message]
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

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=55B29EC7.2070202@polychoron.fr \
    --to=octa@polychoron.fr \
    --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