Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #12637

Re: More on Boolean Shortcircuiting

From Andrew Haley <andrew29@littlepinkcloud.invalid>
Subject Re: More on Boolean Shortcircuiting
Newsgroups comp.lang.forth
References (1 earlier) <jq4v1e$p2s$1@dont-email.me> <wZKdnQwKfNEJlVvSnZ2dnUVZ_rCdnZ2d@supernews.com> <jq58ml$25h$1@dont-email.me> <m4vyf5.eg3@spenarnc.xs4all.nl> <jq822d$4a8$1@dont-email.me>
Message-ID <ObCdncNwwe0RBFrSnZ2dnUVZ_rKdnZ2d@supernews.com> (permalink)
Date 2012-05-31 11:17 -0500

Show all headers | View raw


Arnold Doray <invalid@invalid.com> wrote:
> On Thu, 31 May 2012 11:54:41 +0000, Albert van der Horst wrote:
>
>>
>> Have you experience using Forth's CATCH THROW? Probably not, because the
>> above is patently untrue.
>>
>> I have experience and this is what I would say:
>>
>> "
>> One would be hard pressed to come up with a realistic example where the
>> stack after a CATCH is disturbed beyond the data passed to the word that
>> is executed.
>> "
>>
>> What do you have in mind?
>> Do you think CATCH is used to recover from program errors where you
>> accidentally underflow the data stack?
>
> I have just come from the Java world, where deep exceptions (those thrown
> well up in the calling hierarchy) are the norm. Java deals with this by
> allowing the programmer to trace the exception's propagation through the
> call hierarchy, so you can see immediately what went wrong. Deep
> exceptions are a byproduct of modular programming / programming with
> libraries.
>
> The "problem" I see with THROW's behaviour is that just *before* an
> exception is thrown, the stack could have been:
>
> A) Added to -- in which case THROW does a useful thing by dropping all
> the stuff generated by the XT that CATCH executed.
>
> B) Depleted -- in which you get junk on the stack. You have to manually
> clean these or drop the entire stack.
>
> C) Muddled  (ie, items added to / removed ) -- in which case you are
> likely to just drop the entire stack.

No, that's wrong.  I think an example is needed.

Let's say you have a word FOO that takes three cells and returns one.
Like this:

: foo ( n1 n2 n3 - n)   ...  ;

However, FOO occasionally throws an exception, so you want to call it
via a CATCH.  If the word FOO succeeds, you print its result,
otherwise you print "err".

The code is:

: foo ( n1 n2 n3 - n)
    rot
    random 10 mod 0= throw
    rot rot
    random 10 mod 0= throw
    + *
    random 10 mod 0= throw ;

: sum
    ['] foo catch if  drop drop drop  ." err"  else .  then ;

This code is right regardless of whether the stack is depeted, added
to, or muddled.  It does not protect you from FOO taking things off
the stack that it shouldn't.  FOO can throw an exception if the stack
is muddled, or when it's depleted.

So, we put a couple of numbers on the stack.

66 77  ok

9 3 4 sum err ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum err ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum err ok
9 3 4 sum err ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum err ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum err ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
9 3 4 sum 63  ok
.s
66 77 <-Top  ok

Andrew.

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-26 09:39 +0000
  Re: More on Boolean Shortcircuiting Josh Grams <josh@qualdan.com> - 2012-05-26 12:44 +0000
    Re: More on Boolean Shortcircuiting Josh Grams <josh@qualdan.com> - 2012-05-26 12:47 +0000
      Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-26 14:16 +0000
    Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-26 13:48 +0000
      Re: More on Boolean Shortcircuiting Josh Grams <josh@qualdan.com> - 2012-05-27 00:17 +0000
  Re: More on Boolean Shortcircuiting BruceMcF <agila61@netscape.net> - 2012-05-26 08:27 -0700
    Re: More on Boolean Shortcircuiting BruceMcF <agila61@netscape.net> - 2012-05-26 10:25 -0700
      Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-27 10:47 +0000
        Re: More on Boolean Shortcircuiting BruceMcF <agila61@netscape.net> - 2012-05-27 07:00 -0700
          Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-27 17:20 +0000
            Re: More on Boolean Shortcircuiting BruceMcF <agila61@netscape.net> - 2012-05-27 15:03 -0700
  Re: More on Boolean Shortcircuiting "David N. Williams" <williams@umich.edu> - 2012-05-26 17:17 -0400
    Re: More on Boolean Shortcircuiting BruceMcF <agila61@netscape.net> - 2012-05-26 14:49 -0700
    Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-27 10:21 +0000
  Re: More on Boolean Shortcircuiting Josh Grams <josh@qualdan.com> - 2012-05-28 14:24 +0000
    Re: More on Boolean Shortcircuiting BruceMcF <agila61@netscape.net> - 2012-05-28 10:27 -0700
      Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-29 09:18 +0000
    Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-28 12:41 -0500
      Re: More on Boolean Shortcircuiting "Elizabeth D. Rather" <erather@forth.com> - 2012-05-28 07:55 -1000
    Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-29 11:34 +0000
    Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-29 13:53 +0000
      Re: More on Boolean Shortcircuiting Alex McDonald <blog@rivadpm.com> - 2012-05-29 07:02 -0700
        Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-30 01:45 +0000
          Re: More on Boolean Shortcircuiting BruceMcF <agila61@netscape.net> - 2012-05-29 20:02 -0700
            Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-30 04:27 +0000
              Re: More on Boolean Shortcircuiting Alex McDonald <blog@rivadpm.com> - 2012-05-30 02:09 -0700
                Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-30 11:07 +0000
                Re: More on Boolean Shortcircuiting Alex McDonald <blog@rivadpm.com> - 2012-05-30 04:38 -0700
                Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-30 06:46 -0500
                Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-30 13:52 +0000
                Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-30 09:02 -0500
                Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-30 14:13 +0000
                Re: More on Boolean Shortcircuiting Alex McDonald <blog@rivadpm.com> - 2012-05-30 07:41 -0700
                Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-30 11:46 -0500
                Re: More on Boolean Shortcircuiting BruceMcF <agila61@netscape.net> - 2012-05-31 09:18 -0700
                Re: More on Boolean Shortcircuiting Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-05-31 11:54 +0000
                Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-31 15:17 +0000
                Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-31 11:17 -0500
                Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-31 17:40 +0000
                Re: More on Boolean Shortcircuiting "Elizabeth D. Rather" <erather@forth.com> - 2012-05-31 08:43 -1000
                Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-06-01 02:56 +0000
                Re: More on Boolean Shortcircuiting anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-06-01 09:49 +0000
                Re: More on Boolean Shortcircuiting "Elizabeth D. Rather" <erather@forth.com> - 2012-06-01 09:18 -1000
                Re: More on Boolean Shortcircuiting Josh Grams <josh@qualdan.com> - 2012-05-31 16:45 +0000
                Re: More on Boolean Shortcircuiting BruceMcF <agila61@netscape.net> - 2012-05-31 09:58 -0700
                Re: More on Boolean Shortcircuiting Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-05-31 18:38 +0000
                Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-06-01 03:09 +0000
                Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-06-01 02:44 -0500
                Re: More on Boolean Shortcircuiting Mark Wills <markrobertwills@yahoo.co.uk> - 2012-06-01 04:57 -0700
                Re: More on Boolean Shortcircuiting anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-06-01 12:45 +0000
                Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-06-01 08:45 -0500
                Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-06-01 15:18 +0000
              Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-30 06:39 -0500
      Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-29 09:52 -0500
        Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-30 01:48 +0000
          Re: More on Boolean Shortcircuiting Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2012-05-30 07:55 +0100
          Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-30 02:56 -0500
            Re: More on Boolean Shortcircuiting Arnold Doray <invalid@invalid.com> - 2012-05-30 11:31 +0000
              Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-30 06:53 -0500
                Re: More on Boolean Shortcircuiting Alex McDonald <blog@rivadpm.com> - 2012-05-30 05:04 -0700
                Re: More on Boolean Shortcircuiting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-05-30 09:03 -0500
      Re: More on Boolean Shortcircuiting Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2012-05-29 17:49 +0100
        Re: More on Boolean Shortcircuiting Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2012-05-29 19:06 +0100
      Re: More on Boolean Shortcircuiting Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-05-29 20:25 +0000

csiph-web