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)  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          ]).

Finding and counting sub-terms

This is a SWI-Prolog implementation of the corresponding Quintus library, based on the generalised arg/3 predicate of SWI-Prolog.

See also
-
library(terms) provides similar predicates and is probably more wide-spread than this library. */
 contains_term(+Sub, +Term) is semidet
Succeeds if Sub is contained in Term (=, deterministically)
   59contains_term(X, X) :- !.
   60contains_term(X, Term) :-
   61    compound(Term),
   62    arg(_, Term, Arg),
   63    contains_term(X, Arg),
   64    !.
 contains_var(+Sub, +Term) is det
Succeeds if Sub is contained in Term (==, deterministically)
   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    !.
 free_of_term(+Sub, +Term)
Succeeds of Sub does not unify to any subterm of Term
   84free_of_term(Sub, Term) :-
   85    \+ contains_term(Sub, Term).
 free_of_var(+Sub, +Term)
Succeeds of Sub is not equal (==) to any subterm of Term
   91free_of_var(Sub, Term) :-
   92    \+ contains_var(Sub, Term).
 occurrences_of_term(+SubTerm, +Term, ?Count)
Count the number of SubTerms in Term
   98occurrences_of_term(Sub, Term, Count) :-
   99    count(sub_term(Sub, Term), Count).
 occurrences_of_var(+SubTerm, +Term, ?Count)
Count the number of SubTerms in Term
  105occurrences_of_var(Sub, Term, Count) :-
  106    count(sub_var(Sub, Term), Count).
 sub_term(-Sub, +Term)
Generates (on backtracking) all subterms of Term.
  112sub_term(X, X).
  113sub_term(X, Term) :-
  114    compound(Term),
  115    arg(_, Term, Arg),
  116    sub_term(X, Arg).
 sub_var(-Sub, +Term)
Generates (on backtracking) all subterms (==) of Term.
  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                 *******************************/
 count(:Goal, -Count)
Count number of times Goal succeeds.
  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    )