From c8e42b8540590fc310797e091237ed63a3aa9ad2 Mon Sep 17 00:00:00 2001 From: Mike Archbold Date: Mon, 19 Aug 2024 16:13:16 -0700 Subject: [PATCH 1/4] add xor processing --- src/canary/metta_eval.pl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/canary/metta_eval.pl b/src/canary/metta_eval.pl index 8b616c83b56..7012b3584c2 100755 --- a/src/canary/metta_eval.pl +++ b/src/canary/metta_eval.pl @@ -1335,6 +1335,8 @@ is_comma(','). is_comma('{}'). +bool_xor(A,B) :- (A == 'True'; B == 'True'), \+ (A == B). + eval_20(Eq,RetType,Depth,Self,['and',X,Y],TF):- !, as_tf(( (eval_args_true(Eq,RetType,Depth,Self,X), eval_args_true(Eq,RetType,Depth,Self,Y))), TF). @@ -1344,6 +1346,12 @@ as_tf(( (eval_args_true(Eq,RetType,Depth,Self,X); eval_args_true(Eq,RetType,Depth,Self,Y))), TF). +eval_20(Eq,RetType,Depth,Self,['xor',X,Y],TF):- !, + as_tf( (eval_args_true(Eq,RetType,Depth,Self,X)), XTF), % evaluate X + as_tf( (eval_args_true(Eq,RetType,Depth,Self,Y)), YTF), % evaluate Y + as_tf( (bool_xor(XTF,YTF)) , TF). + + eval_20(Eq,RetType,Depth,Self,['not',X],TF):- !, as_tf(( \+ eval_args_true(Eq,RetType,Depth,Self,X)), TF). From af29a017688c18ca0d51988cac730f47f8f7f010 Mon Sep 17 00:00:00 2001 From: Mike Archbold Date: Mon, 19 Aug 2024 16:49:22 -0700 Subject: [PATCH 2/4] add xor processing --- src/canary/metta_corelib.pl | 6 ++++++ src/canary/metta_ontology.pfc.pl | 1 + 2 files changed, 7 insertions(+) diff --git a/src/canary/metta_corelib.pl b/src/canary/metta_corelib.pl index 165596727c7..8b2ffa0f8db 100755 --- a/src/canary/metta_corelib.pl +++ b/src/canary/metta_corelib.pl @@ -77,6 +77,10 @@ metta_atom_corelib_defn( [=, [or, 'False', 'True'], 'True']). metta_atom_corelib_defn( [=, [or, 'True', 'False'], 'True']). metta_atom_corelib_defn( [=, [or, 'True', 'True'], 'True']). +metta_atom_corelib_defn( [=, [xor, 'False', 'False'], 'False']). +metta_atom_corelib_defn( [=, [xor, 'False', 'True'], 'True']). +metta_atom_corelib_defn( [=, [xor, 'True', 'False'], 'True']). +metta_atom_corelib_defn( [=, [xor, 'True', 'True'], 'False']). metta_atom_corelib_defn( [=, [quote, _], 'NotReducible']). metta_atom_corelib_defn( [=, [reduce, A, B, C], [chain, [eval, A], D, [eval, ['if-error', D, D, [eval, ['if-empty', D, [eval, [subst, A, B, C]], [eval, [reduce, D, B, C]]]]]]]]). metta_atom_corelib_defn( [=, [subst, A, B, C], [match, A, B, C, ['Error', [subst, A, B, C], "subst expects a variable as a second argument"]]]). @@ -129,6 +133,7 @@ metta_atom_corelib_types( [:, let, [->, 'Atom', '%Undefined%', 'Atom', 'Atom']]). metta_atom_corelib_types( [:, match, [->, 'Atom', 'Atom', 'Atom', '%Undefined%']]). metta_atom_corelib_types( [:, or, [->, 'Bool', 'Bool', 'Bool']]). +metta_atom_corelib_types( [:, xor, [->, 'Bool', 'Bool', 'Bool']]). metta_atom_corelib_types( [:, quote, [->, 'Atom', 'Atom']]). metta_atom_corelib_types( [:, return, [->, 'Atom', 'ReturnType']]). metta_atom_corelib_types( [:, switch, [->, '%Undefined%', 'Expression', 'Atom']]). @@ -178,6 +183,7 @@ op_decl(and, [ 'Bool', 'Bool' ], 'Bool'). op_decl(or, [ 'Bool', 'Bool' ], 'Bool'). +op_decl(xor, [ 'Bool', 'Bool' ], 'Bool'). op_decl(case, [ 'Expression', 'Atom' ], 'Atom'). op_decl(apply, [ 'Atom', 'Variable', 'Atom' ], 'Atom'). diff --git a/src/canary/metta_ontology.pfc.pl b/src/canary/metta_ontology.pfc.pl index d34575d5984..a57d0e8f737 100755 --- a/src/canary/metta_ontology.pfc.pl +++ b/src/canary/metta_ontology.pfc.pl @@ -421,6 +421,7 @@ properties('&corelib','\\=', [logic, qhelp("Inequality test."), inequality]). properties('&corelib','==', [logic, qhelp("Equality test."), equality_test]). properties('&corelib','or', [logic, qhelp("Logical OR."), logical_or]). +properties('&corelib','xor', [logic, qhelp("Logical XOR."), logical_or]) properties('&corelib','and', [logic, qhelp("Logical AND."), logical_and]). properties('&corelib','not', [logic, qhelp("Logical NOT."), logical_not]). properties('&corelib','quote', [evaluation_control, qhelp("Prevents evaluation, treating input as literal.")]). From 9c0a92b5840ff089fe36d3000674cb1350249055 Mon Sep 17 00:00:00 2001 From: Mike Archbold Date: Mon, 19 Aug 2024 16:52:42 -0700 Subject: [PATCH 3/4] add xor processing --- src/canary/metta_ontology.pfc.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/canary/metta_ontology.pfc.pl b/src/canary/metta_ontology.pfc.pl index a57d0e8f737..a3a66927119 100755 --- a/src/canary/metta_ontology.pfc.pl +++ b/src/canary/metta_ontology.pfc.pl @@ -421,7 +421,7 @@ properties('&corelib','\\=', [logic, qhelp("Inequality test."), inequality]). properties('&corelib','==', [logic, qhelp("Equality test."), equality_test]). properties('&corelib','or', [logic, qhelp("Logical OR."), logical_or]). -properties('&corelib','xor', [logic, qhelp("Logical XOR."), logical_or]) +properties('&corelib','xor', [logic, qhelp("Logical XOR."), logical_xor]) properties('&corelib','and', [logic, qhelp("Logical AND."), logical_and]). properties('&corelib','not', [logic, qhelp("Logical NOT."), logical_not]). properties('&corelib','quote', [evaluation_control, qhelp("Prevents evaluation, treating input as literal.")]). From df024edbbe7b3aafe62d7836b36580ef09e97175 Mon Sep 17 00:00:00 2001 From: Mike Archbold Date: Mon, 19 Aug 2024 17:26:05 -0700 Subject: [PATCH 4/4] new xor test file --- .../hyperon-mettalog_sanity/xor_test.metta | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/baseline_compat/hyperon-mettalog_sanity/xor_test.metta diff --git a/tests/baseline_compat/hyperon-mettalog_sanity/xor_test.metta b/tests/baseline_compat/hyperon-mettalog_sanity/xor_test.metta new file mode 100644 index 00000000000..de1b1c5d540 --- /dev/null +++ b/tests/baseline_compat/hyperon-mettalog_sanity/xor_test.metta @@ -0,0 +1,19 @@ +;; 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)