@@ -1,1044 +0,0 @@
7−So let's learn Nock! But wait - why learn Nock? After all,
8−we're going to be programming in Hoon, not Nock.
10−Like JVM bytecode, Nock is as inscrutable as assembly language.
11−In fact, you can think of it as a sort of "functional assembly
12−language." There are sometimes reasons to program in real
13−assembly language. There is never a reason to program in Nock.
16−Indeed, it is not necessary for the Hoon programmer to learn
17−Nock. We recommend it strongly, however, because Hoon has a very
18−special relationship with Nock - not unlike the relationship
19−between C and assembly language.
21−Just as C is a very shallow layer over the raw CPU, Hoon is a
22−very shallow layer over raw Nock - often little more than a
23−macro. If you try to learn C without understanding the CPU under
24−it, you will be forever bemused by why it works the way it does.
26−So let's learn Nock! But wait - which Nock? Nock, though more
27−frozen than Walt Disney, does have versions. Nock versions are
28−measured by integer degrees Kelvin, newer being colder. The
29−newest, Nock 5K - roughly the temperature of Neptune. No change
30−is anticipated between 5K and absolute zero, though you never
31−know. Any such change would certainly be quite painful.
35−The best way to learn Nock is to read the spec and write your own
36−naive interpreter. Here is Nock 5K:
40− A noun is an atom or a cell. An atom is a natural number.
41− A cell is an ordered pair of nouns.
46− 2 :: [a b c] [a [b c]]
59− 15 :: /[(a + a) b] /[2 /[a b]]
60− 16 :: /[(a + a + 1) b] /[3 /[a b]]
63− 19 :: *[a [[b c] d]] [*[a [b c]] *[a d]]
65− 21 :: *[a [0 b]] /[b a]
67− 23 :: *[a [2 [b c]]] *[*[a b] *[a c]]
68− 24 :: *[a [3 b]] ?*[a b]
69− 25 :: *[a [4 b]] +*[a b]
70− 26 :: *[a [5 b]] =*[a b]
72− 28 :: *[a [6 [b [c d]]]] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
73− 29 :: *[a [7 [b c]]] *[a 2 b 1 c]
74− 30 :: *[a [8 [b c]]] *[a 7 [[7 [0 1] b] 0 1] c]
75− 31 :: *[a [9 [b c]]] *[a 7 c 2 [0 1] 0 b]
76− 32 :: *[a [10 [[b c] d]]] *[a 8 c 7 [0 3] d]
77− 33 :: *[a [10 [b c]]] *[a c]
81−Your interpreter should be no more than a page of code in
82−any language. For extra credit, `6`-`10` are macros; implement
83−them directly. For extra extra credit, optimize tail calls.
84−To test your code, write a decrement formula b such that
85−`*[a b]` is `a - 1` for any atomic nonzero `a`.
89−The second best way to learn Nock is to boot up your own Arvo
90−virtual computer. See the Arvo tutorial for instructions.
94−To make Nock make sense, let's work through Nock 5K line by line.
99− A noun is an atom or a cell. An atom is any natural number.
100− A cell is any ordered pair of nouns.
102−Nouns are the dumbest data model ever. Nouns make JSON look like
103−XML and XML look like ASN.1. It may also remind you of Lisp's
104−S-expressions - you can think of nouns as "S-expressions without
107−To be exact, a noun _is_ an S-expression, except that classic
108−S-expressions have multiple atom types ("S" is for "symbol").
109−Since Nock is designed to be used with a higher-level type system
110−(such as Hoon's), it does not need low-level types. An atom is
111−just an unsigned integer of any size.
113−For instance, it's common to represent strings (or even whole
114−text files) as atoms, arranging them LSB first - so "foo" becomes
115−`0x6f6f66`. How do we know to print this as "foo", not `0x6f6f66`?
116−We need external information - such as a Hoon type. Similarly,
117−other common atomic types - signed integers, floating point, etc
118−- are all straightforward to map into atoms.
120−It's also important to note that, unlike Lisp, Nock cannot create
121−cyclical data structures. It is normal and common for nouns in a
122−Nock runtime system to have acyclic structure - shared subtrees.
123−But there is no Nock computation that can make a child point to
124−its parent. One consequence: Nock has no garbage collector.
125−(Nor can dag structure be detected, as with Lisp `eq`.)
127−There is also no single syntax for nouns. If you have nouns you
128−have Nock; if you have Nock you have Hoon; if you have Hoon, you
129−can write whatever parser you like.
135−It's important to recognize that the pseudocode of the Nock spec
136−is just that: pseudocode. It looks a little like Hoon. It isn't
137−Hoon - it's just pseudocode. Or in other words, just English.
138−At the bottom of every formal system is a system of axioms, which
139−can only be written in English. (Why pseudocode, not Hoon? Since
140−Hoon is defined in Nock, this would only give a false impression
141−of nonexistent precision.)
143−The logic of this pseudocode is a pattern-matching reduction,
144−matching from the top down. To compute Nock, repeatedly reduce
145−with the first line that matches. Let's jump right in!
151−Nock is a pure (stateless) function from noun to noun. In our
152−pseudocode (and only in our pseudocode) we express this with the
155−This function is defined for every noun, but on many nouns it
156−does nothing useful. For instance, if `a` is an atom, `*a`
157−reduces to... `*a`. In theory, this means that Nock spins
158−forever in an infinite loop. In other words, Nock produces no
159−result - and in practice, your interpreter will stop.
161−(Another way to see this is that Nock has "crash-only" semantics.
162−There is no exception mechanism. The only way to catch Nock
163−errors is to simulate Nock in a higher-level virtual Nock -
164−which, in fact, we do all the time. A simulator (or a practical
165−low-level interpreter) can report, out of band, that Nock would
166−not terminate. It cannot recognize all infinite loops, of
167−course, but it can catch the obvious ones - like `*42`.)
169−Normally `a` in `nock(a)` is a cell `[s f]`, or as we say
173−Intuitively, the formula is your function and the subject is
174−its argument. We call them something different because Hoon,
175−or any other high-level language built on Nock, will build its
176−own function calling convention which *does not* map directly
177−to `*[subject formula]`.
181− 2 :: [a b c] [a [b c]]
183−Ie, brackets (in our pseudocode, as in Hoon) associate to the
184−right. For those with Lisp experience, it's important to note
185−that Nock and Hoon use tuples or "improper lists" much more
186−heavily than Lisp. The list terminator, normally 0, is never
187−automatic. So the Lisp list
191−becomes the Nock noun
195−which is equivalent to
199−Note that we can and do use unnecessary brackets anyway, for
202−Let's move on to the axiomatic functions.
213−Here we define more pseudocode operators, which we'll use in
214−reductions further down. So far we have four built-in functions:
215−`*` meaning Nock itself, `?` testing whether a noun is a cell or
216−an atom, `+` incrementing an atom, and `=` testing for equality.
217−Again, no rocket science here.
219−We should note that in Nock and Hoon, `0` (pronounced "yes") is
220−true, and `1` ("no") is false. Why? It's fresh, it's different,
221−it's new. And it's annoying. And it keeps you on your toes.
222−And it's also just intuitively right.
230− 15 :: /[(a + a) b] /[2 /[a b]]
231− 16 :: /[(a + a + 1) b] /[3 /[a b]]
233−Slightly more interesting is our tree numbering. Every noun is of course a tree. The `/` operator - pronounced
234−"slot" - imposes an address space on that tree, mapping every
235−nonzero atom to a tree position.
237−1 is the root. The head of every node `n` is `2n`; the tail is
238−`2n+1`. Thus a simple tree:
245−If the value of every leaf is its tree address, this tree is
249−and, for some examples of addressing:
251− /[1 [[4 5] [6 14 15]]]
253−is `[[4 5] [6 14 15]]`
255− /[2 [[4 5] [6 14 15]]]
259− /[3 [[4 5] [6 14 15]]]
263− /[7 [[4 5] [6 14 15]]]
267−I do hope this isn't so terribly hard to follow.
271−Now we enter the definition of Nock itself - ie, the `*`
274− 21 :: *[a 0 b] /[b a]
276−`0` is simply Nock's tree-addressing operator. Let's try it out
277−from the Arvo command line.
279−Note that we're using Hoon syntax here. Since we do not use Nock
280−from Hoon all that often (it's sort of like embedding assembly in
281−C), we've left it a little cumbersome. In Hoon, instead of
282−writing `*[a 0 b]`, we write
286−So, to reuse our slot example, let's try the interpreter:
288− ~tasfyn-partyv> .*([[4 5] [6 14 15]] [0 7])
290−gives, while the sky remains blue and the sun rises in the east:
294−Even stupider is line 21:
300−`1` is the constant operator. It produces its argument without
301−reference to the subject. So
303− ~tasfyn-partyv> .*(42 [1 153 218])
312− 23 :: *[a 2 b c] *[*[a b] *[a c]]
314−Line 22 brings us the essential magic of recursion.
315−`2` is the Nock operator. If you can compute a subject and a
316−formula, you can evaluate them in the interpreter. In most
317−fundamental languages, like Lisp, `eval` is a curiosity. But
318−Nock has no `apply` - so all our work gets done with `2`.
320−Let's convert the previous example into a stupid use of `2`:
322− ~tasfyn-partyv> .*(77 [2 [1 42] [1 1 153 218]])
324−with a constant subject and a constant formula, gives the same
330− *[77 [2 [1 42] [1 1 153 218]]]
332− 22 :: *[a 2 b c] *[*[a b] *[a c]]
334− *[*[77 [1 42]] *[77 [1 1 153 218]]]
338− *[42 *[77 [1 1 153 218]]]
346− 24 :: *[a 3 b] ?*[a b]
347− 25 :: *[a 4 b] +*[a b]
348− 26 :: *[a 5 b] =*[a b]
350−In lines 23-25, we meet our axiomatic functions again:
352−For instance, if `x` is a formula that calculates some product,
353−`[4 x]` calculates that product plus one. Hence:
355− ~tasfyn-partyv> .*(57 [0 1])
360− ~tasfyn-partyv> .*([132 19] [0 3])
365− ~tasfyn-partyv> .*(57 [4 0 1])
370− ~tasfyn-partyv> .*([132 19] [4 0 3])
373−If this seems obvious, you're doin' good. Finally, we jump back up
374−to line 18, the trickiest in the spec:
378− 19 :: *[a [b c] d] [*[a b c] *[a d]]
382−Since Nock of an atom just crashes, the practical domain of the
383−Nock function is always a cell. Conventionally, the head of this
384−cell is the "subject," the tail is the "formula," and the result
385−of Nocking it is the "product." Basically, the subject is your
386−data and the formula is your code.
388−We could write line 19 less formally:
390− *[subject [formula-x formula-y]]
391− => [*[subject formula-x] *[subject formula-y]]
393−In other words, if you have two Nock formulas `x` and `y`, a
394−formula that computes the pair of them is just `[x y]`. We can
395−recognize this because no atom is a valid formula, and
396−every formula that _does not_ use line 19 has an atomic head.
398−If you know Lisp, you can think of this feature as a sort of
399−"implicit cons." Where in Lisp you would write `(cons x y)`,
400−in Nock you write `[x y]`.
404− ~tasfyn-partyv> .*(42 [4 0 1])
406−where `42` is the subject (data) and `[4 0 1]` is the formula
407−(code), happens to evaluate to `43`. Whereas
409− ~tasfyn-partyv> .*(42 [3 0 1])
411−is `1`. So if we evaluate
413− ~tasfyn-partyv> .*(42 [[4 0 1] [3 0 1]])
419−Except for the crash defaults (lines 6, 10, 17, and 35), we've actually
420−completed all the _essential_ aspects of Nock. The operators up
421−through 5 provide all necessary computational functionality.
422−Nock, though very simple, is actually much more complex than it
423−formally needs to be.
425−Operators 6 through 10 are macros. They exist because Nock is
426−not a toy, but a practical interpreter. Let's see them all
431− 28 :: *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
432− 29 :: *[a 7 b c] *[a 2 b 1 c]
433− 30 :: *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
434− 31 :: *[a 9 b c] *[a 7 c 2 [0 1] 0 b]
435− 32 :: *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
436− 33 :: *[a 10 b c] *[a c]
438−Whoa! Have we entered rocket-science territory? Let's try to
439−figure out what these strange formulas do - simplest first.
440−The simplest is clearly line 33:
442− 33 :: *[a 10 b c] *[a c]
444−If `x` is an atom and `y` is a formula, the formula `[10 x y]`
445−appears to be equivalent to... `y`. For instance:
447− ~tasfyn-partyv> .*([132 19] [10 37 [4 0 3]])
450−Why would we want to do this? `10` is actually a hint operator.
451−The `37` in this example is discarded information - it is not
452−used, formally, in the computation. It may help the interpreter
453−compute the expression more efficiently, however.
455−Every Nock computes the same result - but not all at the same
456−speed. What hints are supported? What do they do? Hints are a
457−higher-level convention which do not, and should not, appear in
458−the Nock spec. Some are defined in Hoon. Indeed, a naive Nock
459−interpreter not optimized for Hoon will run Hoon quite poorly.
460−When it gets the product, however, the product will be right.
462−There is another reduction for hints - line 32:
464− 32 :: *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
466−Once we see what `7` and `8` do, we'll see that this complex hint
467−throws away an arbitrary `b`, but computes the formula `c`
468−against the subject and... throws away the product. This formula
469−is simply equivalent to `d`. Of course, in practice the product
470−of `c` will be put to some sordid and useful use. It could even
471−wind up as a side effect, though we try not to get _that_ sordid.
473−(Why do we even care that `c` is computed? Because `c` could
474−crash. A correct Nock cannot simply ignore it, and treat both
475−variants of `10` as equivalent.)
477−We move on to the next simplest operator, `7`. Line 29:
479− 29 :: *[a 7 b c] *[a 2 b 1 c]
481−Suppose we have two formulas, `b` and `c`. What is the formula
482−`[7 b c]`? This example will show you:
484− ~tasfyn-partyv> .*(42 [7 [4 0 1] [4 0 1]])
487−`7` is an old mathematical friend, function composition. It's
488−easy to see how this is built out of `2`. The data to evaluate
489−is simply `b`, and the formula is `c` quoted.
491−Line 30 looks very similar:
493− 30 :: *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
495−Indeed, `8` is `7`, except that the subject for `c` is not simply
496−the product of `b`, but the ordered pair of the product of `b`
497−and the original subject. Hence:
499− ~tasfyn-partyv> .*(42 [8 [4 0 1] [0 1]])
504− ~tasfyn-partyv> .*(42 [8 [4 0 1] [4 0 3]])
507−Why would we want to do this? Imagine a higher-level language
508−in which the programmer declares a variable. This language is
509−likely to generate an `8`, because the variable is computed
510−against the present subject, and used in a calculation which
511−depends both on the original subject and the new variable.
513−For extra credit, explain why we can't just define
515− *[a 8 b c] *[a 7 [b 0 1] c]
517−Another simple macro is line 31:
519− 31 :: *[a 9 b c] *[a 7 c 2 [0 1] 0 b]
521−`9` is a calling convention. With `c`, we produce a noun which
522−contains both code and data - a _core_. We use this core as the
523−subject, and apply the formula within it at slot `b`.
525−And finally, we come to the piece de resistance - line 28:
527− 28 :: *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
529−Great giblets! WTF is this doing? It seems we've finally
530−arrived at some real rocket science.
532−Actually, `6` is a primitive known to every programmer - good old
533−"if." If `b` evaluates to `0`, we produce `c`; if `b` evaluates
534−to `1`, we produce `d`; otherwise, we crash.
538− ~tasfyn-partyv> .*(42 [6 [1 0] [4 0 1] [1 233]])
543− ~tasfyn-partyv> .*(42 [6 [1 1] [4 0 1] [1 233]])
546−In real life, of course, the Nock implementor knows that `6` is
547−"if" and implements it as such. There is no practical sense in
548−reducing through this macro, or any of the others. We could have
549−defined "if" as a built-in function, like increment - except that
550−we can write "if" as a macro. If a funky macro.
552−It's a good exercise, however, to peek inside the funk.
554−We can actually simplify the semantics of `6`, at the expense of
555−breaking the system a little, by creating a macro that works as
556−"if" only if `b` is a proper boolean and produces `0` or `1`.
557−Perhaps we have a higher-level type system which checks this.
559−This simpler "if" would be:
561− *[a 6 b c d] *[a [2 [0 1] [2 [1 c d] [[1 0] [4 4 b]]]]]
563−Or without so many unnecessary brackets:
565− *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] [4 4 b]]
567−How does this work? We've replaced `[6 b c d]` with the formula
568−`[2 [0 1] [2 [1 c d] [[1 0] [4 4 b]]]]`. We see two uses of `2`,
569−our evaluation operator - an outer and an inner.
571−Call the inner one `i`. So we have `[2 [0 1] i]`. Which means
572−that, to calculate our product, we use `[0 1]` - that is, the
573−original subject - as the subject; and the product of `i` as
576−Okay, cool. So `i` is `[2 [1 c d] [[1 0] [4 4 b]]]`. We compute
577−Nock with subject `[1 c d]`, formula `[[1 0] [4 4 b]]`.
579−Obviously, `[1 c d]` produces just `[c d]` - that is, the ordered
580−pair of the "then" and "else" formulas. `[[1 0] [4 4 b]]` is a
581−line 19 cell - its head is `[1 0]`, producing just `0`, its tail
582−`[4 4 b]`, producing... what? Well, if `[4 b]` is `b` plus `1`,
583−`[4 4 b]` is `b` plus `2`.
585−We're assuming that `b` produces either `0` or `1`. So `[4 4 b]`
586−yields either `2` or `3`. `[[1 0] [4 4 b]]` is either `[0 2]` or
587−`[0 3]`. Applied to the subject `[c d]`, this gives us either
588−`c` or `d` - the product of our inner evaluation `i`. This is
589−applied to the original subject, and the result is "if."
591−But we need the full power of the funk, because if `b` produces,
592−say, `7`, all kinds of weirdness will result. We'd really like
593−`6` to just crash if the test product is not a boolean. How can
594−we accomplish this? This is an excellent way to prove to
595−yourself that you understand Nock: figure out what the real `6`
596−does. Or you could just agree that `6` is "if," and move on.
598−(It's worth noting that in practical, compiler-generated Nock, we
599−never do anything as funky as these `6` macro internals. There's
600−no reason we couldn't build formulas at runtime, but we have no
601−reason to and we don't - except when actually metaprogramming.
602−As in most languages, normally code is code and data is data.)
604−#1.4 Decrement in Nock#
606−A good practice exercise for Nock is a decrement formula. Ie, a
607−formula `f` which implements the partial function that produces
608−`(s - 1)` if `s` is a nonzero atom, and otherwise does not
611−The normal Hoon programmer has written one Nock formula: this
612−one. Since decrement uses all the Nock techniques the Hoon
613−compiler uses, the exercise is a good foundation. After you
614−write decrement (or just follow this example), you'll never need
615−to deal with Nock again.
617−As we know, the equivalent formula for increment is
623− ~>tasfyn-partyv .*(42 [4 0 1])
626−Of course, increment is built into Nock. So, ha, that's easy.
628−How do we decrement? A good way to start is to gaze fondly on
629−how we'd do it if we actually had a real language, ie, Hoon.
630−Here is a minimal decrement in Hoon:
635− ?: =(a +(b)) :: line 4
639−Or for fun, on one line:
641− =>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b))))))
643−Does Hoon actually work?
645− ~tasfyn-partyv> =>(42 =>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b)))))))
648−Let's translate this into English. How do we decrement the
649−subject? First (line 1), we rename the subject `a`. Second
650−(line 2), we add a variable, `b`, an atom with value `0`.
651−Third (line 3), we loop. Fourth, we test if `a` equals `b` plus
652−1 (line 4), produce `b` if it does (line 5), repeat the loop with
653−`b` set to `b` plus 1 (line 6) if it doesn't. Obviously, while
654−the syntax is unusual, the algorithm is anything but deep. We
655−are calculating `b` minus one by counting up from `0`.
657−(Obviously, this is an O(n) algorithm. Is there a better way?
658−There is not. Do we actually do this in practice? Yes and no.)
660−Unfortunately we are missing a third of our Rosetta stone. We
661−have decrement in Hoon and we have it in English. How do we
662−express this in Nock? What will the Hoon compiler generate from
663−the code above? Let's work through it line by line.
665−Nock has no types, variable names, etc. So line 1 is a no-op.
667−How do we add a variable (line 2)? We compute a new subject,
668−which is a cell of the present subject and the variable. With
669−this new subject, we execute another formula.
671−Since `0` is a constant, a formula that produces it is
675−To combine `0` with the subject, we compute
679−which, if our subject is 42, gives us
683−which we can use as the subject for an inner formula, `g`.
684−Composing our new variable with `g`, we have `f` as
686− [2 [[1 0] [0 1]] [1 g]]
688−which seems a little funky for something so simple. But we
689−can simplify it with the composition macro, `7`:
693−and still further with the augmentation macro, `8`:
697−If you refer back to the Nock definition, you'll see that all
698−these formulas are semantically equivalent.
700−Let's continue with our decrement. So what's `g`? We seem to
701−loop. Does Nock have a loop operator? It most certainly does
702−not. So what do we do?
704−We build a noun called a _core_ - a construct which is behind any
705−kind of interesting control flow in Hoon. Of course, the Nock
706−programmer is not constrained to use the same techniques as the
707−Hoon compiler, but it is probably a good idea.
709−In Hoon, all the flow structures from your old life as an Earth
710−programmer become cores. Functions and/or closures are cores,
711−objects are cores modules are cores, even loops are cores.
713−The core is just a cell whose tail is data (possibly containing
714−other cores) and whose head is code (containing one or more
715−formulas). The tail is the _payload_ and the head is the
716−_battery_. Hence your core is
720−To activate a core, pick a formula out of the battery, and use
721−the entire core (_not_ just the payload) as the subject.
723−(A core formula is called an _arm_. An arm is almost like an
724−object-oriented method, but not quite - a method would be an arm
725−that produces a function on an argument. The arm is just a
726−function of the core, ie, a computed attribute.)
728−Of course, because we feed it the entire core, our arm can
729−invoke itself (or any other formula in the battery). Hence, it
730−can loop. And this is what a loop is - the simplest of cores.
732−We need to do two things with this core: create it, and activate
733−it. To be precise, we need two formulas: a formula which
734−produces the core, and one which activates its subject. We can
735−compose these functions with the handy `7` operator:
739−`p` produces our core, `a` activates it. Let's take these in
740−reverse order. How do we activate a core?
742−Since we have only one formula, it's the battery itself.
743−Thus we want to execute Nock with the whole core (already the
744−subject, and the entire battery (slot `2`). Hence, `a` is
748−We could also use the handy `9` macro - which almost seems
749−designed for firing arms on cores:
753−Which leaves us seeking
755− [8 [1 0] [7 p [9 2 0 1]]]
757−And all we have to do is build the core, `p`. How do we build a
758−core? We add code to the subject, just as we added a variable
759−above. The initial value of our counter was a constant, `0`.
760−The initial (and permanent) value of our battery is a constant,
761−the loop formula `l`. So `p` is
765−Which would leave us seeking
767− [8 [1 0] [7 [8 [1 l] [0 1]] [9 2 0 1]]]
769−except that we have duplicated the `8` pattern again, since we
772− [7 [8 [1 l] [0 1]] [9 2 0 1]]
778−so the full value of `f` is
780− [8 [1 0] [8 [1 l] [9 2 0 1]]]
782−Thus our only formula to compose is the loop body, `l`.
783−Its subject is the loop core:
787−where `bat` is just the loop formula, and `pay` is the pair `[a
788−b]`, `a` being the input subject, and `b` the counter. Thus we
789−could also write this subject as
793−and we see readily that `a` is at slot `7`, `b` `6`, `l` `2`.
794−With this subject, we need to express the Hoon loop body
796− ?: =(a +(b)) :: line 4
800−This is obviously an if statement, and it calls for `6`. Ie:
804−Giving our decrement program as:
806− [8 [1 0] [8 [1 6 t y n] [9 2 0 1]]]
808−For `t`, how do we compute a flag that is yes (`0`) if `a` equals
809−`b` plus one? Equals, we recall, is `5`. So `t` can only be
813−If so, our product `y` is just the counter `b`:
817−And if not? We have to re-execute the loop with the counter
818−incremented. If we were executing it with the same counter,
819−obviously an infinite loop, we could use the same core:
823−But instead we need to construct a new core with the counter
830− [[0 2] [4 0 6] [0 7]]
834− [9 2 [[0 2] [4 0 6] [0 7]]]
836−Hence our complete decrement. Let's reformat vertically so we
837−can actually read it:
862− [9 2 [[0 2] [4 0 6] [0 7]]]
869−or, on one line without superfluous brackets:
871− [8 [1 0] 8 [1 6 [5 [0 7] 4 0 6] [0 6] 9 2 [0 2] [4 0 6] 0 7] 9 2 0 1]
873−which works for the important special case, 42:
875− ~tasfyn-partyv> .*(42 [8 [1 0] 8 [1 6 [5 [0 7] 4 0 6] [0 6] 9 2 [0 2] [4 0 6] 0 7] 9 2 0 1])
878−If you understood this, you understand Nock. At least in principle!
880−If you want to play around more with Nock, the command line will
881−start getting unwieldy. Fortunately, the standard install
882−contains the above Nock decrement packaged as an Arvo app, which
883−you can edit and change if you'd like to get ambitious. Just run
885− ~tasfyn-partyv> :toy/ndec 19
888−The file driving this is
890− hub/$seat/toy/app/ndec.holw
892−Edit this file, ignoring everything above the Nock formula, and
893−hit return in the console to see it update:
896− : ~tasfyn-partyv/toy/app/ndec/holw/
898−If decrement seems fun - why not write add? I wrote a Nock adder
899−a long, long time ago. But I've forgotten where I put it. There
900−is absolutely no use in this exercise, except to prove to
901−yourself that you've mastered Nock.
903−#Appendix A: Operator Reductions#
907− 28 :: *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
909− *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
911− 23 :: *[a 2 b c] *[*[a b] *[a c]]
913− *[*[a 0 1] *[a 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]]
915− 21 :: *[a 0 b] /[b a]
917− *[a *[a 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]]
919− 23 :: *[a 2 b c] *[*[a b] *[a c]]
921− *[a *[*[a [1 c d]] *[a [1 0] 2 [1 2 3] [1 0] 4 4 b]]]
925− 19 :: *[a [b c] d] [*[a b c] *[a d]]
927− *[a *[[c d] [*[a 1 0] *[a 2 [1 2 3] [1 0] 4 4 b]]]]
931− *[a *[[c d] [0 *[a 2 [1 2 3] [1 0] 4 4 b]]]]
933− 23 :: *[a 2 b c] *[*[a b] *[a c]]
935− *[a *[[c d] [0 *[*[a [1 2 3]] *[a [1 0] 4 4 b]]]]]
939− *[a *[[c d] [0 *[[2 3] *[a [1 0] 4 4 b]]]]]
941− 19 :: *[a [b c] d] [*[a b c] *[a d]]
943− *[a *[[c d] [0 *[[2 3] [*[a [1 0]] *[a 4 4 b]]]]]]
947− *[a *[[c d] [0 *[[2 3] [0 *[a 4 4 b]]]]]]
949− 25 :: *[a 4 b] +*[a b]
951− *[a *[[c d] [0 *[[2 3] [0 ++*[a b]]]]]]
955− 6r :: *[a 6 b c d] *[a *[[c d] [0 *[[2 3] [0 ++*[a b]]]]]]
959− 29 :: *[a 7 b c] *[a 2 b 1 c]
963− 23 :: *[a 2 b c] *[*[a b] *[a c]]
973− 7r :: *[a 7 b c] *[*[a b] c]
977− 30 :: *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
979− *[a 7 [[7 [0 1] b] 0 1] c]
981− 7r :: *[a 7 b c] *[*[a b] c]
983− *[*[a [7 [0 1] b] 0 1]] c]
985− 19 :: *[a [b c] d] [*[a b c] *[a d]]
987− *[[*[a [7 [0 1] b]] *[a 0 1]] c]
989− 21 :: *[a 0 b] /[b a]
991− *[[*[a [7 [0 1] b]] /[1 a]] c]
995− *[[*[a [7 [0 1] b]] a] c]
997− 7r :: *[a 7 b c] *[*[a b] c]
999− *[[*[*[a 0 1]] b] a] c]
1003− 8r :: *[a 8 b c] *[[*[a b] a] c]
1008− 31 :: *[a 9 b c] *[a 7 c [2 [0 1] [0 b]]]
1010− *[a 7 c [2 [0 1] [0 b]]]
1012− 7r :: *[a 7 b c] *[*[a b] c]
1014− *[*[a c] [2 [0 1] [0 b]]]
1016− 23 :: *[a 2 b c] *[*[a b] *[a c]]
1018− *[*[*[a c] [0 1]] *[*[a c] [0 b]]]
1020− 21 :: *[a 0 b] /[b a]
1024− 9r :: *[a 9 b c] *[*[a c] *[*[a c] 0 b]]
1029− *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
1031− 8r :: *[a 8 b c] [[*[a b] a] c]
1033− *[[*[a c] a] 7 [0 2] d]
1035− 7r :: *[a 7 b c] *[*[a b] c]
1037− *[*[[*[a c] a] 0 3] d]
1039− 21 :: *[a 0 b] /[b a]
1043− 10r :: *[a 10 [b c] d] *[a d]