Prolog "aggregate"
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
:- ensure_loaded(library(hiord)).
:- ensure_loaded(library(cyclic)).
:- ensure_loaded(library(util/hash)).
:- ensure_loaded(library(sets)).
% sys_meta_args(+Atom, +Integer, -List)
:- multifile(sys_meta_args/3).
% list processing
sys_meta_args(bagof, 3, [t,g,t]).
sys_meta_args(setof, 3, [t,g,t]).
sys_meta_args(aggregate_all, 3, [t,g,t]).
sys_meta_args(aggregate, 3, [t,g,t]).
/****************************************************************/
/* bagof/3 and setof/3 */
/****************************************************************/
/**
* bagof(T, Q, L): [ISO 8.10.2]
* The predicate determines all the solutions to the quantified
* goal Q, whereby collecting copies of the template T and the witness
* to the quantified goal Q. The predicate then repeatedly succeeds
* for the witness and the list of associated templates in L.
*/
% bagof(+Term, +Quant, -List)
bagof(Template, Quant, List) :-
aggregate(bag(Template), Quant, List).
/**
* setof(T, Q, L): [ISO 8.10.3]
* The predicate determines all the solutions to the quantified
* goal Q, whereby collecting copies of the template T and the witness
* to the quantified goal Q. The predicate then repeatedly succeeds
* for the witness and the set of associated templates in L.
*/
% setof(+Term, +Quant, -List)
setof(Template, Quant, List) :-
aggregate(set(Template), Quant, List).
/****************************************************************/
/* aggregate_all/3 and aggregate/3 */
/****************************************************************/
/**
* aggregate_all(A, G, R):
* The predicate aggregates A for the solutions of the
* goal G and unifies the result with R. Works like findall/3,
* without witness grouping. The following aggregates are supported.
* See documentation for the supported aggregates functions.
*/
% aggregate_all(+Aggregate, +Goal, -Term)
aggregate_all(Aggregate, Goal, Result) :-
sys_aggr_init(Aggregate, T),
(Goal,
sys_aggr_next(Aggregate, T),
fail; true),
sys_aggr_fini(Aggregate, T, Result).
/**
* aggregate(A, Q, R):
* The predicate aggregates A for the solutions of the quantified
* goal Q and unifies the result with R. Works like bagof/3, with
* witness grouping.
*/
% aggregate(+Aggregate, +Quant, -Term)
aggregate(Aggregate, Quant, Result) :-
free_variables(Aggregate^Quant, Key, Goal),
hash_new(Hash),
(Goal,
term_minimal(Key, MinKey),
numbervars(MinKey, -1, _),
sys_aggr_make(Hash, MinKey, Aggregate, T),
sys_aggr_next(Aggregate, T),
fail; true),
hash_pairs(Hash, R),
sys_keysorting(sys_acyclic_key, R, L),
member(V-J, L),
unnumbervars(V-J, -1, Key-T),
sys_aggr_fini(Aggregate, T, Result).
% sys_aggr_make(+Hash, +Term, +Aggregate, -Term)
sys_aggr_make(Hash, Key, _, T) :-
hash_current(Hash, Key, T), !.
sys_aggr_make(Hash, Key, A, T) :-
sys_aggr_init(A, T),
copy_term(Key, Key2),
hash_add(Hash, Key2, T).
/****************************************************************/
/* Procedural Aggregates */
/****************************************************************/
% sys_aggr_init(+Aggregate, -Term)
sys_aggr_init(A, _) :- var(A),
throw(error(instantiation_error,_)).
sys_aggr_init(count, C) :- !, S = 0, C = v(S).
sys_aggr_init(sum(_), C) :- !, S = 0, C = v(S).
sys_aggr_init(mul(_), C) :- !, S = 1, C = v(S).
sys_aggr_init(min(_), C) :- !, S = 0rNaN, C = v(S).
sys_aggr_init(max(_), C) :- !, S = 0rNaN, C = v(S).
sys_aggr_init(bag(_), X) :- !, sys_find_init(X).
sys_aggr_init(set(_), X) :- !, hash_new(X).
sys_aggr_init((F,G), Z) :- !,
sys_aggr_init(F, X), sys_aggr_init(G, Y), Z =.. [',',X,Y].
sys_aggr_init(A, _) :-
throw(error(domain_error(aggr_function,A),_)).
% sys_aggr_next(+Aggregate, +Term)
sys_aggr_next(count, Y) :- arg(1, Y, H), J is H+1, change_arg(1, Y, J).
sys_aggr_next(sum(X), Y) :- arg(1, Y, H), J is H+X, change_arg(1, Y, J).
sys_aggr_next(mul(X), Y) :- arg(1, Y, H), J is H*X, change_arg(1, Y, J).
sys_aggr_next(min(X), Y) :- arg(1, Y, H), J is min(H,X), change_arg(1, Y, J).
sys_aggr_next(max(X), Y) :- arg(1, Y, H), J is max(H,X), change_arg(1, Y, J).
sys_aggr_next(bag(X), Y) :- sys_find_next(X, Y).
sys_aggr_next(set(X), Y) :-
term_minimal(X, MinX), sys_aggr_dedup(MinX, Y).
sys_aggr_next((F,G), (X,Y)) :-
sys_aggr_next(F, X), sys_aggr_next(G, Y).
% sys_aggr_fini(+Aggregate, +Term, -Term)
sys_aggr_fini(count, X, A) :- arg(1, X, A).
sys_aggr_fini(sum(_), X, A) :- arg(1, X, A).
sys_aggr_fini(mul(_), X, A) :- arg(1, X, A).
sys_aggr_fini(min(_), X, A) :- arg(1, X, A).
sys_aggr_fini(max(_), X, A) :- arg(1, X, A).
sys_aggr_fini(bag(_), X, A) :- sys_find_fini(X, A).
sys_aggr_fini(set(_), X, A) :-
sys_aggr_keys(X, H), sys_msorting(sys_acyclic_key, H, A).
sys_aggr_fini((F,G), (X,Y), (C,D)) :-
sys_aggr_fini(F, X, C), sys_aggr_fini(G, Y, D).
/****************************************************************/
/* Hash Set */
/****************************************************************/
% sys_aggr_dedup(+Term, +Hash)
sys_aggr_dedup(Key, Hash) :-
hash_current(Hash, Key, _), !.
sys_aggr_dedup(Key, Hash) :-
copy_term(Key, Key2),
hash_add(Hash, Key2, true).
% sys_aggr_keys(+Hash, -List)
sys_aggr_keys(Hash, Keys) :-
hash_pairs(Hash, Pairs),
maplist(sys_apply_key(=), Pairs, Keys).
/****************************************************************/
/* Schwartzian Transform */
/****************************************************************/
% sys_msorting(+Closure, +List, -List)
sys_msorting(F, L, R) :-
maplist(sys_add_key(F), L, H),
keysort(H, J),
maplist(sys_remove_key, J, R).
% sys_keysorting(+Closure, +Map, -Map)
sys_keysorting(F, L, R) :-
maplist(sys_add_key(sys_apply_key(F)), L, H),
keysort(H, J),
maplist(sys_remove_key, J, R).
% sys_add_key(+Closure, +Pair, -Pair)
sys_add_key(F, V, K-V) :-
call(F, V, K).
% sys_remove_key(+Pair, -Term)
sys_remove_key(_-V, V).
% sys_apply_key(+Closure, +Pair, -Term)
sys_apply_key(F, K-_, K2) :-
call(F, K, K2).
/****************************************************************/
/* Acyclic Key */
/****************************************************************/
% sys_acyclic_key(+Term, -List)
sys_acyclic_key(X, R) :-
term_decompose(X, R),
R = [_|L],
foldl(sys_number_key, L, 0, _).
% sys_number_key(+Pair, +Integer, -Integer)
sys_number_key('__'(N)=_, N, M) :-
M is N+1.
/****************************************************************/
/* Error Texts */
/****************************************************************/
% strings(+Atom, +Atom, -Atom)
:- multifile(strings/3).
strings('domain_error.aggr_function', de, 'Aggregatfunktion erwartet, gefunden ~q.').
strings('domain_error.aggr_function', '', 'Aggregate function expected, found ~q.').