on 4fdc19d help sign in
826 lines 10 kB history raw
1+++
2title = "2h: Set Logic"
3weight = 11
4template = "doc.html"
5+++
7## `++in`
9Set operations
11Core whose arms contain a variety of functions that operate on `set`s. Its
12sample accepts the input `set` to be manipulated.
14#### Accepts
16A `set`.
18#### Source
20```hoon
21 ~/ %in
22 =| a=(tree)
23 |@
24```
26#### Examples
28```
29> ~(. in (sy "asd"))
30<16.ufw [a=?(%~ [?(n=@tD n=#1) l=nlr(?(@tD #1)) r=nlr(?(@tD ^#1.?(@tD #1)))]) <123.zao 46.hgz 1.pnw %140>]>
31```
33### `++all:in`
35Logical AND
37Computes the logical AND on every element in `a` slammed with `b`, producing a
38flag.
40#### Accepts
42`a` is a `set`, and is the sample of `+in`.
44`b` is a gate that accepts a noun and produces a flag.
46#### Produces
48A flag.
50#### Source
52```hoon
53++ all
54 ~/ %all
55 |* b=$-(* ?)
56 |- ^- ?
57 ?~ a
58 &
59 ?&((b n.a) $(a l.a) $(a r.a))
60```
62#### Examples
64```
65> (~(all in (silt ~[1 2 3 4])) |=(a=@ (lth a 5)))
66%.y
68> (~(all in (silt ~[1 2 3 4 5])) |=(a=@ (lth a 5)))
69%.n
70```
72---
74### `++any:in`
76Logical OR
78Computes the logical OR on every element of `a` slammed with `b`, producing a
79flag.
81#### Accepts
83`a` is a `set`, and is the sample of `+in`.
85`b` is a gate that accepts a noun and produces a flag.
87#### Produces
89A flag.
91#### Source
93```hoon
94++ any
95 ~/ %any
96 |* b=$-(* ?)
97 |- ^- ?
98 ?~ a
99 |
100 ?|((b n.a) $(a l.a) $(a r.a))
101```
103#### Examples
105```
106> (~(any in (silt ~[2 3 4 5])) |=(a=@ (lth a 3)))
107%.y
109> (~(any in (silt ~[3 4 5])) |=(a=@ (lth a 3)))
110%.n
111```
113---
115### `++apt:in`
117Check correctness
119Computes whether `a` has a correct horizontal order and a correct vertical
120order, producing a flag.
122#### Accepts
124`a` is a set.
126#### Produces
128A flag.
130#### Source
132```hoon
133++ apt
134 =< $
135 ~/ %apt
136 =| [l=(unit) r=(unit)]
137 |. ^- ?
138 ?~ a &
139 ?& ?~(l & (gor n.a u.l))
140 ?~(r & (gor u.r n.a))
141 ?~(l.a & ?&((mor n.a n.l.a) $(a l.a, l `n.a)))
142 ?~(r.a & ?&((mor n.a n.r.a) $(a r.a, r `n.a)))
143 ==
144```
146#### Examples
148```
149> ~(apt in ~)
150%.y
151```
153```
154> =a (silt ~[1 2 3])
155> a
156[n=2 l={1} r={3}]
157> ~(apt in a)
158%.y
160> =z ?~(a ~ a(n 10))
161> z
162[n=10 l={1} r={3}]
163> ~(apt in z)
164%.n
165```
167#### Discussion
169See section [`2f`](/docs/hoon/reference/stdlib/2f) for more information on noun ordering.
171---
173### `++bif:in`
175Bifurcate
177Splits set `a` into sets `l` and `r`, which contain the items either side of `b` but not including `b`.
179#### Accepts
181`a` is a set, and is the sample of `+in`.
183`b` is a noun.
185#### Produces
187A cell of two sets.
189#### Source
191```hoon
192++ bif
193 ~/ %bif
194 |* b=*
195 ^+ [l=a r=a]
196 =< +
197 |- ^+ a
198 ?~ a
199 [b ~ ~]
200 ?: =(b n.a)
201 a
202 ?: (gor b n.a)
203 =+ c=$(a l.a)
204 ?> ?=(^ c)
205 c(r a(l r.c))
206 =+ c=$(a r.a)
207 ?> ?=(^ c)
208 c(l a(r l.c))
209```
211#### Examples
213```
214> =a `(set @)`(silt (gulf 1 20))
215> a
216{17 8 20 13 11 5 19 7 15 10 18 14 6 12 9 1 2 3 16 4}
218> (~(bif in a) 10)
219[l=[n=11 l={17 8 20 13} r={5 19 7 15}] r=[n=12 l={18 14 6} r={9 1 2 3 16 4}]]
221> `[(set @) (set @)]`(~(bif in a) 10)
222[{17 8 20 13 11 5 19 7 15} {18 14 6 12 9 1 2 3 16 4}]
223```
225#### Discussion
227Note that `set`s are horizontally ordered by the [mug](/docs/hoon/reference/stdlib/2e#mug) hash of their items and vertically ordered by the double-`mug` hash of their items. This means bifurcating the set of numbers `(silt ~[10 20 30 40 50])` at `30` will not produce `[{10 20} {40 50}]`, but rather `[{20} {10 40 50}]` due to the tree structure resulting from their `mug` hashes.
229---
231### `++del:in`
233Remove noun
235Removes `b` from the `set` `a`.
237#### Accepts
239`a` is a set, and is the sample of `+in`.
241`b` is a noun.
243#### Produces
245A set.
247#### Source
249```hoon
250++ del
251 ~/ %del
252 |* b=*
253 |- ^+ a
254 ?~ a
255 ~
256 ?. =(b n.a)
257 ?: (gor b n.a)
258 a(l $(a l.a))
259 a(r $(a r.a))
260 |- ^- [$?(~ _a)]
261 ?~ l.a r.a
262 ?~ r.a l.a
263 ?: (mor n.l.a n.r.a)
264 l.a(r $(l.a r.l.a))
265 r.a(l $(r.a l.r.a))
266```
268#### Examples
270```
271> `(set @)`(~(del in (silt ~[1 2 3 4 5])) 3)
272{5 1 2 4}
273```
275```
276> `(set @t)`(~(del in (silt ~['foo' 'bar' 'baz'])) 'bar')
277{'baz' 'foo'}
278```
280```
281> `(set @)`(~(del in (silt ~[1 2 3 4 5])) 10)
282{5 1 2 3 4}
283```
285```
286> `(set @)`(~(del in ~) 10)
287{}
288```
290---
292### `++dif:in`
294Difference
296Computes the difference between `a` and `b`, producing the set of items in `a`
297that are not in `b`.
299#### Accepts
301`a` is a set, and is the sample of `+in`.
303`b` is a set.
305#### Produces
307A set.
309#### Source
311```hoon
312++ dif
313 ~/ %dif
314 =+ b=a
315 |@
316 ++ $
317 |- ^+ a
318 ?~ b
319 a
320 =+ c=(bif n.b)
321 ?> ?=(^ c)
322 =+ d=$(a l.c, b l.b)
323 =+ e=$(a r.c, b r.b)
324 |- ^- [$?(~ _a)]
325 ?~ d e
326 ?~ e d
327 ?: (mor n.d n.e)
328 d(r $(d r.d))
329 e(l $(e l.e))
330 --
331```
333#### Examples
335```
336> =a (silt ~[1 2 3 4 5])
337> =b (silt ~[3 4])
339> `(set @)`(~(dif in a) b)
340{5 1 2}
341```
343---
345### `++dig:in`
347Address b in a
349Produce the tree address of `b` within `a`.
351#### Accepts
353`a` is a set, and is the sample of `+in`.
355`b` is a noun.
357#### Produces
359The `unit` of an atom.
361#### Source
363```hoon
364++ dig
365 |= b=*
366 =+ c=1
367 |- ^- (unit @)
368 ?~ a ~
369 ?: =(b n.a) [~ u=(peg c 2)]
370 ?: (gor b n.a)
371 $(a l.a, c (peg c 6))
372 $(a r.a, c (peg c 7))
373```
375#### Examples
377```
378> =a (silt ~[1 2 3 4 5 6 7])
380> -.a
381n=6
383> (~(dig in a) 7)
384[~ 12]
386> (~(dig in a) 2)
387[~ 60]
389> (~(dig in a) 6)
390[~ 2]
392> (~(dig in a) 10)
394```
396#### Discussion
398For more on the tree addressing system, see section [1b](/docs/hoon/reference/stdlib/1b).
400---
402### `++gas:in`
404Concatenate
406Insert the elements of a `list` `b` into a `set` `a`.
408#### Accepts
410`a` is a set, and is the sample of `+in`.
412`b` is a list.
414#### Produces
416A `set`.
418#### Source
420```hoon
421++ gas
422 ~/ %gas
423 |= b=(list _?>(?=(^ a) n.a))
424 |- ^+ a
425 ?~ b
426 a
427 $(b t.b, a (put i.b))
428```
430#### Examples
432```
433> =a (silt ~['foo' 'bar' 'baz'])
434> `(set @t)`a
435{'bar' 'baz' 'foo'}
437> `(set @t)`(~(gas in a) ~['foo' 'foo' 'foo' 'foo'])
438{'bar' 'baz' 'foo'}
440> `(set @t)`(~(gas in a) ~['abc' 'xyz' '123'])
441{'xyz' 'bar' 'baz' 'foo' 'abc' '123'}
442```
444---
446### `++has:in`
448b in a?
450Checks if `b` is an element of `a`, producing a flag.
452#### Accepts
454`a` is a set, and is the sample of `+in`.
456`b` is a noun.
458#### Produces
460A flag.
462#### Source
464```hoon
465++ has
466 ~/ %has
467 |* b=*
468 ^- ?
469 %. [~ b]
470 |= b=(unit _?>(?=(^ a) n.a))
471 => .(b ?>(?=(^ b) u.b))
472 |- ^- ?
473 ?~ a
474 |
475 ?: =(b n.a)
476 &
477 ?: (gor b n.a)
478 $(a l.a)
479 $(a r.a)
480```
482#### Examples
484```
485> =a (silt ~[1 2 3 4 5])
487> (~(has in a) 2)
488%.y
490> (~(has in a) 6)
491%.n
492```
494---
496### `++int:in`
498Intersection
500Produces a set of the intersection between two sets of the same type,
501`a` and `b`.
503#### Accepts
505`a` is a set, and is the sample of `+in`.
507`b` is a set.
509#### Produces
511A `set`.
513#### Source
515```hoon
516++ int
517 ~/ %int
518 =+ b=a
519 |@
520 ++ $
521 |- ^+ a
522 ?~ b
523 ~
524 ?~ a
525 ~
526 ?. (mor n.a n.b)
527 $(a b, b a)
528 ?: =(n.b n.a)
529 a(l $(a l.a, b l.b), r $(a r.a, b r.b))
530 ?: (gor n.b n.a)
531 %- uni(a $(a l.a, r.b ~)) $(b r.b)
532 %- uni(a $(a r.a, l.b ~)) $(b l.b)
533 --
534```
536#### Examples
538```
539> `(set @tD)`(~(int in (silt "foobar")) (silt "bar"))
540{'r' 'b' 'a'}
542> `(set @tD)`(~(int in (silt "foobar")) ~)
543{}
545> `(set @tD)`(~(int in (silt "foobar")) (silt "baz"))
546{'b' 'a'}
547```
549---
551### `++put:in`
553Put b in a
555Add an element `b` to the set `a`, producing a set.
557#### Accepts
559`a` is a set, and is the sample of `+in`.
561`b` is a noun.
563#### Produces
565A `set`.
567#### Source
569```hoon
570++ put
571 ~/ %put
572 |* b=*
573 |- ^+ a
574 ?~ a
575 [b ~ ~]
576 ?: =(b n.a)
577 a
578 ?: (gor b n.a)
579 =+ c=$(a l.a)
580 ?> ?=(^ c)
581 ?: (mor n.a n.c)
582 a(l c)
583 c(r a(l r.c))
584 =+ c=$(a r.a)
585 ?> ?=(^ c)
586 ?: (mor n.a n.c)
587 a(r c)
588 c(l a(r l.c))
589```
591#### Examples
593```
594> `(set @)`(~(put in (silt ~[1 2 3])) 4)
595{1 2 3 4}
597> `(set @)`(~(put in `(set @)`~) 42)
598{42}
599```
601---
603### `++rep:in`
605Accumulate
607Accumulate the elements of `a` using binary gate `b`.
609#### Accepts
611`a` is a `set`, and is the sample of `+in`.
613`b` is a gate.
615#### Produces
617A noun.
619#### Source
621```hoon
622++ rep
623 ~/ %rep
624 |* b=_=>(~ |=([* *] +<+))
625 |-
626 ?~ a +<+.b
627 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
628```
630#### Examples
632```
633> (~(rep in (silt ~[1 2 3 4 5])) add)
634b=15
635```
637```
638> `@t`(~(rep in (silt ~['foo' 'bar' 'baz'])) |=(a=[@ @] (cat 3 a)))
639'foobarbaz'
640```
642---
644### `++run:in`
646Apply gate to set
648Produce a `set` containing the products of gate `b` applied to each element
649in `a`.
651#### Accepts
653`a` is a `set`.
655`b` is a gate.
657#### Produces
659A set.
661#### Source
663```hoon
664++ run
665 ~/ %run
666 |* b=gate
667 =+ c=`(set _?>(?=(^ a) (b n.a)))`~
668 |- ?~ a c
669 =. c (~(put in c) (b n.a))
670 =. c $(a l.a, c c)
671 $(a r.a, c c)
672```
674#### Examples
676```
677> =s (silt ~["a" "A" "b" "c"])
678> `(set tape)`s
679{"A" "a" "c" "b"}
681> (~(run in s) cuss)
682{"A" "C" "B"}
683```
685---
687### `++tap:in`
689Set to list
691Flattens the `set` `a` into a `list`.
693#### Accepts
695`a` is an set.
697#### Produces
699A list.
701#### Source
703```hoon
704++ tap
705 =< $
706 ~/ %tap
707 =+ b=`(list _?>(?=(^ a) n.a))`~
708 |. ^+ b
709 ?~ a
710 b
711 $(a r.a, b [n.a $(a l.a)])
712```
714#### Examples
716```
717> ~(tap in (silt "foobar"))
718"oafbr"
719```
721```
722> ~(tap in (silt ~[1 2 3 4 5]))
723~[4 3 2 1 5]
724```
726---
728### `++uni:in`
730Union
732Produces a set of the union between two sets of the same type, `a` and
733`b`.
735#### Accepts
737`a` is a set, and is the sample of `+in`.
739`b` is a set.
741#### Produces
743A set.
745#### Source
747```hoon
748++ uni
749 ~/ %uni
750 =+ b=a
751 |@
752 ++ $
753 ?: =(a b) a
754 |- ^+ a
755 ?~ b
756 a
757 ?~ a
758 b
759 ?: =(n.b n.a)
760 b(l $(a l.a, b l.b), r $(a r.a, b r.b))
761 ?: (mor n.a n.b)
762 ?: (gor n.b n.a)
763 $(l.a $(a l.a, r.b ~), b r.b)
764 $(r.a $(a r.a, l.b ~), b l.b)
765 ?: (gor n.a n.b)
766 $(l.b $(b l.b, r.a ~), a r.a)
767 $(r.b $(b r.b, l.a ~), a l.a)
768 --
769```
771#### Examples
773```
774> =a (silt ~[1 2 3 4 5])
775> =b (silt ~[4 5 6 7 8])
777> `(set @)`(~(uni in a) b)
778{8 5 7 6 1 2 3 4}
780> `(set @)`(~(uni in a) ~)
781{5 1 2 3 4}
783> `(set @)`(~(uni in `(set @)`~) b)
784{8 5 7 6 4}
785```
787---
789### `++wyt:in`
791Set size
793Produces the number of elements in set `a` as an atom.
795#### Accepts
797`a` is an set.
799#### Produces
801An atom.
803#### Source
805```hoon
806++ wyt
807 =< $
808 ~% %wyt + ~
809 |. ^- @
810 ?~(a 0 +((add $(a l.a) $(a r.a))))
811--
812```
814#### Examples
816```
817> ~(wyt in (silt ~[1 2 3 4]))
819```
821```
822> ~(wyt in `(set @)`~)
824```
826---