@@ -1,11 +1,11 @@
1:- :~ navhome/'/docs/'
1:- :~ navhome/'/docs/'
2 navuptwo/'true'
2 navuptwo/'true'
4− title/'1.6 Producing Gates with Core Arms'
4+ title/'1.6 Multi-gate Cores and Doors'
8−# 1.6 Producing Gates with Core Arms
8+# 1.6 Multi-gate Cores and Doors
10It'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.
10It'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.
@@ -21,26 +21,6 @@ There are two ways of making a function call in Hoon. First, you can call a gat
28− {our/@p now/@da eny/@uvJ}
30− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
33− our=~bitnus-holleb-rocmeg-fotfer--migsum-nidsym-hilput-patmus
34− now=~2018.8.24..05.51.53..97b9
36− \/0v3vd.sdp3o.ds8eq.5mr1s.du2v6.4a12r.poie2.04123.jaq8r.8qoum.mvb63.dblf7.7\/
37− lt1d.fqebb.pv6dr.lio19.re7lq.016q3.u03ne.c8aiu.7148p
41− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
@@ -54,6 +34,8 @@ The second way of making a function call involves an expression that _produces_
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.
57Are calls to `add` and `mul` of the Hoon standard library of the first kind, or the second?
39Are calls to `add` and `mul` of the Hoon standard library of the first kind, or the second?
@@ -64,9 +46,11 @@ Are calls to `add` and `mul` of the Hoon standard library of the first kind, or
49+They're of the second kind. Neither `add` nor `mul` resolves to a gate directly; they're each arms that _produce_ gates.
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.
51+Often the difference doesn't matter much. Either way you can do a function call using the `(gate arg)` syntax.
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.
71## A Gate-Building Core
55## A Gate-Building Core
@@ -101,9 +85,9 @@ Notice that each arm in core `c` is able to call the other arms of `c` -- `add-t
102### The `..arm` Syntax
86### The `..arm` Syntax
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.
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?
108Do you have an answer? If not, consider going back and trying to figure it out before moving on.
92Do you have an answer? If not, consider going back and trying to figure it out before moving on.
@@ -111,18 +95,16 @@ You may read `..arm` as '`.` of the parent core of `arm`'. This amounts to just
115 { {our/@p now/@da eny/@uvJ}
99 { {our/@p now/@da eny/@uvJ}
116− $~
100+ <19.anu 24.tmo 6.ipz 38.ard 119.spd 241.plj 51.zox 93.pqh 74.dbd 1.qct $141>
117− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
123 { {our/@p now/@da eny/@uvJ}
106 { {our/@p now/@da eny/@uvJ}
124− $~
107+ <19.anu 24.tmo 6.ipz 38.ard 119.spd 241.plj 51.zox 93.pqh 74.dbd 1.qct $141>
125− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
@@ -138,7 +120,7 @@ For an example of this, consider the `add` arm of the Hoon standard library. Th
138<74.dbd 1.qct $141>
120<74.dbd 1.qct $141>
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.
143### Mutating a Gate
125### Mutating a Gate
@@ -151,7 +133,15 @@ Let's say you want to modify the default sample of the gate for `double`. We ca
152Given 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.)
134Given 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.)
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!
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:
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:
157> =double-copy double:c
147> =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
177−It does indeed. Unbind `c` and `double-copy` as follows:
167+It does indeed. Unbind `c` and `double-copy`:
181> =double-copy
172> =double-copy
@@ -196,7 +187,7 @@ If you try to mutate the default sample of `add`, it won't work:
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.
201## Other Functions in the Hoon Standard Library
192## Other Functions in the Hoon Standard Library
@@ -207,7 +198,7 @@ Let's look once more at the parent core of the `add` arm in the Hoon standard li
207<74.dbd 1.qct $141>
198<74.dbd 1.qct $141>
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.)
212Here 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:
203Here 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:
@@ -265,11 +256,11 @@ A brief review: A core is a cell of battery and payload: `[battery payload]`. T
265New 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]`.
256New 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]`.
270−battery .
261+Battery .
272− sample context
263+ Sample Context
275It follows from this definition that a gate is a special case of a door. A gate is a door with exactly one arm, named `$`.
266It 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
297The `|_` rune for making a door works exactly like the `|%` rune for making a core, except it takes one additional subexpression.
288The `|_` rune for making a door works exactly like the `|%` rune for making a core, except it takes one additional subexpression.
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.
301#### Back to the Example
292#### Back to the Example
@@ -338,9 +329,11 @@ Having confirmed that `b` is `0`, let's mutate the `c` sample and then call its
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`:
344> (plus:c 10)
337> (plus:c 10)
@@ -371,34 +364,32 @@ Readers with some mathematical background may notice that `~( )` expressions all
373> ~(plus c 7)
366> ~(plus c 7)
378 {our/@p now/@da eny/@uvJ}
371 {our/@p now/@da eny/@uvJ}
380− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
373+ 24.tmo
386> b:~(plus c 7)
390> b:~(plus c 7)
394− {our/@p now/@da eny/@uvJ}
396− <16.igq 19.rrs 41.xjd 112.hmp 224.nab 54.tyv 119.wim 31.ohr 1.jmk $143>
402> b:~(plus c 17)
393> b:~(plus c 17)
@@ -429,15 +420,15 @@ It turns out that the `rs` in `add:rs` is a Hoon standard library arm that produ
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>}>
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:
440− {{a/@rs b/@rs} <21.vij {r/?($n $u $d $z) <54.tyv 119.wim 31.ohr 1.jmk $143>}>}
431+ {{a/@rs b/@rs} <21.fan {r/?($n $u $d $z) <51.zox 93.pqh 74.dbd 1.qct $141>}>}
@@ -456,8 +447,8 @@ Let's evaluate the `add` arm of `rs`, also modifying the door sample to `%u` for
458> ~(add rs %u)
449> ~(add rs %u)
460− {{a/@rs b/@rs} <21.vij {r/?($n $u $d $z) <54.tyv 119.wim 31.ohr 1.jmk $143>}>}
451+ {{a/@rs b/@rs} <21.fan {r/?($n $u $d $z) <51.zox 93.pqh 74.dbd 1.qct $141>}>}