Subterm substitution

Description

A procedure for substituting a subterm of a term by another subterm.

Source: PROLOG programming for artificial intelligence, 3rd Edition, Harlow, 2001, ISBN 0-201-40375-7.

Download

Program source code: subterm_substitution.pl

Listing

%  Figure 7.3  A procedure for substituting a subterm of a term by another subterm.
 
 
% substitute( Subterm, Term, Subterm1, Term1):
%    if all occurrences of Subterm in Term are substituted
%    with Subterm1 then we get Term1.
 
 
 
% Case 1: Substitute whole term
 
substitute( Term, Term, Term1, Term1)  :-  !.
 
 
% Case 2: Nothing to substitute
 
substitute( _, Term, _, Term)  :-
   atomic(Term), !.
 
 
% Case 3: Do substitution on arguments
 
substitute( Sub, Term, Sub1, Term1)  :-
   Term  =..  [F|Args],                        % Get arguments
   substlist( Sub, Args, Sub1, Args1),         % Perform substitution on them
   Term1  =..  [F|Args1].
 
 
substlist( _, [], _, []).
 
substlist( Sub, [Term|Terms], Sub1, [Term1|Terms1])  :-
   substitute( Sub, Term, Sub1, Term1),
   substlist( Sub, Terms, Sub1, Terms1).

Comments

pl/prolog/pllib/subterm_substitution.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0