RE: Who likes whom?

View: New views
4 Messages — Rating Filter:   Alert me  

Parent Message unknown RE: Who likes whom?

by Steve Kirkby :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Anyone? I would really like to know.

Steve Kirkby

>The following does not work - SWI-Prolog just replies "no":

>likes(a,b).
>likes(b,c).
>likes(c,d).

>likes(X,Z) :- likes(X,Y), likes (Y,Z).

>?- likes(a.c).
>no

>1: Why not please?

>2: And how to achieve the results:

>?- likes(a,c).
>yes

>?- likes(a,d).
>no

>A person likes a third person if he likes someone who likes that third person,
but the person should not like a fourth person if he likes a someone who likes a
third person who likes that fourth person - a reasonable model of human
relations.

>When I used micro-prolog in the early 1990s the If-Then worked.

>Thanks, Steve Kirkby.

_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: RE: Who likes whom?

by Bart Demoen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> >likes(X,Z) :- likes(X,Y), likes (Y,Z).
                                  ^
there is a space just before the (
shouldn't be there
please, do not ignore the SWI syntax error messages !

Cheers

Bart Demoen

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: RE: Who likes whom?

by Andrew Koster :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Your predicate won't work. Even if you fix the syntax errors it will result in

?- likes(a,d).
yes

This is quite easy to see. To solve likes(a,d), it will use the clause  
likes(X,Z) :- likes(X,Y), likes(Y,Z) in the following manner:

likes(a,d) :- likes(a, Y), likes(Y,d). It will then try to bind Y to  
anything for which likes(a,Y) holds. It will find likes(a,b) and try  
to verify likes(b,d). It can do this by using that same predicate  
again (recursion).

Recursion is a very powerful tool in Prolog, but it isn't always the  
right tool to use. In this case, rewrite it as follows:

likes2(X,Y) :- likes(X,Y).
likes2(X,Y) :- likes2(X, Z, Y).

likes2(X, Z, Y) :- likes(X,Z), likes(Z,Y).

This way, you can query: likes2(a,c) and it will answer yes. The first  
clause for likes2 will fail (there is no direct likes predicate).  
However, the second clause calls likes2/3. This succeeds for obvious  
reasons.

Similarly, likes2(a,d) will fail, because there is no recursion.

Cheers,
Andrew


Quoting Steve Kirkby <winkley192@...>:

> Anyone? I would really like to know.
>
> Steve Kirkby
>
>> The following does not work - SWI-Prolog just replies "no":
>
>> likes(a,b).
>> likes(b,c).
>> likes(c,d).
>
>> likes(X,Z) :- likes(X,Y), likes (Y,Z).
>
>> ?- likes(a.c).
>> no
>
>> 1: Why not please?
>
>> 2: And how to achieve the results:
>
>> ?- likes(a,c).
>> yes
>
>> ?- likes(a,d).
>> no
>
>> A person likes a third person if he likes someone who likes that  
>> third person,
> but the person should not like a fourth person if he likes a someone  
> who likes a
> third person who likes that fourth person - a reasonable model of human
> relations.
>
>> When I used micro-prolog in the early 1990s the If-Then worked.
>
>> Thanks, Steve Kirkby.
>
> _______________________________________________
> SWI-Prolog mailing list
> SWI-Prolog@...
> https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>



----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: RE: Who likes whom?

by Steve Kirkby :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks. That is really helpful. I would never have thought of including a
second relationship.

Steve Kirkby

On 3/11/09 00:55, "Andrew Koster" <andrew@...> wrote:

> Your predicate won't work. Even if you fix the syntax errors it will result in
>
> ?- likes(a,d).
> yes
>
> This is quite easy to see. To solve likes(a,d), it will use the clause
> likes(X,Z) :- likes(X,Y), likes(Y,Z) in the following manner:
>
> likes(a,d) :- likes(a, Y), likes(Y,d). It will then try to bind Y to
> anything for which likes(a,Y) holds. It will find likes(a,b) and try
> to verify likes(b,d). It can do this by using that same predicate
> again (recursion).
>
> Recursion is a very powerful tool in Prolog, but it isn't always the
> right tool to use. In this case, rewrite it as follows:
>
> likes2(X,Y) :- likes(X,Y).
> likes2(X,Y) :- likes2(X, Z, Y).
>
> likes2(X, Z, Y) :- likes(X,Z), likes(Z,Y).
>
> This way, you can query: likes2(a,c) and it will answer yes. The first
> clause for likes2 will fail (there is no direct likes predicate).
> However, the second clause calls likes2/3. This succeeds for obvious
> reasons.
>
> Similarly, likes2(a,d) will fail, because there is no recursion.
>
> Cheers,
> Andrew
>
>
> Quoting Steve Kirkby <winkley192@...>:
>
>> Anyone? I would really like to know.
>>
>> Steve Kirkby
>>
>>> The following does not work - SWI-Prolog just replies "no":
>>
>>> likes(a,b).
>>> likes(b,c).
>>> likes(c,d).
>>
>>> likes(X,Z) :- likes(X,Y), likes (Y,Z).
>>
>>> ?- likes(a.c).
>>> no
>>
>>> 1: Why not please?
>>
>>> 2: And how to achieve the results:
>>
>>> ?- likes(a,c).
>>> yes
>>
>>> ?- likes(a,d).
>>> no
>>
>>> A person likes a third person if he likes someone who likes that
>>> third person,
>> but the person should not like a fourth person if he likes a someone
>> who likes a
>> third person who likes that fourth person - a reasonable model of human
>> relations.
>>
>>> When I used micro-prolog in the early 1990s the If-Then worked.
>>
>>> Thanks, Steve Kirkby.
>>
>> _______________________________________________
>> SWI-Prolog mailing list
>> SWI-Prolog@...
>> https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>>
>
>
>
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
>
> _______________________________________________
> SWI-Prolog mailing list
> SWI-Prolog@...
> https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog