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:- use_module(library(error)).   51
   52:- multifile
   53    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.

   84comma_list(CommaList, List) :-
   85    phrase(binlist(CommaList, ','), List).
   86semicolon_list(CommaList, List) :-
   87    phrase(binlist(CommaList, ';'), List).
   88
   89binlist(Term, Functor) -->
   90    { nonvar(Term) },
   91    !,
   92    (   { Term =.. [Functor,A,B] }
   93    ->  binlist(A, Functor),
   94        binlist(B, Functor)
   95    ;   [Term]
   96    ).
   97binlist(Term, Functor) -->
   98    [A],
   99    (   var_tail
  100    ->  (   { Term = A }
  101        ;   { Term =.. [Functor,A,B] },
  102            binlist(B,Functor)
  103        )
  104    ;   \+ [_]
  105    ->  {Term = A}
  106    ;   binlist(B,Functor),
  107        {Term =.. [Functor,A,B]}
  108    ).
  109
  110var_tail(H, H) :-
  111    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.
  119mkconj(A,B,Conj) :-
  120    (   is_true(A)
  121    ->  Conj = B
  122    ;   is_true(B)
  123    ->  Conj = A
  124    ;   Conj = (A,B)
  125    ).
  126
  127mkdisj(A,B,Conj) :-
  128    (   is_false(A)
  129    ->  Conj = B
  130    ;   is_false(B)
  131    ->  Conj = A
  132    ;   Conj = (A;B)
  133    ).
  134
  135is_true(Goal) :- Goal == true.
  136is_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)
  145pi_head(PI, Head) :-
  146    '$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.
  154head_name_arity(Goal, Name, Arity) :-
  155    '$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.
  163most_general_goal(Goal, General) :-
  164    var(Goal),
  165    !,
  166    General = Goal.
  167most_general_goal(Goal, General) :-
  168    atom(Goal),
  169    !,
  170    General = Goal.
  171most_general_goal(M:Goal, M:General) :-
  172    !,
  173    most_general_goal(Goal, General).
  174most_general_goal(Compound, General) :-
  175    compound_name_arity(Compound, Name, Arity),
  176    compound_name_arity(General, Name, Arity).
  177
  178
  179		 /*******************************
  180		 *            LABELS		*
  181		 *******************************/
 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.
  190predicate_label(PI, Label) :-
  191    must_be(ground, PI),
  192    user:prolog_predicate_name(PI, Label),
  193    !.
  194predicate_label(M:Name/Arity, Label) :-
  195    !,
  196    (   hidden_module(M, Name/Arity)
  197    ->  atomic_list_concat([Name, /, Arity], Label)
  198    ;   atomic_list_concat([M, :, Name, /, Arity], Label)
  199    ).
  200predicate_label(M:Name//Arity, Label) :-
  201    !,
  202    (   hidden_module(M, Name//Arity)
  203    ->  atomic_list_concat([Name, //, Arity], Label)
  204    ;   atomic_list_concat([M, :, Name, //, Arity], Label)
  205    ).
  206predicate_label(Name/Arity, Label) :-
  207    !,
  208    atomic_list_concat([Name, /, Arity], Label).
  209predicate_label(Name//Arity, Label) :-
  210    !,
  211    atomic_list_concat([Name, //, Arity], Label).
  212
  213hidden_module(system, _).
  214hidden_module(user, _).
  215hidden_module(M, Name/Arity) :-
  216    functor(H, Name, Arity),
  217    predicate_property(system:H, imported_from(M)).
  218hidden_module(M, Name//DCGArity) :-
  219    Arity is DCGArity+1,
  220    functor(H, Name, Arity),
  221    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.
  227predicate_sort_key(_:PI, Name) :-
  228    !,
  229    predicate_sort_key(PI, Name).
  230predicate_sort_key(Name/_Arity, Name).
  231predicate_sort_key(Name//_Arity, Name)