From: Jacques GARRIGUE <garrigue@kurims.kyoto-u.ac.jp>
To: jgoerzen@complete.org
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Observations on OCaml vs. Haskell
Date: Tue, 28 Sep 2004 10:32:44 +0900 (JST) [thread overview]
Message-ID: <20040928.103244.74171224.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <200409271408.51872.jgoerzen@complete.org>
The answers are clear enough for your first two questions, so I just
add some details to the 3rd.
From: John Goerzen <jgoerzen@complete.org>
> 3. The Num typeclass
>
> I've written several functions that can work with a "number-like" type.
> I don't really care if I get an integer, Int32, Int64, float, or what.
> But since these are all different types in OCaml, I am forced to care,
> right down to using +, +., or Int64.add to perform basic arithmetic.
> The Num typeclass in Haskell neatly solves that whole problem; I could
> take a Num, use a unified set of operators, and produce the appropriate
> result.
This is again a problem of performance. An arithmetic operation on a
normal int when the compiler doesn't know that this is an int will be
several orders of magnitude slower than when it knows this is an int.
This is actually worse than boxing/unboxing: you have to dispatch
according to the runtime type of the data.
Simple overloading would avoid this cost (it is resolved at
compile time), but it doesn't provide what you ask for: real
polymorphism.
If you don't care about performance, there are several ways you can
obtain such polymorphism in ocaml:
* Functors. There are already modules in the compiler for Int32, Int64
and Nativeint, you will just have to write a common signature (they
all have the same operations), and similar modules for Int and
Float. Then you can parameterize your whole algorithm on the type to
be used. Then, if you can find a defunctorializer (I believe there
is one around), you can recover unfettered performance.
* Objects. It may be funnier, as you can make things more dynamic. But
there are no optimizations available.
* Sum types. Define a big sum
type num = Int of int | Float of float | ...
and do all compuations with automatic conversions. This shouldn't be
worse than polymorphism, and you can combine different types.
Of course, that leaves us with the problem of syntax.
If you redefine the standard operators to use your new numbers, then
you're in trouble when using normal ints.
And you don't even have such an option for literal numers, but they
are not too bad with a prefix operator.
Jacques
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
next prev parent reply other threads:[~2004-09-28 1:32 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-09-27 19:08 John Goerzen
2004-09-27 20:24 ` Rafael 'Dido' Sevilla
2004-09-27 21:34 ` Danny Yoo
2004-09-28 7:22 ` Ville-Pertti Keinonen
2004-09-28 18:02 ` Jon Harrop
2004-09-29 14:26 ` Brian Hurt
2004-09-29 14:20 ` Jon Harrop
2004-09-29 15:03 ` Dmitry Lomov
2004-09-28 10:10 ` [Caml-list] Caml monomorphisation (was Observations on OCaml vs. Haskell) Diego Olivier Fernandez Pons
2004-09-28 12:01 ` Richard Jones
2004-09-28 17:50 ` Jon Harrop
2004-09-28 1:56 ` [Caml-list] Observations on OCaml vs. Haskell skaller
2004-09-28 9:31 ` Keith Wansbrough
2004-09-28 9:55 ` Rafael 'Dido' Sevilla
2004-09-27 21:11 ` Christophe TROESTLER
2004-09-28 1:32 ` Jacques GARRIGUE [this message]
2004-09-28 1:46 ` skaller
2004-09-28 8:27 ` Richard Jones
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=20040928.103244.74171224.garrigue@kurims.kyoto-u.ac.jp \
--to=garrigue@kurims.kyoto-u.ac.jp \
--cc=caml-list@inria.fr \
--cc=jgoerzen@complete.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