You are on page 1of 27

PROLOG

PROgramming in LOGic

A Brief Introduction
PROLOG Paradigm

 PROgramming in LOGic
 Draw inferences from facts and rules
 PROLOG is
 declarative - specify facts and logical relationships
 Non-procedural: "what", not "how".
 Execute the specification.  
 Program execution as theorem proving.
 Database language with automated search and the ability to follow
general rules.
 symbolic - symbols are used to represent objects
 high level - contains a built-in problem solving mechanism
 PROLOG Programming
 Declaring some facts about objects and their relationships
 Defining some rules about objects and their relationships
 Asking questions about objects and their relationships
PROLOG Paradigm

Prolog Database
Query
Facts + Rules

The PROLOG Programmer


 Loads facts and rules into the database.
 Makes queries to the database to see if a fact is:
 in the database or
 can be implied from the facts and rules therein
System Interaction

Problem solving in PROLOG


 1. insert facts and rules into the database
 2. ask questions (queries) based on the contents of the database
 Facts
 Used to represent unchanging information about objects and their
relationships.
 Only facts in the PROLOG database can be used for problem solving.
 Insert facts into the database by,
 typing the facts into a file and loading (consulting) the file into a running
PROLOG system
System Interaction

 Queries
 Retrieve information from the database by entering QUERIES
 A query,
 is a pattern that PROLOG is asked to match against the database
 has the syntax of a compound query
 may contain variables

 A query will cause PROLOG to


 look at the database
 try to find a match for the query pattern
 execute the body of the matching head
 return an answer
Logic Programming

 Logic programming is a form of declarative programming


 A program is a collection of axioms
 Each axiom is a Horn clause of the form:
H :- B1, B2, ..., Bn.
where H is the head term and Bi are the body terms
 Meaning H is true if all Bi are true

 A user of the program states a goal (a theorem) to be


proven
 The logic programming system attempts to find axioms using
inference steps that imply the goal (theorem) is true
Modus ponens

Basic Proof technique - modus ponens

A -> B
A
---------
B
Resolution

 Resolution
 To deduce a goal (theorem), the logic programming system searches
axioms and combines sub-goals
 For example, given the axioms:
C :- A, B.
D :- C.
 To deduce goal D given that A and B are true:
 Forward chaining (from fact to goal) deduces that C is true:
C :- A, B
and then that D is true:
D :- C
 Backward chaining (from goal to fact) finds that D can be proven if sub-
goal C is true:
D :- C
the system then deduces that the sub-goal is C is true:
C :- A, B
Since the system could prove C it has proven D
Prolog Uses backward chaining

Prolog Uses backward chaining


 More efficient than forward chaining for larger collections of
axioms
Interactive (hybrid compiled/interpreted)
Applications: expert systems, artificial intelligence,
natural language understanding, logical puzzles and
games
PROLOG Paradigm

Examples (Facts)

English PROLOG

“A dog is a mammal” isa(dog, mammal).


“A sparrow is a bird” isa(sparrow, bird).
PROLOG Paradigm

Examples (Rules)
English PROLOG

“Something is an animal animal(X) :- isa(X,mammal).


if it is a mammal or a bird” animal(X) :- isa(X, bird).
PROLOG Paradigm

Examples (Queries)
English PROLOG

“is a sparrow an animal?” ?- animal(sparrow).


answer: “yes” yes
“is a table an animal?” ?- animal(table).
answer: “no” no
“what is a dog?” ?- isa(dog, X).
answer: “a mammal” X = mammal
PROLOG syntax
Constants
 Atoms
 Alphanumeric atoms - alphabetic character sequence starting with
a lower case letter
Examples: apple a1 apple_cart
 Quoted atoms - sequence of characters surrounded by single quotes

Examples: ‘Apple’ ‘hello world’


 Symbolic atoms - sequence of symbolic characters

Examples: & < > * - + >>


 Special atoms

Examples: ! ; [ ] {}
 Numbers
Integers and Floating Point numbers
Examples: 0 1 9821 -10 1.3 -1.3E102
PROLOG syntax

Variable Names
a sequence of alphanumeric characters beginning with an
upper case letter or an underscore
Examples: Anything _var X _

Compound Terms (structures)


 an atom followed by an argument list containing terms.
The arguments are enclosed within brackets and
separated by commas
Example: isa(dog, mammal)
Prolog syntax

The names of all relationships and objects must


begin with a lower case letter. For example
studies, ali, programming
The relationship is written first and the objects are
enclosed within parentheses and are written
separated by commas. For example studies(ali,
programming)
The full stop character ‘.’ must come at the end of a
fact.
Order is arbitrary but it should be consistent
Example
 Program with three facts and one
rule:

