Skip to content

Commit 27ce1cd

Browse files
authored
Merge pull request #785 from vsbogd/fix-unique
Fix unique
2 parents 021a037 + 23f3a6c commit 27ce1cd

File tree

4 files changed

+251
-214
lines changed

4 files changed

+251
-214
lines changed

lib/src/metta/runner/stdlib.metta

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,47 @@
6767
(: empty (-> %Undefined%))
6868
(= (empty) (let a b never-happens))
6969

70+
(@doc unique
71+
(@desc "Function takes non-deterministic input (first argument) and returns only unique entities. E.g. (unique (superpose (a b c d d))) -> [a, b, c, d]")
72+
(@params (
73+
(@param "Non-deterministic set of values")))
74+
(@return "Unique values from input set"))
75+
(: unique (-> Atom Atom))
76+
(= (unique $arg) (let $c (collapse $arg) (let $u (unique-atom $c) (superpose $u))))
77+
78+
(@doc union
79+
(@desc "Function takes two non-deterministic inputs (first and second argument) and returns their union. E.g. (union (superpose (a b b c)) (superpose (b c c d))) -> [a, b, b, c, b, c, c, d]")
80+
(@params (
81+
(@param "Non-deterministic set of values")
82+
(@param "Another non-deterministic set of values")))
83+
(@return "Union of sets"))
84+
(: union (-> Atom Atom Atom))
85+
(= (union $arg1 $arg2)
86+
(let $c1 (collapse $arg1) (let $c2 (collapse $arg2)
87+
(let $u (union-atom $c1 $c2) (superpose $u)))))
88+
89+
(@doc intersection
90+
(@desc "Function takes two non-deterministic inputs (first and second argument) and returns their intersection. E.g. (intersection (superpose (a b c c)) (superpose (b c c c d))) -> [b, c, c]")
91+
(@params (
92+
(@param "Non-deterministic set of values")
93+
(@param "Another non-deterministic set of values")))
94+
(@return "Intersection of sets"))
95+
(: intersection (-> Atom Atom Atom))
96+
(= (intersection $arg1 $arg2)
97+
(let $c1 (collapse $arg1) (let $c2 (collapse $arg2)
98+
(let $u (intersection-atom $c1 $c2) (superpose $u)))))
99+
100+
(@doc subtraction
101+
(@desc "Function takes two non-deterministic inputs (first and second argument) and returns their subtraction. E.g. !(subtraction (superpose (a b b c)) (superpose (b c c d))) -> [a, b]")
102+
(@params (
103+
(@param "Non-deterministic set of values")
104+
(@param "Another non-deterministic set of values")))
105+
(@return "Subtraction of sets"))
106+
(: subtraction (-> Atom Atom Atom))
107+
(= (subtraction $arg1 $arg2)
108+
(let $c1 (collapse $arg1) (let $c2 (collapse $arg2)
109+
(let $u (subtraction-atom $c1 $c2) (superpose $u)))))
110+
70111
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
71112
; Documentation formatting functions
72113
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -657,35 +698,35 @@
657698
(@params ())
658699
(@return "Random boolean value"))
659700

660-
(@doc unique
661-
(@desc "Function takes non-deterministic input (first argument) and returns only unique entities. E.g. (unique (superpose (a b c d d))) -> [a, b, c, d]")
701+
(@doc unique-atom
702+
(@desc "Function takes tuple and returns only unique entities. E.g. (unique-atom (a b c d d)) -> (a b c d)")
662703
(@params (
663-
(@param "Non-deterministic set of values")))
704+
(@param "List of values")))
664705
(@return "Unique values from input set"))
665706

666-
(@doc union
667-
(@desc "Function takes two non-deterministic inputs (first and second argument) and returns their union. E.g. (union (superpose (a b b c)) (superpose (b c c d))) -> [a, b, b, c, b, c, c, d]")
707+
(@doc union-atom
708+
(@desc "Function takes two tuples and returns their union. E.g. (union-atom (a b b c) (b c c d)) -> (a b b c b c c d)")
668709
(@params (
669-
(@param "Non-deterministic set of values")
670-
(@param "Another non-deterministic set of values")))
710+
(@param "List of values")
711+
(@param "List of values")))
671712
(@return "Union of sets"))
672713

673-
(@doc intersection
674-
(@desc "Function takes two non-deterministic inputs (first and second argument) and returns their intersection. E.g. (intersection (superpose (a b c c)) (superpose (b c c c d))) -> [b, c, c]")
714+
(@doc intersection-atom
715+
(@desc "Function takes two tuples and returns their intersection. E.g. (intersection-atom (a b c c) (b c c c d)) -> (b c c)")
675716
(@params (
676-
(@param "Non-deterministic set of values")
677-
(@param "Another non-deterministic set of values")))
717+
(@param "List of values")
718+
(@param "List of values")))
678719
(@return "Intersection of sets"))
679720

680-
(@doc subtraction
681-
(@desc "Function takes two non-deterministic inputs (first and second argument) and returns their subtraction. E.g. !(subtraction (superpose (a b b c)) (superpose (b c c d))) -> [a, b]")
721+
(@doc subtraction-atom
722+
(@desc "Function takes two tuples and returns their subtraction. E.g. !(subtraction-atom (a b b c) (b c c d)) -> (a b)")
682723
(@params (
683-
(@param "Non-deterministic set of values")
684-
(@param "Another non-deterministic set of values")))
724+
(@param "List of values")
725+
(@param "List of values")))
685726
(@return "Subtraction of sets"))
686727

687728
(@doc git-module!
688729
(@desc "Provides access to module in a remote git repo, from within MeTTa code. Similar to `register-module!`, this op will bypass the catalog search")
689730
(@params (
690731
(@param "URL to github repo")))
691-
(@return "Unit atom"))
732+
(@return "Unit atom"))

0 commit comments

Comments
 (0)