From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id DAA25675; Tue, 28 Sep 2004 03:32:53 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id DAA24837 for ; Tue, 28 Sep 2004 03:32:52 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i8S1Wo1T025883 for ; Tue, 28 Sep 2004 03:32:51 +0200 Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3p2-20030924/3.7W) with ESMTP id KAA07836; Tue, 28 Sep 2004 10:32:44 +0900 (JST) Date: Tue, 28 Sep 2004 10:32:44 +0900 (JST) Message-Id: <20040928.103244.74171224.garrigue@kurims.kyoto-u.ac.jp> To: jgoerzen@complete.org Cc: caml-list@inria.fr Subject: Re: [Caml-list] Observations on OCaml vs. Haskell From: Jacques GARRIGUE In-Reply-To: <200409271408.51872.jgoerzen@complete.org> References: <200409271408.51872.jgoerzen@complete.org> X-Mailer: Mew version 4.0.64 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 4158BF42.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 observations:01 haskell:01 jacques:01 32,:01 haskell:01 slower:01 boxing:01 unboxing:01 runtime:01 functors:01 32,:01 nativeint:01 jacques:01 compiler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk The answers are clear enough for your first two questions, so I just add some details to the 3rd. From: John Goerzen > 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