English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Observations on OCaml vs. Haskell
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-09-27 (21:34)
From: Danny Yoo <dyoo@h...>
Subject: Re: [Caml-list] Observations on OCaml vs. Haskell

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

[text cut]

> However, I do agree with you that the mess with OCaml having to add
> totally different operators to do arithmetic for integral and floating
> point types is a wart in what is otherwise an elegant language.  The
> compiler could very easily make the arithmetic operators polymorphic,
> and constrain the operands to be of the same (numeric) type, and signal
> an error if you attempt to, say, multiply an integer with a floating
> point number without doing the proper conversion.

The comparison operators '<' are polymorphic, but that does incur some
performance cost.  Richard Jones's wonderful tutorial on OCaml touches on
this under "Polymorphic types":


He shows that:

let max a b =
    if a > b then a else b
    print_int (max 2 3)

uses the polymorphic version of '>', even though the use of max here uses
only ints.  I think OCaml's arithmetic operators are monomophic to avoid
the cost of polymorphism.

> Unless there's something in the design of the type system that prevents
> this from happening except at run-time...?

I wonder if the Cartesian Product Algorithm might be applicable to OCaml.
There's a paper about it here:


where it sounds like it might be able to attack this problem.  I'd love to
be able to use just one '+' operator, rather than remember '+' vs '+.' vs
'+/'.  *grin*

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