@@ -1,1044 +0,0 @@
1−---
2−title: Nock tutorial
3−---
4−
5−# Nock tutorial
6−
7−So let's learn Nock! But wait - why learn Nock? After all,
8−we're going to be programming in Hoon, not Nock.
9−
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.
14−Except to learn Nock.
15−
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.
20−
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.
25−
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.
32−
33−#1.1 Definition#
34−
35−The best way to learn Nock is to read the spec and write your own
36−naive interpreter. Here is Nock 5K:
37−
38−**1. Structures**
39−
40− A noun is an atom or a cell. An atom is a natural number.
41− A cell is an ordered pair of nouns.
42−
43−**2. Pseudocode**
44−
45− 1 :: nock(a) *a
46− 2 :: [a b c] [a [b c]]
47− 3 ::
48− 4 :: ?[a b] 0
49− 5 :: ?a 1
50− 6 :: +[a b] +[a b]
51− 7 :: +a 1 + a
52− 8 :: =[a a] 0
53− 9 :: =[a b] 1
54− 10 :: =a =a
55− 11 ::
56− 12 :: /[1 a] a
57− 13 :: /[2 [a b]] a
58− 14 :: /[3 [a b]] b
59− 15 :: /[(a + a) b] /[2 /[a b]]
60− 16 :: /[(a + a + 1) b] /[3 /[a b]]
61− 17 :: /a /a
62− 18 ::
63− 19 :: *[a [[b c] d]] [*[a [b c]] *[a d]]
64− 20 ::
65− 21 :: *[a [0 b]] /[b a]
66− 22 :: *[a [1 b]] b
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]
71− 27 ::
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]
78− 34 ::
79− 35 :: *a *a
80−
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`.
86−
87−#1.2 Installation#
88−
89−The second best way to learn Nock is to boot up your own Arvo
90−virtual computer. See the Arvo tutorial for instructions.
91−
92−#1.3 Nock#
93−
94−To make Nock make sense, let's work through Nock 5K line by line.
95−First the data model:
96−
97−##1. Structures##
98−
99− A noun is an atom or a cell. An atom is any natural number.
100− A cell is any ordered pair of nouns.
101−
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
105−the S."
106−
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.
112−
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.
119−
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`.)
126−
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.
130−
131−Let's continue:
132−
133−##2. Pseudocode##
134−
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.)
142−
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!
146−
147−##Line 1:##
148−
149− 1 :: nock(a) *a
150−
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
153−prefix operator `*`.
154−
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.
160−
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`.)
168−
169−Normally `a` in `nock(a)` is a cell `[s f]`, or as we say
170−
171− [subject formula]
172−
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]`.
178−
179−##Line 2:##
180−
181− 2 :: [a b c] [a [b c]]
182−
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
188−
189− (a b c)
190−
191−becomes the Nock noun
192−
193− [a b c 0]
194−
195−which is equivalent to
196−
197− [a [b [c 0]]]
198−
199−Note that we can and do use unnecessary brackets anyway, for
200−emphasis.
201−
202−Let's move on to the axiomatic functions.
203−
204−##Lines 4-10:##
205−
206− 4 :: ?[a b] 0
207− 5 :: ?a 1
208− 6 :: +[a b] +[a b]
209− 7 :: +a 1 + a
210− 8 :: =[a a] 0
211− 9 :: =[a b] 1
212−
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.
218−
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.
223−
224−
225−##Lines 12-16:##
226−
227− 12 :: /[1 a] a
228− 13 :: /[2 a b] a
229− 14 :: /[3 a b] b
230− 15 :: /[(a + a) b] /[2 /[a b]]
231− 16 :: /[(a + a + 1) b] /[3 /[a b]]
232−
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.
236−
237−1 is the root. The head of every node `n` is `2n`; the tail is
238−`2n+1`. Thus a simple tree:
239−
240− 1
241− 2 3
242− 4 5 6 7
243− 14 15
244−
245−If the value of every leaf is its tree address, this tree is
246−
247− [[4 5] [6 14 15]]
248−
249−and, for some examples of addressing:
250−
251− /[1 [[4 5] [6 14 15]]]
252−
253−is `[[4 5] [6 14 15]]`
254−
255− /[2 [[4 5] [6 14 15]]]
256−
257−is `[4 5]`
258−
259− /[3 [[4 5] [6 14 15]]]
260−
261−is `[6 14 15]`, and
262−
263− /[7 [[4 5] [6 14 15]]]
264−
265−is `[14 15]`
266−
267−I do hope this isn't so terribly hard to follow.
268−
269−##Line 21:##
270−
271−Now we enter the definition of Nock itself - ie, the `*`
272−operator.
273−
274− 21 :: *[a 0 b] /[b a]
275−
276−`0` is simply Nock's tree-addressing operator. Let's try it out
277−from the Arvo command line.
278−
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
283−
284− .*(a [0 b])
285−
286−So, to reuse our slot example, let's try the interpreter:
287−
288− ~tasfyn-partyv> .*([[4 5] [6 14 15]] [0 7])
289−
290−gives, while the sky remains blue and the sun rises in the east:
291−
292− [14 15]
293−
294−Even stupider is line 21:
295−
296−##Line 21:##
297−
298− 21 :: *[a 1 b] b
299−
300−`1` is the constant operator. It produces its argument without
301−reference to the subject. So
302−
303− ~tasfyn-partyv> .*(42 [1 153 218])
304−
305−yields
306−
307− [153 218]
308−
309−
310−##Line 23:##
311−
312− 23 :: *[a 2 b c] *[*[a b] *[a c]]
313−
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`.
319−
320−Let's convert the previous example into a stupid use of `2`:
321−
322− ~tasfyn-partyv> .*(77 [2 [1 42] [1 1 153 218]])
323−
324−with a constant subject and a constant formula, gives the same
325−
326− [153 218]
327−
328−Like so:
329−
330− *[77 [2 [1 42] [1 1 153 218]]]
331−
332− 22 :: *[a 2 b c] *[*[a b] *[a c]]
333−
334− *[*[77 [1 42]] *[77 [1 1 153 218]]]
335−
336− 21 :: *[a 1 b] b
337−
338− *[42 *[77 [1 1 153 218]]]
339−
340− *[42 1 153 218]
341−
342− [153 218]
343−
344−##Lines 24-26:##
345−
346− 24 :: *[a 3 b] ?*[a b]
347− 25 :: *[a 4 b] +*[a b]
348− 26 :: *[a 5 b] =*[a b]
349−
350−In lines 23-25, we meet our axiomatic functions again:
351−
352−For instance, if `x` is a formula that calculates some product,
353−`[4 x]` calculates that product plus one. Hence:
354−
355− ~tasfyn-partyv> .*(57 [0 1])
356− 57
357−
358−and
359−
360− ~tasfyn-partyv> .*([132 19] [0 3])
361− 19
362−
363−and
364−
365− ~tasfyn-partyv> .*(57 [4 0 1])
366− 58
367−
368−and
369−
370− ~tasfyn-partyv> .*([132 19] [4 0 3])
371− 20
372−
373−If this seems obvious, you're doin' good. Finally, we jump back up
374−to line 18, the trickiest in the spec:
375−
376−##Line 19:##
377−
378− 19 :: *[a [b c] d] [*[a b c] *[a d]]
379−
380−Um, what?
381−
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.
387−
388−We could write line 19 less formally:
389−
390− *[subject [formula-x formula-y]]
391− => [*[subject formula-x] *[subject formula-y]]
392−
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.
397−
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]`.
401−
402−For example,
403−
404− ~tasfyn-partyv> .*(42 [4 0 1])
405−
406−where `42` is the subject (data) and `[4 0 1]` is the formula
407−(code), happens to evaluate to `43`. Whereas
408−
409− ~tasfyn-partyv> .*(42 [3 0 1])
410−
411−is `1`. So if we evaluate
412−
413− ~tasfyn-partyv> .*(42 [[4 0 1] [3 0 1]])
414−
415−we get
416−
417− [43 1]
418−
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.
424−
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
427−together:
428−
429−##Lines 28-33:##
430−
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]
437−
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:
441−
442− 33 :: *[a 10 b c] *[a c]
443−
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:
446−
447− ~tasfyn-partyv> .*([132 19] [10 37 [4 0 3]])
448− 20
449−
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.
454−
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.
461−
462−There is another reduction for hints - line 32:
463−
464− 32 :: *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
465−
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.
472−
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.)
476−
477−We move on to the next simplest operator, `7`. Line 29:
478−
479− 29 :: *[a 7 b c] *[a 2 b 1 c]
480−
481−Suppose we have two formulas, `b` and `c`. What is the formula
482−`[7 b c]`? This example will show you:
483−
484− ~tasfyn-partyv> .*(42 [7 [4 0 1] [4 0 1]])
485− 44
486−
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.
490−
491−Line 30 looks very similar:
492−
493− 30 :: *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
494−
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:
498−
499− ~tasfyn-partyv> .*(42 [8 [4 0 1] [0 1]])
500− [43 42]
501−
502−and
503−
504− ~tasfyn-partyv> .*(42 [8 [4 0 1] [4 0 3]])
505− 43
506−
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.
512−
513−For extra credit, explain why we can't just define
514−
515− *[a 8 b c] *[a 7 [b 0 1] c]
516−
517−Another simple macro is line 31:
518−
519− 31 :: *[a 9 b c] *[a 7 c 2 [0 1] 0 b]
520−
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`.
524−
525−And finally, we come to the piece de resistance - line 28:
526−
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]
528−
529−Great giblets! WTF is this doing? It seems we've finally
530−arrived at some real rocket science.
531−
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.
535−
536−For instance:
537−
538− ~tasfyn-partyv> .*(42 [6 [1 0] [4 0 1] [1 233]])
539− 43
540−
541−and
542−
543− ~tasfyn-partyv> .*(42 [6 [1 1] [4 0 1] [1 233]])
544− 233
545−
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.
551−
552−It's a good exercise, however, to peek inside the funk.
553−
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.
558−
559−This simpler "if" would be:
560−
561− *[a 6 b c d] *[a [2 [0 1] [2 [1 c d] [[1 0] [4 4 b]]]]]
562−
563−Or without so many unnecessary brackets:
564−
565− *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] [4 4 b]]
566−
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.
570−
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
574−the formula.
575−
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]]`.
578−
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`.
584−
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."
590−
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.
597−
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.)
603−
604−#1.4 Decrement in Nock#
605−
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
609−terminate.
610−
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.
616−
617−As we know, the equivalent formula for increment is
618−
619− [4 0 1]
620−
621−Thus:
622−
623− ~>tasfyn-partyv .*(42 [4 0 1])
624− 43
625−
626−Of course, increment is built into Nock. So, ha, that's easy.
627−
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:
631−
632− => a=. :: line 1
633− =+ b=0 :: line 2
634− |- :: line 3
635− ?: =(a +(b)) :: line 4
636− b :: line 5
637− $(b +(b)) :: line 6
638−
639−Or for fun, on one line:
640−
641− =>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b))))))
642−
643−Does Hoon actually work?
644−
645− ~tasfyn-partyv> =>(42 =>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b)))))))
646− 41
647−
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`.
656−
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.)
659−
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.
664−
665−Nock has no types, variable names, etc. So line 1 is a no-op.
666−
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.
670−
671−Since `0` is a constant, a formula that produces it is
672−
673− [1 0]
674−
675−To combine `0` with the subject, we compute
676−
677− [[1 0] [0 1]]
678−
679−which, if our subject is 42, gives us
680−
681− [0 42]
682−
683−which we can use as the subject for an inner formula, `g`.
684−Composing our new variable with `g`, we have `f` as
685−
686− [2 [[1 0] [0 1]] [1 g]]
687−
688−which seems a little funky for something so simple. But we
689−can simplify it with the composition macro, `7`:
690−
691− [7 [[1 0] [0 1]] g]
692−
693−and still further with the augmentation macro, `8`:
694−
695− [8 [1 0] g]
696−
697−If you refer back to the Nock definition, you'll see that all
698−these formulas are semantically equivalent.
699−
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?
703−
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.
708−
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.
712−
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
717−
718− [bat pay]
719−
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.
722−
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.)
727−
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.
731−
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:
736−
737− [8 [1 0] [7 p a]]
738−
739−`p` produces our core, `a` activates it. Let's take these in
740−reverse order. How do we activate a core?
741−
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
745−
746− [2 [0 1] [0 2]]
747−
748−We could also use the handy `9` macro - which almost seems
749−designed for firing arms on cores:
750−
751− [9 2 [0 1]]
752−
753−Which leaves us seeking
754−
755− [8 [1 0] [7 p [9 2 0 1]]]
756−
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
762−
763− [8 [1 l] [0 1]]
764−
765−Which would leave us seeking
766−
767− [8 [1 0] [7 [8 [1 l] [0 1]] [9 2 0 1]]]
768−
769−except that we have duplicated the `8` pattern again, since we
770−know
771−
772− [7 [8 [1 l] [0 1]] [9 2 0 1]]
773−
774−is equivalent to
775−
776− [8 [1 l] [9 2 0 1]]
777−
778−so the full value of `f` is
779−
780− [8 [1 0] [8 [1 l] [9 2 0 1]]]
781−
782−Thus our only formula to compose is the loop body, `l`.
783−Its subject is the loop core:
784−
785− [bat pay]
786−
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
790−
791− [l b a]
792−
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
795−
796− ?: =(a +(b)) :: line 4
797− b :: line 5
798− $(b +(b)) :: line 6
799−
800−This is obviously an if statement, and it calls for `6`. Ie:
801−
802− [6 t y n]
803−
804−Giving our decrement program as:
805−
806− [8 [1 0] [8 [1 6 t y n] [9 2 0 1]]]
807−
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
810−
811− [5 [0 7] [4 0 6]]
812−
813−If so, our product `y` is just the counter `b`:
814−
815− [0 6]
816−
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:
820−
821− [9 2 0 1]
822−
823−But instead we need to construct a new core with the counter
824−incremented:
825−
826− [l +(b) a]
827−
828−ie,
829−
830− [[0 2] [4 0 6] [0 7]]
831−
832−and `n` is:
833−
834− [9 2 [[0 2] [4 0 6] [0 7]]]
835−
836−Hence our complete decrement. Let's reformat vertically so we
837−can actually read it:
838−
839− [8
840− [1 0]
841− [ 8
842− [ 1
843− [ 6
844− t
845− y
846− n
847− ]
848− ]
849− [9 2 0 1]
850− ]
851− ]
852−
853−which becomes
854−
855− [8
856− [1 0]
857− [ 8
858− [ 1
859− [ 6
860− [5 [0 7] [4 0 6]]
861− [0 6]
862− [9 2 [[0 2] [4 0 6] [0 7]]]
863− ]
864− ]
865− [9 2 0 1]
866− ]
867− ]
868−
869−or, on one line without superfluous brackets:
870−
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]
872−
873−which works for the important special case, 42:
874−
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])
876− 41
877−
878−If you understood this, you understand Nock. At least in principle!
879−
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
884−
885− ~tasfyn-partyv> :toy/ndec 19
886− 18
887−
888−The file driving this is
889−
890− hub/$seat/toy/app/ndec.holw
891−
892−Edit this file, ignoring everything above the Nock formula, and
893−hit return in the console to see it update:
894−
895− ~tasfyn-partyv>
896− : ~tasfyn-partyv/toy/app/ndec/holw/
897−
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.
902−
903−#Appendix A: Operator Reductions#
904−
905−##`6` Reduction:##
906−
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]
908−
909− *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
910−
911− 23 :: *[a 2 b c] *[*[a b] *[a c]]
912−
913− *[*[a 0 1] *[a 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]]
914−
915− 21 :: *[a 0 b] /[b a]
916−
917− *[a *[a 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]]
918−
919− 23 :: *[a 2 b c] *[*[a b] *[a c]]
920−
921− *[a *[*[a [1 c d]] *[a [1 0] 2 [1 2 3] [1 0] 4 4 b]]]
922−
923− 22 :: *[a 1 b] b
924−
925− 19 :: *[a [b c] d] [*[a b c] *[a d]]
926−
927− *[a *[[c d] [*[a 1 0] *[a 2 [1 2 3] [1 0] 4 4 b]]]]
928−
929− 22 :: *[a 1 b] b
930−
931− *[a *[[c d] [0 *[a 2 [1 2 3] [1 0] 4 4 b]]]]
932−
933− 23 :: *[a 2 b c] *[*[a b] *[a c]]
934−
935− *[a *[[c d] [0 *[*[a [1 2 3]] *[a [1 0] 4 4 b]]]]]
936−
937− 22 :: *[a 1 b] b
938−
939− *[a *[[c d] [0 *[[2 3] *[a [1 0] 4 4 b]]]]]
940−
941− 19 :: *[a [b c] d] [*[a b c] *[a d]]
942−
943− *[a *[[c d] [0 *[[2 3] [*[a [1 0]] *[a 4 4 b]]]]]]
944−
945− 22 :: *[a 1 b] b
946−
947− *[a *[[c d] [0 *[[2 3] [0 *[a 4 4 b]]]]]]
948−
949− 25 :: *[a 4 b] +*[a b]
950−
951− *[a *[[c d] [0 *[[2 3] [0 ++*[a b]]]]]]
952−
953−**`6` Reduced:**
954−
955− 6r :: *[a 6 b c d] *[a *[[c d] [0 *[[2 3] [0 ++*[a b]]]]]]
956−
957−##`7` Reduction:##
958−
959− 29 :: *[a 7 b c] *[a 2 b 1 c]
960−
961− *[a 2 b 1 c]
962−
963− 23 :: *[a 2 b c] *[*[a b] *[a c]]
964−
965− *[*[a b] *[a 1 c]]
966−
967− 22: *[a 1 b] b
968−
969− *[*[a b] c]
970−
971−**`7` Reduced:**
972−
973− 7r :: *[a 7 b c] *[*[a b] c]
974−
975−##`8` Reduction:##
976−
977− 30 :: *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
978−
979− *[a 7 [[7 [0 1] b] 0 1] c]
980−
981− 7r :: *[a 7 b c] *[*[a b] c]
982−
983− *[*[a [7 [0 1] b] 0 1]] c]
984−
985− 19 :: *[a [b c] d] [*[a b c] *[a d]]
986−
987− *[[*[a [7 [0 1] b]] *[a 0 1]] c]
988−
989− 21 :: *[a 0 b] /[b a]
990−
991− *[[*[a [7 [0 1] b]] /[1 a]] c]
992−
993− 12 :: /[1 a] a
994−
995− *[[*[a [7 [0 1] b]] a] c]
996−
997− 7r :: *[a 7 b c] *[*[a b] c]
998−
999− *[[*[*[a 0 1]] b] a] c]
1000−
1001−**`8` Reduced:**
1002−
1003− 8r :: *[a 8 b c] *[[*[a b] a] c]
1004−
1005−
1006−##`9` Reduction:##
1007−
1008− 31 :: *[a 9 b c] *[a 7 c [2 [0 1] [0 b]]]
1009−
1010− *[a 7 c [2 [0 1] [0 b]]]
1011−
1012− 7r :: *[a 7 b c] *[*[a b] c]
1013−
1014− *[*[a c] [2 [0 1] [0 b]]]
1015−
1016− 23 :: *[a 2 b c] *[*[a b] *[a c]]
1017−
1018− *[*[*[a c] [0 1]] *[*[a c] [0 b]]]
1019−
1020− 21 :: *[a 0 b] /[b a]
1021−
1022−**`9` Reduced:**
1023−
1024− 9r :: *[a 9 b c] *[*[a c] *[*[a c] 0 b]]
1025−
1026−
1027−##`10` Reduction:##
1028−
1029− *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
1030−
1031− 8r :: *[a 8 b c] [[*[a b] a] c]
1032−
1033− *[[*[a c] a] 7 [0 2] d]
1034−
1035− 7r :: *[a 7 b c] *[*[a b] c]
1036−
1037− *[*[[*[a c] a] 0 3] d]
1038−
1039− 21 :: *[a 0 b] /[b a]
1040−
1041−**`10` reduced:**
1042−
1043− 10r :: *[a 10 [b c] d] *[a d]
1044−