From 0bfb6988d8768b7d01453cc8886e0f57e9037e53 Mon Sep 17 00:00:00 2001 From: logicmoo Date: Thu, 29 Aug 2024 04:03:57 -0700 Subject: [PATCH] variant_by_type --- src/canary/metta_eval.pl | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/canary/metta_eval.pl b/src/canary/metta_eval.pl index e73be13f868..da5789a84d4 100755 --- a/src/canary/metta_eval.pl +++ b/src/canary/metta_eval.pl @@ -1689,9 +1689,10 @@ fromNumber(N,RetVal), check_returnval(Eq,RetType,RetVal). */ -%% lazy_union(+E1_Call1, +E2_Call2, -E) is nondet. +%% lazy_union(:P2, +E1_Call1, +E2_Call2, -E) is nondet. % - Performs a union operation using lazy evaluation % Arguments: +% - P2: Any arity 2 predicate % - E1^Call1: The first goal (Call1) generating elements (E1) % - E2^Call2: The second goal (Call2) generating elements (E2) % - E: The resulting element that is part of the union of the two sets @@ -1709,19 +1710,21 @@ ). +variant_by_type(X,Y):- var(X),!,X==Y. +variant_by_type(X,Y):- X=@=Y. + eval_20(Eq,RetType,Depth,Self,['unique',Eval],RetVal):- !, term_variables(Eval+RetVal,Vars), no_repeats_var(YY), eval_20(Eq,RetType,Depth,Self,Eval,RetVal),YY=Vars. eval_20(Eq,RetType,Depth,Self,['pred-unique',P2,Eval],RetVal):- !, - term_variables(Eval+RetVal,Vars), no_repeats_var(P2,YY), - eval_20(Eq,RetType,Depth,Self,Eval,RetVal),YY=Vars. + eval_20(Eq,RetType,Depth,Self,Eval,RetVal),YY=RetVal. eval_20(Eq,RetType,Depth,Self,['subtraction',Eval1,Eval2],RetVal):- !, - lazy_subtraction(=@=,RetVal1^eval_args(Eq,RetType,Depth,Self,Eval1,RetVal1), + lazy_subtraction(variant_by_type,RetVal1^eval_args(Eq,RetType,Depth,Self,Eval1,RetVal1), RetVal2^eval_args(Eq,RetType,Depth,Self,Eval2,RetVal2), RetVal). @@ -1731,7 +1734,7 @@ RetVal). eval_20(Eq,RetType,Depth,Self,['union',Eval1,Eval2],RetVal):- !, - lazy_union(=@=,RetVal1^eval_args(Eq,RetType,Depth,Self,Eval1,RetVal1), + lazy_union(variant_by_type,RetVal1^eval_args(Eq,RetType,Depth,Self,Eval1,RetVal1), RetVal2^eval_args(Eq,RetType,Depth,Self,Eval2,RetVal2), RetVal). @@ -1744,7 +1747,7 @@ % !, Atom=[_|CDR],!,do_expander(Eq,RetType,Atom_list, CDR_Y ). eval_20(Eq,RetType,Depth,Self,['intersection',Eval1,Eval2],RetVal):- !, - lazy_intersection(=@=,RetVal1^eval_args(Eq,RetType,Depth,Self,Eval1,RetVal1), + lazy_intersection(variant_by_type,RetVal1^eval_args(Eq,RetType,Depth,Self,Eval1,RetVal1), RetVal2^eval_args(Eq,RetType,Depth,Self,Eval2,RetVal2), RetVal). @@ -1753,20 +1756,29 @@ RetVal2^eval_args(Eq,RetType,Depth,Self,Eval2,RetVal2), RetVal). +%% lazy_intersection(:P2, +E1_Call1, +E2_Call2, -E) is nondet. +% - Performs a intersection operation using lazy evaluation. +% - It intersects elements generated by Call2 from those generated by Call1. +% Arguments: +% - P2: Any arity 2 predicate +% - E1^Call1: The first goal (Call1) generating elements (E1). +% - E2^Call2: The second goal (Call2) generating elements (E2). +% - E: The resulting element after subtracting elements of the second set from the first set. lazy_intersection(P2, E1^Call1, E2^Call2, E1) :- % Step 1: Evaluate Call1 to generate E1 call(Call1), % Step 2: Use lazy_findall/3 to declare that all elements satisfying Call2 are supposedly in List2 lazy_findall(E2, Call2, List2), - % Step 3: Perform the subtraction logic + % Step 3: Perform the intersection logic % Only return E1 if it is not a member of List2 member(E2, List2), call(P2,E1,E2). -%% lazy_subtraction(+E1_Call1, +E2_Call2, -E) is nondet. +%% lazy_subtraction(:P2, +E1_Call1, +E2_Call2, -E) is nondet. % - Performs a subtraction operation using lazy evaluation. % - It subtracts elements generated by Call2 from those generated by Call1. % Arguments: +% - P2: Any arity 2 predicate % - E1^Call1: The first goal (Call1) generating elements (E1). % - E2^Call2: The second goal (Call2) generating elements (E2). % - E: The resulting element after subtracting elements of the second set from the first set.