From: Xavier Leroy <xleroy@pauillac.inria.fr>
To: ohl@crunch.ikp.physik.th-darmstadt.de (Thorsten Ohl)
Cc: caml-list@pauillac.inria.fr
Subject: Re: entiers et reels
Date: Mon, 8 Jan 1996 10:40:41 +0100 (MET) [thread overview]
Message-ID: <199601080940.KAA06302@pauillac.inria.fr> (raw)
In-Reply-To: <9601052114.AA03157@crunch> from "Thorsten Ohl" at Jan 5, 96 10:14:07 pm
> Maybe I'm too dense or I have been exposed to too much Fortran, but I
> don't fully understand:
> Standard ML has [overloaded arithmetic operators]
> (at least in the New Jersey incarnation), so why
> can't Caml?
Caml could very well have it. Indeed, the old Caml V3.1 implementation
has support for overloaded operators that far exceeds what NJML provides.
> As long as all operands are of the same type, the
> semantics should be obvious.
The semantics are obvious once types are assigned to all
subexpressions of the program. But that's where the problem lies in
a language with type inference. Consider:
let f x y = x + y
Assuming + works on ints and floats, f has two possible types, namely
int -> int -> int and float -> float -> float. These are the only two
types. In particular, their least upper bound 'a -> 'a -> 'a is not
a valid type for f (unless + is implemented with run-time type tests,
as in Michel Quercia's post, but this defeats the whole purpose of
static typing -- just go back to Scheme, then).
So, the type inferencer fails on the definition above ("cannot resolve
overloading"), and the programmer must add a type constraint:
let f (x:float) y = x + y
Frankly, if I must explain the compiler what I mean, I'd rather not
have + overloaded and write +. for float addition:
let f x y = x +. y
Of course, in larger examples, there's more context that helps in
resolving overloading, e.g.
let f x = x + y * 3.14
But the general problem remains: in the presence of overloaded
operators, ML's type system no longer possesses the principal typing
property, hence type inference can fail on well-typed but ambiguous
programs.
Lots of effort have been invested in trying to resolve that problem,
in particular the development of type classes as implemented in Haskell.
But it really opens a whole new can of worms, both on the semantic
side and on the implementation side.
If you're interested, here are some references:
Wadler and Blott, "How to make ad-hoc polymorphism less ad-hoc",
POPL 89.
Hudak et al, "Report on the programming language Haskell",
SIGPLAN Notices 27(5), 1992.
Dubois, Rouaix, and Weis, "Extensional polymorphism", POPL 95.
Harper and Morriset, "Compiling polymorphism using intensional
type analysis, POPL 95.
As you can see, the problem of overloaded operators is far from
solved. Until the dust settles, I'd rather keep Caml Light simple and
implement no overloading at all.
- Xavier Leroy
next prev parent reply other threads:[~1996-01-08 10:56 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-01-05 9:05 Fauque UPS
1996-01-05 18:27 ` Pierre Weis
1996-01-05 21:14 ` Thorsten Ohl
1996-01-08 9:40 ` Xavier Leroy [this message]
1996-01-05 20:13 quercia
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=199601080940.KAA06302@pauillac.inria.fr \
--to=xleroy@pauillac.inria.fr \
--cc=caml-list@pauillac.inria.fr \
--cc=ohl@crunch.ikp.physik.th-darmstadt.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