🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #1041

Re: Finding all solutions in Prolog

#1041
From: Richard A. O'Keefe
Date: Mon, 06 Aug 1990 06:15
56 lines
3024 bytes
In article <90216.112514F0O@psuvm.psu.edu>, F0O@psuvm.psu.edu writes:
>      If you have a simple PDC prolog program like:
>           predicates
>                person(symbol)
> 
>           clauses
>                person(jack).
>                person(jill).
>                person(mary).
> and from the goal prompt type: person(X), you will get all 3 persons.

That is a rather nasty peculiarity of PDC Prolog.  Most sensible Prologs
give you one answer at a time, and you have to type something to get the
next answer.  That is, after all, the thing which distinguishes Prolog
from a relational query language:  the language really does find ONE
solution at a time.

> But if you have an internal goal like:
>          person(X), write(X),nl.
> 
> Prolog will only find the first person.

Not quite.  Prolog will find the first solution, and report it, leaving
a suspension ready to look for the next solution.  When that suspension
is activated (by being "failed into") it will look for another solution.
(Actually, another proof.)  Let's face it, a Prolog variable can only be
bound to ONE value, so Prolog _can_ only report one solution at a time.

>      Why does Prolog only find the first solution on internal goals?

Because that's how Prolog works.  You could have a query language
(such as Aditi or NAIL!) which uses Prolog-like syntax but which notionally
finds all solutions, no reason why not.  But it wouldn't be Prolog.  The
point of Prolog is to provide a fast cheap programming language which works
pretty much like Lisp or Pascal, and to do that it works by finding one
proof at a time, using backtracking to go on to the next one.

>      I can see that only finding one solution and then using the fail
> predicate gives you control over the search, however you could also use
> the cut if you wanted to limit the search.

But you can only use the cut to control the search BECAUSE Prolog finds
one solution at a time; the cut has the effect of preventing other solutions
being looked for.  How could you stop other branches of the tree being
explored if they had already been explored?

>      Am I missing something fundamental here?

You have been confused by a poorly chosen interface in PDC ``Prolog''.
(It is not uncommon for a top level query to have _millions_ of answers;
it really is _not_ helpful to display them all.)

-- 
Distinguishing between a work written in Hebrew and one written in Aramaic
when we have only a Latin version made from a Greek translation is not easy.
(D.J.Harrington, discussing pseudo-Philo)
Technical Details
Message-ID: <3520@goanna.cs.rmit.oz.au>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!munnari.oz.au!goanna!ok
References: <90216.112514F0O@psuvm.psu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!munnari.oz.au!goanna!ok
From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
Newsgroups: comp.lang.prolog
Subject: Re: Finding all solutions in Prolog
Message-ID: <3520@goanna.cs.rmit.oz.au>
Date: 6 Aug 90 06:15:49 GMT
References: <90216.112514F0O@psuvm.psu.edu>
Organization: Comp Sci, RMIT, Melbourne, Australia
Lines: 56