3. PRO-LOG
• Programming in Logic
• 1975, Phillippe Roussell
• Predicate Calculus
• Declarative Language
• Predicates and Rules
• Inference Mechanism
4. PROLOG Paradigm
• PROgramming in LOGic
– Draw inferences from facts and rules
• PROLOG is
– declarative - specify facts and logical relationships
• Non-procedural: "what", not "how". (we specify the problem but not
the solution)
• 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 (When we program in ProLog we
need to provide following three things)
– Declaring some facts about objects and their relationships
– Defining some rules about objects and their relationships
– Asking questions about objects and their relationships
5. PROLOG Paradigm
• 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
Prolog Database
Facts + Rules
Query
6. 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
7. 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
8. • 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
10. • Prolog Uses backward chaining
– More efficient than forward chaining for larger
collections of axioms
• Applications: expert systems, artificial
intelligence, natural language understanding,
logical puzzles and games
13. 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
14. PROLOG syntax
• Constants
– Atoms
• Alphanumeric atoms - alphabetic character sequence starting with
a lower case letter
Examples: apple a1 apple_cart
• Quoted atoms “String” - 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
15. 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)
16. 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
17. Example
Program with three facts and one
rule:
rainy(columbo).
rainy(ayubia).
cold(ayubia).
Rule: snowy(X) :- rainy(X), cold(X).
Query and response:
?- snowy(ayubia).
yes
Query and response:
?- snowy(columbo).
no
Query and response:
?- snowy(lahore).
No
• ?- snowy(C).
C = ayubia
– because rainy(ayubia) and
cold(ayubia) are sub-goals
that are both true facts in the
database
– snowy(X) with X=columbo is a
goal that fails, because cold(X)
fails, triggering backtracking
18. snowy(C)
snowy(X)
AND
rainy(X) cold(X)
C = X
rainy(columbo) rainy(ayubia)
X = columbo X = ayubia
cold(ayubia)
C = X = ayubia
C = ayubia
rainy(columbo).
rainy(ayubia).
cold(ayubia).
snowy(X) :- rainy(X), cold(X).
?- snowy(C).
C = ayubia
OR
19. Logic Representation : Propositional
Calculus
• Propositional Logic
– Boolean statements
– Logic connectives
Example – a family
Atomic statements
p: Ahmed is father of Belal
q: Ahmed is brother of Aslam
r : Aslam is uncle of Belal
Complex statements
Statement Logic
p Ahmed is not father of
Belal
p q Ahmed is father of Belal or
Ahmed is brother of Aslam
p q r If Ahmed is father of Belal
and brother of Aslam then
Aslam is uncle of Belal
21. 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)
22. 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).
23. 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().
25. 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, _)
Pro: Programming, Log : Logic
Predicate Calculus : Where we define our predicates and then we inference and deduce new information.
Theorem proving: [Certain fact + Certain Rules + Query (Is it right or wrong)]
Declarative Language: What is the fact and what we need in result?
4
5
6
7
Modus Ponens (Basic Proof technique): If A is true then B is also true.
Logic Programming: First we write horn clauses (Facts+Fules).
We need to establish a Goal with two ways.
1)Forward Chaining
2) Backward Chaining: Back tracking Algorithm
I need to prove it or disprove it.
11
12
13
14
15
Period (.) must come after fact and rule. [End of statement]
We don’t have any information that Snowy(Columbo) , that fact is not avaliable so we say No.
Backward chaining & Inference Mechanism do all these things for us.
3 Parts.
Predicates.
Clauses
Goal [Goad will only be one]
Goal: Is the result of program.
Program: [Facts+Rule]+Query
ProLog have no assignment mechanism.
Loosely typed: You associate the value with it..will be behave like that.
(-) Place holder just just use for garbage