/**
 * Modern Albufeira Prolog Interpreter
 *
 * 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.
 */

/**
 * Reversible encode for faster execution
 * - Replace $ALT by annonymous predicate
 * - Replace goal functors by cachable functors
 * - Replace expr functors by cachable functors
 */

/**
 * sys_encode_horn(C, D, F):
 * The predicate succeeds in D with the cacheable horn D.
 * The flag F decides the caching method.
 */
% sys_encode_horn(+TRHorn, -TRHorn, +Integer)
sys_encode_horn(X, X, 0) :- !.
sys_encode_horn(tr_goal(M,C), tr_goal(M,D), _) :-
   sys_encode_body(C, D, M).
sys_encode_horn(tr_clause(M,H,C), tr_clause(M,H,D), _) :-
   sys_encode_body(C, D, M-H).

/****************************************************************/
/* Meta Arguments                                               */
/****************************************************************/

/**
 * Meta arguments types controlling encoding and decoding:
 *    t : Term
 *    e : Expression
 *    c : Clause
 *    p : Prime
 */
% sys_meta_args(+Atom, +Integer, -List)
:- multifile(sys_meta_args/3).
sys_meta_args(is, 2, [t,e]).
sys_meta_args(=:=, 2, [e,e]).
sys_meta_args(=\=, 2, [e,e]).
sys_meta_args(<, 2, [e,e]).
sys_meta_args(=<, 2, [e,e]).
sys_meta_args(>, 2, [e,e]).
sys_meta_args(>=, 2, [e,e]).
sys_meta_args(\+, 1, [p]).
sys_meta_args(once, 1, [p]).
sys_meta_args(asserta, 1, [c]).
sys_meta_args(assertz, 1, [c]).
sys_meta_args(clause, 2, [p,t]).
sys_meta_args(retract, 1, [c]).
sys_meta_args(retractall, 1, [p]).

/****************************************************************/
/* Body Encode                                                  */
/****************************************************************/

/**
 * sys_encode_body(L, R, Z):
 * The predicate succeeds in R with the cacheable body L. The 
 * parameter Z is the list of the previous goals in reverse order.
 */
% sys_encode_body(+List, -List, +List)
sys_encode_body([], [], _).
sys_encode_body([X|L], [Y|R], Z) :-
   sys_encode_goal(X, Y, L-Z),
   sys_encode_body(L, R, [X|Z]).
   
/**
 * sys_encode_goal(G, H, V):
 * The predicate succeeds in H with the cacheable goal G.
 * The parameter V is the left and right variable context.
 */
% sys_encode_goal(+Goal, -Goal, +Pair)
sys_encode_goal(G, G, _) :- var(G), !.
sys_encode_goal('$ALT'(P), H, V) :- !,
   vars_intersection(P, V, U),
   G =.. [''|U],
   sys_encode_disj(P, G, Q),
   sys_encode_link(G, H, Q).
sys_encode_goal(G, H, V) :- functor(G, F, N), sys_meta_args(F, N, M), !,
   sys_encode_meta(M, G, H, V).
sys_encode_goal(G, H, _) :-
   ir_site_new(G, H).

% sys_encode_disj(+List, +Term, -List)
sys_encode_disj(['$SEQ'(M,X)|L], G, [C|R]) :-
   sys_encode_horn(tr_clause(M,G,X), Y, 1),
   sys_host_ossify(Y, C),
   sys_encode_disj(L, G, R).
sys_encode_disj([], _, []).

% sys_encode_link(+Callable, -Callable, +List)
sys_encode_link(C, D, Q) :- 
   C =.. [_|L],
   ir_link_new(Q, P),
   D =.. [P|L].

% sys_encode_meta(+List, +Callable, -Ast, +Pair)
sys_encode_meta(M, C, H, V) :-
   C =.. [F|L],
   sys_encode_params(M, L, R, V),
   G =.. [F|R],
   ir_site_new(G, H).

% sys_encode_params(+List, +List, -List, +Pair)
sys_encode_params([], [], [], _).
sys_encode_params([T|M], [X|L], [Y|R], V) :-
   sys_encode_param(T, X, Y, V),
   sys_encode_params(M, L, R, V).

