vrc
vrc

Reputation: 3

My override using inheritance in Prolog is inheriting the wrong property ? why?

I am trying to override the color properties using the rules below :-

prop(color, bmw, yellow).
prop(color, audi, red).

maps(mycar, subset, bmw).

maps(hiscar, isa, audi).

%Rules
hasproperty(Property, Object, Value) :-
    maps(Property, Object, Value).

hasproperty(Property, Object, Value) :-
   maps(Object, subset, Parent);
   hasproperty(Property, Parent, Value),
   maps(Property, Ojbect, _).

hasproperty(Property, Object, Value) :-
   maps(Object, isa, Parent),
   hasproperty(Property, Parent, Value),
   maps(Property, Ojbect, _).

But I am getting a wrong value , which should be inherited as red but instead I am getting yellow , why is this happening ?

17 ?- hasproperty(color, hiscolor, Z).
Z =  yellow.

It should be Z= red

Upvotes: 0

Views: 125

Answers (2)

TessellatingHeckler
TessellatingHeckler

Reputation: 28963

Load your code in SWISH online, and click on the left of the line numbers to set tracepoints (red dots) and then submit your query. You can step through the code bit by bit and see what's happening.

SWISH trace of the questioner's code

It looks for wheels on the qe2, and finds none.

Then it looks for the qe2 as a subset of anything, nope.

Then it looks for anything with wheels, and finds land has 4 wheels (??).

Then it looks for anything with wheels (because of a typo of Ojbect it doesn't look for the qe2 - thanks rajashekar! I was stuck why that was behaving strangely).

It found land has 4 wheels, and there exists something with wheels, so that becomes the first answer.

Your code here:

hasproperty(Property, Object, Value) :-
   rel(Object, subset, Parent);
   hasproperty(Property, Parent, Value),   <----- wheels, land, 4
   property(Property, Ojbect, _).          <--typo Ojbect

So the 4 comes from land.

Upvotes: 1

rajashekar
rajashekar

Reputation: 3753

Your program has many errors, you should read the warnings after loading the file into prolog.

  1. Why are you using ; in the second clause of hasproperty predicate?
  2. You spelled Object as Ojbect in the last two clauses.
  3. Why are you asserting property(Property, Ojbect, _) in the last two clause at all?
  4. Unless you have a very good reason, you should define all clauses of a predicate in one place. Do not intersperse rel and property like that. And if you have a good reason to do it, you must use discontiguous to declare them as such.

Prolog should have warned you about 2 and 4.

Upvotes: 2

Related Questions