1/*  detlog_rt.pl -- the Detlog runtime for code written by pl2detlog.pl
    2
    3    Converted files load it with  :- use_module(library(detlog_rt)).
    4    (or with a relative path when the module is not installed as a pack).
    5
    6    Detlog code has no cut, no ;, no ->, no \+, no findall/3 and no call/N.
    7    if_commit/3 is its one decision construct: it takes the first answer of a
    8    deterministic condition and runs Then, or runs Else when the condition has
    9    no answer.  When this library is loaded, if_commit/3 is compiled inline as
   10    (C -> T ; E), so it costs nothing at run time.
   11
   12    Answer lists.  A nondeterministic predicate p/N is written as
   13    p__all(A1, ..., AN, Answers, Tail): Answers-Tail is a difference list of
   14    the instances p(A1', ..., AN') that Prolog would give for the call, in
   15    Prolog's order.  The helpers below make and read such lists.
   16*/
   17
   18:- module(detlog_rt,
   19          [ if_commit/3,
   20            dl_all/3,          % +Goal, -Answers, ?Tail: answers of a builtin generator
   21            dl_once/1,         % +Goal: first answer of a builtin generator
   22            dl_match/4,        % +Facts, +Call, -Answers, ?Tail: facts as data
   23            dl_project/4,      % +Answers, +Pattern, +Template, -Values
   24            dl_aggregate/3,    % +Spec, +Values, -Result
   25            dl_member/2,       % ?X, +Answers: the boundary to backtracking Prolog
   26            dl_fused_first/2,  % +Generator, +Test: first answer, enumerated lazily
   27            dl_fused_all/5     % +Generator, +Test, +Answer, -Answers, ?Tail
   28          ]).   29
   30:- multifile user:goal_expansion/2.   31:- dynamic user:goal_expansion/2.   32user:goal_expansion(if_commit(C, T, E), (C -> T ; E)).
 if_commit(:Cond, :Then, :Else)
The Detlog decision: the first answer of Cond selects Then, no answer selects Else.
   37:- meta_predicate if_commit(0, 0, 0), dl_all(0, -, ?), dl_once(0),
   38                  dl_fused_first(0, 0), dl_fused_all(0, 0, ?, -, ?).   39
   40if_commit(C, T, E) :- ( call(C) -> call(T) ; call(E) ).
 dl_all(+Goal, -Answers, ?Tail)
Answers-Tail lists the instances of Goal, in Prolog's order. The common generators are enumerated directly; any other goal is collected.
   46dl_all(_:G, _, _) :- dl_infinite(G), !,
   47    throw(error(detlog_infinite_answers(G), context(dl_all/3, 'a list of all its answers would be infinite'))).
   48dl_all(M:G, As, T) :- dl_gen(G, As, T), !, M = M.
   49dl_all(M:G, As, T) :- findall(G, M:G, As, T).
   50
   51dl_infinite(between(_, H, X)) :- var(X), ( H == inf ; H == infinite ).
   52dl_infinite(repeat).
   53
   54dl_gen(member(X, L), As, T) :- nonvar(L), dl_member_all(L, X, L, As, T).
   55dl_gen(between(L, H, X), As, T) :- integer(L), integer(H), var(X), dl_between_all(L, H, L, As, T).
   56dl_gen(nth0(I, L, E), As, T) :- var(I), is_list(L), dl_nth_all(L, 0, nth0, L, E, As, T).
   57dl_gen(nth1(I, L, E), As, T) :- var(I), is_list(L), dl_nth_all(L, 1, nth1, L, E, As, T).
   58
   59dl_member_all([], _, _, T, T).
   60dl_member_all([Y|Ys], X, L, As, T) :-
   61    (   \+ X \= Y
   62    ->  As = [member(Y, L)|As1]
   63    ;   As = As1
   64    ),
   65    (   nonvar(Ys) -> dl_member_all(Ys, X, L, As1, T)
   66    ;   findall(member(X, L), member(X, Ys), As1, T)
   67    ).
   68
   69dl_between_all(I, H, L, As, T) :-
   70    (   I > H
   71    ->  As = T
   72    ;   As = [between(L, H, I)|As1],
   73        I1 is I + 1,
   74        dl_between_all(I1, H, L, As1, T)
   75    ).
   76
   77dl_nth_all([], _, _, _, _, T, T).
   78dl_nth_all([X|Xs], K, F, L, E, As, T) :-
   79    G =.. [F, K, L, X],
   80    (   \+ E \= X -> As = [G|As1] ; As = As1 ),
   81    K1 is K + 1,
   82    dl_nth_all(Xs, K1, F, L, E, As1, T).
 dl_once(+Goal)
The first answer of a builtin goal (once/1 for builtins).
   87dl_once(G) :- ( call(G) -> true ; fail ).
 dl_match(+Facts, +Call, -Answers, ?Tail)
Facts written as data: the facts that unify with Call, in order.
   92dl_match([], _, T, T).
   93dl_match([F|Fs], C, As, T) :-
   94    (   \+ C \= F
   95    ->  copy_term(F, F1), As = [F1|As1]
   96    ;   As = As1
   97    ),
   98    dl_match(Fs, C, As1, T).
 dl_project(+Answers, +Pattern, +Template, -Values)
findall/3 over an answer list: Template for each answer that unifies with Pattern.
  103dl_project([], _, _, []).
  104dl_project([A|As], P, T, Vs) :-
  105    copy_term(P-T, P1-T1),
  106    (   P1 = A -> Vs = [T1|Vs1] ; Vs = Vs1 ),
  107    dl_project(As, P, T, Vs1).
 dl_aggregate(+Spec, +Values, -Result)
aggregate_all/3 over the values of an answer list.
  112dl_aggregate(count, Vs, N) :- length(Vs, N).
  113dl_aggregate(sum, Vs, S) :- sum_list(Vs, S0), S is S0.
  114dl_aggregate(max, [V|Vs], M) :- foldl(dl_max, Vs, V, M0), M is M0.
  115dl_aggregate(min, [V|Vs], M) :- foldl(dl_min, Vs, V, M0), M is M0.
  116dl_aggregate(bag, Vs, Vs).
  117dl_aggregate(set, Vs, S) :- sort(Vs, S).
  118
  119dl_max(X, A, M) :- ( X > A -> M = X ; M = A ).
  120dl_min(X, A, M) :- ( X < A -> M = X ; M = A ).
 dl_member(?X, +Answers)
Where Detlog code meets ordinary Prolog (a meta-call, or a predicate kept as Prolog), the answers are given back one by one, as member/2 does.
  126dl_member(X, [Y|Ys]) :- dl_member_(Ys, X, Y).
  127dl_member_(_, X, X).
  128dl_member_([Y|Ys], X, _) :- dl_member_(Ys, X, Y).
MNN fusion: a builtin generator whose answers are filtered by deterministic Test is one loop. The generator's answers are made one at a time and never stored; the first that passes Test is kept (dl_fused_first/2), or each that passes adds Answer (dl_fused_all/5). No choicepoint is left when the call returns.
  137dl_fused_first(G, Test) :- ( call(G), call(Test) -> true ; fail ).
  138
  139dl_fused_all(G, Test, Ans, As, T) :- findall(Ans, ( call(G), call(Test) ), As, T)