% sys_encode_param(+Atom, +Term, -Ast, +Pair)
sys_encode_param(t, X, X, _).
sys_encode_param(e, X, Y, _) :- sys_encode_expr(X, Y).
sys_encode_param(c, X, Y, _) :- sys_encode_clause(X, Y).
sys_encode_param(p, X, Y, V) :- sys_encode_goal(X, Y, V).

/****************************************************************/
/* Individual Encoders                                          */
/****************************************************************/

/**
 * sys_encode_expr(T, S, I, O):
 * The predicate succeeds in S with the encode for the expression T.
 */
% sys_encode_expr(+Term, -Ast)
sys_encode_expr(C, H) :- compound(C), !,
   C =.. [F|L],
   sys_encode_args(L, R),
   G =.. [F|R],
   ir_site_new(G, H).
sys_encode_expr(T, S) :-
   ir_site_new(T, S).

% sys_encode_args(+List, -List)
sys_encode_args([], []).
sys_encode_args([X|L], [Y|R]) :-
   sys_encode_expr(X, Y),
   sys_encode_args(L, R).

% sys_encode_clause(+Clause, -Clause)
sys_encode_clause(T, (P :- B)) :- nonvar(T), T = (H :- B), !,
   ir_site_new(H, P).
sys_encode_clause(T, S) :-
   ir_site_new(T, S).

/****************************************************************/
/* Body Decode                                                  */
/****************************************************************/

/**
 * sys_decode_body(L, R):
 * The predicate succeeds in R with the cachefree body.
 */
% sys_decode_body(+List, -List)
sys_decode_body([], []).
sys_decode_body([X|L], [Y|R]) :-
   sys_decode_goal(X, Y),
   sys_decode_body(L, R).

% sys_decode_goal(+Term, -Term)
sys_decode_goal(G, G) :- var(G), !.
sys_decode_goal(X, Y) :- ir_is_site(X), !,
   ir_site_name(X, H),
   sys_decode_goal(H, Y).
sys_decode_goal(X, '$ALT'(P)) :- ir_is_link(X), !,
   findall(C, kb_clause_ref(X, 0, C), L),
   sys_decode_disj(L, X, P).
sys_decode_goal(G, H) :- functor(G, F, N), sys_meta_args(F, N, M), !,
   sys_decode_meta(M, G, H).
sys_decode_goal(G, G).

% sys_decode_disj(+List, +Callable, -List)
sys_decode_disj([], _, []).
sys_decode_disj([C|L], X, ['$SEQ'(U,W)|R]) :-
   kb_clause_data(C, X, U, V),
   sys_decode_body(V, W),
   sys_decode_disj(L, X, R).

% sys_decode_meta(+List, +Callable, -Callable)
sys_decode_meta(M, G, H) :-
   G =.. [F|L],
   sys_decode_params(M, L, R),
   H =.. [F|R].

% sys_decode_params(+List, +List, -List)
sys_decode_params([], [], []).
sys_decode_params([T|M], [X|L], [Y|R]) :-
   sys_decode_param(T, X, Y),
   sys_decode_params(M, L, R).

% sys_decode_param(+Atom, +Term, -Ast)
sys_decode_param(t, X, X).
sys_decode_param(e, X, Y) :- sys_decode_expr(X, Y).
sys_decode_param(c, X, Y) :- sys_decode_clause(X, Y).
sys_decode_param(p, X, Y) :- sys_decode_goal(X, Y).

/****************************************************************/
/* Individual Decoders                                          */
/****************************************************************/

% sys_decode_expr(+Expr, -Expr)
sys_decode_expr(C, H) :- compound(C), !,
   C =.. [F|L],
   sys_decode_args(L, R),
   G =.. [F|R],
   ir_site_name(G, H).
sys_decode_expr(T, S) :-
   ir_site_name(T, S).

% sys_decode_args(+List, -List)
sys_decode_args([], []).
sys_decode_args([X|L], [Y|R]) :-
   sys_decode_expr(X, Y),
   sys_decode_args(L, R).

% sys_decode_clause(+Clause, -Clause)
sys_decode_clause(T, (P :- B)) :- nonvar(T), T = (H :- B), !,
   ir_site_name(H, P).
sys_decode_clause(T, S) :-
   ir_site_name(T, S).
