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] Why must types be always defined at the top level?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-06-24 (23:17)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] Why must types be always defined at the top level?
On Thu, 24 Jun 2004, Xavier Leroy wrote:

> There is a general need to have polymorphic operations that are 1- not
> defined on all instantiations of their types, and 2- can be defined
> differently on different instantiations.  Haskell type classes are an
> example of a *general* mechanism that addresses this need; GCaml's
> "extentional polymorphism" is another.

Ocaml modules are another way to implement this.  Wether it's a "good" way 
or not is open to debate.

> As to whether equality should be defined on floats, there are pros and
> cons.  My standpoint is that it's eventually better to stick to
> established standards (that is, IEEE float arithmetic) rather than try
> to reinvent a wheel likely to be even squarer than these standards.
> Prof. Kahan found it worthwhile to fully define equality over floats;
> I'll abide by his wisdom.

There are legitimate reasons to want floating point equality.  It's 
generally not what you want, but I had an example of needing it in Ocaml 
just the other day.

Basically, I was writting a root finding algorithm by range subdivision.  
Given a function f and a range [a,b] where sign(f(a)) != sign(f(b)),
find the root in that range, the x such that a <= x <= b, and f(x) = 0 (or 
as close as can come).  The function I ended up with was this:

let root_find f a b =
    let rec loop a b fa_is_neg =
        let c = (a +. b) /. 2. in
        if (c = a) || (c = b) then (* note the floating point equality *)
            (* We've bottomed out- pick a or b depending upon if
               f(a) or f(b) is closer to 0. *)
            if (abs_float (f a)) > (abs_float (f b)) then
            let fc_is_neg = ((f c) < 0.) in
            if (fa_is_neg == fc_is_neg) then
                loop c b fc_is_neg
                loop a c fa_is_neg
    loop a b ((f a) < 0.)

Basically, I run the algorithm out until the distance between a and b is 
1ulp, then stop.

"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 

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