Fixed the documentation a bit

Added some more info. It still looks awful.
This commit is contained in:
Linus 2021-03-07 22:19:13 +01:00
parent bb0de3e949
commit 74514bb4d1
3 changed files with 111 additions and 50 deletions

View file

@ -7,7 +7,8 @@
<section title="Preface">
We have all had the thought. *Why oh why are not all our problems solved by a nice recursive algorithm over a list?*. We see a neat problem. We smile and think about all the nice things we shall do right after finishing off this little tidbit, this delicious little bite of a problem. Then we scrape the surface. Under there is a jungle of state. Suddenly our little recursive list-mangling is not enough. To defeat the anxiety rising within us, we quickly write something using map, filter, zip and reduce. We test it. We smile again. We test it on real world data. The smile stiffens. The little bite has turned in to an o(n²) turd.
We raise our gaze and see the disgustingly mutating little wonders we could have achieved using python list comprehensions. We look sideways and see the magnificent, fantastic, but abominable loop macro in Common Lisp. Even they, the elitist ultra-productive scumbags, hate it, yet they sieze every moment to make underhanded comments about named lets. Why, oh why is the world not as elegant as we want it to be?
We raise our gaze and see the disgustingly mutating little wonders we could have achieved using
python list comprehensions. We look sideways and see the magnificent, fantastic, but abominable loop macro in Common Lisp. Even they, the elitist ultra-productive scumbags, hate it, yet they sieze every moment to make underhanded comments about named lets. Why, oh why is the world not as elegant as we want it to be?
We think again about how python programmers write scheme, and shudder. _No! Never that!_, we think to ourselves. A vague idea starts forming. A quote surfaces from the depths of our minds *"Above all the wonders of Lisp's pantheon stand its metalinguistic tools; by their grace have Lisp's acolytes been liberated from the rigid ascetism of lesser faiths"*. Maybe we should try to do it ourselves. Thinking about it, we have actually written some macros before. Anaphoric ifs, some boilerplate removal macros, and oh! Even an almost-working implementation of SRFI-26. It passed nearly all the tests, and we couldn't be bothered to go any further. But a looping facility... How hard can it be?
@ -32,7 +33,7 @@
(define (erathostenes n)
(define vec (make-vector n #t))
(loop/list ((:for i (up-from 2 (to n)))
:when (vector-ref vec i))
(:when (vector-ref vec i)))
(loop ((:for j (up-from (* 2 i) (to n) (by i))))
(vector-set! vec j #f))
i))
@ -45,38 +46,37 @@
<section title="Specification">
The loop grammar is the following:
<verbatim>
<example>
(loop [name] (loop-clause ...) [=> final-expr] body ...)
name = identifier
loop-clause = (:for id id* ... seq-expr)
| (:acc id id* ... seq-expr)
| :when guard-expr
| :unless guard-expr
| :break break-expr
| :final guard-expr
| (:when guard-expr)
| (:unless guard-expr)
| (:break break-expr)
| (:final guard-expr)
| :subloop
seq-expr = a macro that conforms to the looping protocol described below.
</verbatim>
</example>
If a `name` is provided, it will be bound to a macro that allows named update of loop variables:
<example>
(loop lp ((:for a (in 0 (+ a 1)))
:break (> a 9))
(:break (> a 9)))
=> '()
(if (= 4 a)
(cons a (lp (=> a 8)))
(cons a (lp))))
</example>
This rather inane example would return `(0 1 2 3 4 8 9)`.
This rather inane example would return `(0 1 2 3 4 8 9)`. Read more about this in the part about loop variables.
<subsection title="Subloops">
A subloop is a distinction between an outer loop and an inner loop. An for each element yielded by an outer loop, the inner loop is run until exhaustion. All non :for- or :acc-clauses break out a subloop.
A subloop is a distinction between an outer loop and an inner loop. A subloop means that: for each element yielded by an outer loop, the inner loop is run until exhaustion. All non-binding clauses break out a subloop.
<example>
(loop ((:for a (in-list '(1 2 3)))
@ -84,6 +84,7 @@
(:for b (up-from 0 (to a)))
(:acc acc (listing (cons a b))))
=> acc)
;; => ((1 . 0) (2 . 0) (2 . 1) (3 . 0) (3 . 1) (3 . 2))
</example>
@ -91,10 +92,11 @@
<example>
(loop ((:for a (in-list '(1 2 3)))
:break (= 3 a)
(:for b (up-from 0 (to a)))
(:acc acc (listing (cons a b))))
(:break (= 3 a))
(:for b (up-from 0 (to a)))
(:acc acc (listing (cons a b))))
=> acc)
;; => ((1 . 0) (2 . 0) (2 . 1))
</example>
@ -102,16 +104,33 @@
<example>
(loop ((:for a (in-list '(1 2 3)))
:final (= 3 a)
(:for b (up-from 0 (to a)))
(:acc acc (listing (cons a b))))
(:final (= 3 a))
(:for b (up-from 0 (to a)))
(:acc acc (listing (cons a b))))
=> acc)
;; => ((1 . 0) (2 . 0) (2 . 1) (3 . 0))
</example>
</subsection>
<subsection title="Loop variables">
To be written
Both accumulating clauses and :for clauses have something called loop variables. In the case of `(:for elt (in-list lst))` the loop variable would be the current pair where `elt` is the car. Some :acc- or :for-clauses may expose their loop variables so that they can be queried or even updated.
In the case of the menioned `in-list` we can choose to expose the current pair, as in the following example:
<example>
(define (interpose lst between)
(loop lp ((:for elt pair (in-list lst)))
=> '() ;; reached only if lst is empty
(if (null? (cdr pair))
(list elt)
(cons* elt between (lp)))))
(interpose '(1 2 3 4) ':)
; => (1 : 2 : 3 : 4)
</example>
In the above example we chose to bind the loop variable of `in-list` to `pair`. Using `pair` we can then query if the next iteration is the empty list and decide not to interpose any value.
</subsection>
@ -124,6 +143,8 @@
(b (in 1 (+ a b))))
b)
</example>
The simple forms provided by goof-loop are the following:
<spec>
<syntax name="loop/first">
@ -201,14 +222,14 @@
<form>(:for binding (up-from start [(to bound)] [(by step)]))</form>
<form>(:for binding (up-from start [bound [by]]))</form>
Binds `binding` to the number `start` up to `bound` (exclusive!) by `step`. If no `bound` is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteratiom.
Binds `binding` to the number `start` up to `bound` (exclusive!) by `step`. If no `bound` is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteration if a `step` other than `1` is wanted.
</syntax>
<syntax name="down-from">
<form>(:for binding (down-from start [(to bound)] [(by step)])</form>
<form>(:for binding (down-from start [bound [by]]))</form>
Binds `binding` to the number `(- start 1)` down to `bound` (inclusive!) by `step`. If no `bound` is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteratiom.
Binds `binding` to the number `(- start 1)` down to `bound` (inclusive!) by `step`. If no `bound` is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteration if a `step` other than `1` is wanted.
</syntax>
<syntax name="in-list">
@ -227,24 +248,32 @@
<form>(:for binding [index] (in-vector expr [low [high]]))</form>
Binds `binding` to all elements in the vector produced by `expr` in order from `low` to `high`. `low` defaults to 0 and `high` defaults to the last index of the vector.
The vector produced by `expr` is bound outside the loop body, and `index` is the loop variable.
</syntax>
<syntax name="in-reverse-vector">
<form>(:for binding [index] (in-reverse-vector expr [high [low]]))</form>
Binds `binding` to all elements in the vector produced by `expr` in reverse order from `high` to `low`. `high` defaults to the last element of the vector and `low` defaults to 0.
The vector produced by `expr` is bound outside the loop body, and `index` is the loop variable.
</syntax>
<syntax name="in-string">
<form>(:for binding [index] (in-string expr [low [high]]))</form>
Binds `binding` to all elements in the string produced by `expr` in order from `low` to `high`. `low` defaults to 0 and `high` defaults to the last index of the string.
The string produced by `expr` is bound outside the loop body, and `index` is the loop variable.
</syntax>
<syntax name="in-reverse-string">
<form>(:for binding [index] (in-reverse-string expr [high [low]]))</form>
Binds `binding` to all elements in the vector produced by `expr` in reverse order from `high` to `low`. `high` defaults to the last element of the vector and `low` defaults to 0.
The string produced by `expr` is bound outside the loop body, and `index` is the loop variable.
</syntax>
<syntax name="in-port">
@ -289,7 +318,7 @@
<example>
(loop ((:for a (up-from 0 10))
(:acc acc (listing a (if (odd? a))))
:when (even? a))
(:when (even? a)))
=> acc)
</example>
@ -400,6 +429,7 @@
;; This last one is finalizers. They run on any kind of exit from the same
;; loop or any subloop where the :for-clause is in scope.
() . rest))))
(register-loop-clause 'for #'in-alist)
</example>
In short, the clause (:for key value (in-alist alist-expr)) expands to:
@ -419,6 +449,8 @@
Body bindings are bound before anything except the stop guards are run. They may be an (ice-9 match) pattern, and are multiple-value aware. `(a b (div-and-mod 10 7))` is valid, and so is ((key . val) (let ((c (car %cursor))) (values (car c) (cdr c)))).
Finalizers are run everytime the current loop or any subloop below the current loop exits. This can be used to close resources. `(in-file ...)` uses this.
register-loop-clause registers the loop clause so that it can be verified to exist at expansion time.
</subsection>
<subsection title=":acc-clauses">
@ -443,6 +475,8 @@
;; (let ((name (reverse %cursor))) ...)
((name (reverse %cursor)))
. rest))))
(register-loop-clause 'acc #'alisting)
</example>
The first difference is that the first argument to an accumulator always is the symbol :acc.
@ -454,7 +488,8 @@
The body bindings can be used to bind variables to make information from the accumulator visible, but otherwise not used.
Final bindings. Binds whatever variable name you chose to whatever expression you chose in the final-expression.
register-loop-clause registers the alisting clause so that it can be verified to exist at expansion time.
</subsection>
</section>
@ -462,7 +497,7 @@
The main chunk of a loop expands into something like the following:
A goof loop expands into something looking like this:
<verbatim>
(let* (&lt;outer-let&gt; ...
(let* (&lt;outer-binding&gt; ...
final-function (lambda (&lt;final-binding&gt;) &lt;final-expr&gt;))
(let goof-loop ((&lt;accumulator&gt; &lt;accumulator-init&gt;) ... (&lt;loop-var&gt; &lt;loop-var-init&gt;) ...)
(if (or &lt;check&gt; ...)
@ -482,7 +517,7 @@
(goof-loop &lt;accumulate&gt; ... &lt;loop-var-next&gt; ...))
(goof-loop &lt;accumulator&gt; ... &lt;loop-var-next&gt; ...)))))))))
&lt;outer-let&gt;: are provided by accumulators or for clauses for bindings that are not passed as an argument to the loop, for example a vector. The vector is bound here, and the index into the vector is the thing iterated over.
&lt;outer-binding&gt;: are provided by accumulators or for clauses for bindings that are not passed as an argument to the loop, for example a vector. The vector is bound here, and the index into the vector is the thing iterated over.
&lt;final-binding&gt; and &lt;final-expr&gt;: When the iteration ends, this function is called with the results of the :acc clauses. In the case of (:acc lst-acc (listing ...)), the name of the accumulator is never lst-acc in the loop body, but only in the &lt;final-expr&gt;. In case of (listing ...) the accumulated results are reversed before the final function.