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 LAA02210; Fri, 23 May 2003 11:56:25 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA06504 for ; Fri, 23 May 2003 11:56:24 +0200 (MET DST) Received: from nef.ens.fr (nef.ens.fr [129.199.96.32]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h4N9uOH05522 for ; Fri, 23 May 2003 11:56:24 +0200 (MET DST) Received: from di.ens.fr (di.ens.fr [129.199.99.1]) by nef.ens.fr (8.12.9/1.01.28121999) with ESMTP id h4N9uMHo056458 ; Fri, 23 May 2003 11:56:22 +0200 (CEST) Received: from basilic.ens.fr (monniaux@basilic [129.199.99.48]) by di.ens.fr (8.12.9/jb-1.1) id h4N9uMQI024131 ; Fri, 23 May 2003 11:56:22 +0200 (MEST) Received: from localhost (monniaux@localhost) by basilic.ens.fr (8.11.0/jb-1.1) id h4N9uKZ26327 ; Fri, 23 May 2003 11:56:20 +0200 (MEST) X-Authentication-Warning: basilic.ens.fr: monniaux owned process doing -bs Date: Fri, 23 May 2003 11:56:20 +0200 (MEST) From: David Monniaux X-Sender: monniaux@basilic.ens.fr To: hermanns cc: caml-list@inria.fr Subject: Re: [Caml-list] Why are arithmetic functions not polymorph? In-Reply-To: <147AB214-8CA5-11D7-B453-003065A2962C@gmx.de> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=ISO-8859-1 Content-Transfer-Encoding: 8BIT X-Virus-Scanned: by amavisd-milter (http://amavis.org/) X-Spam: no; 0.00; monniaux:01 caml-list:01 funtions:01 forall:01 implemented:01 traversing:01 run-time:01 operands:01 runtime:01 compiler:01 inefficient:01 garbage:01 overloading:01 int:01 arithmetic:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Fri, 23 May 2003, hermanns wrote: > I don't understand this, because comparison funtions ('<', '>', ...) > are polymorph. > So, where is the problem with arithmetic functions? There are several reasons. First, short of considering arithmetic types to be objects, there is no way to write types such as: forall ('a : arithmetic type), 'a -> 'a -> 'a. You may only write types of the form: forall 'a, 'a -> 'a -> 'a. This is not a problem since the polymorphic comparison function has the type: forall 'a, 'a -> 'a -> int. Polymorphic comparisons is a priori defined on all types; if used on functional objects, it may throw an exception. Second, it would be inefficient. Polymorphic comparison is implemented by traversing the memory data structures, using the run-time type information meant for the garbage collector. Note that if the compiler can figure at compile time that the type of the operands of <, <= etc..., it may generate some specialized version more efficient than calling the polymorphic comparison routine. In short, using a similar solution for +, -, *, / would be a bad idea: - polymorphic comparisons are inefficient compared to comparisons on integers and IEEE floating-point numbers; - short of a significant change in the type system, +, -, *, / would not refuse non-arithmetic operands, but would throw exceptions at runtime if applied to non-arithmetic arguments. SML has a kind of operator overloading, but I don't know the details. David Monniaux http://www.di.ens.fr/~monniaux Laboratoire d'informatique de l'École Normale Supérieure, Paris, France ------------------- 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