Prolog Basics and Installation
Prolog Basics and Installation
Installation
By-
Divyanshu Jha TY CSA
Sharvari Deshmukh TY CSA
Introduction
Applications
Agenda Setup and Installation
Syntax and Components
Example
Introduction
Prolog (PROgramming in LOGic) is one of the most widely used programming
languages in artificial intelligence research.
1. Robotics
2. Expert Systems
3. NLP
4. DBMS
Setup & Installation
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.
Prolog Syntax
2)Number
Numbers are used like constants, and represented like they are
anywhere on the computer
3)Variables
Variables are strings of letters, digits, and the underscore,
starting with a capital letter or an underscore. Examples:
X, Elephant, _4711, X_1_2, MyVariable
4)Compound terms
Compound terms are made up of a Prolog atom followed by a
number of arguments (Prolog terms, i.e. atoms, numbers,
variables, or other compound terms) enclosed in parentheses and
separated by commas.
The following are some examples for compound terms:
is_bigger( horse, X), f( g( X, _), 7), 'My Functor'( dog)
Prolog Components
Prolog programs are made up of facts and rules.
1. Facts -
A fact asserts some property of an object, or relation between 2/more objects.
It is followed by a dot.
It is inherently deemed true.
e.g. mother(jane,alan). # “Jane is the mother of Alan.”
2. Rules-
Consists of a head (a predicate) and a body. (a sequence of predicates separated by , )
Goal expressed by its head is true, if the Prolog system can show, that all of the
expressions (subgoals) in the rule's body are true.
Head and body are separated by :- and terminated by a dot.
e.g. parent(X,Y) :- mother(X,Y). # “ X is the parent of person Y if X is Y’s mother.”
Prolog Components
3. Predicate definitions -
Facts and rules are predicate definitions
Predicate is the name given to the word occurring before the bracket in a fact or rule
Specifies which information needs to be known for the property denoted by the
predicate to be true
4. Program-
A Prolog program is a sequence of clauses.
Facts and rules are also called clauses.
Prolog Components
5. Queries -
After compilation, a Prolog program is run by submitting queries to the interpreter
A query has the same structure as the body of a rule, i.e. it is a sequence of predicates
separated by commas and terminated by a dot.
It is entered at the Prolog prompt, which in most implementations is ?-
e.g. ?- shiny( X), silver( X), metallic( X).
Built-in Predicates
1. Arithmetic Predicates: To perform arithmetic operations E.g. /2, and
comparison predicates like =, >, <, >=, and =<.
2. List Manipulation Predicates: For working with lists, e.g. append/3 for
concatenating lists, member/2 for checking list membership, and length/2 for
determining the length of a list.
3. Input/Output Predicates: Enable input and output operations. E.g. write/1
for displaying information, read/1 for reading user input, and file I/O
predicates like open/3, close/1, read/2, and write/2 for file handling.
4. Control Flow Predicates: To support control flow. E.g. if-then-else
constructs (-> and ;) and predicates for iteration and recursion, such as
repeat/0, !, and fail/0.
5. Type Checking Predicates: Enable type checking and conversion between
data types. E.g. integer/1, float/1, atom/1, and number/1.
An Example
Simplest way of describing the world is by stating facts.
bigger( elephant, horse). # This states that an elephant is bigger than a horse.
bigger( horse, donkey).
bigger( donkey, monkey).
Asking the Prolog system queries about it:
?- bigger( donkey, monkey) # Is a donkey bigger than a monkey? Yes
?- bigger( monkey, elephant) No
?- bigger( elephant, monkey) No
The program says nothing about the relationship between elephants and monkeys.
Still, we know that if elephant > horse, horse > donkey, donkey > monkey, then it
implies elephant > monkey
An Example
The ‘bigger’ relation is transitive. As this transitivity is not defined in our program, it
cannot be proved that an elephant is bigger than a monkey.
A more accurate description of the world: define a new relation, is_bigger, as the
transitive closure of bigger.
is_bigger( X, Y) :- bigger( X, Y).
is_bigger( X, Y) :- bigger( X, Z), is_bigger( Z, Y). # ‘:-’ stands for ‘if’ & ‘,’ stands for ‘and’
Animal X > Y either if this has been stated as a fact or if there is an animal Z for which
it has been stated as a fact that animal X is bigger than animal Z and it can be shown
that animal Z is bigger than animal Y.
X, Y, and Z are variables, indicated using capital letters. Above statements are rules.
An Example
Prolog still cannot find the fact bigger( elephant, monkey) in its database, so it tries to
use the second rule instead.
Done by matching the query with the head of the rule, which is is_bigger( X, Y). When
doing so the two variables get instantiated: X = elephant and Y = monkey.
The rule says, that to prove the goal is bigger( X, Y) Prolog has to prove the two
subgoals bigger( X, Z) and is bigger( Z, Y).
This process is repeated recursively until the facts that make up the chain between
elephant and monkey are found and the query finally succeeds.
?- is_bigger( X, donkey). ?- is_bigger( donkey, X), is_bigger( X, monkey).
X = horse ; No
X = elephant ;
No
Thank You!