Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!watmath!clyde!burl!mgnetp!ihnp4!houxm!houxz!vax135!cornell!uw-beaver!tektronix!hplabs!sri-unix!decvax!mulga!munnari!Lee@Ucb-Vax
From: Lee%Ucb-Vax@munnari.UUCP
Newsgroups: net.lang.prolog
Subject: BF search of SLD tree: Solution
Message-ID: <756@sri-arpa.UUCP>
Date: Wed, 6-Jun-84 22:12:38 EDT
Article-I.D.: sri-arpa.756
Posted: Wed Jun  6 22:12:38 1984
Date-Received: Wed, 13-Jun-84 01:48:23 EDT
Lines: 78

I didn't pay much attention to the recent discussion about
breadth first traversals and that news has been removed
from our system.  However, I believe the original question
concerned the BF traversal of the SLD (proof) tree for a
given goal.  The advantage of BF is that it will find all
solutions eventually (depth first can get lost down an
infinite branch).  I have just been working on an algorithm
which needs a fair search strategy for SLD trees so, as a
first step, I wrote the following program.  It has been
briefly tested on MU-Prolog and C-Prolog.


% Breadth first search of the SLD tree of a goal.
% Returns all solutions.
% The main data structure is a list of 'buds' (starts of
% branches which have not been explored).  Each one has
% an instance of the initial goal and the current goal
% for that branch.  A difference list (X-Y) is used to
% implement a queue of buds.
% -- Lee Naish 6/6/84

?- op(400, xfx, :).     % used for initial_goal:current_goal
?- op(490, xfy, '.'.    % so W:X.Y-Z is ((W:X).Y)-Z

                        % return all solutions to G, using bf
solve(G, Solns) :-
        bf(G:G.X-X, Solns).

% do bf search of SLD tree
bf(X-Y, []) :-
        X == Y,         % d-list of buds is empty
        !.
bf(G:true.X-Y, G.S) :-  % found solution - put it on list
        !,
        bf(X-Y, S).
bf(G:(B1,B).X-Y, S) :-
        !,
        clauses(B1, Cl),
        newbuds(G, B1, B, Cl, Y-Z),
        bf(X-Z, S).
bf(G:B.X-Y, S) :-
        clauses(B, Cl),
        newbuds(G, B, true, Cl, Y-Z),
        bf(X-Z, S).

% expand a bud into a d-list of new buds
newbuds(_, _, _, [], X-X).
newbuds(G, B1, B, (H:-T).C, CG:B2.X-Y) :-
copy(G:(B1,B), CG:(H,CB)),      % copy so branches dont
cappend(T, CB, B2),             % interfere with each other
newbuds(G, B1, B, C, X-Y).

                        % copy a term
copy(X, Y) :-
        assert(tmp(X)),
        retract(tmp(Y)),
        !.

% get list of clauses for call
clauses(true, [(true:-true)]) :- !.
clauses((X;Y), [(X;Y:-X), (X;Y:-Y)]) :- !.

% handle other system preds here
clauses(H, Cl) :-
        all((H:-T), clause(H, T), Cl).

% append for conjunctions (with ',')
cappend(A, true, A) :-
        !.
cappend(true, A, A) :-
        !.
cappend((A,B), C, (A,D)) :-
        !,
        cappend(B, C, D).
cappend(A, B, (A,B)).

?- lib all.     % all solutions predicate from library
                % (could also use bagof)