on master help sign in
Merge pull request #266 from urbit/rmariani-patch-6
d51bc0cJoshua Reagan
docs/hoon/library/1b.md+166−52
@@ -4,72 +4,85 @@ navhome: /docs/
4
4
5
5
6# 1b: tree addressing
6# 1b: tree addressing
7+
8+Check out the Nock [explanation](docs/nock/explanation/) for more
9+information on the tree-addressing system.
10+
7### `++cap`
11### `++cap`
8
12
9Tree head
13Tree head
10
14
11−Tests whether an `a` is in the head or tail of a noun. Produces the
15+Tests whether the tree address `a` is in the head or the tail of a noun.
12−cube `%2` if it is within the head, or the cube `%3` if it is
16+Produces the constant atom `%2` if it is within the head (subtree `+2`), or
13−within the tail.
17+the constant atom `%3` if it is within the tail (subtree `+3`).
18+
14
19
15−Accepts
20+#### Accepts
16−-------
17
21
18`a` is an atom.
22`a` is an atom.
19
23
20−Produces
24+#### Produces
21−--------
22
25
23−A cube.
26+A constant atom.
24
27
25−Source
28+#### Source
26−------
27
29
28− ++ cap :: tree head
30+ ++ cap
29 ~/ %cap
31 ~/ %cap
30 |= a=@
32 |= a=@
31− ^- ?(%2 %3)
33+ ^- ?($2 $3)
32 ?- a
34 ?- a
33− %2 %2
35+ $2 %2
34− %3 %3
36+ $3 %3
35 ?(%0 %1) !!
37 ?(%0 %1) !!
36 * $(a (div a 2))
38 * $(a (div a 2))
37 ==
39 ==
38− ::
39
40
41+
40−Examples
42+#### Examples
41−--------
42
43
43 > (cap 4)
44 > (cap 4)
44 %2
45 %2
46+
45 > (cap 6)
47 > (cap 6)
46 %3
48 %3
49+
47 > (cap (add 10 9))
50 > (cap (add 10 9))
48 %2
51 %2
49
52
53+ > (cap 1) ::address '1' is in neither the head nor the tail
54+ ! exit
55+
56+ > (cap 0x40))
57+ %2
58+ > `@`0x40
59+ 64
60+
61+ > (cap 'a')
62+ %3
63+ > `@`'a'
64+ 97
50
65
51
66
52***
67***
53### `++mas`
68### `++mas`
54
69
55−Axis within head/tail?
70+Address within head/tail
56
71
57−Computes the axis of `a` within either the head or tail of a noun (depends whether `a` lies within the the head or tail).
72+Computes the tree address of atom `a` within either the head (`+2`) or tail
73+(`+3`) of a noun.
58
74
59−Accepts
75+#### Accepts
60−-------
61
76
62−`a` is an [atom]().
77+`a` is an atom.
63
78
64−Produces
79+#### Produces
65−--------
66
80
67An atom.
81An atom.
68
82
69−Source
83+#### Source
70−------
71
84
72− ++ mas :: tree body
85+ ++ mas
73 ~/ %mas
86 ~/ %mas
74 |= a=@
87 |= a=@
75 ^- @
88 ^- @
@@ -79,69 +92,170 @@ Source
79 3 1
92 3 1
80 * (add (mod a 2) (mul $(a (div a 2)) 2))
93 * (add (mod a 2) (mul $(a (div a 2)) 2))
81 ==
94 ==
82− ::
83
95
96+
84−Examples
97+#### Examples
85−--------
86
98
87 > (mas 3)
99 > (mas 3)
88 1
100 1
101+
89 > (mas 4)
102 > (mas 4)
90 2
103 2
104+
91 > (mas 5)
105 > (mas 5)
92 3
106 3
107+ > (cap 5) ::`(cap a)` computes whether address `a` is in the head or the tail
108+ %2
109+
110+ > (mas 7)
111+ 3
112+ > (cap 7)
113+ %3
114+
115+ > (mas 11)
116+ 7
117+
118+ > (mas (mas 11))
119+ 3
120+
93− > (mas 6)
121+ > (cap (mas 6))
94− 2
122+ %3
123+
95− > (mas 0)
124+ > (mas 0)
96− ! exit
125+ ! exit ::address `0` is in neither the head nor the tail
126+
97 > (mas 1)
127 > (mas 1)
98− ! exit
128+ ! exit ::address `1` is in neither the head nor the tail
129+
130+
131+#### Discussion
132+
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...)
99
142
143+Running `(mas 7)` in the Dojo will return `3`, because address `+3` is what
144+`+7` now occupies. The tree below helps illustrate the relationship. With
145+parentheses are `a` values (if `a` is in subtree `+3`), and without parentheses
146+are the values returned with `(mas a)`.
100
147
148+ 1(3) ::new/(old) addresses
149+ / \
150+ 2 3
151+ (6) (7)
152+ / \ /\
153+ / \ / \
154+ 4 5 6 7
155+ (12) (13) (14) (15)
156+ / \ / \ / \ / \
157+ (continues...)
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`).
101
161
102***
162***
103### `++peg`
163### `++peg`
104
164
105−Axis within axis
165+Address within address
106
166
107−Computes the axis of `b` within axis `a`.
167+Computes the absolute address of `b`, a relative address within the subtree
168+`a`.
108
169
109−Accepts
170+#### Accepts
110−-------
111
171
112`a` is an atom.
172`a` is an atom.
113
173
114`b` is an atom.
174`b` is an atom.
115
175
116−Produces
176+#### Produces
117−--------
118
177
119An atom.
178An atom.
120
179
121−Source
180+#### Source
122−------
123
181
124− ++ peg :: tree connect
182+ ++ peg
125 ~/ %peg
183 ~/ %peg
126− |= {a/@ b/@}
184+ |= [a=@ b=@]
185+ ?< =(0 a)
127− ^- @
186+ ^- @
128− ?- b
187+ ?- b
129 $1 a
188 $1 a
130 $2 (mul a 2)
189 $2 (mul a 2)
131 $3 +((mul a 2))
190 $3 +((mul a 2))
132 * (add (mod b 2) (mul $(b (div b 2)) 2))
191 * (add (mod b 2) (mul $(b (div b 2)) 2))
133 ==
192 ==
134
193
135−Examples
194+#### Examples
136−--------
195+
196+ > (peg 4 1)
197+ 4
137
198
138− > (peg 4 1)
199+ > (peg 1 4)
139 4
200 4
201+
140− > (peg 4 2)
202+ > (peg 4 2)
141 8
203 8
204+
205+ > (peg 4 8)
206+ 32
207+
208+ > (peg 4 (4 2))
209+ 32
210+
142− > (peg 8 45)
211+ > (peg 8 45)
143 269
212 269
144
213
214+ > (cap (peg 4 2)) ::`(cap a)` computes whether address `a` is in the head or the tail
215+ %2
216+
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...)
258+
145
259
146
260
147***
261***