@@ -883,12 +883,45 @@ set_difference([X|Xs], [Y|Ys], Zs) :-
|
||||
set_difference([], _, []) :- !.
|
||||
set_difference(Xs, [], Xs).
|
||||
|
||||
|
||||
% variant/2 checks whether X is a variant of Y per the definition in
|
||||
% 7.1.6.1 of the ISO standard.
|
||||
|
||||
:- non_counted_backtracking variant/4.
|
||||
|
||||
variant(X,Y,VPs,VPs0) :-
|
||||
( var(X) ->
|
||||
var(Y),
|
||||
VPs = [X-Y|VPs0]
|
||||
; var(Y) ->
|
||||
false
|
||||
; X =.. [FX | XArgs],
|
||||
Y =.. [FX | YArgs],
|
||||
lists:foldl('$call'(builtins:variant), XArgs, YArgs, VPs, VPs0)
|
||||
).
|
||||
|
||||
:- non_counted_backtracking variant/2.
|
||||
|
||||
singleton([_]).
|
||||
|
||||
variant(X, Y) :-
|
||||
variant(X,Y, VPs, []),
|
||||
keysort(VPs, SVPs),
|
||||
pairs:group_pairs_by_key(SVPs, SVPKs),
|
||||
pairs:pairs_values(SVPKs, Vals),
|
||||
lists:maplist('$call'(builtins:term_variables), Vals, Vs),
|
||||
lists:maplist('$call'(builtins:singleton), Vs),
|
||||
term_variables(Vs, YVars),
|
||||
lists:length(SVPKs, N),
|
||||
lists:length(YVars, N).
|
||||
|
||||
|
||||
:- non_counted_backtracking group_by_variant/4.
|
||||
|
||||
group_by_variant([V2-S2 | Pairs], V1-S1, [S2 | Solutions], Pairs0) :-
|
||||
V1 = V2, % \+ \+ (V1 = V2), % (2) % iso_ext:variant(V1, V2), % (1)
|
||||
variant(V1, V2),
|
||||
!,
|
||||
% V1 = V2, % (3)
|
||||
V1 = V2,
|
||||
group_by_variant(Pairs, V2-S2, Solutions, Pairs0).
|
||||
group_by_variant(Pairs, _, [], Pairs).
|
||||
|
||||
@@ -1008,7 +1041,7 @@ setof(Template, Goal, Solution) :-
|
||||
term_variables(TemplateVars+GoalVars, TGVs),
|
||||
lists:append(TemplateVars, Witnesses0, TGVs),
|
||||
findall_with_existential(Template, Goal, PairedSolutions0, Witnesses0, Witnesses),
|
||||
keysort(PairedSolutions0, PairedSolutions),
|
||||
'$keysort_with_constant_var_ordering'(PairedSolutions0, PairedSolutions), % see 7.2.1
|
||||
group_by_variants(PairedSolutions, GroupedSolutions),
|
||||
iterate_variants_and_sort(GroupedSolutions, Witnesses, Solution).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user