View source with formatted 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)  2001-2012, University of 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(occurs,
   36          [ contains_term/2,            % +SubTerm, +Term
   37            contains_var/2,             % +SubTerm, +Term
   38            free_of_term/2,             % +SubTerm, +Term
   39            free_of_var/2,              % +SubTerm, +Term
   40            occurrences_of_term/3,      % +SubTerm, +Term, ?Tally
   41            occurrences_of_var/3,       % +SubTerm, +Term, ?Tally
   42            sub_term/2,                 % -SubTerm, +Term
   43            sub_var/2                   % -SubTerm, +Term (SWI extra)
   44          ]).   45
   46/** <module> Finding and counting sub-terms
   47
   48This  is  a  SWI-Prolog  implementation  of  the  corresponding  Quintus
   49library, based on the generalised arg/3 predicate of SWI-Prolog.
   50
   51@see library(terms) provides similar predicates and is probably
   52     more wide-spread than this library.
   53*/
   54
   55%!  contains_term(+Sub, +Term) is semidet.
   56%
   57%   Succeeds if Sub is contained in Term (=, deterministically)
   58
   59contains_term(X, X) :- !.
   60contains_term(X, Term) :-
   61    compound(Term),
   62    arg(_, Term, Arg),
   63    contains_term(X, Arg),
   64    !.
   65
   66
   67%!  contains_var(+Sub, +Term) is det.
   68%
   69%   Succeeds if Sub is contained in Term (==, deterministically)
   70
   71contains_var(X0, X1) :-
   72    X0 == X1,
   73    !.
   74contains_var(X, Term) :-
   75    compound(Term),
   76    arg(_, Term, Arg),
   77    contains_var(X, Arg),
   78    !.
   79
   80%!  free_of_term(+Sub, +Term)
   81%
   82%   Succeeds of Sub does not unify to any subterm of Term
   83
   84free_of_term(Sub, Term) :-
   85    \+ contains_term(Sub, Term).
   86
   87%!  free_of_var(+Sub, +Term)
   88%
   89%   Succeeds of Sub is not equal (==) to any subterm of Term
   90
   91free_of_var(Sub, Term) :-
   92    \+ contains_var(Sub, Term).
   93
   94%!  occurrences_of_term(+SubTerm, +Term, ?Count)
   95%
   96%   Count the number of SubTerms in Term
   97
   98occurrences_of_term(Sub, Term, Count) :-
   99    count(sub_term(Sub, Term), Count).
  100
  101%!  occurrences_of_var(+SubTerm, +Term, ?Count)
  102%
  103%   Count the number of SubTerms in Term
  104
  105occurrences_of_var(Sub, Term, Count) :-
  106    count(sub_var(Sub, Term), Count).
  107
  108%!  sub_term(-Sub, +Term)
  109%
  110%   Generates (on backtracking) all subterms of Term.
  111
  112sub_term(X, X).
  113sub_term(X, Term) :-
  114    compound(Term),
  115    arg(_, Term, Arg),
  116    sub_term(X, Arg).
  117
  118%!  sub_var(-Sub, +Term)
  119%
  120%   Generates (on backtracking) all subterms (==) of Term.
  121
  122sub_var(X0, X1) :-
  123    X0 == X1.
  124sub_var(X, Term) :-
  125    compound(Term),
  126    arg(_, Term, Arg),
  127    sub_var(X, Arg).
  128
  129
  130                 /*******************************
  131                 *              UTIL            *
  132                 *******************************/
  133
  134%!  count(:Goal, -Count)
  135%
  136%   Count number of times Goal succeeds.
  137
  138:- meta_predicate count(0,-).  139
  140count(Goal, Count) :-
  141    State = count(0),
  142    (   Goal,
  143        arg(1, State, N0),
  144        N is N0 + 1,
  145        nb_setarg(1, State, N),
  146        fail
  147    ;   arg(1, State, Count)
  148    )