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, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(wfs,
   36          [ call_residual_program/2,            % :Goal, -Clauses
   37
   38            call_delays/2,                      % :Goal, -Delays
   39            delays_residual_program/2,          % +Delays, -Clauses
   40            answer_residual/2,                  % :Goal, :Residual
   41
   42            op(900, fy, tnot)
   43          ]).   44:- use_module(library(error)).   45:- use_module(library(apply)).   46:- use_module(library(lists)).

Well Founded Semantics interface

The library(wfs) provides the user interface to the Well Founded Semantics (WFS) support in SWI-Prolog. */

   54:- meta_predicate
   55    call_delays(0, :),
   56    delays_residual_program(:, :),
   57    call_residual_program(0, :),
   58    answer_residual(:, :).
 call_delays(:Goal, -Delays)
True when Goal is true with Delays. Delays is true if the answer is unconditionally true and a conjuctions of tabled goals that are unknown according to the Well Founded Semantics otherwise. Delays only contains the unknown goals used for proving Goal. The predicate call_delays/2 is semantically equivalent to call/1, including management of the delay list.
   69call_delays(Goal, Delays) :-
   70    '$wfs_call'(Goal, Delays).
 delays_residual_program(+Delays, -Clauses)
Given a delay as returned by call_delays/2, produce a set of clauses the represents the complete residual program responsible for these delays, The program contains at least one loop through tnot/1 and is either inconsistent or has multiple models according to the stable model semantics.
   80delays_residual_program(GM:Delays, M:Clauses) :-
   81    phrase(residual_program(Delays, GM, [], _), Program),
   82    maplist(unqualify_clause(M), Program, Clauses0),
   83    list_to_set(Clauses0, Clauses).
 call_residual_program(:Goal, -Clauses)
Call Goal and return the full residual program as a list of Clauses.
   89call_residual_program(Goal, M:Clauses) :-
   90    '$wfs_call'(Goal, 0:R0),                    % 0: leave qualified
   91    phrase(residual_program(R0, M, [], _), Program),
   92    maplist(unqualify_clause(M), Program, Clauses).
   93
   94
   95residual_program(Var, _, _, _) -->
   96    { var(Var),
   97      !,
   98      instantiation_error(Var)
   99    }.
  100residual_program(M:G, _, Done0, Done) -->
  101    !,
  102    residual_program(G, M, Done0, Done).
  103residual_program(true, _, Done, Done) -->
  104    !.
  105residual_program(undefined, _, Done, Done) -->
  106    !.
  107residual_program(G, M, Done, Done) -->
  108    { member(M:G2, Done),
  109      G2 =@= G
  110    }, !.
  111residual_program((A;B), M, Done0, Done) -->
  112    !,
  113    residual_program(A, M, Done0, Done1),
  114    residual_program(B, M, Done1, Done).
  115residual_program((A,B), M, Done0, Done) -->
  116    !,
  117    residual_program(A, M, Done0, Done1),
  118    residual_program(B, M, Done1, Done).
  119residual_program(tnot(A), M, Done0, Done) -->
  120    !,
  121    residual_program(A, M, Done0, Done).
  122residual_program(Goal0, M, Done0, Done) -->
  123    { predicate_property(M:Goal0, imported_from(M2))
  124    },
  125    !,
  126    residual_program(Goal0, M2, Done0, Done).
  127residual_program(Goal, M, Done0, Done) -->
  128    { (   current_table(M:Goal, Trie)
  129      ->  true
  130      ;   '$tabling':more_general_table(M:Goal, Trie)
  131      ->  true
  132      ;   format(user_error, 'OOPS: Missing Call? ~p', [M:Goal])
  133      ),
  134      '$tbl_table_status'(Trie, _Status, M:Goal, Skeleton),
  135      '$tbl_answer'(Trie, Skeleton, Condition)
  136    },
  137    [ (M:Goal :- Condition) ],
  138    residual_program(Condition, M, [M:Goal|Done0], Done).
  139
  140unqualify_clause(M, (Head0 :- Body0), (Head :- Body)) :-
  141    unqualify(Head0, M, Head),
  142    unqualify(Body0, M, Body).
 answer_residual(:Goal, :Residual)
True when Goal resolves to a tabled predicate and Residual is the residual goal associated with an answer for Goal. Residual is in its most general form a disjunction (;/2) of conjunctions (,/2) of tabled goals.
  151answer_residual(Goal, M:Residual) :-
  152    predicate_property(Goal, tabled(_)),
  153    !,
  154    '$tbl_variant_table'(VariantTrie),
  155    trie_gen(VariantTrie, Goal, Trie),
  156    '$tbl_table_status'(Trie, _Status, Goal, Skeleton),
  157    '$tbl_answer'(Trie, Skeleton, Condition),
  158    unqualify(Condition, M, Residual).
  159answer_residual(Goal, _) :-
  160    permission_error(answer_residual, non_tabled_procedure, Goal).
  161
  162unqualify((A0;B0), M, G) :-
  163    !,
  164    G = (A;B),
  165    unqualify(A0, M, A),
  166    unqualify(B0, M, B).
  167unqualify((A0,B0), M, G) :-
  168    !,
  169    G = (A,B),
  170    unqualify(A0, M, A),
  171    unqualify(B0, M, B).
  172unqualify(tnot(A0), M, G) :-
  173    !,
  174    G = tnot(A),
  175    unqualify(A0, M, A).
  176unqualify(M:G0, MG, G) :-
  177    '$c_current_predicate'(_, MG:G0),
  178    predicate_property(MG:G0, imported_from(M)),
  179    !,
  180    G = G0.
  181unqualify(M:G0, M, G) :-
  182    !,
  183    G = G0.
  184unqualify(G, _, G)