1

Using prolog make a code to get some compared outputs but some outputs not working properly. seems those are not comparing with facts. here the code

fact(Fact) :- Fact,!.
fact(Fact):-Fact = ..[Rel, Arg1, Arg2],isa(Arg1, SuperArg).
SuperFact =..[Rel,SuperArg,Arg2].
covers(animal,skin).
isa(fish,animal).
isa(bird,animal).
isa(mammal,animal).
isa(shark,fish).
isa(salmon,fish).
isa(parrot,bird).
isa(penguin,bird).
speacial_organ(fish,gills).
travel(fish,swim).
birth(bird,lay_eggs).
special_organ(bird,wings).
travel(bird,fly).
birth(fish,lay_eggs).
birth(mammal,not_lay_eggs).
birth(shark,not_lay_eggs).
nature(shark,dangerous).
food(salmon,delicacy).
colour(parrot,green).
travel(penguin,walk).

this are the questions i want to find answers from this program

• Can parrot fly?
• What is the color of parrot?
• Do parrots have skin?
• Are sharks dangerous?

repeat
  • 18,496
  • 4
  • 54
  • 166
Chara
  • 31
  • 10

1 Answers1

3

Step one: define the reflexive of isa/2 (named is_a/2 in the following).

isa(fish,animal).
isa(bird,animal).
isa(mammal,animal).
isa(shark,fish).
isa(salmon,fish).
isa(parrot,bird).
isa(penguin,bird).

is_a(X, Y) :-
   closure0(isa, X, Y).

Step two: specify the remaining facts (grouped by predicate indicator).

covers(animal,skin).

special_organ(fish,gills).
special_organ(bird,wings).

travel(fish,swim).
travel(bird,fly).
travel(penguin,walk).

birth(bird,lay_eggs).
birth(fish,lay_eggs).
birth(mammal,not_lay_eggs).
birth(shark,not_lay_eggs).

nature(shark,dangerous).

food(salmon,delicacy).

colour(parrot,green).

Step three: let's ask some queries!

  • Can parrots fly?

    ?- is_a(parrot, X), travel(X, fly).
       X = bird
    ;  false.
    
  • What is the color of parrots?

    ?- is_a(parrot, X), colour(X, Colour).
       Colour = green, X = parrot         
    ;  false.
    
  • Do parrots have skin?

    ?- is_a(parrot, X), covers(X, skin).
       X = animal
    ;  false.
    
  • Are sharks dangerous?

    ?- is_a(shark, X), nature(X, dangerous).
       X = shark
    ;  false.
    
Community
  • 1
  • 1
repeat
  • 18,496
  • 4
  • 54
  • 166
  • thanks for ur answer but why parrot can fly is false.. Parrot is a bird and bird can fly then why its getting false... Like vice shark is dangerous also same – Chara Dec 23 '15 at 12:04
  • @Chara. Do not get too excited about answers like `true ; false`. Read http://stackoverflow.com/questions/5013323/understanding-rules-false-as-answer . – repeat Dec 23 '15 at 12:20
  • But with that given facts it will be true.. Can u check why this all getting false – Chara Dec 23 '15 at 12:24
  • @Chara. Look closely! The binary operator `;` expresses "logical or"... Because of this `X = foo ; false` is logically equivalent to `X = foo`. – repeat Dec 23 '15 at 12:31
  • Are redundant answers by `is_a/2` intended? – false Dec 23 '15 at 12:32
  • Yes. got thanks my friend. how to get all answers like this is_a(parrot, X), colour(X, Colour). Colour = green, X = parrot ; – Chara Dec 23 '15 at 12:39
  • answer will be like Colour = green like shark = dangerous – Chara Dec 23 '15 at 12:41
  • Yes your answer is correct. but any other way to ask that questions to get answer like shark = dangerous – Chara Dec 23 '15 at 12:56
  • @false. No. Initially I wanted to use `closure0/3`, but I made some errors... will edit! btw closure0 should have some redundant answers, too. memoization? – repeat Dec 23 '15 at 13:03
  • @repeat: It would mean to go a very steep step further ... Nice only as long as terms are ground. – false Dec 23 '15 at 13:25
  • "but I made some errors".. ok thanks friend when u edited to correct code let me know. i very close to answers with ur support.. – Chara Dec 23 '15 at 13:25
  • and how to use cut operator to this code for stop coming false. – Chara Dec 23 '15 at 13:33
  • Ok will do. hope u got what i need exactly – Chara Dec 23 '15 at 13:47
  • i check with that code given this errors 1 ?- is_a(parrot, X), travel(X, fly). ERROR: is_a/2: Undefined procedure: closure0/3 Exception: (9) closure0(isa, parrot, _G1369) ? creep – Chara Dec 23 '15 at 13:52
  • @Chara. Include the source code of `closure0/3` (follow the link to the related question). – repeat Dec 23 '15 at 13:54
  • @Chara. Reload with `?- make.` – repeat Dec 23 '15 at 14:21
  • @repeat yes i did.. But same problem.. Can u edit with full code then i can copy and past to test – Chara Dec 23 '15 at 14:48
  • @Chara. Part1: `closure0(R_2,X0,X) :- closure0(R_2,X0,X,[X0]). closure0(_,X,X,_). closure0(R_2,X0,X,Xs):-call(R_2,X0,X1),maplist(dif(X1),Xs), closure0(R_2,X1,X,[X1|Xs]).` – repeat Dec 23 '15 at 15:14
  • @Chara. Part2: `is_a(X,Y):-closure0(isa,X,Y). isa(fish,animal). isa(bird,animal). isa(mammal,animal). isa(shark,fish). isa(salmon,fish). isa(parrot,bird). isa(penguin,bird). covers(animal,skin). special_organ(fish,gills). special_organ(bird,wings). travel(fish,swim). travel(bird,fly). travel(penguin,walk). birth(bird,lay_eggs). birth(fish,lay_eggs). birth(mammal,not_lay_eggs). birth(shark,not_lay_eggs). nature(shark,dangerous). food(salmon,delicacy). colour(parrot,green).` – repeat Dec 23 '15 at 15:15
  • 1
    Yes not its run without errors but answers are not for exact questions i need.. need to get answers like Colour = green ; 1 ?- is_a(parrot, X), travel(X, fly). X = bird ; false. 2 ?- is_a(parrot, X), colour(X, Colour). X = parrot, Colour = green ; false. 3 ?- is_a(parrot, X), covers(X, skin). X = animal ; false. 4 ?- is_a(shark, X), nature(X, dangerous). X = shark ; false. – Chara Dec 23 '15 at 15:21
  • @Chara. Please give one full concrete example. – repeat Dec 23 '15 at 16:33
  • @Chara. Please work more diligently. Reading sentences like "Yes not its run without errors" is not fun. – repeat Dec 23 '15 at 16:35
  • @Chara. Would you care to share some of them? – repeat Dec 23 '15 at 16:39
  • Yes. i change the question asking type. wait i upload full details. – Chara Dec 23 '15 at 16:40