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] More or bignums/ints
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-06-12 (00:09)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] More or bignums/ints
From: "John Hughes" <jfh@cs.brown.edu>
> So the questions are
> 1. Is there a way to catch overflow exceptions within an entire 
>    computation?

Not that I know of.

> 2. Is there a way to tell OCaml that ints really are either 
>    (a) bignums or
>    (b) overflow-protected ints (as in SML/NJ, for instance)

No. Old Caml had (a), but this is more than 10 years ago.
Note however that you may redefine operators if you want to:

exception Int_overflow

let (+) a b =
  let c = Pervasives.(+) a b in
  if a < 0 && b < 0 && c >= 0 || a > 0 && b > 0 && c <= 0
  then raise Int_overflow;

This is going to be pretty slow, but if this is for beginners this
shouldn't matter too much.
And it won't change the behaviour of other operations in the library,
but you don't want to: they may depend on the rollover behaviour.

> Perhaps the implicit third question is
> 3. Is there a reasonable debugger for some dialect of ML that has 
> what I might call "protected integers"? 

I don't know, but it should be possible to hack ocamlrun to check for
overflows. Combined with C primitives, you could also set whether an
exception should be raise or not.
A bit more painful than the above approach, but this should run

Jacques Garrigue

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