Skip to content

Commit

Permalink
variant_by_type
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon committed Aug 29, 2024
1 parent 3a58804 commit 0bfb698
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/canary/metta_eval.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).

Expand All @@ -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).

Expand All @@ -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).

Expand All @@ -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.
Expand Down

0 comments on commit 0bfb698

Please sign in to comment.