caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andrej Bauer <Andrej.Bauer@andrej.com>
To: Caml list <caml-list@inria.fr>
Subject: Restarting a piece of code
Date: Sat, 27 Aug 2005 16:42:14 +0200	[thread overview]
Message-ID: <43107BC6.2010508@andrej.com> (raw)

I have a problem which I do not know how to program in The Right Way.
I would be grateful to hear ideas on how to do it.

Suppose I have a bunch of functions (the actual example is exact real
number arithmetic) which optimistically compute results (e.g. we compute
numbers up to some precision and hope it will be good enough) that may
turn out not to be good enough later during the computation. When this
happens, the entire computation needs to be restarted (e.g. we need to
recompute the numbers again with higher precision).

I will describe a specific made up example which demonstrates my point.
Suppose we want to add numbers, but if we ever exceed max_value during
addition, an exception is thrown:

let max_value := ref 10

exception Overflow

let add x y =
  let z = x + y in
    if z < !max_value then z else raise Overflow

If in a computation Overflow is thrown, we want to catch it, increase
max_value by some amount (say, double it), and try again. One way to do
this is by wrapping the code with a "compute" function that does this:

let rec compute code =
  try
    code ()
  with
    Overflow ->
      (max_value := 2 * !max_value;
       compute code)

To compute the sum 1 + 2 + 3 + .. + 100 we evaluate

compute (fun () ->
  let s = ref 0 in
  for i = 1 to 100 do
    s := add !s i
  done ;
  s)

While this idea works, there are two problems (side-effect are also an
issue but I do not care about that):

1) The programer who has to enclose pieces of code that use "add"
   inside the "compute" wrapper that catches Overflow and restarts
   the computation.

2) max_value is a global variable. Sometimes we want nested "computes",
   each with its own max_value.

What to do? I want the programmer to have a minimal overhead
(syntactically and also in terms of having to think about what is going
on). Which programming concept am I failing to use here?

Thanks for your help,

Andrej

P.S. One idea is to remember enough info so that we can recompute better
values on demand. This turns out to be bad in my case (real number
arithmetic) because you end up using space linear in the number of
arithmetic operations, which is too expensive (think a hundred billion
arithmetic operations).


             reply	other threads:[~2005-08-27 14:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-27 14:42 Andrej Bauer [this message]
2005-08-31  7:56 ` [Caml-list] " Alex Baretta
     [not found]   ` <4315B8D8.4060602@andrej.com>
2005-08-31 15:54     ` Alex Baretta

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=43107BC6.2010508@andrej.com \
    --to=andrej.bauer@andrej.com \
    --cc=caml-list@inria.fr \
    /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).