Reputation: 197
I am trying to create a semantic network that looks a bit like this: .
So far I have just set up the basic facts, such as isa(man,person)
or hasa(person,node)
.
If I create instances of a Person, such as isa(mark,father)
and then look up height(mark,Height)
I get false. I understand that I have to somehow further develop the definition for height(Person,Height)
as well as isa(X,Y)
and hasa(X,Y)
but am unsure how to go about this.
What I would like to happen is to be able to be able to look up, for example, height(mark,Height)
and get 195
, or hasa(mark,X)
and get body
.
Upvotes: 1
Views: 406
Reputation: 1915
For the first issue you need a rule with variables. In words you can specify the rule like
If X is of type T and things of type T have height H then X has height H.
in this case
If mark is a father and fathers have height 195 then mark has height 195.
In Prolog:
height(X,H) :- isa(X,T), height(T,H).
For the second issue you need a recursive rule. In words:
If X is of type T and things of type T have element E then X has element E.
Rephrased:
X has element E if X is of type T and a thing of type T has element E.
You can see from the second statement that the rule for has element contains itself in the definition. This is recursion, and it allows the rule to expand itself automatically into multiple levels. In the current example the first expansion would be:
X has element E if X is of type T and a thing of type T is of type Z and a thing of type Z has element E.
In the example this would be:
X has a body if X is a father and a father is a man and a man has a body.
This expansion can continue as long as there are isa
statements with the right values available (in this case we need one more expansion). The last expansion does not use the rule but matches on a simple fact (in this case hasa(person,body)
). In prolog:
hasa(X,E) :- isa(X,Z), hasa(Z,E).
Full example:
% isa: general facts
isa(father,man).
isa(man,person).
% isa: specific facts
isa(mark,father).
% height: general facts
height(father,195).
% height: rules
height(X,H) :- isa(X,T), height(T,H).
% hasa: general facts
hasa(person,body).
% hasa: rules
hasa(X,E) :- isa(X,Z), hasa(Z,E).
Queries:
?- height(mark,Height).
Height = 195 .
?- hasa(mark,X).
X = body .
Upvotes: 3