Modul Sequence

Jan Burse, erstellt 26. Jul 2019
/**
* This module is inspired by SQL query options such as TOP. Providing
* such a module was recently pioneered by SWI-Prolog. Currently predicates
* limit/2 and offset/2 are provided. The predicates solely work tuple
* oriented and it is possible to cascade these predicates:
*
* Example:
* ?- limit(5, offset(3, between(1, 10, X))).
* X = 4 ;
* X = 5 ;
* X = 6 ;
* X = 7 ;
* X = 8
*
* The current implementation is based on call_nth/2, which is also
* provided through this module. call_nth/2 is in turn implemented with
* pivots, an alternative to nb_setarg/3 which does not destruct a
* Prolog term, but instead a Java object.
*
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies GmbH makes no warranties
* regarding the provided information. XLOG Technologies GmbH assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies GmbH.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies GmbH. If the company was not the originator of some
* excerpts, XLOG Technologies GmbH 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 GmbH.
*/
:- package(library(jekpro/frequent/advanced)).
:- use_package(foreign(jekpro/frequent/advanced)).
:- use_package(foreign(matula/util/data)).
:- use_package(foreign(jekpro/tools/call)).
:- module(sequence, []).
/**
* limit(C, G):
* The predicate succeeds whenever the goal G succeeds but limits the
* number of solutions to C.
*/
% limit(+Integer, +Goal)
:- public limit/2.
:- meta_predicate limit(?, 0).
limit(C, G) :-
C > 0,
call_nth2(G, N),
(N < C -> true; !).
/**
* offset(C, G):
* The predicate succeeds whenever the goal G succeeds except for the
* first C solutions which are suppressed.
*/
% offset(+Integer, +Goal)
:- public offset/2.
:- meta_predicate offset(?, 0).
offset(C, G) :-
call_nth2(G, N),
N > C.
/**
* call_nth(G, C):
* The predicate succeeds whenever G succeeds and unifies C with
* the numbering of the successes.
*/
% call_nth(+Goal, -Integer)
:- public call_nth/2.
:- meta_predicate call_nth(0, ?).
call_nth(G, C) :- var(C), !,
call_nth2(G, N),
C = N.
call_nth(G, C) :-
C > 0,
call_nth2(G, N),
(C =:= N -> !; fail).
:- private call_nth2/2.
:- meta_predicate call_nth2(0, ?).
pivot_set(P, 0),
call(G),
pivot_get(P, M),
N is M+1,
pivot_set(P, N).
/*************************************************************/
/* Pivot Datatype */
/*************************************************************/
/**
* pivot_new(P):
* The predicate succeeds in P with a new pivot.
*/
% pivot_new(-Pivot)
:- foreign_constructor(pivot_new/1, 'VariantKey', new).
/**
* pivot_set(P, O):
* The predicate succeeds setting the pivot P to O.
*/
% pivot_set(+Pivot, +Term)
:- foreign(pivot_set/2, 'ForeignSequence',
sysPivotSet('Interpreter', 'SetEntry', 'Object')).
/**
* pivot_get(P, O):
* The predicate succeeds in O with a copy of the pivot P.
*/
% pivot_get(+Pivot, -Term)
:- foreign(pivot_get/2, 'ForeignSequence', sysPivotGet('SetEntry')).

Kommentare