LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2013, 07:22 AM   #1
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Question gprolog: having trouble getting rule to work


I don't know whether this is a bug, or something I've misunderstood. I came across prolog in a book I was reading, so decided to give it a try. I installed the default gprolog package in Debain Wheezy (version 1.3.0), and was trying some basic examples. What I've found, though, is that "not equals" works with atoms, but not with variables.

In 'family.pl', I have the following:
Code:
person(becky).
person(frances).
person(john).
person(jules).

father(john, jules).
father(john, becky).
mother(frances, jules).
mother(frances, becky).

parent(X,Y):- father(X,Y).
parent(X,Y):- mother(X,Y).

sibling(X,Y):- \+(X = Y),
        parent(Z,X),
        parent(Z,Y).
Querying sibling gives me:
Code:
| ?- sibling(becky,jules).

true ? ;

true ? ;

no
| ?- sibling(Who,jules).

no
| ?-
However, if I replace \+(X = Y) with (X = Y), it is able to tell me that jules is jules' own sibling:
Code:
| ?- sibling(becky,jules).

no
| ?- sibling(jules,jules).

true ? ;

true ? ;

no
| ?- sibling(Who,jules).

Who = jules ? ;

Who = jules ? ;

no
| ?-
Is it something I'm doing, or should I try a different version of Prolog?
 
Old 10-12-2013, 10:05 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
(=)/2 doesn't mean equal, it means unifiable. A variable always unifies with an atom so Who = jules succeeds. I think you want (==)/2 (term identical), or probably (\==)/2 (term not identical).
 
Old 10-12-2013, 02:37 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 97
Unhappy

Thanks for the response, ntubski. It still seems to be misbehaving when it comes to variables, though:

Code:
| ?- [user].
compiling user for byte code...
sibling(X,Y):- (X \== Y),
  parent(Z,X),
  parent(Z,X).

user compiled, 4 lines read - 489 bytes written, 61657 ms
warning: user:1: redefining procedure sibling/2
         /home/rob/prolog/family.pl:14: previous definition

yes
| ?- sibling(jules,becky).

true ? ;

true ? ;

no
| ?- sibling(jules,jules).

no
| ?- sibling(Who,jules).

Who = jules ? ;

Who = becky ? ;

Who = jules ? ;

Who = becky ? ;

yes
| ?-
Does look like a bug...

Update:
Or not so much, now. Just tried with swi-prolog instead, and am getting the same behaviour. *confused*

Last edited by Robhogg; 10-12-2013 at 03:02 PM.
 
Old 10-12-2013, 04:04 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Ah, the problem is that you are the checking X (or Who) before it has been unified:
Code:
| ?- ['/home/npostavs/tmp/sib.prolog'].
compiling /home/npostavs/tmp/sib.prolog for byte code...
/home/npostavs/tmp/sib.prolog compiled, 17 lines read - 1533 bytes written, 38 ms

yes
| ?-trace.
The debugger will first creep -- showing everything (trace)

yes
{trace}
| ?-sibling(Who, jules).
      1    1  Call: sibling(_16,jules) ? 
      2    2  Call: _16\==jules ? s
      2    2  Exit: _16\==jules ? s
      3    2  Call: parent(_110,_16) ? s
      3    2  Exit: parent(john,jules) ? l

Who = jules ? 

(4 ms) yes
{trace}
_16 is Who, and _110 is Z. You can see _16 is unified to jules after it was checked. Moving that check to the end seems to work.
 
1 members found this post helpful.
Old 10-12-2013, 04:46 PM   #5
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 97
Thanks, ntubski. Yes, that fixed it. But tricky...
 
  


Reply

Tags
gnu, gprolog, prolog



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gprolog Slack32 not compiling (Compiles fine on Slack64) Gavin Harper Slackware 4 09-17-2010 02:33 PM
simple DNAT iptables rule doesn't work. firatkucuk Linux - Networking 2 10-22-2007 01:35 AM
writing to a file with gprolog manolakis Programming 0 06-21-2007 02:48 PM
Maths predicates for gprolog manolakis Programming 6 06-12-2007 11:39 AM
GProlog installation manolakis Linux - Newbie 1 01-16-2007 09:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration