caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <xavier.leroy@inria.fr>
To: John Hughes <jfh@cs.brown.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] More or bignums/ints
Date: Mon, 14 Jun 2004 17:55:52 +0200	[thread overview]
Message-ID: <20040614175552.A28810@pauillac.inria.fr> (raw)
In-Reply-To: <20040611193818.0A43457251@twix.cs.brown.edu>; from jfh@cs.brown.edu on Fri, Jun 11, 2004 at 09:38:29PM +0200

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


  parent reply	other threads:[~2004-06-14 15:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-11 19:38 John Hughes
2004-06-12  0:09 ` Jacques Garrigue
2004-06-14 15:37 ` Brian Hurt
2004-06-14 16:17   ` Andreas Rossberg
2004-06-14 15:55 ` Xavier Leroy [this message]
2004-06-15 22:45   ` Manos Renieris
2004-06-15 20:26 ` Brian Hurt
2004-06-15 20:36   ` Brian Hurt
2004-06-23 10:50   ` John Hughes
2004-06-23 14:51     ` skaller

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=20040614175552.A28810@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=jfh@cs.brown.edu \
    /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).