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-2020, VU University Amsterdam
    7                              CWI, Amsterdam
    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(prolog_code,
   37          [ comma_list/2,                       % (A,B) <-> [A,B]
   38            semicolon_list/2,                   % (A;B) <-> [A,B]
   39
   40            mkconj/3,                           % +A, +B, -Conjunction
   41            mkdisj/3,                           % +A, +B, -Disjunction
   42
   43            pi_head/2,                          % :PI, :Head
   44            head_name_arity/3,			% ?Goal, ?Name, ?Arity
   45
   46            most_general_goal/2,                % :Goal, -General
   47            extend_goal/3,                      % :Goal, +Extra, -GoalOut
   48
   49            predicate_label/2,                  % +PI, -Label
   50            predicate_sort_key/2,               % +PI, -Key
   51
   52            is_control_goal/1                   % @Term
   53          ]).   54:- autoload(library(error),[must_be/2, instantiation_error/1]).   55:- autoload(library(lists),[append/3]).   56
   57
   58:- multifile
   59    user:prolog_predicate_name/2.

Utilities for reasoning about code

This library collects utilities to reason about terms commonly needed for reasoning about Prolog code. Note that many related facilities can be found in the core as well as other libraries:

*/

 comma_list(?CommaList, ?List)
 semicolon_list(?SemicolonList, ?List)
True if CommaList is a nested term over the ','/2 (';'/2) functor and List is a list expressing the elements of the conjunction. The predicate is deterministic if at least CommaList or List is sufficiently instantiated. If both are partial structures it enumerates ever growing conjunctions and lists. CommaList may be left or right associative on input. When generated, the CommaList is always right associative.

This predicate is typically used to reason about Prolog conjunctions (disjunctions) as many operations are easier on lists than on binary trees over some operator.

   90comma_list(CommaList, List) :-
   91    phrase(binlist(CommaList, ','), List).
   92semicolon_list(CommaList, List) :-
   93    phrase(binlist(CommaList, ';'), List).
   94
   95binlist(Term, Functor) -->
   96    { nonvar(Term) },
   97    !,
   98    (   { Term =.. [Functor,A,B] }
   99    ->  binlist(A, Functor),
  100        binlist(B, Functor)
  101    ;   [Term]
  102    ).
  103binlist(Term, Functor) -->
  104    [A],
  105    (   var_tail
  106    ->  (   { Term = A }
  107        ;   { Term =.. [Functor,A,B] },
  108            binlist(B,Functor)
  109        )
  110    ;   \+ [_]
  111    ->  {Term = A}
  112    ;   binlist(B,Functor),
  113        {Term =.. [Functor,A,B]}
  114    ).
  115
  116var_tail(H, H) :-
  117    var(H).
 mkconj(A, B, Conj) is det
 mkdisj(A, B, Disj) is det
Create a conjunction or disjunction from two terms. Reduces on true.
  125mkconj(A,B,Conj) :-
  126    (   is_true(A)
  127    ->  Conj = B
  128    ;   is_true(B)
  129    ->  Conj = A
  130    ;   Conj = (A,B)
  131    ).
  132
  133mkdisj(A,B,Conj) :-
  134    (   is_false(A)
  135    ->  Conj = B
  136    ;   is_false(B)
  137    ->  Conj = A
  138    ;   Conj = (A;B)
  139    ).
  140
  141is_true(Goal) :- Goal == true.
  142is_false(Goal) :- (Goal == false -> true ; Goal == fail).
 pi_head(?PredicateIndicator, ?Goal) is det
Translate between a PredicateIndicator and a Goal term. The terms may have a module qualification.
Errors
- type_error(predicate_indicator, PredicateIndicator)
  151pi_head(PI, Head) :-
  152    '$pi_head'(PI, Head).
 head_name_arity(?Goal, ?Name, ?Arity) is det
Similar to functor/3, but deals with SWI-Prolog's zero-argument callable terms and avoids creating a non-callable term if Name is not an atom and Arity is zero.
  160head_name_arity(Goal, Name, Arity) :-
  161    '$head_name_arity'(Goal, Name, Arity).
 most_general_goal(+Goal, -General) is det
