• Eliminating STATE

    From none) (albert@21:1/5 to All on Tue Nov 8 12:45:15 2022
    You can restrict knowledge of STATE to the read/interpret loop.

    Take for instance number 1789 and assume that it is interpreted
    as a prefix word by `1 in the minimum search order.
    There is one behaviour associated with this:
    put 1789 on the stack.
    No compilation-semantics, no execution-semantics.

    It is easy for the interpreter to handle this. It has to keep
    track of stack depth and compile a literal in compile state
    if the stack changes.

    The method/field `state' of the interpret-object is local and
    can be fondled only by privileged words like [ and ] , belonging
    to the interpret-object.

    [ There is more to this, of course. Nobody are going
    to read this article.]

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ruvim@21:1/5 to albert on Wed Nov 9 06:03:56 2022
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").


    --
    Ruvim

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to ruvim.pinka@gmail.com on Wed Nov 9 11:03:03 2022
    In article <tkffsc$1ntf$1@dont-email.me>, Ruvim <ruvim.pinka@gmail.com> wrote: >On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").

    You can add a word COMPILATION? to the interpret object.
    That would be an addition to the kernel and require recompilation.
    That is cheating, so no.

    --
    Ruvim


    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@arcor.de@21:1/5 to none albert on Wed Nov 9 04:35:48 2022
    none albert schrieb am Mittwoch, 9. November 2022 um 11:03:07 UTC+1:
    In article <tkffsc$1ntf$1...@dont-email.me>, Ruvim <ruvim...@gmail.com> wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean, >without the word "state").
    You can add a word COMPILATION? to the interpret object.
    That would be an addition to the kernel and require recompilation.
    That is cheating, so no.


    Kill STATE and you have to recompile as well.
    Let COMPILATION? check whether there is a colon-sys on stack.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ruvim@21:1/5 to albert on Wed Nov 9 14:03:40 2022
    On 2022-11-09 10:03, albert wrote:
    In article <tkffsc$1ntf$1@dont-email.me>, Ruvim <ruvim.pinka@gmail.com> wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").

    You can add a word COMPILATION? to the interpret object.
    That would be an addition to the kernel and require recompilation.
    That is cheating, so no.

    No the interpret object. No cheating. In a standard program only.
    Even in two different ways.

    --
    Ruvim

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gerry Jackson@21:1/5 to Ruvim on Thu Nov 10 12:19:49 2022
    On 09/11/2022 06:03, Ruvim wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").


    One solution

    0 value st
    : compilation -1 to st s" 0 to st" evaluate st ; immediate

    Testing

    Gforth 0.7.9_20180905, Copyright (C) 1995-2017 Free Software Foundation,
    Inc.
    Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
    Type `help' for basic help
    ok
    ok
    0 value st ok
    : compilation -1 to st s" 0 to st" evaluate st ; immediate ok
    ok
    ok
    compilation . 0 ok
    : foo compilation literal . [ compilation ] literal . ; ok
    foo -1 0 ok

    A minor blemish is that it compiles "0 to st" when compiling so the
    value of ST returned is transient.

    --
    Gerry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to minf...@arcor.de on Thu Nov 10 18:03:59 2022
    In article <c12e858b-d351-42aa-8bd6-d5c0c18cd25bn@googlegroups.com>, minf...@arcor.de <minforth@arcor.de> wrote:
    none albert schrieb am Mittwoch, 9. November 2022 um 11:03:07 UTC+1:
    In article <tkffsc$1ntf$1...@dont-email.me>, Ruvim <ruvim...@gmail.com> wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").
    You can add a word COMPILATION? to the interpret object.
    That would be an addition to the kernel and require recompilation.
    That is cheating, so no.


    Kill STATE and you have to recompile as well.
    Let COMPILATION? check whether there is a colon-sys on stack.

    Sorry. Think about objects and information hiding.

    In this example the compilation stack has to be a separate stack,
    because the normal stack is used for literals/denotations/recognised-items.
    In the normal forth engine the compilation stack can only be used in
    certain ways, such as when `REPEAT resolves a branch offset.
    There are no ways to legally inspect the top of the colon
    stack. The standard provides only the restricted ways the compilation
    stack can be used. Moreover you can't tell whether it is colon-sys or not.

    You can decompile the Forth's `[ or `INTERPRET word and find out where
    the interpret object is keeping compilation state.
    That is cheating, also.

    Maybe I should provide a version of ciforth that
    1. lacks `STATE (the word)
    2. makes it impossible to make STATE-smart words, obviously
    3. behave normally otherwise .

    If you have a STATE-smart `." (contra Anton's Ertl advise) 1]
    you have to change
    ." we gaan naar rome"
    everywhere.
    These has to be replaced by
    "we gaan naar rome" TYPE

    1] that works in both interpret and compilation mode, with
    the usual pitfalls.

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ruvim@21:1/5 to Gerry Jackson on Thu Nov 10 16:48:27 2022
    On 2022-11-10 12:19, Gerry Jackson wrote:
    On 09/11/2022 06:03, Ruvim wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").


    One solution

    0 value st
    : compilation -1 to st s" 0 to st" evaluate st ; immediate
    [...]

    Yes! Though, I mean an ordinary word (particularly, not immediate).

    A variant without an additional variable:

    : compilation ( -- flag )
    -1 s" 0" evaluate dup if ['] drop compile, exit then nip
    ;



    Probably, according to the next version of the standard it should be
    wrapped by "catch", since a system will be obligated to throw an
    exception if a program appends something to an absent current definition
    (due to the "remove ambiguous conditions" movement).

    : compilation ( -- flag )
    [: -1 s" 0" evaluate dup if ['] drop compile, exit then nip ;]
    catch if -1 then
    \ actually, a particular throw code should be tested
    ;



    There is another standard-compliant solution.

    And yet another solution based on a system-specific method of creating dual-semantics words (if any).


    --
    Ruvim

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to ruvim.pinka@gmail.com on Thu Nov 10 18:51:15 2022
    In article <tkja0s$iqu2$1@dont-email.me>, Ruvim <ruvim.pinka@gmail.com> wrote: >On 2022-11-10 12:19, Gerry Jackson wrote:
    On 09/11/2022 06:03, Ruvim wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").


    One solution

    0 value st
    : compilation -1 to st s" 0 to st" evaluate st ; immediate
    [...]

    Yes! Though, I mean an ordinary word (particularly, not immediate).

    A variant without an additional variable:

    : compilation ( -- flag )
    -1 s" 0" evaluate dup if ['] drop compile, exit then nip
    ;



    Probably, according to the next version of the standard it should be
    wrapped by "catch", since a system will be obligated to throw an
    exception if a program appends something to an absent current definition
    (due to the "remove ambiguous conditions" movement).

    : compilation ( -- flag )
    [: -1 s" 0" evaluate dup if ['] drop compile, exit then nip ;]
    catch if -1 then
    \ actually, a particular throw code should be tested
    ;



    There is another standard-compliant solution.

    And yet another solution based on a system-specific method of creating >dual-semantics words (if any).

    Very clever. Now make a state smart word with that.

    Such as:

    : ."
    POSTPONE " STATE @ IF
    ['] TYPE COMPILE, ELSE TYPE THEN
    ; IMMEDIATE

    I admit that you come close at detecting the compilation state of Forth.

    [My point was that you don't need the STATE variable at all, not start
    a puzzle marathon.]

    --
    Ruvim
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ruvim@21:1/5 to albert on Thu Nov 10 18:51:37 2022
    On 2022-11-10 17:51, albert wrote:
    In article <tkja0s$iqu2$1@dont-email.me>, Ruvim <ruvim.pinka@gmail.com> wrote:
    On 2022-11-10 12:19, Gerry Jackson wrote:
    On 09/11/2022 06:03, Ruvim wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").


    One solution

    0 value st
    : compilation -1 to st s" 0 to st" evaluate st ; immediate
    [...]

    Yes! Though, I mean an ordinary word (particularly, not immediate).

    A variant without an additional variable:

    : compilation ( -- flag )
    -1 s" 0" evaluate dup if ['] drop compile, exit then nip
    ;



    Probably, according to the next version of the standard it should be
    wrapped by "catch", since a system will be obligated to throw an
    exception if a program appends something to an absent current definition
    (due to the "remove ambiguous conditions" movement).

    : compilation ( -- flag )
    [: -1 s" 0" evaluate dup if ['] drop compile, exit then nip ;]
    catch if -1 then
    \ actually, a particular throw code should be tested
    ;



    There is another standard-compliant solution.

    And yet another solution based on a system-specific method of creating
    dual-semantics words (if any).

    Very clever. Now make a state smart word with that.

    Such as:

    : ."
    POSTPONE " STATE @ IF
    ['] TYPE COMPILE, ELSE TYPE THEN
    ; IMMEDIATE


    : ." ( "ccc<quot>" -- )
    '"' parse compilation 0= if type exit then
    postpone sliteral postpone type
    ; immediate


    It works as expected.




    I admit that you come close at detecting the compilation state of Forth.

    [My point was that you don't need the STATE variable at all, not start
    a puzzle marathon.]


    Literally, your said that one *can* restrict knowledge of STATE to the read/interpret loop.

    And I have demonstrated that it's technically impossible to hide this
    knowledge from a program (i.e., the knowledge whether the system in interpretation state on in compilation state).


    Gerry Jackson also has provided a solution. It seems, some people here
    like Forth-puzzles, after all :)


    --
    Ruvim

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gerry Jackson@21:1/5 to Ruvim on Fri Nov 11 11:51:25 2022
    On 10/11/2022 18:51, Ruvim wrote:
    On 2022-11-10 17:51, albert wrote:
    In article <tkja0s$iqu2$1@dont-email.me>, Ruvim
    <ruvim.pinka@gmail.com> wrote:
    On 2022-11-10 12:19, Gerry Jackson wrote:
    On 09/11/2022 06:03, Ruvim wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean, >>>>> without the word "state").


    [ ...]

    I admit that you come close at detecting the compilation state of Forth.

    [My point was that you don't need the STATE variable at all, not start
    a puzzle marathon.]


    Literally, your said that one *can* restrict knowledge of STATE to the read/interpret loop.

    And I have demonstrated that it's technically impossible to hide this knowledge from a program (i.e., the knowledge whether the system in interpretation state on in compilation state).


    Gerry Jackson also has provided a solution. It seems, some people here
    like Forth-puzzles, after all :)

    Well it's much better than yet another long boring discussion about
    local variables versus using the stack. We could do with a few more
    puzzles and "how do you do ..." queries

    --
    Gerry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gerry Jackson@21:1/5 to Ruvim on Fri Nov 11 11:47:45 2022
    On 10/11/2022 16:48, Ruvim wrote:
    On 2022-11-10 12:19, Gerry Jackson wrote:
    On 09/11/2022 06:03, Ruvim wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").


    One solution

    0 value st
    : compilation -1 to st s" 0 to st" evaluate st ; immediate
    [...]

    Yes! Though, I mean an ordinary word (particularly, not immediate).

    Well you didn't state that :)

    A non-immediate word won't pass the test I did - it won't even compile
    : foo compilation literal . ;

    So to be useful a non-immediate COMPILATION has to be called inside an immediate word that tests the output of COMPILATION such as your later definition of ." (similarly CS-PICK and CS-ROLL have to be called in an immediate word).

    Removing IMMEDIATE from my definition of COMPILATION also works in your definition of ."


    A variant without an additional variable:

      : compilation ( -- flag )
        -1  s" 0" evaluate dup if ['] drop compile, exit then nip
      ;

    Yes that's a better definition


    Probably, according to the next version of the standard it should be
    wrapped by "catch", since a system will be obligated to throw an
    exception if a program appends something to an absent current definition
    (due to the "remove ambiguous conditions" movement).

      : compilation ( -- flag )
        [: -1  s" 0" evaluate dup if ['] drop compile, exit then nip ;]
        catch if -1 then
        \ actually, a particular throw code should be tested
      ;


    That suggests another standard-compliant solution (abusing the CATCH ...
    THROW concept!):

    : compilation [: s" 99 throw" evaluate ;] catch 0= ;
    : foo compilation ; immediate
    : bar foo [ . ] ; -1 ok
    foo . 0 ok

    Although, when interpreting, some systems (e.g. SwiftForth) display "99
    throw" as an error. That seems to me undesirable if not non-standard
    behaviour when a program handles the THROW code.


    There is another standard-compliant solution.

    Is that the above solution? I did actually think of using CATCH with an undefined word before my first post but rejected it because throwing an exception on an undefined word isn't standard behaviour. The thought of
    calling THROW in the quotation didn't occur to me at the time.

    --
    Gerry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ruvim@21:1/5 to Gerry Jackson on Fri Nov 11 16:21:53 2022
    On 2022-11-11 11:47, Gerry Jackson wrote:
    On 10/11/2022 16:48, Ruvim wrote:
    On 2022-11-10 12:19, Gerry Jackson wrote:
    On 09/11/2022 06:03, Ruvim wrote:
    On 2022-11-08 11:45, albert wrote:
    You can restrict knowledge of STATE to the read/interpret loop.

    In Forth, it's always possible to define a word
    compilation ( -- flag )
    that returns a flag whether the system in compilation state (I mean,
    without the word "state").


    One solution

    0 value st
    : compilation -1 to st s" 0 to st" evaluate st ; immediate
    [...]

    Yes! Though, I mean an ordinary word (particularly, not immediate).

    Well you didn't state that :)

    We should only state if a word is immediate (or specify interpretation/compilation semantics explicitly).

    Without additional notes a specified behavior is execution semantics,
    and the word is ordinary (i.e., the interpretation semantics and
    compilation semantics for the word are defined by default) — according
    to 3.4.3.



    A non-immediate word won't pass the test I did - it won't even compile
    : foo compilation literal . ;

    So to be useful a non-immediate COMPILATION has to be called inside an immediate word that tests the output of COMPILATION such as your later definition of ." (similarly CS-PICK and CS-ROLL have to be called in an immediate word).


    The definition for '."' also shows — to be useful not in tests but in practice, this word should be ordinary ;)



    Removing IMMEDIATE from my definition of COMPILATION also works in your definition of ."


    A variant without an additional variable:

       : compilation ( -- flag )
         -1  s" 0" evaluate dup if ['] drop compile, exit then nip
       ;

    Yes that's a better definition

    Also it is less dependent on the search order.



    Probably, according to the next version of the standard it should be
    wrapped by "catch", since a system will be obligated to throw an
    exception if a program appends something to an absent current
    definition (due to the "remove ambiguous conditions" movement).

       : compilation ( -- flag )
         [: -1  s" 0" evaluate dup if ['] drop compile, exit then nip ;]
         catch if -1 then
         \ actually, a particular throw code should be tested
       ;


    That suggests another standard-compliant solution (abusing the CATCH ... THROW concept!):

    : compilation [: s" 99 throw" evaluate ;] catch 0= ;
    : foo compilation ; immediate
    : bar foo [ . ] ; -1 ok
    foo . 0 ok

    Although, when interpreting, some systems (e.g. SwiftForth) display "99 throw" as an error.

    It should be so on any system. Since this "compilation" produces a side
    effect for the current definition in compilation state, it appends the execution semantics of "99 throw".

    So, this variant is incorrect.


    That seems to me undesirable if not non-standard
    behaviour when a program handles the THROW code.

    In compilation state it is not handled (caught), but compiled. So this
    "bar" is equivalent to:

    : bar 99 throw ;





    There is another standard-compliant solution.

    Is that the above solution?

    I thought about a totally different solution, that does not employ
    "evaluate" or alike.

    6.1.2250 says:

    | Only the following standard words alter the value in STATE:
    | : (colon), ; (semicolon), ABORT, QUIT, :NONAME, [ (left-bracket),
    | ] (right-bracket).

    And 15.6.2.2250 adds that ";CODE" also alter STATE.


    So, it's enough to redefine these words (except "abort") in a standard
    program:


    variable s

    : compilation ( -- flag ) s @ ;

    : ] ] s on ;
    : :noname :noname s on ;
    : : : s on ;

    : [ s off postpone [ ; immediate
    : ; s off postpone ; ; immediate
    : quit s off quit ;

    [defined] ;code [if]
    : ;code s off postpone ;code ; immediate
    [then]


    Concerning "abort" — if the corresponding exception is caught by a
    program, the STATE is not changed. If it is not caught, the program
    loses control (i.e., it's effectively terminated).


    --
    Ruvim

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Ruvim on Sat Nov 12 14:19:18 2022
    On 11/11/2022 5:51 am, Ruvim wrote:
    ...
    Literally, your said that one *can* restrict knowledge of STATE to the read/interpret loop.

    And I have demonstrated that it's technically impossible to hide this knowledge from a program (i.e., the knowledge whether the system in interpretation state on in compilation state).


    Gerry Jackson also has provided a solution. It seems, some people here like Forth-puzzles, after all :)

    It took its users no time at all to transform ANS-Forth into a puzzle :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gerry Jackson@21:1/5 to All on Sun Nov 13 20:35:21 2022
    On 10/11/2022 16:48, Ruvim wrote:

    Probably, according to the next version of the standard it should be
    wrapped by "catch", since a system will be obligated to throw an
    exception if a program appends something to an absent current
    definition (due to the "remove ambiguous conditions" movement).

    : compilation ( -- flag )
    [: -1 s" 0" evaluate dup if ['] drop compile, exit then nip ;]
    catch if -1 then
    \ actually, a particular throw code should be tested
    ;


    That suggests another standard-compliant solution (abusing the CATCH ... THROW concept!):

    : compilation [: s" 99 throw" evaluate ;] catch 0= ;
    : foo compilation ; immediate
    : bar foo [ . ] ; -1 ok
    foo . 0 ok

    Although, when interpreting, some systems (e.g. SwiftForth) display "99 throw" as an error.
    It should be so on any system. Since this "compilation" produces a side effect for the current definition in compilation state, it appends the execution semantics of "99 throw".

    So, this variant is incorrect.

    Yoou're right I should have tested it properly.

    That seems to me undesirable if not non-standard
    behaviour when a program handles the THROW code.
    In compilation state it is not handled (caught), but compiled. So this
    "bar" is equivalent to:

    : bar 99 throw ;

    There is another standard-compliant solution.

    Is that the above solution?
    I thought about a totally different solution, that does not employ
    "evaluate" or alike.

    6.1.2250 says:

    | Only the following standard words alter the value in STATE:
    | : (colon), ; (semicolon), ABORT, QUIT, :NONAME, [ (left-bracket),
    | ] (right-bracket).

    And 15.6.2.2250 adds that ";CODE" also alter STATE.


    So, it's enough to redefine these words (except "abort") in a standard program:


    variable s

    : compilation ( -- flag ) s @ ;

    : ] ] s on ;
    : :noname :noname s on ;
    : : : s on ;

    : [ s off postpone [ ; immediate
    : ; s off postpone ; ; immediate
    : quit s off quit ;

    [defined] ;code [if]
    : ;code s off postpone ;code ; immediate
    [then]


    I see, a mundane solution. I'm glad you posted it, that's saved me
    wasting any more time on it


    Concerning "abort" — if the corresponding exception is caught by a
    program, the STATE is not changed. If it is not caught, the program
    loses control (i.e., it's effectively terminated).


    --
    Gerry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)