If you are not familiar with computer programing, you will not understand the hassle behind what it takes to make things work on a computer and it will really look so fascinating to you when someone can write a program to execute on the computer. But some languages can make programming  as easy as A B C D. One of the languages use in Artificial Language is Prolog and it’s pretty cool to work around. It is a declarative language as oppossed to procedural languages like java, c++. That means in prolog, you tell the computer what you want and the computer does it but in procedural languages, you have to tell the computer how to accomplish your task. As a matter of fact, you can write your first code of prolog just after an hour of introduction to the language. I’m going to give a little flavor of the language so anyone can see how easy it is to write a program in prolog. First, you have to download the prolog compiler and install it on your computer. To download it, go to  http://www.swi-prolog.org/download/stable and get a free copy for your windows or macs system. For unix users, type >bp at your terminal to load the prolog compiler. Okay, assuming you have access to the compiler, lets try to write a program in prolog. These program will build a family relation system for you.
%  Facts and Rules of the program and their meaning are as follows;
%  father(X,Y) means X is Y’s father.
%  mother(X,Y) means X is Y’s mother.
%  female(X) means X is a female.
%  male(X) means X is a male.
%  husband(X,Y) means X is Y’s husband
%  parent(X,Y) means X is Y’s parent.
%  grandfather(X,Y) means X is Y’s grandfather.
%  grandparent(X,Y) means X is Y’s grandparent.
%  children(X,Y) means X is Y’s child.
%  son(X,Y) means X is Y’s son.
%  daughter(X,Y) means X is Y’s daughter.
%  sibling(X,Y) means X and Y are siblings.
%  h-mother-in-law(X,Y) means X is Y’s husband’s mother.
%  h-father-in-law(X,Y) means X is Y’s husband’s father.
%  w-mother-in-law(X,Y) means X is Y’s wife’s mother.
%  w-father-in-law(X,Y) means X is Y’s wife’s father.
father(vin,aby).
father(ral,nic).
father(chris,vin).
father(vin,sky).
mother(gina,vin).
mother(nic,aby).
mother(ala,nic).
mother(nic,sky).
female(aby).
female(nic).
female(ala).
female(gina).
female(sky).
male(ral).
male(vin).
male(chris).
parent(X,Y):-father(X,Y);mother(X,Y).
grandparent(X,Y):-parent(X,M),parent(M,Y).
children(X,Y):-parent(Y,X).
husband(X,Y):-male(X),female(Y),children(M,X),children(M,Y).
sibling(X,Y):-mother(M,X),mother(M,Y),father(F,X),father(F,Y),X=Y.
grandfather(X,Y):-father(X,K),father(K,Y).
grandfather(X,Y):-father(X,K),mother(K,Y).
grandmother(X,Y):-mother(X,K),mother(K,Y).
grandmother(X,Y):-mother(X,K),father(K,Y).
h-mother-in-law(X,Y):-mother(X,K),male(K),husband(K,Y).
w-mother-in-law(X,Y):-mother(X,K),female(K),husband(Y,K).
h-father-in-law(X,Y):-father(X,K),male(K),husband(K,Y).
w-father-in-law(X,Y):-father(X,K),female(K),husband(Y,K).
son(X,Y):-children(X,Y),male(X).
daughter(X,Y):-children(X,Y),female(X).

% End of the program, the percent sign just means comment in prolog. so the compiler ignores all staments that begins with %                       Type the program in notepad and store it as 2smart.pro in the directory where you installed your prolog compiler.Now open the compiler and  type consult(‘2smart.pro’). and  press enter. When you have typed everything correct as above, the program will compile and it will come to the prompt for you to query the data stored in the program.Now lets run some querries.we want to find the father of aby, so you will type this at the prompt and press enter.    father(X,aby). the X is capital. that’s the prolog rule, all variables must begin in capital and all facts must be entered in lower case letters.try to find other insteresting stuff like who are the parents or grandparents of sky. That will be

parent(X,sky).

grandparent(X,sky).

For any corrections, pls add it as a comment to help all of us improve our study of prolog.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *