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


Groups > comp.lang.forth > #45329

The extended Eaker-Pelc CASE as general control structure

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject The extended Eaker-Pelc CASE as general control structure
Date 2016-02-06 18:15 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2016Feb6.191548@mips.complang.tuwien.ac.at> (permalink)

Show all headers | View raw


A few weeks agod we were discussing (again) about using

begin .. while .. while .. again then then

and other ways to program multi-exit loops.  Someone pointed out
(again) that a generalized CASE achieves this in a nicer way.  At that
time I concluded that we probably don't really need this, based on the
observation that I had implemented it, but not used it.  I then looked
at my mails and postings, and found (again) that the CASE
implementation of Gforth is not standard-conformant (being based on
the flawed implementation in Section A.3.2.3.2 in the standard).  I
also found that the then-current implementation of the extended CASE
in Gforth was a little more complex than necessary.

So I set about to implement CASE again, in a standard-conformant way,
and while I was at it, also implemented the extensions.  The
extensions are three relatively simple words: ?OF, NEXT-CASE, and
CONTOF.

Given the relative cheapness in implementation and documentation, I
reconsidered my verdict: While one may rarely need the extensions,
they cost little, and IMO the benefit over the other approaches is
sufficient to justify them.  More on that below.

The new implementation is written in almost-standard Forth; it assumes
that control-flow stack items are on the data stack (at least
partially).  It has been tested and works on Gforth, SwiftForth, and
VFX.  You can find it in the package
<http://theforth.net/package/compat/current.zip> in file caseext.fs;
or, if you want to look at the source code, look at
<http://theforth.net/package/compat/current-view/caseext.fs>.

On the benefits: Consider a loop with three exit conditions, with a
different action necessary on each exit, and afterwards a common
action is necessary.  With the extended CASE you can write this as

case
  condition1 ?of exit-action1 endof
  condition2 ?of exit-action2 endof
  condition3 ?of exit-action3 endof
  ...
next-case
common-action

If you want to do this with EXIT or a multi-WHILE loop, things get
cumbersome.  Of course, Forthers often try to rationalize such
problems as encouraging factoring or somesuch, but if it really is
better factoring, do you really need to restrict your tools to achieve
this; and the workarounds for such problems are not always better
factoring.

That example does not demonstrate CONTOF: CONTOF is useful if you have
different iteration actions depending on the condition.  Here's an
example:

: gcd ( n1 n2 -- n )
    case
        2dup > ?of tuck - contof
        2dup < ?of over - contof
    endcase ;

Here the two ?OFs have different ways of continuing the loop; when
neither ?OF triggers, the two numbers are equal and are the gcd.
ENDCASE drops one of them, leaving the other as n (usually the
dropping nature of ENDCASE is a nuissance in combination with ?OF,
here it happens to work out).

Note that this kind of CASE usage is similar to Dijsktra's guarded
command "do", and
<https://en.wikipedia.org/wiki/Guarded_Command_Language#Original_Euclidean_algorithm>
gives the same algorithm as example.

And here's an example where the all words except ENDCASE are used:

: collatz ( u -- )
    \ print the 3n+1 sequence starting at u until we reach 1
    case
        dup .
        1 of endof
        dup 1 and ?of 3 * 1+ contof
        2/
    next-case ;

This example keeps the current value of the sequence on the stack.  If
it is 1, the OF triggers, drops the value, and leaves the
CASE structure.  For odd numbers, the ?OF triggers,
computes 3n+1, and starts the next iteration with CONTOF.
Otherwise, if the number is even, it is divided by 2, and the loop is
restarted with NEXT-CASE.

If you are a word-saver, you can note that ?OF is an alias of IF, and
one could do without it (however, IMO ?OF makes the intention
clearer).  Also, one could do without CONTOF (or alternatively without
NEXT-CASE) without losing generality (but one would lose conciseness
of expression).  But I think that three words is cheap enough.

But I already saved some words compared to what's currently in VFX:
CASE also has the function of VFX's BEGINCASE; and I only have ENDCASE
(no END-CASE) and NEXT-CASE (no NEXTCASE); I chose these because
ENDCASE is entrenched, and NEXT-CASE appears more useful than NEXTCASE
(which drops a value).

Credits: AFAIK the idea to generalize CASE, and for ?OF and NEXT-CASE
came from Stephen Pelc, and has been implemented in a similar way in
VFX for quite some time; the original CASE came from Chuck Eaker.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2015: http://www.rigwit.co.uk/EuroForth2015/

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


Thread

The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-06 18:15 +0000
  Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-07 03:57 -0800
    Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 12:59 +0000
      Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-08 09:48 -0800
        Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 18:10 +0000
          Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-09 14:02 -0800
            Short-circuit evaluation (was: The extended Eaker-Pelc CASE ...) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-10 16:10 +0000
  Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-07 14:32 +0000
  Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-07 14:53 +0000
    Re: The extended Eaker-Pelc CASE as general control structure Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-07 15:56 +0000
      Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-07 18:11 +0000
        Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-08 12:04 +1100
          Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 10:29 +0000
            Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 11:00 +0000
              Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 12:28 +0000
                Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 15:26 +0000
                Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 16:16 +0000
                Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 16:37 +0000
                Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 16:21 +0000
            Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-11 21:12 +1100
              Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-11 10:46 +0000
      Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-07 12:33 -0800
        Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 10:38 +0000
  Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-08 12:07 +1100
    Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 11:11 +0000
      Re: The extended Eaker-Pelc CASE as general control structure Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-08 14:49 +0000
      Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-09 20:31 +1100
        Re: The extended Eaker-Pelc CASE as general control structure hughaguilar96@gmail.com - 2016-02-09 12:38 -0800
      Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-11 14:23 +0000
        Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-11 19:45 +0000

csiph-web