General is the most general version of Goal. Goal can be qualified.
See also
- is_most_general_term/1.
  169most_general_goal(Goal, General) :-
  170    var(Goal),
  171    !,
  172    General = Goal.
  173most_general_goal(Goal, General) :-
  174    atom(Goal),
  175    !,
  176    General = Goal.
  177most_general_goal(M:Goal, M:General) :-
  178    !,
  179    most_general_goal(Goal, General).
  180most_general_goal(Compound, General) :-
  181    compound_name_arity(Compound, Name, Arity),
  182    compound_name_arity(General, Name, Arity).
 extend_goal(:Goal0, +Extra, -Goal) is det
Extend the possibly qualified Goal0 with additional arguments from Extra.
  190extend_goal(Goal0, _, _) :-
  191    var(Goal0),
  192    !,
  193    instantiation_error(Goal0).
  194extend_goal(M:Goal0, Extra, M:Goal) :-
  195    extend_goal(Goal0, Extra, Goal).
  196extend_goal(Atom, Extra, Goal) :-
  197    atom(Atom),
  198    !,
  199    Goal =.. [Atom|Extra].
  200extend_goal(Goal0, Extra, Goal) :-
  201    compound_name_arguments(Goal0, Name, Args0),
  202    append(Args0, Extra, Args),
  203    compound_name_arguments(Goal, Name, Args).
  204
  205
  206		 /*******************************
  207		 *            LABELS		*
  208		 *******************************/
 predicate_label(++PI, -Label) is det
Create a human-readable label for the given predicate indicator. This notably hides the module qualification from user and built-in predicates. This predicate is intended for reporting predicate information to the user, for example in the profiler.

First PI is converted to a head and the hook user:prolog_predicate_name/2 is tried.

  220predicate_label(PI, Label) :-
  221    must_be(ground, PI),
  222    pi_head(PI, Head),
  223    user:prolog_predicate_name(Head, Label),
  224    !.
  225predicate_label(M:Name/Arity, Label) :-
  226    !,
  227    (   hidden_module(M, Name/Arity)
  228    ->  atomic_list_concat([Name, /, Arity], Label)
  229    ;   atomic_list_concat([M, :, Name, /, Arity], Label)
  230    ).
  231predicate_label(M:Name//Arity, Label) :-
  232    !,
  233    (   hidden_module(M, Name//Arity)
  234    ->  atomic_list_concat([Name, //, Arity], Label)
  235    ;   atomic_list_concat([M, :, Name, //, Arity], Label)
  236    ).
  237predicate_label(Name/Arity, Label) :-
  238    !,
  239    atomic_list_concat([Name, /, Arity], Label).
  240predicate_label(Name//Arity, Label) :-
  241    !,
  242    atomic_list_concat([Name, //, Arity], Label).
  243
  244hidden_module(system, _).
  245hidden_module(user, _).
  246hidden_module(M, Name/Arity) :-
  247    functor(H, Name, Arity),
  248    predicate_property(system:H, imported_from(M)).
  249hidden_module(M, Name//DCGArity) :-
  250    Arity is DCGArity+1,
  251    functor(H, Name, Arity),
  252    predicate_property(system:H, imported_from(M)).
 predicate_sort_key(+PI, -Key) is det
Key is the (module-free) name of the predicate for sorting purposes.
  258predicate_sort_key(_:PI, Name) :-
  259    !,
  260    predicate_sort_key(PI, Name).
  261predicate_sort_key(Name/_Arity, Name).
  262predicate_sort_key(Name//_Arity, Name).
 is_control_goal(@Goal)
True if Goal is a compiled Prolog control structure. The difference between control structures and meta-predicates is rather unclear. The constructs below are recognised by the compiler and cannot be redefined. Note that (if->then;else) is recognised as ((if->then);else).
  272is_control_goal(Goal) :-
  273    var(Goal),
  274    !, fail.
  275is_control_goal((_,_)).
  276is_control_goal((_;_)).
  277is_control_goal((_->_)).
  278is_control_goal((_|_)).
  279is_control_goal((_*->_)).
  280is_control_goal(\+(_))