Skip to content

Commit

Permalink
internal op was the same things as MeTTa #53
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon committed Aug 17, 2024
1 parent bfbe880 commit 59d50e0
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/canary/metta_eval.pl
Original file line number Diff line number Diff line change
Expand Up @@ -823,9 +823,11 @@
%;; superpose-bind because `superpose` doesnt guarentee shared bindings
% @TODO need to keep bindings
eval_20(Eq,RetType,Depth,Self,['superpose-bind',List],Res):- !,
member(E,List),
re_member(Res,E,List),
eval_ret(Eq,RetType,Depth,Self,E,Res).

re_member(Res,E,List):- term_variables(Res+E+List,TV),copy_term(TV,Copy),
member(E,List),TV=Copy.

%[collapse,[1,2,3]]
eval_20(Eq,RetType,Depth,Self,['collapse',List],Res):-!,
Expand Down Expand Up @@ -1635,12 +1637,12 @@
fromNumber(N,RetVal), check_returnval(Eq,RetType,RetVal).
*/

eval_20(Eq,RetType,Depth,Self,['dedup!',Eval],RetVal):- !,

eval_20(Eq,RetType,Depth,Self,['unique',Eval],RetVal):- !,
term_variables(Eval+RetVal,Vars),
no_repeats_var(YY),!,
no_repeats_var(YY),
eval_20(Eq,RetType,Depth,Self,Eval,RetVal),YY=Vars.


eval_20(Eq,RetType,Depth,Self,PredDecl,Res):-
Do_more_defs = do_more_defs(true),
clause(eval_21(Eq,RetType,Depth,Self,PredDecl,Res),Body),
Expand Down
105 changes: 105 additions & 0 deletions tests/baseline_compat/hyperon-mettalog_sanity/unique_test.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@


(: list-to-set (-> Expression Expression))
(= (list-to-set $list)
(collapse (unique (superpose $list))))

;; Test unique with simple duplicates
;; This test verifies that `unique` removes all duplicates from (a b b c c c d), resulting in [a, b, c, d].
!(assertEqual
(unique (superpose (a b b c c c d)))
(superpose (a b c d))
)

;; Test unique with no duplicates
;; This test ensures that `unique` leaves a list unchanged if it already contains only unique elements.
!(assertEqual
(unique (superpose (a b c d)))
(superpose (a b c d))
)

;; Test unique with a single element
;; This checks that `unique` works correctly for a single-element list, leaving it unchanged as [a].
!(assertEqual
(unique (superpose (a)))
(superpose (a))
)

;; Test unique with an empty list
;; This confirms that `unique` handles an empty list without errors, correctly returning an empty result.
!(assertEqual
(unique (superpose ()))
(superpose ())
)

;; Test unique with all elements being the same
;; This verifies that when all elements are the same (a a a a), `unique` reduces it to a single element [a].
!(assertEqual
(unique (superpose (a a a a)))
(superpose (a))
)

;; Test unique with nested lists
;; This ensures that unique identifies identical nested lists and reduces them to a single instance.
!(assertEqual
(unique (superpose ((1 2) (1 2) (3 4))))
(superpose ((1 2) (3 4)))
)

;; Test unique with mixed types
;; This test checks that `unique` handles different data types correctly and removes duplicates while preserving the order.
!(assertEqual
(unique (superpose (1 "hello" 1 "world" "hello" 3.14 3.14)))
(superpose (1 "hello" "world" 3.14))
)

;; Test list-to-set with simple duplicates
;; This test ensures that list-to-set correctly collapses duplicates into a unique set.
!(assertEqual
(list-to-set (a b b c c c d))
(a b c d)
)

;; Test list-to-set with nested lists
;; This test checks that list-to-set properly collapses duplicates within nested lists.
!(assertEqual
(list-to-set ((1 2) (1 2) (3 4)))
((1 2) (3 4))
)

;; Test unique with variables
;; This test checks that unique handles variables correctly, ensuring that each unique instance is preserved.
!(assertEqual
(unique (superpose ($x $y $x $z)))
(superpose ($x $y $z))
)

;; Test unique with variables in nested lists
;; This test checks that unique correctly handles variables within nested lists, preserving each unique nested structure.
!(assertEqual
(unique (superpose ((:: $x $y) (:: $x $z) (:: $x $y) (:: $a $b))))
(superpose ((:: $x $y) (:: $x $z) (:: $a $b)))
)

;; Test unique with repeated variables in nested lists
;; This test ensures that unique treats identical nested lists with variables as duplicates, reducing them to a single instance.
!(assertEqual
(unique (superpose ((:: $x $y) (:: $x $y) (:: $x $z) (:: $x $y))))
(superpose ((:: $x $y) (:: $x $z)))
)

;; Test unique with mixed types and variables in nested lists
;; This test checks that unique handles a mix of nested structures with variables, symbols, and other data types correctly.
!(assertEqual
(unique (superpose ((:: 1 $x) (:: $x "hello") (:: 1 $x) (:: $y $z) (:: $x "hello"))))
(superpose ((:: 1 $x) (:: $x "hello") (:: $y $z)))
)

;; Test unique with complex nested structures including variables
;; This test verifies that unique handles more complex nested structures containing variables, preserving each unique structure.
!(assertEqual
(unique (superpose ((:: $x (:: a b)) (:: $x (:: a b)) (:: (:: 1 $x) (:: 2 $y)) (:: (:: 1 $x) (:: 2 $y)))))
(superpose ((:: $x (:: a b)) (:: (:: 1 $x) (:: 2 $y))))
)


0 comments on commit 59d50e0

Please sign in to comment.