View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Benoit Desouter <Benoit.Desouter@UGent.be>
    4                   Jan Wielemaker (SWI-Prolog port)
    5                   Fabrizio Riguzzi (mode directed tabling)
    6    Copyright (c) 2016-2021, Benoit Desouter,
    7                             Jan Wielemaker,
    8                             Fabrizio Riguzzi
    9                             SWI-Prolog Solutions b.v.
   10    All rights reserved.
   11
   12    Redistribution and use in source and binary forms, with or without
   13    modification, are permitted provided that the following conditions
   14    are met:
   15
   16    1. Redistributions of source code must retain the above copyright
   17       notice, this list of conditions and the following disclaimer.
   18
   19    2. Redistributions in binary form must reproduce the above copyright
   20       notice, this list of conditions and the following disclaimer in
   21       the documentation and/or other materials provided with the
   22       distribution.
   23
   24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   25    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   26    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   27    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   28    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   29    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   30    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   31    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   32    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   34    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   35    POSSIBILITY OF SUCH DAMAGE.
   36*/
   37
   38:- module('$tabling',
   39          [ (table)/1,                  % :PI ...
   40            untable/1,                  % :PI ...
   41
   42            (tnot)/1,                   % :Goal
   43            not_exists/1,               % :Goal
   44            undefined/0,
   45            answer_count_restraint/0,
   46            radial_restraint/0,
   47
   48            current_table/2,            % :Variant, ?Table
   49            abolish_all_tables/0,
   50            abolish_private_tables/0,
   51            abolish_shared_tables/0,
   52            abolish_table_subgoals/1,   % :Subgoal
   53            abolish_module_tables/1,    % +Module
   54            abolish_nonincremental_tables/0,
   55            abolish_nonincremental_tables/1, % +Options
   56            abolish_monotonic_tables/0,
   57
   58            start_tabling/3,            % +Closure, +Wrapper, :Worker
   59            start_subsumptive_tabling/3,% +Closure, +Wrapper, :Worker
   60            start_abstract_tabling/3,   % +Closure, +Wrapper, :Worker
   61            start_moded_tabling/5,      % +Closure, +Wrapper, :Worker,
   62                                        % :Variant, ?ModeArgs
   63
   64            '$tbl_answer'/4,            % +Trie, -Return, -ModeArgs, -Delay
   65
   66            '$wrap_tabled'/2,		% :Head, +Mode
   67            '$moded_wrap_tabled'/5,	% :Head, +Opts, +ModeTest, +Varnt, +Moded
   68            '$wfs_call'/2,              % :Goal, -Delays
   69
   70            '$set_table_wrappers'/1,    % :Head
   71            '$start_monotonic'/2        % :Head, :Wrapped
   72          ]).   73
   74:- meta_predicate
   75    table(:),
   76    untable(:),
   77    tnot(0),
   78    not_exists(0),
   79    tabled_call(0),
   80    start_tabling(+, +, 0),
   81    start_abstract_tabling(+, +, 0),
   82    start_moded_tabling(+, +, 0, +, ?),
   83    current_table(:, -),
   84    abolish_table_subgoals(:),
   85    '$wfs_call'(0, :).

Tabled execution (SLG WAM)

This library handled tabled execution of predicates using the characteristics if the SLG WAM. The required suspension is realised using delimited continuations implemented by reset/3 and shift/1. The table space and work lists are part of the SWI-Prolog core.

author
- Benoit Desouter, Jan Wielemaker and Fabrizio Riguzzi */
   97% Enable debugging using debug(tabling(Topic)) when compiled with
   98% -DO_DEBUG
   99goal_expansion(tdebug(Topic, Fmt, Args), Expansion) :-
  100    (   current_prolog_flag(prolog_debug, true)
  101    ->  Expansion = debug(tabling(Topic), Fmt, Args)
  102    ;   Expansion = true
  103    ).
  104goal_expansion(tdebug(Goal), Expansion) :-
  105    (   current_prolog_flag(prolog_debug, true)
  106    ->  Expansion = (   debugging(tabling(_))
  107                    ->  (   Goal
  108                        ->  true
  109                        ;   print_message(error,
  110                                          format('goal_failed: ~q', [Goal]))
  111                        )
  112                    ;   true
  113                    )
  114    ;   Expansion = true
  115    ).
  116
  117:- if(current_prolog_flag(prolog_debug, true)).  118wl_goal(tnot(WorkList), ~(Goal), Skeleton) :-
  119    !,
  120    '$tbl_wkl_table'(WorkList, ATrie),
  121    trie_goal(ATrie, Goal, Skeleton).
  122wl_goal(WorkList, Goal, Skeleton) :-
  123    '$tbl_wkl_table'(WorkList, ATrie),
  124    trie_goal(ATrie, Goal, Skeleton).
  125
  126trie_goal(ATrie, Goal, Skeleton) :-
  127    '$tbl_table_status'(ATrie, _Status, M:Variant, Skeleton),
  128    (   M:'$table_mode'(Goal0, Variant, _Moded)
  129    ->  true
  130    ;   Goal0 = Variant                 % dynamic IDG nodes
  131    ),
  132    unqualify_goal(M:Goal0, user, Goal).
  133
  134delay_goals(List, Goal) :-
  135    delay_goals(List, user, Goal).
  136
  137user_goal(Goal, UGoal) :-
  138    unqualify_goal(Goal, user, UGoal).
  139
  140:- multifile
  141    prolog:portray/1.  142
  143user:portray(ATrie) :-
  144    '$is_answer_trie'(ATrie, _),
  145    trie_goal(ATrie, Goal, _Skeleton),
  146    (   '$idg_falsecount'(ATrie, FalseCount)
  147    ->  (   '$idg_forced'(ATrie)
  148        ->  format('~q [fc=~d/F] for ~p', [ATrie, FalseCount, Goal])
  149        ;   format('~q [fc=~d] for ~p', [ATrie, FalseCount, Goal])
  150        )
  151    ;   format('~q for ~p', [ATrie, Goal])
  152    ).
  153user:portray(Cont) :-
  154    compound(Cont),
  155    compound_name_arguments(Cont, '$cont$', [_Context, Clause, PC | Args]),
  156    clause_property(Clause, file(File)),
  157    file_base_name(File, Base),
  158    clause_property(Clause, line_count(Line)),
  159    clause_property(Clause, predicate(PI)),
  160    format('~q at ~w:~d @PC=~w, ~p', [PI, Base, Line, PC, Args]).
  161
  162:- endif.
 table :PredicateIndicators
Prepare the given PredicateIndicators for tabling. This predicate is normally used as a directive, but SWI-Prolog also allows runtime conversion of non-tabled predicates to tabled predicates by calling table/1. The example below prepares the predicate edge/2 and the non-terminal statement//1 for tabled execution.
:- table edge/2, statement//1.

In addition to using predicate indicators, a predicate can be declared for mode directed tabling using a term where each argument declares the intended mode. For example:

:- table connection(_,_,min).

Mode directed tabling is discussed in the general introduction section about tabling.

  187table(M:PIList) :-
  188    setup_call_cleanup(
  189        '$set_source_module'(OldModule, M),
  190        expand_term((:- table(PIList)), Clauses),
  191        '$set_source_module'(OldModule)),
  192    dyn_tabling_list(Clauses, M).
  193
  194dyn_tabling_list([], _).
  195dyn_tabling_list([H|T], M) :-
  196    dyn_tabling(H, M),
  197    dyn_tabling_list(T, M).
  198
  199dyn_tabling(M:Clause, _) :-
  200    !,
  201    dyn_tabling(Clause, M).
  202dyn_tabling((:- multifile(PI)), M) :-
  203    !,
  204    multifile(M:PI),
  205    dynamic(M:PI).
  206dyn_tabling(:- initialization(Wrap, now), M) :-
  207    !,
  208    M:Wrap.
  209dyn_tabling('$tabled'(Head, TMode), M) :-
  210    (   clause(M:'$tabled'(Head, OMode), true, Ref),
  211        (   OMode \== TMode
  212        ->  erase(Ref),
  213            fail
  214        ;   true
  215        )
  216    ->  true
  217    ;   assertz(M:'$tabled'(Head, TMode))
  218    ).
  219dyn_tabling('$table_mode'(Head, Variant, Moded), M) :-
  220    (   clause(M:'$table_mode'(Head, Variant0, Moded0), true, Ref)
  221    ->  (   t(Head, Variant, Moded) =@= t(Head, Variant0, Moded0)
  222        ->  true
  223        ;   erase(Ref),
  224            assertz(M:'$table_mode'(Head, Variant, Moded))
  225        )
  226    ;   assertz(M:'$table_mode'(Head, Variant, Moded))
  227    ).
  228dyn_tabling(('$table_update'(Head, S0, S1, S2) :- Body), M) :-
  229    (   clause(M:'$table_update'(Head, S00, S10, S20), Body0, Ref)
  230    ->  (   t(Head, S0, S1, S2, Body) =@= t(Head, S00, S10, S20, Body0)
  231        ->  true
  232        ;   erase(Ref),
  233            assertz(M:('$table_update'(Head, S0, S1, S2) :- Body))
  234        )
  235    ;   assertz(M:('$table_update'(Head, S0, S1, S2) :- Body))
  236    ).
 untable(M:PIList) is det
