🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: net.lang.prolog
3 messages
3 total messages Started by Chuck Restivo, The Moderator Sat, 23 Aug 1986 14:02
PROLOG Digest V4 #41
#745
Author: Chuck Restivo, The Moderator
Date: Sat, 23 Aug 1986 14:02
205 lines
6635 bytes
PROLOG Digest            Monday, 25 Aug 1986       Volume 4 : Issue 41

Today's Topics:
                        Query - Fixed Points,
                       Implementation - Bagof,
                     Puzzle - Knights and Knaves
----------------------------------------------------------------------

Date: Thu 14 Aug 86 19:40:02-EDT
From: Paul G. Weiss <PGW@XX.LCS.MIT.EDU>
Subject: Fixed Points

The familiar non-trivial LISP fixed point is:

((lambda (X) (list X (list (quote quote) X)))
 (quote (lambda (X) (list X (list (quote quote) X)))))


What can be done for Prolog?  Ideally, I would like to find a
one-clause predicate fixed/1, with the property that fixed(Term)
is true of the Term which is the clause that defines fixed/1.

So far the best I can do is a pair of predicates foo/0 and b/1, each
of one clause such that typing "foo" at the ?- prompt causes the two
defining clauses to be written out.  I am unhappy with this since it
uses write and writeq:

foo :- b((
        b(X) :-
                writeq((foo :- b(X))), write('. '), nl,
                writeq(X), write('. '), nl
        )).

b(X) :-
        writeq((foo :- b(X))), write('. '), nl,
        writeq(X), write('. '), nl.

Can such a predicate be defined ?

------------------------------

Date: 20 Aug 86 14:10:37 GMT
From: Ramesh Krishnamurti <mcvax!ukc!cstvax!edcaad!ramesh@seismo>
Subject: Dave Plummer's bagof query

In reponse to Dave Plummer's query, the way bag_of(X,P,B) works
in C-Prolog and hence (?) ought to in DEC-10 Prolog is to

find to all instances of X such that P is provable with the
condition that any variables in P not X are bound.

Thus for the program,

p(1,2).
p(1,1).
p(2,_).
p(3,_).
p(4,4).

bagof(X,p(X,Y),B) will, on backtracking, successively generate
the lists

[3,2,1] with Y bound to 2
[1]     with Y bound to 1
[4]     with Y bound to 4

Ideally, it should produce the lists

[3,2,1] with Y bound to 2
[3,2,1] with Y bound to 1
[4,3,2] with Y bound to 4

But thats asking too much of Prolog !

If you want the variables in P not in X to remain free then
they must be explicitly bound by an existential quantifier ^.

That is,

bagof(X,Y^p(X,Y),B) will generate the list

[4,3,2,1,1] with Y bound to _

If you don't want to be bothered with specifying the free
variables the following version of bagof works.

%-------------------------------------------------------------
bag(_,_,_):-            asserta($bag(base,_)),
                        fail.
bag(X,P,B):-            Pred,
                        asserta($bag(item,X)),
                        fail.
bag(_,_,B):-            $gather([],B).


$gather(C,B):-          retract($bag(Tag,X)),
                        !,
                        $gather(Tag,X,C,B).


$gather(base,_,B,B):-   !.
$gather(item,X,C,B):-   !,
                        $gather([X|C],B).
%-------------------------------------------------------------

However, for the database:

drinks(tom,lager).
drinks(dick,bitter).
drinks(harry,bitter).
drinks(bill,lager).
drinks(jack,lager).
drinks(bill,bitter).

bag(X,drinks(X,Y),B) will produce

B = [tom,dick,harry,bill,jack,bill] with Y bound to

whereas

bagof(X,drinks(X,Y),B) will successively produce

B = [jack,bill,tom]    with Y = lager
B = [bill,harry,dick]  with Y = bitter

Take your pick.

-- Ramesh

------------------------------

Date: 18 Aug 86 22:26:39 GMT
From: David Fiore <ucdavis!ucrmath!hope!fiore@Berkeley>
Subject: Knights and Knaves

This is an attempt at writting a Prolog program and I have come to a
point where I don't know why the program doesn't work and I have run
out of ideas on how to make it work. Could anyone help me out with
this?

Thank you,

-- David Fiore

