on b93c19b help sign in
797 lines 12 kB history raw
1+++
2title = "2h: Set Logic"
3weight = 11
4template = "doc.html"
5+++
6### `++in`
8Set operations
10Core whose arms contain a variety of functions that operate on `set`s. Its
11sample accepts the input `set` to be manipulated.
13#### Accepts
15A `set`.
17#### Source
19```
20 ++ in
21 ~/ %in
22 |_ a=(set)
23```
25#### Examples
27```
28 > ~(. in (sy "asd"))
29 <13.evb [nlr(^$1{@tD $1}) <414.fvk 101.jzo 1.ypj %164>]>
30```
32### `+-all:in`
34Logical AND
36Computes the logical AND on every element in `a` slammed with `b`, producing a
37flag.
39#### Accepts
41`a` is a `set`.
43`b` is a wet gate that accepts a noun and produces a flag.
45#### Produces
47A flag.
49#### Source
51```
52 +- all
53 ~/ %all
54 |* b=$-(* ?)
55 |- ^- ?
56 ?~ a
57 &
58 ?&((b n.a) $(a l.a) $(a r.a))
59```
61#### Examples
63```
64 > =b (sy `(list [@t *])`[['a' 1] ['b' [2 3]] ~])
65 > (~(all in b) |=(a=* ?@(+.a & |)))
66 %.n
68 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
69 > (~(all in b) |=(a=@t (gte a 100)))
70 %.y
71```
73---
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`.
85`b` is a gate that accepts a noun and produces a flag.
87#### Produces
89A flag.
91#### Source
93```
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 > =b (sy `(list [@t *])`[['a' 1] ['b' [2 3]] ~])
107 > (~(any in b) |=(a/* ?@(+.a & |)))
108 %.y
110 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
111 > (~(any in b) |=(a=@t (lte a 100)))
112 %.n
113```
115---
116### `+-apt:in`
118Check correctness
120Computes whether `a` has a correct horizontal order and a correct vertical
121order, producing a flag.
123#### Accepts
125`a` is a set.
127#### Produces
129A flag.
131#### Source
133```
134 +- apt
135 =| [l=(unit) r=(unit)]
136 |- ^- ?
137 ?~ a &
138 ?& ?~(l & (hor n.a u.l))
139 ?~(r & (hor u.r n.a))
140 ?~(l.a & ?&((vor n.a n.l.a) $(a l.a, l `n.a)))
141 ?~(r.a & ?&((vor n.a n.r.a) $(a r.a, r `n.a)))
142 ==
143```
145#### Examples
147```
148 > ~(apt in ~)
149 %.y
151 > =a (sy [1 2 3 4 5 6 ~])
152 > a
153 [n=6 l={5 4} r={1 3 2}]
154 > ~(apt in a)
155 %.y
157 > =z ?~ a ~ a(n 7)
158 > z
159 [n=7 l={5 4} r={1 3 2}]
160 > ~(apt in z)
161 %.n
162```
164#### Discussion
166See section [`2f`](@/docs/reference/library/2f.md) for more information on noun ordering.
168---
169### `+-bif:in`
171Bifurcate
173Splits set `a` into two sets `l` and `r`, with any noun `b` removed.
175#### Accepts
177`a` is a set.
179`b` is a noun.
181#### Produces
183A cell of two sets.
185#### Source
187```
188 +- bif
189 ~/ %bif
190 |* b=*
191 ^+ [l=a r=a]
192 =< [+< +>]
193 |- ^+ a
194 ?~ a
195 [b ~ ~]
196 ?: =(b n.a)
197 a
198 ?: (hor b n.a)
199 =+ c=$(a l.a)
200 ?> ?=(^ c)
201 [n.c l.c [n.a r.c r.a]]
202 =+ c=$(a r.a)
203 ?> ?=(^ c)
204 [n.c [n.a l.a l.c] r.c]
205```
207#### Examples
209```
210 > =a (sy `(list @)`[1 2 3 4 5 6 ~])
211 > a
212 [n=6 l={5 4} r={1 3 2}]
214 > (~(bif in a) 6)
215 [l=[n=4 l=[n=5 l={} r={}] r=~] r=[n=2 l=[n=1 l={} r={3}] r=~]]
217 > (~(bif in a) 100)
218 [l=[n=6 l=[n=4 l={5} r={}] r=[n=2 l={1 3} r={}]] r=~]
219```
221---
222### `+-del:in`
224Remove noun
226Removes `b` from the `set` `a`.
228#### Accepts
230`a` is a set.
232`b` is a noun.
234#### Produces
236A set.
238#### Source
240```
241 +- del
242 ~/ %del
243 |* b=*
244 |- ^+ a
245 ?~ a
246 ~
247 ?. =(b n.a)
248 ?: (hor b n.a)
249 [n.a $(a l.a) r.a]
250 [n.a l.a $(a r.a)]
251 |- ^- {$?($~ _a)}
252 ?~ l.a r.a
253 ?~ r.a l.a
254 ?: (vor n.l.a n.r.a)
255 [n.l.a l.l.a $(l.a r.l.a)]
256 [n.r.a $(r.a l.r.a) r.r.a]
257```
260#### Examples
262```
263 > =b (sy `(list @t)`['a' 'b' 'c' ~])
264 > (~(del in b) 'a')
265 [n='b' l=[n='c' l={} r={}] r=~]
267 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
268 > (~(del in b) 'john')
269 [n='madeleine' l=[n='daniel' l={} r={'bonita'}] r=~]
271 > (~(del in b) 'susan')
272 [n='madeleine' l=[n='daniel' l={} r={'bonita'}] r=[n='john' l={} r={}]]
273```
275---
276### `+-dif:in`
278Difference
280Computes the difference between `a` and `b`, producing the set of items in `a`
281that are not in `b`.
283#### Accepts
285`a` is a set.
287`b` is a set.
289#### Produces
291A set.
293#### Source
295```
296 +- dif
297 ~/ %dif
298 |* b=_a
299 |- ^+ a
300 ?~ b
301 a
302 =+ c=(bif n.b)
303 ?> ?=(^ c)
304 =+ d=$(a l.c, b l.b)
305 =+ e=$(a r.c, b r.b)
306 |- ^- {$?($~ _a)}
307 ?~ e d
308 ?: (vor n.d n.e)
309 [n.d l.d $(d r.d)]
310 [n.e $(e l.e) r.e]
311```
313#### Examples
315```
316 > =c (sy 1 2 3 ~)
317 > =d (sy 2 3 4 ~)
318 > (~(dif in c) d)
319 [n=1 l=~ r=~]
321 > (~(dif in d) c)
322 [n=4 l=~ r=~]
324 > `(set @ud)`(~(dif in c) d)
325 {1}
327 > `(set @ud)`(~(dif in d) c)
328 {4}
329```
331---
332### `+-dig:in`
334Address b in a
336Produce the tree address of `b` within `a`.
338#### Accepts
340`a` is a set.
342`b` is a noun.
344#### Produces
346The `unit` of an atom.
348#### Source
350```
351 +- dig
352 |= b=*
353 =+ c=1
354 |- ^- (unit @)
355 ?~ a ~
356 ?: =(b n.a) [~ u=(peg c 2)]
357 ?: (hor b n.a)
358 $(a l.a, c (peg c 6))
359 $(a r.a, c (peg c 7))
360```
363#### Examples
365```
366 > =a (sy `(list @)`[1 2 3 4 5 6 7 ~])
367 > `(set @)`a
368 {5 4 7 6 1 3 2}
370 > -.a
371 n=6
373 > (~(dig in a) 7)
374 [~ u=12]
376 > (~(dig in a) 2)
377 [~ u=14]
379 > (~(dig in a) 6)
380 [~ u=2]
381```
383#### Discussion
385For more on the tree addressing system, see section [1b](@/docs/reference/library/1b.md).
387---
388### `+-gas:in`
390Concatenate
392Insert the elements of a `list` `b` into a `set` `a`.
394#### Accepts
396`a` is a set.
398`b` is a list.
400#### Produces
402A `set`.
404#### Source
406```
407 +- gas
408 ~/ %gas
409 |= b=(list _?>(?=(^ a) n.a))
410 |- ^+ a
411 ?~ b
412 a
413 $(b t.b, a (put i.b))
414```
416#### Examples
418```
419 > =b `(set @t)`(sy `(list @t)`['bonita' 'madeleine' 'rudolf' 'john' ~])
420 > b
421 {'bonita' 'madeleine' 'rudolf' 'john'}
423 > (~(gas in b) `(list @t)`['14' 'things' 'number' '1.337' ~])
424 [n='number' l={'1.337' '14'} r={'things' 'bonita' 'madeleine' 'rudolf' 'john'}]
426 > =s (sy `(list @t)`['a' 'b' 'c' 'd' 'e' ~])
427 > (~(gas in s) `(list @t)`['1' '2' '3' ~])
428 [n='e' l=[n='1' l={} r={'3' '2'}] r=[n='b' l={'d' 'a' 'c'} r={}]]
429```
431---
432### `+-has:in`
434b in a?
436Checks if `b` is an element of `a`, producing a flag.
438#### Accepts
440`a` is a set.
442`b` is a noun.
444#### Produces
446A flag.
448#### Source
450```
451 +- has
452 ~/ %has
453 |* b=*
454 |- ^- ?
455 ?~ a
456 |
457 ?: =(b n.a)
458 &
459 ?: (hor b n.a)
460 $(a l.a)
461 $(a r.a)
462```
464#### Examples
466```
467 > =a (~(gas in `(set @t)`~) `(list @t)`['a' 'b' 'c' ~])
468 > (~(has in a) 'a')
469 %.y
471 > (~(has in a) 'z')
472 %.n
473```
475---
476### `+-int:in`
478Intersection
480Produces a set of the intersection between two sets of the same type,
481`a` and `b`.
483#### Accepts
485`a` is a set.
487`b` is a set.
489#### Produces
491A `set`.
493#### Source
495```
496 +- int
497 ~/ %int
498 |* b=_a
499 |- ^+ a
500 ?~ b
501 ~
502 ?~ a
503 ~
504 ?. (vor n.a n.b)
505 $(a b, b a)
506 ?: =(n.b n.a)
507 [n.a $(a l.a, b l.b) $(a r.a, b r.b)]
508 ?: (hor n.b n.a)
509 %- uni(a $(a l.a, b [n.b l.b ~])) $(b r.b)
510 %- uni(a $(a r.a, b [n.b ~ r.b])) $(b l.b)
511```
514#### Examples
516```
517 > (~(int in `(set @t)`(sy "acmo")) `(set @t)`(sy "ham"))
518 [n='m' l={'a'} r={}]
520 > (~(int in (sy "acmo")) ~)
521 ~
523 > (~(int in `(set @t)`(sy "ac")) `(set @t)`(sy "ha"))
524 [n='a' l={} r={}]
525```
527---
528### `+-put:in`
530Put b in a
532Add an element `b` to the set `a`, producing a set.
534#### Accepts
536`a` is a set.
538`b` is a noun.
540#### Produces
542A `set`.
544#### Source
546```
547 +- put
548 ~/ %put
549 |* b=*
550 |- ^+ a
551 ?~ a
552 [b ~ ~]
553 ?: =(b n.a)
554 a
555 ?: (hor b n.a)
556 =+ c=$(a l.a)
557 ?> ?=(^ c)
558 ?: (vor n.a n.c)
559 [n.a c r.a]
560 [n.c l.c [n.a r.c r.a]]
561 =+ c=$(a r.a)
562 ?> ?=(^ c)
563 ?: (vor n.a n.c)
564 [n.a l.a c]
565 [n.c [n.a l.a l.c] r.c]
566```
568#### Examples
570```
571 > =a (~(gas in `(set @t)`~) `(list @t)`['a' 'b' 'c' ~])
572 > =b (~(put in a) 'd')
573 > `(set @t)`b
574 {'d' 'a' 'c' 'b'}
576 > -.l.+.b
577 n='d'
578```
580---
581### `+-rep:in`
583Accumulate
585Accumulate the elements of `a` using a gate `b`.
587#### Accepts
589`a` is a `set`.
591`b` is a gate.
593#### Produces
595A noun.
597#### Source
599```
600 +- rep :: replace by product
601 |* b=_|=([* *] +<+)
602 |-
603 ?~ a +<+.b
604 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
605```
607#### Examples
609```
610 > =a (~(gas in *(set @)) [1 2 3 ~])
611 > `(set @)`a
612 {1 3 2}
614 > (~(rep in a) |=([a=@ b=@] (add a b)))
615 b=6
616```
618---
619### `+-run:in`
621Apply gate to set
623Produce a `set` containing the products of gate `b` applied to each element
624in `a`.
626#### Accepts
628`a` is a `set`.
630`b` is a gate.
632#### Produces
634A set.
636#### Source
638```
639 +- run
640 ~/ %run
641 |* b=gate
642 =| c=(set _?>(?=(^ a) (b n.a)))
643 |- ?~ a c
644 =. c (~(put in c) (b n.a))
645 =. c $(a l.a, c c)
646 $(a r.a, c c)
647```
649#### Examples
651```
652 > =s (sy ~["a" "A" "b" "c"])
653 > `(set tape)`s
654 {"A" "a" "c" "b"}
656 > (~(run in s) cuss)
657 {"A" "C" "B"}
658```
660---
661### `+-tap:in`
663Set to list
665Flattens the `set` `a` into a `list`.
667#### Accepts
669`a` is an set.
671#### Produces
673A list.
675#### Source
677```
678 +- tap
679 ~/ %tap
680 |= b=(list _?>(?=(^ a) n.a))
681 ^+ b
682 ?~ a
683 b
684 $(a r.a b [n.a $(a l.a)])
685```
688#### Examples
690```
691 > =s (sy `(list @t)`['a' 'b' 'c' 'd' 'e' ~])
692 > s
693 [n='e' l={} r={'d' 'a' 'c' 'b'}]
695 > ~(tap in s)
696 ~['b' 'c' 'a' 'd' 'e']
697```
699---
700### `+-uni:in`
702Union
704Produces a set of the union between two sets of the same type, `a` and
705`b`.
707#### Accepts
709`a` is a set.
711`b` is a set.
713#### Produces
715A set.
717#### Source
719```
720 +- uni
721 ~/ %uni
722 |* b=_a
723 ?: =(a b) a
724 |- ^+ a
725 ?~ b
726 a
727 ?~ a
728 b
729 ?: (vor n.a n.b)
730 ?: =(n.b n.a)
731 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
732 ?: (hor n.b n.a)
733 $(a [n.a $(a l.a, b [n.b l.b ~]) r.a], b r.b)
734 $(a [n.a l.a $(a r.a, b [n.b ~ r.b])], b l.b)
735 ?: =(n.a n.b)
736 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
737 ?: (hor n.a n.b)
738 $(b [n.b $(b l.b, a [n.a l.a ~]) r.b], a r.a)
739 $(b [n.b l.b $(b r.b, a [n.a ~ r.a])], a l.a)
740 ::
741```
744#### Examples
746```
747 > (~(uni in `(set @t)`(sy "ac")) `(set @)`(sy "ha"))
748 [n='c' l={'a'} r={'h'}]
750 > (~(uni in `(set @t)`(sy "acmo")) ~)
751 [n='c' l={'a'} r={'m' 'o'}]
753 > (~(uni in `(set @t)`(sy "acmo")) `(set @t)`(sy "ham"))
754 [n='c' l={'a'} r={'m' 'o' 'h'}]
756 > (~(uni in `(set @t)`(sy "acmo")) `(set @t)`(sy "lep"))
757 [n='e' l={} r={'a' 'c' 'm' 'l' 'o' 'p'}]
758```
760---
761### `+-wyt:in`
763Set size
765Produces the number of elements in set `a` as an atom.
767#### Accepts
769`a` is an set.
771#### Produces
773An atom.
775#### Source
777```
778 +- wyt :: size of set
779 |- ^- @
780 ?~(a 0 +((add $(a l.a) $(a r.a))))
781 --
782```
785#### Examples
787```
788 > =a (~(put in (~(put in (sy)) 'a')) 'b')
789 > ~(wyt in a)
790 2
792 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
793 > ~(wyt in b)
794 4
795```
797---