on master help sign in
Merge pull request #281 from urbit/rmariani-patch-13

Update 2f

commita88de8b3279345569a82108a26d9c2b6976e311e authorJoshua Reagan <joshuareagan@users.noreply.github.com> dateMon 2 Jul 2018, 23:03 (8 years ago) parentd5d4701005ec77
1 file changed+216−73
docs/hoon/library/2f.md+216−73
docs/hoon/library/2f.md+216−73
@@ -6,30 +6,25 @@ navhome: /docs/
66# 2f: noun ordering
77### `++aor`
88
9−Alphabetic order
9+Alphabetical order
1010
11−Computes whether `a` and `b` are in alphabetical order, producing a
12−boolean.
11+Computes whether `a` and `b` are in alphabetical order, producing a flag.
1312
14−Accepts
15−-------
13+#### Accepts
1614
1715`a` is a noun.
1816
1917`b` is a noun.
2018
21−Produces
22−--------
19+#### Produces
2320
24−A boolean atom.
21+A flag.
2522
23+#### Source
2624
27−Source
28−------
29−
30− ++ aor :: a-order
25+ ++ aor
3126 ~/ %aor
32− |= {a/* b/*}
27+ |= [a=* b=*]
3328 ^- ?
3429 ?: =(a b) &
3530 ?. ?=(@ a)
@@ -44,24 +39,32 @@ Source
4439 $(a (rsh 3 1 a), b (rsh 3 1 b))
4540 (lth c d)
4641
47−
48−Examples
49−--------
42+#### Examples
5043
5144 > (aor 'a' 'b')
5245 %.y
46+
5347 > (aor 'b' 'a')
5448 %.n
49+
50+ > (aor 'a' 'a')
51+ %.y
52+
5553 > (aor "foo" "bar")
5654 %.n
55+
5756 > (aor "bar" "foo")
5857 %.y
58+
5959 > (aor "abcdefz" "abcdefa")
6060 %.n
61+
6162 > (aor "abcdefa" "abcdefz")
6263 %.y
64+
6365 > (aor 10.000 17.000)
6466 %.y
67+
6568 > (aor 10 9)
6669 %.n
6770
@@ -72,27 +75,23 @@ Examples
7275
7376Numeric order
7477
75−Computes whether `a` and `b` are in ascending numeric order, producing a
76−boolean.
78+Computes whether `a` and `b` are in ascending numeric order, producing a flag.
7779
78−Accepts
79−-------
80+#### Accepts
8081
8182`a` is a noun.
8283
8384`b` is a noun.
8485
85−Produces
86−--------
86+#### Produces
8787
88−A boolean atom.
88+A flag.
8989
90−Source
91−------
90+#### Source
9291
93− ++ dor :: d-order
92+ ++ dor
9493 ~/ %dor
95− |= {a/* b/*}
94+ |= [a=* b=*]
9695 ^- ?
9796 ?: =(a b) &
9897 ?. ?=(@ a)
@@ -104,22 +103,34 @@ Source
104103 (lth a b)
105104
106105
107−Examples
108−--------
106+#### Examples
109107
110108 > (dor 1 2)
111109 %.y
110+
112111 > (dor 2 1)
113112 %.n
113+
114114 > (dor ~[1 2 3] ~[1 2 4])
115115 %.y
116+
116117 > (dor ~[1 2 4] ~[1 2 3])
117118 %.n
118− > (dor (limo ~[99 100 10.000]) ~[99 101 10.000])
119+
120+ > (dor `(list @)`~[99 100 10.000] ~[99 101 10.000])
119121 %.y
120− > (dor ~[99 101 10.999] (limo ~[99 100 10.000]))
122+
123+ > (dor ~[99 101 10.999] `(list @)`~[99 100 10.000])
121124 %.n
122125
126+#### Discussion
127+
128+If `a` and `b` are both atoms, `dor` is equivalent to [`lte`](../1a). If they're
129+cells, `dor` recurses on the heads, and then if the heads are the same it checks
130+the tails.
131+
132+If one sample is a cell and the other is an atom, the cell sample is treated as
133+"greater."
123134
124135
125136***
@@ -127,26 +138,27 @@ Examples
127138
128139Hash order
129140
130−XX revisit
141+Computes whether of `(mug a)` and `(mug b)` are in ascending numeric order,
142+producing a flag. If the `mug` hashes are equal, `a` and `b` are compared by
143+`dor` instead.
144+
145+[`mug`](../2e) is the the 31-bit nonzero FNV-1a hash algorithm.
131146
132−Accepts
133−-------
147+#### Accepts
134148
135149`a` is a noun.
136150
137151`b` is a noun.
138152
139−Produces
140−--------
153+#### Produces
141154
142−A boolean atom.
155+A flag.
143156
144−Source
145−------
157+#### Source
146158
147− ++ gor :: g-order
159+ ++ gor
148160 ~/ %gor
149− |= {a/* b/*}
161+ |= [a=* b=*]
150162 ^- ?
151163 =+ [c=(mug a) d=(mug b)]
152164 ?: =(c d)
@@ -154,61 +166,77 @@ Source
154166 (lth c d)
155167
156168
157−Examples
158−--------
169+#### Examples
159170
160171 > (gor 'd' 'c')
161172 %.y
173+
162174 > 'd'
163175 'd'
176+
164177 > 'c'
178+ 'c'
179+
165180 > `@ud`'d'
166181 100
182+
167183 > `@ud`'c'
168184 99
169− > (mug 'd')
185+
186+ > (mug 'd')
170187 1.628.185.714
188+
171189 > (mug 'c')
172190 1.712.073.811
191+
173192 > (gor 'd' 'c')
174193 %.y
194+
175195 > (gor 'c' 'd')
176196 %.n
197+
177198 > (gor "foo" "bar")
178199 %.n
179− > (gor (some 10) (limo [1 2 3 ~]))
200+
201+ > (gor (some 10) `(list @)`[1 2 3 ~])
180202 %.n
181203
204+#### Discussion
182205
206+`map`s use `gor` on the key for horizontal ordering and `vor` for
207+vertical order. `map`s only look at the keys (the head of the key-value pair
208+elements) for ordering.
183209
184210***
185211### `++hor`
186212
187−Hash order
213+Horizontal hash order
214+
215+If `a` and `b` are atoms, they are computed as samples of `gor`, producing a
216+flag.
188217
189−XX revisit
218+If `a` and `b` are cells, `hor` checks to see if their heads are equal. If their
219+heads are equal, `gor` is computed on their tails, producing a flag. If their
220+heads are not equal, `gor` is computed on their heads, producing a flag.
190221
191−Recursive hash comparator gate.
222+If `a` is an atom and `b` is not, then `%.y` is produced. If `b` is an atom and
223+`a` is not, `%.n` is produced.
192224
193−Accepts
194−-------
225+#### Accepts
195226
196227`a` is a noun.
197228
198229`b` is a noun.
199230
200−Produces
201−--------
231+#### Produces
202232
203−A boolean atom.
233+A flag.
204234
235+#### Source
205236
206−Source
207−------
208−
209− ++ hor :: h-order
237+ ++ hor
210238 ~/ %hor
211− |= {a/* b/*}
239+ |= [a=* b=*]
212240 ^- ?
213241 ?: ?=(@ a)
214242 ?. ?=(@ b) &
@@ -217,46 +245,144 @@ Source
217245 ?: =(-.a -.b)
218246 (gor +.a +.b)
219247 (gor -.a -.b)
220− ::
221248
222249
223−Examples
224−--------
250+#### Examples
225251
226252 > (hor . 1)
227253 %.n
254+
228255 > (hor 1 2)
229256 %.y
257+
230258 > (hor "abc" "cba")
231259 %.y
260+
232261 > (hor 'c' 'd')
233262 %.n
234263
264+ > (hor 100 [1 1])
265+ %.y
266+
267+ > (hor [1 1] 100)
268+ %.n
269+
270+ > (hor 500.000.000 "foo")
271+ %.y
272+
273+ > (hor "foo" 500.000.000)
274+ %.n
275+
276+ > (hor 0 "foo")
277+ %.y
278+
279+ > (gor `(list @)`[0 ~] 1)
280+ %.n
281+
282+ > (hor `(list @)`[50 500 5.000 ~] 1)
283+ %.n
284+
235285
286+#### Discussion
287+
288+Sets (+set, +in) use `hor` for their horizontal order and `vor` for their
289+vertical order. See [`apt:in`](../2h) to see how a set verifies its correctness.
290+`vor` is used to verify.
291+
292+***
293+### `++lor`
294+
295+Leg order
296+
297+Computes whether the corresponding legs of `a` and `b` are in ascending
298+numeric order, producing a flag.
299+
300+#### Accepts
301+
302+`a` is a noun.
303+
304+`b` is a noun.
305+
306+#### Accepts
307+
308+A flag.
309+
310+#### Source
311+
312+ ++ lor
313+ ~/ %lor
314+ |= [a=* b=*]
315+ ^- ?
316+ ?: =(a b) &
317+ ?@ a
318+ ?^ b &
319+ (lth a b)
320+ ?: =(-.a -.b)
321+ $(a +.a, b +.b)
322+ $(a -.a, b -.b)
323+
324+
325+#### Examples
326+
327+ > (lor 1 2)
328+ %.y
329+
330+ > (lor 2 1)
331+ %.n
332+
333+ > (lor 9 [1 2])
334+ %.y
335+
336+ > (lor `(list @)`[1 2 3 ~] `(list @)`[1 2 3 4 ~])
337+ %.y
338+
339+ > (lor `(list @)`[1 2 3 ~] `(list @)`[1 2 2 ~])
340+ %.n
341+
342+ > (lor `(list @)`[1 2 3 ~] `(list @)`[2 2 ~])
343+ %.y
344+
345+ > (lor [2 2 5] [1 5 9 8])
346+ %.n
347+
348+ > (lor [1 5 8] [1 5 9 8])
349+ %.y
350+
351+ > (lor [1 2 3] [1 2])
352+ !! exit
353+
354+
355+#### Discussion
356+
357+`lor` is no longer used internally.
236358
237359***
238360### `++vor`
239361
240−Double hash comparator gate.
362+Vertical order
363+
364+Computes whether the double-hashes `(mug (mug a))` and `(mug (mug b))` are in
365+ascending numeric order, producing a flag. If the double-`mug` hashes are
366+equal, `a` and `b` are compared by `dor` instead.
241367
242−XX revisit
368+[`mug`](../2e) is the the 31-bit nonzero FNV-1a hash algorithm.
243369
244−Accepts
245−-------
370+
371+#### Accepts
246372
247373`a` is a noun
248374
249375`b` is a noun
250376
251−Produces
252−-------
377+#### Produces
378+
379+A flag.
253380
254−Source
255−------
381+#### Source
256382
257− ++ vor :: v-order
383+ ++ vor
258384 ~/ %vor
259− |= {a/* b/*}
385+ |= [a=* b=*]
260386 ^- ?
261387 =+ [c=(mug (mug a)) d=(mug (mug b))]
262388 ?: =(c d)
@@ -264,16 +390,33 @@ Source
264390 (lth c d)
265391
266392
267−Examples
268−--------
393+#### Examples
269394
270395 > (vor 'f' 'g')
271396 %.y
397+
398+ > [(mug 'f') (mug 'g')]
399+ [1.661.740.952 1.644.963.335]
400+
401+ > [(mug (mug 'f')) (mug (mug 'g'))]
402+ [261.421.509 1.861.258.547]
403+
272404 > (vor 'a' 'z')
273405 %.n
406+
274407 > (vor 43.326 41.106)
275408 %.n
276409
277410
411+#### Discussion
412+
413+Maps, sets, and queues all use `vor` to check for vertical ordering. Maps and
414+sets also use `gor` and `hor` for horizontal order, respectively, but queues use
415+vertical ordering alone.
416+
417+Since hashing removes correlation, double-`mug`ging with `vor` removes
418+correlation with single-`mug`ged `hor`. Vertical order becomes uncorrelated
419+with horizontal order.
420+
278421
279422***