on c19bc7a help sign in
801 lines 12 kB history raw
1:- :~ navhome/'/docs/'
2 sort/'11'
3 title/'2h: Set Logic'
4 ==
6;>
8# 2h: set logic
10### `++in`
12Set operations
14Core whose arms contain a variety of functions that operate on `set`s. Its
15sample accepts the input `set` to be manipulated.
17#### Accepts
19A `set`.
21#### Source
23```
24 ++ in
25 ~/ %in
26 |_ a=(set)
27```
29#### Examples
31```
32 > ~(. in (sy "asd"))
33 <13.evb [nlr(^$1{@tD $1}) <414.fvk 101.jzo 1.ypj %164>]>
34```
36### `+-all:in`
38Logical AND
40Computes the logical AND on every element in `a` slammed with `b`, producing a
41flag.
43#### Accepts
45`a` is a `set`.
47`b` is a wet gate that accepts a noun and produces a flag.
49#### Produces
51A flag.
53#### Source
55```
56 +- all
57 ~/ %all
58 |* b=$-(* ?)
59 |- ^- ?
60 ?~ a
61 &
62 ?&((b n.a) $(a l.a) $(a r.a))
63```
65#### Examples
67```
68 > =b (sy `(list [@t *])`[['a' 1] ['b' [2 3]] ~])
69 > (~(all in b) |=(a=* ?@(+.a & |)))
70 %.n
72 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
73 > (~(all in b) |=(a=@t (gte a 100)))
74 %.y
75```
77---
78### `+-any:in`
80Logical OR
82Computes the logical OR on every element of `a` slammed with `b`, producing a
83flag.
85#### Accepts
87`a` is a `set`.
89`b` is a gate that accepts a noun and produces a flag.
91#### Produces
93A flag.
95#### Source
97```
98 +- any
99 ~/ %any
100 |* b=$-(* ?)
101 |- ^- ?
102 ?~ a
103 |
104 ?|((b n.a) $(a l.a) $(a r.a))
105```
107#### Examples
109```
110 > =b (sy `(list [@t *])`[['a' 1] ['b' [2 3]] ~])
111 > (~(any in b) |=(a/* ?@(+.a & |)))
112 %.y
114 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
115 > (~(any in b) |=(a=@t (lte a 100)))
116 %.n
117```
119---
120#### `+-apt:in`
122Check correctness
124Computes whether `a` has a correct horizontal order and a correct vertical
125order, producing a flag.
127#### Accepts
129`a` is a set.
131#### Produces
133A flag.
135#### Source
137```
138 +- apt
139 =| [l=(unit) r=(unit)]
140 |- ^- ?
141 ?~ a &
142 ?& ?~(l & (hor n.a u.l))
143 ?~(r & (hor u.r n.a))
144 ?~(l.a & ?&((vor n.a n.l.a) $(a l.a, l `n.a)))
145 ?~(r.a & ?&((vor n.a n.r.a) $(a r.a, r `n.a)))
146 ==
147```
149#### Examples
151```
152 > ~(apt in ~)
153 %.y
155 > =a (sy [1 2 3 4 5 6 ~])
156 > a
157 [n=6 l={5 4} r={1 3 2}]
158 > ~(apt in a)
159 %.y
161 > =z ?~ a ~ a(n 7)
162 > z
163 [n=7 l={5 4} r={1 3 2}]
164 > ~(apt in z)
165 %.n
166```
168#### Discussion
170See section [`2f`](../2f) for more information on noun ordering.
172---
173### `+-bif:in`
175Bifurcate
177Splits set `a` into two sets `l` and `r`, with any noun `b` removed.
179#### Accepts
181`a` is a set.
183`b` is a noun.
185#### Produces
187A cell of two sets.
189#### Source
191```
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 ?: (hor b n.a)
203 =+ c=$(a l.a)
204 ?> ?=(^ c)
205 [n.c l.c [n.a r.c r.a]]
206 =+ c=$(a r.a)
207 ?> ?=(^ c)
208 [n.c [n.a l.a l.c] r.c]
209```
211#### Examples
213```
214 > =a (sy `(list @)`[1 2 3 4 5 6 ~])
215 > a
216 [n=6 l={5 4} r={1 3 2}]
218 > (~(bif in a) 6)
219 [l=[n=4 l=[n=5 l={} r={}] r=~] r=[n=2 l=[n=1 l={} r={3}] r=~]]
221 > (~(bif in a) 100)
222 [l=[n=6 l=[n=4 l={5} r={}] r=[n=2 l={1 3} r={}]] r=~]
223```
225---
226### `+-del:in`
228Remove noun
230Removes `b` from the `set` `a`.
232#### Accepts
234`a` is a set.
236`b` is a noun.
238#### Produces
240A set.
242#### Source
244```
245 +- del
246 ~/ %del
247 |* b=*
248 |- ^+ a
249 ?~ a
250 ~
251 ?. =(b n.a)
252 ?: (hor b n.a)
253 [n.a $(a l.a) r.a]
254 [n.a l.a $(a r.a)]
255 |- ^- {$?($~ _a)}
256 ?~ l.a r.a
257 ?~ r.a l.a
258 ?: (vor n.l.a n.r.a)
259 [n.l.a l.l.a $(l.a r.l.a)]
260 [n.r.a $(r.a l.r.a) r.r.a]
261```
264#### Examples
266```
267 > =b (sy `(list @t)`['a' 'b' 'c' ~])
268 > (~(del in b) 'a')
269 [n='b' l=[n='c' l={} r={}] r=~]
271 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
272 > (~(del in b) 'john')
273 [n='madeleine' l=[n='daniel' l={} r={'bonita'}] r=~]
275 > (~(del in b) 'susan')
276 [n='madeleine' l=[n='daniel' l={} r={'bonita'}] r=[n='john' l={} r={}]]
277```
279---
280### `+-dif:in`
282Difference
284Computes the difference between `a` and `b`, producing the set of items in `a`
285that are not in `b`.
287#### Accepts
289`a` is a set.
291`b` is a set.
293#### Produces
295A set.
297#### Source
299```
300 +- dif
301 ~/ %dif
302 |* b=_a
303 |- ^+ a
304 ?~ b
305 a
306 =+ c=(bif n.b)
307 ?> ?=(^ c)
308 =+ d=$(a l.c, b l.b)
309 =+ e=$(a r.c, b r.b)
310 |- ^- {$?($~ _a)}
311 ?~ e d
312 ?: (vor n.d n.e)
313 [n.d l.d $(d r.d)]
314 [n.e $(e l.e) r.e]
315```
317#### Examples
319```
320 > =c (sy 1 2 3 ~)
321 > =d (sy 2 3 4 ~)
322 > (~(dif in c) d)
323 [n=1 l=~ r=~]
325 > (~(dif in d) c)
326 [n=4 l=~ r=~]
328 > `(set @ud)`(~(dif in c) d)
329 {1}
331 > `(set @ud)`(~(dif in d) c)
332 {4}
333```
335---
336### `+-dig:in`
338Address b in a
340Produce the tree address of `b` within `a`.
342#### Accepts
344`a` is a set.
346`b` is a noun.
348#### Produces
350The `unit` of an atom.
352#### Source
354```
355 +- dig
356 |= b=*
357 =+ c=1
358 |- ^- (unit @)
359 ?~ a ~
360 ?: =(b n.a) [~ u=(peg c 2)]
361 ?: (hor b n.a)
362 $(a l.a, c (peg c 6))
363 $(a r.a, c (peg c 7))
364```
367#### Examples
369```
370 > =a (sy `(list @)`[1 2 3 4 5 6 7 ~])
371 > `(set @)`a
372 {5 4 7 6 1 3 2}
374 > -.a
375 n=6
377 > (~(dig in a) 7)
378 [~ u=12]
380 > (~(dig in a) 2)
381 [~ u=14]
383 > (~(dig in a) 6)
384 [~ u=2]
385```
387#### Discussion
389For more on the tree addressing system, see section [1b](../1b).
391---
392### `+-gas:in`
394Concatenate
396Insert the elements of a `list` `b` into a `set` `a`.
398#### Accepts
400`a` is a set.
402`b` is a list.
404#### Produces
406A `set`.
408#### Source
410```
411 +- gas
412 ~/ %gas
413 |= b=(list _?>(?=(^ a) n.a))
414 |- ^+ a
415 ?~ b
416 a
417 $(b t.b, a (put i.b))
418```
420#### Examples
422```
423 > =b `(set @t)`(sy `(list @t)`['bonita' 'madeleine' 'rudolf' 'john' ~])
424 > b
425 {'bonita' 'madeleine' 'rudolf' 'john'}
427 > (~(gas in b) `(list @t)`['14' 'things' 'number' '1.337' ~])
428 [n='number' l={'1.337' '14'} r={'things' 'bonita' 'madeleine' 'rudolf' 'john'}]
430 > =s (sy `(list @t)`['a' 'b' 'c' 'd' 'e' ~])
431 > (~(gas in s) `(list @t)`['1' '2' '3' ~])
432 [n='e' l=[n='1' l={} r={'3' '2'}] r=[n='b' l={'d' 'a' 'c'} r={}]]
433```
435---
436### `+-has:in`
438b in a?
440Checks if `b` is an element of `a`, producing a flag.
442#### Accepts
444`a` is a set.
446`b` is a noun.
448#### Produces
450A flag.
452#### Source
454```
455 +- has
456 ~/ %has
457 |* b=*
458 |- ^- ?
459 ?~ a
460 |
461 ?: =(b n.a)
462 &
463 ?: (hor b n.a)
464 $(a l.a)
465 $(a r.a)
466```
468#### Examples
470```
471 > =a (~(gas in `(set @t)`~) `(list @t)`['a' 'b' 'c' ~])
472 > (~(has in a) 'a')
473 %.y
475 > (~(has in a) 'z')
476 %.n
477```
479---
480### `+-int:in`
482Intersection
484Produces a set of the intersection between two sets of the same type,
485`a` and `b`.
487#### Accepts
489`a` is a set.
491`b` is a set.
493#### Produces
495A `set`.
497#### Source
499```
500 +- int
501 ~/ %int
502 |* b=_a
503 |- ^+ a
504 ?~ b
505 ~
506 ?~ a
507 ~
508 ?. (vor n.a n.b)
509 $(a b, b a)
510 ?: =(n.b n.a)
511 [n.a $(a l.a, b l.b) $(a r.a, b r.b)]
512 ?: (hor n.b n.a)
513 %- uni(a $(a l.a, b [n.b l.b ~])) $(b r.b)
514 %- uni(a $(a r.a, b [n.b ~ r.b])) $(b l.b)
515```
518#### Examples
520```
521 > (~(int in `(set @t)`(sy "acmo")) `(set @t)`(sy "ham"))
522 [n='m' l={'a'} r={}]
524 > (~(int in (sy "acmo")) ~)
525 ~
527 > (~(int in `(set @t)`(sy "ac")) `(set @t)`(sy "ha"))
528 [n='a' l={} r={}]
529```
531---
532### `+-put:in`
534Put b in a
536Add an element `b` to the set `a`, producing a set.
538#### Accepts
540`a` is a set.
542`b` is a noun.
544#### Produces
546A `set`.
548#### Source
550```
551 +- put
552 ~/ %put
553 |* b=*
554 |- ^+ a
555 ?~ a
556 [b ~ ~]
557 ?: =(b n.a)
558 a
559 ?: (hor b n.a)
560 =+ c=$(a l.a)
561 ?> ?=(^ c)
562 ?: (vor n.a n.c)
563 [n.a c r.a]
564 [n.c l.c [n.a r.c r.a]]
565 =+ c=$(a r.a)
566 ?> ?=(^ c)
567 ?: (vor n.a n.c)
568 [n.a l.a c]
569 [n.c [n.a l.a l.c] r.c]
570```
572#### Examples
574```
575 > =a (~(gas in `(set @t)`~) `(list @t)`['a' 'b' 'c' ~])
576 > =b (~(put in a) 'd')
577 > `(set @t)`b
578 {'d' 'a' 'c' 'b'}
580 > -.l.+.b
581 n='d'
582```
584---
585### `+-rep:in`
587Accumulate
589Accumulate the elements of `a` using a gate `b`.
591#### Accepts
593`a` is a `set`.
595`b` is a gate.
597#### Produces
599A noun.
601#### Source
603```
604 +- rep :: replace by product
605 |* b=_|=([* *] +<+)
606 |-
607 ?~ a +<+.b
608 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
609```
611#### Examples
613```
614 > =a (~(gas in *(set @)) [1 2 3 ~])
615 > `(set @)`a
616 {1 3 2}
618 > (~(rep in a) |=([a=@ b=@] (add a b)))
619 b=6
620```
622---
623### `+-run:in`
625Apply gate to set
627Produce a `set` containing the products of gate `b` applied to each element
628in `a`.
630#### Accepts
632`a` is a `set`.
634`b` is a gate.
636#### Produces
638A set.
640#### Source
642```
643 +- run
644 ~/ %run
645 |* b=gate
646 =| c=(set _?>(?=(^ a) (b n.a)))
647 |- ?~ a c
648 =. c (~(put in c) (b n.a))
649 =. c $(a l.a, c c)
650 $(a r.a, c c)
651```
653#### Examples
655```
656 > =s (sy ~["a" "A" "b" "c"])
657 > `(set tape)`s
658 {"A" "a" "c" "b"}
660 > (~(run in s) cuss)
661 {"A" "C" "B"}
662```
664---
665### `+-tap:in`
667Set to list
669Flattens the `set` `a` into a `list`.
671#### Accepts
673`a` is an set.
675#### Produces
677A list.
679#### Source
681```
682 +- tap
683 ~/ %tap
684 |= b=(list _?>(?=(^ a) n.a))
685 ^+ b
686 ?~ a
687 b
688 $(a r.a b [n.a $(a l.a)])
689```
692#### Examples
694```
695 > =s (sy `(list @t)`['a' 'b' 'c' 'd' 'e' ~])
696 > s
697 [n='e' l={} r={'d' 'a' 'c' 'b'}]
699 > ~(tap in s)
700 ~['b' 'c' 'a' 'd' 'e']
701```
703---
704### `+-uni:in`
706Union
708Produces a set of the union between two sets of the same type, `a` and
709`b`.
711#### Accepts
713`a` is a set.
715`b` is a set.
717#### Produces
719A set.
721#### Source
723```
724 +- uni
725 ~/ %uni
726 |* b=_a
727 ?: =(a b) a
728 |- ^+ a
729 ?~ b
730 a
731 ?~ a
732 b
733 ?: (vor n.a n.b)
734 ?: =(n.b n.a)
735 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
736 ?: (hor n.b n.a)
737 $(a [n.a $(a l.a, b [n.b l.b ~]) r.a], b r.b)
738 $(a [n.a l.a $(a r.a, b [n.b ~ r.b])], b l.b)
739 ?: =(n.a n.b)
740 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
741 ?: (hor n.a n.b)
742 $(b [n.b $(b l.b, a [n.a l.a ~]) r.b], a r.a)
743 $(b [n.b l.b $(b r.b, a [n.a ~ r.a])], a l.a)
744 ::
745```
748#### Examples
750```
751 > (~(uni in `(set @t)`(sy "ac")) `(set @)`(sy "ha"))
752 [n='c' l={'a'} r={'h'}]
754 > (~(uni in `(set @t)`(sy "acmo")) ~)
755 [n='c' l={'a'} r={'m' 'o'}]
757 > (~(uni in `(set @t)`(sy "acmo")) `(set @t)`(sy "ham"))
758 [n='c' l={'a'} r={'m' 'o' 'h'}]
760 > (~(uni in `(set @t)`(sy "acmo")) `(set @t)`(sy "lep"))
761 [n='e' l={} r={'a' 'c' 'm' 'l' 'o' 'p'}]
762```
764---
765### `+-wyt:in`
767Set size
769Produces the number of elements in set `a` as an atom.
771#### Accepts
773`a` is an set.
775#### Produces
777An atom.
779#### Source
781```
782 +- wyt :: size of set
783 |- ^- @
784 ?~(a 0 +((add $(a l.a) $(a r.a))))
785 --
786```
789#### Examples
791```
792 > =a (~(put in (~(put in (sy)) 'a')) 'b')
793 > ~(wyt in a)
794 2
796 > =b (sy `(list @t)`['john' 'bonita' 'daniel' 'madeleine' ~])
797 > ~(wyt in b)
798 4
799```
801---