/**
 * 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.
 */

% sys_meta_args(+Atom, +Integer, -List)
:- multifile(sys_meta_args/3).
sys_meta_args(clause, 3, [p,t,t]).
sys_meta_args(compile, 2, [c,t]).

/**
 * clause(H, B, C):
 * The predicate succeeds with the asserted clauses that unify H :- B,
 * and succeeds in C with their clause references.
 */
% clause(+Callable, -Goal, -Reference)
clause(H, B, C) :-
   kb_clause_ref(H, 2, C),
   kb_clause_data(C, H, O, L),
   sys_untrans_body(L, O, 0, A),
   A = B.

/**
 * erase(C):
 * The predicate succceeds if the clause referenced by C is asserted.
 * As a side effect the clause referenced by C is removed.
 */
% erase(+Reference)
erase(C) :-
   kb_clause_remove(C, 0).

/**
 * compile(D, C):
 * The predicate succeeds in C with a new clause reference for the clause D.
 */
% compile(+Clause, -Reference)
compile(T, C) :- nonvar(T), T = (H :- B), !,
   sys_trans_body(B, nothing, M, 0, R, []),
   H =.. [W|L],
   sys_clause_ossify(L, R, M, W, C).
compile(T, C) :-
   T =.. [W|L],
   sys_clause_ossify(L, [], nothing, W, C).

/**
 * compiled(C, H, B):
 * The predicate succeeds with the clause H :- B referenced by C.
 */
% compiled(+Reference, -Callable, -Goal)
compiled(C, H, B) :-
   kb_clause_data(C, H, O, L),
   sys_untrans_body(L, O, 0, A),
   A = B.

/**
 * recorda(C):
 * The predicate succeeds. As a side effect, the clause
 * referenced by C is inserted at the top.
 */
% recorda(+Reference)
recorda(C) :-
   sys_clause_add(C, 1).

/**
 * recordz(C):
 * The predicate succeeds. As a side effect, the clause
 * referenced by C is inserted at the bottom.
 */
% recordz(+Reference)
recordz(C) :-
   sys_clause_add(C, 3).
