From: Markus Mottl <markus.mottl@gmail.com>
To: Jeremy Yallop <yallop@gmail.com>
Cc: "Markus Weißmann" <markus.weissmann@in.tum.de>,
"Caml List" <caml-list@inria.fr>
Subject: Re: [Caml-list] Clever typing for client-server communication?
Date: Sat, 8 Aug 2015 17:39:03 -0400 [thread overview]
Message-ID: <CAP_800qwdDkqZdMEpQTJmes-sy+TNHGGfUHUKukSnBNAOADLqA@mail.gmail.com> (raw)
In-Reply-To: <CAAxsn=H62R8eooPkgGJwsQ6Mozi+HuLaE95UzOBbxLiF1fd0nw@mail.gmail.com>
The object type trick is pretty neat and can actually be used with
module types, too. The advantage there is that whereas object types
are "flat", module types support more structure via submodules, e.g.:
module type Bla = sig module Foo : sig type t end end
type 'a t = 'foo_t constraint 'a = (module Bla with type Foo.t = 'foo_t)
Sadly, there is one significant shortcoming here: there does not seem
to be any way to exploit subtyping with module types. Not sure, but
the only reason for this seems to be that values of module type (i.e.
first-class modules) cannot be reasonably implemented with subtyping,
because unlike objects they don't carry around a dictionary at runtime
for looking up entries in the module.
Please correct me if I'm wrong, but from a pure typing perspective I
think that there should be no reason to disallow support for
expressing subtyping with module types. The compiler is already able
to infer whether module signatures satisfy subtyping relationships
anyway.
Would it be possible to support this in the type system? Or is there
already some ingenious hack that can give comparable results with the
existing type system?
Regards,
Markus
On Fri, Jul 24, 2015 at 4:25 PM, Jeremy Yallop <yallop@gmail.com> wrote:
> 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.
>
> --
> 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
--
Markus Mottl http://www.ocaml.info markus.mottl@gmail.com
next prev parent reply other threads:[~2015-08-08 21:39 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
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 [this message]
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=CAP_800qwdDkqZdMEpQTJmes-sy+TNHGGfUHUKukSnBNAOADLqA@mail.gmail.com \
--to=markus.mottl@gmail.com \
--cc=caml-list@inria.fr \
--cc=markus.weissmann@in.tum.de \
--cc=yallop@gmail.com \
/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).