-- Leo's gemini proxy

-- Connecting to thrig.me:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

Redefined


Forth is a bit unusual in terms of word redefinition, which are something like subroutines in other languages. In most languages the previous definition is clobbered, and sometimes you'll get a warning to that effect.


    $ cat redef
    #!/usr/bin/perl
    use 5.36.0;
    sub foo { 42 }
    sub bar { foo }
    sub foo { 43 }
    say foo
    $ perl redef
    Subroutine foo redefined at redef line 5.
    43

    (defun foo () 42)
    (defun bar () (foo))
    (defun foo () 43)
    (bar)

In a forth there may be a warning, though the previous definition still exists and will be used by words defined prior to the redefinition. gforth is a pretty common implementation that is readily available on a number of unixlike systems.


    $ cat redef2
    : foo 42 ;
    : bar foo ;
    : foo 43 ;
    foo bar foo
    .s
    $ gforth < redef2
    : foo 42 ;  ok
    : bar foo ;  ok
    : foo 43 ; redefined foo   ok
    foo bar foo  ok
    .s <3> 43 42 43  ok

The BAR word here puts 42 onto the stack (which the .S word shows) by way of the original FOO, which is still in the dictionary. Subsequent use of FOO will however find the latest definition, here one that instead puts 43 onto the stack. Some forth have FORGET, if you want to revert to some prior definition, also minus everything you've added since that point. This was probably useful on memory constrained systems: do some work, forget back to some point to free up memory, then make new words for new work.


Even more weird is perhaps troff, which lets you append to existing macros, not just redefine them.


    $ cat append
    .de xx  \" define macro xx ...
    .sp     \" vertical space
    ..
    1       \" use the macro a few times
    .xx
    2
    .xx
    3
    .am xx  \" append to macro xx
    .sp -3  \" effective motion now -2 with prior .sp
    \0      \" indent a digit
    ..
    4       \" use the redefined macro a few times
    .xx
    5
    .xx
    6
    $ doas pkg_add ghostscript heirloom-doctools
    ...
    $ PATH=/usr/local/heirloom-doctools:$PATH
    $ troff append | dpost | ps2pdf - example.pdf

example.pdf


This may be about as useful as some old tree in some old Chinese text.


tags #forth #troff

-- Response ended

-- Page fetched on Tue May 21 15:43:58 2024