on master help sign in
Update 1b
1b7073ermariani
docs/hoon/library/1b.md+88−121
@@ -16,24 +16,18 @@ Tests whether the tree address `a` is in the head or the tail of a noun.
16Produces the constant atom `%2` if it is within the head (subtree `+2`), or
16Produces the constant atom `%2` if it is within the head (subtree `+2`), or
17the constant atom `%3` if it is within the tail (subtree `+3`).
17the constant atom `%3` if it is within the tail (subtree `+3`).
18
18
19−In the Urbit binary-tree system, a parent at address `n` will always have its
20−children at address `2n` (the head) and address `2n+1` (the tail).
21
19
22−
23−Accepts
20+#### Accepts
24−-------
25
21
26`a` is an atom.
22`a` is an atom.
27
23
28−Produces
24+#### Produces
29−--------
30
25
31A constant atom.
26A constant atom.
32
27
33−Source
28+#### Source
34−------
35
29
36− ++ cap :: tree head
30+ ++ cap
37 ~/ %cap
31 ~/ %cap
38 |= a=@
32 |= a=@
39 ^- ?($2 $3)
33 ^- ?($2 $3)
@@ -43,10 +37,9 @@ Source
43 ?(%0 %1) !!
37 ?(%0 %1) !!
44 * $(a (div a 2))
38 * $(a (div a 2))
45 ==
39 ==
46− ::
47
40
41+
48−Examples
42+#### Examples
49−--------
50
43
51 > (cap 4)
44 > (cap 4)
52 %2
45 %2
@@ -77,52 +70,19 @@ Examples
77Address within head/tail
70Address within head/tail
78
71
79Computes the tree address of atom `a` within either the head (`+2`) or tail
72Computes the tree address of atom `a` within either the head (`+2`) or tail
80−(`+3`) of a noun. In other words, `++mas` returns the relative address of `a`
73+(`+3`) of a noun.
81−with respect to the head or the tail subtrees, depending on which one `a`
82−descends from.
83−
84− 1
85− / \
86− / \
87− 2 3 <--here are the head (`+2`) and the tail (`+3`)
88− / \ /\
89− 4 5 6 7
90− /\ /\ /\ /\
91− (continues...)
92
74
93−Running `(mas 7)` in the Dojo will return `3`, because address `+3` is what
94−`+7` now occupies. The tree below helps illustrate the relationship. With
95−parentheses are `a` values (if `a` is in subtree `+3`), and without parentheses
96−are the values returned with `(mas a)`.
97−
98− 1(3) ::new/(old) addresses
99− / \
100− 2 3
101− (6) (7)
102− / \ /\
103− / \ / \
104− 4 5 6 7
105− (12) (13) (14) (15)
106− / \ / \ / \ / \
107− (continues...)
108−
109−Notice how the old values in the head (subtree `+2`) were not illustrated in
110−this case, because `+7` is within the tail (subtree `+3`).
111−
112−Accepts
75+#### Accepts
113−-------
114
76
115`a` is an atom.
77`a` is an atom.
116
78
117−Produces
79+#### Produces
118−--------
119
80
120An atom.
81An atom.
121
82
122−Source
83+#### Source
123−------
124
84
125− ++ mas :: tree body
85+ ++ mas
126 ~/ %mas
86 ~/ %mas
127 |= a=@
87 |= a=@
128 ^- @
88 ^- @
@@ -132,10 +92,9 @@ Source
132 3 1
92 3 1
133 * (add (mod a 2) (mul $(a (div a 2)) 2))
93 * (add (mod a 2) (mul $(a (div a 2)) 2))
134 ==
94 ==
135− ::
136
95
96+
137−Examples
97+#### Examples
138−--------
139
98
140 > (mas 3)
99 > (mas 3)
141 1
100 1
@@ -159,12 +118,8 @@ Examples
159 > (mas (mas 11))
118 > (mas (mas 11))
160 3
119 3
161
120
162− > (mas 'p')
163− 47
164− > `@`'p'
165− 111
166−
167− > (cap (mas 5))
121+ > (cap (mas 6))
122+ %3
168
123
169 > (mas 0)
124 > (mas 0)
170 ! exit ::address `0` is in neither the head nor the tail
125 ! exit ::address `0` is in neither the head nor the tail
@@ -173,75 +128,62 @@ Examples
173 ! exit ::address `1` is in neither the head nor the tail
128 ! exit ::address `1` is in neither the head nor the tail
174
129
175
130
131+#### Discussion
176
132
177−***
178−### `++peg`
179−
180−Address within address
181−
182−Computes the absolute address of `b`, a relative address within the subtree
183−`a`.
184−
185−In other words, the subtree at address `a` is treated as a tree in its own
186−right (starting with root `+1`, head `+2`, and tail `+3`). Relative address
187−`b` is found with respect to `a`, and then its absolute address, within the
188−greater tree, is returned.
189−
190−Running `(peg 3 4)` in the Dojo, for example, will return `12`. Looking at
191−a tree diagram makes it easy to see why.
192−
193− 1
194− / \
195− / \
196− / \
197− 2 3 <- here is the subtree `+3`. The subtree address is `a` in `(peg a b)`
198− / \ / \
199− / \ / \
200− 4 5 6 7
201− / \ / \ / \ / \
202− 8 9 10 11 12 13 14 15
203− /\ /\ /\ /\ /\ /\ /\ /\
204− (continues...)
205
133
134+ 1
135+ / \
136+ / \
137+ 2 3 <--here are the head (`+2`) and the tail (`+3`)
138+ / \ /\
139+ 4 5 6 7
140+ /\ /\ /\ /\
141+ (continues...)
206
142
207−When we consider subtree at address `+3` by itself, it has relative addresses
143+Running `(mas 7)` in the Dojo will return `3`, because address `+3` is what
208−that are structured in the same way as its parent tree's absolute addresses.
144+`+7` now occupies. The tree below helps illustrate the relationship. With
209−The absolute addresses are given in parentheses in the diagram below.
145+parentheses are `a` values (if `a` is in subtree `+3`), and without parentheses
210−Notice how relative address `+4` is at the same position as absolute address
146+are the values returned with `(mas a)`.
211−`+12`.
212
147
213− 1(3) ::new/(old) addresses
148+ 1(3) ::new/(old) addresses
214 / \
149 / \
215 2 3
150 2 3
216 (6) (7)
151 (6) (7)
217 / \ /\
152 / \ /\
218 / \ / \
153 / \ / \
219 4 5 6 7
154 4 5 6 7
220− (12) (13) (14) (15)
155+ (12) (13) (14) (15)
221 / \ / \ / \ / \
156 / \ / \ / \ / \
222 (continues...)
157 (continues...)
223
158
159+Notice how the old values in the head (subtree `+2`) were not illustrated in
160+this case, because `+7` is within the tail (subtree `+3`).
161+
162+***
163+### `++peg`
164+
165+Address within address
224
166
167+Computes the absolute address of `b`, a relative address within the subtree
168+`a`.
169+
225−Accepts
170+#### Accepts
226−-------
227
171
228`a` is an atom.
172`a` is an atom.
229
173
230`b` is an atom.
174`b` is an atom.
231
175
232−Produces
176+#### Produces
233−--------
234
177
235An atom.
178An atom.
236
179
237−Source
180+#### Source
238−------
239
181
240− ++ peg :: tree connect
182+ ++ peg
241 ~/ %peg
183 ~/ %peg
242− |= [a=@ b=@] :: creates a gate
184+ |= [a=@ b=@]
243− ?< =(0 a) :: branches if there is no equality
185+ ?< =(0 a)
244− ^- @ :: casts to type @
186+ ^- @
245 ?- b
187 ?- b
246 $1 a
188 $1 a
247 $2 (mul a 2)
189 $2 (mul a 2)
@@ -249,8 +191,7 @@ Source
249 * (add (mod b 2) (mul $(b (div b 2)) 2))
191 * (add (mod b 2) (mul $(b (div b 2)) 2))
250 ==
192 ==
251
193
252−Examples
194+#### Examples
253−--------
254
195
255 > (peg 4 1)
196 > (peg 4 1)
256 4
197 4
@@ -270,24 +211,50 @@ Examples
270 > (peg 8 45)
211 > (peg 8 45)
271 269
212 269
272
213
273− > (peg 5 'a')
274− 353
275− > `@`'a'
276− 97
277−
278− > (peg 0x8 0x2d)
279− 269
280− > `@`0x8
281− 8
282− > `@`0x2d
283− 45
284−
285 > (cap (peg 4 2)) ::`(cap a)` computes whether address `a` is in the head or the tail
214 > (cap (peg 4 2)) ::`(cap a)` computes whether address `a` is in the head or the tail
286 %2
215 %2
287
216
288− > (peg 0 4)
289− ! exit
290
217
218+#### Discussion
219+
220+In other words, the subtree at address `a` is treated as a tree in its own
221+right (starting with root `+1`, head `+2`, and tail `+3`). Relative address
222+`b` is found with respect to `a`, and then its absolute address, within the
223+greater tree, is returned.
224+
225+Running `(peg 3 4)` in the Dojo, for example, will return `12`. Looking at
226+a tree diagram makes it easy to see why.
227+
228+ 1
229+ / \
230+ / \
231+ / \
232+ 2 3 <- here is the subtree `+3`. The subtree address is `a` in `(peg a b)`
233+ / \ / \
234+ / \ / \
235+ 4 5 6 7
236+ / \ / \ / \ / \
237+ 8 9 10 11 12 13 14 15
238+ /\ /\ /\ /\ /\ /\ /\ /\
239+ (continues...)
240+
241+
242+When we consider subtree at address `+3` by itself, it has relative addresses
243+that are structured in the same way as its parent tree's absolute addresses.
244+The absolute addresses are given in parentheses in the diagram below.
245+Notice how relative address `+4` is at the same position as absolute address
246+`+12`.
247+
248+ 1(3) ::new/(old) addresses
249+ / \
250+ 2 3
251+ (6) (7)
252+ / \ /\
253+ / \ / \
254+ 4 5 6 7
255+ (12) (13) (14) (15)
256+ / \ / \ / \ / \
257+ (continues...)
291
258
292
259
293
260