caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <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 <
Date: Thu, 12 Oct 2006 14:45:18 +0900 (JST)	[thread overview]
Message-ID: <20061012.144518.115907516.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <1160630285.7649.18.camel@monad>

From: Carlos Pita <cpitaper@yahoo.com.ar>

> 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 = <fun>
> 
> 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


  reply	other threads:[~2006-10-12  5:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-12  5:18 Carlos Pita
2006-10-12  5:45 ` Jacques Garrigue [this message]
2006-10-12  5:58   ` [Caml-list] " Carlos Pita
2006-10-12  6:08     ` Jonathan Roewen
     [not found]       ` <452DF46C.802@fmf.uni-lj.si>
2006-10-12 14:26         ` Carlos Pita
2006-10-13 11:56       ` Diego Olivier FERNANDEZ PONS
2006-10-13 12:14         ` Gerd Stolpmann
2006-10-13 12:46           ` Diego Olivier FERNANDEZ PONS
2006-10-13 13:01             ` Luc Maranget
2006-10-13 13:15               ` Diego Olivier FERNANDEZ PONS
2006-10-13 13:15               ` skaller
2006-10-13 13:36                 ` Luc Maranget
2006-10-13 13:53             ` Gerd Stolpmann
2006-10-13 14:16               ` Luc Maranget
2006-10-12  5:19 Carlos Pita
2006-10-12  5:41 ` [Caml-list] " Carlos Pita
2006-10-12  5:49   ` Basile STARYNKEVITCH
2006-10-12  5:53   ` Jonathan Roewen
2006-10-12  6:10     ` Carlos Pita

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=20061012.144518.115907516.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=cpitaper@yahoo.com.ar \
    /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;
as well as URLs for NNTP newsgroup(s).