trueagi-io / metta-wam

A Hyperon MeTTa Interpreter/Transpilier that targets the Warren Abstract Machine
8 stars 11 forks source link

Implement 'xor' function in interpreter #55

Closed TeamSPoon closed 2 months ago

TeamSPoon commented 2 months ago

This issue involves implementing the xor (exclusive OR) function in MeTTa and creating test cases to ensure its correctness. The xor function should evaluate the given arguments and return True only if exactly one of the arguments is True. Additionally, test cases should be integrated into the tests/baseline_compat/hyperon-mettalog_sanity directory to verify different scenarios, including side effects to ensure both expressions are executed.

Include the Following Test Cases but make up a couple if you wish:

;; Basic XOR Logic
!(assertEqual (xor True False) True)
!(assertEqual (xor False True) True)
!(assertEqual (xor True True) False)
!(assertEqual (xor False False) False)

;; XOR with Expressions
!(assertEqual (xor (> 5 3) (< 2 1)) True)
!(assertEqual (xor (== 1 1) (== 2 2)) False)
!(assertEqual (xor (not True) (and True False)) False)

(: mprogn (-> Atom Atom Atom ))
(= (mprogn $code1 $code2) (let $_ (eval $code1) (eval $code2)))

;; XOR with Side Effects to Ensure Both Expressions Are Evaluated
!(assertEqual (xor (mprogn (println! "First") True) (mprogn (println! "Second") False)) True)
!(assertEqual (xor (mprogn (println! "First") True) (mprogn (println! "Second") True)) False)
!(assertEqual (xor (mprogn (println! "First") False) (mprogn (println! "Second") True)) True)
!(assertEqual (xor (mprogn (println! "First") False) (mprogn (println! "Second") False)) False)

The xor function should be implemented and tested comprehensively, covering simple, expression-based, and side-effect scenarios. The tests should be integrated into the existing test suite, confirming that the function behaves consistently across different cases and that both bodies are evaluated, even when side effects are involved.