caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <Pierre.Weis@inria.fr>
To: williamc@paneris.org (William Chesters)
Cc: caml-list@inria.fr
Subject: Re: Caml wish list
Date: Tue, 25 Apr 2000 20:16:24 +0200 (MET DST)	[thread overview]
Message-ID: <200004251816.UAA13437@pauillac.inria.fr> (raw)
In-Reply-To: <200004191345.OAA27001@toy.william.bogus> from "William Chesters" at Apr 19, 2000 02:45:06 PM

> Well, I can't remember offhand how SMJ/NJ handles overloading, but it
> seems to work reasonably well.  On the other hand, I am nowadays a
> convert to ocaml's hard line on overloading---it's an absolute curse
> of many, many C++ programs by coders whose enthusiasm outruns their
> judgement.

We think many Caml users feel that using different addition function
(+) and (+.) for integers and floats is a safe way of programming, but
really frustrating at the same time.

So do we. Therefore we are currently in the effort of adding some kind
of limited overloading to Caml. This is not a trivial problem, if you
want something really good both at the expressiveness and the
efficiency levels.

      *  *  * 

As you mentioned, SML provides a set of overloaded functions such as
(+), but it is quite limited. In addition, for the sake of efficiency
and simplicity of the typing and compilation schemes, the overloaded
functions in SML have a restricted ``functional'' status: you cannot
abstract pieces of code containing overloaded operators. Hence, you
cannot define derived overloaded functions using already existing
overloaded functions.

A simple example that novice SML users often encounter is the double
function using overloaded (+):

# let double = fun x -> x + x;;

