0% found this document useful (0 votes)
11 views

Introduction Prolog Programming-1

Uploaded by

aviralsinha22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Introduction Prolog Programming-1

Uploaded by

aviralsinha22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction : Prolog

•Prolog is a logic programming language.


It has important role in artificial intelligence. Unlike
many other programming languages, Prolog is
intended primarily as a declarative programming
language.
•In prolog, logic is expressed as relations (called as
Facts and Rules).
•Core heart of prolog lies at the logic being applied.
•Formulation or Computation is carried out by
running a query over these relations.
Installation in Linux :

• Open a terminal (Ctrl+Alt+T) and type:


sudo apt-get install swi-prolog
Installing SWI Prolog on Windows:

Step 1: Visit swi-prolog.org website using any web browser.


Step 2: Click on Download which is adjacent to Home,
dropdown list will appear then click on SWI-Prolog.
Step 3: New webpage will open, click on Stable release.
Step 4: After clicking on stable release new webpage will open
which will contain stable versions of prolog for different
platforms. Under binaries there are two stable releases for
windows, first is SWI-Prolog 8.4.0-1 for Microsoft Windows
(64 bit) and the other is SWI-Prolog 8.4.0-1 for Microsoft
Windows (32 bit). Click on the one as per your system
configuration. Lets take the one for 64-bit operating
system.
• Step 5. After clicking on SWI-Prolog 8.4.0-1
for Microsoft Windows (64 bit), a new
webpage will open, check on I understand
checkbox to make the download link active.
Then click on the download link, downloading
of the executable file will start shortly. It is a
small 11.9 MB file that will hardly take a
minute.
• Step 6: Now check for the executable file in
downloads in your system and run it.
• Step 7: It will prompt confirmation to make
changes to your system. Click on Yes.
• Step 8: Setup screen will appear, click on Next.
• Step 9: The next screen will be of License
Agreement, click on I Agree
• Step 10: After it there will be screen of installing
options so check the box for Add swipl to the
system path for all users, and also check the box
for create a desktop icon and then click on the
Next button.
• Step 11: The next screen will be of installing location so
choose the drive which will have sufficient memory
space for installation. It needed only a memory space
of 50 MB.
• Step 12: Next screen will be of choosing Start menu
folder so don’t do anything just click on Next Button.
• Step 13: This last screen is of choosing components, all
components are already marked so don’t change
anything just click on Install button.
• Step 14: After this installation process will start and will
hardly take a minute to complete the installation.
• Step 15: Click on Finish after the installation
process is complete.
steps for running prolog code in linux

Step1: Install a Prolog Interpreter:


