trueagi-io / metta-wam

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

Implement the `subtraction` Function and Add Test Cases #60

Closed TeamSPoon closed 1 week ago

TeamSPoon commented 1 month ago

This issue involves implementing the subtraction function in MeTTa, which should take non-deterministic results from two lists and return the elements of the first list that are not present in the second. The goal is to handle lists of various types, including numbers, symbols, nested structures, and mixed types, while ensuring correct behavior across all scenarios.

Task Description

  1. Implement the subtraction Function:

    • The subtraction function should take two non-deterministic inputs, evaluate their difference, and return a non-deterministic output containing only the elements present in the first list but not in the second.
    • Example behavior:

      metta+> !(subtraction (superpose (1 2 3 4)) (superpose (3 4 5 6)))
      [1, 2]
      
      metta+> !(subtraction (superpose (foo bar baz)) (superpose (bar qux)))
      [foo, baz]
  2. Create the Test File:

    • A new file named subtraction_test.metta should be added to the tests/baseline_compat/hyperon-mettalog_sanity directory.
  3. Include the Following Test Cases:

    ;; Test basic subtraction functionality
    ;; This checks that the subtraction of (a b c c) from (b c c c d) is correctly evaluated as (a).
    !(assertEqual
      (subtraction (superpose (a b c c)) (superpose (b c c c d)))
      (superpose (a))
    )
    
    ;; Test subtraction with `unique` applied outside
    ;; Here, `unique` is applied after the subtraction, ensuring duplicates are removed, resulting in (a).
    !(assertEqual
      (unique (subtraction (superpose (a a b c)) (superpose (b c c d))))
      (superpose (a))
    )
    
    ;; Test subtraction with `unique` applied inside
    ;; In this test, `unique` is applied to each input set before performing the subtraction, also resulting in (a).
    !(assertEqual
      (subtraction (unique (superpose (a a b c))) (unique (superpose (b c c d))))
      (superpose (a))
    )
    
    ;; Test equivalence of applying `unique` inside and outside of subtraction
    ;; This ensures that applying `unique` either inside or outside of the subtraction yields the same result.
    !(assertEqual
      (unique (subtraction (superpose (a a b c)) (superpose (b c c d))))
      (subtraction (unique (superpose (a a b c))) (unique (superpose (b c c d))))
    )
  4. Run and Verify:

    • Once the test cases are in place, ensure they run correctly within the MeTTa test suite.

Expected Outcome

The subtraction function should correctly handle non-deterministic input, returning non-deterministic results that represent the difference between the provided lists. The function should work correctly with lists of varying types, including duplicates, symbols, nested lists, and mixed types. The tests should confirm that the function behaves as expected across these scenarios, ensuring consistency with MeTTa's non-deterministic evaluation principles.