View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2019-2021, VU University Amsterdam
    7                              SWI-Prolog Solutions b.v.
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(increval,
   37          [ incr_assert/1,                      % :Clause
   38            incr_asserta/1,                     % :Clause
   39            incr_assertz/1,                     % :Clause
   40            incr_retractall/1,                  % :Head
   41            incr_retract/1,                     % :Clause
   42
   43            is_incremental_subgoal/1,           % :Goal
   44            incr_directly_depends/2,            % :Goal1, :Goal2
   45            incr_trans_depends/2,		% :Goal1, :Goal2
   46            incr_invalid_subgoals/1,            % -List
   47            incr_is_invalid/1,                  % :Goal
   48
   49            incr_invalidate_call/1,		% :Goal
   50            incr_invalidate_calls/1,		% :Goal
   51            incr_table_update/0,
   52
   53            incr_propagate_calls/1              % :Answer
   54          ]).   55:- use_module(library(tables)).

Incremental dynamic predicate modification

This module emulates the XSB module increval. This module serves two goals: (1) provide alternatives for the dynamic clause manipulation predicates that propagate into the incremental tables and (2) query the dynamically maintained Incremental Depency Graph (IDG).

The change propagation for incremental dynamic predicates. SWI-Prolog relies in prolog_listen/2 to forward any change to dynamic predicates to the table IDG and incr_assert/1 and friends thus simply call the corresponding database update.

Compatibility
- XSB */
   72:- meta_predicate
   73    incr_assert(:),
   74    incr_asserta(:),
   75    incr_assertz(:),
   76    incr_retractall(:),
   77    incr_retract(:),
   78    is_incremental_subgoal(:),
   79    incr_directly_depends(:,:),
   80    incr_trans_depends(:, :),
   81    incr_is_invalid(:),
   82    incr_invalidate_call(:),
   83    incr_invalidate_calls(:),
   84    incr_propagate_calls(:).   85
   86incr_assert(T)     :- assertz(T).
   87incr_asserta(T)    :- asserta(T).
   88incr_assertz(T)    :- assertz(T).
   89incr_retractall(T) :- retractall(T).
   90incr_retract(T)    :- retract(T).
 is_incremental_subgoal(?SubGoal) is nondet
This predicate non-deterministically unifies Subgoal with incrementally tabled subgoals that are currently table entries.
   97is_incremental_subgoal(SubGoal) :-
   98    '$tbl_variant_table'(VTrie),
   99    trie_gen(VTrie, SubGoal, ATrie),
  100    (   '$idg_edge'(ATrie, _Dir, _Value)
  101    ->  true
  102    ).
 incr_directly_depends(:Goal1, :Goal2) is nondet
True if Goal1 depends on Goal2 in the IDG.
Compatibility
- : In XSB, at least one of Goal 1 or Goal 2 must be bound. This implementation may be used with both arguments unbound.
  111incr_directly_depends(Goal1, Goal2) :-
  112    '$tbl_variant_table'(VTrie),
  113    (   nonvar(Goal2)
  114    ->  trie_gen(VTrie, Goal2, ATrie2),
  115        '$idg_edge'(ATrie2, affected, ATrie1),
  116        '$tbl_table_status'(ATrie1, _Status, Goal1, _Return)
  117    ;   trie_gen(VTrie, Goal1, ATrie1),
  118        '$idg_edge'(ATrie1, dependent, ATrie2),
  119        '$tbl_table_status'(ATrie2, _Status, Goal2, _Return)
  120    ).
 incr_trans_depends(:Goal1, Goal2) is nondet
True for each pair in the transitive closure of incr_directly_depends(G1, G2).
  127incr_trans_depends(Goal1, Goal2) :-
  128    '$tbl_variant_table'(VTrie),
  129    (   nonvar(Goal1)
  130    ->  trie_gen(VTrie, Goal1, ATrie1),
  131        incr_trans_depends(ATrie1, dependent, ATrie2, []),
  132        '$tbl_table_status'(ATrie2, _Status, Goal2, _Return)
  133    ;   trie_gen(VTrie, Goal2, ATrie2),
  134        incr_trans_depends(ATrie2, affected, ATrie1, []),
  135        '$tbl_table_status'(ATrie1, _Status, Goal1, _Return)
  136    ).
  137
  138incr_trans_depends(ATrie1, Dir, ATrie, Done) :-
  139    '$idg_edge'(ATrie1, Dir, ATrie2),
  140    \+ memberchk(ATrie2, Done),
  141    (   ATrie = ATrie2
  142    ;   incr_trans_depends(ATrie2, Dir, ATrie, [ATrie2|Done])
  143    ).
 incr_invalid_subgoals(-List) is det
List is a sorted list (set) of the incremental subgoals that are currently invalid.
  150incr_invalid_subgoals(List) :-
  151    findall(Invalid, invalid_subgoal(Invalid, _), List0),
  152    sort(List0, List).
  153
  154invalid_subgoal(Goal, ATrie) :-
  155    '$tbl_variant_table'(VTrie),
  156    trie_gen(VTrie, Goal, ATrie),
  157    (   '$idg_edge'(ATrie, _Dir, _Value)
  158    ->  true
  159    ),
  160    '$idg_falsecount'(ATrie, Count),
  161    Count > 0,
  162    \+ '$tbl_table_status'(ATrie, dynamic, _Goal, _Return).
 incr_is_invalid(:Subgoal) is semidet
True when Subgoal's table is marked as invalid.
  168incr_is_invalid(Subgoal) :-
  169    get_calls(Subgoal, Table, _Return),
  170    '$idg_falsecount'(Table, Count),
  171    Count > 0.
 incr_invalidate_calls(:Goal) is det
Invalidate all tables for subgoals of Goal as well as tables that are affected by these.
  178incr_invalidate_calls(Goal) :-
  179    '$tbl_variant_table'(VTable),
  180    !,
  181    forall(trie_gen(VTable, Goal, ATrie),
  182           '$idg_changed'(ATrie)).
  183incr_invalidate_calls(_).
 incr_invalidate_call(:Goal) is det
This is the XSB name, but the manual says incr_invalidate_calls/1 and the comment with the code suggests this is misnamed.
deprecated
- Use incr_invalidate_calls/1.
  192incr_invalidate_call(Goal) :-
  193    incr_invalidate_calls(Goal).
 incr_table_update
Updated all invalid tables
  199incr_table_update :-
  200    repeat,
  201    (   invalid_subgoal(Goal, ATrie)
  202    ->  '$tabling':reeval(ATrie, Goal, _Return),
  203        fail
  204    ;   !
  205    ).
 incr_propagate_calls(:Answer) is det
Activate the monotonic answer propagation similarly to when a new fact is asserted for a monotonic dynamic predicate. The Answer term must match a monotonic dynamic predicate.
  213incr_propagate_calls(Answer) :-
  214    setup_call_cleanup(
  215        '$tbl_propagate_start'(Old),
  216        '$tabling':incr_propagate_assert(Answer),
  217        '$tbl_propagate_end'(Old))