In article <
2023Aug1...@mips.complang.tuwien.ac.at>,
Anton Ertl <
an...@mips.complang.tuwien.ac.at> wrote:
>albert@cherry.(none) (albert) writes:
>>I finally understood what closures mean.
>>In Forth parlance it is a search order that is kept with an
>>Forth word.
>
>That view leads down to boy compilers (in Knuth's man-or-boy test),
>and at best (if you save and restore the variables on function entry
>and exit) dynamically-scoped Lisp.
I'm an amateur. I implement MAL. I don't know whether that is a
dynamically-scoped Lisp. Actually my goal is to prove that
Forth is capable of parsing lisp itself, with a modular parser
in the style of BEGIN WHILE REPEAT words.
For example the rule that comma is to be treated as blank space
translate to the rule:
: , ; PREFIX
>
>>An environment is a wordlist,
>
>A wordlist has only one instance of each word in a wordlist.
Okay, so we have multiple instance of a wordlist.
>
>An environment in a statically-scoped language is a set of local
>frames, where each local frame is created dynamically when the
>function to which the frame belongs is called. So if a function has
>two instances at the same time (e.g., in recursion), a wordlist is
>insufficient.
That would mean that
(def! fib (fn* (N) (if (= N 0) 1
(if (= N 1) 1 (+ (fib (- N 1))
(fib (- N 2)))))))
would fail, but it isn't:
(fib 10)
89
Also the quine tests succeeds. It was commented out of the tests,
because the line was too long. (and it is too long for a usenet post,
sorry). That was not a restriction in my implementation.
;;;;; Test quine
((fn* [q] (quasiquote ((unquote q) (quote (unquote q))))) (quote (fn* [q] (quasiquote ((unquote q) (quote (unquote q)))))))
;=>((fn* [q] (quasiquote ((unquote q) (quote (unquote q))))) (quote (fn* [q] (quasiquote ((unquote q) (quote (unquote q)))))))
What fails is:
( ( (fn* (a) (fn* (b) (+ a b))) 5) 7)
(b) (+ a b))) 5) 7) ? ciforth ERROR # 8010
Error 8010 means that a symbol is not found in the nested environments.
`` a '' is not found.
The phrase clearly means that in the environment that `a has the
value 7, a new instance of the function has to be generated.
>
>You can use wordlists to store the offsets of variable within the
>frames, but you have to manage the frames separately.
I think I do with the proposed mechanism.
We shall see.
>
>- anton