6 Replies - 1356 Views - Last Post: 09 June 2010 - 08:36 AM Rate Topic: -----

#1 beibei  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-June 10

...Programming in Prolog

Posted 05 June 2010 - 02:55 AM

Dear...

I have done my code in prolog but it is still not the same as right answer so i just wondering that what wrong with my code shown as following...

This is knowledge base
cat(ann)
cat(on)
feline(bee)
feline(gara)

-------
THis is my code in database(bluefish)
[code]
prove(cat(ann)).
prove(cat(on)).
prove(feline(bee)).
prove(feline(gara)).

prove(cat(X)) :- prove(feline(X)).
prove(feline(X)) :- prove(cat(X)).


test :- write('A cat is a feline and a feline is a cat:'), nl;
cat(X);
feline(X);
write(X), nl, fail.
test.
-------
THis is my result in prolog program(pl)
?- prove(cat(X)).
X = ann ;
X = on ;
X = bee ;
X = gara ;
X = ann ;
X = on ;
X = bee ;
X = gara ;
X = ann ;
X = on ;
X = bee .

?- prove(feline(X)).
X = bee ;
X = gara ;
X = ann ;
X = on ;
X = bee ;
X = gara ;
X = ann .

?- test.
A cat is a feline and a feline is a cat:
true .
----------------

The result appears continuous term until i press (.) it will stop. However,i donot want it like this,
i would like the result like

?- prove(cat(X)).
X = ann ;
X = on ;
X = bee ;
X = gara .
false

?- prove(feline(X)).
X = bee ;
X = gara ;
X = ann ;
X = on ;
false

and
?-test.
A cat is a feline and a feline is a cat:
ann
on
bee
gara
false.

--------------i have fixed it but it was still output similar like ever so i got confuse

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: ...Programming in Prolog

#2 Vestah  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 86
  • Joined: 15-October 09

Re: ...Programming in Prolog

Posted 05 June 2010 - 08:09 AM

Hey beibei,

Are the any prolog operators you are not allowed to use such as ; and \+ (disjunction and negation as failure)?
Anyway the main problem is how to tackle the cyclic behavior.
To illustrate how it works I have placed clause numbers on your program:
1. prove(cat(ann)).
2. prove(cat(on)).
3. prove(feline(bee)).
4. prove(feline(gara)).

5. prove(cat(X)) :- prove(feline(X)).
6. prove(feline(X)) :- prove(cat(X)).


Let me walk you through what happens if the ?- prove(cat(X)). goal clause is given:
  • It tries to unify with clause 1 which can be done with X = ann.
  • The body is empty so it immediately succeeds. If you choose to search for another solution: (The ; key indicates that)
  • Clause 1 cannot be unified to anything else so it goes on to the next clause.
  • It tries to unify with clause 2 which can be done with X = on.
  • The body is empty so it immediately succeeds. If you choose to search for another solution: (The ; key indicates that)
  • Clause 2 cannot be unified to anything else so it goes on to the next clause.
  • It tries to unify prove(cat(X)) with prove(feline(bee)).
  • There are no most general unifier so the it goes on to the next clause.
  • Clause 4 is a repeat of clause 3 and moves on to clause 5 without succeeding. I think you have started to spot a pattern.
  • It tries to unify with clause 5 which can be trivially done since prove(cat(X)) = prove(cat(X)).
  • It tries to satisfy the body which states that prove(feline(X)) must hold.

  • We now start from clause 1 with prove(feline(X)).
  • Clause 1 does not succeed.
  • Clause 2 does not succeed.
  • Clause 3 succeeds once with X = bee.
  • Clause 4 succeeds once with X = gara.
  • Clause 5 does not succeed. And now we come to clause 6.
  • It tries to unify with clause 6 which can be trivially done since prove(feline(X)) = prove(feline(X)).
  • It tries to satisfy the body which states that prove(cat(X)) must hold.

  • We now start from clause 1 with prove(cat(X)).
  • Clause 1 succeeds with X = ann.
  • ...


I hope you clearly see what the problem is with your approach.
I will leave you with the problem to work some more on it.
Just ask if you still can't solve it and I'll try to help you some more.

This post has been edited by Vestah: 05 June 2010 - 08:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_beibei*


Reputation:

Re: ...Programming in Prolog

Posted 06 June 2010 - 03:24 AM

First of all i am grateful with your help.
As your question Are the any prolog operators you are not allowed to use such as ; and \+ (disjunction and negation as failure)?
Ans. I can use every operators that bring the accomplish result.

I am still confuse about your answer because i cannot solve it ,anyway i will try.

Thank you
Was This Post Helpful? 0

#4 Vestah  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 86
  • Joined: 15-October 09

Re: ...Programming in Prolog

Posted 06 June 2010 - 05:10 AM

It's good to know that there are no limits on the operators you can use.
I believe if I give you a solution you will say 'of course' and not learn very much.
Just keep on trying. Solving this problem is hard if you don't think about it the right way.

Lemme make a small change to your code:
prove(cat(X)) :- cat(X).
prove(feline(bee)).
prove(feline(gara)).

prove(cat(X)) :- prove(feline(X)).
prove(feline(X)) :- prove(cat(X)).


What is the effect of this change? How does it work differently from your code?

This post has been edited by Vestah: 06 June 2010 - 05:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_beibei*


Reputation:

Re: ...Programming in Prolog

Posted 07 June 2010 - 05:45 PM

Dear Vestah

Thank your for being helpful so this following code is mine so far

cat(sido).
cat(lasi).
feline(dore).
feline(remi).

prove(cat(X)) :- cat(X).
prove(feline(X)) :- feline(X).
prove(cat(X)) :- prove(feline(X)).
prove(feline(X)) :- prove(cat(X)).


test :- write('A cat is a feline and a feline is a cat:'),
        prove(cat(X)),     
        write(X),
        nl,
        fail.



It is still run non-stop but i am fixing it anyway. ^^
Was This Post Helpful? 0

#6 Guest_beibei*


Reputation:

Re: ...Programming in Prolog

Posted 07 June 2010 - 05:49 PM

PS. probably make you confuse ,i just change a bit of name of cat and feline.

sido
lasi
dore
remi
Was This Post Helpful? 0

#7 Vestah  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 86
  • Joined: 15-October 09

Re: ...Programming in Prolog

Posted 09 June 2010 - 08:36 AM

You still have the problem with the cycle, but let me start with talking a bit about the effect of your change. Let us consider two code examples:

Example 1:
cat(sido).
cat(lasi).
feline(dore).
feline(remi).

prove(cat(sidi)).
prove(cat(lasi)).
prove(feline(dore)).
prove(feline(remi)).


Example 2:
cat(sido).
cat(lasi).
feline(dore).
feline(remi).

prove(cat(X)) :- cat(X).
prove(feline(X)) :- feline(X).


Notice that the prove predicate in example 1 do not use the knowledge base for anything. Rather, it redefines the knowledge base. This is not a good design because whenever you modify or extend the knowledge base you have to modify the knowledge base as well. Another issue is illustrated in the code example. I wrote prove(cat(sidi)) while it should have been prove(cat(sido)). In other words you have to be careful not to introduce inconsistencies.
In example 2 the prove predicate uses the knowledge base. Want a new cat? Just introduce it to the knowledge base. The issues I mentioned with example 1 are not present.


Before I help you with the cyclic problem I would like you to introduce a two new predicates:
  • proveCat which proves that a cat is a cat. It does not consider a feline as being a cat.
  • proveFeline which proves that a feline is a feline. It does not consider a cat as being a feline.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1