• The most common Prolog interpreter is SWI-
Prolog, but there are others like GNU Prolog as
well. To install SWI-Prolog on a Debian-based
Linux distribution (such as Ubuntu), you can
use the following command in the terminal:
sudo apt-get update
sudo apt-get install swi-prolog
2-Write Your Prolog Code: Create a new text file
with a ‘.pl’ extension (e.g., ‘my_program.pl’)
using a text editor of your choice.
% This is a simple Prolog program.
loves( john, mary).
loves(mary, john).
loves_person(A, B) :- loves(A, B), loves(B, A).
3- Run the Prolog Interpreter: Open a terminal window and
navigate to the directory where your ‘.pl’ file is located.
4- Start the SWI-Prolog Interpreter: Type ‘swipl’ in the
terminal and press Enter. This will start the SWI-Prolog
interpreter.
5- Load Your Prolog Code: Inside the Prolog interpreter, load
your code by typing ‘[my_program]’. (replace ‘my_program’
with the actual filename without the extension) and press
Enter.
?- [my_program].
% my_program compiled 0.00 sec, 2 clauses
true.
6- Interact with Your Code: Once your program
is loaded, you can start querying it. For
example, you can ask if John loves Mary by
typing loves(john, mary). and pressing Enter.
?- loves(john, mary).
true.
7- Exit the Prolog Interpreter: To exit the Prolog
interpreter, you can type halt. and press Enter,
or use the keyboard shortcut Ctrl+D.
steps for running prolog code in
windows
1- Install a Prolog Interpreter: Download and
install the SWI-Prolog interpreter for Windows
from the official website: https://www.swi-
prolog.org/Download.html
• Follow the installation instructions provided
on the website.
2- Write Your Prolog Code: Create a new text
file with a .pl extension (e.g., my_program.pl)
using a text editor like Notepad.
% This is a simple Prolog program.
loves(john, mary).
loves(mary, john).
loves_person(A, B) :- loves(A, B), loves(B, A).
3- Open the SWI-Prolog Interpreter: After
installing SWI-Prolog, you should have a
shortcut or program entry in your Start menu.
Open the SWI-Prolog interpreter.
4- Load Your Prolog Code: Inside the SWI-Prolog
interpreter, go to "File" > "Consult" and navigate
to the directory where your .pl file is located.
Select your file and click "Open" or "Consult".
• Alternatively, you can load your file directly from
the command prompt of the SWI-Prolog
interpreter:
• ?- consult('C:/path/to/your/file/my_program.pl').
% my_program.pl compiled 0.00 sec, 2 clauses
true.
5- Interact with Your Code: Once your program
is loaded, you can start querying it. For
example, you can ask if John loves Mary by
typing loves(john, mary). and pressing Enter.
6- Exit the Prolog Interpreter: To exit the SWI-
Prolog interpreter, you can type halt. and
press Enter, or simply close the interpreter
window.
Syntax and Basic Fields :
• In prolog, We declare some facts. These facts constitute the
Knowledge Base of the system. We can query against the
Knowledge Base. We get output as affirmative if our query
is already in the knowledge Base or it is implied by
Knowledge Base, otherwise we get output as negative.
• So, Knowledge Base can be considered similar to database,
against which we can query.
• Prolog facts are expressed in definite pattern.
• Facts contain entities and their relation.
• Entities are written within the parenthesis separated by
comma (, ).
• Their relation is expressed at the start and outside the
parenthesis. Every fact/rule ends with a dot (.). So, a typical
prolog fact goes as follows :

Format : relation(entity1, entity2, ....k'th entity).


What are the rules of Prolog?
• Names of properties/relationships begin with
lower case letters.
• The relationship name appears as the first
term.
• Objects appear as comma-separated
arguments within parentheses.
• A period "." must end a fact.
• Objects also begin with lower case letters. ...
Example :

friends(raju, mahesh).
singer(sonu).
odd_number(5).
Explanation : These facts can be interpreted as :
raju and mahesh are friends.
sonu is a singer.
5 is an odd number.
Key Features :

1. Unification : The basic idea is, can the given


terms be made to represent the same
structure.
2. Backtracking : When a task fails, prolog
traces backwards and tries to satisfy previous
task.
3. Recursion : Recursion is the basis for any
search in program.
Running queries :
A typical prolog query can be asked as :
• Query 1 : ?- singer(sonu).
• Output : Yes.
• Explanation : As our knowledge base contains
the above fact, so output was 'Yes', otherwise
it would have been 'No'.
• Query 2 : ?- odd_number(7).
• Output : No.
• Explanation : As our knowledge base does not
contain the above fact, so output was 'No'.
Advantages :
1. Easy to build database. Doesn’t need a lot of
programming effort.
2. Pattern matching is easy. Search is recursion
based.
3. It has built in list handling. Makes it easier
to play with any algorithm involving lists.
Disadvantages :
1. LISP (another logic programming language)
dominates over prolog with respect to I/O
features.
2. Sometimes input and output is not easy.
Applications :

• Prolog is highly used in artificial


intelligence(AI).
• Prolog is also used for pattern matching over
natural language parse trees.

You might also like