Thursday, June 4, 2015

calculations in prolog

The following are the programs which can perform         different arithmetic  operations in Prolog 

 To Sum  Numbers.

 sum:-
write('Type your First Number '),
read(X),
write('Type Your 2nd Number '),
read(Y),
S is X+Y,
write('The Sum of your numbers is = '),write(S),nl.

To Subtract Numbers


 sub:-
write('Type your first Number '),
read(X),
write('Type your 2nd Number '),
read(Y),
D is X-Y,
write('When we subtract your Numbers we get ='),write(D),nl.

To Divide Numbers

 divide:-

write('Type your First Number '),
read(X),
write('Type Your 2nd Number '),
read(Y),
Di is X/Y,
write('when we divide your numbers we get = '),write(Di),nl.

To Multiply Numbers 

 multiply:-

write('Type your First Number '),
read(X),
write('Type Your 2nd Number '),
read(Y),
M is X*Y,
write('The Multiply   your numbers we get  = '),write(M),nl.

Prolog Family Tree

Establishing Facts

Prolog programs are text files. This means that we can write them in a text editor like Notepad. Launch Notepad a file to your area as family tree.pl You will have to make sure that you select All Files in the save dialog.
Type up the following facts for the family tree program.
/* Family Tree Program */

/* facts */
male(jack).
male(bill).
male(john).
female(dorothy).
female(evelyn).
female(grace).
female(liz).
parent(dorothy, evelyn).
parent(jack, evelyn).
parent(jack, grace).
parent(bill, liz).
parent(evelyn, liz).
parent(bill, john).
parent(evelyn, john).

The lines which begin and end with asterisks and forward slashes are comments - these are ignored when the program is run. All names must be in lower case - Prolog uses upper case for unknown information. All statements in Prolog end with a full stop.

Establishing Rules

Your program will need some rules if it is going to be used to make decisions. Add the following lines to the bottom of your program.
/* rules */
mother(M,X):-
parent(M,X),
female(M).

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

The first rule states that M is the mother of X if M is a parent of X and M is female. The father rule works in a similar way.

Testing The Program

Save your program. Launch Prolog.
Click on File, Consult and select the program that you have saved.
We will start by getting our program to find out who the parents of evelyn are. Type the following line in the Prolog window.
mother(X, evelyn).
In our statement, X stands for the information that we don't know. You should be given the answer X = dorothy. Press enter and type the following
father(X, evelyn).
You should get the answer X = jack. Use the program to find out the following.
  1. The father of liz
  2. The mother of john
  3. The father of grace

Adding More Rules

Return to your program and add the following rules.
grandparent(G, X):-
parent(G,P),
parent(P,X).

sister(S,X):-
parent(Z,X),
parent(Z,S),
female(S),
not(S=X).

To get your program to use the new rules, first save your program. Now click on File and Reload Modified Files.
Type in the following
brother(X, liz).
You should get the answer X = john.
Now type in the following
grandparent(X, liz).
After the first answer, press the semi-colon key. You should get the second possible answer.

Using The Anonymous Variable

We can get our program to answer vaguer questions than the ones we have been putting. Suppose we want to find out who all the mothers in the system are. Type the following
mother(X, _).
The underscore in Prolog is called the anonymous variable. This query should return all of the mothers in the system. Press the semi-colon after each answer.

Creating A User Interface

Expert systems can be made easier to use by creating an interface for the queries that you wish to run. Add the following to the bottom of your program.
findMother:-
write('Whose mother do you want to find? '),
read(Name),
mother(X,Name),
write('The mother of '), write(Name), write(' is '), write(X), nl.

To use this, type findMother. at the prompt. The program should help you with questions. Remember to place a full stop at the end of any line that you enter at the prompt.

Extending The Program

Using the information above, try to write some new rules for your program to find the brother, grandfather, grandmother of a person.
Write interfaces for all standard queries in the system.

Prolog Variables

Variables always start with an upper case alphabetic character or an underscore.

Other than the first character they can be made up of any mixture of letters, digits, and underscores.
 
  e.g.   X, ABC, _89two5, _very_long_variable


There are no “types” for variables (or constants) – a variable can take any value.

All Prolog variables have a “local” scope:
they only keep the same value within a clause; the same variable used outside of a clause does not inherit the value (this would be a “global” scope). 

Prolog Basics

Introudction To Prolog : Uses of Prolog 


Programming languages are of two kinds:

Procedural (e.g BASIC, ForTran, C++, Pascal, Java);

Declarative (e.g LISP, Prolog, ML).

In procedural programming, we tell the computer how  to solve a problem.

In declarative programming, we tell the computer what problem we want solved.

Note : (However, in Prolog, we are often forced to give clues as to the solution method).

Uses of Prolog


Good at

Grammars and Language processing,
Knowledge representation and reasoning,
Unification,
Pattern matching,
Planning and Search.

i.e. Prolog is good at Symbolic AI.

Poor at:

Representing complex data structures,
Input/Output (interfaces).




Wednesday, June 3, 2015

Installing Prolog on Windows

  1. Open your favourite web browser (Internet Explorer, Mozilla Firefox etc). In the address bar type:

    http://www.swi-prolog.org

    (or simply click on this link), to visit the homepage of the SWI-Prolog Project.


  2. On the navigation menu at the left hand side of the screen, click on Download and in the page that will appear, click on Stable Release.



    This page shows all the current available SWI-Prolog stable releases for a variety of platforms and operating systems. Since the lab PCs have MS Windows XP as their operating system, we will download and install the SWI-Prolog XPCE for MS-Windows XP release.


  3. In the dialog box that pops up, click on Save and save the file on your desktop



    (in the Save as... window that pops up, click on Desktop and then click on Save).


  4. When the download is complete click on the Run Button (alternatively double click on the w32pl5654.exe file on your desktop.


  5. If you are running Internet Explorer, you will be prompted with a security question ("The publisher could not be verified. Are you sure you want to run this software?"). Select Run.


  6. The SWI-Prolog Installer will ask you to accept the GNU general Public Licese. Read it (if you wish) and click on I Agree.


  7. In the following window select the Typical Installation (the default selection) and click on Next.
  8. The Installer will now prompt for the installation directory: in the Destination folder line change the c: directory to your M: drive letter (usually m:).


  9. Accept the next dialog box as-is and click on Install. The Installer will install SWI-Prolog to your system.


  10. During installation, the installer might output an error message ("Could not write registry to register filetypes..."), in which you should just click OK.



    When the installation is complete, click on Finished to close the installer and complete the installation.


  11. The installer will now ask if you would like to view the Readme file. Click on Yes if you wish to view the readme andon No if otherwise. The Readme file is also available via the Help menu within SWI-Prolog or in the following path: M:\Program Files\pl\doc\windows.html.