caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: guillaume.yziquel@citycable.ch, "'Stéphane Glondu'" <steph@glondu.net>
Cc: "'OCaml List'" <caml-list@inria.fr>
Subject: RE: [Caml-list] Recursive subtyping issue
Date: Mon, 1 Mar 2010 12:49:53 -0000	[thread overview]
Message-ID: <000d01cab93d$b095c770$11c15650$@romulus.metastack.com> (raw)
In-Reply-To: <4B8BA350.8090404@citycable.ch>

Guillaume Yziquel wrote:
> Stéphane Glondu a écrit :
> > Guillaume Yziquel a écrit :
> >>> # type untyped;;
> >>> type untyped
> >>> # type 'a typed = private untyped;;
> >>> type 'a typed = private untyped
> >>> # type -'typing tau = private obj
> >>>   and 'a t = 'a typed tau
> >>>   and obj = private untyped tau;;
> >>> type 'a tau = private obj
> >>> and 'a t = 'a typed tau
> >>> and obj = private untyped tau
> >>> # let f : 'a t -> obj = fun x -> (x : 'a t :> obj);; val f : 'a t ->
> >>> obj = <fun> # let g : obj -> 'a t = fun x -> (x : obj :> 'a t);; val
> >>> g : obj -> 'a t = <fun> #
> >
> > Why don't you just declare 'a t to be synonym for obj in the
> > implementation of your module, declare them as abstract in its
> > interface, and export the specially typed identities f and g?
> 
> Because subtyping seems more efficient than applying a noop function.

I wholeheartedly agree that doing this in the type system is much cleaner than using noop/coercion functions but I don't think that there's any difference in terms of efficiency. If the noop/coercion functions are correctly coded then they will be of the form:

external foo_of_bar : bar -> foo = "%identity"

in *both* the .ml and .mli file for the module in question. I'm virtually certain that ocamlopt eliminates calls to the %identity primitive.

> And this code might run really often, so I do not like very much the
> idea of having noop functions running really often.

See previous; I don't think it makes a difference (to runtime performance, anyway).

> Moreover, having conversion functions is not really handy, from a
> syntactic point of view: It's quite convenient to write something like
> 
> let f : string -> obj :> string -> float t = blah blah blah...
> 
> than doing the explicit, runtime, casting in the definition of f.

Agreed - this is where your approach is really neat!

<snip>

> I then tried to go the whole way, and get rid of conversion functions
> altogether.

Being pedantic, what you mean is getting rid of *coercion* functions; *conversion* functions could never eliminated because by their nature they are "doing" something (for example, int_of_string constructs a new integer value based on the string value given to it - you could never just trick the type system into using the same value for both in a meaningful way).

This is tremendously clean - as long as the types are clearly documented! The problem is that ocamldoc doesn't let you "document" coercions (by which I mean that having a conversion function provides means for the documentation of that particular usage).


David


  parent reply	other threads:[~2010-03-01 12:53 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-27  1:52 Guillaume Yziquel
2010-02-27  6:38 ` [Caml-list] " Andreas Rossberg
2010-02-27 10:25   ` Guillaume Yziquel
2010-02-27 11:49     ` Goswin von Brederlow
2010-02-27 13:11       ` Guillaume Yziquel
2010-02-27 16:52         ` Andreas Rossberg
2010-02-27 18:10           ` Guillaume Yziquel
2010-02-27 19:52             ` Guillaume Yziquel
2010-02-27 20:32               ` Guillaume Yziquel
2010-03-01 10:55                 ` Stéphane Glondu
2010-03-01 11:21                   ` Guillaume Yziquel
2010-03-01 12:28                     ` Stéphane Glondu
2010-03-01 12:49                       ` David Allsopp
2010-03-01 13:06                       ` Guillaume Yziquel
2010-03-01 12:49                     ` David Allsopp [this message]
2010-03-01 13:28                       ` Goswin von Brederlow
2010-03-01 20:12                         ` David Allsopp
2010-03-02 10:22                           ` Goswin von Brederlow
2010-03-01 13:33                       ` Guillaume Yziquel
2010-03-01 20:18                         ` David Allsopp
2010-02-28  9:54         ` Goswin von Brederlow
2010-02-28 11:08           ` Guillaume Yziquel

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='000d01cab93d$b095c770$11c15650$@romulus.metastack.com' \
    --to=dra-news@metastack.com \
    --cc=caml-list@inria.fr \
    --cc=guillaume.yziquel@citycable.ch \
    --cc=steph@glondu.net \
    /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).