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 RAA13156; Mon, 14 Jun 2004 17:55:55 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA10940 for ; Mon, 14 Jun 2004 17:55:54 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i5EFtqEV005077; Mon, 14 Jun 2004 17:55:52 +0200 Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA12420; Mon, 14 Jun 2004 17:55:52 +0200 (MET DST) Date: Mon, 14 Jun 2004 17:55:52 +0200 From: Xavier Leroy To: John Hughes Cc: caml-list@inria.fr Subject: Re: [Caml-list] More or bignums/ints Message-ID: <20040614175552.A28810@pauillac.inria.fr> References: <20040611193818.0A43457251@twix.cs.brown.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <20040611193818.0A43457251@twix.cs.brown.edu>; from jfh@cs.brown.edu on Fri, Jun 11, 2004 at 09:38:29PM +0200 X-Miltered: at nez-perce with ID 40CDCA88.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 bignums:01 bignums:01 bignum:01 bignum:01 camlp:01 101.:99 lxor:01 lxor:01 101.:99 ocamlinit:01 linked:01 cmo:01 cmo:01 -bit:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Dear John, > 1. Is there a way to catch overflow exceptions within an entire > computation? > 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) Solution (a) is problematic because of literals. It's easy to redefine the infix `+' operator to mean "bignum addition", and similarly for other arithmetic operators, but OCaml has no syntax for bignum literals, e.g. to get the bignum 123 one needs to type Int 123 or num_of_string "123". A Camlp4 syntax extension could possibly do the job, however. Solution (b) is much easier, provided you don't care much for performance (probably true for an intro course). Stick the following definitions in, say, CS101.ml exception Overflow let ( + ) a b = let c = a + b in if (a lxor b) lor (a lxor (lnot c)) < 0 then c else raise Overflow let ( - ) a b = let c = a - b in if (a lxor (lnot b)) lor (b lxor c) < 0 then c else raise Overflow let ( * ) a b = let c = a * b in if Int64.of_int c = Int64.mul (Int64.of_int a) (Int64.of_int b) then c else raise Overflow let ( / ) a b = if a = min_int && b = -1 then raise Overflow else a / b [Note that the definition of ( * ) above assumes a 32-bit processor. Something even less efficient is required to work both on 32- and 64-bit architectures.] Compile this source to CS101.cmo and make sure that the module CS101 is linked and opened in the students' code. For instance, with the interactive toplevel loop, make them stick #load "/path/to/CS101.cmo";; #directory "/path/to";; open CS101;; in $HOME/.ocamlinit, and voila, every time they start ocaml, they get overflow-protected integers. Don't even think of modifying the OCaml bytecode interpreter so that the arithmetic operations of the abstract machine perform overflow detection: some code in the standard library and the compilers rely on modulo arithmetic. Hope this helps, - Xavier ------------------- 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