Prolog Knowledge Base

Summer 2023

Tech Stack: Visual Studio Code, Prolog, GNU Prolog.

Overview:

I wrote a relational knowledge base in prolog. The program sets up rules for family members such as: mother, father, brother, sister, grandma, grandpa, aunt, uncle and ancestor. The goal of the program is to be able to query the knowledge base and it correctly tell us relational facts.

Prolog Language:

Prolog is a LOGical PROgraming language. It is based on facts and rules. Rules being the prolog functions and facts being the truth you tell it.

Syntax Description:

My rules are set up as the image below. A father is a male and a parent.

father(F, Y) :-
   male(F),
   parent(F, Y).

To create a fact that Abraham is the father to Chance you would say father(abraham, chance)..

Full Code and Knowledge base

father(F, Y) :-
   male(F),
   parent(F, Y).

mother(M,Y) :-
   female(M),
   parent(M,Y).

grandfather(G, Y) :- 
   male(G),
   parent(G, P),
   parent(P, Y).
   
grandmother(G, Y) :-
   female(G),
   parent(G, P),
   parent(P, Y).

sister(X,Y) :-
   female(X),
   parent(P,X),
   parent(P,Y),
   =\=(X, Y).

brother(X, Y) :-
   male(X), 
   parent(P, X),
   parent(P, Y),
  =\=(X,Y).

aunt(A,Y) :-
  female(A),
  parent(P,Y),
  sister(A,P).

uncle(U,Y) :-
  male(U),
  parent(P,Y), 
  brother(U, P).

ancestor(A,Y) :- 
   parent(Z, Y),
   ancestor(A, Z).

male(al).      
male(carl).    
male(ed).      
male(frank).   
male(duncan).
male(kurt).     
male(rene).    
male(vinny).    
male(tony).

female(berta).  
female(dora).  
female(tina).  
female(deb).   
female(jane).
female(laura).  
female(mia).   
female(olga).  
female(wendy).  
female(susan).
female(linda).

parent(al,tdeb).   
parent(al,duncan).   
parent(carl,deb).   
parent(carl,jane).
parent(ed,kurt).    
parent(frank,mia).  
parent(frank,olga). 
parent(duncan,wendy).
parent(duncan,rene).  
parent(duncan,vinny).  
parent(kurt,tony).  
parent(kurt,linda).    

parent(berta,tdeb). 
parent(berta,hugo). 
parent(dora,deb).   
parent(dora,jane).
parent(tina,mia).   
parent(tina,olga).  
parent(deb,wendy).   
parent(deb,rene).
parent(deb,vinny).   
parent(jane,susan). 
parent(laura,tony). 
parent(laura,linda).

Querying:

In order to query and compile this code I had to install GNU Prolog. Like python you have to open the environment run your code and then you can query.

Results:

My results were good, below are some of the queries I tested.

  • Querying "who is the brother of deb?" or brother(X, deb). Images of prolog brother(f|y)

  • Querying "Does Jane have an uncle?" or uncle(A, jane). Images of prolog uncle(f|y)

  • Querying "Who are all the mothers and who are their children?" or mother(X,Y). Images of prolog mother(f|y)

  • Querying "Who are all the grandmothers and who are their grandchildren?" or grandmother(X,Y). Images of prolog grandmother(f|y)

  • Querying "Who are Jane's parents?" or parent(X, jane). Images of prolog parent(f|y)

Discussion:

The strengths of the program is prolog is easy to use and adding new rules to the knowledge base was easy after you got the hang of the language. It was overly simple to the point where I thought I was doing it wrong at first. It kind of reminds me of caveman talk “Father. Man! Parent!”.

Prolog