SlideShare a Scribd company logo
Chapter 10: Logic Programming Languages 
Principles of Programming Languages
Contents 
•Predicate Calculus and Proving Theorems 
•The Basic Elements of Prolog 
•Simple Arithmetic and List in Prolog 
•Deficiencies of Prolog 
•Applications of Logic Programming
•Another programming methodology: 
–Express programs in a form of symbolic logic 
–Use a logical inferencing process to produce results 
•Prolog 
Introduction
•Proposition: a statement that may or may not be true 
–James I is a male 
–Catherine is a female 
–James I is parent of Charles I 
–Catherine is parent of Sophia 
•Compound term: functor + parameter lists 
male(james1) 
female(catherine) 
parent(james1, charles1) 
parent(catherine, sophia) 
•Two modes: facts or queries 
Proposition
Logic Operators 
¬ a 
negation 
a ˄ b 
conjunction 
a ˅ b 
disjunction 
a → b 
implication 
a ↔ b 
equivalence 
•Examples: 
(a ˄ b) → c
Predicates 
•When we employ variables, the truth value of a statement is unknown until the variable has a specific value 
–X is a male 
–male(X) 
•Predicates turn into propositions if they have quantifiers 
∀X (woman(X) → human(X)) 
∃X (mother(mary, X) ˄ male(X))
Clausal Form 
•A standard form for propositions 
B1 ˅ B2 ˅ …˅ Bn ← A1 ˄ A2 ˄ … ˄ Am 
•Examples 
mother(catherine, sophia) ← parent(catherine, sophia) ˄ female(catherine) 
father(louis, al) ˅ father(louis, violet) ← father(al, bob) ˄ mother(violet, bob) ˄ grandfather(louis, bob) 
•RHS = antecedent 
•LHS = consequent
Automatic Theorem Proving 
•Resolution: if we have 
older(joanne, jake) ← mother(joanne, jake) 
wiser(joanne, jake) ← older(joanne, jake) 
•Using resolution, we can construct 
wiser(joanne, jake) ← mother(joanne, jake) 
•Mechanics: 
1.AND two LHS to make the new LHS 
2.AND two RHS to make the new RHS 
3.Remove any terms that appear in the both sides
Questions 
father(bob, jake) ˅ mother(bob, jake) ← parent(bob, jake) 
grandfather(bob, fred) ← father(bob, jake) ˄ father(jake, fred) 
What can we infer?
Horn Clauses 
•When propositions are used for resolution, only a restricted kind of clausal form can be used 
•There are two forms of Horn clauses: 
–Headed: They have single atomic proposition on the LHS 
likes(bob, trout) ← likes(bob, fish) ˄ fish(trout) 
–Headless: Empty LHS 
father(bob, jake)
Resolution 
•Ability to detect any inconsistency in a given set of propositions 
•Theorem is proved by 
–We are given a set of pertinent propositions 
–Negation of theorem as a new proposition 
–Resolution is used to find inconsistency 
•Hypotheses: a set of original propositions 
•Goal: negation of theorem stated as a proposition
An Overview of Logic Programming 
•Declarative, rather that procedural or functional 
•Specify relation between objects 
–larger(3, 2) 
–father(tom, jane) 
•Separate logic from control: 
–Programmer declares what facts and relations are true 
–System determines how to use facts to solve problems 
–System instantiates variables in order to make relations true
Example: Sorting a List 
•Describe the characteristics of a sorted list, not the process of rearranging a list 
sort(old_list, new_list) ← permute (old_list, new_list) ˄ 
sorted (new_list) 
sorted (list) ← j such that 1 j < n, list(j)  list (j+1)
The Origins of Prolog 
•Alain Colmerauer at Marseille in 1970s 
–Natural language processing 
•University of Edinburgh 
–Automated theorem proving 
•1981, Japanese Fifth Generation Computing Systems choose Prolog as the basis 
•Despite the great assumed potential of logic programming and Prolog, little great significance had been covered
Facts 
male(charlie). 
male(bob). 
female(alice). 
female(eve). 
parent(charlie, bob). 
parent(eve, bob). 
parent(charlie, alice). 
parent(eve, alice). 
•Headless Horn clauses 
•Every Prolog statement is terminated by a period
Rules 
•Headed Horn clauses 
mother(X, Y) ← female(X) ˄ parent(X, Y) 
mother(X, Y) :- female(X), parent(X, Y).
Queries 
•Let the computer know the facts, then ask it some questions – queries 
•Ask the system to prove or disprove a theorem 
?- mother(sophia, george1). 
true 
•Ask the system to identify the instantiation of variables that make the query true 
?- mother(X, sophia). 
X = Elizabeth 
false
Some Prolog Syntax 
•Layer 0: constants and variables 
•Constants are: 
–Atoms: any string consisting of alphanumeric characters and underscore (_) that begins with a lowercase letter 
–Numbers 
•Variables are strings that begin with “_” or an uppercase letter
Some Prolog Syntax 
const -> atom | number 
var -> ucase string | _ string 
atom -> lcase string 
letter -> ucase | lcase 
string -> epsilon | letter string 
| number string | _ string 
ucase -> A | ... | Z 
lcase -> a | ... | z 
number -> ...
Some Prolog Syntax 
•Layer 1: terms 
•These are inductively defined: 
–Constants and variables are terms 
–Compound terms – applications of any n-ary functor to any n terms – are terms 
–Nothing else is a term
Some Prolog Syntax 
•Layer 2: atomic formulae 
•These consist of an n-ary relation (also called predicate) applied to n terms 
•Formulae are either true or false; terms denote entities in the world 
•In Prolog, atomic formulae can be used in three ways: 
–As facts: in which they assert truth 
–As queries: in which they enquire about truth 
–As components of more complicated statements
Some Prolog Syntax 
term -> const | var | 
functor “(“ term (“,” term)* “)” 
pred -> pname “(“ term (“,” term)* “)”
Prolog Queries 
•A query is a proposed fact that is to be proven 
–If the query has no variables, returns true/false 
–If the query has variables, returns appropriate values of variables (called a substitution) 
?- male(charlie). 
true 
?- male(eve). 
false 
?- female(Person). 
Person = alice; 
Person = eve; 
false
Terms 
•Horn Clause = Clause 
•Consequent = Goal = Head 
•Antecedents = Subgoals = Tail 
•Horn Clause with No Tail = Fact 
•Horn Clause with Tail = Rule
Some Prolog Syntax 
clause-> pred . | 
pred :- pred (“,” pred)* . 
term -> const | var | 
functor “(“ term (“,” term)* “)” 
pred -> pname “(“ term (“,” term)* “)” 
const -> ... 
var -> ...
Variables 
•Variables may appear in the LHS or RHS of a clause. Prolog interprets free variables of a rule universally 
mother(X) :- female(X), parent(X, Y). 
In first-order logic: 
∀X mother(X) ← ∃Y parent(X, Y) ˄ female(X) 
•Note that the Prolog query 
?-q(X1, ..., Xn) 
means 
∃X1,...,Xn q(X1, …, Xn)
Execution of Prolog Programs 
•Unification: variable bindings 
•Backward Chaining/Top-Down Reasoning/Goal Directed Reasoning: reduces a goal to one or more subgoals 
•Backtracking: systematically searches for all possible solutions that can be obtained via unification and backchaining.
Unification 
•Two atomic formulae unify iff they can be made syntactically identical by replacing their variables by other terms 
•For example: 
–parent(bob,Y) unifies with parent(bob,sue) by replacing Y by sue 
?-parent(bob,Y)=parent(bob,sue) 
Y = sue ; 
false 
–parent(bob,Y) unifies with parent(X,sue) by replacing Y by sue and X by bob 
?-parent(bob,Y)=parent(X,sue). 
Y = sue 
X = bob ; 
false
Unification Algorithm 
Equation 
Action 
f(s1, ..., sn) = f(t1, ..., tn) 
Replace with s1 = t1, ..., sn = tn 
f(s1, ..., sn) = g(t1, ..., tn) where f ≠ g 
Halt with failure 
X = X 
Delete the equation 
t = X where t is not a variable 
Replace with X = t 
X = t where X does not occur in t and it occurs elsewhere 
Apply the substitution X = t to all other terms 
X = t where X occurs in t (but t ≠ X) 
Halt with failure
Prolog Search Trees 
•Encapsulate unification, backward chaining, and backtracking 
–Internal nodes are ordered list of subgoals 
–Leaves are success nodes or failures, where computation can proceed no further 
–Edges are labeled with variable bindings that occur by unification 
•Describe all possible computation paths 
–There can be many success nodes 
–There can be infinite branches
Prolog Search Tree 
/* program P clause # */ 
p(a). /* #1 */ 
p(X) :- q(X), r(X). /* #2 */ 
p(X) :- u(X). /* #3 */ 
q(X) :- s(X). /* #4 */ 
r(a). /* #5 */ 
r(b). /* #6 */ 
s(a). /* #7 */ 
s(b). /* #8 */ 
s(c). /* #9 */ 
u(d). /* #10 */ 
?- p(X)
[trace] ?- p(X). 
Call: (7) p(_G312) ? creep 
Exit: (7) p(a) ? creep 
X = a ; 
Redo: (7) p(_G312) ? creep 
Call: (8) q(_G312) ? creep 
Call: (9) s(_G312) ? creep 
Exit: (9) s(a) ? creep 
Exit: (8) q(a) ? creep 
Call: (8) r(a) ? creep 
Exit: (8) r(a) ? creep 
Exit: (7) p(a) ? creep 
X = a ; 
Redo: (9) s(_G312) ? creep 
Exit: (9) s(b) ? creep 
Exit: (8) q(b) ? creep 
Call: (8) r(b) ? creep 
Exit: (8) r(b) ? creep 
Exit: (7) p(b) ? creep 
X = b ; 
Redo: (9) s(_G312) ? creep 
Exit: (9) s(c) ? creep 
Exit: (8) q(c) ? creep 
Call: (8) r(c) ? creep 
Fail: (8) r(c) ? creep 
Redo: (7) p(_G312) ? creep 
Call: (8) u(_G312) ? creep 
Exit: (8) u(d) ? creep 
Exit: (7) p(d) ? creep 
X = d ; 
No
Approaches 
•Prolog implementations use backward chaining 
•Top-down resolution, backward chaining 
–Begin with goal and attempt to find sequence that leads to set of facts in database 
–Works well with a small set of possibly correct answers 
•Bottom-up resolution, forward chaining 
–Begin with facts and rules of database and attempt to find sequence that leads to goal 
–Works well with a large set of possibly correct answers
Subgoal Strategies 
•When goal has more than one subgoal, can use either 
–Depth-first search: find a complete proof for the first subgoal before working on others 
–Breadth-first search: work on all subgoals in parallel 
•Prolog uses depth-first search 
–Can be done with fewer computer resources
Backtracking 
•With a goal with multiple subgoals, if fail to show truth of one of subgoals, reconsider previous subgoal to find an alternative solution: backtracking 
•Begin search where previous search left off 
•Can take lots of time and space because may find all possible proofs to every subgoal
Trace 
•Built-in structure that displays instantiations at each step 
•Tracing model of execution - four events: 
–Call (beginning of attempt to satisfy goal) 
–Exit (when a goal has been satisfied) 
–Redo (when backtrack occurs) 
–Fail (when goal fails)
Example 
likes(jake,chocolate). 
likes(jake,apricots). 
likes(darcie,licorice). 
likes(darcie,apricots). 
trace. 
likes(jake,X), 
likes(darcie,X).
Simple Arithmetic 
•Prolog supports integer variables and integer arithmetic 
•is operator: takes an arithmetic expression as right operand and variable as left operand 
A is B / 17 + C 
•Not the same as an assignment statement!
Example 
speed(ford,100). 
speed(chevy,105). 
speed(dodge,95). 
speed(volvo,80). 
time(ford,20). 
time(chevy,21). 
time(dodge,24). 
time(volvo,24). 
distance(X,Y) :- speed(X,Speed), 
time(X,Time), 
Y is Speed * Time.
List Structures 
•List is a sequence of any number of elements 
•Elements can be atoms, atomic propositions, or other terms (including other lists) 
[apple, prune, grape, kumquat] 
[] (empty list) 
[H | T] (head H and tail T)
Append Example 
append([], List, List). 
append([Head | List_1], List_2, [Head | List_3]) :- 
append (List_1, List_2, List_3).
Another Example 
whatisme([], []). 
whatisme([Head | Tail], List) :- 
whatisme(Tail, Result), 
append(Result, [Head], List). 
•Try to write member rule to check if an element is a member of a list
Deficiencies of Prolog 
•Resolution order control 
–Prolog always matches in the same order – user can control the ordering 
–Prolog allows explicit control of backtracking via cut operator -- “!”, which is actually a goal, not an operator 
•Always succeed but cannot be resatisfied through backtracking 
a, b, !, c, d 
–It is detrimental to one of the important advantages of logic programming – programs do not specify how solutions are to be found.
Deficiencies of Prolog 
•The closed-world assumption 
–In Prolog, the truths are those that can be proved using its database 
–If there is insufficient information in the database to prove a query, it is not actually false, it fails 
–It relates to negation problem
Deficiencies of Prolog 
•The negation problem 
–Stating that two atoms are not equal is not straightforward in Prolog 
–Bad solution: adding facts stating that they are not the same 
–Alternative: state in the goal that X must not be the same as Y 
sibling(X, Y) :- parent(M, X), parent(M, Y), not(X=Y) 
–However, not operator is not logical NOT, not(not(some_goal)) is not equivalent to some_goal 
–Reason: the use of Horn clause form prevents any negative conclusions
Deficiencies of Prolog 
•Intrinsic limitations 
–Prolog has no idea of how to sort, other than simply to enumerate all permutations of the given list until it happens to create the one that has the list in sorted order 
–We must specify the details of how that sorting can be done – not logic programming any more 
–Resolution is not capable of doing this sorting
Applications of Logic Programming 
•Relational database management systems 
–Logic programming is a natural match to the needs of implementing a RDBMS 
SQL is goal, tables are facts 
•Expert systems 
–Computer systems designed to emulate human expertise in some particular domain 
–In addition to the initial knowledge base, ES can learn from the process of being used 
•Natural language processing
Summary 
•Symbolic logic provides basis for logic programming 
•Logic programs should be nonprocedural 
•Prolog statements are facts, rules, or goals 
•Resolution is the primary activity of a Prolog interpreter 
•Although there are a number of drawbacks with the current state of logic programming it has been used in a number of areas

More Related Content

PPT
Prolog basics
PPTX
Prolog Programming : Basics
PPTX
Problems problem spaces and search
PPT
Introduction to prolog
PDF
Ai lecture 7(unit02)
PPTX
Prolog Programming Language
PDF
AI 6 | Adversarial Search
PDF
Stuart russell and peter norvig artificial intelligence - a modern approach...
Prolog basics
Prolog Programming : Basics
Problems problem spaces and search
Introduction to prolog
Ai lecture 7(unit02)
Prolog Programming Language
AI 6 | Adversarial Search
Stuart russell and peter norvig artificial intelligence - a modern approach...

What's hot (20)

PPT
Goal stack planning.ppt
PDF
Rabin karp string matcher
PPT
Artificial intelligence Prolog Language
PPTX
AI_session 22 inference and unification.pptx
PPTX
Adversarial search with Game Playing
PPT
Knowledge Representation in Artificial intelligence
PPTX
knowledge representation using rules
PPT
Predicate logic_2(Artificial Intelligence)
PDF
Daa notes 3
PPTX
N-queens.pptx
PPT
CS8461 - Design and Analysis of Algorithms
PDF
Ai lab manual
PPTX
KMP String Matching Algorithm
PPTX
Unification and Lifting
PPTX
Introduction To HBase
PPTX
15 puzzle problem using branch and bound
PPTX
Local beam search example
PDF
First Order Predicate Logic Examples| Different Examples
PDF
backtracking algorithms of ada
PDF
Introduction to Deep Generative Models
Goal stack planning.ppt
Rabin karp string matcher
Artificial intelligence Prolog Language
AI_session 22 inference and unification.pptx
Adversarial search with Game Playing
Knowledge Representation in Artificial intelligence
knowledge representation using rules
Predicate logic_2(Artificial Intelligence)
Daa notes 3
N-queens.pptx
CS8461 - Design and Analysis of Algorithms
Ai lab manual
KMP String Matching Algorithm
Unification and Lifting
Introduction To HBase
15 puzzle problem using branch and bound
Local beam search example
First Order Predicate Logic Examples| Different Examples
backtracking algorithms of ada
Introduction to Deep Generative Models
Ad

Viewers also liked (20)

PPT
Unit 3 principles of programming language
PPTX
Introduction to Prolog
PPTX
Hadoop
PDF
How to build a news website use CMS wordpress
PDF
09 implementing+subprograms
PPTX
Introduction to HBase
PDF
08 subprograms
PDF
Untitled Presentation
PDF
Datatype
PDF
Chapter2
PDF
Nhập môn công tác kỹ sư
PDF
Config websocket on apache
PDF
Control structure
PPT
Memory allocation
DOCX
Chapter 9 & chapter 10 solutions
PPT
Penyederhanaan Karnaugh Map
PPTX
Aljabar boolean
PPT
Lecture # 2
PDF
Chapter 17 dccn
Unit 3 principles of programming language
Introduction to Prolog
Hadoop
How to build a news website use CMS wordpress
09 implementing+subprograms
Introduction to HBase
08 subprograms
Untitled Presentation
Datatype
Chapter2
Nhập môn công tác kỹ sư
Config websocket on apache
Control structure
Memory allocation
Chapter 9 & chapter 10 solutions
Penyederhanaan Karnaugh Map
Aljabar boolean
Lecture # 2
Chapter 17 dccn
Ad

Similar to 10 logic+programming+with+prolog (20)

DOCX
Prolog_Programminvfygugy7gtugbugtg_Notes.docx
PPT
Chaps 1-3-ai-prolog
DOCX
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
DOCX
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
DOCX
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
PDF
Logic Programming and ILP
PPTX
Introduction on Prolog - Programming in Logic
PPTX
PROLOG: Introduction To Prolog
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Logical Programming Paradigm for programming Languages.
PDF
BCS515B Module 5 vtu notes : Artificial Intelligence Module 5.pdf
PPTX
Prolog 7-Languages
PPTX
ProLog (Artificial Intelligence) Introduction
PPT
Chaps 1-3-ai-prolog
Prolog_Programminvfygugy7gtugbugtg_Notes.docx
Chaps 1-3-ai-prolog
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
Logic Programming and ILP
Introduction on Prolog - Programming in Logic
PROLOG: Introduction To Prolog
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Logical Programming Paradigm for programming Languages.
BCS515B Module 5 vtu notes : Artificial Intelligence Module 5.pdf
Prolog 7-Languages
ProLog (Artificial Intelligence) Introduction
Chaps 1-3-ai-prolog

More from baran19901990 (15)

PDF
Tìm đường đi xe buýt trong TPHCM bằng Google Map
PDF
How to install nginx vs unicorn
PDF
Subprogram
PDF
Lexical
PDF
Introduction
PDF
07 control+structures
PDF
How to install git on ubuntu
DOC
Ruby notification
DOC
Rails notification
DOC
Linux notification
PDF
PDF
DOCX
Báo cáo mô hình quản lý khách sạn
PPTX
MDA Framework
Tìm đường đi xe buýt trong TPHCM bằng Google Map
How to install nginx vs unicorn
Subprogram
Lexical
Introduction
07 control+structures
How to install git on ubuntu
Ruby notification
Rails notification
Linux notification
Báo cáo mô hình quản lý khách sạn
MDA Framework

Recently uploaded (20)

PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
CSharp_Syntax_Basics.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
DOCX
Unit-3 cyber security network security of internet system
PPTX
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
PDF
Sims 4 Historia para lo sims 4 para jugar
PDF
Behind the Smile Unmasking Ken Childs and the Quiet Trail of Deceit Left in H...
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PPTX
international classification of diseases ICD-10 review PPT.pptx
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PPTX
presentation_pfe-universite-molay-seltan.pptx
Slides PDF The World Game (s) Eco Economic Epochs.pdf
CSharp_Syntax_Basics.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxx
QR Codes Qr codecodecodecodecocodedecodecode
522797556-Unit-2-Temperature-measurement-1-1.pptx
Module 1 - Cyber Law and Ethics 101.pptx
Job_Card_System_Styled_lorem_ipsum_.pptx
Decoding a Decade: 10 Years of Applied CTI Discipline
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
RPKI Status Update, presented by Makito Lay at IDNOG 10
Unit-3 cyber security network security of internet system
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
Sims 4 Historia para lo sims 4 para jugar
Behind the Smile Unmasking Ken Childs and the Quiet Trail of Deceit Left in H...
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
international classification of diseases ICD-10 review PPT.pptx
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Tenda Login Guide: Access Your Router in 5 Easy Steps
Unit-1 introduction to cyber security discuss about how to secure a system
presentation_pfe-universite-molay-seltan.pptx

10 logic+programming+with+prolog

  • 1. Chapter 10: Logic Programming Languages Principles of Programming Languages
  • 2. Contents •Predicate Calculus and Proving Theorems •The Basic Elements of Prolog •Simple Arithmetic and List in Prolog •Deficiencies of Prolog •Applications of Logic Programming
  • 3. •Another programming methodology: –Express programs in a form of symbolic logic –Use a logical inferencing process to produce results •Prolog Introduction
  • 4. •Proposition: a statement that may or may not be true –James I is a male –Catherine is a female –James I is parent of Charles I –Catherine is parent of Sophia •Compound term: functor + parameter lists male(james1) female(catherine) parent(james1, charles1) parent(catherine, sophia) •Two modes: facts or queries Proposition
  • 5. Logic Operators ¬ a negation a ˄ b conjunction a ˅ b disjunction a → b implication a ↔ b equivalence •Examples: (a ˄ b) → c
  • 6. Predicates •When we employ variables, the truth value of a statement is unknown until the variable has a specific value –X is a male –male(X) •Predicates turn into propositions if they have quantifiers ∀X (woman(X) → human(X)) ∃X (mother(mary, X) ˄ male(X))
  • 7. Clausal Form •A standard form for propositions B1 ˅ B2 ˅ …˅ Bn ← A1 ˄ A2 ˄ … ˄ Am •Examples mother(catherine, sophia) ← parent(catherine, sophia) ˄ female(catherine) father(louis, al) ˅ father(louis, violet) ← father(al, bob) ˄ mother(violet, bob) ˄ grandfather(louis, bob) •RHS = antecedent •LHS = consequent
  • 8. Automatic Theorem Proving •Resolution: if we have older(joanne, jake) ← mother(joanne, jake) wiser(joanne, jake) ← older(joanne, jake) •Using resolution, we can construct wiser(joanne, jake) ← mother(joanne, jake) •Mechanics: 1.AND two LHS to make the new LHS 2.AND two RHS to make the new RHS 3.Remove any terms that appear in the both sides
  • 9. Questions father(bob, jake) ˅ mother(bob, jake) ← parent(bob, jake) grandfather(bob, fred) ← father(bob, jake) ˄ father(jake, fred) What can we infer?
  • 10. Horn Clauses •When propositions are used for resolution, only a restricted kind of clausal form can be used •There are two forms of Horn clauses: –Headed: They have single atomic proposition on the LHS likes(bob, trout) ← likes(bob, fish) ˄ fish(trout) –Headless: Empty LHS father(bob, jake)
  • 11. Resolution •Ability to detect any inconsistency in a given set of propositions •Theorem is proved by –We are given a set of pertinent propositions –Negation of theorem as a new proposition –Resolution is used to find inconsistency •Hypotheses: a set of original propositions •Goal: negation of theorem stated as a proposition
  • 12. An Overview of Logic Programming •Declarative, rather that procedural or functional •Specify relation between objects –larger(3, 2) –father(tom, jane) •Separate logic from control: –Programmer declares what facts and relations are true –System determines how to use facts to solve problems –System instantiates variables in order to make relations true
  • 13. Example: Sorting a List •Describe the characteristics of a sorted list, not the process of rearranging a list sort(old_list, new_list) ← permute (old_list, new_list) ˄ sorted (new_list) sorted (list) ← j such that 1 j < n, list(j)  list (j+1)
  • 14. The Origins of Prolog •Alain Colmerauer at Marseille in 1970s –Natural language processing •University of Edinburgh –Automated theorem proving •1981, Japanese Fifth Generation Computing Systems choose Prolog as the basis •Despite the great assumed potential of logic programming and Prolog, little great significance had been covered
  • 15. Facts male(charlie). male(bob). female(alice). female(eve). parent(charlie, bob). parent(eve, bob). parent(charlie, alice). parent(eve, alice). •Headless Horn clauses •Every Prolog statement is terminated by a period
  • 16. Rules •Headed Horn clauses mother(X, Y) ← female(X) ˄ parent(X, Y) mother(X, Y) :- female(X), parent(X, Y).
  • 17. Queries •Let the computer know the facts, then ask it some questions – queries •Ask the system to prove or disprove a theorem ?- mother(sophia, george1). true •Ask the system to identify the instantiation of variables that make the query true ?- mother(X, sophia). X = Elizabeth false
  • 18. Some Prolog Syntax •Layer 0: constants and variables •Constants are: –Atoms: any string consisting of alphanumeric characters and underscore (_) that begins with a lowercase letter –Numbers •Variables are strings that begin with “_” or an uppercase letter
  • 19. Some Prolog Syntax const -> atom | number var -> ucase string | _ string atom -> lcase string letter -> ucase | lcase string -> epsilon | letter string | number string | _ string ucase -> A | ... | Z lcase -> a | ... | z number -> ...
  • 20. Some Prolog Syntax •Layer 1: terms •These are inductively defined: –Constants and variables are terms –Compound terms – applications of any n-ary functor to any n terms – are terms –Nothing else is a term
  • 21. Some Prolog Syntax •Layer 2: atomic formulae •These consist of an n-ary relation (also called predicate) applied to n terms •Formulae are either true or false; terms denote entities in the world •In Prolog, atomic formulae can be used in three ways: –As facts: in which they assert truth –As queries: in which they enquire about truth –As components of more complicated statements
  • 22. Some Prolog Syntax term -> const | var | functor “(“ term (“,” term)* “)” pred -> pname “(“ term (“,” term)* “)”
  • 23. Prolog Queries •A query is a proposed fact that is to be proven –If the query has no variables, returns true/false –If the query has variables, returns appropriate values of variables (called a substitution) ?- male(charlie). true ?- male(eve). false ?- female(Person). Person = alice; Person = eve; false
  • 24. Terms •Horn Clause = Clause •Consequent = Goal = Head •Antecedents = Subgoals = Tail •Horn Clause with No Tail = Fact •Horn Clause with Tail = Rule
  • 25. Some Prolog Syntax clause-> pred . | pred :- pred (“,” pred)* . term -> const | var | functor “(“ term (“,” term)* “)” pred -> pname “(“ term (“,” term)* “)” const -> ... var -> ...
  • 26. Variables •Variables may appear in the LHS or RHS of a clause. Prolog interprets free variables of a rule universally mother(X) :- female(X), parent(X, Y). In first-order logic: ∀X mother(X) ← ∃Y parent(X, Y) ˄ female(X) •Note that the Prolog query ?-q(X1, ..., Xn) means ∃X1,...,Xn q(X1, …, Xn)
  • 27. Execution of Prolog Programs •Unification: variable bindings •Backward Chaining/Top-Down Reasoning/Goal Directed Reasoning: reduces a goal to one or more subgoals •Backtracking: systematically searches for all possible solutions that can be obtained via unification and backchaining.
  • 28. Unification •Two atomic formulae unify iff they can be made syntactically identical by replacing their variables by other terms •For example: –parent(bob,Y) unifies with parent(bob,sue) by replacing Y by sue ?-parent(bob,Y)=parent(bob,sue) Y = sue ; false –parent(bob,Y) unifies with parent(X,sue) by replacing Y by sue and X by bob ?-parent(bob,Y)=parent(X,sue). Y = sue X = bob ; false
  • 29. Unification Algorithm Equation Action f(s1, ..., sn) = f(t1, ..., tn) Replace with s1 = t1, ..., sn = tn f(s1, ..., sn) = g(t1, ..., tn) where f ≠ g Halt with failure X = X Delete the equation t = X where t is not a variable Replace with X = t X = t where X does not occur in t and it occurs elsewhere Apply the substitution X = t to all other terms X = t where X occurs in t (but t ≠ X) Halt with failure
  • 30. Prolog Search Trees •Encapsulate unification, backward chaining, and backtracking –Internal nodes are ordered list of subgoals –Leaves are success nodes or failures, where computation can proceed no further –Edges are labeled with variable bindings that occur by unification •Describe all possible computation paths –There can be many success nodes –There can be infinite branches
  • 31. Prolog Search Tree /* program P clause # */ p(a). /* #1 */ p(X) :- q(X), r(X). /* #2 */ p(X) :- u(X). /* #3 */ q(X) :- s(X). /* #4 */ r(a). /* #5 */ r(b). /* #6 */ s(a). /* #7 */ s(b). /* #8 */ s(c). /* #9 */ u(d). /* #10 */ ?- p(X)
  • 32. [trace] ?- p(X). Call: (7) p(_G312) ? creep Exit: (7) p(a) ? creep X = a ; Redo: (7) p(_G312) ? creep Call: (8) q(_G312) ? creep Call: (9) s(_G312) ? creep Exit: (9) s(a) ? creep Exit: (8) q(a) ? creep Call: (8) r(a) ? creep Exit: (8) r(a) ? creep Exit: (7) p(a) ? creep X = a ; Redo: (9) s(_G312) ? creep Exit: (9) s(b) ? creep Exit: (8) q(b) ? creep Call: (8) r(b) ? creep Exit: (8) r(b) ? creep Exit: (7) p(b) ? creep X = b ; Redo: (9) s(_G312) ? creep Exit: (9) s(c) ? creep Exit: (8) q(c) ? creep Call: (8) r(c) ? creep Fail: (8) r(c) ? creep Redo: (7) p(_G312) ? creep Call: (8) u(_G312) ? creep Exit: (8) u(d) ? creep Exit: (7) p(d) ? creep X = d ; No
  • 33. Approaches •Prolog implementations use backward chaining •Top-down resolution, backward chaining –Begin with goal and attempt to find sequence that leads to set of facts in database –Works well with a small set of possibly correct answers •Bottom-up resolution, forward chaining –Begin with facts and rules of database and attempt to find sequence that leads to goal –Works well with a large set of possibly correct answers
  • 34. Subgoal Strategies •When goal has more than one subgoal, can use either –Depth-first search: find a complete proof for the first subgoal before working on others –Breadth-first search: work on all subgoals in parallel •Prolog uses depth-first search –Can be done with fewer computer resources
  • 35. Backtracking •With a goal with multiple subgoals, if fail to show truth of one of subgoals, reconsider previous subgoal to find an alternative solution: backtracking •Begin search where previous search left off •Can take lots of time and space because may find all possible proofs to every subgoal
  • 36. Trace •Built-in structure that displays instantiations at each step •Tracing model of execution - four events: –Call (beginning of attempt to satisfy goal) –Exit (when a goal has been satisfied) –Redo (when backtrack occurs) –Fail (when goal fails)
  • 37. Example likes(jake,chocolate). likes(jake,apricots). likes(darcie,licorice). likes(darcie,apricots). trace. likes(jake,X), likes(darcie,X).
  • 38. Simple Arithmetic •Prolog supports integer variables and integer arithmetic •is operator: takes an arithmetic expression as right operand and variable as left operand A is B / 17 + C •Not the same as an assignment statement!
  • 39. Example speed(ford,100). speed(chevy,105). speed(dodge,95). speed(volvo,80). time(ford,20). time(chevy,21). time(dodge,24). time(volvo,24). distance(X,Y) :- speed(X,Speed), time(X,Time), Y is Speed * Time.
  • 40. List Structures •List is a sequence of any number of elements •Elements can be atoms, atomic propositions, or other terms (including other lists) [apple, prune, grape, kumquat] [] (empty list) [H | T] (head H and tail T)
  • 41. Append Example append([], List, List). append([Head | List_1], List_2, [Head | List_3]) :- append (List_1, List_2, List_3).
  • 42. Another Example whatisme([], []). whatisme([Head | Tail], List) :- whatisme(Tail, Result), append(Result, [Head], List). •Try to write member rule to check if an element is a member of a list
  • 43. Deficiencies of Prolog •Resolution order control –Prolog always matches in the same order – user can control the ordering –Prolog allows explicit control of backtracking via cut operator -- “!”, which is actually a goal, not an operator •Always succeed but cannot be resatisfied through backtracking a, b, !, c, d –It is detrimental to one of the important advantages of logic programming – programs do not specify how solutions are to be found.
  • 44. Deficiencies of Prolog •The closed-world assumption –In Prolog, the truths are those that can be proved using its database –If there is insufficient information in the database to prove a query, it is not actually false, it fails –It relates to negation problem
  • 45. Deficiencies of Prolog •The negation problem –Stating that two atoms are not equal is not straightforward in Prolog –Bad solution: adding facts stating that they are not the same –Alternative: state in the goal that X must not be the same as Y sibling(X, Y) :- parent(M, X), parent(M, Y), not(X=Y) –However, not operator is not logical NOT, not(not(some_goal)) is not equivalent to some_goal –Reason: the use of Horn clause form prevents any negative conclusions
  • 46. Deficiencies of Prolog •Intrinsic limitations –Prolog has no idea of how to sort, other than simply to enumerate all permutations of the given list until it happens to create the one that has the list in sorted order –We must specify the details of how that sorting can be done – not logic programming any more –Resolution is not capable of doing this sorting
  • 47. Applications of Logic Programming •Relational database management systems –Logic programming is a natural match to the needs of implementing a RDBMS SQL is goal, tables are facts •Expert systems –Computer systems designed to emulate human expertise in some particular domain –In addition to the initial knowledge base, ES can learn from the process of being used •Natural language processing
  • 48. Summary •Symbolic logic provides basis for logic programming •Logic programs should be nonprocedural •Prolog statements are facts, rules, or goals •Resolution is the primary activity of a Prolog interpreter •Although there are a number of drawbacks with the current state of logic programming it has been used in a number of areas