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(prolog_code,
   36          [ comma_list/2,                       % (A,B) <-> [A,B]
   37            semicolon_list/2,                   % (A;B) <-> [A,B]
   38
   39            mkconj/3,                           % +A, +B, -Conjunction
   40            mkdisj/3,                           % +A, +B, -Disjunction
   41
   42            pi_head/2,                          % :PI, :Head
   43            head_name_arity/3,			% ?Goal, ?Name, ?Arity
   44
   45            most_general_goal/2,                % :Goal, -General
   46
   47            predicate_label/2,                  % +PI, -Label
   48            predicate_sort_key/2                % +PI, -Key
   49          ]).   50:- autoload(library(error),[must_be/2]).   51
   52
   53:- multifile
   54    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.

   85comma_list(CommaList, List) :-
   86    phrase(binlist(CommaList, ','), List).
   87semicolon_list(CommaList, List) :-
   88    phrase(binlist(CommaList, ';'), List).
   89
   90binlist(Term, Functor) -->
   91    { nonvar(Term) },
   92    !,
   93    (   { Term =.. [Functor,A,B] }
   94    ->  binlist(A, Functor),
   95        binlist(B, Functor)
   96    ;   [Term]
   97    ).
   98binlist(Term, Functor) -->
   99    [A],
  100    (   var_tail
  101    ->  (   { Term = A }
  102        ;   { Term =.. [Functor,A,B] },
  103            binlist(B,Functor)
  104        )
  105    ;   \+ [_]
  106    ->  {Term = A}
  107    ;   binlist(B,Functor),
  108        {Term =.. [Functor,A,B]}
  109    ).
  110
  111var_tail(H, H) :-
  112    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.
  120mkconj(A,B,Conj) :-
  121    (   is_true(A)
  122    ->  Conj = B
  123    ;   is_true(B)
  124    ->  Conj = A
  125    ;   Conj = (A,B)
  126    ).
  127
  128mkdisj(A,B,Conj) :-
  129    (   is_false(A)
  130    ->  Conj = B
  131    ;   is_false(B)
  132    ->  Conj = A
  133    ;   Conj = (A;B)
  134    ).
  135
  136is_true(Goal) :- Goal == true.
  137is_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)
  146pi_head(PI, Head) :-
  147    '$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.
  155head_name_arity(Goal, Name, Arity) :-
  156    '$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.
  164most_general_goal(Goal, General) :-
  165    var(Goal),
  166    !,
  167    General = Goal.
  168most_general_goal(Goal, General) :-
  169    atom(Goal),
  170    !,
  171    General = Goal.
  172most_general_goal(M:Goal, M:General) :-
  173    !,
  174    most_general_goal(Goal, General).
  175most_general_goal(Compound, General) :-
  176    compound_name_arity(Compound, Name, Arity),
  177    compound_name_arity(General, Name, Arity).
  178
  179
  180		 /*******************************
  181		 *            LABELS		*
  182		 *******************************/
 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 prolog_predicate_name/2 is tried.

  194predicate_label(PI, Label) :-
  195    must_be(ground, PI),
  196    pi_head(PI, Head),
  197    user:prolog_predicate_name(Head, Label),
  198    !.
  199predicate_label(M:Name/Arity, Label) :-
  200    !,
  201    (   hidden_module(M, Name/Arity)
  202    ->  atomic_list_concat([Name, /, Arity], Label)
  203    ;   atomic_list_concat([M, :, Name, /, Arity], Label)
  204    ).
  205predicate_label(M:Name//Arity, Label) :-
  206    !,
  207    (   hidden_module(M, Name//Arity)
  208    ->  atomic_list_concat([Name, //, Arity], Label)
  209    ;   atomic_list_concat([M, :, Name, //, Arity], Label)
  210    ).
  211predicate_label(Name/Arity, Label) :-
  212    !,
  213    atomic_list_concat([Name, /, Arity], Label).
  214predicate_label(Name//Arity, Label) :-
  215    !,
  216    atomic_list_concat([Name, //, Arity], Label).
  217
  218hidden_module(system, _).
  219hidden_module(user, _).
  220hidden_module(M, Name/Arity) :-
  221    functor(H, Name, Arity),
  222    predicate_property(system:H, imported_from(M)).
  223hidden_module(M, Name//DCGArity) :-
  224    Arity is DCGArity+1,
  225    functor(H, Name, Arity),
  226    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.
  232predicate_sort_key(_:PI, Name) :-
  233    !,
  234    predicate_sort_key(PI, Name).
  235predicate_sort_key(Name/_Arity, Name).
  236predicate_sort_key(Name//_Arity, Name)