/* This program is supposed to solve a logic puzzle I read in R.
   Smullyn's book "What Is The Name Of This Book?".  The problem
   is as follows:

           On a certain island in the pacific is an island
      of knights and knaves.  Now knights by nature allways
      tell the truth, as far as they know it, while knaves
      by nature allways tell lies, as far as they know it.
      A visitor to the Island of Knights and Knaves comes
      across three inhabitants of the island standing together
      in a garden.  We will call them Larry, Curly, and Moe.
      The stranger says to Larry: "Are you a knight or a
      knave?".  Larry answers but, rather indistinctly, so the
      stranger could not make out what Larry said.  So the
      stranger says to Curly: "What did Larry say?".  Curly
      says: "Larry said that he was a knave."  At this point,
      the third man Moe says: "Don't believe Curly, he's lying!"
      The question is what are Curly and Moe?

It is immediately obvious that whatever Curly is, Moe is the
opposite.  What is not immediately obvious to everyone is that
independant of Moe's statement, we can see that Curly is lying
because it is impossible for any inhabitant to claim to be a
knave.  We can therefore deduce that Curly is a knave and Moe is
a knight.

*/
is_knight (X) :-
   is_knave (X),!,false.

is_knight (X) :-                /* Someone is a knight if he */
   says (X, Y), Y.              /* says something and that */
                                /* thing is true. */

is_knave (X) :-
   is_knight (X), !, false.

is_knave (X) :-                 /* Someone is a knave is he */
   says (X, Y), not (Y).        /* says something and that */
                                /* thing is not true. */

legal (X, Y) :-
   Y, is_knight (X).
legal (X, Y) :-
   not (Y), is_knave (X).

says (larry, Z):-               /* We don't know what he said */
   legal (larry, Z).


/* Says larry claims knavehood */
says (curly, says (larry, is_knave (larry))).

/* Says curly is a knave */
says (moe, is_knave (curly)).

------------------------------

End of PROLOG Digest
********************
Re: fixed points
#752
Author: Bjorn Danielsson
Date: Sat, 30 Aug 1986 06:48
47 lines
2263 bytes
In article <8608250740.AA00215@ucbvax.Berkeley.EDU>
 PGW@XX.LCS.MIT.EDU (Paul G. Weiss) writes:
>Ideally, I would like to find a one-clause predicate fixed/1, with the
>property that fixed(Term) is true of the Term which is the clause that
>defines fixed/1.

I have found a solution, but it requires an extra predicate that interprets
a "represented" clause and melts it into a lower level of representation.
Using an extra predicate might seem like cheating, especially if you write
something like:

   fixed(X) :- cheat(X).   cheat((fixed(X) :- cheat(X))).

However, the extra predicate in my solution contains no information about
the problem. The program follows:

fixed(X) :- Y = (-z = (fixed(quote(-x)) :- quote(-y) = build_quote(-y),
                                           -y,
                                           melt(quote(-z),quote(-x)))),
            Z = (fixed(-x) :- -y = quote(Y),
                              Y,
                              melt(-z,-x)),
            melt(Z,X).

% melt(X,Y) interprets the representation X of a clause fragment,
% and unifies Y with the most general term it represents.
% -(foo) represents the variable _foo.
% quote(C) represents the constant C.
% build_quote(X) represents the term quote(Y), where X is a term
% that represents Y.
% Everything else has its usual meaning.

melt(X,Y) :- melt(X,Y,_).
melt(quote(X),X,_).
melt(build_quote(X),quote(Y),L) :- melt(X,Y,L).
melt(-X,V,L) :- member([X|V],L).
melt([A|B],[X|Y],L) :- melt(A,X,L), melt(B,Y,L).
melt([],[],_).
melt(A,X,L) :- A=..[F|B], melt(B,Y,L), X=..[F|Y].
melt(A,A,_).

member(X,[Y|Z]) :- X=Y; member(X,Z).

% End of program

-- 
Bjorn Danielsson, ZYX, +46 8 653205, ...mcvax!enea!zyx!bd
Re: fixed points
#754
Author: Jamie Andrews
Date: Tue, 02 Sep 1986 17:04
27 lines
1736 bytes
>In article <8608250740.AA00215@ucbvax.Berkeley.EDU>
> PGW@XX.LCS.MIT.EDU (Paul G. Weiss) writes:
>>Ideally, I would like to find a one-clause predicate fixed/1, with the
>>property that fixed(Term) is true of the Term which is the clause that
>>defines fixed/1.

In article <177@zyx.UUCP> bd@zyx.UUCP (Bjorn Danielsson) writes:
>I have found a solution, but it requires an extra predicate that interprets
>a "represented" clause and melts it into a lower level of representation.
>Using an extra predicate might seem like cheating, especially if you write
>something like:
>
>   fixed(X) :- cheat(X).   cheat((fixed(X) :- cheat(X))).

     Actually, the original query was framed so loosely that even this
program works:

fixed(X).

     I think Paul Weiss probably wanted to say "fixed(Term) is true ONLY
of Terms which are clauses defining a predicate extensionally identical
to fixed(Term)."  Can the people who proposed solutions prove that their
solutions have this property?

--Jamie.
...!seismo!ubc-vision!ubc-cs!andrews
"Two sapphires and a couple rows of pearls"
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads