Professional Documents
Culture Documents
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
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 -> 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
Examples (Facts)
English PROLOG
Examples (Rules)
English PROLOG
Examples (Queries)
English PROLOG
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 _
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
?- 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 pq Ahmed is father of Belal or
r : Aslam is uncle of Belal Ahmed is brother of Aslam
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).
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
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