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

Time:10-04

I am trying to override the fuel and wheels properties using the rules below :-

rel(land, subset, vehicles).
rel(aquatic, subset, vehicles).

rel(aircraft, subset, vehicles).

property(wheels, land, 4).
property(wheels, aquatic, 0).
property(wheels, aircraft, 3).

property(fuel, land, gas).
property(fuel, aquatic, gas).
property(fuel, aircraft, gas).

rel(trian, subset, land).
rel(car, subset, land).

property(wheels, train, 8).

rel(silver_bullet, isa, car).
rel(f150_lightening, isa, car).
rel(opal, isa, car).
rel(pontiac_grand_prix, isa, car).

property(fuel, f150_lightening, electirc).

rel(qe2, isa, aquatic).
rel(multnomah, isa, aquatic).
property(wheels, multnomah, 1).

rel(submarine, subset, aquatic).
rel(peral, isa, submarine).
property(fuel, peral, electirc).

rel(air_force_one, isa, aircraft).

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

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

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

But I am getting a wrong value for aquatic vehicles wheels , which should be inherited as 0 but instead I am getting 4 , why is this happening ?

17 ?- hasproperty(wheels, qe2, Z).
Z = 4 .

It should be Z= 0

CodePudding user response:

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.

CodePudding user response:

Load your code in 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.

  • Related