From 2566ebcee520d377e6c30ab7e11fd3a0ad13f8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa=20II?= Date: Tue, 20 Apr 2010 23:49:14 +0100 Subject: [PATCH] make predsort a system predicate. --- docs/yap.tex | 10 ++++++++ library/dialect/swi.yap | 56 ----------------------------------------- pl/sort.yap | 43 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 56 deletions(-) diff --git a/docs/yap.tex b/docs/yap.tex index a2c43eaf6..42346f5ff 100644 --- a/docs/yap.tex +++ b/docs/yap.tex @@ -3743,6 +3743,16 @@ would return: S = [1-b,1-a,1-b,2-c,3-a] @end example +@item predsort(+@var{Pred}, +@var{List}, -@var{Sorted}) +@findex predsort/3 +@snindex predsort/3 +@cnindex predsort/3 +Sorts similar to sort/2, but determines the order of two terms by +calling @var{Pred}(-@var{Delta}, +@var{E1}, +@var{E2}) . This call must +unify @var{Delta} with one of @code{<}, @code{>} or @code{=}. If +built-in predicate compare/3 is used, the result is the same as +sort/2. + @item length(?@var{L},?@var{S}) @findex length/2 @syindex length/2 diff --git a/library/dialect/swi.yap b/library/dialect/swi.yap index dad6fdafd..7dc069c18 100755 --- a/library/dialect/swi.yap +++ b/library/dialect/swi.yap @@ -137,62 +137,6 @@ user:file_search_path(foreign, swi(ArchLib)) :- atom_concat('lib/', Arch, ArchLib). user:file_search_path(foreign, swi(lib)). -:- meta_predicate prolog:predsort(:,+,-). - -prolog:plus(X, Y, Z) :- - integer(X), - integer(Y), !, - Z is X + Y. -prolog:plus(X, Y, Z) :- - integer(X), - integer(Z), !, - Y is Z - X. -prolog:plus(X, Y, Z) :- - integer(Y), - integer(Z), !, - X is Z - Y. - -%% predsort(:Compare, +List, -Sorted) is det. -% -% Sorts similar to sort/2, but determines the order of two terms -% by calling Compare(-Delta, +E1, +E2). This call must unify -% Delta with one of <, > or =. If built-in predicate compare/3 is -% used, the result is the same as sort/2. See also keysort/2. - -prolog:predsort(P, L, R) :- - length(L, N), - predsort(P, N, L, _, R1), !, - R = R1. - -predsort(P, 2, [X1, X2|L], L, R) :- !, - call(P, Delta, X1, X2), - sort2(Delta, X1, X2, R). -predsort(_, 1, [X|L], L, [X]) :- !. -predsort(_, 0, L, L, []) :- !. -predsort(P, N, L1, L3, R) :- - N1 is N // 2, - plus(N1, N2, N), - predsort(P, N1, L1, L2, R1), - predsort(P, N2, L2, L3, R2), - predmerge(P, R1, R2, R). - -sort2(<, X1, X2, [X1, X2]). -sort2(=, X1, _, [X1]). -sort2(>, X1, X2, [X2, X1]). - -predmerge(_, [], R, R) :- !. -predmerge(_, R, [], R) :- !. -predmerge(P, [H1|T1], [H2|T2], Result) :- - call(P, Delta, H1, H2), - predmerge(Delta, P, H1, H2, T1, T2, Result). - -predmerge(>, P, H1, H2, T1, T2, [H2|R]) :- - predmerge(P, [H1|T1], T2, R). -predmerge(=, P, H1, _, T1, T2, [H1|R]) :- - predmerge(P, T1, T2, R). -predmerge(<, P, H1, H2, T1, T2, [H1|R]) :- - predmerge(P, T1, [H2|T2], R). - % % maybe a good idea to eventually support this in YAP. diff --git a/pl/sort.yap b/pl/sort.yap index 91eee683e..d6c2620eb 100644 --- a/pl/sort.yap +++ b/pl/sort.yap @@ -53,3 +53,46 @@ msort(L,O) :- keysort(L,O) :- '$keysort'(L,O). +:- meta_predicate prolog:predsort(:,+,-). + +%% predsort(:Compare, +List, -Sorted) is det. +% +% Sorts similar to sort/2, but determines the order of two terms +% by calling Compare(-Delta, +E1, +E2). This call must unify +% Delta with one of <, > or =. If built-in predicate compare/3 is +% used, the result is the same as sort/2. See also keysort/2. + +predsort(P, L, R) :- + length(L, N), + predsort(P, N, L, _, R1), !, + R = R1. + +predsort(P, 2, [X1, X2|L], L, R) :- !, + call(P, Delta, X1, X2), + sort2(Delta, X1, X2, R). +predsort(_, 1, [X|L], L, [X]) :- !. +predsort(_, 0, L, L, []) :- !. +predsort(P, N, L1, L3, R) :- + N1 is N // 2, + plus(N1, N2, N), + predsort(P, N1, L1, L2, R1), + predsort(P, N2, L2, L3, R2), + predmerge(P, R1, R2, R). + +sort2(<, X1, X2, [X1, X2]). +sort2(=, X1, _, [X1]). +sort2(>, X1, X2, [X2, X1]). + +predmerge(_, [], R, R) :- !. +predmerge(_, R, [], R) :- !. +predmerge(P, [H1|T1], [H2|T2], Result) :- + call(P, Delta, H1, H2), + predmerge(Delta, P, H1, H2, T1, T2, Result). + +predmerge(>, P, H1, H2, T1, T2, [H2|R]) :- + predmerge(P, [H1|T1], T2, R). +predmerge(=, P, H1, _, T1, T2, [H1|R]) :- + predmerge(P, T1, T2, R). +predmerge(<, P, H1, H2, T1, T2, [H1|R]) :- + predmerge(P, T1, [H2|T2], R). +