From Wikibooks, open books for an open world
< Introspector
Jump to navigation Jump to search

This is a short example based tutorial of Knowledge Interchange Format. It's not complete nor very correct, but it should give you an idea about what you can do with KIF.

The knowledge base I'm using here is a simple representation of human relations. Basically two types of relation exist: family and friends. There are three kinds of family: father, mother, child. There is only one kind of friend. For our purposes partners are also friends. Let's start by entering some data:

   (male Fred)
   (male John)
   (female Martha)
   (female Joan)
   (female Betty)
   (female Alice)

Fred and John are males, Martha, Joan, Betty and Alice are females.

   (father Fred John)

John is the father of Fred

   (mother Fred Martha)

Martha is the mother of Fred

   (father Pete Fred)

Fred is the father of Pete

   (mother Pete Joan)

Joan is the mother of Pete

   (friend Pete Betty)

Betty is a friend of Pete

   (mother Alice Martha)

Martha is the mother of Alice

   (mother Betty Alice)

Alice is the mother of Betty

Now it's nice if we could get some information from these relations. For example _we_ know Fred is the child of Pete, but the computer doesn't. Let's tell him that.

   (defrelation child (?p ?c) :=
     (or (mother ?c ?p) (father ?c ?p)))

Know we say that somebody, called ?c for now, is a child of somebody, called ?p for now, if ?p is the mother of ?c or if ?p is the father of ?c.

Let's define some other relations:

   (defrelation son (?p ?c) :=
     (and (male ?c) (child ?p ?c)))
   (defrelation daughter (?p ?c) :=
     (and (female ?c) (child ?p ?c)))

A son is a male child. A daughter is a female child.

Now to querying, getting information from the knowledge base. Here are a few queries with their result behind the =>.

   (male Fred) => true
   (female John) => false

That were simple queries that only return true or false. Note that your implementation may display the results in a different way (since I'm not using an interpreter at all (except my brains) while writing this it's kinda logical actually).

   (male ?x) => ?x = Fred, ?x = John

Here we ask the system: for each object (known as ?x for now) in the universe of discourse the set which contains every object that exists) that is a male ((male ?x) => true) tell me what the value of ?w is.

Next example, querying with two variables:

   (daughter (?x ?y)) => (?x = Alice, ?y = Martha), (?x = Betty, ?y = Alice)

As you can see using two variables also yields the correct result.

Now let's select in queries. Say we want the sister of Fred.

   (and (daughter (?x ?y)) (son (?x Fred))) => (?x = Martha, ?y = Alice)

As you can imagine we can program relations out of this:

   (defrelation sister (?s ?o) :=
     (and (daughter ?x ?y) (child ?y ?o)))

Now to 'bottom'. Sometimes you want to say: this can be anything, but I don't care what it is. KIF supports this through bottom. If you write bottom you're saying: this can be anything, but you will not be informed of what it is. For example:

   (son bottom ?x) => ?x = Fred, ?x = Pete

In this query we asked for all sons, but not about their parents.

More to follow, when I update this wiki :-).