@@ -1,11 +1,11 @@
11:- :~ navhome/'/docs/'
22 navuptwo/'true'
33 sort/'6'
4− title/'1.6 Producing Gates with Core Arms'
4+ title/'1.6 Multi-gate Cores and Doors'
55 ==
66;>
77
8−# 1.6 Producing Gates with Core Arms
8+# 1.6 Multi-gate Cores and Doors
99
1010It's useful to have cores whose arms evaluate to make gates. The use of such cores is common in Hoon; that's how the functions of the Hoon standard library are stored in the subject. Learning about such cores will also deepen the reader's understanding of Hoon semantics, and for that reason alone is worthwhile.
1111
@@ -21,26 +21,6 @@ There are two ways of making a function call in Hoon. First, you can call a gat
2121> (inc 10)
222211
2323
24−> .
25−[ [ inc
26− < 1.viz
27− { a/@
28− {our/@p now/@da eny/@uvJ}
29− $~
30− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
31− }
32− >
33− our=~bitnus-holleb-rocmeg-fotfer--migsum-nidsym-hilput-patmus
34− now=~2018.8.24..05.51.53..97b9
35− eny
36− \/0v3vd.sdp3o.ds8eq.5mr1s.du2v6.4a12r.poie2.04123.jaq8r.8qoum.mvb63.dblf7.7\/
37− lt1d.fqebb.pv6dr.lio19.re7lq.016q3.u03ne.c8aiu.7148p
38− \/ \/
39− ]
40− ~
41− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
42−]
43−
4424> =inc
4525```
4626
@@ -54,6 +34,8 @@ The second way of making a function call involves an expression that _produces_
5434246
5535```
5636
37+The difference is that `inc` is an already-created gate in the subject when we called it. The latter calls involve producing a gate that doesn't exist anywhere in the subject, and then calling it.
38+
5739Are calls to `add` and `mul` of the Hoon standard library of the first kind, or the second?
5840
5941```
@@ -64,9 +46,11 @@ Are calls to `add` and `mul` of the Hoon standard library of the first kind, or
6446276
6547```
6648
67−They're of the second kind. Neither `add` nor `mul` is a gate; they're each arms that _produce_ gates. Often the difference doesn't matter much. Either way you can do a function call using the `(gate arg)` syntax.
49+They're of the second kind. Neither `add` nor `mul` resolves to a gate directly; they're each arms that _produce_ gates.
50+
51+Often the difference doesn't matter much. Either way you can do a function call using the `(gate arg)` syntax.
6852
69−It's important to learn the difference, however, because for certain use cases you'll want the extra flexibility that comes with having an already produced core in the subject. This is particularly important when you want to mutate the core as you call it. It's far more straight-forward to mutate a core that already exists in the subject, than to have to produce it first and then mutate it.
53+It's important to learn the difference, however, because for certain use cases you'll want the extra flexibility that comes with having an already produced core in the subject.
7054
7155## A Gate-Building Core
7256
@@ -101,9 +85,9 @@ Notice that each arm in core `c` is able to call the other arms of `c` -- `add-t
10185
10286### The `..arm` Syntax
10387
104−Let's say you have a wing that resolves to a leg of the subject. In this wing is an arm name, e.g., `a.b.arm`. As you know from lesson 1.4, this should be read as '`a` in `b` in the parent core of `arm`'. Speaking more directly: the wing resolution path goes through the parent core of the arm, not the arm itself.
88+Let's say you have a wing that resolves to a leg of the subject. In this wing is an arm name, e.g., `a.b.arm`. As you learned in lesson 1.4, this should be read as '`a` in `b` in the parent core of `arm`'. Speaking more directly: the wing resolution path goes through the parent core of the arm, not the arm itself.
10589
106−Recall that `.` by itself returns the whole subject. With that in mind, take a moment to answer the following question on your own. In general, what should the expression `..arm` produce?
90+Recall that using `.` by itself returns the whole subject. With that in mind, take a moment to answer the following question on your own. In general, what should the expression `..arm` produce?
10791
10892Do you have an answer? If not, consider going back and trying to figure it out before moving on.
10993
@@ -111,18 +95,16 @@ You may read `..arm` as '`.` of the parent core of `arm`'. This amounts to just
11195
11296```
11397> ..inc:c
114−< 4.vgw
98+< 4.han
11599 { {our/@p now/@da eny/@uvJ}
116− $~
117− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
100+ <19.anu 24.tmo 6.ipz 38.ard 119.spd 241.plj 51.zox 93.pqh 74.dbd 1.qct $141>
118101 }
119102>
120103
121104> c
122−< 4.vgw
105+< 4.han
123106 { {our/@p now/@da eny/@uvJ}
124− $~
125− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
107+ <19.anu 24.tmo 6.ipz 38.ard 119.spd 241.plj 51.zox 93.pqh 74.dbd 1.qct $141>
126108 }
127109>
128110```
@@ -138,7 +120,7 @@ For an example of this, consider the `add` arm of the Hoon standard library. Th
138120<74.dbd 1.qct $141>
139121```
140122
141−Here you see a core with a battery of 31 arms, and whose payload is another core with one arm.
123+Here you see a core with a battery of 74 arms (!), and whose payload is another core with one arm.
142124
143125### Mutating a Gate
144126
@@ -151,7 +133,15 @@ Let's say you want to modify the default sample of the gate for `double`. We ca
151133
152134Given that `a x 2 = 0`, `a` must be `0`. (Remember that `a` is the face for the `double` sample, as defined in the core we bound to `c` above.)
153135
154−Let's say we want to modify the default sample to be `25`. There is only one problem: `double` isn't a gate! It's an arm that produces a gate, and every time it does so the default sample is `0`. If you want to mutate the gate produced by `double`, you'll first have to put a copy of that gate into the subject:
136+Let's say we want to mutate the `double` gate so that the default sample is `25`. There is only one problem: `double` isn't a gate!
137+
138+```
139+> double.c(a 25)
140+-tack.a
141+-find.a
142+```
143+
144+It's an arm that produces a gate, and `a` cannot be found in `double` until the gate is created. Furthermore, every time the gate is created, it has the default sample, `0`. If you want to mutate the gate produced by `double`, you'll first have to put a copy of that gate into the subject:
155145
156146```
157147> =double-copy double:c
@@ -174,10 +164,11 @@ Good. Let's call it with no argument and see if it returns double the value of
17416450
175165```
176166
177−It does indeed. Unbind `c` and `double-copy` as follows:
167+It does indeed. Unbind `c` and `double-copy`:
178168
179169```
180170> =c
171+
181172> =double-copy
182173```
183174
@@ -196,7 +187,7 @@ If you try to mutate the default sample of `add`, it won't work:
196187-find.a
197188```
198189
199−The reason it doesn't work is that `add` is an arm, not a gate. Hoon can't find an `a` to modify in a gate that doesn't exist yet!
190+As before with `double`, Hoon can't find an `a` to modify in a gate that doesn't exist yet.
200191
201192## Other Functions in the Hoon Standard Library
202193
@@ -207,7 +198,7 @@ Let's look once more at the parent core of the `add` arm in the Hoon standard li
207198<74.dbd 1.qct $141>
208199```
209200
210−The battery of this core contains 31 arms, each of which evaluates to a gate in the standard library. This 'library' is nothing more than a core containing useful basic functions that Hoon often makes available as part of the subject. You can see the Hoon code defining these arms near the beginning of [hoon.hoon](https://github.com/urbit/arvo/blob/master/sys/hoon.hoon), starting with [`++ add`](https://github.com/urbit/arvo/blob/master/sys/hoon.hoon#L21). (Yes, the Hoon standard library is written in Hoon.)
201+The battery of this core contains 74 arms, each of which evaluates to a gate in the standard library. This 'library' is nothing more than a core containing useful basic functions that Hoon often makes available as part of the subject. You can see the Hoon code defining these arms near the beginning of [hoon.hoon](https://github.com/urbit/arvo/blob/master/sys/hoon.hoon), starting with [`++ add`](https://github.com/urbit/arvo/blob/master/sys/hoon.hoon#L21). (Yes, the Hoon standard library is written in Hoon.)
211202
212203Here are some of the other gates that can be generated from this core in the Hoon standard library. It should be fairly obvious what they do:
213204
@@ -265,11 +256,11 @@ A brief review: A core is a cell of battery and payload: `[battery payload]`. T
265256New material: A *door* is a core with a sample. That is, a door is a core whose payload is a cell of sample and context: `[sample context]`.
266257
267258```
268− door
259+ Door
269260 / \
270−battery .
261+Battery .
271262 / \
272− sample context
263+ Sample Context
273264```
274265
275266It follows from this definition that a gate is a special case of a door. A gate is a door with exactly one arm, named `$`.
@@ -296,7 +287,7 @@ Before getting into what these arms do, let's cover how the `|_` rune works in g
296287
297288The `|_` rune for making a door works exactly like the `|%` rune for making a core, except it takes one additional subexpression.
298289
299−The first subexpression after the `|_` rune defines the door sample. (This is the subexpression the `|%` rune lacks.) Following that are a series of `++` runes, each of which defines an arm of the door. Finally, the expression is terminated with a `--` rune.
290+The first subexpression after the `|_` rune defines the door's sample. (This is the subexpression the `|%` rune lacks.) Following that are a series of `++` runes, each of which defines an arm of the door. Finally, the expression is terminated with a `--` rune.
300291
301292#### Back to the Example
302293
@@ -338,9 +329,11 @@ Having confirmed that `b` is `0`, let's mutate the `c` sample and then call its
338329%.n
339330```
340331
341−Doing the same mutation repeatedly can be tedious, so let's bind `c` to the modified version of the door:
332+Doing the same mutation repeatedly can be tedious, so let's bind `c` to the modified version of the door, where `b` is `7`:
342333
343334```
335+> =c c(b 7)
336+
344337> (plus:c 10)
34533817
346339
@@ -371,34 +364,32 @@ Readers with some mathematical background may notice that `~( )` expressions all
371364
372365```
373366> ~(plus c 7)
374−< 1.umt
367+< 1.gxk
375368 { a/@
376− < 3.uvn
369+ < 3.iba
377370 { b/@
378371 {our/@p now/@da eny/@uvJ}
379− $~
380− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
372+ < 19.anu
373+ 24.tmo
374+ 6.ipz
375+ 38.ard
376+ 119.spd
377+ 241.plj
378+ 51.zox
379+ 93.pqh
380+ 74.dbd
381+ 1.qct
382+ $141
383+ >
381384 }
382385 >
383386 }
384387>
385388
389+
386390> b:~(plus c 7)
3873917
388392
389−> ~(plus c 17)
390−< 1.umt
391− { a/@
392− < 3.uvn
393− { b/@
394− {our/@p now/@da eny/@uvJ}
395− $~
396− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
397− }
398− >
399− }
400−>
401−
402393> b:~(plus c 17)
40339417
404395```
@@ -429,15 +420,15 @@ It turns out that the `rs` in `add:rs` is a Hoon standard library arm that produ
429420
430421```
431422> rs
432−<21?vij {r/?($n $u $d $z) <54.tyv 119.wim 31.ohr 1.jmk $143>}>
423+<21|fan {r/?($n $u $d $z) <51.zox 93.pqh 74.dbd 1.qct $141>}>
433424```
434425
435−The battery of this core, pretty-printed as `21?vij`, has 21 arms that define functions specifically for `@rs` atoms. One of these arms is named `add`; it's a different `add` from the standard one we've been using for vanilla atoms. So when you invoke `add:rs` instead of just `add` in a function call, (1) the `rs` door is produced, and then (2) the name search for `add` resolves to the special `add` arm in `rs`. This produces the gate for adding `@rs` atoms:
426+The battery of this core, pretty-printed as `21|fan`, has 21 arms that define functions specifically for `@rs` atoms. One of these arms is named `add`; it's a different `add` from the standard one we've been using for vanilla atoms. So when you invoke `add:rs` instead of just `add` in a function call, (1) the `rs` door is produced, and then (2) the name search for `add` resolves to the special `add` arm in `rs`. This produces the gate for adding `@rs` atoms:
436427
437428```
438429> add:rs
439−< 1.dfv
440− {{a/@rs b/@rs} <21.vij {r/?($n $u $d $z) <54.tyv 119.wim 31.ohr 1.jmk $143>}>}
430+< 1.hsu
431+ {{a/@rs b/@rs} <21.fan {r/?($n $u $d $z) <51.zox 93.pqh 74.dbd 1.qct $141>}>}
441432>
442433```
443434
@@ -456,8 +447,8 @@ Let's evaluate the `add` arm of `rs`, also modifying the door sample to `%u` for
456447
457448```
458449> ~(add rs %u)
459−< 1.dfv
460− {{a/@rs b/@rs} <21.vij {r/?($n $u $d $z) <54.tyv 119.wim 31.ohr 1.jmk $143>}>}
450+< 1.hsu
451+ {{a/@rs b/@rs} <21.fan {r/?($n $u $d $z) <51.zox 93.pqh 74.dbd 1.qct $141>}>}
461452>
462453```
463454