rainy(columbo).  ?- snowy(C).
rainy(ayubia). C = ayubia
cold(ayubia).
snowy(X) :- rainy(X),
cold(X).  because rainy(ayubia) and
cold(ayubia) are sub-goals
 Query and response: that are both true facts in the
?- snowy(ayubia).
yes database

 Query and response:


?- snowy(columbo).  snowy(X) with
no X=columbo is a goal that
fails, because cold(X) fails,
 Query and response: triggering backtracking
?- snowy(lahore).
No
rainy(columbo).
rainy(ayubia).
cold(ayubia).
snowy(X) :- rainy(X), cold(X).

?- snowy(C).
C = ayubia
C = ayubia
snowy(
C = XC) C = X = ayubia
snowy(
X)AND

rainy(X cold(X)
) OR
X= X = ayubia
columbo
rainy(colum rainy(ayubi cold(ayubia)
bo) a)
Logic Representation : Propositional
Calculus
 Propositional Logic
 Boolean statements
 Logic connectives     

Example – a family
Statement Logic

Atomic statements p
 Ahmed is not father of
Belal
p: Ahmed is father of Belal
q: Ahmed is brother of Aslam pq Ahmed is father of Belal or
r : Aslam is uncle of Belal Ahmed is brother of Aslam

pqr If Ahmed is father of Belal


Complex statements  and brother of Aslam then
Aslam is uncle of Belal
Prolog Programming
Predicate Calculus Prolog code
 Predicates Predicates
 man(Ahmed) man(symbol)
 father(Ahmed, Belal) father(symbol, symbol)
 brother(Belal, Chand) brother(symbol, symbol)
owns(symbol, symbol)
 brother(Chand, Delawar)
tall(symbol)
 owns(Belal, car) hates(symbol, symbol)
 tall(Belal) family()
 hates(Ahmed, Chand) Clauses
 family() man(ahmed).
 Formulae father(ahmed, belal).
brother(ahmed, chand).
 X,Y,Z(man(X)  man(Y)  father(Z,Y) 
owns(belal, car).
father(Z,X)  brother(X,Y)) tall(belal).
 Variables hates(ahmed, chand).
 X, Y and Z family().

brother(X,Y):-
man(X),
 Constants
man(Y),
 Ahmed, Belal, Chand, Delawar and car father(Z,Y),
father(Z,X).
Goal
brother(ahmed,belal).
Sections of Prolog Program (1-3)

Predicates
 Declarations of Relations or Rules
 Just like function prototype (declaration).

 Zero or more arguments


 Example

Predicates
man(symbol)
family()
a_new_predicate (integer, char)
Sections of Prolog Program (2-3)
 Clauses
 Definition(s) of Predicate = Sentence OR Phrase
 Types
 Rules (function definitions)
 0 or more conditions (statements)
 Facts
 0 conditions
 All parameters constant
 Examples
 Fact
brother(ahmed, chand).
 Rule
brother(X,Y):-
man(X),
man(Y),
father(Z,Y),
father(Z,X).
Sections of Prolog Program (3-3)
 Goal
 Goal of inference
 Only and exactly one instance
 The only tentative section of the program
 The main() of prolog
 Goal truth value = output of program
 Syntactically and Semantically just another clause
 Empty, simple (one goal), or compound (sub-goals)
 Examples
 Goal
brother(X,chand). <or> brother(ahmed,chand).
 Goal
brother(X,chand),
father().
Sample Program Update
domains
person = symbol
predicates brother(X,Y):-
nondeterm father(person,person) X<>Y,
nondeterm brother(person,person) father(Z,X),
nondeterm cousin(person,person) father(Z,Y).
nondeterm grandfather(person,person)
cousin(X,Y):-
clauses father(A,X),
father(a,b). father(B,Y),
father(a,c). brother(A,B).
father(a,d).
father(b,e). grandfather(X,Y):-
father(b,f). father(Z,Y),
father(b,g). father(X,Z).
father(c,h). goal
father(c,i). cousin(X,Y).
father(d,j).
father(d,k).
Sample Program Update
A

B C D

E F G H I J K

Example Family Tree


Prolog Variables

 Constant placeholders (NOT variables)


 Bounded once
 Loosely typed
 Start with Capital letter or underscore
 Examples
 brother(ahmed, Ahmed)
 brother(ahmed, _x)
 Brother(ahmed, X)
 Anonymous variable
 The _
 Some value that isn’t required
 Example
brother(ahmed, _)
Other Syntactic Elements

Special predicates
 cut <or> !
 not(predicate)

Predefined predicates
 write(arg1[,arg2[,arg3[,arg4[…..]]]])
 nl
 readint(var)
 readchar(var)
 readln(var)
 … many more related to i/o, arithmetic, OS etc
Other Syntactic Elements

Operators
 Arithmetic
 + - * div / mod
 Relational
 < <= => > = <> ><

Additional domains
 Facts
 Database

You might also like