on 979ae21 help sign in
798 lines 12 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 ~/ %in
23 |_ a=(set)
24```
26#### Examples
28```
29 > ~(. in (sy "asd"))
30 <13.evb [nlr(^$1{@tD $1}) <414.fvk 101.jzo 1.ypj %164>]>
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`.
44`b` is a wet 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 > =b (sy `(list [@t *])`[['a' 1] ['b' [2 3]] ~])
66 > (~(all in b) |=(a=* ?@(+.a & |)))
67 %.n
69 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
70 > (~(all in b) |=(a=@t (gte a 100)))
71 %.y
72```
74---
75### `+-any:in`
77Logical OR
79Computes the logical OR on every element of `a` slammed with `b`, producing a
80flag.
82#### Accepts
84`a` is a `set`.
86`b` is a gate that accepts a noun and produces a flag.
88#### Produces
90A flag.
92#### Source
94```hoon
95 +- any
96 ~/ %any
97 |* b=$-(* ?)
98 |- ^- ?
99 ?~ a
100 |
101 ?|((b n.a) $(a l.a) $(a r.a))
102```
104#### Examples
106```
107 > =b (sy `(list [@t *])`[['a' 1] ['b' [2 3]] ~])
108 > (~(any in b) |=(a/* ?@(+.a & |)))
109 %.y
111 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
112 > (~(any in b) |=(a=@t (lte a 100)))
113 %.n
114```
116---
117### `+-apt:in`
119Check correctness
121Computes whether `a` has a correct horizontal order and a correct vertical
122order, producing a flag.
124#### Accepts
126`a` is a set.
128#### Produces
130A flag.
132#### Source
134```hoon
135 +- apt
136 =| [l=(unit) r=(unit)]
137 |- ^- ?
138 ?~ a &
139 ?& ?~(l & (hor n.a u.l))
140 ?~(r & (hor u.r n.a))
141 ?~(l.a & ?&((vor n.a n.l.a) $(a l.a, l `n.a)))
142 ?~(r.a & ?&((vor n.a n.r.a) $(a r.a, r `n.a)))
143 ==
144```
146#### Examples
148```
149 > ~(apt in ~)
150 %.y
152 > =a (sy [1 2 3 4 5 6 ~])
153 > a
154 [n=6 l={5 4} r={1 3 2}]
155 > ~(apt in a)
156 %.y
158 > =z ?~ a ~ a(n 7)
159 > z
160 [n=7 l={5 4} r={1 3 2}]
161 > ~(apt in z)
162 %.n
163```
165#### Discussion
167See section [`2f`](@/docs/hoon/reference/stdlib/2f.md) for more information on noun ordering.
169---
170### `+-bif:in`
172Bifurcate
174Splits set `a` into two sets `l` and `r`, with any noun `b` removed.
176#### Accepts
178`a` is a set.
180`b` is a noun.
182#### Produces
184A cell of two sets.
186#### Source
188```hoon
189 +- bif
190 ~/ %bif
191 |* b=*
192 ^+ [l=a r=a]
193 =< [+< +>]
194 |- ^+ a
195 ?~ a
196 [b ~ ~]
197 ?: =(b n.a)
198 a
199 ?: (hor b n.a)
200 =+ c=$(a l.a)
201 ?> ?=(^ c)
202 [n.c l.c [n.a r.c r.a]]
203 =+ c=$(a r.a)
204 ?> ?=(^ c)
205 [n.c [n.a l.a l.c] r.c]
206```
208#### Examples
210```
211 > =a (sy `(list @)`[1 2 3 4 5 6 ~])
212 > a
213 [n=6 l={5 4} r={1 3 2}]
215 > (~(bif in a) 6)
216 [l=[n=4 l=[n=5 l={} r={}] r=~] r=[n=2 l=[n=1 l={} r={3}] r=~]]
218 > (~(bif in a) 100)
219 [l=[n=6 l=[n=4 l={5} r={}] r=[n=2 l={1 3} r={}]] r=~]
220```
222---
223### `+-del:in`
225Remove noun
227Removes `b` from the `set` `a`.
229#### Accepts
231`a` is a set.
233`b` is a noun.
235#### Produces
237A set.
239#### Source
241```hoon
242 +- del
243 ~/ %del
244 |* b=*
245 |- ^+ a
246 ?~ a
247 ~
248 ?. =(b n.a)
249 ?: (hor b n.a)
250 [n.a $(a l.a) r.a]
251 [n.a l.a $(a r.a)]
252 |- ^- {$?($~ _a)}
253 ?~ l.a r.a
254 ?~ r.a l.a
255 ?: (vor n.l.a n.r.a)
256 [n.l.a l.l.a $(l.a r.l.a)]
257 [n.r.a $(r.a l.r.a) r.r.a]
258```
261#### Examples
263```
264 > =b (sy `(list @t)`['a' 'b' 'c' ~])
265 > (~(del in b) 'a')
266 [n='b' l=[n='c' l={} r={}] r=~]
268 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
269 > (~(del in b) 'john')
270 [n='madeleine' l=[n='daniel' l={} r={'bonita'}] r=~]
272 > (~(del in b) 'susan')
273 [n='madeleine' l=[n='daniel' l={} r={'bonita'}] r=[n='john' l={} r={}]]
274```
276---
277### `+-dif:in`
279Difference
281Computes the difference between `a` and `b`, producing the set of items in `a`
282that are not in `b`.
284#### Accepts
286`a` is a set.
288`b` is a set.
290#### Produces
292A set.
294#### Source
296```hoon
297 +- dif
298 ~/ %dif
299 |* b=_a
300 |- ^+ a
301 ?~ b
302 a
303 =+ c=(bif n.b)
304 ?> ?=(^ c)
305 =+ d=$(a l.c, b l.b)
306 =+ e=$(a r.c, b r.b)
307 |- ^- {$?($~ _a)}
308 ?~ e d
309 ?: (vor n.d n.e)
310 [n.d l.d $(d r.d)]
311 [n.e $(e l.e) r.e]
312```
314#### Examples
316```
317 > =c (sy 1 2 3 ~)
318 > =d (sy 2 3 4 ~)
319 > (~(dif in c) d)
320 [n=1 l=~ r=~]
322 > (~(dif in d) c)
323 [n=4 l=~ r=~]
325 > `(set @ud)`(~(dif in c) d)
326 {1}
328 > `(set @ud)`(~(dif in d) c)
329 {4}
330```
332---
333### `+-dig:in`
335Address b in a
337Produce the tree address of `b` within `a`.
339#### Accepts
341`a` is a set.
343`b` is a noun.
345#### Produces
347The `unit` of an atom.
349#### Source
351```hoon
352 +- dig
353 |= b=*
354 =+ c=1
355 |- ^- (unit @)
356 ?~ a ~
357 ?: =(b n.a) [~ u=(peg c 2)]
358 ?: (hor b n.a)
359 $(a l.a, c (peg c 6))
360 $(a r.a, c (peg c 7))
361```
364#### Examples
366```
367 > =a (sy `(list @)`[1 2 3 4 5 6 7 ~])
368 > `(set @)`a
369 {5 4 7 6 1 3 2}
371 > -.a
372 n=6
374 > (~(dig in a) 7)
375 [~ u=12]
377 > (~(dig in a) 2)
378 [~ u=14]
380 > (~(dig in a) 6)
381 [~ u=2]
382```
384#### Discussion
386For more on the tree addressing system, see section [1b](@/docs/hoon/reference/stdlib/1b.md).
388---
389### `+-gas:in`
391Concatenate
393Insert the elements of a `list` `b` into a `set` `a`.
395#### Accepts
397`a` is a set.
399`b` is a list.
401#### Produces
403A `set`.
405#### Source
407```hoon
408 +- gas
409 ~/ %gas
410 |= b=(list _?>(?=(^ a) n.a))
411 |- ^+ a
412 ?~ b
413 a
414 $(b t.b, a (put i.b))
415```
417#### Examples
419```
420 > =b `(set @t)`(sy `(list @t)`['bonita' 'madeleine' 'rudolf' 'john' ~])
421 > b
422 {'bonita' 'madeleine' 'rudolf' 'john'}
424 > (~(gas in b) `(list @t)`['14' 'things' 'number' '1.337' ~])
425 [n='number' l={'1.337' '14'} r={'things' 'bonita' 'madeleine' 'rudolf' 'john'}]
427 > =s (sy `(list @t)`['a' 'b' 'c' 'd' 'e' ~])
428 > (~(gas in s) `(list @t)`['1' '2' '3' ~])
429 [n='e' l=[n='1' l={} r={'3' '2'}] r=[n='b' l={'d' 'a' 'c'} r={}]]
430```
432---
433### `+-has:in`
435b in a?
437Checks if `b` is an element of `a`, producing a flag.
439#### Accepts
441`a` is a set.
443`b` is a noun.
445#### Produces
447A flag.
449#### Source
451```hoon
452 +- has
453 ~/ %has
454 |* b=*
455 |- ^- ?
456 ?~ a
457 |
458 ?: =(b n.a)
459 &
460 ?: (hor b n.a)
461 $(a l.a)
462 $(a r.a)
463```
465#### Examples
467```
468 > =a (~(gas in `(set @t)`~) `(list @t)`['a' 'b' 'c' ~])
469 > (~(has in a) 'a')
470 %.y
472 > (~(has in a) 'z')
473 %.n
474```
476---
477### `+-int:in`
479Intersection
481Produces a set of the intersection between two sets of the same type,
482`a` and `b`.
484#### Accepts
486`a` is a set.
488`b` is a set.
490#### Produces
492A `set`.
494#### Source
496```hoon
497 +- int
498 ~/ %int
499 |* b=_a
500 |- ^+ a
501 ?~ b
502 ~
503 ?~ a
504 ~
505 ?. (vor n.a n.b)
506 $(a b, b a)
507 ?: =(n.b n.a)
508 [n.a $(a l.a, b l.b) $(a r.a, b r.b)]
509 ?: (hor n.b n.a)
510 %- uni(a $(a l.a, b [n.b l.b ~])) $(b r.b)
511 %- uni(a $(a r.a, b [n.b ~ r.b])) $(b l.b)
512```
515#### Examples
517```
518 > (~(int in `(set @t)`(sy "acmo")) `(set @t)`(sy "ham"))
519 [n='m' l={'a'} r={}]
521 > (~(int in (sy "acmo")) ~)
522 ~
524 > (~(int in `(set @t)`(sy "ac")) `(set @t)`(sy "ha"))
525 [n='a' l={} r={}]
526```
528---
529### `+-put:in`
531Put b in a
533Add an element `b` to the set `a`, producing a set.
535#### Accepts
537`a` is a set.
539`b` is a noun.
541#### Produces
543A `set`.
545#### Source
547```hoon
548 +- put
549 ~/ %put
550 |* b=*
551 |- ^+ a
552 ?~ a
553 [b ~ ~]
554 ?: =(b n.a)
555 a
556 ?: (hor b n.a)
557 =+ c=$(a l.a)
558 ?> ?=(^ c)
559 ?: (vor n.a n.c)
560 [n.a c r.a]
561 [n.c l.c [n.a r.c r.a]]
562 =+ c=$(a r.a)
563 ?> ?=(^ c)
564 ?: (vor n.a n.c)
565 [n.a l.a c]
566 [n.c [n.a l.a l.c] r.c]
567```
569#### Examples
571```
572 > =a (~(gas in `(set @t)`~) `(list @t)`['a' 'b' 'c' ~])
573 > =b (~(put in a) 'd')
574 > `(set @t)`b
575 {'d' 'a' 'c' 'b'}
577 > -.l.+.b
578 n='d'
579```
581---
582### `+-rep:in`
584Accumulate
586Accumulate the elements of `a` using a gate `b`.
588#### Accepts
590`a` is a `set`.
592`b` is a gate.
594#### Produces
596A noun.
598#### Source
600```hoon
601 +- rep :: replace by product
602 |* b=_|=([* *] +<+)
603 |-
604 ?~ a +<+.b
605 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
606```
608#### Examples
610```
611 > =a (~(gas in *(set @)) [1 2 3 ~])
612 > `(set @)`a
613 {1 3 2}
615 > (~(rep in a) |=([a=@ b=@] (add a b)))
616 b=6
617```
619---
620### `+-run:in`
622Apply gate to set
624Produce a `set` containing the products of gate `b` applied to each element
625in `a`.
627#### Accepts
629`a` is a `set`.
631`b` is a gate.
633#### Produces
635A set.
637#### Source
639```hoon
640 +- run
641 ~/ %run
642 |* b=gate
643 =| c=(set _?>(?=(^ a) (b n.a)))
644 |- ?~ a c
645 =. c (~(put in c) (b n.a))
646 =. c $(a l.a, c c)
647 $(a r.a, c c)
648```
650#### Examples
652```
653 > =s (sy ~["a" "A" "b" "c"])
654 > `(set tape)`s
655 {"A" "a" "c" "b"}
657 > (~(run in s) cuss)
658 {"A" "C" "B"}
659```
661---
662### `+-tap:in`
664Set to list
666Flattens the `set` `a` into a `list`.
668#### Accepts
670`a` is an set.
672#### Produces
674A list.
676#### Source
678```hoon
679 +- tap
680 ~/ %tap
681 |= b=(list _?>(?=(^ a) n.a))
682 ^+ b
683 ?~ a
684 b
685 $(a r.a b [n.a $(a l.a)])
686```
689#### Examples
691```
692 > =s (sy `(list @t)`['a' 'b' 'c' 'd' 'e' ~])
693 > s
694 [n='e' l={} r={'d' 'a' 'c' 'b'}]
696 > ~(tap in s)
697 ~['b' 'c' 'a' 'd' 'e']
698```
700---
701### `+-uni:in`
703Union
705Produces a set of the union between two sets of the same type, `a` and
706`b`.
708#### Accepts
710`a` is a set.
712`b` is a set.
714#### Produces
716A set.
718#### Source
720```hoon
721 +- uni
722 ~/ %uni
723 |* b=_a
724 ?: =(a b) a
725 |- ^+ a
726 ?~ b
727 a
728 ?~ a
729 b
730 ?: (vor n.a n.b)
731 ?: =(n.b n.a)
732 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
733 ?: (hor n.b n.a)
734 $(a [n.a $(a l.a, b [n.b l.b ~]) r.a], b r.b)
735 $(a [n.a l.a $(a r.a, b [n.b ~ r.b])], b l.b)
736 ?: =(n.a n.b)
737 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
738 ?: (hor n.a n.b)
739 $(b [n.b $(b l.b, a [n.a l.a ~]) r.b], a r.a)
740 $(b [n.b l.b $(b r.b, a [n.a ~ r.a])], a l.a)
741 ::
742```
745#### Examples
747```
748 > (~(uni in `(set @t)`(sy "ac")) `(set @)`(sy "ha"))
749 [n='c' l={'a'} r={'h'}]
751 > (~(uni in `(set @t)`(sy "acmo")) ~)
752 [n='c' l={'a'} r={'m' 'o'}]
754 > (~(uni in `(set @t)`(sy "acmo")) `(set @t)`(sy "ham"))
755 [n='c' l={'a'} r={'m' 'o' 'h'}]
757 > (~(uni in `(set @t)`(sy "acmo")) `(set @t)`(sy "lep"))
758 [n='e' l={} r={'a' 'c' 'm' 'l' 'o' 'p'}]
759```
761---
762### `+-wyt:in`
764Set size
766Produces the number of elements in set `a` as an atom.
768#### Accepts
770`a` is an set.
772#### Produces
774An atom.
776#### Source
778```hoon
779 +- wyt :: size of set
780 |- ^- @
781 ?~(a 0 +((add $(a l.a) $(a r.a))))
782 --
783```
786#### Examples
788```
789 > =a (~(put in (~(put in (sy)) 'a')) 'b')
790 > ~(wyt in a)
791 2
793 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
794 > ~(wyt in b)
795 4
796```
798---