From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.3 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 22486BBE1 for ; Thu, 12 Oct 2006 07:45:43 +0200 (CEST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id k9C5jeNr020713 for ; Thu, 12 Oct 2006 07:45:42 +0200 Received: from localhost (orion [130.54.16.5]) by kurims.kurims.kyoto-u.ac.jp (8.13.7/8.13.7) with ESMTP id k9C5jKMO011604; Thu, 12 Oct 2006 14:45:20 +0900 (JST) Date: Thu, 12 Oct 2006 14:45:18 +0900 (JST) Message-Id: <20061012.144518.115907516.garrigue@math.nagoya-u.ac.jp> To: cpitaper@yahoo.com.ar Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Why + vs +. but "fake" parametric polymorphism for < From: Jacques Garrigue In-Reply-To: <1160630285.7649.18.camel@monad> References: <1160630285.7649.18.camel@monad> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 452DD684.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; parametric:01 polymorphism:01 ocaml:01 inference:01 parametric:01 polymorphism:01 subtyping:01 compiler:01 bool:01 generality:01 computations:01 compiler:01 statically:01 cheers:01 equality:01 From: Carlos Pita > I would like to implement some number crunching inner loops for dsp > stuff with ocaml. I'm a newcomer to the language with strong > scheme/python background and trying to come into terms with type > inference, parametric polymorphism and structural subtyping. One thing > than I'm pretty fond of is the difference between floating point and > integer basic mathematical operators. I guess the compiler is able to > generate specific and more efficient code for each case without further > analysis. But then I found out that comparison operators offer some kind > of adhoc polymorphism in the guise of parametric one: > > # (<);; > - : 'a -> 'a -> bool = > > Is there any reason for this apparently inconsistent design? Would the > generality of < be against performance if for example, say, my critical > inner loops check against a top limit value thousands of times per > second? The reason is that this operator comes handy for symbolic computations, where you may want to compare lists or other data structures. And contrary to addition, comparison makes sense for almost any data structure. Of course there is an overhead. But the compiler is clever enough to remove this overhead when the types of arguments are statically known. In particular this is true for floats. If you're going to do number crunching, you must be careful of using it only in situation where the types of the arguments are known, i.e. not inside polymorphic functions. Typical examples of polymorphic functions that will call the slow version of equality are List.mem and List.assoc. Cheers, Jacques Garrigue