Remove tabling for the predicates in PIList. This can be used to undo the effect of table/1 at runtime. In addition to removing the tabling instrumentation this also removes possibly associated tables using abolish_table_subgoals/1.
Arguments:
PIList- is a comma-list that is compatible ith table/1.
  247untable(M:PIList) :-
  248    untable(PIList, M).
  249
  250untable(Var, _) :-
  251    var(Var),
  252    !,
  253    '$instantiation_error'(Var).
  254untable(M:Spec, _) :-
  255    !,
  256    '$must_be'(atom, M),
  257    untable(Spec, M).
  258untable((A,B), M) :-
  259    !,
  260    untable(A, M),
  261    untable(B, M).
  262untable(Name//Arity, M) :-
  263    atom(Name), integer(Arity), Arity >= 0,
  264    !,
  265    Arity1 is Arity+2,
  266    untable(Name/Arity1, M).
  267untable(Name/Arity, M) :-
  268    !,
  269    functor(Head, Name, Arity),
  270    (   '$get_predicate_attribute'(M:Head, tabled, 1)
  271    ->  abolish_table_subgoals(M:Head),
  272        dynamic(M:'$tabled'/2),
  273        dynamic(M:'$table_mode'/3),
  274        retractall(M:'$tabled'(Head, _TMode)),
  275        retractall(M:'$table_mode'(Head, _Variant, _Moded)),
  276        unwrap_predicate(M:Name/Arity, table),
  277        '$set_predicate_attribute'(M:Head, tabled, false),
  278        '$set_predicate_attribute'(M:Head, opaque, false),
  279        '$set_predicate_attribute'(M:Head, incremental, false),
  280        '$set_predicate_attribute'(M:Head, monotonic, false),
  281        '$set_predicate_attribute'(M:Head, lazy, false)
  282    ;   true
  283    ).
  284untable(Head, M) :-
  285    callable(Head),
  286    !,
  287    functor(Head, Name, Arity),
  288    untable(Name/Arity, M).
  289untable(TableSpec, _) :-
  290    '$type_error'(table_desclaration, TableSpec).
  291
  292untable_reconsult(PI) :-
  293    print_message(informational, untable(PI)),
  294    untable(PI).
  295
  296:- initialization
  297   prolog_listen(untable, untable_reconsult).  298
  299
  300'$wrap_tabled'(Head, Options) :-
  301    get_dict(mode, Options, subsumptive),
  302    !,
  303    set_pattributes(Head, Options),
  304    '$wrap_predicate'(Head, table, Closure, Wrapped,
  305                      start_subsumptive_tabling(Closure, Head, Wrapped)).
  306'$wrap_tabled'(Head, Options) :-
  307    get_dict(subgoal_abstract, Options, _Abstract),
  308    !,
  309    set_pattributes(Head, Options),
  310    '$wrap_predicate'(Head, table, Closure, Wrapped,
  311                      start_abstract_tabling(Closure, Head, Wrapped)).
  312'$wrap_tabled'(Head, Options) :-
  313    !,
  314    set_pattributes(Head, Options),
  315    '$wrap_predicate'(Head, table, Closure, Wrapped,
  316                      start_tabling(Closure, Head, Wrapped)).
 set_pattributes(:Head, +Options) is det
Set all tabling attributes for Head. These have been collected using table_options/3 from the :- table Head as (Attr1,...) directive.
  323set_pattributes(Head, Options) :-
  324    '$set_predicate_attribute'(Head, tabled, true),
  325    (   tabled_attribute(Attr),
  326        get_dict(Attr, Options, Value),
  327        '$set_predicate_attribute'(Head, Attr, Value),
  328        fail
  329    ;   current_prolog_flag(table_monotonic, lazy),
  330        '$set_predicate_attribute'(Head, lazy, true),
  331        fail
  332    ;   true
  333    ).
  334
  335tabled_attribute(incremental).
  336tabled_attribute(dynamic).
  337tabled_attribute(tshared).
  338tabled_attribute(max_answers).
  339tabled_attribute(subgoal_abstract).
  340tabled_attribute(answer_abstract).
  341tabled_attribute(monotonic).
  342tabled_attribute(opaque).
  343tabled_attribute(lazy).
 start_tabling(:Closure, :Wrapper, :Implementation)
Execute Implementation using tabling. This predicate should not be called directly. The table/1 directive causes a predicate to be translated into a renamed implementation and a wrapper that involves this predicate.
Arguments:
Closure- is the wrapper closure to find the predicate quickly. It is also allowed to pass nothing. In that cases the predicate is looked up using Wrapper. We suggest to pass 0 in this case.
Compatibility
- This interface may change or disappear without notice from future versions.
  359start_tabling(Closure, Wrapper, Worker) :-
  360    '$tbl_variant_table'(Closure, Wrapper, Trie, Status, Skeleton, IsMono),
  361    (   IsMono == true
  362    ->  shift(dependency(Skeleton, Trie, Mono)),
  363        (   Mono == true
  364        ->  tdebug(monotonic, 'Monotonic new answer: ~p', [Skeleton])
  365        ;   start_tabling_2(Closure, Wrapper, Worker, Trie, Status, Skeleton)
  366        )
  367    ;   start_tabling_2(Closure, Wrapper, Worker, Trie, Status, Skeleton)
  368    ).
  369
  370start_tabling_2(Closure, Wrapper, Worker, Trie, Status, Skeleton) :-
  371    tdebug(deadlock, 'Got table ~p, status ~p', [Trie, Status]),
  372    (   Status == complete
  373    ->  trie_gen_compiled(Trie, Skeleton)
  374    ;   functor(Status, fresh, 2)
  375    ->  catch(create_table(Trie, Status, Skeleton, Wrapper, Worker),
  376              deadlock,
  377              restart_tabling(Closure, Wrapper, Worker))
  378    ;   Status == invalid
  379    ->  reeval(Trie, Wrapper, Skeleton)
  380    ;   % = run_follower, but never fresh and Status is a worklist
  381        shift_for_copy(call_info(Skeleton, Status))
  382    ).
  383
  384create_table(Trie, Fresh, Skeleton, Wrapper, Worker) :-
  385    tdebug(Fresh = fresh(SCC, WorkList)),
  386    tdebug(wl_goal(WorkList, Goal, _)),
  387    tdebug(schedule, 'Created component ~d for ~p', [SCC, Goal]),
  388    setup_call_catcher_cleanup(
  389        '$idg_set_current'(OldCurrent, Trie),
  390        run_leader(Skeleton, Worker, Fresh, LStatus, Clause),
  391        Catcher,
  392        finished_leader(OldCurrent, Catcher, Fresh, Wrapper)),
  393    tdebug(schedule, 'Leader ~p done, status = ~p', [Goal, LStatus]),
  394    done_leader(LStatus, Fresh, Skeleton, Clause).
 restart_tabling(+Closure, +Wrapper, +Worker)
We were aborted due to a deadlock. Simply retry. We sleep a very tiny amount to give the thread against which we have deadlocked the opportunity to grab our table. Without, it is common that we re-grab the table within our time slice and before the kernel managed to wakeup the other thread.
  404restart_tabling(Closure, Wrapper, Worker) :-
  405    tdebug(user_goal(Wrapper, Goal)),
  406    tdebug(deadlock, 'Deadlock running ~p; retrying', [Goal]),
  407    sleep(0.000001),
  408    start_tabling(Closure, Wrapper, Worker).
  409
  410restart_abstract_tabling(Closure, Wrapper, Worker) :-
  411    tdebug(user_goal(Wrapper, Goal)),
  412    tdebug(deadlock, 'Deadlock running ~p; retrying', [Goal]),
  413    sleep(0.000001),
  414    start_abstract_tabling(Closure, Wrapper, Worker).
 start_subsumptive_tabling(:Closure, :Wrapper, :Implementation)
(*) We should not use trie_gen_compiled/2 here as this will enumerate all answers while '$tbl_answer_update_dl'/2 uses the available trie indexing to only fetch the relevant answer(s).
To be done
- In the end '$tbl_answer_update_dl'/2 is problematic with incremental and shared tabling as we do not get the consistent update view from the compiled result.
  426start_subsumptive_tabling(Closure, Wrapper, Worker) :-
  427    (   '$tbl_existing_variant_table'(Closure, Wrapper, Trie, Status, Skeleton)
  428    ->  (   Status == complete
  429        ->  trie_gen_compiled(Trie, Skeleton)
  430        ;   Status == invalid
  431        ->  reeval(Trie, Wrapper, Skeleton),
  432            trie_gen_compiled(Trie, Skeleton)
  433        ;   shift_for_copy(call_info(Skeleton, Status))
  434        )
  435    ;   more_general_table(Wrapper, ATrie),
  436        '$tbl_table_status'(ATrie, complete, Wrapper, Skeleton)
  437    ->  '$tbl_answer_update_dl'(ATrie, Skeleton) % see (*)
  438    ;   more_general_table(Wrapper, ATrie),
  439        '$tbl_table_status'(ATrie, Status, GenWrapper, GenSkeleton)
  440    ->  (   Status == invalid
  441        ->  reeval(ATrie, GenWrapper, GenSkeleton),
  442            Wrapper = GenWrapper,
  443            '$tbl_answer_update_dl'(ATrie, GenSkeleton)
  444        ;   wrapper_skeleton(GenWrapper, GenSkeleton, Wrapper, Skeleton),
  445            shift_for_copy(call_info(GenSkeleton, Skeleton, Status)),
  446            unify_subsumptive(Skeleton, GenSkeleton)
  447        )
  448    ;   start_tabling(Closure, Wrapper, Worker)
  449    ).
 wrapper_skeleton(+GenWrapper, +GenSkeleton, +Wrapper, -Skeleton)
Skeleton is a specialized version of GenSkeleton for the subsumed new consumer.
  456wrapper_skeleton(GenWrapper, GenSkeleton, Wrapper, Skeleton) :-
  457    copy_term(GenWrapper+GenSkeleton, Wrapper+Skeleton),
  458    tdebug(call_subsumption, 'GenSkeleton+Skeleton = ~p',
  459           [GenSkeleton+Skeleton]).
  460
  461unify_subsumptive(X,X).
 start_abstract_tabling(:Closure, :Wrapper, :Worker)
Deal with table p/1 as subgoal_abstract(N). This is a merge between variant and subsumptive tabling. If the goal is not abstracted this is simple variant tabling. If the goal is abstracted we must solve the more general goal and use answers from the abstract table.

Wrapper is e.g., user:p(s(s(s(X))),Y) Worker is e.g., call(<closure>(p/2)(s(s(s(X))),Y))

  474start_abstract_tabling(Closure, Wrapper, Worker) :-
  475    '$tbl_abstract_table'(Closure, Wrapper, Trie, _Abstract, Status, Skeleton),
  476    tdebug(abstract, 'Wrapper=~p, Worker=~p, Skel=~p',
  477           [Wrapper, Worker, Skeleton]),
  478    (   is_most_general_term(Skeleton)           % TBD: Fill and test Abstract
  479    ->  start_tabling_2(Closure, Wrapper, Worker, Trie, Status, Skeleton)
  480    ;   Status == complete
  481    ->  '$tbl_answer_update_dl'(Trie, Skeleton)
  482    ;   functor(Status, fresh, 2)
  483    ->  '$tbl_table_status'(Trie, _, GenWrapper, GenSkeleton),
  484        abstract_worker(Worker, GenWrapper, GenWorker),
  485        catch(create_abstract_table(Trie, Status, Skeleton, GenSkeleton, GenWrapper,
  486                                    GenWorker),
  487              deadlock,
  488              restart_abstract_tabling(Closure, Wrapper, Worker))
  489    ;   Status == invalid
  490    ->  '$tbl_table_status'(Trie, _, GenWrapper, GenSkeleton),
  491        reeval(ATrie, GenWrapper, GenSkeleton),
  492        Wrapper = GenWrapper,
  493        '$tbl_answer_update_dl'(ATrie, Skeleton)
  494    ;   shift_for_copy(call_info(GenSkeleton, Skeleton, Status)),
  495        unify_subsumptive(Skeleton, GenSkeleton)
  496    ).
  497
  498create_abstract_table(Trie, Fresh, Skeleton, GenSkeleton, Wrapper, Worker) :-
  499    tdebug(Fresh = fresh(SCC, WorkList)),
  500    tdebug(wl_goal(WorkList, Goal, _)),
  501    tdebug(schedule, 'Created component ~d for ~p', [SCC, Goal]),
  502    setup_call_catcher_cleanup(
  503        '$idg_set_current'(OldCurrent, Trie),
  504        run_leader(GenSkeleton, Worker, Fresh, LStatus, _Clause),
  505        Catcher,
  506        finished_leader(OldCurrent, Catcher, Fresh, Wrapper)),
  507    tdebug(schedule, 'Leader ~p done, status = ~p', [Goal, LStatus]),
  508    Skeleton = GenSkeleton,
  509    done_abstract_leader(LStatus, Fresh, GenSkeleton, Trie).
  510
  511abstract_worker(_:call(Term), _M:GenWrapper, call(GenTerm)) :-
  512    functor(Term, Closure, _),
  513    GenWrapper =.. [_|Args],
  514    GenTerm =.. [Closure|Args].
  515
  516:- '$hide'((done_abstract_leader/4)).  517
  518done_abstract_leader(complete, _Fresh, Skeleton, Trie) :-
  519    !,
  520    '$tbl_answer_update_dl'(Trie, Skeleton).
  521done_abstract_leader(final, fresh(SCC, _Worklist), Skeleton, Trie) :-
  522    !,
  523    '$tbl_free_component'(SCC),
  524    '$tbl_answer_update_dl'(Trie, Skeleton).
  525done_abstract_leader(_,_,_,_).
 done_leader(+Status, +Fresh, +Skeleton, -Clause)
Called on completion of a table. Possibly destroys the component and generates the answers from the complete table. The last cases deals with leaders that are merged into a higher SCC (and thus no longer a leader).
  534:- '$hide'((done_leader/4, finished_leader/4)).  535
  536done_leader(complete, _Fresh, Skeleton, Clause) :-
  537    !,
  538    trie_gen_compiled(Clause, Skeleton).
  539done_leader(final, fresh(SCC, _Worklist), Skeleton, Clause) :-
  540    !,
  541    '$tbl_free_component'(SCC),
  542    trie_gen_compiled(Clause, Skeleton).
  543done_leader(_,_,_,_).
  544
  545finished_leader(OldCurrent, Catcher, Fresh, Wrapper) :-
  546    '$idg_set_current'(OldCurrent),
  547    (   Catcher == exit
  548    ->  true
  549    ;   Catcher == fail
  550    ->  true
  551    ;   Catcher = exception(_)
  552    ->  Fresh = fresh(SCC, _),
  553        '$tbl_table_discard_all'(SCC)
  554    ;   print_message(error, tabling(unexpected_result(Wrapper, Catcher)))
  555    ).
 run_leader(+Skeleton, +Worker, +Fresh, -Status, -Clause) is det
Run the leader of a (new) SCC, storing instantiated copies of Wrapper into Trie. Status is the status of the SCC when this predicate terminates. It is one of complete, in which case local completion finished or merged if running the completion finds an open (not completed) active goal that resides in a parent component. In this case, this SCC has been merged with this parent.

If the SCC is merged, the answers it already gathered are added to the worklist and we shift (suspend), turning our leader into an internal node for the upper SCC.

  570run_leader(Skeleton, Worker, fresh(SCC, Worklist), Status, Clause) :-
  571    tdebug(wl_goal(Worklist, Goal, Skeleton)),
  572    tdebug(schedule, '-> Activate component ~p for ~p', [SCC, Goal]),
  573    activate(Skeleton, Worker, Worklist),
  574    tdebug(schedule, '-> Complete component ~p for ~p', [SCC, Goal]),
  575    completion(SCC, Status, Clause),
  576    tdebug(schedule, '-> Completed component ~p for ~p: ~p', [SCC, Goal, Status]),
  577    (   Status == merged
  578    ->  tdebug(merge, 'Turning leader ~p into follower', [Goal]),
  579        '$tbl_wkl_make_follower'(Worklist),
  580        shift_for_copy(call_info(Skeleton, Worklist))
  581    ;   true                                    % completed
  582    ).
  583
  584activate(Skeleton, Worker, WorkList) :-
  585    tdebug(activate, '~p: created wl=~p', [Skeleton, WorkList]),
  586    (   reset_delays,
  587        delim(Skeleton, Worker, WorkList, []),
  588        fail
  589    ;   true
  590    ).
 delim(+Skeleton, +Worker, +WorkList, +Delays)
Call WorkList and add all instances of Skeleton as answer to WorkList, conditional according to Delays.
Arguments:
Skeleton- is the return skeleton (ret/N term)
Worker- is either the (wrapped) tabled goal or a continuation
WorkList- is the work list associated with Worker (or its continuation).
Delays- is the current delay list. Note that the actual delay also include the internal global delay list. '$tbl_wkl_add_answer'/4 joins the two. For a dependency we join the two explicitly.
  606delim(Skeleton, Worker, WorkList, Delays) :-
  607    reset(Worker, SourceCall, Continuation),
  608    tdebug(wl_goal(WorkList, Goal, _)),
  609    (   Continuation == 0
  610    ->  tdebug('$tbl_add_global_delays'(Delays, AllDelays)),
  611        tdebug(delay_goals(AllDelays, Cond)),
  612        tdebug(answer, 'New answer ~p for ~p (delays = ~p)',
  613               [Skeleton, Goal, Cond]),
  614        '$tbl_wkl_add_answer'(WorkList, Skeleton, Delays, Complete),
  615        Complete == !,
  616        !
  617    ;   SourceCall = call_info(SrcSkeleton, SourceWL)
  618    ->  '$tbl_add_global_delays'(Delays, AllDelays),
  619        tdebug(wl_goal(SourceWL, SrcGoal, _)),
  620        tdebug(wl_goal(WorkList, DstGoal, _)),
  621        tdebug(schedule, 'Suspended ~p, for solving ~p', [SrcGoal, DstGoal]),
  622        '$tbl_wkl_add_suspension'(
  623            SourceWL,
  624            dependency(SrcSkeleton, Continuation, Skeleton, WorkList, AllDelays))
  625    ;   SourceCall = call_info(SrcSkeleton, InstSkeleton, SourceWL)
  626    ->  '$tbl_add_global_delays'(Delays, AllDelays),
  627        tdebug(wl_goal(SourceWL, SrcGoal, _)),
  628        tdebug(wl_goal(WorkList, DstGoal, _)),
  629        tdebug(schedule, 'Suspended ~p, for solving ~p', [SrcGoal, DstGoal]),
  630        '$tbl_wkl_add_suspension'(
  631            SourceWL,
  632            InstSkeleton,
  633            dependency(SrcSkeleton, Continuation, Skeleton, WorkList, AllDelays))
  634    ;   '$tbl_wkl_table'(WorkList, ATrie),
  635        mon_assert_dep(SourceCall, Continuation, Skeleton, ATrie)
  636    ->  delim(Skeleton, Continuation, WorkList, Delays)
  637    ).
 start_moded_tabling(+Closure, :Wrapper, :Implementation, +Variant, +ModeArgs)
As start_tabling/2, but in addition separates the data stored in the answer trie in the Variant and ModeArgs.
  644'$moded_wrap_tabled'(Head, Options, ModeTest, WrapperNoModes, ModeArgs) :-
  645    set_pattributes(Head, Options),
  646    '$wrap_predicate'(Head, table, Closure, Wrapped,
  647                      (   ModeTest,
  648                          start_moded_tabling(Closure, Head, Wrapped,
  649                                              WrapperNoModes, ModeArgs)
  650                      )).
  651
  652
  653start_moded_tabling(Closure, Wrapper, Worker, WrapperNoModes, ModeArgs) :-
  654    '$tbl_moded_variant_table'(Closure, WrapperNoModes, Trie,
  655                               Status, Skeleton, IsMono),
  656    (   IsMono == true
  657    ->  shift(dependency(Skeleton/ModeArgs, Trie, Mono)),
  658        (   Mono == true
  659        ->  tdebug(monotonic, 'Monotonic new answer: ~p', [Skeleton])
  660        ;   start_moded_tabling_2(Closure, Wrapper, Worker, ModeArgs,
  661                                  Trie, Status, Skeleton)
  662        )
  663    ;   start_moded_tabling_2(Closure, Wrapper, Worker, ModeArgs,
  664                              Trie, Status, Skeleton)
  665    ).
  666
  667start_moded_tabling_2(_Closure, Wrapper, Worker, ModeArgs,
  668                      Trie, Status, Skeleton) :-
  669    (   Status == complete
  670    ->  moded_gen_answer(Trie, Skeleton, ModeArgs)
  671    ;   functor(Status, fresh, 2)
  672    ->  setup_call_catcher_cleanup(
  673            '$idg_set_current'(OldCurrent, Trie),
  674            moded_run_leader(Wrapper, Skeleton/ModeArgs,
  675                             Worker, Status, LStatus),
  676            Catcher,
  677            finished_leader(OldCurrent, Catcher, Status, Wrapper)),
  678        tdebug(schedule, 'Leader ~p done, modeargs = ~p, status = ~p',
  679               [Wrapper, ModeArgs, LStatus]),
  680        moded_done_leader(LStatus, Status, Skeleton, ModeArgs, Trie)
  681    ;   Status == invalid
  682    ->  reeval(Trie, Wrapper, Skeleton),
  683        moded_gen_answer(Trie, Skeleton, ModeArgs)
  684    ;   % = run_follower, but never fresh and Status is a worklist
  685        shift_for_copy(call_info(Skeleton/ModeArgs, Status))
  686    ).
  687
  688:- public
  689    moded_gen_answer/3.                         % XSB tables.pl
  690
  691moded_gen_answer(Trie, Skeleton, ModedArgs) :-
  692    trie_gen(Trie, Skeleton),
  693    '$tbl_answer_update_dl'(Trie, Skeleton, ModedArgs).
  694
  695'$tbl_answer'(ATrie, Skeleton, ModedArgs, Delay) :-
  696    trie_gen(ATrie, Skeleton),
  697    '$tbl_answer_c'(ATrie, Skeleton, ModedArgs, Delay).
  698
  699moded_done_leader(complete, _Fresh, Skeleton, ModeArgs, Trie) :-
  700    !,
  701    moded_gen_answer(Trie, Skeleton, ModeArgs).
  702moded_done_leader(final, fresh(SCC, _WorkList), Skeleton, ModeArgs, Trie) :-
  703    !,
  704    '$tbl_free_component'(SCC),
  705    moded_gen_answer(Trie, Skeleton, ModeArgs).
  706moded_done_leader(_, _, _, _, _).
  707
  708moded_run_leader(Wrapper, SkeletonMA, Worker, fresh(SCC, Worklist), Status) :-
  709    tdebug(wl_goal(Worklist, Goal, _)),
  710    tdebug(schedule, '-> Activate component ~p for ~p', [SCC, Goal]),
  711    moded_activate(SkeletonMA, Worker, Worklist),
  712    tdebug(schedule, '-> Complete component ~p for ~p', [SCC, Goal]),
  713    completion(SCC, Status, _Clause),           % TBD: propagate
  714    tdebug(schedule, '-> Completed component ~p for ~p: ~p', [SCC, Goal, Status]),
  715    (   Status == merged
  716    ->  tdebug(merge, 'Turning leader ~p into follower', [Wrapper]),
  717        '$tbl_wkl_make_follower'(Worklist),
  718        shift_for_copy(call_info(SkeletonMA, Worklist))
  719    ;   true                                    % completed
  720    ).
  721
  722moded_activate(SkeletonMA, Worker, WorkList) :-
  723    (   reset_delays,
  724        delim(SkeletonMA, Worker, WorkList, []),
  725        fail
  726    ;   true
  727    ).
 update(+Flags, +Head, +Module, +A1, +A2, -A3, -Action) is semidet
Update the aggregated value for an answer. Iff this predicate succeeds, the aggregated value is updated to A3. If Del is unified with true, A1 should be deleted.
Arguments:
Flags- is a bit mask telling which of A1 and A2 are uncondional
Head- is the head of the predicate
Module- is the module of the predicate
A1- is the currently aggregated value
A2- is the newly produced value
Action- is one of
  • delete to replace the old answer with the new
  • keep to keep the old answer and add the new
  • done to stop the update process
  745:- public
  746    update/7.  747
  748update(0b11, Wrapper, M, A1, A2, A3, delete) :-
  749    !,
  750    M:'$table_update'(Wrapper, A1, A2, A3),
  751    A1 \=@= A3.
  752update(0b10, Wrapper, M, A1, A2, A3, Action) :-
  753    !,
  754    (   is_subsumed_by(Wrapper, M, A2, A1)
  755    ->  Action = done
  756    ;   A3 = A2,
  757        Action = keep
  758    ).
  759update(0b01, Wrapper, M, A1, A2, A2, Action) :-
  760    !,
  761    (   is_subsumed_by(Wrapper, M, A1, A2)
  762    ->  Action = delete
  763    ;   Action = keep
  764    ).
  765update(0b00, _Wrapper, _M, _A1, A2, A2, keep) :-
  766    !.
  767
  768is_subsumed_by(Wrapper, M, Instance, General) :-
  769    M:'$table_update'(Wrapper, Instance, General, New),
  770    New =@= General.
 completion(+Component, -Status, -Clause) is det
Wakeup suspended goals until no new answers are generated. Status is one of merged, completed or final. If Status is not merged, Clause is a compiled representation for the answer trie of the Component leader.
  779completion(SCC, Status, Clause) :-
  780    (   reset_delays,
  781        completion_(SCC),
  782        fail
  783    ;   '$tbl_table_complete_all'(SCC, Status, Clause),
  784        tdebug(schedule, 'SCC ~p: ~p', [scc(SCC), Status])
  785    ).
  786
  787completion_(SCC) :-
  788    repeat,
  789    (   '$tbl_pop_worklist'(SCC, WorkList)
  790    ->  tdebug(wl_goal(WorkList, Goal, _)),
  791        tdebug(schedule, 'Complete ~p in ~p', [Goal, scc(SCC)]),
  792        completion_step(WorkList)
  793    ;   !
  794    ).
 $tbl_wkl_work(+WorkList, -Answer, -Continuation, -Wrapper, -TargetWorklist, -Delays) is nondet
True when Continuation needs to run with Answer and possible answers need to be added to TargetWorklist. The remaining arguments are there to restore variable bindings and restore the delay list.

The suspension added by '$tbl_wkl_add_suspension'/2 is a term dependency(SrcWrapper, Continuation, Wrapper, WorkList, Delays). Note that:

Arguments:
Answer- is the answer term from the answer cluster (node in the answer trie). For answer subsumption it is a term Ret/ModeArgs
Goal- to Delays are extracted from the dependency/5 term in the same order.
  825completion_step(SourceWL) :-
  826    '$tbl_wkl_work'(SourceWL,
  827                    Answer, Continuation, TargetSkeleton, TargetWL, Delays),
  828    tdebug(wl_goal(SourceWL, SourceGoal, _)),
  829    tdebug(wl_goal(TargetWL, TargetGoal, _Skeleton)),
  830    tdebug('$tbl_add_global_delays'(Delays, AllDelays)),
  831    tdebug(delay_goals(AllDelays, Cond)),
  832    tdebug(schedule, 'Resuming ~p, calling ~p with ~p (delays = ~p)',
  833           [TargetGoal, SourceGoal, Answer, Cond]),
  834    delim(TargetSkeleton, Continuation, TargetWL, Delays),
  835    fail.
  836
  837
  838		 /*******************************
  839		 *     STRATIFIED NEGATION	*
  840		 *******************************/
 tnot(:Goal)
Tabled negation.

(*): Only variant tabling is allowed under tnot/1.

  848tnot(Goal0) :-
  849    '$tnot_implementation'(Goal0, Goal),        % verifies Goal is tabled
  850    (   '$tbl_existing_variant_table'(_, Goal, Trie, Status, Skeleton),
  851        Status \== invalid
  852    ->  '$idg_add_edge'(Trie),
  853        (   '$tbl_answer_dl'(Trie, _, true)
  854        ->  fail
  855        ;   '$tbl_answer_dl'(Trie, _, _)
  856        ->  tdebug(tnot, 'tnot: adding ~p to delay list', [Goal]),
  857            add_delay(Trie)
  858        ;   Status == complete
  859        ->  true
  860        ;   negation_suspend(Goal, Skeleton, Status)
  861        )
  862    ;   tdebug(tnot, 'tnot: ~p: fresh', [Goal]),
  863        (   '$wrapped_implementation'(Goal, table, Implementation), % see (*)
  864            functor(Implementation, Closure, _),
  865            start_tabling(Closure, Goal, Implementation),
  866            fail
  867        ;   '$tbl_existing_variant_table'(_, Goal, Trie, NewStatus, NewSkeleton),
  868            tdebug(tnot, 'tnot: fresh ~p now ~p', [Goal, NewStatus]),
  869            (   '$tbl_answer_dl'(Trie, _, true)
  870            ->  fail
  871            ;   '$tbl_answer_dl'(Trie, _, _)
  872            ->  add_delay(Trie)
  873            ;   NewStatus == complete
  874            ->  true
  875            ;   negation_suspend(Goal, NewSkeleton, NewStatus)
  876            )
  877        )
  878    ).
  879
  880floundering(Goal) :-
  881    format(string(Comment), 'Floundering goal in tnot/1: ~p', [Goal]),
  882    throw(error(instantiation_error, context(_Stack, Comment))).
 negation_suspend(+Goal, +Skeleton, +Worklist)
Suspend Worklist due to negation. This marks the worklist as dealing with a negative literal and suspend.

The completion step will resume negative worklists that have no solutions, causing this to succeed.

  893negation_suspend(Wrapper, Skeleton, Worklist) :-
  894    tdebug(tnot, 'negation_suspend ~p (wl=~p)', [Wrapper, Worklist]),
  895    '$tbl_wkl_negative'(Worklist),
  896    shift_for_copy(call_info(Skeleton, tnot(Worklist))),
  897    tdebug(tnot, 'negation resume ~p (wl=~p)', [Wrapper, Worklist]),
  898    '$tbl_wkl_is_false'(Worklist).
 not_exists(:P) is semidet
Tabled negation for non-ground goals. This predicate uses the tabled meta-predicate tabled_call/1. The tables for tabled_call/1 must be cleared if `the world changes' as well as to avoid aggregating too many variants.
  907not_exists(Goal) :-
  908    ground(Goal),
  909    '$get_predicate_attribute'(Goal, tabled, 1),
  910    !,
  911    tnot(Goal).
  912not_exists(Goal) :-
  913    (   tabled_call(Goal), fail
  914    ;   tnot(tabled_call(Goal))
  915    ).
  916
  917		 /*******************************
  918		 *           DELAY LISTS	*
  919		 *******************************/
  920
  921add_delay(Delay) :-
  922    '$tbl_delay_list'(DL0),
  923    '$tbl_set_delay_list'([Delay|DL0]).
  924
  925reset_delays :-
  926    '$tbl_set_delay_list'([]).
 $wfs_call(:Goal, :Delays)
Call Goal and provide WFS delayed goals as a conjunction in Delays. This predicate is the internal version of call_delays/2 from library(wfs).
  934'$wfs_call'(Goal, M:Delays) :-
  935    '$tbl_delay_list'(DL0),
  936    reset_delays,
  937    call(Goal),
  938    '$tbl_delay_list'(DL1),
  939    (   delay_goals(DL1, M, Delays)
  940    ->  true
  941    ;   Delays = undefined
  942    ),
  943    '$append'(DL0, DL1, DL),
  944    '$tbl_set_delay_list'(DL).
  945
  946delay_goals([], _, true) :-
  947    !.
  948delay_goals([AT+AN|T], M, Goal) :-
  949    !,
  950    (   integer(AN)
  951    ->  at_delay_goal(AT, M, G0, Answer, Moded),
  952        (   '$tbl_is_trienode'(Moded)
  953        ->  trie_term(AN, Answer)
  954        ;   true                        % TBD: Generated moded answer
  955        )
  956    ;   AN = Skeleton/ModeArgs
  957    ->  '$tbl_table_status'(AT, _, M1:GNoModes, Skeleton),
  958        M1:'$table_mode'(G0plain, GNoModes, ModeArgs),
  959        G0 = M1:G0plain
  960    ;   '$tbl_table_status'(AT, _, G0, AN)
  961    ),
  962    GN = G0,
  963    (   T == []
  964    ->  Goal = GN
  965    ;   Goal = (GN,GT),
  966        delay_goals(T, M, GT)
  967    ).
  968delay_goals([AT|T], M, Goal) :-
  969    atrie_goal(AT, G0),
  970    unqualify_goal(G0, M, G1),
  971    GN = tnot(G1),
  972    (   T == []
  973    ->  Goal = GN
  974    ;   Goal = (GN,GT),
  975        delay_goals(T, M, GT)
  976    ).
  977
  978at_delay_goal(tnot(Trie), M, tnot(Goal), Skeleton, Moded) :-
  979    is_trie(Trie),
  980    !,
  981    at_delay_goal(Trie, M, Goal, Skeleton, Moded).
  982at_delay_goal(Trie, M, Goal, Skeleton, Moded) :-
  983    is_trie(Trie),
  984    !,
  985    '$tbl_table_status'(Trie, _Status, M2:Variant, Skeleton),
  986    M2:'$table_mode'(Goal0, Variant, Moded),
  987    unqualify_goal(M2:Goal0, M, Goal).
  988
  989atrie_goal(Trie, M:Goal) :-
  990    '$tbl_table_status'(Trie, _Status, M:Variant, _Skeleton),
  991    M:'$table_mode'(Goal, Variant, _Moded).
  992
  993unqualify_goal(M:Goal, M, Goal0) :-
  994    !,
  995    Goal0 = Goal.
  996unqualify_goal(Goal, _, Goal).
  997
  998
  999                 /*******************************
 1000                 *            CLEANUP           *
 1001                 *******************************/
 abolish_all_tables
Remove all tables. This is normally used to free up the space or recompute the result after predicates on which the result for some tabled predicates depend.

Abolishes both local and shared tables. Possibly incomplete tables are marked for destruction upon completion. The dependency graphs for incremental and monotonic tabling are reclaimed as well.

 1013abolish_all_tables :-
 1014    (   '$tbl_abolish_local_tables'
 1015    ->  true
 1016    ;   true
 1017    ),
 1018    (   '$tbl_variant_table'(VariantTrie),
 1019        trie_gen(VariantTrie, _, Trie),
 1020        '$tbl_destroy_table'(Trie),
 1021        fail
 1022    ;   true
 1023    ).
 1024
 1025abolish_private_tables :-
 1026    (   '$tbl_abolish_local_tables'
 1027    ->  true
 1028    ;   (   '$tbl_local_variant_table'(VariantTrie),
 1029            trie_gen(VariantTrie, _, Trie),
 1030            '$tbl_destroy_table'(Trie),
 1031            fail
 1032        ;   true
 1033        )
 1034    ).
 1035
 1036abolish_shared_tables :-
 1037    (   '$tbl_global_variant_table'(VariantTrie),
 1038        trie_gen(VariantTrie, _, Trie),
 1039        '$tbl_destroy_table'(Trie),
 1040        fail
 1041    ;   true
 1042    ).
 abolish_table_subgoals(:Subgoal) is det
Abolish all tables that unify with SubGoal.
To be done
- : SubGoal must be callable. Should we allow for more general patterns?
 1051abolish_table_subgoals(SubGoal0) :-
 1052    '$tbl_implementation'(SubGoal0, M:SubGoal),
 1053    !,
 1054    '$must_be'(acyclic, SubGoal),
 1055    (   '$tbl_variant_table'(VariantTrie),
 1056        trie_gen(VariantTrie, M:SubGoal, Trie),
 1057        '$tbl_destroy_table'(Trie),
 1058        fail
 1059    ;   true
 1060    ).
 1061abolish_table_subgoals(_).
 abolish_module_tables(+Module) is det
Abolish all tables for predicates associated with the given module.
 1067abolish_module_tables(Module) :-
 1068    '$must_be'(atom, Module),
 1069    '$tbl_variant_table'(VariantTrie),
 1070    current_module(Module),
 1071    !,
 1072    forall(trie_gen(VariantTrie, Module:_, Trie),
 1073           '$tbl_destroy_table'(Trie)).
 1074abolish_module_tables(_).
 abolish_nonincremental_tables is det
Abolish all tables that are not related to incremental predicates.
 1080abolish_nonincremental_tables :-
 1081    (   '$tbl_variant_table'(VariantTrie),
 1082        trie_gen(VariantTrie, _, Trie),
 1083        '$tbl_table_status'(Trie, Status, Goal, _),
 1084        (   Status == complete
 1085        ->  true
 1086        ;   '$permission_error'(abolish, incomplete_table, Trie)
 1087        ),
 1088        \+ predicate_property(Goal, incremental),
 1089        '$tbl_destroy_table'(Trie),
 1090        fail
 1091    ;   true
 1092    ).
 abolish_nonincremental_tables(+Options)
Allow for skipping incomplete tables while abolishing.
To be done
- Mark tables for destruction such that they are abolished when completed.
 1101abolish_nonincremental_tables(Options) :-
 1102    (   Options = on_incomplete(Action)
 1103    ->  Action == skip
 1104    ;   '$option'(on_incomplete(skip), Options)
 1105    ),
 1106    !,
 1107    (   '$tbl_variant_table'(VariantTrie),
 1108        trie_gen(VariantTrie, _, Trie),
 1109        '$tbl_table_status'(Trie, complete, Goal, _),
 1110        \+ predicate_property(Goal, incremental),
 1111        '$tbl_destroy_table'(Trie),
 1112        fail
 1113    ;   true
 1114    ).
 1115abolish_nonincremental_tables(_) :-
 1116    abolish_nonincremental_tables.
 1117
 1118
 1119                 /*******************************
 1120                 *        EXAMINE TABLES        *
 1121                 *******************************/
 current_table(:Variant, -Trie) is nondet
True when Trie is the answer table for Variant. If Variant has an unbound module or goal, all possible answer tries are generated, otherwise Variant is considered a fully instantiated variant and the predicate is semidet.
 1130current_table(Variant, Trie) :-
 1131    ct_generate(Variant),
 1132    !,
 1133    current_table_gen(Variant, Trie).
 1134current_table(Variant, Trie) :-
 1135    current_table_lookup(Variant, Trie),
 1136    !.
 1137
 1138current_table_gen(M:Variant, Trie) :-
 1139    '$tbl_local_variant_table'(VariantTrie),
 1140    trie_gen(VariantTrie, M:NonModed, Trie),
 1141    M:'$table_mode'(Variant, NonModed, _Moded).
 1142current_table_gen(M:Variant, Trie) :-
 1143    '$tbl_global_variant_table'(VariantTrie),
 1144    trie_gen(VariantTrie, M:NonModed, Trie),
 1145    \+ '$tbl_table_status'(Trie, fresh), % shared tables are not destroyed
 1146    M:'$table_mode'(Variant, NonModed, _Moded).
 1147
 1148current_table_lookup(M:Variant, Trie) :-
 1149    M:'$table_mode'(Variant, NonModed, _Moded),
 1150    '$tbl_local_variant_table'(VariantTrie),
 1151    trie_lookup(VariantTrie, M:NonModed, Trie).
 1152current_table_lookup(M:Variant, Trie) :-
 1153    M:'$table_mode'(Variant, NonModed, _Moded),
 1154    '$tbl_global_variant_table'(VariantTrie),
 1155    trie_lookup(VariantTrie, NonModed, Trie),
 1156    \+ '$tbl_table_status'(Trie, fresh).
 1157
 1158ct_generate(M:Variant) :-
 1159    (   var(Variant)
 1160    ->  true
 1161    ;   var(M)
 1162    ).
 1163
 1164                 /*******************************
 1165                 *      WRAPPER GENERATION      *
 1166                 *******************************/
 1167
 1168:- multifile
 1169    system:term_expansion/2,
 1170    tabled/2. 1171:- dynamic
 1172    system:term_expansion/2. 1173
 1174wrappers(Spec, M) -->
 1175    { tabling_defaults(
 1176          [ (table_incremental=true)            - (incremental=true),
 1177            (table_shared=true)                 - (tshared=true),
 1178            (table_subsumptive=true)            - ((mode)=subsumptive),
 1179            call(subgoal_size_restraint(Level)) - (subgoal_abstract=Level)
 1180          ],
 1181          #{}, Defaults)
 1182    },
 1183    wrappers(Spec, M, Defaults).
 1184
 1185wrappers(Var, _, _) -->
 1186    { var(Var),
 1187      !,
 1188      '$instantiation_error'(Var)
 1189    }.
 1190wrappers(M:Spec, _, Opts) -->
 1191    !,
 1192    { '$must_be'(atom, M) },
 1193    wrappers(Spec, M, Opts).
 1194wrappers(Spec as Options, M, Opts0) -->
 1195    !,
 1196    { table_options(Options, Opts0, Opts) },
 1197    wrappers(Spec, M, Opts).
 1198wrappers((A,B), M, Opts) -->
 1199    !,
 1200    wrappers(A, M, Opts),
 1201    wrappers(B, M, Opts).
 1202wrappers(Name//Arity, M, Opts) -->
 1203    { atom(Name), integer(Arity), Arity >= 0,
 1204      !,
 1205      Arity1 is Arity+2
 1206    },
 1207    wrappers(Name/Arity1, M, Opts).
 1208wrappers(Name/Arity, Module, Opts) -->
 1209    { '$option'(mode(TMode), Opts, variant),
 1210      atom(Name), integer(Arity), Arity >= 0,
 1211      !,
 1212      functor(Head, Name, Arity),
 1213      '$tbl_trienode'(Reserved)
 1214    },
 1215    qualify(Module,
 1216            [ '$tabled'(Head, TMode),
 1217              '$table_mode'(Head, Head, Reserved)
 1218            ]),
 1219    [ (:- initialization('$wrap_tabled'(Module:Head, Opts), now))
 1220    ].
 1221wrappers(ModeDirectedSpec, Module, Opts) -->
 1222    { '$option'(mode(TMode), Opts, variant),
 1223      callable(ModeDirectedSpec),
 1224      !,
 1225      functor(ModeDirectedSpec, Name, Arity),
 1226      functor(Head, Name, Arity),
 1227      extract_modes(ModeDirectedSpec, Head, Variant, Modes, Moded),
 1228      updater_clauses(Modes, Head, UpdateClauses),
 1229      mode_check(Moded, ModeTest),
 1230      (   ModeTest == true
 1231      ->  WrapClause = '$wrap_tabled'(Module:Head, Opts),
 1232          TVariant = Head
 1233      ;   WrapClause = '$moded_wrap_tabled'(Module:Head, Opts, ModeTest,
 1234                                            Module:Variant, Moded),
 1235          TVariant = Variant
 1236      )
 1237    },
 1238    qualify(Module,
 1239            [ '$tabled'(Head, TMode),
 1240              '$table_mode'(Head, TVariant, Moded)
 1241            ]),
 1242    [ (:- initialization(WrapClause, now))
 1243    ],
 1244    qualify(Module, UpdateClauses).
 1245wrappers(TableSpec, _M, _Opts) -->
 1246    { '$type_error'(table_desclaration, TableSpec)
 1247    }.
 1248
 1249qualify(Module, List) -->
 1250    { prolog_load_context(module, Module) },
 1251    !,
 1252    clist(List).
 1253qualify(Module, List) -->
 1254    qlist(List, Module).
 1255
 1256clist([])    --> [].
 1257clist([H|T]) --> [H], clist(T).
 1258
 1259qlist([], _)    --> [].
 1260qlist([H|T], M) --> [M:H], qlist(T, M).
 1261
 1262
 1263tabling_defaults([], Dict, Dict).
 1264tabling_defaults([Condition-(Opt=Value)|T], Dict0, Dict) :-
 1265    (   tabling_default(Condition)
 1266    ->  Dict1 = Dict0.put(Opt,Value)
 1267    ;   Dict1 = Dict0
 1268    ),
 1269    tabling_defaults(T, Dict1, Dict).
 1270
 1271tabling_default(Flag=FValue) :-
 1272    !,
 1273    current_prolog_flag(Flag, FValue).
 1274tabling_default(call(Term)) :-
 1275    call(Term).
 1276
 1277% Called from wrappers//2.
 1278
 1279subgoal_size_restraint(Level) :-
 1280    current_prolog_flag(max_table_subgoal_size_action, abstract),
 1281    current_prolog_flag(max_table_subgoal_size, Level).
 table_options(+Options, +OptDictIn, -OptDictOut)
Handler the ... as options ... construct.
 1287table_options(Options, _Opts0, _Opts) :-
 1288    var(Options),
 1289    '$instantiation_error'(Options).
 1290table_options((A,B), Opts0, Opts) :-
 1291    !,
 1292    table_options(A, Opts0, Opts1),
 1293    table_options(B, Opts1, Opts).
 1294table_options(subsumptive, Opts0, Opts1) :-
 1295    !,
 1296    put_dict(mode, Opts0, subsumptive, Opts1).
 1297table_options(variant, Opts0, Opts1) :-
 1298    !,
 1299    put_dict(mode, Opts0, variant, Opts1).
 1300table_options(incremental, Opts0, Opts1) :-
 1301    !,
 1302    put_dict(#{incremental:true,opaque:false}, Opts0, Opts1).
 1303table_options(monotonic, Opts0, Opts1) :-
 1304    !,
 1305    put_dict(monotonic, Opts0, true, Opts1).
 1306table_options(opaque, Opts0, Opts1) :-
 1307    !,
 1308    put_dict(#{incremental:false,opaque:true}, Opts0, Opts1).
 1309table_options(lazy, Opts0, Opts1) :-
 1310    !,
 1311    put_dict(lazy, Opts0, true, Opts1).
 1312table_options(dynamic, Opts0, Opts1) :-
 1313    !,
 1314    put_dict(dynamic, Opts0, true, Opts1).
 1315table_options(shared, Opts0, Opts1) :-
 1316    !,
 1317    put_dict(tshared, Opts0, true, Opts1).
 1318table_options(private, Opts0, Opts1) :-
 1319    !,
 1320    put_dict(tshared, Opts0, false, Opts1).
 1321table_options(max_answers(Count), Opts0, Opts1) :-
 1322    !,
 1323    restraint(max_answers, Count, Opts0, Opts1).
 1324table_options(subgoal_abstract(Size), Opts0, Opts1) :-
 1325    !,
 1326    restraint(subgoal_abstract, Size, Opts0, Opts1).
 1327table_options(answer_abstract(Size), Opts0, Opts1) :-
 1328    !,
 1329    restraint(answer_abstract, Size, Opts0, Opts1).
 1330table_options(Opt, _, _) :-
 1331    '$domain_error'(table_option, Opt).
 1332
 1333restraint(Name, Value0, Opts0, Opts) :-
 1334    '$table_option'(Value0, Value),
 1335    (   Value < 0
 1336    ->  Opts = Opts0
 1337    ;   put_dict(Name, Opts0, Value, Opts)
 1338    ).
 mode_check(+Moded, -TestCode)
Enforce the output arguments of a mode-directed tabled predicate to be unbound.
 1346mode_check(Moded, Check) :-
 1347    var(Moded),
 1348    !,
 1349    Check = (var(Moded)->true;'$uninstantiation_error'(Moded)).
 1350mode_check(Moded, true) :-
 1351    '$tbl_trienode'(Moded),
 1352    !.
 1353mode_check(Moded, (Test->true;'$tabling':instantiated_moded_arg(Vars))) :-
 1354    Moded =.. [s|Vars],
 1355    var_check(Vars, Test).
 1356
 1357var_check([H|T], Test) :-
 1358    (   T == []
 1359    ->  Test = var(H)
 1360    ;   Test = (var(H),Rest),
 1361        var_check(T, Rest)
 1362    ).
 1363
 1364:- public
 1365    instantiated_moded_arg/1. 1366
 1367instantiated_moded_arg(Vars) :-
 1368    '$member'(V, Vars),
 1369    \+ var(V),
 1370    '$uninstantiation_error'(V).
 extract_modes(+ModeSpec, +Head, -Variant, -Modes, -ModedAnswer) is det
Split Head into its variant and term that matches the moded arguments.
Arguments:
ModedAnswer- is a term that captures that value of all moded arguments of an answer. If there is only one, this is the value itself. If there are multiple, this is a term s(A1,A2,...)
 1382extract_modes(ModeSpec, Head, Variant, Modes, ModedAnswer) :-
 1383    compound(ModeSpec),
 1384    !,
 1385    compound_name_arguments(ModeSpec, Name, ModeSpecArgs),
 1386    compound_name_arguments(Head, Name, HeadArgs),
 1387    separate_args(ModeSpecArgs, HeadArgs, VariantArgs, Modes, ModedArgs),
 1388    length(ModedArgs, Count),
 1389    atomic_list_concat([$,Name,$,Count], VName),
 1390    Variant =.. [VName|VariantArgs],
 1391    (   ModedArgs == []
 1392    ->  '$tbl_trienode'(ModedAnswer)
 1393    ;   ModedArgs = [ModedAnswer]
 1394    ->  true
 1395    ;   ModedAnswer =.. [s|ModedArgs]
 1396    ).
 1397extract_modes(Atom, Atom, Variant, [], ModedAnswer) :-
 1398    atomic_list_concat([$,Atom,$,0], Variant),
 1399    '$tbl_trienode'(ModedAnswer).
 separate_args(+ModeSpecArgs, +HeadArgs, -NoModesArgs, -Modes, -ModeArgs) is det
Split the arguments in those that need to be part of the variant identity (NoModesArgs) and those that are aggregated (ModeArgs).
Arguments:
Args- seems a copy of ModeArgs, why?
 1409separate_args([], [], [], [], []).
 1410separate_args([HM|TM], [H|TA], [H|TNA], Modes, TMA):-
 1411    indexed_mode(HM),
 1412    !,
 1413    separate_args(TM, TA, TNA, Modes, TMA).
 1414separate_args([M|TM], [H|TA], TNA, [M|Modes], [H|TMA]):-
 1415    separate_args(TM, TA, TNA, Modes, TMA).
 1416
 1417indexed_mode(Mode) :-                           % XSB
 1418    var(Mode),
 1419    !.
 1420indexed_mode(index).                            % YAP
 1421indexed_mode(+).                                % B
 updater_clauses(+Modes, +Head, -Clauses)
Generates a clause to update the aggregated state. Modes is a list of predicate names we apply to the state.
 1428updater_clauses([], _, []) :- !.
 1429updater_clauses([P], Head, [('$table_update'(Head, S0, S1, S2) :- Body)]) :- !,
 1430    update_goal(P, S0,S1,S2, Body).
 1431updater_clauses(Modes, Head, [('$table_update'(Head, S0, S1, S2) :- Body)]) :-
 1432    length(Modes, Len),
 1433    functor(S0, s, Len),
 1434    functor(S1, s, Len),
 1435    functor(S2, s, Len),
 1436    S0 =.. [_|Args0],
 1437    S1 =.. [_|Args1],
 1438    S2 =.. [_|Args2],
 1439    update_body(Modes, Args0, Args1, Args2, true, Body).
 1440
 1441update_body([], _, _, _, Body, Body).
 1442update_body([P|TM], [A0|Args0], [A1|Args1], [A2|Args2], Body0, Body) :-
 1443    update_goal(P, A0,A1,A2, Goal),
 1444    mkconj(Body0, Goal, Body1),
 1445    update_body(TM, Args0, Args1, Args2, Body1, Body).
 1446
 1447update_goal(Var, _,_,_, _) :-
 1448    var(Var),
 1449    !,
 1450    '$instantiation_error'(Var).
 1451update_goal(lattice(M:PI), S0,S1,S2, M:Goal) :-
 1452    !,
 1453    '$must_be'(atom, M),
 1454    update_goal(lattice(PI), S0,S1,S2, Goal).
 1455update_goal(lattice(Name/Arity), S0,S1,S2, Goal) :-
 1456    !,
 1457    '$must_be'(oneof(integer, lattice_arity, [3]), Arity),
 1458    '$must_be'(atom, Name),
 1459    Goal =.. [Name,S0,S1,S2].
 1460update_goal(lattice(Head), S0,S1,S2, Goal) :-
 1461    compound(Head),
 1462    !,
 1463    compound_name_arity(Head, Name, Arity),
 1464    '$must_be'(oneof(integer, lattice_arity, [3]), Arity),
 1465    Goal =.. [Name,S0,S1,S2].
 1466update_goal(lattice(Name), S0,S1,S2, Goal) :-
 1467    !,
 1468    '$must_be'(atom, Name),
 1469    update_goal(lattice(Name/3), S0,S1,S2, Goal).
 1470update_goal(po(Name/Arity), S0,S1,S2, Goal) :-
 1471    !,
 1472    '$must_be'(oneof(integer, po_arity, [2]), Arity),
 1473    '$must_be'(atom, Name),
 1474    Call =.. [Name, S0, S1],
 1475    Goal = (Call -> S2 = S0 ; S2 = S1).
 1476update_goal(po(M:Name/Arity), S0,S1,S2, Goal) :-
 1477    !,
 1478    '$must_be'(atom, M),
 1479    '$must_be'(oneof(integer, po_arity, [2]), Arity),
 1480    '$must_be'(atom, Name),
 1481    Call =.. [Name, S0, S1],
 1482    Goal = (M:Call -> S2 = S0 ; S2 = S1).
 1483update_goal(po(M:Name), S0,S1,S2, Goal) :-
 1484    !,
 1485    '$must_be'(atom, M),
 1486    '$must_be'(atom, Name),
 1487    update_goal(po(M:Name/2), S0,S1,S2, Goal).
 1488update_goal(po(Name), S0,S1,S2, Goal) :-
 1489    !,
 1490    '$must_be'(atom, Name),
 1491    update_goal(po(Name/2), S0,S1,S2, Goal).
 1492update_goal(Alias, S0,S1,S2, Goal) :-
 1493    update_alias(Alias, Update),
 1494    !,
 1495    update_goal(Update, S0,S1,S2, Goal).
 1496update_goal(Mode, _,_,_, _) :-
 1497    '$domain_error'(tabled_mode, Mode).
 1498
 1499update_alias(first, lattice('$tabling':first/3)).
 1500update_alias(-,     lattice('$tabling':first/3)).
 1501update_alias(last,  lattice('$tabling':last/3)).
 1502update_alias(min,   lattice('$tabling':min/3)).
 1503update_alias(max,   lattice('$tabling':max/3)).
 1504update_alias(sum,   lattice('$tabling':sum/3)).
 1505
 1506mkconj(true, G,  G) :- !.
 1507mkconj(G1,   G2, (G1,G2)).
 1508
 1509
 1510		 /*******************************
 1511		 *          AGGREGATION		*
 1512		 *******************************/
 first(+S0, +S1, -S) is det
 last(+S0, +S1, -S) is det
 min(+S0, +S1, -S) is det
 max(+S0, +S1, -S) is det
 sum(+S0, +S1, -S) is det
Implement YAP tabling modes.
 1522:- public first/3, last/3, min/3, max/3, sum/3. 1523
 1524first(S, _, S).
 1525last(_, S, S).
 1526min(S0, S1, S) :- (S0 @< S1 -> S = S0 ; S = S1).
 1527max(S0, S1, S) :- (S0 @> S1 -> S = S0 ; S = S1).
 1528sum(S0, S1, S) :- S is S0+S1.
 1529
 1530
 1531		 /*******************************
 1532		 *      DYNAMIC PREDICATES	*
 1533		 *******************************/
 $set_table_wrappers(:Head)
Clear/add wrappers and notifications to trap dynamic predicates. This is required both for incremental and monotonic tabling.
 1540'$set_table_wrappers'(Pred) :-
 1541    (   '$get_predicate_attribute'(Pred, incremental, 1),
 1542        \+ '$get_predicate_attribute'(Pred, opaque, 1)
 1543    ->  wrap_incremental(Pred)
 1544    ;   unwrap_incremental(Pred)
 1545    ),
 1546    (   '$get_predicate_attribute'(Pred, monotonic, 1)
 1547    ->  wrap_monotonic(Pred)
 1548    ;   unwrap_monotonic(Pred)
 1549    ).
 1550
 1551		 /*******************************
 1552		 *       MONOTONIC TABLING	*
 1553		 *******************************/
 mon_assert_dep(+Dependency, +Continuation, +Skel, +ATrie) is det
Create a dependency for monotonic tabling. Skel and ATrie are the target trie for solutions of Continuation.
 1560mon_assert_dep(dependency(Dynamic), Cont, Skel, ATrie) :-
 1561    '$idg_add_mono_dyn_dep'(Dynamic,
 1562                            dependency(Dynamic, Cont, Skel),
 1563                            ATrie).
 1564mon_assert_dep(dependency(SrcSkel, SrcTrie, IsMono), Cont, Skel, ATrie) :-
 1565    '$idg_add_monotonic_dep'(SrcTrie,
 1566                             dependency(SrcSkel, IsMono, Cont, Skel),
 1567                             ATrie).
 monotonic_affects(+SrcTrie, +SrcReturn, -IsMono, -Continuation, -Return, -Atrie)
Dependency between two monotonic tables. If SrcReturn is added to SrcTrie we must add all answers for Return of Continuation to Atrie. IsMono shares with Continuation and is used in start_tabling/3 to distinguish normal tabled call from propagation.
 1577monotonic_affects(SrcTrie, SrcSkel, IsMono, Cont, Skel, ATrie) :-
 1578    '$idg_mono_affects_eager'(SrcTrie, ATrie,
 1579                              dependency(SrcSkel, IsMono, Cont, Skel)).
 monotonic_dyn_affects(:Head, -Continuation, -Return, -ATrie)
Dynamic predicate that maintains the dependency from a monotonic
 1585monotonic_dyn_affects(Head, Cont, Skel, ATrie) :-
 1586    dyn_affected(Head, DTrie),
 1587    '$idg_mono_affects_eager'(DTrie, ATrie,
 1588                              dependency(Head, Cont, Skel)).
 wrap_monotonic(:Head)
Prepare the dynamic predicate Head for monotonic tabling. This traps calls to build the dependency graph and updates to propagate answers from new clauses through the dependency graph.
 1596wrap_monotonic(Head) :-
 1597    '$wrap_predicate'(Head, monotonic, _Closure, Wrapped,
 1598                      '$start_monotonic'(Head, Wrapped)),
 1599    '$pi_head'(PI, Head),
 1600    prolog_listen(PI, monotonic_update).
 unwrap_monotonic(+Head)
Remove the monotonic wrappers and dependencies.
 1606unwrap_monotonic(Head) :-
 1607    '$pi_head'(PI, Head),
 1608    (   unwrap_predicate(PI, monotonic)
 1609    ->  prolog_unlisten(PI, monotonic_update)
 1610    ;   true
 1611    ).
 $start_monotonic(+Head, +Wrapped)
This is called the monotonic wrapper around a dynamic predicate to collect the dependencies between the dynamic predicate and the monotonic tabled predicates.
 1619'$start_monotonic'(Head, Wrapped) :-
 1620    (   '$tbl_collect_mono_dep'
 1621    ->  shift(dependency(Head)),
 1622        tdebug(monotonic, 'Cont in $start_dynamic/2 with ~p', [Head]),
 1623        Wrapped,
 1624        tdebug(monotonic, '  --> ~p', [Head])
 1625    ;   Wrapped
 1626    ).
 monotonic_update(+Action, +ClauseRef)
Trap changes to the monotonic dynamic predicate and forward them.
 1632:- public monotonic_update/2. 1633monotonic_update(Action, ClauseRef) :-
 1634    (   atomic(ClauseRef)                       % avoid retractall, start(_)
 1635    ->  '$clause'(Head, _Body, ClauseRef, _Bindings),
 1636        mon_propagate(Action, Head, ClauseRef)
 1637    ;   true
 1638    ).
 mon_propagate(+Action, +Head, +ClauseRef)
Handle changes to a dynamic predicate as part of monotonic updates.
 1645mon_propagate(Action, Head, ClauseRef) :-
 1646    assert_action(Action),
 1647    !,
 1648    setup_call_cleanup(
 1649        '$tbl_propagate_start'(Old),
 1650        propagate_assert(Head),                 % eager monotonic dependencies
 1651        '$tbl_propagate_end'(Old)),
 1652    forall(dyn_affected(Head, ATrie),
 1653           '$mono_idg_changed'(ATrie, ClauseRef)). % lazy monotonic dependencies
 1654mon_propagate(retract, Head, _) :-
 1655    !,
 1656    mon_invalidate_dependents(Head).
 1657mon_propagate(rollback(Action), Head, _) :-
 1658    mon_propagate_rollback(Action, Head).
 1659
 1660mon_propagate_rollback(Action, _Head) :-
 1661    assert_action(Action),
 1662    !.
 1663mon_propagate_rollback(retract, Head) :-
 1664    mon_invalidate_dependents(Head).
 1665
 1666assert_action(asserta).
 1667assert_action(assertz).
 propagate_assert(+Head) is det
Propagate assertion of a dynamic clause with head Head.
 1673propagate_assert(Head) :-
 1674    tdebug(monotonic, 'Asserted ~p', [Head]),
 1675    (   monotonic_dyn_affects(Head, Cont, Skel, ATrie),
 1676        tdebug(monotonic, 'Propagating dyn ~p to ~p', [Head, ATrie]),
 1677        '$idg_set_current'(_, ATrie),
 1678        pdelim(Cont, Skel, ATrie),
 1679        fail
 1680    ;   true
 1681    ).
 incr_propagate_assert(+Head) is det
Propagate assertion of a dynamic clause with head Head, both through eager and dynamic tables.
 1688incr_propagate_assert(Head) :-
 1689    tdebug(monotonic, 'New dynamic answer ~p', [Head]),
 1690    (   dyn_affected(Head, DTrie),
 1691         '$idg_mono_affects'(DTrie, ATrie,
 1692                             dependency(Head, Cont, Skel)),
 1693        tdebug(monotonic, 'Propagating dyn ~p to ~p', [Head, ATrie]),
 1694        '$idg_set_current'(_, ATrie),
 1695        pdelim(Cont, Skel, ATrie),
 1696        fail
 1697    ;   true
 1698    ).
 propagate_answer(+SrcTrie, +SrcSkel) is det
Propagate the new answer SrcSkel to the answer table SrcTrie.
 1705propagate_answer(SrcTrie, SrcSkel) :-
 1706    (   monotonic_affects(SrcTrie, SrcSkel, true, Cont, Skel, ATrie),
 1707        tdebug(monotonic, 'Propagating tab ~p to ~p', [SrcTrie, ATrie]),
 1708        pdelim(Cont, Skel, ATrie),
 1709        fail
 1710    ;   true
 1711    ).
 pdelim(+Worker, +Skel, +ATrie)
Call Worker (a continuation) and add each binding it provides for Skel to ATrie. If a new answer is added to ATrie, using propagate_answer/2 to propagate this further. Note that we may hit new dependencies and thus we need to run this using reset/3.
To be done
- Not sure whether we need full tabling here. Need to think of test cases.
 1723pdelim(Worker, Skel, ATrie) :-
 1724    reset(Worker, Dep, Cont),
 1725    (   Cont == 0
 1726    ->  '$tbl_monotonic_add_answer'(ATrie, Skel),
 1727        propagate_answer(ATrie, Skel)
 1728    ;   mon_assert_dep(Dep, Cont, Skel, ATrie),
 1729        pdelim(Cont, Skel, ATrie)
 1730    ).
 mon_invalidate_dependents(+Head)
A non-monotonic operation was done on Head. Invalidate all dependent tables, preparing for normal incremental reevaluation on the next cycle.
 1738mon_invalidate_dependents(Head) :-
 1739    tdebug(monotonic, 'Invalidate dependents for ~p', [Head]),
 1740    forall(dyn_affected(Head, ATrie),
 1741           '$idg_mono_invalidate'(ATrie)).
 abolish_monotonic_tables
Abolish all monotonic tables and the monotonic dependency relations.
To be done
- : just prepare for incremental reevaluation?
 1749abolish_monotonic_tables :-
 1750    (   '$tbl_variant_table'(VariantTrie),
 1751        trie_gen(VariantTrie, Goal, ATrie),
 1752        '$get_predicate_attribute'(Goal, monotonic, 1),
 1753        '$tbl_destroy_table'(ATrie),
 1754        fail
 1755    ;   true
 1756    ).
 1757
 1758		 /*******************************
 1759		 *      INCREMENTAL TABLING	*
 1760		 *******************************/
 wrap_incremental(:Head) is det
Wrap an incremental dynamic predicate to be added to the IDG.
 1766wrap_incremental(Head) :-
 1767    tdebug(monotonic, 'Wrapping ~p', [Head]),
 1768    abstract_goal(Head, Abstract),
 1769    '$pi_head'(PI, Head),
 1770    (   Head == Abstract
 1771    ->  prolog_listen(PI, dyn_update)
 1772    ;   prolog_listen(PI, dyn_update(Abstract))
 1773    ).
 1774
 1775abstract_goal(M:Head, M:Abstract) :-
 1776    compound(Head),
 1777    '$get_predicate_attribute'(M:Head, abstract, 1),
 1778    !,
 1779    compound_name_arity(Head, Name, Arity),
 1780    functor(Abstract, Name, Arity).
 1781abstract_goal(Head, Head).
 dyn_update(+Action, +Context) is det
Track changes to added or removed clauses. We use '$clause'/4 because it works on erased clauses.
To be done
- Add a '$clause_head'(-Head, +ClauseRef) to only decompile the head.
 1791:- public dyn_update/2, dyn_update/3. 1792
 1793dyn_update(_Action, ClauseRef) :-
 1794    (   atomic(ClauseRef)                       % avoid retractall, start(_)
 1795    ->  '$clause'(Head, _Body, ClauseRef, _Bindings),
 1796        dyn_changed_pattern(Head)
 1797    ;   true
 1798    ).
 1799
 1800dyn_update(Abstract, _, _) :-
 1801    dyn_changed_pattern(Abstract).
 1802
 1803dyn_changed_pattern(Term) :-
 1804    forall(dyn_affected(Term, ATrie),
 1805           '$idg_changed'(ATrie)).
 1806
 1807dyn_affected(Term, ATrie) :-
 1808    '$tbl_variant_table'(VTable),
 1809    trie_gen(VTable, Term, ATrie).
 unwrap_incremental(:Head) is det
Remove dynamic predicate incremenal forwarding, reset the possible abstract property and remove possible tables.
 1816unwrap_incremental(Head) :-
 1817    '$pi_head'(PI, Head),
 1818    abstract_goal(Head, Abstract),
 1819    (   Head == Abstract
 1820    ->  prolog_unlisten(PI, dyn_update)
 1821    ;   '$set_predicate_attribute'(Head, abstract, 0),
 1822        prolog_unlisten(PI, dyn_update(_))
 1823    ),
 1824    (   '$tbl_variant_table'(VariantTrie)
 1825    ->  forall(trie_gen(VariantTrie, Head, ATrie),
 1826               '$tbl_destroy_table'(ATrie))
 1827    ;   true
 1828    ).
 reeval(+ATrie, :Goal, ?Return) is nondet
Called if the table ATrie is out-of-date (has non-zero falsecount). The answers of this predicate are the answers to Goal after re-evaluating the answer trie.

This finds all dependency paths to dynamic predicates and then evaluates the nodes in a breath-first fashion starting at the level just above the dynamic predicates and moving upwards. Bottom up evaluation is used to profit from upward propagation of not-modified events that may cause the evaluation to stop early.

Note that false paths either end in a dynamic node or a complete node. The latter happens if we have and IDG "D -> P -> Q" and we first re-evaluate P for some reason. Now Q can still be invalid after P has been re-evaluated.

Arguments:
ATrie- is the answer trie. When shared tabling, we own this trie.
Goal- is tabled goal (variant). If we run into a deadlock we need to call this.
Return- is the return skeleton. We must run trie_gen_compiled(ATrie, Return) to enumerate the answers
 1854reeval(ATrie, Goal, Return) :-
 1855    catch(try_reeval(ATrie, Goal, Return), deadlock,
 1856          retry_reeval(ATrie, Goal)).
 1857
 1858retry_reeval(ATrie, Goal) :-
 1859    '$tbl_reeval_abandon'(ATrie),
 1860    tdebug(deadlock, 'Deadlock re-evaluating ~p; retrying', [ATrie]),
 1861    sleep(0.000001),
 1862    call(Goal).
 1863
 1864try_reeval(ATrie, Goal, Return) :-
 1865    nb_current('$tbl_reeval', true),
 1866    !,
 1867    tdebug(reeval, 'Nested re-evaluation for ~p', [ATrie]),
 1868    do_reeval(ATrie, Goal, Return).
 1869try_reeval(ATrie, Goal, Return) :-
 1870    tdebug(reeval, 'Planning reeval for ~p', [ATrie]),
 1871    findall(Path, false_path(ATrie, Path), Paths0),
 1872    sort(0, @>, Paths0, Paths1),
 1873    clean_paths(Paths1, Paths),
 1874    tdebug(forall('$member'(Path, Paths),
 1875                  tdebug(reeval, '  Re-eval complete path: ~p', [Path]))),
 1876    reeval_paths(Paths, ATrie),
 1877    do_reeval(ATrie, Goal, Return).
 1878
 1879do_reeval(ATrie, Goal, Return) :-
 1880    '$tbl_reeval_prepare_top'(ATrie, Clause),
 1881    (   Clause == 0                          % complete and answer subsumption
 1882    ->  '$tbl_table_status'(ATrie, _Status, M:Variant, Return),
 1883        M:'$table_mode'(Goal0, Variant, ModeArgs),
 1884        Goal = M:Goal0,
 1885        moded_gen_answer(ATrie, Return, ModeArgs)
 1886    ;   nonvar(Clause)                       % complete
 1887    ->  trie_gen_compiled(Clause, Return)
 1888    ;   call(Goal)                           % actually re-evaluate
 1889    ).
 clean_paths(+PathsIn, -Paths)
Clean the reevaluation paths. Get rid of the head term for ranking and remove duplicate paths. Note that a Path is a list of tries, ground terms.
 1898clean_paths([], []).
 1899clean_paths([[_|Path]|T0], [Path|T]) :-
 1900    clean_paths(T0, Path, T).
 1901
 1902clean_paths([], _, []).
 1903clean_paths([[_|CPath]|T0], CPath, T) :-
 1904    !,
 1905    clean_paths(T0, CPath, T).
 1906clean_paths([[_|Path]|T0], _, [Path|T]) :-
 1907    clean_paths(T0, Path, T).
 reeval_paths(+Paths, +Atrie)
Make Atrie valid again by re-evaluating nodes in Paths. We stop as soon as Atrie is valid again. Note that we may not need to reevaluate all paths because evaluating the head of some path may include other nodes in an SCC, making them valid as well.
 1916reeval_paths([], _) :-
 1917    !.
 1918reeval_paths(BottomUp, ATrie) :-
 1919    is_invalid(ATrie),
 1920    !,
 1921    reeval_heads(BottomUp, ATrie, BottomUp1),
 1922    tdebug(assertion(BottomUp \== BottomUp1)),
 1923    '$list_to_set'(BottomUp1, BottomUp2),
 1924    reeval_paths(BottomUp2, ATrie).
 1925reeval_paths(_, _).
 1926
 1927reeval_heads(_, ATrie, []) :-                % target is valid again
 1928    \+ is_invalid(ATrie),
 1929    !.
 1930reeval_heads([], _, []).
 1931reeval_heads([[H]|B], ATrie, BT) :-          % Last one of a falsepath
 1932    reeval_node(H),
 1933    !,
 1934    reeval_heads(B, ATrie, BT).
 1935reeval_heads([[H|T]|B], ATrie, [T|BT]) :-
 1936    reeval_node(H),
 1937    !,
 1938    reeval_heads(B, ATrie, BT).
 1939reeval_heads([FP|B], ATrie, [FP|BT]) :-
 1940    reeval_heads(B, ATrie, BT).
 false_path(+Atrie, -Path) is nondet
True when Path is a list of invalid tries (bottom up, ending with ATrie). The last element of the list is a term s(Rank,Length,ATrie) that is used for sorting the paths.

If we find a table along the way that is being worked on by some other thread we wait for it.

 1952false_path(ATrie, BottomUp) :-
 1953    false_path(ATrie, Path, []),
 1954    '$reverse'(Path, BottomUp).
 1955
 1956false_path(ATrie, [ATrie|T], Seen) :-
 1957    \+ memberchk(ATrie, Seen),
 1958    '$idg_false_edge'(ATrie, Dep, Status),
 1959    tdebug(reeval, '    ~p has dependent ~p (~w)', [ATrie, Dep, Status]),
 1960    (   Status == invalid
 1961    ->  (   false_path(Dep, T, [ATrie|Seen])
 1962        ->  true
 1963        ;   length(Seen, Len),               % invalid has no dependencies:
 1964            T = [s(2, Len, [])]              % dynamic and tabled or explicitly
 1965        )                                    % invalidated
 1966    ;   status_rank(Status, Rank),
 1967        length(Seen, Len),
 1968        T = [s(Rank,Len,Dep)]
 1969    ).
 1970
 1971status_rank(dynamic,   2) :- !.
 1972status_rank(monotonic, 2) :- !.
 1973status_rank(complete,  1) :- !.
 1974status_rank(Status,    Rank) :-
 1975    var(Rank),
 1976    !,
 1977    format(user_error, 'Re-eval from status ~p~n', [Status]),
 1978    Rank = 0.
 1979status_rank(Rank,   Rank) :-
 1980    format(user_error, 'Re-eval from rank ~p~n', [Rank]).
 1981
 1982is_invalid(ATrie) :-
 1983    '$idg_falsecount'(ATrie, FalseCount),
 1984    FalseCount > 0.
 reeval_node(+ATrie) is semidet
Re-evaluate the invalid answer trie ATrie. Initially this created a nested tabling environment, but this is dropped:

Fails if the node is not ready for evaluation. This is the case if it is valid or it is a lazy table that has invalid dependencies.

 2000reeval_node(ATrie) :-
 2001    '$tbl_reeval_prepare'(ATrie, M:Variant),
 2002    !,
 2003    M:'$table_mode'(Goal0, Variant, _Moded),
 2004    Goal = M:Goal0,
 2005    tdebug(reeval, 'Re-evaluating ~p', [Goal]),
 2006    (   '$idg_reset_current',
 2007        setup_call_cleanup(
 2008            nb_setval('$tbl_reeval', true),
 2009            ignore(Goal),                    % assumes local scheduling
 2010            nb_delete('$tbl_reeval')),
 2011        fail
 2012    ;   tdebug(reeval, 'Re-evaluated ~p', [Goal])
 2013    ).
 2014reeval_node(ATrie) :-
 2015    '$mono_reeval_prepare'(ATrie, Size),
 2016    !,
 2017    reeval_monotonic_node(ATrie, Size).
 2018reeval_node(ATrie) :-
 2019    \+ is_invalid(ATrie).
 2020
 2021reeval_monotonic_node(ATrie, Size) :-
 2022    setup_call_cleanup(
 2023        '$tbl_propagate_start'(Old),
 2024        reeval_monotonic_node(ATrie, Size, Deps),
 2025        '$tbl_propagate_end'(Old)),
 2026    (   Deps == []
 2027    ->  tdebug(reeval, 'Re-evaluation for ~p complete', [ATrie])
 2028    ;   Deps == false
 2029    ->  tdebug(reeval, 'Re-evaluation for ~p queued new answers', [ATrie]),
 2030        reeval_node(ATrie)
 2031    ;   tdebug(reeval, 'Re-evaluation for ~p: new invalid deps: ~p',
 2032               [ATrie, Deps]),
 2033        reeval_nodes(Deps),
 2034        reeval_node(ATrie)
 2035    ).
 2036
 2037reeval_nodes([]).
 2038reeval_nodes([H|T]) :-
 2039    reeval_node(H),
 2040    reeval_nodes(T).
 2041
 2042reeval_monotonic_node(ATrie, Size, Deps) :-
 2043    tdebug(reeval, 'Re-evaluating lazy monotonic ~p', [ATrie]),
 2044    (   '$idg_mono_affects_lazy'(ATrie, _0SrcTrie, Dep, DepRef, Answers),
 2045        length(Answers, Count),
 2046        '$idg_mono_empty_queue'(DepRef, Count),
 2047        (   Dep = dependency(Head, Cont, Skel)
 2048        ->  (   '$member'(ClauseRef, Answers),
 2049                '$clause'(Head, _Body, ClauseRef, _Bindings),
 2050                tdebug(monotonic, 'Propagating ~p from ~p to ~p',
 2051                       [Head, _0SrcTrie, ATrie]),
 2052                '$idg_set_current'(_, ATrie),
 2053                pdelim(Cont, Skel, ATrie),
 2054                fail
 2055            ;   true
 2056            )
 2057        ;   Dep = dependency(SrcSkel, true, Cont, Skel)
 2058        ->  (   '$member'(Node, Answers),
 2059                '$tbl_node_answer'(Node, SrcSkel),
 2060                tdebug(monotonic, 'Propagating ~p from ~p to ~p',
 2061                       [Skel, _0SrcTrie, ATrie]),
 2062                '$idg_set_current'(_, ATrie),
 2063                pdelim(Cont, Skel, ATrie),
 2064                fail
 2065            ;   true
 2066            )
 2067        ;   tdebug(monotonic, 'Skipped queued ~p, answers ~p',
 2068                   [Dep, Answers])
 2069        ),
 2070        fail
 2071    ;   '$mono_reeval_done'(ATrie, Size, Deps)
 2072    ).
 2073
 2074
 2075		 /*******************************
 2076		 *      EXPAND DIRECTIVES	*
 2077		 *******************************/
 2078
 2079system:term_expansion((:- table(Preds)), Expansion) :-
 2080    \+ current_prolog_flag(xref, true),
 2081    prolog_load_context(module, M),
 2082    phrase(wrappers(Preds, M), Clauses),
 2083    multifile_decls(Clauses, Directives0),
 2084    sort(Directives0, Directives),
 2085    '$append'(Directives, Clauses, Expansion).
 2086
 2087multifile_decls([], []).
 2088multifile_decls([H0|T0], [H|T]) :-
 2089    multifile_decl(H0, H),
 2090    !,
 2091    multifile_decls(T0, T).
 2092multifile_decls([_|T0], T) :-
 2093    multifile_decls(T0, T).
 2094
 2095multifile_decl(M:(Head :- _Body), (:- multifile(M:Name/Arity))) :-
 2096    !,
 2097    functor(Head, Name, Arity).
 2098multifile_decl(M:Head, (:- multifile(M:Name/Arity))) :-
 2099    !,
 2100    functor(Head, Name, Arity).
 2101multifile_decl((Head :- _Body), (:- multifile(Name/Arity))) :-
 2102    !,
 2103    functor(Head, Name, Arity).
 2104multifile_decl(Head, (:- multifile(Name/Arity))) :-
 2105    !,
 2106    Head \= (:-_),
 2107    functor(Head, Name, Arity).
 2108
 2109
 2110		 /*******************************
 2111		 *      ANSWER COMPLETION	*
 2112		 *******************************/
 2113
 2114:- public answer_completion/2.
 answer_completion(+AnswerTrie, +Return) is det
Find positive loops in the residual program and remove the corresponding answers, possibly causing additional simplification. This is called from C if simplify_component() detects there are conditional answers after simplification.

Note that we are called recursively from C. Our caller prepared a clean new tabling environment and restores the old one after this predicate terminates.

author
- This code is by David Warren as part of XSB.
See also
- called from C, pl-tabling.c, answer_completion()
 2130answer_completion(AnswerTrie, Return) :-
 2131    tdebug(trie_goal(AnswerTrie, Goal, _Return)),
 2132    tdebug(ac(start), 'START: Answer completion for ~p', [Goal]),
 2133    call_cleanup(answer_completion_guarded(AnswerTrie, Return, Propagated),
 2134                 abolish_table_subgoals(eval_subgoal_in_residual(_,_))),
 2135    (   Propagated > 0
 2136    ->  answer_completion(AnswerTrie, Return)
 2137    ;   true
 2138    ).
 2139
 2140answer_completion_guarded(AnswerTrie, Return, Propagated) :-
 2141    (   eval_subgoal_in_residual(AnswerTrie, Return),
 2142        fail
 2143    ;   true
 2144    ),
 2145    delete_answers_for_failing_calls(Propagated),
 2146    (   Propagated == 0
 2147    ->  mark_succeeding_calls_as_answer_completed
 2148    ;   true
 2149    ).
 delete_answers_for_failing_calls(-Propagated)
Delete answers whose condition is determined to be false and return the number of additional answers that changed status as a consequence of additional simplification propagation.
 2157delete_answers_for_failing_calls(Propagated) :-
 2158    State = state(0),
 2159    (   subgoal_residual_trie(ASGF, ESGF),
 2160        \+ trie_gen(ESGF, _ETmp),
 2161        tdebug(trie_goal(ASGF, Goal0, _)),
 2162        tdebug(trie_goal(ASGF, Goal, _0Return)),
 2163        '$trie_gen_node'(ASGF, _0Return, ALeaf),
 2164        tdebug(ac(prune), '  Removing answer ~p from ~p', [Goal, Goal0]),
 2165	'$tbl_force_truth_value'(ALeaf, false, Count),
 2166        arg(1, State, Prop0),
 2167        Prop is Prop0+Count-1,
 2168        nb_setarg(1, State, Prop),
 2169	fail
 2170    ;   arg(1, State, Propagated)
 2171    ).
 2172
 2173mark_succeeding_calls_as_answer_completed :-
 2174    (   subgoal_residual_trie(ASGF, _ESGF),
 2175        (   '$tbl_answer_dl'(ASGF, _0Return, _True)
 2176        ->  tdebug(trie_goal(ASGF, Answer, _0Return)),
 2177            tdebug(trie_goal(ASGF, Goal, _0Return)),
 2178            tdebug(ac(prune), '  Completed ~p on ~p', [Goal, Answer]),
 2179            '$tbl_set_answer_completed'(ASGF)
 2180        ),
 2181        fail
 2182    ;   true
 2183    ).
 2184
 2185subgoal_residual_trie(ASGF, ESGF) :-
 2186    '$tbl_variant_table'(VariantTrie),
 2187    context_module(M),
 2188    trie_gen(VariantTrie, M:eval_subgoal_in_residual(ASGF, _), ESGF).
 eval_dl_in_residual(+Condition)
Evaluate a condition by only looking at the residual goals of the involved calls.
 2195eval_dl_in_residual(true) :-
 2196    !.
 2197eval_dl_in_residual((A;B)) :-
 2198    !,
 2199    (   eval_dl_in_residual(A)
 2200    ;   eval_dl_in_residual(B)
 2201    ).
 2202eval_dl_in_residual((A,B)) :-
 2203    !,
 2204    eval_dl_in_residual(A),
 2205    eval_dl_in_residual(B).
 2206eval_dl_in_residual(tnot(G)) :-
 2207    !,
 2208    tdebug(ac, ' ? tnot(~p)', [G]),
 2209    current_table(G, SGF),
 2210    '$tbl_table_status'(SGF, _Status, _Wrapper, Return),
 2211    tnot(eval_subgoal_in_residual(SGF, Return)).
 2212eval_dl_in_residual(G) :-
 2213    tdebug(ac, ' ? ~p', [G]),
 2214    (   current_table(G, SGF)
 2215    ->	true
 2216    ;   more_general_table(G, SGF)
 2217    ->	true
 2218    ;	writeln(user_error, 'MISSING CALL? '(G)),
 2219        fail
 2220    ),
 2221    '$tbl_table_status'(SGF, _Status, _Wrapper, Return),
 2222    eval_subgoal_in_residual(SGF, Return).
 2223
 2224more_general_table(G, Trie) :-
 2225    term_variables(G, Vars),
 2226    '$tbl_variant_table'(VariantTrie),
 2227    trie_gen(VariantTrie, G, Trie),
 2228    is_most_general_term(Vars).
 2229
 2230:- table eval_subgoal_in_residual/2.
 eval_subgoal_in_residual(+AnswerTrie, ?Return)
Derive answers for the variant represented by AnswerTrie based on the residual goals only.
 2237eval_subgoal_in_residual(AnswerTrie, _Return) :-
 2238    '$tbl_is_answer_completed'(AnswerTrie),
 2239    !,
 2240    undefined.
 2241eval_subgoal_in_residual(AnswerTrie, Return) :-
 2242    '$tbl_answer'(AnswerTrie, Return, Condition),
 2243    tdebug(trie_goal(AnswerTrie, Goal, Return)),
 2244    tdebug(ac, 'Condition for ~p is ~p', [Goal, Condition]),
 2245    eval_dl_in_residual(Condition).
 2246
 2247
 2248		 /*******************************
 2249		 *            TRIPWIRES		*
 2250		 *******************************/
 tripwire(+Wire, +Action, +Context)
Called from the tabling engine of some tripwire is exceeded and the situation is not handled internally (such as abstract and bounded_rationality.
 2258:- public tripwire/3. 2259:- multifile prolog:tripwire/2. 2260
 2261tripwire(Wire, _Action, Context) :-
 2262    prolog:tripwire(Wire, Context),
 2263    !.
 2264tripwire(Wire, Action, Context) :-
 2265    Error = error(resource_error(tripwire(Wire, Context)), _),
 2266    tripwire_action(Action, Error).
 2267
 2268tripwire_action(warning, Error) :-
 2269    print_message(warning, Error).
 2270tripwire_action(error, Error) :-
 2271    throw(Error).
 2272tripwire_action(suspend, Error) :-
 2273    print_message(warning, Error),
 2274    break.
 2275
 2276
 2277		 /*******************************
 2278		 *   SYSTEM TABLED PREDICATES	*
 2279		 *******************************/
 2280
 2281:- table
 2282    system:undefined/0,
 2283    system:answer_count_restraint/0,
 2284    system:radial_restraint/0,
 2285    system:tabled_call/1.
 undefined is undefined
Expresses the value bottom from the well founded semantics.
 2291system:(undefined :-
 2292    tnot(undefined)).
 answer_count_restraint is undefined
 radial_restraint is undefined
Similar to undefined/0, providing a specific undefined for restraint violations.
 2300system:(answer_count_restraint :-
 2301    tnot(answer_count_restraint)).
 2302
 2303system:(radial_restraint :-
 2304    tnot(radial_restraint)).
 2305
 2306system:(tabled_call(X) :- call(X))