caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* polymorphic variants
@ 2000-06-09  8:52 David Chemouil
  2000-06-09 19:04 ` Markus Mottl
  2000-06-09 20:03 ` Markus Mottl
  0 siblings, 2 replies; 4+ messages in thread
From: David Chemouil @ 2000-06-09  8:52 UTC (permalink / raw)
  To: caml-list



Hi,




I and my teammates are currently writing a compiler for a distributed
language, called ML-Act, that generates OCaml 3.00 code. I'm involved in
the code generation and middleware design. 

The middleware is made of servers which are, of course, intended to work
with many different applications. 

Each application is likely to send messages through the network. We only
find the type of an argument when compiling an ML-Act program.

The problem is that, as server communicate for admistrative reasons,
they exchange messages with arguments of which I already know the type
constructor. 

As a consequence, the servers must manipulate a type 'argument' which is
not yet completely defined: the part interesting the servers is defined
but not the one interesting the generated OCaml program. 

Therefore, the server programs could not be linked, because the type
'argument' can't be created as long as I don't know all the type
constructors. So, these server programs should be compiled for a
specific application, for which I would have found all possible
constructors for an argument.

The solution I found is the following one: I use polymorphic variants.
Then, the servers use polymorphic constructors, and can be compiled.
And for each compiled ML-Act application, I generate a type 'argument'
containing, as constructors, the server constructors, plus the
constructors found at compilation.


However, I wonder if my solution is really good, from a software
engineering point of view. Or, on the contrary, is it a good example of
why polymorphic variants can be interested. In fact, I wonder how I
could have done without this new possibility in OCaml 3.00.


-- 
David Chemouil [mailto:chemouil@enseeiht.fr] [mobile: 06 84 16 26 65]

Laboratoire d'informatique et de mathématiques appliquées (IRIT-INPT)

"Je vous ai fait trop faibles pour sortir du gouffre, parce que 
 je vous ai fait assez forts pour n'y point tomber" -- Rousseau




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

* Re: polymorphic variants
  2000-06-09  8:52 polymorphic variants David Chemouil
@ 2000-06-09 19:04 ` Markus Mottl
  2000-06-09 20:03 ` Markus Mottl
  1 sibling, 0 replies; 4+ messages in thread
From: Markus Mottl @ 2000-06-09 19:04 UTC (permalink / raw)
  To: David Chemouil; +Cc: caml-list

On Fri, 09 Jun 2000, David Chemouil wrote:
> The problem is that, as server communicate for admistrative reasons,
> they exchange messages with arguments of which I already know the type
> constructor. 
> 
> As a consequence, the servers must manipulate a type 'argument' which is
> not yet completely defined: the part interesting the servers is defined
> but not the one interesting the generated OCaml program. 

Maybe you would want to try something like this (uses the Marshal module):

  type admin_args = ...
  type user_data = string
  type argument = MsgA of admin_args * user_data | MsgB of ... | ...

Then you can react to messages as follows, for example:

  let recv = function
    | MsgA (adm_args, user_data) ->
        handle_adm_args adm_args;
        call_user_program (Marshal.from_string user_data)
    | MsgB ... ->
    ...

Sending would be something like:

  let send user_data =
    let adm_args = create_args ()
    and contents = Marshal.to_string user_data [Closures] in
    send_network (MsgA (adm_args, contents))

Very rudimentary, but the basic idea (keep user data encoded in a string
until needed) should be clear...

Best regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl




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

* Re: polymorphic variants
  2000-06-09  8:52 polymorphic variants David Chemouil
  2000-06-09 19:04 ` Markus Mottl
@ 2000-06-09 20:03 ` Markus Mottl
  1 sibling, 0 replies; 4+ messages in thread
From: Markus Mottl @ 2000-06-09 20:03 UTC (permalink / raw)
  To: David Chemouil; +Cc: OCAML

Followup to my last mail:

Actually, you need not necessarily double decode things (first the network
message, then the user data): you can do this in one step, too, also using
the Marshal-module.

It does not matter whether you know the internals of the user data or not
(when the type is abstract): the Marshal-module will always do the right
thing during decoding - but make sure that you only pass on the user data
to functions of the right type (i.e. ones that can really handle it),
otherwise your program will crash.

E.g. (recv takes the raw string from the network now):

  type admin_args = ...
  type user_data  (* abstract *)

  type argument = MsgA of admin_args * user_data | MsgB of ... | ...

  let recv network_data =
    let msg : argument = Marshal.from_string network_data 0 in
    match msg with
    | MsgA (admin_args, user_data) ->
        handle_admin_args admin_args;
        call_user_program user_data
    | ...
    ...

Best regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl




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

* Polymorphic Variants
@ 2007-01-16 20:32 Tom
  0 siblings, 0 replies; 4+ messages in thread
From: Tom @ 2007-01-16 20:32 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 568 bytes --]

I have a question... I hope it will not be dismissed right away, thou I
guess most of you will find it stupid (some might however agree with me...
hopefully).

Cut the crap!

So... why actually are polymorphic variants useful? Why can't they simply be
implemented as normal, concrete (or how would you call them? ...) variants?
Doesn't the use of polymorphic variants just mess up the function type?

I'm not orthogonally against polymorphic variants, it's just that I am
looking for an alternative concept that could be used instead... Maybe
subtyped records?

- Tom

[-- Attachment #2: Type: text/html, Size: 614 bytes --]

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

end of thread, other threads:[~2007-01-16 20:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-06-09  8:52 polymorphic variants David Chemouil
2000-06-09 19:04 ` Markus Mottl
2000-06-09 20:03 ` Markus Mottl
2007-01-16 20:32 Polymorphic Variants Tom

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