@@ -4,72 +4,85 @@ navhome: /docs/
6# 1b: tree addressing
6# 1b: tree addressing
8+Check out the Nock [explanation](docs/nock/explanation/) for more
9+information on the tree-addressing system.
7### `++cap`
11### `++cap`
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`).
15−Accepts
20+#### Accepts
18`a` is an atom.
22`a` is an atom.
20−Produces
24+#### Produces
23−A cube.
26+A constant atom.
28− ++ cap :: tree head
30+ ++ cap
31− ^- ?(%2 %3)
33+ ^- ?($2 $3)
35 ?(%0 %1) !!
37 ?(%0 %1) !!
36 * $(a (div a 2))
38 * $(a (div a 2))
40−Examples
42+#### Examples
47 > (cap (add 10 9))
50 > (cap (add 10 9))
53+ > (cap 1) ::address '1' is in neither the head nor the tail
53### `++mas`
68### `++mas`
55−Axis within head/tail?
70+Address within head/tail
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
59−Accepts
75+#### Accepts
62−`a` is an [atom]().
77+`a` is an atom.
64−Produces
79+#### Produces
72− ++ mas :: tree body
85+ ++ mas
@@ -79,69 +92,170 @@ Source
80 * (add (mod a 2) (mul $(a (div a 2)) 2))
93 * (add (mod a 2) (mul $(a (div a 2)) 2))
84−Examples
97+#### Examples
89 > (mas 4)
102 > (mas 4)
91 > (mas 5)
105 > (mas 5)
107+ > (cap 5) ::`(cap a)` computes whether address `a` is in the head or the tail
93− > (mas 6)
121+ > (cap (mas 6))
95− > (mas 0)
124+ > (mas 0)
96− ! exit
125+ ! exit ::address `0` is in neither the head nor the tail
97 > (mas 1)
127 > (mas 1)
98− ! exit
128+ ! exit ::address `1` is in neither the head nor the tail
137+ 2 3 <--here are the head (`+2`) and the tail (`+3`)
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)`.
148+ 1(3) ::new/(old) addresses
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`).
103### `++peg`
163### `++peg`
105−Axis within axis
165+Address within address
107−Computes the axis of `b` within axis `a`.
167+Computes the absolute address of `b`, a relative address within the subtree
109−Accepts
170+#### Accepts
112`a` is an atom.
172`a` is an atom.
114`b` is an atom.
174`b` is an atom.
116−Produces
176+#### Produces
121−Source
180+#### Source
124− ++ peg :: tree connect
182+ ++ peg
126− |= {a/@ b/@}
184+ |= [a=@ b=@]
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))
135−Examples
194+#### Examples
138− > (peg 4 1)
199+ > (peg 1 4)
140− > (peg 4 2)
202+ > (peg 4 2)
142− > (peg 8 45)
211+ > (peg 8 45)
214+ > (cap (peg 4 2)) ::`(cap a)` computes whether address `a` is in the head or the tail
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.
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.
232+ 2 3 <- here is the subtree `+3`. The subtree address is `a` in `(peg a b)`
237+ 8 9 10 11 12 13 14 15
238+ /\ /\ /\ /\ /\ /\ /\ /\
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
248+ 1(3) ::new/(old) addresses