on 50abe45 help sign in
urbit.org/docs/nock/example.md
281 lines 8.1 kB history raw
1---
2navhome: '/docs/'
3next: True
4sort: 3
5title: Example
6---
8# Nock by hand
10Some people write assembly language by hand. Some of them have a good reason to
11do so. But there's no good reason to write Nock by hand, ever.
13Except for the purposes of learning Nock. It's not necessary to know Nock to
14program in Hoon. But it's quite desirable. The feeling of understanding the
15system at two levels is pleasant.
17## Decrement in Nock
19A good practice exercise for Nock is a decrement formula. Ie, a formula `f`
20which implements the partial function that produces `(s - 1)` if `s` is a
21nonzero atom, and otherwise does not terminate.
23As we know, the equivalent formula for increment is
25 [4 0 1]
27Thus:
29 ~>zod:dojo .*(42 [4 0 1])
30 43
32Of course, increment is built into Nock. So, ha, that's easy.
34The best way to learn Nock is to *stop reading right now*, and go write your own
35decrement formula. There's no substitute for doing it yourself. But the second
36best way is faster...
38## Decrement in Hoon
40How do we decrement? A good way to start is to gaze fondly on how we'd do it if
41we actually had a real language, ie, Hoon. Here is a minimal decrement in Hoon:
43 => a=. :: line 1
44 =+ b=0 :: line 2
45 |- :: line 3
46 ?: =(a +(b)) :: line 4
47 b :: line 5
48 $(b +(b)) :: line 6
50Or for fun, on one line:
52 =>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b))))))
54Does Hoon actually work?
56 ~zod:dojo> =>(42 =>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b)))))))
57 41
59Let's translate this into English. How do we decrement the subject? First (line
601), we rename the subject `a`. Second (line 2), we add a variable, `b`, an atom
61with value `0`. Third (line 3), we loop. Fourth, we test if `a` equals `b` plus
621 (line 4), produce `b` if it does (line 5), repeat the loop with `b` set to `b`
63plus 1 (line 6) if it doesn't. Obviously, while the syntax is unusual, the
64algorithm is anything but deep. We are calculating `b` minus one by counting up
65from `0`.
67(Obviously, this is an O(n) algorithm. Is there a better way? There is not. Do
68we actually do this in practice? Yes and no.)
70Unfortunately we are missing a third of our Rosetta stone. We have decrement in
71Hoon and we have it in English. How do we express this in Nock? What will the
72Hoon compiler generate from the code above? Let's work through it line by line.
74Nock has no types, variable names, etc. So line 1 is a no-op.
76How do we add a variable (line 2)? We compute a new subject, which is a cell of
77the present subject and the variable. With this new subject, we execute another
78formula.
80Since `0` is a constant, a formula that produces it is
82 [1 0]
84To combine `0` with the subject, we compute
86 [[1 0] [0 1]]
88which, if our subject is 42, gives us
90 [0 42]
92which we can use as the subject for an inner formula, `g`. Composing our new
93variable with `g`, we have `f` as
95 [2 [[1 0] [0 1]] [1 g]]
97which seems a little funky for something so simple. But we can simplify it with
98the composition macro, `7`:
100 [7 [[1 0] [0 1]] g]
102and still further with the augmentation macro, `8`:
104 [8 [1 0] g]
106If you refer back to the Nock definition, you'll see that all these formulas are
107semantically equivalent.
109Let's continue with our decrement. So what's `g`? We seem to loop. Does Nock
110have a loop instruction? It most certainly does not. So what do we do?
112We build a noun called a *core* - a construct which is behind any kind of
113interesting control flow in Hoon. Of course, the Nock programmer is not
114constrained to use the same techniques as the Hoon compiler, but it is probably
115a good idea.
117In Hoon, all the flow structures from your old life as an Earth programmer
118become cores. Functions and/or closures are cores, objects are cores modules are
119cores, even loops are cores.
121The core is just a cell whose tail is data (possibly containing other cores) and
122whose head is code (containing one or more formulas). The tail is the *payload*
123and the head is the *battery*. Hence your core is
125 [bat pay]
127To activate a core, pick a formula out of the battery, and use the entire core
128(*not* just the payload) as the subject.
130(A core formula is called an *arm*. An arm is almost like an object-oriented
131method, but not quite - a method would be an arm that produces a function on an
132argument. The arm is just a function of the core, ie, a computed attribute.)
134Of course, because we feed it the entire core, our arm can invoke itself (or any
135other formula in the battery). Hence, it can loop. And this is what a loop is -
136the simplest of cores.
138We need to do two things with this core: create it, and activate it. To be
139precise, we need two formulas: a formula which produces the core, and one which
140activates its subject. We can compose these functions with the handy `7`
141instruction:
143 [8 [1 0] [7 p a]]
145`p` produces our core, `a` activates it. Let's take these in reverse order. How
146do we activate a core?
148Since we have only one formula, it's the battery itself. Thus we want to execute
149Nock with the whole core (already the subject, and the entire battery (slot
150`2`). Hence, `a` is
152 [2 [0 1] [0 2]]
154We could also use the handy `9` macro - which almost seems designed for firing
155arms on cores:
157 [9 2 [0 1]]
159Which leaves us seeking
161 [8 [1 0] [7 p [9 2 0 1]]]
163And all we have to do is build the core, `p`. How do we build a core? We add
164code to the subject, just as we added a variable above. The initial value of our
165counter was a constant, `0`. The initial (and permanent) value of our battery is
166a constant, the loop formula `l`. So `p` is
168 [8 [1 l] [0 1]]
170Which would leave us seeking
172 [8 [1 0] [7 [8 [1 l] [0 1]] [9 2 0 1]]]
174except that we have duplicated the `8` pattern again, since we know
176 [7 [8 [1 l] [0 1]] [9 2 0 1]]
178is equivalent to
180 [8 [1 l] [9 2 0 1]]
182so the full value of `f` is
184 [8 [1 0] [8 [1 l] [9 2 0 1]]]
186Thus our only formula to compose is the loop body, `l`. Its subject is the loop
187core:
189 [bat pay]
191where `bat` is just the loop formula, and `pay` is the pair `[b a]`, `b` being
192the counter, and `a` the input subject. Thus we could also write this subject as
194 [l b a]
196and we see readily that `a` is at slot `7`, `b` `6`, `l` `2`. With this subject,
197we need to express the Hoon loop body
199 ?: =(a +(b)) :: line 4
200 b :: line 5
201 $(b +(b)) :: line 6
203This is obviously an if statement, and it calls for `6`. Ie:
205 [6 t y n]
207Giving our decrement program as:
209 [8 [1 0] [8 [1 6 t y n] [9 2 0 1]]]
211For `t`, how do we compute a flag that is yes (`0`) if `a` equals `b` plus one?
212Equals, we recall, is `5`. So `t` can only be
214 [5 [0 7] [4 0 6]]
216If so, our product `y` is just the counter `b`:
218 [0 6]
220And if not? We have to re-execute the loop with the counter incremented. If we
221were executing it with the same counter, obviously an infinite loop, we could
222use the same core:
224 [9 2 0 1]
226But instead we need to construct a new core with the counter incremented:
228 [l +(b) a]
230ie,
232 [[0 2] [4 0 6] [0 7]]
234and `n` is:
236 [9 2 [[0 2] [4 0 6] [0 7]]]
238Hence our complete decrement. Let's reformat vertically so we can actually read
239it:
241 [8
242 [1 0]
243 [ 8
244 [ 1
245 [ 6
246 t
247 y
248 n
249 ]
250 ]
251 [9 2 0 1]
252 ]
253 ]
255which becomes
257 [8
258 [1 0]
259 [ 8
260 [ 1
261 [ 6
262 [5 [0 7] [4 0 6]]
263 [0 6]
264 [9 2 [[0 2] [4 0 6] [0 7]]]
265 ]
266 ]
267 [9 2 0 1]
268 ]
269 ]
271or, on one line without superfluous brackets:
273 [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]
275which works for the important special case, 42:
277 ~zod:dojo> .*(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])
278 41
280If you understood this, you understand Nock. At least in principle! Now, go
281write an adder. Or don't.