In SML, this double function is not an overloaded function for
integers and floats, because the type of (+) must always be statically
deduced from the context, which is impossible here. This definition is
just either rejected (SML '90), or resolved using a default type
assignment for overloaded symbols (SML '97): then (+) is typed as its
default type int -> int -> int, and consequently double gets type int
-> int.

This way, the compiler always has the precise type of use for each
occurrence of (+). Therefore occurrences of (+) can be replaced by 
the corresponding primitives, for example:

        # 1 + 2            ====>   add_int 1 2
        # 1.0 + 2.0        ====>   add_float 1.0 2.0
        # fun x -> x + x   ====>   fun x -> add_int x x

Therefore overloaded functions are just "typeful macros" in SML.

We think this is one reasonable solution, since the users will feel
much less frustration than using the separated functions (+) and (+.),
and the compilation for these overloaded functions is straightforward
and has no run time overhead.

      *  *  * 

However, we are not promoting this simple overloading facility,
because we think the ``abstraction restriction'' to be unnatural in a
functional setting: even if the definition of new overloaded symbols
is prohibited, the abstraction over overloaded operators (as in double
above) should be possible in higher-order functional languages like
SML or Caml!

Our "extensional polymorphism" framework is a bit more complicated
than the ``default assigment'' static scheme, but it provides
abstraction and "real" overloaded functions. (By the way, we call
those "generic" functions.) Using extensional polymorphism, the double
function is polymorphic, being defined for ``any argument whose type
is suitable for (+)''. Thus, you can use it for integers and floats!

# let double = fun x -> x + x;;
val double : $a -> $a = <fun>

# double 1;;
- : int = 2 

# double 3.14;;
- : float = 6.28

We use special type variables $a, $b, ..., called dynamic type
variables, to denote type parameters abstracted into type schemes for
polymorphic generic functions. In the definition of double, the
context of (+) is left unresolved, and double becomes polymorphic. The
compilation of double abstracts the type context for the internal (+),
and dynamically passes it to (+) to select the appropriate addition
primitive.

You may worry about efficient compilation of this feature, since, as
examplify by the double generic function, some information from the
typing context has to be passed at run time, and hence double involves
an extra argument (a type) to be provided to (+) at run time, and also
an additional pattern matching selection to apply the proper addition
primitive; undoubtedly, the generic double function is a bit slower
than its direct non-overloaded counterpart definitions for integers
and floats, like

# let double_int x = x + x;;
# let double_float x = x +. x;;

However, writing those two definitions means twice more code, hence
twice more possibilities of bugs, twice more maintenance, and twice
more identifiers to remember. This simplification is worth a slight
efficiency loss ...

Note also that usual simple overloading of operators is still as
efficient as it can be, since static type annotations allow the
inlining of the corresponding primitives, at least for the predefined
set of usual arithmetic operators (+, -, *, ...).

Polymorphic uses of overloaded operators in generic functions need an
extra type book keeping for dynamic resolution, but there is no
possible efficiency comparison with other compilation schemes, since
these functions are completely new in ML and cannot be expressed
neither in SML nor in Caml.

As a side effect of the extensional polymorphism discipline, we
manipulate types at run time, and this provides various benefits and
new features to the lasnguage:

        * safe value I/O (persistent value I/O between different
          programs, or safe usage of input_value/output_value),
        * dynamic values,
        * some limited polymorphic print facility,
        * a new kind of computation mixing types and values ...

The current prototype is an extension of the 2.04 version of Objective
Caml; we are planning to release it when it will be fully stable, like
the label extension in 2.99. We don't know yet which version of
Objective Caml it will be 3.0, 3.x or 3.99...

Hope this helps,

Jun Furuse & Pierre Weis





  parent reply	other threads:[~2000-04-25 18:18 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-04-03  1:27 When functional languages can be accepted by industry? Dennis (Gang) Chen
2000-04-06 16:51 ` Jean-Christophe Filliatre
2000-04-07  5:27   ` Dennis (Gang) Chen
     [not found]     ` <14574.1721.508470.790475@cylinder.csl.sri.com>
2000-04-11  0:24       ` Dennis (Gang) Chen
2000-04-11 17:58         ` Pierre Weis
2000-04-12  1:45           ` Dennis (Gang) Chen
2000-04-12 17:27             ` Daniel de Rauglaudre
2000-04-13 15:40               ` John Max Skaller
2000-04-14 19:16                 ` John Max Skaller
2000-04-12 18:06             ` David Brown
2000-04-13  1:23               ` Dennis (Gang) Chen
2000-04-13 14:36                 ` Pierre Weis
2000-04-13  6:53             ` Jean-Christophe Filliatre
2000-04-13 12:20               ` Frank Atanassow
2000-04-13 17:28                 ` John Max Skaller
2000-04-13 12:28               ` Steve Stevenson
2000-04-13 13:38               ` jean-marc alliot
2000-04-13 16:00                 ` William Chesters
2000-04-13 14:29               ` T. Kurt Bond
2000-04-13 17:23                 ` Julian Assange
2000-04-16 16:33                   ` John Max Skaller
2000-04-17 15:06                   ` Markus Mottl
2000-04-17 19:55                     ` John Prevost
2000-04-24  2:36                       ` Chris Tilt
2000-04-14  9:19                 ` The beginning of a library for Formal algebra and numerical Analysis Christophe Raffalli
2000-04-14  9:32                 ` Caml wish list Christophe Raffalli
2000-04-19 11:40                   ` thierry BRAVIER
2000-04-19 13:45                     ` William Chesters
2000-04-19 20:45                       ` Christophe Raffalli
2000-04-25 18:16                       ` Pierre Weis [this message]
2000-05-10  4:50                         ` reference initialization Hongwei Xi
2000-05-11 13:58                           ` Pierre Weis
2000-05-11 18:59                             ` Hongwei Xi
2000-05-12 17:07                               ` Pierre Weis
2000-05-12 19:59                                 ` Hongwei Xi
2000-05-15  6:58                                   ` Max Skaller
2000-05-15 17:56                                     ` Hongwei Xi
2000-05-14 14:37                                 ` John Max Skaller
2000-05-13  7:07                               ` Daniel de Rauglaudre
2000-05-13  7:09                               ` Daniel de Rauglaudre
2000-05-11 16:02                           ` John Prevost
2000-04-13 16:59               ` When functional languages can be accepted by industry? John Max Skaller
2000-04-15 22:29                 ` William Chesters
2000-04-16 22:24                 ` Nickolay Semyonov
2000-04-18  6:52                   ` Max Skaller
2000-04-17 12:51                 ` jean-marc alliot
2000-04-17 17:49                   ` John Max Skaller
2000-04-17 22:34                     ` Brian Rogoff
2000-04-19 15:31                       ` John Max Skaller
2000-04-19 18:30                       ` Michael Hicks
2000-04-20 16:40                       ` Markus Mottl
2000-04-20 17:58                         ` Brian Rogoff
2000-04-20 18:52                           ` Markus Mottl
2000-04-21 20:44                             ` Michael Hohn
2000-04-21 19:22                           ` John Max Skaller
2000-04-21 19:09                         ` John Max Skaller
2000-04-21 19:45                           ` Markus Mottl
2000-04-21 19:56                           ` Brian Rogoff
2000-04-21 19:18                         ` John Max Skaller
2000-04-18 10:53                     ` Sven LUTHER
2000-04-19 15:57                       ` John Max Skaller
2000-04-13  7:05             ` Pierre Weis
2000-04-13 17:04               ` Julian Assange
2000-04-07 15:44 ` John Max Skaller

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=200004251816.UAA13437@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=williamc@paneris.org \
    /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).