0% found this document useful (0 votes)
1 views

1Artificial Intelligence Compiled Book- TutorialsD

The document provides an overview of the Artificial Intelligence course for B.Sc. (H) Computer Science at Delhi University, including topics covered, practical assignments, and references. It lists various subjects available for free resources on TutorialsDuniya.com, such as programming languages and computer science fundamentals. Additionally, it details specific Prolog programming tasks related to AI concepts and techniques.

Uploaded by

kagradiksha17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

1Artificial Intelligence Compiled Book- TutorialsD

The document provides an overview of the Artificial Intelligence course for B.Sc. (H) Computer Science at Delhi University, including topics covered, practical assignments, and references. It lists various subjects available for free resources on TutorialsDuniya.com, such as programming languages and computer science fundamentals. Additionally, it details specific Prolog programming tasks related to AI concepts and techniques.

Uploaded by

kagradiksha17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 336

Delhi University (DU)

B.Sc. (H) Computer Science


Sem - VI

Artificial Intelligence

TutorialsDuniya.com
Visit https://www.tutorialsduniya.com for
Compiled books, Notes, books, programs,
question papers with solutions etc.

Facebook: https://www.facebook.com/tutorialsduniya
Youtube: https://www.youtube.com/user/TutorialsDuniya
LinkedIn: https://www.linkedin.com/company/tutorialsduniya
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers
with solutions etc.
December 20, 2017
Core Paper XIII: Artificial Intelligence

Sr. No. Topic Chapter Reference # of


Lecture
1. Introduction:
Introduction to Artificial Intelligence, 1 (1.1, 1.4) 2
Background, Turing Test and Rational Agent
approaches to AI, Applications of AI.
6
Introduction to Intelligent Agents, their
2 (Complete) 2
structure, behavior and environment.

m
2. Problem Solving and Searching Techniques:
Problem Characteristics, Production Systems, 2.1-2.3 3
Control Strategies: Breadth First Search, Depth

co
First Search, Hill climbing and its Variations,
Heuristics Search Techniques: Best First
Search, A* algorithm, Constraint Satisfaction 3.1 - 3.3.2, 3

a.
3.5, 3.6 12
Problem, Means-End Analysis.
Introduction to Game Playing, Min-Max and 12.1 - 12.3 3

iy
Alpha-Beta pruning algorithms.
3. Knowledge Representation:
un
Introduction to First Order Predicate Logic, 4 (4.1 - 4.9) 1
Resolution Principle, Unification.
Semantic Nets, Conceptual Dependencies,
Frames, and Scripts, Production Rules, 7 (Complete) 1 14
lsD

Conceptual Graphs.
Programming in Logic (PROLOG). 1, 2, 3, 4 4
4. Dealing with Uncertainty and Inconsistencies:
Truth Maintenance System, Default Reasoning, 5 (5.1 - 5.3) 1
ia

Probabilistic Reasoning: Bayesian Probabilistic 6 (6.1 - 6.3) 8


Inference, Possible World Representations.
or

5. Understanding Natural Languages: 12 (12.1 - 1


Parsing Techniques, Context-Free and 12.4)
Transformational Grammars, Recursive and 10
t

Augmented Transition Nets.


Tu

References:

1. DAN. W. Patterson, Introduction to A.I and Expert Systems – PHI, 2004.


2. Russell & Norvig, Artificial Intelligence-A Modern Approach, LPE, Pearson Ptentice Hall, 2nd
edition, 2005.
3. Rich & Knight, Artificial Intelligence- Tata McGraw Hill, 3rd Edition, 2009.
4. William F. Clocksin, Christopher S. Mellish, Programming in Prolog, Springer-Verlag Berlin, 5th
edition, 2003.
5. Ivan Bratko, Prolog Programming for Artificial Intelligence, Addison-Wesley, Pearson
Education, 3rd Edition, 2000.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers


with solutions etc.
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Note: Solutions of all these programs are available on https://www.tutorialsduniya.com

LIST OF PRACTICALS CORE PAPER XIII: ARTIFICIAL INTELLIGENCE

1. Write a prolog program to calculate the sum of two numbers.


2. Write a Prolog program to implement max(X, Y, M) so that M is the maximum of two numbers X
and Y.
3. Write a program in PROLOG to implement factorial (N, F) where F represents the factorial of a
number N.
4. Write a program in PROLOG to implement generate_fib(N,T) where T represents the Nth term of
the fibonacci series.
5. Write a Prolog program to implement GCD of two numbers.

m
6. Write a Prolog program to implement power (Num,Pow, Ans) : where Num is raised to the
power Pow to get Ans.

co
7. Prolog program to implement multi (N1, N2, R) : where N1 and N2 denotes the numbers to be
multiplied and R represents the result.
8. Write a program in PROLOG to implement towerofhanoi (N) where N represents the number of

a.
discs
9. Consider a cyclic directed graph [edge (p, q), edge (q, r), edge (q, r), edge (q, s), edge (s,t)] where

iy
edge (A,B) is a predicate indicating directed edge in a graph from a node A to a node B. Write a
program to check whether there is a route from one node to another node.
un
10. Write a Prolog program to implement memb(X, L): to check whether X is a member of L or not.
11. Write a Prolog program to implement conc (L1, L2, L3) where L2 is the list to be appended with
L1 to get the resulted list L3.
lsD

12. Write a Prolog program to implement reverse (L, R) where List L is original and List R is reversed
list.
13. Write a program in PROLOG to implement palindrome (L) which checks whether a list L is a
palindrome or not.
ia

14. Write a Prolog program to implement sumlist(L, S) so that S is the sum of a given list L.
15. Write a Prolog program to implement two predicates evenlength(List) and oddlength(List) so
or

that they are true if their argument is a list of even or odd length respectively
16. Write a Prolog program to implement nth_element (N, L, X) where N is the desired position, L is
a list and X represents the Nth element of L.
t

17. Write a program in PROLOG to implement remove_dup (L, R) where L denotes the list with some
Tu

duplicates and the list R denotes the list with duplicates removed.
18. Write a Prolog program to implement maxlist(L, M) so that M is the maximum number in the list
19. Write a prolog program to implement insert_nth(I, N, L, R) that inserts an item I into Nth
position of list L to generate a list R.
20. Write a Program in PROLOG to implement sublist(S, L) that checks whether the list S is the
sublist of list L or not. (Check for sequence or the part in the same order).
21. Write a Prolog program to implement delete_nth (N, L, R) that removes the element on Nth
position from a list L to generate a list R.
22. Write a program in PROLOG to implement delete_all (X, L, R) where X denotes the element
whose all occurrences has to be deleted from list L to obtain list R.

Note: Solutions of all these programs are available on https://www.tutorialsduniya.com


Note: Solutions of all these programs are available on https://www.tutorialsduniya.com

23. Write a program in PROLOG to implement merge (L1, L2, L3) where L1 is first ordered list and L2
is second ordered list and L3 represents the merged list.
24. Write a PROLOG program that will take grammar rules in the following format:
NT  (NT | T)*
Where NT is any nonterminal, T is any terminal and Kleene star (*) signifies any number of
repetitions, and generate the corresponding top-down parser, that is:
sentence  noun-phrase, verb-phrase
determiner  [the]

m
will generate the following:
sentence (I, O) :- noun-phrase(I,R), verb-phrase (R,O).

co
determiner ([the|X], X) :- !.
25. Write a prolog program that implements Semantic Networks (ATN/RTN).

a.
iy
un
lsD
ia
t or
Tu

Note: Solutions of all these programs are available on https://www.tutorialsduniya.com


Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

For copyright related query, contact us at [email protected]

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Contents

m
co
a.
iy
un
sD
al
ri
to
Tu

Get FREE Compiled Books, Notes, Programs, Books, Question Papers with
Solution* etc of following subjects from https://www.tutorialsduniya.com.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with

a.
Solution* etc of following subjects from https://www.tutorialsduniya.com.
iy
un
sD
al
ri
to
Tu

For any copyright query, contact us at [email protected]

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Artificial Intelligence
A Modern Approach

m
Third Edition

co
a.
Stuart J. Russell and Peter Norvig

iy
un
sD
al

Contributing writers:
Ernest Davis
ri

Douglas D. Edwards
David Forsyth
to

Nicholas J. Hay
Jitendra M. Malik
Vibhu Mittal
Tu

Mehran Sahami
Sebastian Thrun

Upper Saddle River Boston Columbus San Francisco New York


Indianapolis London Toronto Sydney Singapore Tokyo Montreal
Dubai Madrid Hong Kong Mexico City Munich Paris Amsterdam Cape Town

For any copyright query, contact us at [email protected]

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Contents
I Artificial Intelligence
1 Introduction 1
1.1 What Is AI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 The Foundations of Artificial Intelligence . . . . . . . . . . . . . . . . . . 5

m
1.3 The History of Artificial Intelligence . . . . . . . . . . . . . . . . . . . . 16
1.4 The State of the Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.5 Summary, Bibliographical and Historical Notes, Exercises . . . . . . . . . 29

co
2 Intelligent Agents 34
2.1 Agents and Environments . . . . . . . . . . . . . . . . . . . . . . . . . . 34

a.
2.2 Good Behavior: The Concept of Rationality . . . . . . . . . . . . . . . . 36
2.3 The Nature of Environments . . . . . . . . . . . . . . . . . . . . . . . . . 40

iy
2.4 The Structure of Agents . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.5 Summary, Bibliographical and Historical Notes, Exercises . . . . . . . . . 59

un
II Problem-solving
3 Solving Problems by Searching 64
3.1 Problem-Solving Agents . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
sD
3.2 Example Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.3 Searching for Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.4 Uninformed Search Strategies . . . . . . . . . . . . . . . . . . . . . . . . 81
al

3.5 Informed (Heuristic) Search Strategies . . . . . . . . . . . . . . . . . . . 92


3.6 Heuristic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.7 Summary, Bibliographical and Historical Notes, Exercises . . . . . . . . . 108
ri

4 Beyond Classical Search 120


to

4.1 Local Search Algorithms and Optimization Problems . . . . . . . . . . . 120


4.2 Local Search in Continuous Spaces . . . . . . . . . . . . . . . . . . . . . 129
4.3 Searching with Nondeterministic Actions . . . . . . . . . . . . . . . . . . 133
Tu

4.4 Searching with Partial Observations . . . . . . . . . . . . . . . . . . . . . 138


4.5 Online Search Agents and Unknown Environments . . . . . . . . . . . . 147
4.6 Summary, Bibliographical and Historical Notes, Exercises . . . . . . . . . 153

5 Adversarial Search 161


5.1 Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
5.2 Optimal Decisions in Games . . . . . . . . . . . . . . . . . . . . . . . . 163
5.3 Alpha–Beta Pruning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
5.4 Imperfect Real-Time Decisions . . . . . . . . . . . . . . . . . . . . . . . 171
5.5 Stochastic Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

xiii

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri

For any copyright query, contact us at [email protected]


to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
a lsD
ri
to
Tu

For any copyright query, contact us at [email protected]

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
For any copyright query, contact us at [email protected]
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

W. F. Clocksin · C.S. Mellish

Programming

om
in Prolog

c
Fifth Edition

a.
iy
un
sD
al
ri
to
Tu

123
For any copyright query, contact us at [email protected]

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com
for Notes, books,programs, question paperswith solutions et

Table of Contents

m
co
a.
1 Tutorial Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Gives the student a feel for what it is like to program in Prolog.
Introduces objects, relationships, facts, rules, variables
iy
un
1.1 Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Objects and Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
sD

1.5 Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.7 Conjunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.8 Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
al

1.9 Summary and Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22


ri

2 A Closer Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
More detailed presentation of Prolog syntax and data structures
to

2.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.1.1 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Tu

2.1.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1.3 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.4 Equality and Unification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.5 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.6 Summary of Satisfying Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.6.1 Successful satisfaction of a conjunction of goals . . . . . . . . . . 38

Visit https://www.tutorialsduniya.com
for Notes, books,programs, question paperswith solutions et
Visit https://www.tutorialsduniya.com
for Notes, books,programs, question paperswith solutions et

X Table of Contents

2.6.2 Consideration of goals in backtracking . . . . . . . . . . . . . . . . . . 42


2.6.3 Unification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

3 Using Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47


Representing objects and relationships by using trees and lists.
Developing several standard Prolog programming techniques

m
3.1 Structures and Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

co
3.3 Recursive Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.4 Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.5 Recursive Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
3.6 Joining Structures Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

a.
3.7 Accumulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.8 Difference Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

4 iy
Backtracking and the “Cut” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
un
How a set of clauses generates a set of solutions. Using “cut” to modify
the control sequence of running Prolog programs
4.1 Generating Multiple Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
sD

4.2 The “Cut” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80


4.3 Common Uses of the Cut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.3.1 Confirming the Choice of a Rule . . . . . . . . . . . . . . . . . . . . . . . 85
4.3.2 The “cut-fail” Combination . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
al

4.3.3 Terminating a “generate and test” . . . . . . . . . . . . . . . . . . . . . . . 92


4.4 Problems with the Cut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
ri

5 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99


to

Facilities available for the input and output of characters and structures.
Developing a program to read sentences from the user and represent
the structure as a list of words, which can be used with the Grammar Rules
Tu

of Chapter 9
5.1 Reading and Writing Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
5.1.1 Reading Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
5.1.2 Writing Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
5.2 Reading and Writing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
5.2.1 Reading Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
5.2.2 Writing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

Visit https://www.tutorialsduniya.com
for Notes, books,programs, question paperswith solutions et
Chapter Topic
Introduction to Artificial Intelligence,
1 (1.1, 1.4) Background, Turing Test and Rational Agent
approaches to AI, Applications of AI.

Introduction to Intelligent Agents, their


2 (Complete)
structure, behavior and environment.

m
co
a.
TutorialsDuniya.com
iy
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with
un
Solution* etc of following subjects from https://www.tutorialsduniya.com.
lsD

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
ia

 Data Structures  Operating Systems


 Computer Networks  Algorithms
or

 Android Programming  DataBase Management Systems


t
Tu

 PHP Programming  Software Engineering


 JavaScript  Theory of Computation
 Java Server Pages  Operational Research
 Python  System Programming
 Microprocessor  Data Mining
 Artificial Intelligence  Computer Graphics
 Machine Learning  Data Science
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

1 INTRODUCTION

m
co
In which we try to explain why we consider artificial intelligence to be a subject
most worthy of study, and in which we try to decide what exactly it is, this being a
good thing to decide before embarking.

a.
iy
INTELLIGENCE We call ourselves Homo sapiens—man the wise—because our intelligence is so important
to us. For thousands of years, we have tried to understand how we think; that is, how a mere
handful of matter can perceive, understand, predict, and manipulate a world far larger and

un
ARTIFICIAL
INTELLIGENCE more complicated than itself. The field of artificial intelligence, or AI, goes further still: it
attempts not just to understand but also to build intelligent entities.
AI is one of the newest fields in science and engineering. Work started in earnest soon
after World War II, and the name itself was coined in 1956. Along with molecular biology,
sD
AI is regularly cited as the “field I would most like to be in” by scientists in other disciplines.
A student in physics might reasonably feel that all the good ideas have already been taken by
Galileo, Newton, Einstein, and the rest. AI, on the other hand, still has openings for several
al

full-time Einsteins and Edisons.


AI currently encompasses a huge variety of subfields, ranging from the general (learning
and perception) to the specific, such as playing chess, proving mathematical theorems, writing
ri

poetry, driving a car on a crowded street, and diagnosing diseases. AI is relevant to any
intellectual task; it is truly a universal field.
to

1.1 W HAT I S AI?


Tu

We have claimed that AI is exciting, but we have not said what it is. In Figure 1.1 we see
eight definitions of AI, laid out along two dimensions. The definitions on top are concerned
with thought processes and reasoning, whereas the ones on the bottom address behavior. The
definitions on the left measure success in terms of fidelity to human performance, whereas
RATIONALITY the ones on the right measure against an ideal performance measure, called rationality. A
system is rational if it does the “right thing,” given what it knows.
Historically, all four approaches to AI have been followed, each by different people
with different methods. A human-centered approach must be in part an empirical science, in-

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

2 Chapter 1. Introduction

Thinking Humanly Thinking Rationally


“The exciting new effort to make comput- “The study of mental faculties through the
ers think . . . machines with minds, in the use of computational models.”
full and literal sense.” (Haugeland, 1985) (Charniak and McDermott, 1985)
“[The automation of] activities that we “The study of the computations that make
associate with human thinking, activities it possible to perceive, reason, and act.”
such as decision-making, problem solv- (Winston, 1992)

m
ing, learning . . .” (Bellman, 1978)
Acting Humanly Acting Rationally

co
“The art of creating machines that per- “Computational Intelligence is the study
form functions that require intelligence of the design of intelligent agents.” (Poole
when performed by people.” (Kurzweil, et al., 1998)

a.
1990)
“The study of how to make computers do “AI . . . is concerned with intelligent be-

iy
things at which, at the moment, people are havior in artifacts.” (Nilsson, 1998)
better.” (Rich and Knight, 1991)

un
Figure 1.1 Some definitions of artificial intelligence, organized into four categories.

volving observations and hypotheses about human behavior. A rationalist1 approach involves
a combination of mathematics and engineering. The various group have both disparaged and
sD
helped each other. Let us look at the four approaches in more detail.

1.1.1 Acting humanly: The Turing Test approach


al

TURING TEST The Turing Test, proposed by Alan Turing (1950), was designed to provide a satisfactory
operational definition of intelligence. A computer passes the test if a human interrogator, after
ri

posing some written questions, cannot tell whether the written responses come from a person
or from a computer. Chapter 26 discusses the details of the test and whether a computer would
really be intelligent if it passed. For now, we note that programming a computer to pass a
to

rigorously applied test provides plenty to work on. The computer would need to possess the
following capabilities:
Tu

NATURAL LANGUAGE
PROCESSING • natural language processing to enable it to communicate successfully in English;
KNOWLEDGE
REPRESENTATION • knowledge representation to store what it knows or hears;
AUTOMATED
REASONING • automated reasoning to use the stored information to answer questions and to draw
new conclusions;
MACHINE LEARNING • machine learning to adapt to new circumstances and to detect and extrapolate patterns.
1 By distinguishing between human and rational behavior, we are not suggesting that humans are necessarily
“irrational” in the sense of “emotionally unstable” or “insane.” One merely need note that we are not perfect:
not all chess players are grandmasters; and, unfortunately, not everyone gets an A on the exam. Some systematic
errors in human reasoning are cataloged by Kahneman et al. (1982).

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 1.1. What Is AI? 3

Turing’s test deliberately avoided direct physical interaction between the interrogator and the
computer, because physical simulation of a person is unnecessary for intelligence. However,
TOTAL TURING TEST the so-called total Turing Test includes a video signal so that the interrogator can test the
subject’s perceptual abilities, as well as the opportunity for the interrogator to pass physical
objects “through the hatch.” To pass the total Turing Test, the computer will need
COMPUTER VISION • computer vision to perceive objects, and
ROBOTICS • robotics to manipulate objects and move about.

m
These six disciplines compose most of AI, and Turing deserves credit for designing a test
that remains relevant 60 years later. Yet AI researchers have devoted little effort to passing

co
the Turing Test, believing that it is more important to study the underlying principles of in-
telligence than to duplicate an exemplar. The quest for “artificial flight” succeeded when the
Wright brothers and others stopped imitating birds and started using wind tunnels and learn-

a.
ing about aerodynamics. Aeronautical engineering texts do not define the goal of their field
as making “machines that fly so exactly like pigeons that they can fool even other pigeons.”

iy
1.1.2 Thinking humanly: The cognitive modeling approach
If we are going to say that a given program thinks like a human, we must have some way of

un
determining how humans think. We need to get inside the actual workings of human minds.
There are three ways to do this: through introspection—trying to catch our own thoughts as
they go by; through psychological experiments—observing a person in action; and through
sD
brain imaging—observing the brain in action. Once we have a sufficiently precise theory of
the mind, it becomes possible to express the theory as a computer program. If the program’s
input–output behavior matches corresponding human behavior, that is evidence that some of
the program’s mechanisms could also be operating in humans. For example, Allen Newell
al

and Herbert Simon, who developed GPS, the “General Problem Solver” (Newell and Simon,
1961), were not content merely to have their program solve problems correctly. They were
ri

more concerned with comparing the trace of its reasoning steps to traces of human subjects
COGNITIVE SCIENCE solving the same problems. The interdisciplinary field of cognitive science brings together
computer models from AI and experimental techniques from psychology to construct precise
to

and testable theories of the human mind.


Cognitive science is a fascinating field in itself, worthy of several textbooks and at least
Tu

one encyclopedia (Wilson and Keil, 1999). We will occasionally comment on similarities or
differences between AI techniques and human cognition. Real cognitive science, however, is
necessarily based on experimental investigation of actual humans or animals. We will leave
that for other books, as we assume the reader has only a computer for experimentation.
In the early days of AI there was often confusion between the approaches: an author
would argue that an algorithm performs well on a task and that it is therefore a good model
of human performance, or vice versa. Modern authors separate the two kinds of claims;
this distinction has allowed both AI and cognitive science to develop more rapidly. The two
fields continue to fertilize each other, most notably in computer vision, which incorporates
neurophysiological evidence into computational models.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

4 Chapter 1. Introduction

1.1.3 Thinking rationally: The “laws of thought” approach


The Greek philosopher Aristotle was one of the first to attempt to codify “right thinking,” that
SYLLOGISM is, irrefutable reasoning processes. His syllogisms provided patterns for argument structures
that always yielded correct conclusions when given correct premises—for example, “Socrates
is a man; all men are mortal; therefore, Socrates is mortal.” These laws of thought were
LOGIC supposed to govern the operation of the mind; their study initiated the field called logic.
Logicians in the 19th century developed a precise notation for statements about all kinds
of objects in the world and the relations among them. (Contrast this with ordinary arithmetic

m
notation, which provides only for statements about numbers.) By 1965, programs existed
that could, in principle, solve any solvable problem described in logical notation. (Although

co
LOGICIST if no solution exists, the program might loop forever.) The so-called logicist tradition within
artificial intelligence hopes to build on such programs to create intelligent systems.
There are two main obstacles to this approach. First, it is not easy to take informal

a.
knowledge and state it in the formal terms required by logical notation, particularly when
the knowledge is less than 100% certain. Second, there is a big difference between solving
a problem “in principle” and solving it in practice. Even problems with just a few hundred

iy
facts can exhaust the computational resources of any computer unless it has some guidance
as to which reasoning steps to try first. Although both of these obstacles apply to any attempt

un
to build computational reasoning systems, they appeared first in the logicist tradition.

1.1.4 Acting rationally: The rational agent approach


sD
AGENT An agent is just something that acts (agent comes from the Latin agere, to do). Of course,
all computer programs do something, but computer agents are expected to do more: operate
autonomously, perceive their environment, persist over a prolonged time period, adapt to
RATIONAL AGENT change, and create and pursue goals. A rational agent is one that acts so as to achieve the
al

best outcome or, when there is uncertainty, the best expected outcome.
In the “laws of thought” approach to AI, the emphasis was on correct inferences. Mak-
ri

ing correct inferences is sometimes part of being a rational agent, because one way to act
rationally is to reason logically to the conclusion that a given action will achieve one’s goals
and then to act on that conclusion. On the other hand, correct inference is not all of ration-
to

ality; in some situations, there is no provably correct thing to do, but something must still be
done. There are also ways of acting rationally that cannot be said to involve inference. For
Tu

example, recoiling from a hot stove is a reflex action that is usually more successful than a
slower action taken after careful deliberation.
All the skills needed for the Turing Test also allow an agent to act rationally. Knowledge
representation and reasoning enable agents to reach good decisions. We need to be able to
generate comprehensible sentences in natural language to get by in a complex society. We
need learning not only for erudition, but also because it improves our ability to generate
effective behavior.
The rational-agent approach has two advantages over the other approaches. First, it
is more general than the “laws of thought” approach because correct inference is just one
of several possible mechanisms for achieving rationality. Second, it is more amenable to

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 1.2. The Foundations of Artificial Intelligence 5

scientific development than are approaches based on human behavior or human thought. The
standard of rationality is mathematically well defined and completely general, and can be
“unpacked” to generate agent designs that provably achieve it. Human behavior, on the other
hand, is well adapted for one specific environment and is defined by, well, the sum total
of all the things that humans do. This book therefore concentrates on general principles
of rational agents and on components for constructing them. We will see that despite the
apparent simplicity with which the problem can be stated, an enormous variety of issues
come up when we try to solve it. Chapter 2 outlines some of these issues in more detail.

m
One important point to keep in mind: We will see before too long that achieving perfect
rationality—always doing the right thing—is not feasible in complicated environments. The
computational demands are just too high. For most of the book, however, we will adopt the

co
working hypothesis that perfect rationality is a good starting point for analysis. It simplifies
the problem and provides the appropriate setting for most of the foundational material in
LIMITED
the field. Chapters 5 and 17 deal explicitly with the issue of limited rationality—acting

a.
RATIONALITY
appropriately when there is not enough time to do all the computations one might like.

iy
1.2 T HE F OUNDATIONS OF A RTIFICIAL I NTELLIGENCE

un
In this section, we provide a brief history of the disciplines that contributed ideas, viewpoints,
and techniques to AI. Like any history, this one is forced to concentrate on a small number
of people, events, and ideas and to ignore others that also were important. We organize the
sD
history around a series of questions. We certainly would not wish to give the impression that
these questions are the only ones the disciplines address or that the disciplines have all been
working toward AI as their ultimate fruition.
al

1.2.1 Philosophy
• Can formal rules be used to draw valid conclusions?
ri

• How does the mind arise from a physical brain?


• Where does knowledge come from?
to

• How does knowledge lead to action?


Aristotle (384–322 B . C .), whose bust appears on the front cover of this book, was the first
Tu

to formulate a precise set of laws governing the rational part of the mind. He developed an
informal system of syllogisms for proper reasoning, which in principle allowed one to gener-
ate conclusions mechanically, given initial premises. Much later, Ramon Lull (d. 1315) had
the idea that useful reasoning could actually be carried out by a mechanical artifact. Thomas
Hobbes (1588–1679) proposed that reasoning was like numerical computation, that “we add
and subtract in our silent thoughts.” The automation of computation itself was already well
under way. Around 1500, Leonardo da Vinci (1452–1519) designed but did not build a me-
chanical calculator; recent reconstructions have shown the design to be functional. The first
known calculating machine was constructed around 1623 by the German scientist Wilhelm
Schickard (1592–1635), although the Pascaline, built in 1642 by Blaise Pascal (1623–1662),

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

28 Chapter 1. Introduction

new patterns that help label new examples. Banko and Brill (2001) show that techniques
like this perform even better as the amount of available text goes from a million words to a
billion and that the increase in performance from using more data exceeds any difference in
algorithm choice; a mediocre algorithm with 100 million words of unlabeled training data
outperforms the best known algorithm with 1 million words.
As another example, Hays and Efros (2007) discuss the problem of filling in holes in a
photograph. Suppose you use Photoshop to mask out an ex-friend from a group photo, but
now you need to fill in the masked area with something that matches the background. Hays

m
and Efros defined an algorithm that searches through a collection of photos to find something
that will match. They found the performance of their algorithm was poor when they used
a collection of only ten thousand photos, but crossed a threshold into excellent performance

co
when they grew the collection to two million photos.
Work like this suggests that the “knowledge bottleneck” in AI—the problem of how to
express all the knowledge that a system needs—may be solved in many applications by learn-

a.
ing methods rather than hand-coded knowledge engineering, provided the learning algorithms
have enough data to go on (Halevy et al., 2009). Reporters have noticed the surge of new ap-

iy
plications and have written that “AI Winter” may be yielding to a new Spring (Havenstein,
2005). As Kurzweil (2005) writes, “today, many thousands of AI applications are deeply
embedded in the infrastructure of every industry.”

1.4 un
T HE S TATE OF THE A RT
sD
What can AI do today? A concise answer is difficult because there are so many activities in
so many subfields. Here we sample a few applications; others appear throughout the book.
al

Robotic vehicles: A driverless robotic car named S TANLEY sped through the rough
terrain of the Mojave dessert at 22 mph, finishing the 132-mile course first to win the 2005
ri

DARPA Grand Challenge. S TANLEY is a Volkswagen Touareg outfitted with cameras, radar,
and laser rangefinders to sense the environment and onboard software to command the steer-
ing, braking, and acceleration (Thrun, 2006). The following year CMU’s B OSS won the Ur-
to

ban Challenge, safely driving in traffic through the streets of a closed Air Force base, obeying
traffic rules and avoiding pedestrians and other vehicles.
Tu

Speech recognition: A traveler calling United Airlines to book a flight can have the en-
tire conversation guided by an automated speech recognition and dialog management system.
Autonomous planning and scheduling: A hundred million miles from Earth, NASA’s
Remote Agent program became the first on-board autonomous planning program to control
the scheduling of operations for a spacecraft (Jonsson et al., 2000). R EMOTE AGENT gen-
erated plans from high-level goals specified from the ground and monitored the execution of
those plans—detecting, diagnosing, and recovering from problems as they occurred. Succes-
sor program MAPGEN (Al-Chang et al., 2004) plans the daily operations for NASA’s Mars
Exploration Rovers, and MEXAR2 (Cesta et al., 2007) did mission planning—both logistics
and science planning—for the European Space Agency’s Mars Express mission in 2008.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 1.5. Summary 29

Game playing: IBM’s D EEP B LUE became the first computer program to defeat the
world champion in a chess match when it bested Garry Kasparov by a score of 3.5 to 2.5 in
an exhibition match (Goodman and Keene, 1997). Kasparov said that he felt a “new kind of
intelligence” across the board from him. Newsweek magazine described the match as “The
brain’s last stand.” The value of IBM’s stock increased by $18 billion. Human champions
studied Kasparov’s loss and were able to draw a few matches in subsequent years, but the
most recent human-computer matches have been won convincingly by the computer.
Spam fighting: Each day, learning algorithms classify over a billion messages as spam,

m
saving the recipient from having to waste time deleting what, for many users, could comprise
80% or 90% of all messages, if not classified away by algorithms. Because the spammers are
continually updating their tactics, it is difficult for a static programmed approach to keep up,

co
and learning algorithms work best (Sahami et al., 1998; Goodman and Heckerman, 2004).
Logistics planning: During the Persian Gulf crisis of 1991, U.S. forces deployed a
Dynamic Analysis and Replanning Tool, DART (Cross and Walker, 1994), to do automated

a.
logistics planning and scheduling for transportation. This involved up to 50,000 vehicles,
cargo, and people at a time, and had to account for starting points, destinations, routes, and

iy
conflict resolution among all parameters. The AI planning techniques generated in hours
a plan that would have taken weeks with older methods. The Defense Advanced Research
Project Agency (DARPA) stated that this single application more than paid back DARPA’s

un
30-year investment in AI.
Robotics: The iRobot Corporation has sold over two million Roomba robotic vacuum
cleaners for home use. The company also deploys the more rugged PackBot to Iraq and
sD
Afghanistan, where it is used to handle hazardous materials, clear explosives, and identify
the location of snipers.
Machine Translation: A computer program automatically translates from Arabic to
English, allowing an English speaker to see the headline “Ardogan Confirms That Turkey
al

Would Not Accept Any Pressure, Urging Them to Recognize Cyprus.” The program uses a
statistical model built from examples of Arabic-to-English translations and from examples of
ri

English text totaling two trillion words (Brants et al., 2007). None of the computer scientists
on the team speak Arabic, but they do understand statistics and machine learning algorithms.
to

These are just a few examples of artificial intelligence systems that exist today. Not
magic or science fiction—but rather science, engineering, and mathematics, to which this
book provides an introduction.
Tu

1.5 S UMMARY

This chapter defines AI and establishes the cultural background against which it has devel-
oped. Some of the important points are as follows:
• Different people approach AI with different goals in mind. Two important questions to
ask are: Are you concerned with thinking or behavior? Do you want to model humans
or work from an ideal standard?

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Exercises 31

readable account of the philosophical and practical problems of AI. Significant early papers
in AI are anthologized in the collections by Webber and Nilsson (1981) and by Luger (1995).
The Encyclopedia of AI (Shapiro, 1992) contains survey articles on almost every topic in
AI, as does Wikipedia. These articles usually provide a good entry point into the research
literature on each topic. An insightful and comprehensive history of AI is given by Nils
Nillson (2009), one of the early pioneers of the field.
The most recent work appears in the proceedings of the major AI conferences: the bi-
ennial International Joint Conference on AI (IJCAI), the annual European Conference on AI

m
(ECAI), and the National Conference on AI, more often known as AAAI, after its sponsoring
organization. The major journals for general AI are Artificial Intelligence, Computational
Intelligence, the IEEE Transactions on Pattern Analysis and Machine Intelligence, IEEE In-

co
telligent Systems, and the electronic Journal of Artificial Intelligence Research. There are also
many conferences and journals devoted to specific areas, which we cover in the appropriate
chapters. The main professional societies for AI are the American Association for Artificial

a.
Intelligence (AAAI), the ACM Special Interest Group in Artificial Intelligence (SIGART),
and the Society for Artificial Intelligence and Simulation of Behaviour (AISB). AAAI’s AI

iy
Magazine contains many topical and tutorial articles, and its Web site, aaai.org, contains
news, tutorials, and background information.

un
E XERCISES
sD
These exercises are intended to stimulate discussion, and some might be set as term projects.
Alternatively, preliminary attempts can be made now, and these attempts can be reviewed
after the completion of the book.
al
1.1 Define in your own words: (a) intelligence, (b) artificial intelligence, (c) agent, (d)
rationality, (e) logical reasoning.
1.2 Read Turing’s original paper on AI (Turing, 1950). In the paper, he discusses several
ri

objections to his proposed enterprise and his test for intelligence. Which objections still carry
weight? Are his refutations valid? Can you think of new objections arising from develop-
to

ments since he wrote the paper? In the paper, he predicts that, by the year 2000, a computer
will have a 30% chance of passing a five-minute Turing Test with an unskilled interrogator.
What chance do you think a computer would have today? In another 50 years?
Tu

1.3 Are reflex actions (such as flinching from a hot stove) rational? Are they intelligent?
1.4 Suppose we extend Evans’s A NALOGY program so that it can score 200 on a standard
IQ test. Would we then have a program more intelligent than a human? Explain.
1.5 The neural structure of the sea slug Aplysia has been widely studied (first by Nobel
Laureate Eric Kandel) because it has only about 20,000 neurons, most of them large and
easily manipulated. Assuming that the cycle time for an Aplysia neuron is roughly the same
as for a human neuron, how does the computational power, in terms of memory updates per
second, compare with the high-end computer described in Figure 1.3?

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

32 Chapter 1. Introduction

1.6 How could introspection—reporting on one’s inner thoughts—be inaccurate? Could I


be wrong about what I’m thinking? Discuss.
1.7 To what extent are the following computer systems instances of artificial intelligence:
• Supermarket bar code scanners.
• Web search engines.
• Voice-activated telephone menus.
• Internet routing algorithms that respond dynamically to the state of the network.

m
1.8 Many of the computational models of cognitive activities that have been proposed in-
volve quite complex mathematical operations, such as convolving an image with a Gaussian

co
or finding a minimum of the entropy function. Most humans (and certainly all animals) never
learn this kind of mathematics at all, almost no one learns it before college, and almost no
one can compute the convolution of a function with a Gaussian in their head. What sense

a.
does it make to say that the “vision system” is doing this kind of mathematics, whereas the
actual person has no idea how to do it?

iy
1.9 Why would evolution tend to result in systems that act rationally? What goals are such
systems designed to achieve?

un 1.10 Is AI a science, or is it engineering? Or neither or both? Explain.


1.11 “Surely computers cannot be intelligent—they can do only what their programmers
tell them.” Is the latter statement true, and does it imply the former?
sD
1.12 “Surely animals cannot be intelligent—they can do only what their genes tell them.”
Is the latter statement true, and does it imply the former?
1.13 “Surely animals, humans, and computers cannot be intelligent—they can do only what
al

their constituent atoms are told to do by the laws of physics.” Is the latter statement true, and
does it imply the former?
ri

1.14 Examine the AI literature to discover whether the following tasks can currently be
solved by computers:
to

a. Playing a decent game of table tennis (Ping-Pong).


b. Driving in the center of Cairo, Egypt.
Tu

c. Driving in Victorville, California.


d. Buying a week’s worth of groceries at the market.
e. Buying a week’s worth of groceries on the Web.
f. Playing a decent game of bridge at a competitive level.
g. Discovering and proving new mathematical theorems.
h. Writing an intentionally funny story.
i. Giving competent legal advice in a specialized area of law.
j. Translating spoken English into spoken Swedish in real time.
k. Performing a complex surgical operation.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Exercises 33

For the currently infeasible tasks, try to find out what the difficulties are and predict when, if
ever, they will be overcome.
1.15 Various subfields of AI have held contests by defining a standard task and inviting re-
searchers to do their best. Examples include the DARPA Grand Challenge for robotic cars,
The International Planning Competition, the Robocup robotic soccer league, the TREC infor-
mation retrieval event, and contests in machine translation, speech recognition. Investigate
five of these contests, and describe the progress made over the years. To what degree have the
contests advanced toe state of the art in AI? Do what degree do they hurt the field by drawing

m
energy away from new ideas?

co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

2 INTELLIGENT AGENTS

m
co
In which we discuss the nature of agents, perfect or otherwise, the diversity of
environments, and the resulting menagerie of agent types.

a.
Chapter 1 identified the concept of rational agents as central to our approach to artificial

iy
intelligence. In this chapter, we make this notion more concrete. We will see that the concept
of rationality can be applied to a wide variety of agents operating in any imaginable environ-
ment. Our plan in this book is to use this concept to develop a small set of design principles

un for building successful agents—systems that can reasonably be called intelligent.


We begin by examining agents, environments, and the coupling between them. The
observation that some agents behave better than others leads naturally to the idea of a rational
agent—one that behaves as well as possible. How well an agent can behave depends on
sD
the nature of the environment; some environments are more difficult than others. We give a
crude categorization of environments and show how properties of an environment influence
the design of suitable agents for that environment. We describe a number of basic “skeleton”
al

agent designs, which we flesh out in the rest of the book.

2.1 AGENTS AND E NVIRONMENTS


ri
to

ENVIRONMENT An agent is anything that can be viewed as perceiving its environment through sensors and
SENSOR acting upon that environment through actuators. This simple idea is illustrated in Figure 2.1.
Tu

ACTUATOR A human agent has eyes, ears, and other organs for sensors and hands, legs, vocal tract, and so
on for actuators. A robotic agent might have cameras and infrared range finders for sensors
and various motors for actuators. A software agent receives keystrokes, file contents, and
network packets as sensory inputs and acts on the environment by displaying on the screen,
writing files, and sending network packets.
PERCEPT We use the term percept to refer to the agent’s perceptual inputs at any given instant. An
PERCEPT SEQUENCE agent’s percept sequence is the complete history of everything the agent has ever perceived.
In general, an agent’s choice of action at any given instant can depend on the entire percept
sequence observed to date, but not on anything it hasn’t perceived. By specifying the agent’s
choice of action for every possible percept sequence, we have said more or less everything

34

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.1. Agents and Environments 35

Agent Sensors
Percepts

Environment
?

m
co
Actions
Actuators

a.
Figure 2.1 Agents interact with environments through sensors and actuators.

iy
there is to say about the agent. Mathematically speaking, we say that an agent’s behavior is
AGENT FUNCTION described by the agent function that maps any given percept sequence to an action.

un
We can imagine tabulating the agent function that describes any given agent; for most
agents, this would be a very large table—infinite, in fact, unless we place a bound on the
length of percept sequences we want to consider. Given an agent to experiment with, we can,
in principle, construct this table by trying out all possible percept sequences and recording
sD
which actions the agent does in response.1 The table is, of course, an external characterization
of the agent. Internally, the agent function for an artificial agent will be implemented by an
AGENT PROGRAM agent program. It is important to keep these two ideas distinct. The agent function is an
al

abstract mathematical description; the agent program is a concrete implementation, running


within some physical system.
To illustrate these ideas, we use a very simple example—the vacuum-cleaner world
ri

shown in Figure 2.2. This world is so simple that we can describe everything that happens;
it’s also a made-up world, so we can invent many variations. This particular world has just two
to

locations: squares A and B. The vacuum agent perceives which square it is in and whether
there is dirt in the square. It can choose to move left, move right, suck up the dirt, or do
nothing. One very simple agent function is the following: if the current square is dirty, then
Tu

suck; otherwise, move to the other square. A partial tabulation of this agent function is shown
in Figure 2.3 and an agent program that implements it appears in Figure 2.8 on page 48.
Looking at Figure 2.3, we see that various vacuum-world agents can be defined simply
by filling in the right-hand column in various ways. The obvious question, then, is this: What
is the right way to fill out the table? In other words, what makes an agent good or bad,
intelligent or stupid? We answer these questions in the next section.

1 If the agent uses some randomization to choose its actions, then we would have to try each sequence many

times to identify the probability of each action. One might imagine that acting randomly is rather silly, but we
show later in this chapter that it can be very intelligent.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

36 Chapter 2. Intelligent Agents

A B

m
Figure 2.2 A vacuum-cleaner world with just two locations.

co
Percept sequence Action
[A, Clean] Right

a.
[A, Dirty] Suck
[B, Clean] Left

iy
[B, Dirty] Suck
[A, Clean], [A, Clean] Right
[A, Clean], [A, Dirty] Suck

un ..
.
[A, Clean], [A, Clean], [A, Clean]
[A, Clean], [A, Clean], [A, Dirty]
..
.
Right
Suck
sD
.. ..
. .
Figure 2.3 Partial tabulation of a simple agent function for the vacuum-cleaner world
shown in Figure 2.2.
al

Before closing this section, we should emphasize that the notion of an agent is meant to
ri

be a tool for analyzing systems, not an absolute characterization that divides the world into
agents and non-agents. One could view a hand-held calculator as an agent that chooses the
to

action of displaying “4” when given the percept sequence “2 + 2 =,” but such an analysis
would hardly aid our understanding of the calculator. In a sense, all areas of engineering can
be seen as designing artifacts that interact with the world; AI operates at (what the authors
Tu

consider to be) the most interesting end of the spectrum, where the artifacts have significant
computational resources and the task environment requires nontrivial decision making.

2.2 G OOD B EHAVIOR : T HE C ONCEPT OF R ATIONALITY

RATIONAL AGENT A rational agent is one that does the right thing—conceptually speaking, every entry in the
table for the agent function is filled out correctly. Obviously, doing the right thing is better
than doing the wrong thing, but what does it mean to do the right thing?

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.2. Good Behavior: The Concept of Rationality 37

We answer this age-old question in an age-old way: by considering the consequences


of the agent’s behavior. When an agent is plunked down in an environment, it generates a
sequence of actions according to the percepts it receives. This sequence of actions causes the
environment to go through a sequence of states. If the sequence is desirable, then the agent
PERFORMANCE
MEASURE has performed well. This notion of desirability is captured by a performance measure that
evaluates any given sequence of environment states.
Notice that we said environment states, not agent states. If we define success in terms
of agent’s opinion of its own performance, an agent could achieve perfect rationality simply

m
by deluding itself that its performance was perfect. Human agents in particular are notorious
for “sour grapes”—believing they did not really want something (e.g., a Nobel Prize) after
not getting it.

co
Obviously, there is not one fixed performance measure for all tasks and agents; typically,
a designer will devise one appropriate to the circumstances. This is not as easy as it sounds.
Consider, for example, the vacuum-cleaner agent from the preceding section. We might

a.
propose to measure performance by the amount of dirt cleaned up in a single eight-hour shift.
With a rational agent, of course, what you ask for is what you get. A rational agent can

iy
maximize this performance measure by cleaning up the dirt, then dumping it all on the floor,
then cleaning it up again, and so on. A more suitable performance measure would reward the
agent for having a clean floor. For example, one point could be awarded for each clean square

un
at each time step (perhaps with a penalty for electricity consumed and noise generated). As
a general rule, it is better to design performance measures according to what one actually
wants in the environment, rather than according to how one thinks the agent should behave.
sD
Even when the obvious pitfalls are avoided, there remain some knotty issues to untangle.
For example, the notion of “clean floor” in the preceding paragraph is based on average
cleanliness over time. Yet the same average cleanliness can be achieved by two different
agents, one of which does a mediocre job all the time while the other cleans energetically but
al

takes long breaks. Which is preferable might seem to be a fine point of janitorial science, but
in fact it is a deep philosophical question with far-reaching implications. Which is better—
ri

a reckless life of highs and lows, or a safe but humdrum existence? Which is better—an
economy where everyone lives in moderate poverty, or one in which some live in plenty
while others are very poor? We leave these questions as an exercise for the diligent reader.
to

2.2.1 Rationality
Tu

What is rational at any given time depends on four things:


• The performance measure that defines the criterion of success.
• The agent’s prior knowledge of the environment.
• The actions that the agent can perform.
• The agent’s percept sequence to date.
DEFINITION OF A
RATIONAL AGENT This leads to a definition of a rational agent:
For each possible percept sequence, a rational agent should select an action that is ex-
pected to maximize its performance measure, given the evidence provided by the percept
sequence and whatever built-in knowledge the agent has.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

38 Chapter 2. Intelligent Agents

Consider the simple vacuum-cleaner agent that cleans a square if it is dirty and moves to the
other square if not; this is the agent function tabulated in Figure 2.3. Is this a rational agent?
That depends! First, we need to say what the performance measure is, what is known about
the environment, and what sensors and actuators the agent has. Let us assume the following:
• The performance measure awards one point for each clean square at each time step,
over a “lifetime” of 1000 time steps.
• The “geography” of the environment is known a priori (Figure 2.2) but the dirt distri-

m
bution and the initial location of the agent are not. Clean squares stay clean and sucking
cleans the current square. The Left and Right actions move the agent left and right
except when this would take the agent outside the environment, in which case the agent

co
remains where it is.
• The only available actions are Left, Right, and Suck .
• The agent correctly perceives its location and whether that location contains dirt.

a.
We claim that under these circumstances the agent is indeed rational; its expected perfor-

iy
mance is at least as high as any other agent’s. Exercise 2.2 asks you to prove this.
One can see easily that the same agent would be irrational under different circum-
stances. For example, once all the dirt is cleaned up, the agent will oscillate needlessly back

un and forth; if the performance measure includes a penalty of one point for each movement left
or right, the agent will fare poorly. A better agent for this case would do nothing once it is
sure that all the squares are clean. If clean squares can become dirty again, the agent should
sD
occasionally check and re-clean them if needed. If the geography of the environment is un-
known, the agent will need to explore it rather than stick to squares A and B. Exercise 2.2
asks you to design agents for these cases.
al

2.2.2 Omniscience, learning, and autonomy


OMNISCIENCE We need to be careful to distinguish between rationality and omniscience. An omniscient
ri

agent knows the actual outcome of its actions and can act accordingly; but omniscience is
impossible in reality. Consider the following example: I am walking along the Champs
to

Elysées one day and I see an old friend across the street. There is no traffic nearby and I’m
not otherwise engaged, so, being rational, I start to cross the street. Meanwhile, at 33,000
feet, a cargo door falls off a passing airliner, 2 and before I make it to the other side of the
Tu

street I am flattened. Was I irrational to cross the street? It is unlikely that my obituary would
read “Idiot attempts to cross street.”
This example shows that rationality is not the same as perfection. Rationality max-
imizes expected performance, while perfection maximizes actual performance. Retreating
from a requirement of perfection is not just a question of being fair to agents. The point is
that if we expect an agent to do what turns out to be the best action after the fact, it will be
impossible to design an agent to fulfill this specification—unless we improve the performance
of crystal balls or time machines.
2 See N. Henderson, “New door latches urged for Boeing 747 jumbo jets,” Washington Post, August 24, 1989.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.2. Good Behavior: The Concept of Rationality 39

Our definition of rationality does not require omniscience, then, because the rational
choice depends only on the percept sequence to date. We must also ensure that we haven’t
inadvertently allowed the agent to engage in decidedly underintelligent activities. For exam-
ple, if an agent does not look both ways before crossing a busy road, then its percept sequence
will not tell it that there is a large truck approaching at high speed. Does our definition of
rationality say that it’s now OK to cross the road? Far from it! First, it would not be rational
to cross the road given this uninformative percept sequence: the risk of accident from cross-
ing without looking is too great. Second, a rational agent should choose the “looking” action

m
before stepping into the street, because looking helps maximize the expected performance.
Doing actions in order to modify future percepts—sometimes called information gather-
INFORMATION
ing—is an important part of rationality and is covered in depth in Chapter 16. A second

co
GATHERING
EXPLORATION example of information gathering is provided by the exploration that must be undertaken by
a vacuum-cleaning agent in an initially unknown environment.
LEARNING Our definition requires a rational agent not only to gather information but also to learn

a.
as much as possible from what it perceives. The agent’s initial configuration could reflect
some prior knowledge of the environment, but as the agent gains experience this may be

iy
modified and augmented. There are extreme cases in which the environment is completely
known a priori. In such cases, the agent need not perceive or learn; it simply acts correctly.
Of course, such agents are fragile. Consider the lowly dung beetle. After digging its nest and

un
laying its eggs, it fetches a ball of dung from a nearby heap to plug the entrance. If the ball of
dung is removed from its grasp en route, the beetle continues its task and pantomimes plug-
ging the nest with the nonexistent dung ball, never noticing that it is missing. Evolution has
sD
built an assumption into the beetle’s behavior, and when it is violated, unsuccessful behavior
results. Slightly more intelligent is the sphex wasp. The female sphex will dig a burrow, go
out and sting a caterpillar and drag it to the burrow, enter the burrow again to check all is
well, drag the caterpillar inside, and lay its eggs. The caterpillar serves as a food source when
al

the eggs hatch. So far so good, but if an entomologist moves the caterpillar a few inches
away while the sphex is doing the check, it will revert to the “drag” step of its plan and will
ri

continue the plan without modification, even after dozens of caterpillar-moving interventions.
The sphex is unable to learn that its innate plan is failing, and thus will not change it.
To the extent that an agent relies on the prior knowledge of its designer rather than
to

AUTONOMY on its own percepts, we say that the agent lacks autonomy. A rational agent should be
autonomous—it should learn what it can to compensate for partial or incorrect prior knowl-
Tu

edge. For example, a vacuum-cleaning agent that learns to foresee where and when additional
dirt will appear will do better than one that does not. As a practical matter, one seldom re-
quires complete autonomy from the start: when the agent has had little or no experience, it
would have to act randomly unless the designer gave some assistance. So, just as evolution
provides animals with enough built-in reflexes to survive long enough to learn for themselves,
it would be reasonable to provide an artificial intelligent agent with some initial knowledge
as well as an ability to learn. After sufficient experience of its environment, the behavior
of a rational agent can become effectively independent of its prior knowledge. Hence, the
incorporation of learning allows one to design a single rational agent that will succeed in a
vast variety of environments.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

40 Chapter 2. Intelligent Agents

2.3 T HE NATURE OF E NVIRONMENTS

Now that we have a definition of rationality, we are almost ready to think about building
TASK ENVIRONMENT rational agents. First, however, we must think about task environments, which are essen-
tially the “problems” to which rational agents are the “solutions.” We begin by showing how
to specify a task environment, illustrating the process with a number of examples. We then
show that task environments come in a variety of flavors. The flavor of the task environment

m
directly affects the appropriate design for the agent program.

2.3.1 Specifying the task environment

co
In our discussion of the rationality of the simple vacuum-cleaner agent, we had to specify
the performance measure, the environment, and the agent’s actuators and sensors. We group
all these under the heading of the task environment. For the acronymically minded, we call

a.
PEAS this the PEAS (Performance, Environment, Actuators, Sensors) description. In designing an
agent, the first step must always be to specify the task environment as fully as possible.

iy
The vacuum world was a simple example; let us consider a more complex problem: an
automated taxi driver. We should point out, before the reader becomes alarmed, that a fully

un
automated taxi is currently somewhat beyond the capabilities of existing technology. (page 28
describes an existing driving robot.) The full driving task is extremely open-ended. There is
no limit to the novel combinations of circumstances that can arise—another reason we chose
it as a focus for discussion. Figure 2.4 summarizes the PEAS description for the taxi’s task
sD
environment. We discuss each element in more detail in the following paragraphs.

Agent Type Performance Environment Actuators Sensors


Measure
al

Taxi driver Safe, fast, legal, Roads, other Steering, Cameras, sonar,
comfortable trip, traffic, accelerator, speedometer,
ri

maximize profits pedestrians, brake, signal, GPS, odometer,


customers horn, display accelerometer,
engine sensors,
to

keyboard
Tu

Figure 2.4 PEAS description of the task environment for an automated taxi.

First, what is the performance measure to which we would like our automated driver
to aspire? Desirable qualities include getting to the correct destination; minimizing fuel con-
sumption and wear and tear; minimizing the trip time or cost; minimizing violations of traffic
laws and disturbances to other drivers; maximizing safety and passenger comfort; maximiz-
ing profits. Obviously, some of these goals conflict, so tradeoffs will be required.
Next, what is the driving environment that the taxi will face? Any taxi driver must
deal with a variety of roads, ranging from rural lanes and urban alleys to 12-lane freeways.
The roads contain other traffic, pedestrians, stray animals, road works, police cars, puddles,

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.3. The Nature of Environments 41

and potholes. The taxi must also interact with potential and actual passengers. There are also
some optional choices. The taxi might need to operate in Southern California, where snow
is seldom a problem, or in Alaska, where it seldom is not. It could always be driving on the
right, or we might want it to be flexible enough to drive on the left when in Britain or Japan.
Obviously, the more restricted the environment, the easier the design problem.
The actuators for an automated taxi include those available to a human driver: control
over the engine through the accelerator and control over steering and braking. In addition, it
will need output to a display screen or voice synthesizer to talk back to the passengers, and

m
perhaps some way to communicate with other vehicles, politely or otherwise.
The basic sensors for the taxi will include one or more controllable video cameras so
that it can see the road; it might augment these with infrared or sonar sensors to detect dis-

co
tances to other cars and obstacles. To avoid speeding tickets, the taxi should have a speedome-
ter, and to control the vehicle properly, especially on curves, it should have an accelerometer.
To determine the mechanical state of the vehicle, it will need the usual array of engine, fuel,

a.
and electrical system sensors. Like many human drivers, it might want a global positioning
system (GPS) so that it doesn’t get lost. Finally, it will need a keyboard or microphone for

iy
the passenger to request a destination.
In Figure 2.5, we have sketched the basic PEAS elements for a number of additional
agent types. Further examples appear in Exercise 2.4. It may come as a surprise to some read-

un
ers that our list of agent types includes some programs that operate in the entirely artificial
environment defined by keyboard input and character output on a screen. “Surely,” one might
say, “this is not a real environment, is it?” In fact, what matters is not the distinction between
sD
“real” and “artificial” environments, but the complexity of the relationship among the behav-
ior of the agent, the percept sequence generated by the environment, and the performance
measure. Some “real” environments are actually quite simple. For example, a robot designed
to inspect parts as they come by on a conveyor belt can make use of a number of simplifying
al

assumptions: that the lighting is always just so, that the only thing on the conveyor belt will
be parts of a kind that it knows about, and that only two actions (accept or reject) are possible.
ri

SOFTWARE AGENT In contrast, some software agents (or software robots or softbots) exist in rich, unlim-
SOFTBOT ited domains. Imagine a softbot Web site operator designed to scan Internet news sources and
show the interesting items to its users, while selling advertising space to generate revenue.
to

To do well, that operator will need some natural language processing abilities, it will need
to learn what each user and advertiser is interested in, and it will need to change its plans
Tu

dynamically—for example, when the connection for one news source goes down or when a
new one comes online. The Internet is an environment whose complexity rivals that of the
physical world and whose inhabitants include many artificial and human agents.

2.3.2 Properties of task environments


The range of task environments that might arise in AI is obviously vast. We can, however,
identify a fairly small number of dimensions along which task environments can be catego-
rized. These dimensions determine, to a large extent, the appropriate agent design and the
applicability of each of the principal families of techniques for agent implementation. First,

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

42 Chapter 2. Intelligent Agents

Agent Type Performance Environment Actuators Sensors


Measure

Medical Healthy patient, Patient, hospital, Display of Keyboard entry


diagnosis system reduced costs staff questions, tests, of symptoms,
diagnoses, findings, patient’s
treatments, answers
referrals

m
Satellite image Correct image Downlink from Display of scene Color pixel
analysis system categorization orbiting satellite categorization arrays

co
Part-picking Percentage of Conveyor belt Jointed arm and Camera, joint
robot parts in correct with parts; bins hand angle sensors
bins

a.
Refinery Purity, yield, Refinery, Valves, pumps, Temperature,

iy
controller safety operators heaters, displays pressure,
chemical sensors

un Interactive
English tutor
Student’s score
on test
Set of students,
testing agency
Display of
exercises,
suggestions,
Keyboard entry
sD
corrections

Figure 2.5 Examples of agent types and their PEAS descriptions.


al

we list the dimensions, then we analyze several task environments to illustrate the ideas. The
definitions here are informal; later chapters provide more precise statements and examples of
ri

each kind of environment.


Fully observable vs. partially observable: If an agent’s sensors give it access to the
to

FULLY OBSERVABLE
PARTIALLY
OBSERVABLE complete state of the environment at each point in time, then we say that the task environ-
ment is fully observable. A task environment is effectively fully observable if the sensors
Tu

detect all aspects that are relevant to the choice of action; relevance, in turn, depends on the
performance measure. Fully observable environments are convenient because the agent need
not maintain any internal state to keep track of the world. An environment might be partially
observable because of noisy and inaccurate sensors or because parts of the state are simply
missing from the sensor data—for example, a vacuum agent with only a local dirt sensor
cannot tell whether there is dirt in other squares, and an automated taxi cannot see what other
drivers are thinking. If the agent has no sensors at all then the environment is unobserv-
UNOBSERVABLE able. One might think that in such cases the agent’s plight is hopeless, but, as we discuss in
Chapter 4, the agent’s goals may still be achievable, sometimes with certainty.
SINGLE AGENT Single agent vs. multiagent: The distinction between single-agent and multiagent en-
MULTIAGENT

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.3. The Nature of Environments 43

vironments may seem simple enough. For example, an agent solving a crossword puzzle by
itself is clearly in a single-agent environment, whereas an agent playing chess is in a two-
agent environment. There are, however, some subtle issues. First, we have described how an
entity may be viewed as an agent, but we have not explained which entities must be viewed
as agents. Does an agent A (the taxi driver for example) have to treat an object B (another
vehicle) as an agent, or can it be treated merely as an object behaving according to the laws of
physics, analogous to waves at the beach or leaves blowing in the wind? The key distinction
is whether B’s behavior is best described as maximizing a performance measure whose value

m
depends on agent A’s behavior. For example, in chess, the opponent entity B is trying to
maximize its performance measure, which, by the rules of chess, minimizes agent A’s per-
COMPETITIVE formance measure. Thus, chess is a competitive multiagent environment. In the taxi-driving

co
environment, on the other hand, avoiding collisions maximizes the performance measure of
COOPERATIVE all agents, so it is a partially cooperative multiagent environment. It is also partially com-
petitive because, for example, only one car can occupy a parking space. The agent-design

a.
problems in multiagent environments are often quite different from those in single-agent en-
vironments; for example, communication often emerges as a rational behavior in multiagent

iy
environments; in some competitive environments, randomized behavior is rational because
it avoids the pitfalls of predictability.
DETERMINISTIC Deterministic vs. stochastic. If the next state of the environment is completely deter-

un
STOCHASTIC mined by the current state and the action executed by the agent, then we say the environment
is deterministic; otherwise, it is stochastic. In principle, an agent need not worry about uncer-
tainty in a fully observable, deterministic environment. (In our definition, we ignore uncer-
sD
tainty that arises purely from the actions of other agents in a multiagent environment; thus,
a game can be deterministic even though each agent may be unable to predict the actions of
the others.) If the environment is partially observable, however, then it could appear to be
stochastic. Most real situations are so complex that it is impossible to keep track of all the
al

unobserved aspects; for practical purposes, they must be treated as stochastic. Taxi driving is
clearly stochastic in this sense, because one can never predict the behavior of traffic exactly;
ri

moreover, one’s tires blow out and one’s engine seizes up without warning. The vacuum
world as we described it is deterministic, but variations can include stochastic elements such
as randomly appearing dirt and an unreliable suction mechanism (Exercise 2.13). We say an
to

UNCERTAIN environment is uncertain if it is not fully observable or not deterministic. One final note:
our use of the word “stochastic” generally implies that uncertainty about outcomes is quan-
Tu

NONDETERMINISTIC tified in terms of probabilities; a nondeterministic environment is one in which actions are
characterized by their possible outcomes, but no probabilities are attached to them. Nonde-
terministic environment descriptions are usually associated with performance measures that
require the agent to succeed for all possible outcomes of its actions.
EPISODIC Episodic vs. sequential: In an episodic task environment, the agent’s experience is
SEQUENTIAL divided into atomic episodes. In each episode the agent receives a percept and then performs
a single action. Crucially, the next episode does not depend on the actions taken in previous
episodes. Many classification tasks are episodic. For example, an agent that has to spot
defective parts on an assembly line bases each decision on the current part, regardless of
previous decisions; moreover, the current decision doesn’t affect whether the next part is

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

44 Chapter 2. Intelligent Agents

defective. In sequential environments, on the other hand, the current decision could affect
all future decisions.3 Chess and taxi driving are sequential: in both cases, short-term actions
can have long-term consequences. Episodic environments are much simpler than sequential
environments because the agent does not need to think ahead.
STATIC Static vs. dynamic: If the environment can change while an agent is deliberating, then
DYNAMIC we say the environment is dynamic for that agent; otherwise, it is static. Static environments
are easy to deal with because the agent need not keep looking at the world while it is deciding
on an action, nor need it worry about the passage of time. Dynamic environments, on the

m
other hand, are continuously asking the agent what it wants to do; if it hasn’t decided yet,
that counts as deciding to do nothing. If the environment itself does not change with the
passage of time but the agent’s performance score does, then we say the environment is

co
SEMIDYNAMIC semidynamic. Taxi driving is clearly dynamic: the other cars and the taxi itself keep moving
while the driving algorithm dithers about what to do next. Chess, when played with a clock,
is semidynamic. Crossword puzzles are static.

a.
DISCRETE Discrete vs. continuous: The discrete/continuous distinction applies to the state of the
CONTINUOUS environment, to the way time is handled, and to the percepts and actions of the agent. For

iy
example, the chess environment has a finite number of distinct states (excluding the clock).
Chess also has a discrete set of percepts and actions. Taxi driving is a continuous-state and
continuous-time problem: the speed and location of the taxi and of the other vehicles sweep

un through a range of continuous values and do so smoothly over time. Taxi-driving actions are
also continuous (steering angles, etc.). Input from digital cameras is discrete, strictly speak-
ing, but is typically treated as representing continuously varying intensities and locations.
sD
KNOWN Known vs. unknown: Strictly speaking, this distinction refers not to the environment
UNKNOWN itself but to the agent’s (or designer’s) state of knowledge about the “laws of physics” of
the environment. In a known environment, the outcomes (or outcome probabilities if the
environment is stochastic) for all actions are given. Obviously, if the environment is unknown,
al

the agent will have to learn how it works in order to make good decisions. Note that the
distinction between known and unknown environments is not the same as the one between
ri

fully and partially observable environments. It is quite possible for a known environment
to be partially observable—for example, in solitaire card games, I know the rules but am
still unable to see the cards that have not yet been turned over. Conversely, an unknown
to

environment can be fully observable—in a new video game, the screen may show the entire
game state but I still don’t know what the buttons do until I try them.
Tu

As one might expect, the hardest case is partially observable, multiagent, stochastic,
sequential, dynamic, continuous, and unknown. Taxi driving is hard in all these senses, except
that for the most part the driver’s environment is known. Driving a rented car in a new country
with unfamiliar geography and traffic laws is a lot more exciting.
Figure 2.6 lists the properties of a number of familiar environments. Note that the
answers are not always cut and dried. For example, we describe the part-picking robot as
episodic, because it normally considers each part in isolation. But if one day there is a large
3 The word “sequential” is also used in computer science as the antonym of “parallel.” The two meanings are
largely unrelated.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.3. The Nature of Environments 45

Task Environment Observable Agents Deterministic Episodic Static Discrete

Crossword puzzle Fully Single Deterministic Sequential Static Discrete


Chess with a clock Fully Multi Deterministic Sequential Semi Discrete
Poker Partially Multi Stochastic Sequential Static Discrete
Backgammon Fully Multi Stochastic Sequential Static Discrete
Taxi driving Partially Multi Stochastic Sequential Dynamic Continuous
Medical diagnosis Partially Single Stochastic Sequential Dynamic Continuous

m
Image analysis Fully Single Deterministic Episodic Semi Continuous
Part-picking robot Partially Single Stochastic Episodic Dynamic Continuous

co
Refinery controller Partially Single Stochastic Sequential Dynamic Continuous
Interactive English tutor Partially Multi Stochastic Sequential Dynamic Discrete

Figure 2.6 Examples of task environments and their characteristics.

a.
iy
batch of defective parts, the robot should learn from several observations that the distribution
of defects has changed, and should modify its behavior for subsequent parts. We have not
included a “known/unknown” column because, as explained earlier, this is not strictly a prop-

un
erty of the environment. For some environments, such as chess and poker, it is quite easy to
supply the agent with full knowledge of the rules, but it is nonetheless interesting to consider
how an agent might learn to play these games without such knowledge.
sD
Several of the answers in the table depend on how the task environment is defined. We
have listed the medical-diagnosis task as single-agent because the disease process in a patient
is not profitably modeled as an agent; but a medical-diagnosis system might also have to
deal with recalcitrant patients and skeptical staff, so the environment could have a multiagent
al

aspect. Furthermore, medical diagnosis is episodic if one conceives of the task as selecting a
diagnosis given a list of symptoms; the problem is sequential if the task can include proposing
ri

a series of tests, evaluating progress over the course of treatment, and so on. Also, many
environments are episodic at higher levels than the agent’s individual actions. For example,
a chess tournament consists of a sequence of games; each game is an episode because (by
to

and large) the contribution of the moves in one game to the agent’s overall performance is
not affected by the moves in its previous game. On the other hand, decision making within a
Tu

single game is certainly sequential.


The code repository associated with this book (aima.cs.berkeley.edu) includes imple-
mentations of a number of environments, together with a general-purpose environment simu-
lator that places one or more agents in a simulated environment, observes their behavior over
time, and evaluates them according to a given performance measure. Such experiments are
often carried out not for a single environment but for many environments drawn from an en-
ENVIRONMENT
CLASS vironment class. For example, to evaluate a taxi driver in simulated traffic, we would want to
run many simulations with different traffic, lighting, and weather conditions. If we designed
the agent for a single scenario, we might be able to take advantage of specific properties
of the particular case but might not identify a good design for driving in general. For this

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

46 Chapter 2. Intelligent Agents

ENVIRONMENT
GENERATOR reason, the code repository also includes an environment generator for each environment
class that selects particular environments (with certain likelihoods) in which to run the agent.
For example, the vacuum environment generator initializes the dirt pattern and agent location
randomly. We are then interested in the agent’s average performance over the environment
class. A rational agent for a given environment class maximizes this average performance.
Exercises 2.8 to 2.13 take you through the process of developing an environment class and
evaluating various agents therein.

m
2.4 T HE S TRUCTURE OF AGENTS

co
So far we have talked about agents by describing behavior—the action that is performed after
any given sequence of percepts. Now we must bite the bullet and talk about how the insides

a.
AGENT PROGRAM work. The job of AI is to design an agent program that implements the agent function—
the mapping from percepts to actions. We assume this program will run on some sort of
ARCHITECTURE computing device with physical sensors and actuators—we call this the architecture:

iy
agent = architecture + program .

un
Obviously, the program we choose has to be one that is appropriate for the architecture. If the
program is going to recommend actions like Walk, the architecture had better have legs. The
architecture might be just an ordinary PC, or it might be a robotic car with several onboard
computers, cameras, and other sensors. In general, the architecture makes the percepts from
sD
the sensors available to the program, runs the program, and feeds the program’s action choices
to the actuators as they are generated. Most of this book is about designing agent programs,
although Chapters 24 and 25 deal directly with the sensors and actuators.
al

2.4.1 Agent programs


ri

The agent programs that we design in this book all have the same skeleton: they take the
current percept as input from the sensors and return an action to the actuators.4 Notice the
difference between the agent program, which takes the current percept as input, and the agent
to

function, which takes the entire percept history. The agent program takes just the current
percept as input because nothing more is available from the environment; if the agent’s actions
Tu

need to depend on the entire percept sequence, the agent will have to remember the percepts.
We describe the agent programs in the simple pseudocode language that is defined in
Appendix B. (The online code repository contains implementations in real programming
languages.) For example, Figure 2.7 shows a rather trivial agent program that keeps track of
the percept sequence and then uses it to index into a table of actions to decide what to do.
The table—an example of which is given for the vacuum world in Figure 2.3—represents
explicitly the agent function that the agent program embodies. To build a rational agent in
4 There are other choices for the agent program skeleton; for example, we could have the agent programs be
coroutines that run asynchronously with the environment. Each such coroutine has an input and output port and
consists of a loop that reads the input port for percepts and writes actions to the output port.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.4. The Structure of Agents 47

function TABLE -D RIVEN -AGENT( percept ) returns an action


persistent: percepts, a sequence, initially empty
table, a table of actions, indexed by percept sequences, initially fully specified
append percept to the end of percepts
action ← L OOKUP( percepts, table)
return action

m
Figure 2.7 The TABLE -D RIVEN -AGENT program is invoked for each new percept and
returns an action each time. It retains the complete percept sequence in memory.

co
this way, we as designers must construct a table that contains the appropriate action for every
possible percept sequence.
It is instructive to consider why the table-driven approach to agent construction is

a.
doomed to failure. Let P be the set of possible percepts and let T be the lifetime  of the
agent (the total number of percepts it will receive). The lookup table will contain Tt= 1 |P|t

iy
entries. Consider the automated taxi: the visual input from a single camera comes in at the
rate of roughly 27 megabytes per second (30 frames per second, 640 × 480 pixels with 24
bits of color information). This gives a lookup table with over 10250,000,000,000 entries for an

un
hour’s driving. Even the lookup table for chess—a tiny, well-behaved fragment of the real
world—would have at least 10150 entries. The daunting size of these tables (the number of
atoms in the observable universe is less than 1080 ) means that (a) no physical agent in this
sD
universe will have the space to store the table, (b) the designer would not have time to create
the table, (c) no agent could ever learn all the right table entries from its experience, and (d)
even if the environment is simple enough to yield a feasible table size, the designer still has
no guidance about how to fill in the table entries.
al

Despite all this, TABLE -D RIVEN -AGENT does do what we want: it implements the
desired agent function. The key challenge for AI is to find out how to write programs that,
ri

to the extent possible, produce rational behavior from a smallish program rather than from
a vast table. We have many examples showing that this can be done successfully in other
to

areas: for example, the huge tables of square roots used by engineers and schoolchildren prior
to the 1970s have now been replaced by a five-line program for Newton’s method running
on electronic calculators. The question is, can AI do for general intelligent behavior what
Tu

Newton did for square roots? We believe the answer is yes.


In the remainder of this section, we outline four basic kinds of agent programs that
embody the principles underlying almost all intelligent systems:
• Simple reflex agents;
• Model-based reflex agents;
• Goal-based agents; and
• Utility-based agents.
Each kind of agent program combines particular components in particular ways to generate
actions. Section 2.4.6 explains in general terms how to convert all these agents into learning

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

48 Chapter 2. Intelligent Agents

function R EFLEX -VACUUM -AGENT( [location,status]) returns an action


if status = Dirty then return Suck
else if location = A then return Right
else if location = B then return Left

Figure 2.8 The agent program for a simple reflex agent in the two-state vacuum environ-
ment. This program implements the agent function tabulated in Figure 2.3.

m
agents that can improve the performance of their components so as to generate better actions.

co
Finally, Section 2.4.7 describes the variety of ways in which the components themselves can
be represented within the agent. This variety provides a major organizing principle for the
field and for the book itself.

.
ya
2.4.2 Simple reflex agents
SIMPLE REFLEX
AGENT The simplest kind of agent is the simple reflex agent. These agents select actions on the basis
of the current percept, ignoring the rest of the percept history. For example, the vacuum agent

i
whose agent function is tabulated in Figure 2.3 is a simple reflex agent, because its decision

un
is based only on the current location and on whether that location contains dirt. An agent
program for this agent is shown in Figure 2.8.
Notice that the vacuum agent program is very small indeed compared to the correspond-
lsD
ing table. The most obvious reduction comes from ignoring the percept history, which cuts
down the number of possibilities from 4T to just 4. A further, small reduction comes from
the fact that when the current square is dirty, the action does not depend on the location.
Simple reflex behaviors occur even in more complex environments. Imagine yourself
as the driver of the automated taxi. If the car in front brakes and its brake lights come on, then
ia

you should notice this and initiate braking. In other words, some processing is done on the
visual input to establish the condition we call “The car in front is braking.” Then, this triggers
or

some established connection in the agent program to the action “initiate braking.” We call
CONDITION–ACTION
RULE such a connection a condition–action rule,5 written as
if car-in-front-is-braking then initiate-braking.
t

Humans also have many such connections, some of which are learned responses (as for driv-
Tu

ing) and some of which are innate reflexes (such as blinking when something approaches the
eye). In the course of the book, we show several different ways in which such connections
can be learned and implemented.
The program in Figure 2.8 is specific to one particular vacuum environment. A more
general and flexible approach is first to build a general-purpose interpreter for condition–
action rules and then to create rule sets for specific task environments. Figure 2.9 gives the
structure of this general program in schematic form, showing how the condition–action rules
allow the agent to make the connection from percept to action. (Do not worry if this seems
5 Also called situation–action rules, productions, or if–then rules.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.4. The Structure of Agents 49

Agent Sensors

What the world


is like now

Environment

m
Condition-action rules What action I
should do now

co
Actuators

a.
Figure 2.9 Schematic diagram of a simple reflex agent.

iy
function S IMPLE -R EFLEX -AGENT( percept ) returns an action
persistent: rules, a set of condition–action rules

un
state ← I NTERPRET-I NPUT( percept )
rule ← RULE -M ATCH(state, rules)
action ← rule.ACTION
sD
return action

Figure 2.10 A simple reflex agent. It acts according to a rule whose condition matches
the current state, as defined by the percept.
al
ri

trivial; it gets more interesting shortly.) We use rectangles to denote the current internal state
of the agent’s decision process, and ovals to represent the background information used in
the process. The agent program, which is also very simple, is shown in Figure 2.10. The
to

I NTERPRET-I NPUT function generates an abstracted description of the current state from the
percept, and the RULE -M ATCH function returns the first rule in the set of rules that matches
Tu

the given state description. Note that the description in terms of “rules” and “matching” is
purely conceptual; actual implementations can be as simple as a collection of logic gates
implementing a Boolean circuit.
Simple reflex agents have the admirable property of being simple, but they turn out to be
of limited intelligence. The agent in Figure 2.10 will work only if the correct decision can be
made on the basis of only the current percept—that is, only if the environment is fully observ-
able. Even a little bit of unobservability can cause serious trouble. For example, the braking
rule given earlier assumes that the condition car-in-front-is-braking can be determined from
the current percept—a single frame of video. This works if the car in front has a centrally
mounted brake light. Unfortunately, older models have different configurations of taillights,

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com
for Notes, books,programs, question paperswith solutions etc.

50 Chapter 2. Intelligent Agents

brake lights, and turn-signal lights, and it is not always possible to tell from a single image
whether the car is braking. A simple reflex agent driving behind such a car would either brake
continuously and unnecessarily, or, worse, never brake at all.
We can see a similar problem arising in the vacuum world. Suppose that a simple reflex
vacuum agent is deprived of its location sensor and has only a dirt sensor. Such an agent
has just two possible percepts: [Dirty] and [Clean]. It can Suck in response to [Dirty]; what
should it do in response to [Clean]? Moving Left fails (forever) if it happens to start in square
A, and moving Right fails (forever) if it happens to start in square B. Infinite loops are often

m
unavoidable for simple reflex agents operating in partially observable environments.
RANDOMIZATION Escape from infinite loops is possible if the agent can randomize its actions. For ex-
ample, if the vacuum agent perceives [Clean], it might flip a coin to choose between Left and

co
Right. It is easy to show that the agent will reach the other square in an average of two steps.
Then, if that square is dirty, the agent will clean it and the task will be complete. Hence, a
randomized simple reflex agent might outperform a deterministic simple reflex agent.

a.
We mentioned in Section 2.3 that randomized behavior of the right kind can be rational
in some multiagent environments. In single-agent environments, randomization is usually not

iy
rational. It is a useful trick that helps a simple reflex agent in some situations, but in most
cases we can do much better with more sophisticated deterministic agents.

2.4.3 Model-based reflex agents


un
The most effective way to handle partial observability is for the agent to keep track of the
sD
part of the world it can’t see now. That is, the agent should maintain some sort of internal
INTERNAL STATE state that depends on the percept history and thereby reflects at least some of the unobserved
aspects of the current state. For the braking problem, the internal state is not too extensive—
just the previous frame from the camera, allowing the agent to detect when two red lights at
al

the edge of the vehicle go on or off simultaneously. For other driving tasks such as changing
lanes, the agent needs to keep track of where the other cars are if it can’t see them all at once.
ri

And for any driving to be possible at all, the agent needs to keep track of where its keys are.
Updating this internal state information as time goes by requires two kinds of knowl-
edge to be encoded in the agent program. First, we need some information about how the
to

world evolves independently of the agent—for example, that an overtaking car generally will
be closer behind than it was a moment ago. Second, we need some information about how
Tu

the agent’s own actions affect the world—for example, that when the agent turns the steering
wheel clockwise, the car turns to the right, or that after driving for five minutes northbound
on the freeway, one is usually about five miles north of where one was five minutes ago. This
knowledge about “how the world works”—whether implemented in simple Boolean circuits
or in complete scientific theories—is called a model of the world. An agent that uses such a
MODEL-BASED
AGENT model is called a model-based agent.
Figure 2.11 gives the structure of the model-based reflex agent with internal state, show-
ing how the current percept is combined with the old internal state to generate the updated
description of the current state, based on the agent’s model of how the world works. The agent
program is shown in Figure 2.12. The interesting part is the function U PDATE -S TATE , which

Visit https://www.tutorialsduniya.com
for Notes, books,programs, question paperswith solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.4. The Structure of Agents 51

Sensors
State
How the world evolves What the world
is like now

Environment
What my actions do

m
Condition-action rules What action I
should do now

co
Agent Actuators

a.
Figure 2.11 A model-based reflex agent.

iy
function M ODEL -BASED -R EFLEX -AGENT( percept ) returns an action
persistent: state, the agent’s current conception of the world state

un
model , a description of how the next state depends on current state and action
rules, a set of condition–action rules
action, the most recent action, initially none
state ← U PDATE -S TATE(state, action , percept , model )
sD
rule ← RULE -M ATCH(state, rules)
action ← rule.ACTION
return action
al

Figure 2.12 A model-based reflex agent. It keeps track of the current state of the world,
using an internal model. It then chooses an action in the same way as the reflex agent.
ri

is responsible for creating the new internal state description. The details of how models and
to

states are represented vary widely depending on the type of environment and the particular
technology used in the agent design. Detailed examples of models and updating algorithms
Tu

appear in Chapters 4, 12, 11, 15, 17, and 25.


Regardless of the kind of representation used, it is seldom possible for the agent to
determine the current state of a partially observable environment exactly. Instead, the box
labeled “what the world is like now” (Figure 2.11) represents the agent’s “best guess” (or
sometimes best guesses). For example, an automated taxi may not be able to see around the
large truck that has stopped in front of it and can only guess about what may be causing the
hold-up. Thus, uncertainty about the current state may be unavoidable, but the agent still has
to make a decision.
A perhaps less obvious point about the internal “state” maintained by a model-based
agent is that it does not have to describe “what the world is like now” in a literal sense. For

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

52 Chapter 2. Intelligent Agents

Sensors

State
What the world
How the world evolves is like now

Environment
What it will be like
What my actions do if I do action A

m
What action I
Goals should do now

co
Agent Actuators

a.
Figure 2.13 A model-based, goal-based agent. It keeps track of the world state as well as
a set of goals it is trying to achieve, and chooses an action that will (eventually) lead to the
achievement of its goals.

iy
un
example, the taxi may be driving back home, and it may have a rule telling it to fill up with
gas on the way home unless it has at least half a tank. Although “driving back home” may
seem to an aspect of the world state, the fact of the taxi’s destination is actually an aspect of
the agent’s internal state. If you find this puzzling, consider that the taxi could be in exactly
sD
the same place at the same time, but intending to reach a different destination.

2.4.4 Goal-based agents


al

Knowing something about the current state of the environment is not always enough to decide
what to do. For example, at a road junction, the taxi can turn left, turn right, or go straight
ri

on. The correct decision depends on where the taxi is trying to get to. In other words, as well
GOAL as a current state description, the agent needs some sort of goal information that describes
situations that are desirable—for example, being at the passenger’s destination. The agent
to

program can combine this with the model (the same information as was used in the model-
based reflex agent) to choose actions that achieve the goal. Figure 2.13 shows the goal-based
Tu

agent’s structure.
Sometimes goal-based action selection is straightforward—for example, when goal sat-
isfaction results immediately from a single action. Sometimes it will be more tricky—for
example, when the agent has to consider long sequences of twists and turns in order to find a
way to achieve the goal. Search (Chapters 3 to 5) and planning (Chapters 10 and 11) are the
subfields of AI devoted to finding action sequences that achieve the agent’s goals.
Notice that decision making of this kind is fundamentally different from the condition–
action rules described earlier, in that it involves consideration of the future—both “What will
happen if I do such-and-such?” and “Will that make me happy?” In the reflex agent designs,
this information is not explicitly represented, because the built-in rules map directly from

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.4. The Structure of Agents 53

percepts to actions. The reflex agent brakes when it sees brake lights. A goal-based agent, in
principle, could reason that if the car in front has its brake lights on, it will slow down. Given
the way the world usually evolves, the only action that will achieve the goal of not hitting
other cars is to brake.
Although the goal-based agent appears less efficient, it is more flexible because the
knowledge that supports its decisions is represented explicitly and can be modified. If it starts
to rain, the agent can update its knowledge of how effectively its brakes will operate; this will
automatically cause all of the relevant behaviors to be altered to suit the new conditions.

m
For the reflex agent, on the other hand, we would have to rewrite many condition–action
rules. The goal-based agent’s behavior can easily be changed to go to a different destination,
simply by specifying that destination as the goal. The reflex agent’s rules for when to turn

co
and when to go straight will work only for a single destination; they must all be replaced to
go somewhere new.

a.
2.4.5 Utility-based agents
Goals alone are not enough to generate high-quality behavior in most environments. For

iy
example, many action sequences will get the taxi to its destination (thereby achieving the
goal) but some are quicker, safer, more reliable, or cheaper than others. Goals just provide a

un
crude binary distinction between “happy” and “unhappy” states. A more general performance
measure should allow a comparison of different world states according to exactly how happy
they would make the agent. Because “happy” does not sound very scientific, economists and
UTILITY computer scientists use the term utility instead.6
sD
We have already seen that a performance measure assigns a score to any given sequence
of environment states, so it can easily distinguish between more and less desirable ways of
UTILITY FUNCTION getting to the taxi’s destination. An agent’s utility function is essentially an internalization
al

of the performance measure. If the internal utility function and the external performance
measure are in agreement, then an agent that chooses actions to maximize its utility will be
rational according to the external performance measure.
ri

Let us emphasize again that this is not the only way to be rational—we have already
seen a rational agent program for the vacuum world (Figure 2.8) that has no idea what its
to

utility function is—but, like goal-based agents, a utility-based agent has many advantages in
terms of flexibility and learning. Furthermore, in two kinds of cases, goals are inadequate but
a utility-based agent can still make rational decisions. First, when there are conflicting goals,
Tu

only some of which can be achieved (for example, speed and safety), the utility function
specifies the appropriate tradeoff. Second, when there are several goals that the agent can
aim for, none of which can be achieved with certainty, utility provides a way in which the
likelihood of success can be weighed against the importance of the goals.
Partial observability and stochasticity are ubiquitous in the real world, and so, therefore,
is decision making under uncertainty. Technically speaking, a rational utility-based agent
EXPECTED UTILITY chooses the action that maximizes the expected utility of the action outcomes—that is, the
utility the agent expects to derive, on average, given the probabilities and utilities of each
6 The word “utility” here refers to “the quality of being useful,” not to the electric company or waterworks.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

54 Chapter 2. Intelligent Agents

Sensors
State
What the world
How the world evolves is like now

Environment
What it will be like
What my actions do if I do action A

Utility How happy I will be

m
in such a state

What action I
should do now

co
Agent Actuators

a.
Figure 2.14 A model-based, utility-based agent. It uses a model of the world, along with
a utility function that measures its preferences among states of the world. Then it chooses the
action that leads to the best expected utility, where expected utility is computed by averaging

iy
over all possible outcome states, weighted by the probability of the outcome.

un outcome. (Appendix A defines expectation more precisely.) In Chapter 16, we show that any
rational agent must behave as if it possesses a utility function whose expected value it tries
to maximize. An agent that possesses an explicit utility function can make rational decisions
sD
with a general-purpose algorithm that does not depend on the specific utility function being
maximized. In this way, the “global” definition of rationality—designating as rational those
agent functions that have the highest performance—is turned into a “local” constraint on
rational-agent designs that can be expressed in a simple program.
al

The utility-based agent structure appears in Figure 2.14. Utility-based agent programs
appear in Part IV, where we design decision-making agents that must handle the uncertainty
ri

inherent in stochastic or partially observable environments.


At this point, the reader may be wondering, “Is it that simple? We just build agents that
to

maximize expected utility, and we’re done?” It’s true that such agents would be intelligent,
but it’s not simple. A utility-based agent has to model and keep track of its environment,
tasks that have involved a great deal of research on perception, representation, reasoning,
Tu

and learning. The results of this research fill many of the chapters of this book. Choosing
the utility-maximizing course of action is also a difficult task, requiring ingenious algorithms
that fill several more chapters. Even with these algorithms, perfect rationality is usually
unachievable in practice because of computational complexity, as we noted in Chapter 1.

2.4.6 Learning agents


We have described agent programs with various methods for selecting actions. We have
not, so far, explained how the agent programs come into being. In his famous early paper,
Turing (1950) considers the idea of actually programming his intelligent machines by hand.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.4. The Structure of Agents 55

Performance standard

Critic Sensors

feedback

Environment
changes
Learning Performance

m
element element
knowledge
learning
goals

co
Problem
generator

Actuators
Agent

a.
Figure 2.15 A general learning agent.

iy
He estimates how much work this might take and concludes “Some more expeditious method

un
seems desirable.” The method he proposes is to build learning machines and then to teach
them. In many areas of AI, this is now the preferred method for creating state-of-the-art
systems. Learning has another advantage, as we noted earlier: it allows the agent to operate
sD
in initially unknown environments and to become more competent than its initial knowledge
alone might allow. In this section, we briefly introduce the main ideas of learning agents.
Throughout the book, we comment on opportunities and methods for learning in particular
kinds of agents. Part V goes into much more depth on the learning algorithms themselves.
al

A learning agent can be divided into four conceptual components, as shown in Fig-
LEARNING ELEMENT ure 2.15. The most important distinction is between the learning element, which is re-
ri

PERFORMANCE
ELEMENT sponsible for making improvements, and the performance element, which is responsible for
selecting external actions. The performance element is what we have previously considered
to be the entire agent: it takes in percepts and decides on actions. The learning element uses
to

CRITIC feedback from the critic on how the agent is doing and determines how the performance
element should be modified to do better in the future.
Tu

The design of the learning element depends very much on the design of the performance
element. When trying to design an agent that learns a certain capability, the first question is
not “How am I going to get it to learn this?” but “What kind of performance element will my
agent need to do this once it has learned how?” Given an agent design, learning mechanisms
can be constructed to improve every part of the agent.
The critic tells the learning element how well the agent is doing with respect to a fixed
performance standard. The critic is necessary because the percepts themselves provide no
indication of the agent’s success. For example, a chess program could receive a percept
indicating that it has checkmated its opponent, but it needs a performance standard to know
that this is a good thing; the percept itself does not say so. It is important that the performance

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

56 Chapter 2. Intelligent Agents

standard be fixed. Conceptually, one should think of it as being outside the agent altogether
because the agent must not modify it to fit its own behavior.
PROBLEM
GENERATOR The last component of the learning agent is the problem generator. It is responsible
for suggesting actions that will lead to new and informative experiences. The point is that
if the performance element had its way, it would keep doing the actions that are best, given
what it knows. But if the agent is willing to explore a little and do some perhaps suboptimal
actions in the short run, it might discover much better actions for the long run. The problem
generator’s job is to suggest these exploratory actions. This is what scientists do when they

m
carry out experiments. Galileo did not think that dropping rocks from the top of a tower in
Pisa was valuable in itself. He was not trying to break the rocks or to modify the brains of
unfortunate passers-by. His aim was to modify his own brain by identifying a better theory

co
of the motion of objects.
To make the overall design more concrete, let us return to the automated taxi example.
The performance element consists of whatever collection of knowledge and procedures the

a.
taxi has for selecting its driving actions. The taxi goes out on the road and drives, using
this performance element. The critic observes the world and passes information along to the

iy
learning element. For example, after the taxi makes a quick left turn across three lanes of traf-
fic, the critic observes the shocking language used by other drivers. From this experience, the
learning element is able to formulate a rule saying this was a bad action, and the performance

un element is modified by installation of the new rule. The problem generator might identify
certain areas of behavior in need of improvement and suggest experiments, such as trying out
the brakes on different road surfaces under different conditions.
sD
The learning element can make changes to any of the “knowledge” components shown
in the agent diagrams (Figures 2.9, 2.11, 2.13, and 2.14). The simplest cases involve learning
directly from the percept sequence. Observation of pairs of successive states of the environ-
ment can allow the agent to learn “How the world evolves,” and observation of the results of
al

its actions can allow the agent to learn “What my actions do.” For example, if the taxi exerts
a certain braking pressure when driving on a wet road, then it will soon find out how much
ri

deceleration is actually achieved. Clearly, these two learning tasks are more difficult if the
environment is only partially observable.
The forms of learning in the preceding paragraph do not need to access the external
to

performance standard—in a sense, the standard is the universal one of making predictions
that agree with experiment. The situation is slightly more complex for a utility-based agent
Tu

that wishes to learn utility information. For example, suppose the taxi-driving agent receives
no tips from passengers who have been thoroughly shaken up during the trip. The external
performance standard must inform the agent that the loss of tips is a negative contribution to
its overall performance; then the agent might be able to learn that violent maneuvers do not
contribute to its own utility. In a sense, the performance standard distinguishes part of the
incoming percept as a reward (or penalty) that provides direct feedback on the quality of the
agent’s behavior. Hard-wired performance standards such as pain and hunger in animals can
be understood in this way. This issue is discussed further in Chapter 21.
In summary, agents have a variety of components, and those components can be repre-
sented in many ways within the agent program, so there appears to be great variety among

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.4. The Structure of Agents 57

learning methods. There is, however, a single unifying theme. Learning in intelligent agents
can be summarized as a process of modification of each component of the agent to bring the
components into closer agreement with the available feedback information, thereby improv-
ing the overall performance of the agent.

2.4.7 How the components of agent programs work


We have described agent programs (in very high-level terms) as consisting of various compo-
nents, whose function it is to answer questions such as: “What is the world like now?” “What

m
action should I do now?” “What do my actions do?” The next question for a student of AI
is, “How on earth do these components work?” It takes about a thousand pages to begin to
answer that question properly, but here we want to draw the reader’s attention to some basic

co
distinctions among the various ways that the components can represent the environment that
the agent inhabits.
Roughly speaking, we can place the representations along an axis of increasing com-

a.
plexity and expressive power—atomic, factored, and structured. To illustrate these ideas,
it helps to consider a particular agent component, such as the one that deals with “What my

iy
actions do.” This component describes the changes that might occur in the environment as
the result of taking an action, and Figure 2.16 provides schematic depictions of how those

un
transitions might be represented.
sD
B C
al

B C
ri

(a) Atomic (b) Factored (b) Structured


to

Figure 2.16 Three ways to represent states and the transitions between them. (a) Atomic
representation: a state (such as B or C) is a black box with no internal structure; (b) Factored
Tu

representation: a state consists of a vector of attribute values; values can be Boolean, real-
valued, or one of a fixed set of symbols. (c) Structured representation: a state includes
objects, each of which may have attributes of its own as well as relationships to other objects.

ATOMIC
REPRESENTATION In an atomic representation each state of the world is indivisible—it has no internal
structure. Consider the problem of finding a driving route from one end of a country to the
other via some sequence of cities (we address this problem in Figure 3.2 on page 68). For the
purposes of solving this problem, it may suffice to reduce the state of world to just the name
of the city we are in—a single atom of knowledge; a “black box” whose only discernible
property is that of being identical to or different from another black box. The algorithms

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

58 Chapter 2. Intelligent Agents

underlying search and game-playing (Chapters 3–5), Hidden Markov models (Chapter 15),
and Markov decision processes (Chapter 17) all work with atomic representations—or, at
least, they treat representations as if they were atomic.
Now consider a higher-fidelity description for the same problem, where we need to be
concerned with more than just atomic location in one city or another; we might need to pay
attention to how much gas is in the tank, our current GPS coordinates, whether or not the oil
warning light is working, how much spare change we have for toll crossings, what station is
FACTORED
REPRESENTATION on the radio, and so on. A factored representation splits up each state into a fixed set of

m
VARIABLE variables or attributes, each of which can have a value. While two different atomic states
ATTRIBUTE have nothing in common—they are just different black boxes—two different factored states
VALUE can share some attributes (such as being at some particular GPS location) and not others (such

co
as having lots of gas or having no gas); this makes it much easier to work out how to turn
one state into another. With factored representations, we can also represent uncertainty—for
example, ignorance about the amount of gas in the tank can be represented by leaving that

a.
attribute blank. Many important areas of AI are based on factored representations, including
constraint satisfaction algorithms (Chapter 6), propositional logic (Chapter 7), planning

iy
(Chapters 10 and 11), Bayesian networks (Chapters 13–16), and the machine learning al-
gorithms in Chapters 18, 20, and 21.
For many purposes, we need to understand the world as having things in it that are

un related to each other, not just variables with values. For example, we might notice that a
large truck ahead of us is reversing into the driveway of a dairy farm but a cow has got loose
and is blocking the truck’s path. A factored representation is unlikely to be pre-equipped
sD
with the attribute TruckAheadBackingIntoDairyFarmDrivewayBlockedByLooseCow with
STRUCTURED
REPRESENTATION value true or false. Instead, we would need a structured representation, in which ob-
jects such as cows and trucks and their various and varying relationships can be described
explicitly. (See Figure 2.16(c).) Structured representations underlie relational databases
al

and first-order logic (Chapters 8, 9, and 12), first-order probability models (Chapter 14),
knowledge-based learning (Chapter 19) and much of natural language understanding
ri

(Chapters 22 and 23). In fact, almost everything that humans express in natural language
concerns objects and their relationships.
As we mentioned earlier, the axis along which atomic, factored, and structured repre-
to

EXPRESSIVENESS sentations lie is the axis of increasing expressiveness. Roughly speaking, a more expressive
representation can capture, at least as concisely, everything a less expressive one can capture,
Tu

plus some more. Often, the more expressive language is much more concise; for example, the
rules of chess can be written in a page or two of a structured-representation language such
as first-order logic but require thousands of pages when written in a factored-representation
language such as propositional logic. On the other hand, reasoning and learning become
more complex as the expressive power of the representation increases. To gain the benefits
of expressive representations while avoiding their drawbacks, intelligent systems for the real
world may need to operate at all points along the axis simultaneously.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Section 2.5. Summary 59

2.5 S UMMARY

This chapter has been something of a whirlwind tour of AI, which we have conceived of as
the science of agent design. The major points to recall are as follows:
• An agent is something that perceives and acts in an environment. The agent function
for an agent specifies the action taken by the agent in response to any percept sequence.
• The performance measure evaluates the behavior of the agent in an environment. A

m
rational agent acts so as to maximize the expected value of the performance measure,
given the percept sequence it has seen so far.

co
• A task environment specification includes the performance measure, the external en-
vironment, the actuators, and the sensors. In designing an agent, the first step must
always be to specify the task environment as fully as possible.

a.
• Task environments vary along several significant dimensions. They can be fully or
partially observable, single-agent or multiagent, deterministic or stochastic, episodic or

iy
sequential, static or dynamic, discrete or continuous, and known or unknown.
• The agent program implements the agent function. There exists a variety of basic
agent-program designs reflecting the kind of information made explicit and used in the

un
decision process. The designs vary in efficiency, compactness, and flexibility. The
appropriate design of the agent program depends on the nature of the environment.
• Simple reflex agents respond directly to percepts, whereas model-based reflex agents
sD
maintain internal state to track aspects of the world that are not evident in the current
percept. Goal-based agents act to achieve their goals, and utility-based agents try to
maximize their own expected “happiness.”
• All agents can improve their performance through learning.
al
ri

B IBLIOGRAPHICAL AND H ISTORICAL N OTES


to

The central role of action in intelligence—the notion of practical reasoning—goes back at


least as far as Aristotle’s Nicomachean Ethics. Practical reasoning was also the subject of
Tu

McCarthy’s (1958) influential paper “Programs with Common Sense.” The fields of robotics
and control theory are, by their very nature, concerned principally with physical agents. The
CONTROLLER concept of a controller in control theory is identical to that of an agent in AI. Perhaps sur-
prisingly, AI has concentrated for most of its history on isolated components of agents—
question-answering systems, theorem-provers, vision systems, and so on—rather than on
whole agents. The discussion of agents in the text by Genesereth and Nilsson (1987) was an
influential exception. The whole-agent view is now widely accepted and is a central theme in
recent texts (Poole et al., 1998; Nilsson, 1998; Padgham and Winikoff, 2004; Jones, 2007).
Chapter 1 traced the roots of the concept of rationality in philosophy and economics. In
AI, the concept was of peripheral interest until the mid-1980s, when it began to suffuse many

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

60 Chapter 2. Intelligent Agents

discussions about the proper technical foundations of the field. A paper by Jon Doyle (1983)
predicted that rational agent design would come to be seen as the core mission of AI, while
other popular topics would spin off to form new disciplines.
Careful attention to the properties of the environment and their consequences for ra-
tional agent design is most apparent in the control theory tradition—for example, classical
control systems (Dorf and Bishop, 2004; Kirk, 2004) handle fully observable, deterministic
environments; stochastic optimal control (Kumar and Varaiya, 1986; Bertsekas and Shreve,
2007) handles partially observable, stochastic environments; and hybrid control (Henzinger

m
and Sastry, 1998; Cassandras and Lygeros, 2006) deals with environments containing both
discrete and continuous elements. The distinction between fully and partially observable en-
vironments is also central in the dynamic programming literature developed in the field of

co
operations research (Puterman, 1994), which we discuss in Chapter 17.
Reflex agents were the primary model for psychological behaviorists such as Skinner
(1953), who attempted to reduce the psychology of organisms strictly to input/output or stim-

a.
ulus/response mappings. The advance from behaviorism to functionalism in psychology,
which was at least partly driven by the application of the computer metaphor to agents (Put-

iy
nam, 1960; Lewis, 1966), introduced the internal state of the agent into the picture. Most
work in AI views the idea of pure reflex agents with state as too simple to provide much
leverage, but work by Rosenschein (1985) and Brooks (1986) questioned this assumption

un (see Chapter 25). In recent years, a great deal of work has gone into finding efficient algo-
rithms for keeping track of complex environments (Hamscher et al., 1992; Simon, 2006). The
Remote Agent program (described on page 28) that controlled the Deep Space One spacecraft
sD
is a particularly impressive example (Muscettola et al., 1998; Jonsson et al., 2000).
Goal-based agents are presupposed in everything from Aristotle’s view of practical rea-
soning to McCarthy’s early papers on logical AI. Shakey the Robot (Fikes and Nilsson,
1971; Nilsson, 1984) was the first robotic embodiment of a logical, goal-based agent. A
al

full logical analysis of goal-based agents appeared in Genesereth and Nilsson (1987), and a
goal-based programming methodology called agent-oriented programming was developed by
ri

Shoham (1993). The agent-based approach is now extremely popular in software engineer-
ing (Ciancarini and Wooldridge, 2001). It has also infiltrated the area of operating systems,
AUTONOMIC
where autonomic computing refers to computer systems and networks that monitor and con-
to

COMPUTING
trol themselves with a perceive–act loop and machine learning methods (Kephart and Chess,
2003). Noting that a collection of agent programs designed to work well together in a true
Tu

multiagent environment necessarily exhibits modularity—the programs share no internal state


and communicate with each other only through the environment—it is common within the
MULTIAGENT
SYSTEMS field of multiagent systems to design the agent program of a single agent as a collection of
autonomous sub-agents. In some cases, one can even prove that the resulting system gives
the same optimal solutions as a monolithic design.
The goal-based view of agents also dominates the cognitive psychology tradition in the
area of problem solving, beginning with the enormously influential Human Problem Solv-
ing (Newell and Simon, 1972) and running through all of Newell’s later work (Newell, 1990).
Goals, further analyzed as desires (general) and intentions (currently pursued), are central to
the theory of agents developed by Bratman (1987). This theory has been influential both in

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Exercises 61

natural language understanding and multiagent systems.


Horvitz et al. (1988) specifically suggest the use of rationality conceived as the maxi-
mization of expected utility as a basis for AI. The text by Pearl (1988) was the first in AI to
cover probability and utility theory in depth; its exposition of practical methods for reasoning
and decision making under uncertainty was probably the single biggest factor in the rapid
shift towards utility-based agents in the 1990s (see Part IV).
The general design for learning agents portrayed in Figure 2.15 is classic in the machine
learning literature (Buchanan et al., 1978; Mitchell, 1997). Examples of the design, as em-

m
bodied in programs, go back at least as far as Arthur Samuel’s (1959, 1967) learning program
for playing checkers. Learning agents are discussed in depth in Part V.
Interest in agents and in agent design has risen rapidly in recent years, partly because of

co
the growth of the Internet and the perceived need for automated and mobile softbot (Etzioni
and Weld, 1994). Relevant papers are collected in Readings in Agents (Huhns and Singh,
1998) and Foundations of Rational Agency (Wooldridge and Rao, 1999). Texts on multiagent

a.
systems usually provide a good introduction to many aspects of agent design (Weiss, 2000a;
Wooldridge, 2002). Several conference series devoted to agents began in the 1990s, including

iy
the International Workshop on Agent Theories, Architectures, and Languages (ATAL), the
International Conference on Autonomous Agents (AGENTS), and the International Confer-
ence on Multi-Agent Systems (ICMAS). In 2002, these three merged to form the International

un
Joint Conference on Autonomous Agents and Multi-Agent Systems (AAMAS). The journal
Autonomous Agents and Multi-Agent Systems was founded in 1998. Finally, Dung Beetle
Ecology (Hanski and Cambefort, 1991) provides a wealth of interesting information on the
sD
behavior of dung beetles. YouTube features inspiring video recordings of their activities.

E XERCISES
al

2.1 Suppose that the performance measure is concerned with just the first T time steps of
ri

the environment and ignores everything thereafter. Show that a rational agent’s action may
depend not just on the state of the environment but also on the time step it has reached.
to

2.2 Let us examine the rationality of various vacuum-cleaner agent functions.


a. Show that the simple vacuum-cleaner agent function described in Figure 2.3 is indeed
Tu

rational under the assumptions listed on page 38.


b. Describe a rational agent function for the case in which each movement costs one point.
Does the corresponding agent program require internal state?
c. Discuss possible agent designs for the cases in which clean squares can become dirty
and the geography of the environment is unknown. Does it make sense for the agent to
learn from its experience in these cases? If so, what should it learn? If not, why not?

2.3 For each of the following assertions, say whether it is true or false and support your
answer with examples or counterexamples where appropriate.
a. An agent that senses only partial information about the state cannot be perfectly rational.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

62 Chapter 2. Intelligent Agents

b. There exist task environments in which no pure reflex agent can behave rationally.
c. There exists a task environment in which every agent is rational.
d. The input to an agent program is the same as the input to the agent function.
e. Every agent function is implementable by some program/machine combination.
f. Suppose an agent selects its action uniformly at random from the set of possible actions.
There exists a deterministic task environment in which this agent is rational.
g. It is possible for a given agent to be perfectly rational in two distinct task environments.

m
h. Every agent is rational in an unobservable environment.
i. A perfectly rational poker-playing agent never loses.

co
2.4 For each of the following activities, give a PEAS description of the task environment
and characterize it in terms of the properties listed in Section 2.3.2.

a.
• Playing soccer.
• Exploring the subsurface oceans of Titan.

iy
• Shopping for used AI books on the Internet.
• Playing a tennis match.

un
Practicing tennis against a wall.
• Performing a high jump.
• Knitting a sweater.

sD
Bidding on an item at an auction.

2.5 Define in your own words the following terms: agent, agent function, agent program,
rationality, autonomy, reflex agent, model-based agent, goal-based agent, utility-based agent,
al

learning agent.
2.6 This exercise explores the differences between agent functions and agent programs.
ri

a. Can there be more than one agent program that implements a given agent function?
Give an example, or show why one is not possible.
to

b. Are there agent functions that cannot be implemented by any agent program?
c. Given a fixed machine architecture, does each agent program implement exactly one
Tu

agent function?
d. Given an architecture with n bits of storage, how many different possible agent pro-
grams are there?
e. Suppose we keep the agent program fixed but speed up the machine by a factor of two.
Does that change the agent function?

2.7 Write pseudocode agent programs for the goal-based and utility-based agents.
The following exercises all concern the implementation of environments and agents for the
vacuum-cleaner world.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

Exercises 63

2.8 Implement a performance-measuring environment simulator for the vacuum-cleaner


world depicted in Figure 2.2 and specified on page 38. Your implementation should be modu-
lar so that the sensors, actuators, and environment characteristics (size, shape, dirt placement,
etc.) can be changed easily. (Note: for some choices of programming language and operating
system there are already implementations in the online code repository.)
2.9 Implement a simple reflex agent for the vacuum environment in Exercise 2.8. Run the
environment with this agent for all possible initial dirt configurations and agent locations.
Record the performance score for each configuration and the overall average score.

m
2.10 Consider a modified version of the vacuum environment in Exercise 2.8, in which the
agent is penalized one point for each movement.

co
a. Can a simple reflex agent be perfectly rational for this environment? Explain.
b. What about a reflex agent with state? Design such an agent.

a.
c. How do your answers to a and b change if the agent’s percepts give it the clean/dirty
status of every square in the environment?

iy
2.11 Consider a modified version of the vacuum environment in Exercise 2.8, in which the
geography of the environment—its extent, boundaries, and obstacles—is unknown, as is the

un
initial dirt configuration. (The agent can go Up and Down as well as Left and Right.)
a. Can a simple reflex agent be perfectly rational for this environment? Explain.
b. Can a simple reflex agent with a randomized agent function outperform a simple reflex
agent? Design such an agent and measure its performance on several environments.
sD
c. Can you design an environment in which your randomized agent will perform poorly?
Show your results.
d. Can a reflex agent with state outperform a simple reflex agent? Design such an agent
al

and measure its performance on several environments. Can you design a rational agent
of this type?
ri

2.12 Repeat Exercise 2.11 for the case in which the location sensor is replaced with a
“bump” sensor that detects the agent’s attempts to move into an obstacle or to cross the
to

boundaries of the environment. Suppose the bump sensor stops working; how should the
agent behave?
Tu

2.13 The vacuum environments in the preceding exercises have all been deterministic. Dis-
cuss possible agent programs for each of the following stochastic versions:
a. Murphy’s law: twenty-five percent of the time, the Suck action fails to clean the floor if
it is dirty and deposits dirt onto the floor if the floor is clean. How is your agent program
affected if the dirt sensor gives the wrong answer 10% of the time?
b. Small children: At each time step, each clean square has a 10% chance of becoming
dirty. Can you come up with a rational agent design for this case?

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Chapter Topic
Problem Characteristics, Production Systems, Control
2.1 - 2.3 Strategies: Breadth First Search, Depth First Search,
Hill climbing and its Variations,

Heuristics Search Techniques: Best First Search, A*


3.1 - 3.3.2,
algorithm, Constraint Satisfaction Problem, Means-End
3.5, 3.6
Analysis.

12.1 - 12.3 Introduction to Game Playing, Min-Max and Alpha-Beta

m
pruning algorithms.

co
a.
TutorialsDuniya.com iy
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with
un
Solution* etc of following subjects from https://www.tutorialsduniya.com.
lsD

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
ia

 Data Structures  Operating Systems


or

 Computer Networks  Algorithms


 Android Programming  DataBase Management Systems
t
Tu

 PHP Programming  Software Engineering


 JavaScript  Theory of Computation
 Java Server Pages  Operational Research
 Python  System Programming
 Microprocessor  Data Mining
 Artificial Intelligence  Computer Graphics
 Machine Learning  Data Science
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
r ia
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

c om
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
a lsD
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

om
a .c
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
a lsD
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

o m
.c
i ya
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

om
a .c
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
r ia
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

om
a .c
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

om
a .c
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

o m
.c
i ya
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures

m
 Data Structures  Operating Systems
 Computer Networks  Algorithms

co
 Android Programming  DataBase Management Systems

a.
 PHP Programming  Software Engineering
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
sD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
al
ri

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


to

 Programs: https://www.tutorialsduniya.com/programs
Tu

 Question Papers: https://www.tutorialsduniya.com/question-papers


 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
r ia
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

o m
a .c
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

o m
.c
i ya
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
a lsD
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
r ia
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

om
a .c
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

om
a .c
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
a lsD
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
r ia
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Chapter Topic
Introduction to First Order Predicate Logic, Resolution
4 (4.1 - 4.9)
Principle, Unification.

7 (Complete) Semantic Nets, Conceptual Dependencies, Frames, and


Scripts, Production Rules, Conceptual Graphs.

1, 2, 3, 4 Programming in Logic (PROLOG)

m
co
a.
TutorialsDuniya.com
iy
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with
un
Solution* etc of following subjects from https://www.tutorialsduniya.com.
lsD

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
ia

 Data Structures  Operating Systems


 Computer Networks  Algorithms
or

 Android Programming  DataBase Management Systems


t
Tu

 PHP Programming  Software Engineering


 JavaScript  Theory of Computation
 Java Server Pages  Operational Research
 Python  System Programming
 Microprocessor  Data Mining
 Artificial Intelligence  Computer Graphics
 Machine Learning  Data Science
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

Tutorial Introduction

m
co
a.
1.1 Prolog

iy
Prolog is a computer programming language. Since its beginnings around 1970, Pro-
log has been chosen by many programmers for applications of symbolic computa-
un
tion, including:

relational databases
sD

mathematical logic
abstract problem solving
understanding natural language
al

design automation
symbolic equation solving
ri

biochemical structure analysis


to

many areas of artificial intelligence

Newcomers to Prolog find that the task of writing a Prolog program is not like spec-
Tu

ifying an algorithm in the same way as in a conventional programming language.


Instead, the Prolog programmer asks more about which formal relationships and ob-
jects occur in the problem, and which relationships are “true” about the desired solu-
tion. So, Prolog can be viewed as a descriptive language as well as a prescriptive one.
The Prolog approach is more about describing known facts and relationships about
a problem, and less about prescribing the sequence of steps taken by a computer to
solve the problem. When a computer is programmed in Prolog, the actual way the
computer carries out the computation is specified partly by the logical declarative se-

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

2 Chapter 1 Tutorial Introduction

mantics of Prolog, partly by what new facts Prolog can “infer” from the given ones,
and only partly by explicit control information supplied by the programmer.

1.2 Objects and Relationships


Prolog is a computer programming language that is used for solving problems

m
that involve objects and the relationships between objects. When we say “John owns
the book”, we are declaring that a relationship, ownership, exists between one object
“John” and another individual object “the book”. Furthermore, the relationship has

co
a specific order: John owns the book, but the book doesn’t own John! When we ask
the question, “Does John own the book?” we are trying to find out about a relation-
ship. Many problems can be expressed by specifying objects and their relationships.

a.
Solving the problem amounts to asking the computer to find out about objects and
relationships that can be derived from our program.

iy
Some relationships don’t always mention all the objects that are involved. For
example, when we say “The jewel is valuable”, we are specifying a relationship,
called “being valuable”, which involves a jewel. We did not mention who finds the
un
jewel valuable, or why. It all depends on what you want to say. In Prolog, when
you will be programming the computer about relationships like these, the amount of
detail you provide also depends on what you want the computer to accomplish.
sD

This way of talking about objects should not be confused with another popular
programming methodology called object-oriented programming. In object-oriented
programming, an object is a data structure that can inherit fields and executable meth-
ods from a class hierarchy to which the object belongs. Although the origin of object-
al

oriented programming can be traced back to the middle 1960s, it became popular in
the 1980s and 1990s with the introduction of Smalltalk-80, C++, and Java, among
other languages.
ri

By contrast, Prolog developed along an independent track from the early 1970s,
and was inspired by logic programming research. Prolog should not be compared
to

with object-oriented languages such as C++ and Java, because Prolog does a com-
pletely different job, and uses the word “object” in a completely different way. Pro-
log’s flexibility means that it is possible to write a Prolog program that interprets a
Tu

Prolog-like object-oriented language, but that is a different matter. So in Prolog, the


word “object” does not refer to a data structure that can inherit variables and methods
from a class, but it refers to things that we can represent using terms.
Prolog is a practical and efficient implementation of many aspects of “intelli-
gent” program execution, such as non-determinism, parallelism, and pattern-directed
procedure call. Prolog provides a uniform data structure, called the term, from which
all data, as well as Prolog programs, are constructed. A Prolog program consists of
a set of clauses, where each clause is either a fact about the given information or a

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.3 Programming 3

rule about how the solution may relate to or be inferred from the given facts. Thus,
Prolog can be seen as a first step towards the ultimate goal of programming in logic.
In this book we shall not be concerned greatly with the wider implications of logic
programming nor with why Prolog is not the ultimate logic programming language.
Instead, we will be concerned with showing how useful programs can be written
using the Standard Prolog systems that exist today.
There is one more point of philosophy to mention, then we shall begin program-

m
ming. We are all familiar with using rules to describe relationships between objects.
For example, the rule, “Two people are sisters if they are both female and have the
same parents” tells us something about what it means to be sisters. It also tells us

co
how to find out if two people are sisters: simply check to see if they are both female
and have the same parents. What is important to notice about rules is that they are
usually oversimplified, but they are acceptable as definitions. After all, one cannot

a.
expect a definition to tell us everything about something.
For example, most people would agree there is much more to “being sisters”

iy
in real life than the above rule implies. However, when we are solving a particular
problem, we need to concentrate on just those rules that help to solve the problem.
So, we ought to consider an imaginary and simplified definition if it is sufficient for
un
our purposes.
sD

1.3 Programming

In this chapter we shall show the essential elements of the Prolog in real pro-
grams, but without becoming diverted by details, formal rules, and exceptions. At
al

this point, we are not trying to be complete or precise. We want to bring you quickly
to the point where you can write useful programs, so to do that we must concentrate
ri

on the basics: facts, questions, variables, conjunctions, and rules. Other features of
Prolog, such as lists and recursion, will be treated in later chapters.
to

Computer programming in Prolog consists of:

specifying some facts about objects and their relationships,


Tu

defining some rules about objects and their relationships, and


asking questions about objects and their relationships.

For example, suppose we told a Prolog system our rule about sisters. We could then
ask the question whether Mary and Jane are sisters. Prolog would search through
what we told it about Mary and Jane, and come back with the answer yes or no,
depending on what we told it earlier. So, we can consider Prolog as a storehouse of

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4 Chapter 1 Tutorial Introduction

facts and rules, and it uses the facts and rules to answer questions. Programming in
Prolog consists of supplying all these facts and rules. Prolog can do much more than
answer yes-or-no questions. The Prolog system enables a computer to be used as a
storehouse of facts and rules, and it provides ways to make inferences from one fact
to another, finding the values of variables that lead to a logical deduction.
The usual way to use Prolog is interactively, which means that you and the
computer carry out a kind of conversation. The computer you use has a keyboard

m
and a display. You use the keyboard to type characters into the computer, and the
computer uses the display to show results to you. Prolog will wait for you to type in
the facts and rules that pertain to the problem you want to solve. Then, if you ask

co
the right kind of questions, Prolog will work out the answers and show them on the
display.
We shall now introduce each of the fundamentals of Prolog one by one. Don’t

a.
worry about not having the complete story about each feature of Prolog straight away.
There will be complete summaries and more examples worked out in later chapters.

iy
1.4 Facts
un
We first discuss facts about objects. Suppose we want to tell Prolog the fact that
“John likes Mary”. This fact consists of two objects, called “Mary” and “John”, and
sD

a relationship, called “likes”. In Prolog, we need to write facts in a standard form,


like this:
likes(john, mary).
al

The following things are important:


ri

The names of all relationships and objects must begin with a lower-case letter.
For example, likes, john, mary.
to

The relationship is written first, and the objects are written separated by commas,
and the objects are enclosed by a pair of round brackets.
Tu

The dot character “.” must come at the end of a fact. The dot is what some people
also call a “period” or a “full stop”.

When defining relationships between objects using facts, you should pay attention to
what order the objects are written between the round brackets. The order is arbitrary,
but you must decide on some order and be consistent about it. For example, in the
above fact, we have put the “liker” in as the first of the two objects in round brackets,
and we have but the object that is liked in the second slot. So, the fact likes(john,

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.4 Facts 5

mary) is not the same thing as likes(mary, john). The first fact says that John likes
Mary, and the second fact says that Mary likes John, according to our current arbi-
trary convention. If we want to say that Mary likes John, then we must explicitly say
so:
likes(mary, john).
Look at the following examples of facts, together with possible interpretations in

om
English:

valuable(gold). Gold is valuable.


female(jane). Jane is female.
owns(jane, gold). Jane owns gold.

c
father(john, mary). John is the father of Mary.

a.
gives(john, book, mary). John gives the book to Mary.

Each time a name is used, the name refers to a particular individual object. Because

iy
of our familiarity with English, it is fairly clear that the names john and jane refer
to individuals. But, in some other facts, we have used the names gold and valuable
un
and it is not obvious that they refer to individuals. This sort of name is called a “non-
count word” by logicians. When using names, we must decide on how to interpret
the name.
A name can have several interpretations. For example, the name gold could re-
sD

fer to a particular object. In this case we think of the object as some particular lump
of gold that we denote by the name gold. So when we say valuable(gold), we would
mean that this particular lump of gold, which we have named gold, is valuable. On
the other hand, we could interpret the name gold to be a word standing for the chem-
al

ical element Gold having atomic number 79, and when we say valuable(gold), we
would mean that the chemical element Gold is valuable. So, there is more than one
ri

way to interpret a name, and it is you, the programmer, who decides on the interpre-
tation. There should be no problem as long as you interpret names consistently. It is
important to think about the distinctions between different interpretations early, so
to

that you are quite certain what the names mean in your program.
Now for some terminology. The names of the objects that are enclosed within
Tu

the round brackets in each fact are called the arguments. Note that computer pro-
grammers use the word “argument” in a technical sense that bears none of the com-
mon connotations of dispute, debate, discussion, theme, or topic. The name of the
relationship, which comes just before the round brackets, is called the predicate.
So, valuable is a predicate having one argument, and likes is a predicate having two
arguments.
The names of the objects and relationships are completely arbitrary. Instead of a term
such as likes(john,mary), we could just as well represent this as a(b,c), and remember

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

6 Chapter 1 Tutorial Introduction

that a means likes, b means John, and c means Mary. However, we normally select
names that help us to remember what they represent. So, we must decide in advance
what our names mean, and what the order of arguments shall be. Thereafter we must
remain consistent.
Relationships can have an arbitrary number of arguments. If we want to define
a predicate called play, where we mention two players and a game they play with
each other, we need three arguments. Here are two examples of this:

m
play(john, mary, football).
play(jane, jim, badminton).

co
Using many arguments is important for representing complicated interactions be-
tween relationships, as we shall see later.

a.
We may also declare facts that are not true in the real world. We could write
king(john, france).

iy
to specify that John is the present king of France. In the real world this is obvi-
ously false, not least because the French monarchy was suppressed sometime around
1792 and John is an unlikely name for a modern day French king. But Prolog does
un
not know, and does not care. Facts in Prolog simply allow you to express arbitrary
relationships between arbitrary objects.
In Prolog, a collection of facts is called a database. We shall use the word
sD

database whenever we have collected together some facts (and later, rules) that are
used to solve a particular problem.
al

1.5 Questions
ri

Once we have some facts, we can ask some questions about them. In Prolog, a ques-
tion looks just like a fact, except that we put a special symbol before it. The special
to

symbol is written as a question mark followed by a hyphen. Consider the question:


?- owns(mary, book).
Tu

If we interpret mary to be a person called Mary, and book to be some particular book,
this question is asking Does Mary own the book?, or Is it a fact that Mary owns the
book? We are not asking whether she owns all books, or books in general.
When a question is asked of a Prolog system, it will search through the database.
It looks for facts that unify the fact in the question. Two facts unify if their predicates
are the same (spelled the same way), and if their corresponding arguments each are
the same. If Prolog finds a fact that unifies with the question, Prolog will respond
yes. If no such fact exists in the database, Prolog will respond no. The response from

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.5 Questions 7

Prolog appears on the display of your computer terminal on the line just below your
question. Consider the following database:

likes(joe, fish).
likes(joe, mary).
likes(mary, book).
likes(john, book).

m
likes(john, france).

If we typed in all those facts to the Prolog system, we could ask the following ques-

co
tions, and Prolog would give the answers (shown from now on in bold italic type) on
the line just after the question:

?- likes(joe, money).

a.
no
?- likes(mary, joe).

iy no
?- likes(mary, book).
yes
un
The answers to the first three questions should be clear to you. In Prolog, the answer
no is used to mean nothing unifies with the question. It is important to remember
that no is not the same as false. For example, suppose a database about some famous
sD

Greeks contains only the following three facts:

human(socrates).
human(aristotle).
al

athenian(socrates).
ri

We can ask some questions:

?- athenian(socrates).
to

yes
?- athenian(aristotle).
no
Tu

Although it may be true in real history that Aristotle once lived in Athens, we cannot
prove it simply from the facts shown in the database.
Now what happens if we ask a question about a relationship that is not in the
database? Suppose we are using the above database about likes, and ask the perfectly
sensible question:

?- king(john, france).

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

8 Chapter 1 Tutorial Introduction

The database says nothing about kings, even though john and france are in the
database. In most versions of Prolog, the answer no will be given, because noth-
ing about kings can be proved from the database. However, Standard Prolog now
provides a choice of behaviours if the relationship is not in the database. The ques-
tion can simply say no as in many Prolog systems, or a warning can be given before
saying no, or an error message can be printed. For example, using the above database
about Greeks, suppose we ask

om
?- greek(socrates).

Although it is shown in the database that Socrates is an Athenian, this does not prove
he is a Greek unless more information is in the database. Nothing about Greeks is

c
given in the database. So, a Standard Prolog system can say:

a.
Existence error: procedure greek
no

iy
Precisely what behaviour will take place depends on how your Standard Prolog sys-
tem is set up, so we will not be concerned with these details for now.
un
The facts and questions we have discussed so far are not particularly interesting.
All we can do is get back the same information we put in. It would be more useful to
ask question such as, What objects does Mary like? and Who lives in Athens? This is
what variables are for.
sD

1.6 Variables
al

If you want to find out what things John likes, it is tiresome to ask Does John like
ri

books?, Does John like Mary?, and so forth, with Prolog giving a yes-or-no answer
each time. It is more sensible to ask Prolog to tell you something that John likes. We
to

could phrase a question of this form as, Does John like X?. When we ask a question,
we do not know what the object is that X could stand for. We would like Prolog to
tell us what the possibilities are. In Prolog we can not only name particular objects,
Tu

but we can also use terms like X to stand for objects that we are unwilling or unable
to name. Terms of this second kind are called variables.
When Prolog uses a variable, the variable can be either instantiated or not in-
stantiated. A variable is instantiated when there is an object that the variable stands
for. A variable is not instantiated when what the variable stands for is not yet known.
Prolog can distinguish variables from names of particular objects because any name
beginning with a capital letter is taken to be a variable.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.6 Variables 9

When Prolog is asked a question containing a variable, Prolog searches through


all its facts to find an object that the variable could stand for. So when we ask Does
John like X?, Prolog searches through all its facts to find things that John likes.
A variable, such as X, does not name a particular object in itself, but it can
be used to stand for objects that we cannot name. For example, we cannot name
something that John likes as an object, so Prolog adopts a way of saying this. Instead
of asking a question like:

m
?- likes(john, something that John likes).
Prolog lets us use variables, like this:

co
?- likes(john, X).
Variables can have longer names if we wish. This question is acceptable to Prolog:

a.
?- likes(john, SomethingThatJohnLikes).
Why? Because a variable can be any word that begins with a capital letter. Consider

iy
the following database of facts, about what John likes followed by a question:
un
likes(john, flowers).
likes(john, mary).
likes(paul, mary).
?- likes(john, X).
sD

The question asks, Is there anything that John likes? When asked the question, Pro-
log will respond:
al

X = flowers
and then wait for further instructions, which we will talk about shortly. How does this
ri

work? When Prolog is asked this question, the variable X is initially not instantiated.
Prolog searches though the database, looking for a fact that unifies with the question.
to

Now if an uninstantiated variable appears as an argument, Prolog will allow that


argument to unify with any other argument in the same position in the fact. What
happens here is that Prolog searches for any fact where the predicate is likes, and the
Tu

first argument is john. The second argument in this case may be anything, because
the question was asked with an uninstantiated variable as the second argument. When
such a fact is found, then the variable X now stands for the second argument in the
fact, whatever it may be. Prolog searches through the database in the order it was
typed in (or top-to-bottom of the page) so the fact likes(john, flowers) is found first.
Variable X now stands for the object flowers. We say that X is instantiated to flowers.
Prolog now marks the place in the database where a unifier is found. The place-
marker is used for reasons we discuss shortly.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

10 Chapter 1 Tutorial Introduction

Once Prolog finds a fact that unifies with a question, it displays the objects
that the variables now stand for. In this case, the only variable was X, and it unified
with the object flowers, so Prolog replies X=flowers. Now Prolog waits for further
instructions, as we said above. If you press the E NTER key ¬ (sometimes called
the R ETURN key), meaning you are satisfied with just one answer, then Prolog will
stop searching for more. If instead you press the semicolon key ; (followed by the
E NTER key ¬), Prolog will resume its search through the database as before, starting

m
from where it left the place-marker, to find another possible answer to the question.
When Prolog begins searching from a place-marker instead of from the beginning of
the database, we say that Prolog is attempting to re-satisfy the question.

co
Suppose in response to Prolog’s first answer (X=flowers
( ) we asked it to carry
on (by typing ; ¬). This means we want to satisfy the question in another way;
we want to find another object that X could stand for. This means that Prolog must

a.
forget that X stands for flowers, and resume searching with X uninstantiated again.
Because we are searching for an alternative solution, the search is continued from

iy
the place-marker. The next unifying fact found is likes(john, mary). The variable X
is now instantiated to mary, and Prolog puts a place-marker at the fact likes(john,
mary). Prolog will reply X=mary and wait for further commands. If we type another
un
semicolon, Prolog will continue the search. In this example there is nothing more
that John likes. So, Prolog will stop its search, and allow us to ask more questions or
declare more facts.
sD

What happens if, given the same facts above, we ask the question:
?-likes(X, mary).
This question asks, Is there an object that likes Mary? By now you should see that
al

the objects in the example that like Mary are john and paul. Again, if we wanted to
see all of them, we would type ; ¬ after Prolog displays each one of the answers:
ri

?- likes(X, mary). our question.


X = john ; first answer. We type ; ¬ in reply.
second answer. Again we type ; ¬.
to

X = paul ;
no no more answers.
Tu

1.7 Conjunctions

Suppose we wish to answer questions about more complicated relationships such as,
Do John and Mary like each other? One way to do this would be first to ask if John
likes Mary, and if Prolog tells us yes, then we ask if Mary likes John. So, this problem
consists of two separate goals that the Prolog system must try to satisfy. Because a

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.7 Conjunctions 11

combination like this is frequently used by Prolog programmers, there is a special


notation for it. Suppose we have the following database:
likes(mary, chocolate).
likes(mary, wine).
likes(john, wine).
likes(john, mary).

m
We want to ask if John and Mary like each other. To do this, we ask, Does John like
Mary? and does Mary like John? The and expresses the idea that we are interested
in the conjunction of the two goals: we want to satisfy them both one after the other.

co
We represent this by putting a comma between the goals:
?- likes(john, mary), likes(mary, john).

a.
The comma is pronounced “and”, and it serves to separate any number of different
goals that have to be satisfied in order to answer a question. When a sequence of
goals (separated by commas) is given to Prolog, Prolog attempts to satisfy each goal

iy
in turn by searching for a unifying goal in the database. All goals have to be satisfied
in order for the sequence to be satisfied. Using the above list of facts, what should
un
Prolog display when given the above question? The answer is no. Why? It is a fact
that John likes Mary, so the first goal is true. However, the second goal cannot be
proved, since there is nowhere in the list of facts where likes(mary, john) occurs.
Since we wanted to know if they both like each other, the whole question is answered
sD

no.
Conjunctions and the use of variables can be combined to ask quite interesting
questions. Now that we know that it cannot be shown that John and Mary like each
other, we ask: Is there anything that John and Mary both like? This question also
al

consists of two goals:


First, find out if there is some X that Mary likes.
ri

Then, find out if John likes whatever X is.


to

In Prolog the two goals would be written as a conjunction like this:


?- likes(mary, X), likes(john, X).
Tu

Prolog answers the question by attempting to satisfy the first goal. If the first goal
is in the database, then Prolog will mark the place in the database, and attempt to
satisfy the second goal. If the second goal is satisfied, then Prolog marks that goal’s
place in the database, and we have found a solution that satisfies both goals. It is
most important to remember that each goal keeps its own place-marker.
If the second goal of a conjunction is not satisfied, then Prolog will attempt
to re-satisfy the previous goal (in this case the first goal). Remember that Prolog
searches the database completely for each goal. If a fact in the database happens to

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

12 Chapter 1 Tutorial Introduction

unify, satisfying the goal, then Prolog will mark the place in the database in case it
has to re-satisfy the goal at a later time. But when a goal needs to be re-satisfied,
Prolog will begin the search from the goal’s own place-marker, rather than from the
start of the database. Our above question is anything liked by Mary also liked by
John? illustrates an example of this “backtracking” behaviour in the following way:
1. The database is searched for the first goal. As the second argument (X) is unin-
stantiated, it may unify with anything. The first such unifying fact in our above

om
database is likes(mary, chocolate). So, now X is instantiated to chocolate every-
where in the question where X appears. Prolog marks the place in the database
where it found the fact, so it can return to this point in case it needs to re-satisfy
the goal. Furthermore, Prolog needs to remember that X became instantiated

.c
here, so Prolog can “forget” X if it needs to re-satisfy this goal.
2. Now, the database is searched for likes(john, chocolate). This is because the

ya
next goal is likes(john, X), and X currently stands for chocolate. As you can see,
no such fact exists, so the goal fails. Now when a goal fails, we must try to re-
satisfy the previous goal, so Prolog attempts to re-satisfy likes(mary, X), but this
i
time starting from the place that was marked in the database. But first Prolog
un
needs to make X uninstantiated once more, so X may unify with anything.
3. The marked place is likes(mary, chocolate), so Prolog begins searching from
after that fact. Because we have not reached the end of the database yet, we have
sD

not exhausted the possibilities of what Mary likes, and the next unifying fact is
likes(mary, wine). The variable X is now instantiated to wine, and Prolog marks
the place in case it must re-satisfy what mary likes.
al

4. As before, Prolog now tries the second goal, searching this time for likes(john,
wine). Prolog is not trying to re-satisfy this goal. It is entering the goal again
(from the left-hand side, as it were), so it must start searching from the beginning
ri

of the database. After not too much searching, the unifying fact is found, and
Prolog notifies you. Since this goal was satisfied, Prolog also marks its place in
to

the database, in case you want to re-satisfy the goal. There is a place-marker in
the database for each goal that Prolog is attempting to satisfy.
5. At this point, both goals have been satisfied. Variable X stands for the name wine.
Tu

The first goal has a place-marker in the database at the fact likes(mary, wine),
and the second goal has a place-marker in the database at the fact likes(john,
wine).
As with any other question, as soon as Prolog finds one answer, it stops and waits
for further instructions. If we type ; ¬ Prolog will search for more things that
both John and Mary like. We know now that this amounts to re-satisfying both goals
starting from the place-markers they left behind.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.7 Conjunctions 13

To sum up, we can imagine a conjunction of goals to be arranged from left to


right, separated by commas. Each goal may have a left-hand neighbour and a right-
hand neighbour. Clearly, the left-most goal does not have a left-hand neighbour, and
the right-most goal does not have a right-hand neighbour. When handling a conjunc-
tion of goals, Prolog attempts to satisfy each goal in turn, working from left to right.
If a goal becomes satisfied, Prolog leaves a place-marker in the database that is asso-
ciated with the goal. Think of this as drawing an arrow from the goal to the place in

m
the database where the solution is. Furthermore, any variables previously uninstanti-
ated might now be instantiated. This happened above at Step 1. If a variable becomes
instantiated, all occurrences of the variable in the question become instantiated. Pro-

co
log then attempts to satisfy the goal’s right-hand neighbour, starting from the top of
the database.
As each goal in turn becomes satisfied, it leaves behind a place-marker in the

a.
database (draws another arrow from the goal to the unifying fact), in case the goal
needs to be re-satisfied at a later time. Any time a goal fails (cannot find a unifying

iy
fact), Prolog goes back and attempts to satisfy its left-hand neighbour, starting from
its place-marker. Furthermore, Prolog must “uninstantiate” any variables that became
instantiated at this goal. In other words, Prolog must “undo” all the variables when
un
it re-satisfies a goal. If each goal, upon being entered from its right, cannot be re-
satisfied, then the failures will cause Prolog to gradually creep to the left as each
goal fails. If the first goal (the left-most goal) fails, then it does not have a left-
hand neighbour it can attempt to re-satisfy. In this case, the entire conjunction fails.
sD

This behaviour, where Prolog repeatedly attempts to satisfy and re-satisfy goals in a
conjunction, is called backtracking. Backtracking is summarised in the next chapter,
and is given a more complete and sophisticated treatment in Chapter 4.
al

When following the examples, you may find it helpful to write, below each
variable in a goal, the object that has been instantiated by the success of the goal.
You should also write in an arrow from the goal to its place-marker in the database.
ri

An example of this pencil-and-paper aid is shown below at four “snapshots” during


the evaluation of the above example. In each snapshot, the complete database and
to

question is shown, together with a numbered commentary. Goals which have been
satisfied are outlined in their own little box:
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

14 Chapter 1 Tutorial Introduction

likes(mary,food).
likes(mary,wine).
likes(john,wine).
likes(john,mary).

m
?- likes(mary,X) , likes(john,X).

co
food

1. The first goal succeeds, instantiating X to food.

a.
2. Next, attempt to satisfy the second goal:

iy
un
likes(mary,food).
likes(mary,wine).
likes(john,wine).
sD

likes(john,mary).

?- likes(mary,X) , likes(john,X).
al
ri

food

3. The second goal fails.


to

4. Next, backtrack: forget the previous X, and attempt to re-satisfy the first goal.
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.7 Conjunctions 15

likes(mary,food).
likes(mary,wine).
likes(john,wine).
likes(john,mary).

om
?- likes(mary,X) , likes(john,X).

wine

c
5. The first goal succeeds again, instantiating X to wine.

a.
6. Next, attempt to satisfy the second goal:

iy
un
likes(mary,food).
likes(mary,wine).
sD

likes(john,wine).
likes(john,mary).
al

?- likes(mary,X) , likes(john,X).
ri

wine
to

7. The second goal succeeds.


8. Prolog notifies you of success, and waits for a reply:
Tu

X = wine
Throughout this book we will endeavour to show where backtracking occurs in the
examples, and what effect it has on solving the problems. Backtracking is so impor-
tant that the whole of Chapter 4 is devoted to it.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

16 Chapter 1 Tutorial Introduction

Exercise 1.1: Continue the pencil-and-paper simulation of the example given above,
assuming that you have just typed a semicolon to initiate backtracking in order to
find out if John and Mary both like anything else.

1.8 Rules

m
Suppose we wanted to state the fact that John likes all people. One way to do this
would be to write down separate facts, like this:

co
likes(john, alfred).
likes(john, bertrand).

a.
likes(john, charles).
likes(john, david).
..
.
iy
for every person in our database. This could become tedious, especially if there are
un
hundreds of people in our Prolog program. Another way to say that John likes all
people is to say, John likes any object provided it is a person. This fact is in the form
of a rule about what John likes, instead of listing all the people John likes. In a world
sD

where John could like every person, the rule is much more compact than a list of
facts.
In Prolog, rules are used when you want to say that a fact depends on a group
of other facts. In English, we use the word “if” to express a rule. For example,
al

I use an umbrella if there is rain.


John buys the wine if it is less expensive than the beer.
ri

Rules are also used to express definitions, for example:


to

X is a bird if:
X is an animal, and
X has feathers.
Tu

or
X is a sister of Y if:
X is female, and
X and Y have the same parents.

In the above English definitions, we have used variables X and Y Y. It is important


to remember that a variable stands for the same object wherever it occurs in a rule.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.8 Rules 17

Otherwise we would be violating the spirit of the definition. For example, in the bird
rule above, we could not show that Fred is a bird because Fido is an animal and Mary
has feathers. The same principle of consistent interpretation of variables is true also
for rules in Prolog. If one X stands for Fred, then all the X’s in the same rule must
stand for Fred.
A rule is a general statement about objects and their relationships. For example,
we can say that Fred is a bird if Fred is an animal and Fred has feathers, and we can

om
also say that Bertram is a bird if Bertram is an animal and Bertram has feathers. So,
we can allow a variable to stand for a different object in each different use of the rule.
Within a use of a rule, of course, variables are interpreted consistently as pointed out
above.

c
Let us consider several examples, beginning with a rule using one variable and a

a.
conjunction.
John likes anyone who likes wine,
or, in other words,
John likes anything if it likes wine,
iy
un
or, with variables,
John likes X if X likes wine.
sD

In Prolog, a rule consists of a head and a body. The head and body are connected by
the symbol “:-”, which is made up of a colon and a hyphen. The “:-” is pronounced
f The above example is written in Prolog as:
if.
al

likes(john, X) :- likes(X, wine).


Notice that rules also end with a dot (actually a “period” or “full stop” character). The
ri

head of this rule is likes(john, X). The head of the rule describes what fact the rule is
intended to define. The body, in this case likes(X, wine), describes the conjunction of
goals that must be satisfied, one after the other, for the head to be true. For example,
to

we can make John more choosy about whom he likes, simply by adding more goals
onto the body, separated by commas:
Tu

likes(john, X) :- likes(X, wine), likes(X, food).


or, in words, John likes anyone who likes wine and food. Or, suppose John likes any
female who likes wine:
likes(john, X) :- female(X), likes(X, wine).
Whenever we look at a Prolog rule, we should take notice of where the variables are.
In the above rule, the variable X is used three times. Whenever X becomes instantiated

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

18 Chapter 1 Tutorial Introduction

to some object, all X’s are instantiated within the scope of X. For some particular use
of a rule, the scope of X is the whole rule, including the head, and extending to the
dot “.” at the end of the rule. So, in the above rule, if X happens to be instantiated to
mary, then Prolog will try to satisfy the goals female(mary) and likes(mary, wine).
Next, as an example of a rule that uses more than one variable, consider a
database consisting of facts about some of the family of Queen Victoria. We shall
use the predicate parents having three arguments such that: parents(X ,Y, Z) means

m
The parents of X are Y and Z. The second argument is for the mother, and the third
argument is for the father. We shall also use the predicates female and male in the
obvious way. One part of the database might look like this:

co
male(albert).
male(edward).

a.
female(alice).
female(victoria).
parents(edward, victoria, albert).
parents(alice, victoria, albert). iy
un
Now we shall use the rule about sister of described earlier. The rule defines the
predicate sister_of, having two arguments such that sister_of(X, Y) is a fact if X is
a sister of Y. Notice that we have used the underscore character “ ” in the predicate
name. Although we have not yet given the complete rules for how to construct names,
sD

it is permitted to include underscores in a name, and we shall summarise the rules in


the next chapter. Now X is a sister of Y if:

X is female,
al

X has mother M and father F, and


ri

Y has the same mother and father as X does.

This can be written as the following Prolog rule:


to

sister_of(X, Y) :-
female(X),
Tu

parents(X, M, F),
parents(Y, M, F).
Or if you prefer, you can write it out on one line like this:
sister_of(X, Y) :- female(X), parents(X, M, F), parents(Y, M, F).
We use the variable names M and F to indicate mother and father, although we could
have used Mother and Father had we been so inclined. Notice that we are using vari-
ables that do not appear in the head of the rule. These variables, M and F, are treated

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.8 Rules 19

in the same way as any other variable. When Prolog uses the rule, variables M and
F will initially be uninstantiated, so they will unify with anything when it becomes
time to satisfy the goal parents(X,M,F). However, as soon as they are instantiated,
then all the M’s and F’s in this use of the rule will become instantiated. The following
example should help to explain how these variables are used. Let us ask the question:
?- sister_of(alice, edward).

m
When asked this question given the above database and rule for sister_of, Prolog
proceeds as follows:

co
1. First, the question unifies with the head of the only sister_of rule above, so X
in the rule becomes instantiated to alice, and Y becomes instantiated to edward.
The place marker for the question is put against this rule. Now Prolog attempts

a.
to satisfy the three goals in the body, one by one.
2. The first goal is female(alice) because X was instantiated to alice in the previous

iy step. This goal is true from the list of facts, so the goal succeeds. As it succeeds,
Prolog marks the goal’s place in the database (the third entry in the database). No
new variables were instantiated, so no other note is made. Prolog now attempts
un
to satisfy the next goal.
3. Now Prolog searches for parents(alice, M, F), where M and F will unify with
any arguments because they are uninstantiated. A unifying fact is parents(alice,
sD

victoria, albert), so the goal succeeds. Prolog marks the place in the database
(sixth down from the top) and records that M became instantiated to victoria, and
F to albert. (You may write these under the goal in the rule if you want to keep
track of them on paper). Prolog now attempts to satisfy the next goal.
al

4. Now Prolog searches for parents(edward, victoria, albert) because Y is known


as edward from the question, and M and F were known to stand for victoria and
ri

albert from the previous goal. The goal succeeds, because a unifying fact is
found (fifth down from the top). Since it is the last goal in the conjunction, the
to

entire goal succeeds, and the fact sister_of(alice, edward) is established as true.
Prolog answers yes.
Suppose we want to know if Alice is the sister of anyone. The appropriate question
Tu

in Prolog is
?- sister_of(alice, X).
For this question, Prolog proceeds as follows:

1. The question unifies with the head of the only sister_of rule. Variable X in the
rule becomes instantiated to alice. As variable X in the question is uninstanti-
ated, then variable Y in the question will also be uninstantiated. However, these

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

20 Chapter 1 Tutorial Introduction

variables now become shared. We can also say that these variables are corefer-
ences, because each one references the other. As soon as one of the variables
becomes instantiated to an object, the other variable becomes instantiated to the
same object. At the moment, as we said, they are not instantiated. Coreferencing
variables are explained more in the next chapter.
2. The first goal is female(alice), which succeeds as before.

m
3. The second goal is parents(alice, M, F), and it unifies with parents(alice, victo-
ria, albert). Variables M and F are now known.

co
4. As Y is not yet known, the third goal is therefore parents(Y, victoria, albert).
This goal unifies with the fact parents(edward, victoria, albert). Variable Y is
now known to be edward.

a.
5. Since all goals succeed, the entire rule succeeds, with X known to be alice (given
in the question), and Y as edward. Since Y (in the rule) is shared with X (in

iy
the question), then X is also instantiated to edward. Prolog displays the reply
X=edward.
un
As usual, Prolog waits for you to tell it if you want to find all the solutions to the
question. As it turns out, this question has more than one solution. How Prolog finds
the remaining solution[s] is set as an exercise at the end of this chapter.
As we have seen thus far, there are two ways to provide information about a
sD

given predicate such as likes. We can provide both facts and rules. In general, a
predicate is defined by a mixture of facts and rules. These are called the clauses for
a predicate. We shall use the word clause whenever we refer to either a fact or a rule.
al

As a further example, this time not dealing with monarchs, consider the rule: A
person may steal something if the person is a thief and the person likes the thing. In
Prolog, this is written:
ri

may_steal(P, T) :- thief(P), likes(P, T).


to

Here we use the predicate may_steal, which has two arguments P and T to represent
the idea that some person P may steal thing T. This rule depends on clauses for thief
and likes. These could be represented as a mixture of facts and rules, whatever is
Tu

most appropriate. For example, consider the following Prolog database, which has
been made up from clauses discussed earlier. We have added some clause numbers
enclosed between /*. . .*/ brackets. This is how we write a comment. Comments
are ignored by Prolog, but we may add them to our programs for convenience. In the
discussion that follows, we shall refer to the clause number comments.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.8 Rules 21

/*1*/ thief(john).

/*2*/ likes(mary, chocolate).


/*3*/ likes(mary, wine).
/*4*/ likes(john, X) :- likes(X, wine).

/*5*/ may_steal(X, Y) :- thief(X), likes(X, Y).

m
Notice that the definition of likes has three separate clauses: two facts and a rule. Let
us follow what happens when the question What may John steal? is asked. First, this

co
question translates into Prolog as:
?- may_steal(john, X).
To answer this question, Prolog searches as follows:

a.
1. First, Prolog searches in the database for a clause about may_steal, and finds one

iy
in the form of a rule at clause number 5. Prolog marks the place in the database.
Since it is a rule, its body must be satisfied to establish whether the head is true.
So the X in the rule is instantiated to john from the question. Again we find that
un
we have to unify two uninstantiated variables (X in the question and Y in the rule),
so they will share. The goals of a rule must succeed for the rule to succeed: the
first goal, thief(john) is now searched for.
sD

2. The goal succeeds, since thief(john) is in the database (clause 1). Prolog marks
the place in the database, and no variables have become newly instantiated. Pro-
log then attempts to satisfy the second goal using clause 5. Since X still stands
for john, Prolog now searches for likes(john, Y). Notice that Y is still instantiated
al

at this point.
3. The goal likes(john, Y) unifies with the head of the rule (at clause 4). The Y in
ri

the goal shares with the X in the head, and both remain uninstantiated. To satisfy
this rule, likes(X, wine) is now searched for.
to

4. The goal succeeds, because it unifies with likes(mary, wine), the fact at clause
3. So, X now stands for mary. Since the goal in clause 4 succeeds, the whole rule
Tu

succeeds. The fact likes(john, mary) is established from clause 4 because Y in


clause 5 shares with X; it is also instantiated to mary.
5. Clause 5 now succeeds, with Y instantiated to mary. As Y was shared with the
second argument of the original question, X in the question is now instantiated to
mary.

We chose this example to show how easy it is to generate strange and unexpected
answers, such as “John may steal Mary”. This conclusion is a logical deduction from

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

22 Chapter 1 Tutorial Introduction

the program, but it might not make much sense to us. Sometimes this can indicate
a problem with the program. The reasoning behind establishing that John may steal
Mary is:

In order to steal something, first John must be a thief. From clause 1, this is
a fact. Next John must like the thing. From clause 4, we see that John likes
anything that likes wine. From clause 3, we see that Mary likes wine. There-

m
fore, John likes Mary. Therefore, both conditions for stealing something can
be satisfied, so John may steal Mary.

co
Notice that the fact (clause 2) that Mary likes chocolate, is irrelevant to this particular
question.
In this example we have repeatedly used the variables X and Y in different

a.
clauses. For example, in the may_steal rule, X stands for the object that can steal
something. But in the likes rule, X stands for the object that is liked. In order for this

iy
program to make sense, Prolog must be able to tell that X can stand for two different
things in two different uses of the clauses. Remember that knowing the scope of a
variable can resolve any confusion. We could have used more mnemonic names to
un
attempt to prevent any confusion, but we use simple names such as X to demonstrate
the scoping principle.
sD

1.9 Summary and Exercises


al

At this point we have covered most of the basic core of Prolog. In particular, we have
looked at
ri

Asserting facts about objects.


Asking questions about the facts.
to

Using variables and what their scopes are.


Conjunction as a way of saying “and”.
Tu

Representing relationships in the form of rules.


An introduction to backtracking.

With this small number of building blocks, it is possible to write useful programs for
manipulating simple databases, and it would probably be a good idea if you did so
by working out the exercises below.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

1.9 Summary and Exercises 23

When you begin to write programs for a Prolog system that is available to you,
you should consult its reference manual to see how to begin a programming session.
You will also find some practical tips in Chapter 8.
After you have this much of Prolog under your control, you should carry on
into the next chapter, which makes clear some of the points we did not mention in
this chapter. Also, we shall show how to work with numbers in Prolog. The features
covered in the next few chapters are where the expressiveness and convenience of

m
Prolog become apparent.

Exercise 1.2 When the sister_of rule is applied to the database of part of Queen Vic-

co
toria’s family discussed previously, more than one answer can be obtained. Explain
how all the answers can be obtained, and what they are.

a.
Exercise 1.3 This exercise has been inspired by one in Robert Kowalski’s book Logic
for Problem Solving, published by North Holland in 1979. Suppose someone has
already written Prolog clauses that define the following relationships:
father(X, Y)
mother(X, Y)
iy
/* X is the father of Y */
/* X is the mother of Y */
un
male(X) /* X is male */
female(X) /* X is female */
parent(X, Y) /* X is a parent of Y */
sD

diff(X, Y) /* X and Y are different */


The exercise is to write Prolog clauses to define the following relationships:
is_mother(X) /* X is a mother */
al

is_father(X) /* X is a father */
is_son(X) /* X is a son */
sister_of(X, Y) /* X is a sister of Y */
ri

grandpa_of(X, Y) /* X is a grandfather of Y */
sibling(X, Y) /* X is a sibling of Y */
to

For example, we could write a rule for aunt, provided we were supplied with (or
wrote) rules for female, sibling, and parent.
Tu

aunt(X, Y) :- female(X), sibling(X, Z), parent(Z, Y).


This could also be written:
aunt(X, Y) :- sister_of(X, Z), parent(Z, Y).
provided that we wrote the sister_of rule.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

24 Chapter 1 Tutorial Introduction

Exercise 1.4 Using the sister_of rule defined in the text, explain why it is possible
for some object to be her own sister. How would you change the rule if you did not
want this property? Hint: assume that the predicate diff of Exercise 1.3 is already
defined.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

A Closer Look

m
co
a.
In this chapter we provide a more complete discussion of the parts of Prolog that were
introduced in the previous chapter. Prolog provides ways to structure data as well as

iy
ways to structure the order in which attempts are made to satisfy goals. Structuring
data involves knowing the syntax by which we can denote data. Structuring the order
un
in which goals are solved involves knowing about backtracking.

2.1 Syntax
sD

The syntax of a language describes how we are allowed to fit words together. In


English, the syntax of the sentence “I see a zebra” is correct, but the syntax of “zebra
al

see I a” is not correct. In the first chapter, we did not discuss the syntax of Prolog
explicitly, but we simply showed what some parts of Prolog looked like. Here we
will summarise the syntax of those parts of Prolog we have seen thus far.
ri

Prolog programs are built from terms. A term is either a constant, a variable, or
a structure. We saw each of these terms in the previous chapter, but we did not know
to

them by these names. Each term is written as a sequence of characters. Characters


are divided into four categories as follows:
Tu

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
+-*/\~^ <>:.? @#$&

The first row consists of upper-case letters. The second row consists of lower-case
letters. The third row consists of digits. The fourth row consists of sign characters.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

26 Chapter 2 A Closer Look

There are actually more sign characters than those shown in the fourth row, but oth-
ers have special uses discussed below. Each kind of term, whether it is a constant,
variable, or structure, has different rules for how characters are put together to form
its name. Now we shall summarise each kind of term.

2.1.1 Constants

om
Constants name specific objects or specific relationships. There are two kinds of
constants: atoms, and numbers. Examples of atoms are the names that were given in
the last chapter:

.c
likes mary john book wine owns jewels can_steal
The special symbols that Prolog uses to denote questions “?-” and rules “:-” are

ya
also atoms. There are two kinds of atoms: those made up of letters and digits, and
those made up from signs. The first kind must normally begin with a lower-case
letter, as did all the ones we saw in the previous chapter. Those atoms made from
i
signs normally are made up from signs only. Sometimes it may be necessary to have
un
an atom beginning with a capital letter or a digit. If an atom is enclosed in single
quotes “’”, then the atom may have any characters in its name. Finally, the underline
character “_” may be inserted in the middle of an atom to improve legibility. The
following are further examples of atoms:
sD

a void = ’george-smith’ --> george_smith ieh2304


The following are not examples of atoms:
al

2304ieh george-smith Void _alpha


Numbers are the other kind of constant. We have not discussed how to do arithmetic
ri

in Prolog, but this will be introduced later in this chapter. Here are some examples of
numbers:
to

-17 -2.67e2 0 1 99.9 512 8192 14765 67344 6.02e-23


Most of these are familiar. The “e” notation is used to denote a power of 10. So, for
Tu

example, the number -2.67e2 is -2.67 10 or just -267; 6.02e-23 is 6.02 10 .


In addition to these numbers, Prolog programmers have added libraries to define
features such as arithmetic operations on rational numbers and numbers of arbitrary
precision, but we will not be needing these in this book.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

2.1 Syntax 27

2.1.2 Variables

The second kind of term used in Prolog is the variable. Variables look like atoms,
except they have names beginning with a capital letter or an underline sign “_”.
A variable should be thought of as standing for some object that we are unable or
unwilling to name at the time we write the program. This corresponds roughly to the
use of a pronoun in English. In the example Prolog clauses we have seen so far, we

m
have used variables with names such as X, Y, and Z. However, the names can be as
long as you like, for example:

co
Answer Input Gross_Pay _3_blind_mice A_very_long_variable_name
Sometimes one needs to use a variable, but its name will never be used. For example,
if we want to find out if anyone likes John, but we do not need to know just who it is,

a.
we can use the anonymous variable. The anonymous variable is written as a single
underline character. Our example is written in Prolog as:

iy ?- likes(_, john).
Several anonymous variables in the same clause need not be given consistent inter-
un
pretations. This is a characteristic peculiar to the anonymous variable. It is used to
save having to dream up different variable names when they will not be used else-
where in the clause.
sD

2.1.3 Structures
al

The third kind of term with which Prolog programs are written is the structure. Struc-
tures are called “compound terms” in Standard Prolog, but in this book we use the
word “structure” because it is shorter and more easily distinguished from other kinds
ri

of terms. A structure is a single object consisting of a collection of other objects,


called components. The components are grouped together into a single structure for
to

convenience in handling them.


One example of a structure in real life is an index card for a library book. The
index card will contain several components: the author’s name, the title of the book,
Tu

the date when it was published, the location where it can be found in the library, and
so forth. Some of the components can be broken down into further components. For
example, the author’s name consists of some initials and a surname.
Structures help to organise the data in a program because they permit a group
of related information to be treated as a single object (a library card) instead of as
separate entities. The way that you decompose data into components depends on
what problem you want to solve, and later on we will give advice on how to do this.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

28 Chapter 2 A Closer Look

Structures are also useful when there is a common kind of object, of which
many may exist. Books, for example. In Chapter 1 we discussed the fact
owns(john, book).
to denote that John owns some particular book. If we later said
owns(mary, book).

om
this means that Mary owns the same object that John owns, because it has the same
name. There is no other way of telling objects apart, except by their name. We could
say:

owns(john, wuthering_heights).

.c
owns(mary, moby_dick).

ya
to specify more carefully what books John and Mary own. However, in large pro-
grams, it may become confusing to have many different constants with no context to
tell what they mean. Someone reading this Prolog program may not know that we
i
meant wuthering_heights to be the name of the book written by the author Emily
un
Bronte¨ who flourished in Yorkshire, England during the 19th Century. Perhaps they
will think that John has named his pet rabbit “wuthering h heights”, say. Structures
can help to provide this context.
A structure is written in Prolog by specifying its functor and its components.
sD

The functor names the general kind of structure, and corresponds to a datatype in an
ordinary programming language. The components are enclosed in round brackets and
separated by commas. The functor is written just before the opening round bracket.
al

Consider the following fact, that John owns the book called Wuthering Heights, by
Emily Brontë:
owns(john, book(wuthering_heights, bronte)).
ri

Inside the owns fact we have a structure by the name of book, which has two compo-
to

nents, a title and an author. Since the book structure appears inside the fact as one of
the fact’s arguments, it is acting as an object, taking part in a relationship. If we like,
we can also have another structure for the author’s name, because there were three
Tu

Bronte¨ writers we wish to distinguish:


owns(john, book(wuthering_heights, author(emily, bronte))).
Structures may participate in the process of question-answering using variables. For
example, we may ask if John owns any book by any of the Brontë¨ sisters:
?- owns(john, book(X, author(Y, bronte))).

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions et

2.2 Characters 29

If this is true, X will then be instantiated to the title that was found, and Y will be
instantiated to the first name of the author. Or, we may not need to use the variables,
so we can use anonymous ones:
?- owns(john, book(_, author(_, bronte))).
Remember that the anonymous variables never co-refer with any other variable, even
other anonymous variables.

m
We could improve the book structure by adding another argument indicating
which copy the book is. For example, a third argument, where we would insert an
integer, would provide a way of uniquely identifying a book:

co
owns(john, book(ulysses, author(james, joyce), 3129)).
which we could use to represent John owns the 3,129th copy of Ulysses, by James

a.
Joyce.
If you have guessed that the syntax for structures is the same as for Prolog

iy
facts, you are correct. A predicate (used in facts and rules) is actually the functor
of a structure. The arguments of a ffact or rule are actually the components of a
structure. There are many advantages to representing Prolog programs themselves as
un
structures. It is not important to know why just now, but do keep in mind that all parts
of Prolog, even Prolog programs themselves, are made up of constants, variables, and
structures.
sD

2.2 Characters
al

The names of constants and variables are built up from strings of characters. Al-
though each kind of name (atom, integer, variable) has special rules about what char-
ri

acters may make it up, it is helpful to know what all the characters are that Prolog
recognises. This is because a character can be treated as an item of data in its own
to

right. In Standard Prolog, a character is actually an atom of length 1. It is most


common to use input and output operations on characters; this will be discussed in
Chapter 5.
Tu

Prolog recognises two kinds of characters: printing characters and non-printing


characters. Printing characters cause a symbol to appear on your computer terminal’s
display. Non-printing characters do not cause a symbol to appear, but cause an action
to be carried out. Such actions include printing a blank space, beginning new lines of
text, or perhaps making a beeping sound. The following are all the printing characters
that can be used

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions et
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

30 Chapter 2 A Closer Look

abcdefghijklmnopqrstuvwxyz
0123456789
! " # $ % & ’ ( ) = - ~ ^ | \{ } [ ] _ ‘ @ + ; * : < > , . ? /

You should recognise this as a more complete set than the one given at the begin-
ning of this chapter. Some of the characters have special meanings. For example, the

om
round brackets are used to enclose the components of a structure. However, we shall
see in later chapters that all the characters may be treated as information by Prolog
programs. Characters may be printed, read from the keyboard, compared, and take
part in arithmetic operations.

c
a.
2.3 Operators

iy
Sometimes it is convenient to write some functors as operators. This is a form of
syntax that makes some structures easier to read. For example, arithmetic operations
un
are commonly written as operators. When we write the arithmetic expression “x + y
* z”, we call the “plus” sign and the “multiply” sign operators. If we had to write the
arithmetic expression “x + y * z” in the normal way for structures, it would look like
this: +(x,*(y,z)), and this would be a legal Prolog term. The operators are sometimes
sD

easier to use, however, because we have grown accustomed to using them in arith-
metic expressions ever since our schooldays. Also, the structure form requires that
round brackets be placed around the functor’s components, which may be awkward
al

at times.
It is important to note that the operators do not “cause” any arithmetic to be
carried out. So in Prolog, 3+4 does not mean the same thing as 7. The term 3+4 is
ri

another way to write the term +(3,4), which is a data structure. Later we shall explain
a way in which structures can be interpreted as though they represent arithmetic
to

expressions, and evaluated according to the rules of arithmetic.


First we need to know how to read arithmetic expressions that have operators in
them. To do this, we need to know three things about each operator: its position, its
Tu

precedence, and its associativity. In this section we will describe how to use Prolog
operators with these three things in mind, but we will not go into very much detail at
this point. Although many different kinds of operators can be made up, we shall deal
only with the familiar atoms +, -, *, and /.
The syntax of a term with operators depends in part on the position of the opera-
tor. Operators like plus (+), hyphen (-), asterisk (*), and slash (/) are written between
their arguments, so we call them infix operators. It is also possible to put operators
before their arguments, as in “-x + y”, where the hyphen before the x is used in

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

2.3 Operators 31

arithmetic to denote negation. Operators that come before their arguments are called
prefix operators. Finally, some operators may come after their argument. For exam-
ple, the factorial operator, used by mathematicians, comes after the number you want
to find the factorial of. In mathematical notation, the factorial of x is written “x!”,
where the exclamation sign is used to denote ffactorial. Operators that are written af-
ter their arguments are called postfix operators. So, the position of an operator tells
where it is written with relationship to its arguments. It turns out that the operators

m
that we will use in the next section are all infix operators.
Now precedence. When we see the term “x + y * z”, and assume that it can be
interpreted as an arithmetic expression, we know that to evaluate it, we must multiply

co
y and z first, then add x. This is because we were taught in school that mutiplications
and divisions are done before additions and subtractions, except where brackets are
used for grouping. On the other hand, the structure form +(x,*(y,z)) makes explicit

a.
the rule that the multiplication is done before the addition. This is because the “*”
structure is an argument of the “+” structure, so if we actually wanted the computer

iy
to carry out the calculation, the “*” has to be carried out first in order for “+” to know
what its arguments are. So when using operators, we need rules that tell us the order
in which operations are carried out. This is what precedence tells us about.
un
The precedence of an operator is used to indicate which operation is carried
out first. Each operator that is used in Prolog has a precedence class associated with
it. The precedence class is an integer that is associated with an operator. The exact
value of the integer depends on the particular version of Prolog you are using, and
sD

we will give details in Chapter 5. However, it is always true that an operator with a
higher precedence has a precedence class that is closer to 1. If precedence classes
range from 1 to 255, then an operator in the first precedence class is carried out
al

first, before operators belonging to the 129th (say) precedence class. In Prolog the
multiplication and division operators are in a higher precedence class than addition
and subtraction, so the term a-b/c is the same as the term -(a,/(b,c)). The exact
ri

association of operators to precedence classes is not important at the moment, but it


is worth remembering the relative order in which operations are carried out.
to

Finally, consider how different operators associate. How they associate comes
to our attention when we have several operators of the same precedence. When we
see the expression “8/2/2”, does this mean “(8/2)/2” or “8/(2/2)”? In the first case,
Tu

the expression could be interpreted to mean 2, and in the second case, 8. To be able
to distinguish between these two cases, we must be able to tell whether an operator
is left associative or right associative. A left associative operator must have the same
or lower precedence operations on the left, and lower precedence operations on the
right. For example, all the arithmetic operations (add, subtract, multiply, and divide)
are left associative. This means that expressions like “8/4/4” are read as “(8/4)/4”.
Also, “5+8/2/2” is read as “5+((8/2)/2)”.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

32 Chapter 2 A Closer Look

In practice, people tend to use round brackets for expressions that may be dif-
ficult to understand because of the precedence and associativity rules. In this book
we will also try to make it as clear as possible by using lots of round brackets, but
it is still important to know the syntax rules for operators so your understanding of
operators is complete.
Remember that a structure made up of arithmetic operators is like any other
structure. No arithmetic is actually carried out until commanded by the “is” predicate

m
described in Section 2.5.

co
2.4 Equality and Unification

a.
One noteworthy predicate is equality, which is an infix operator written as “=”. When
an attempt is made to satisfy the goal

iy ?- X = Y.
(pronounced “X equals Y”), Prolog attempts to unify X and Y, and the goal succeeds
un
if they unify. We can think of this act as trying to make X and Y equal. The equality
predicate is built-in, which means that it is already defined in the Prolog system. The
equality predicate works as though it were defined by the following fact:
sD

X = X.
Within a use of some clause, X always equals X, and we exploit this property when
defining the equality predicate in the way shown.
Given a goal of the form X=Y, where X and Y are any two terms which are per-
al

mitted to contain uninstantiated variables, the rules for deciding whether X and Y are
equal are as follows:
ri

If X is an uninstantiated variable, and if Y is instantiated to any term, then X


to

and Y are equal. Also, X will become instantiated to whatever Y is. For exam-
ple, the following question succeeds, causing X to be instantiated to the structure
rides(student, bicycle):
Tu

?- rides(student, bicycle) = X.
Integers and atoms are always equal to themselves. For example, the following
goals have the behaviour shown:
policeman = policeman succeeds
paper = pencil fails
1066 = 1066 succeeds
1206 = 1583 fails

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

2.4 Equality and Unification 33

Two structures are equal if they have the same functor and number of compo-
nents, and all the corresponding components are equal. For example, the follow-
ing goal succeeds, and causes X to be instantiated to bicycle:
rides(student, bicycle) = rides(student, X)
Look closely: this is not a question about rides; it is a question about =.

m
Structures can be “nested” one inside another to any depth. If such nested structures
are tested for equality, the test may take more time to carry out, because there is more
structure to test. The following goal

co
a(b, C, d(e, F, g(h, i, J))) = a(B, c, d(E, f, g(H, i, j)))
would succeed, and causes B to be instantiated to b, C to c, E to e, F to f, H to h, and J to

a.
j. What happens when we attempt to make two uninstantiated variables equal? This
is just a special case of the first rule above. The goal succeeds, and the two variables

iy
share. If two variables share, then whenever one of them becomes instantiated to
some term, the other one automatically is instantiated to the same term. A more
technical way to say this is that the variables co-refer: that is, they refer to the same
un
thing. So, in the following rule, the second argument will be instantiated to whatever
the first argument is:
equal(X, Y) :- X = Y.
sD

An X = Y goal will always succeed if either argument is uninstantiated. An easier way


to write such a rule is to take advantage of the fact that a variable equals itself, and
write:
al

equal(X, X).
ri

Exercise 2.1: Say whether the following goals would succeed, and which variables,
if any, would be instantiated to what values:
to

pilots(A, london) = pilots(london, paris)


point(X, Y, Z) = point(X1, Y1, Z1)
letter(C) = word(letter)
Tu

noun(alpha) = alpha
’student’ = student
f(X, X) = f(a, b)
f(X, a(b, c)) = f(Z, a(Z, c))

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

34 Chapter 2 A Closer Look

2.5 Arithmetic

Many people use computers to do operations on numbers. Arithmetic operations are


useful for comparing numbers and for calculating results. In this section we will see
examples of each kind.
First, consider comparing numbers. Given two numbers, we can tell whether
one number is equal to the other, or less than the other, or greater than the other.

m
Prolog provides certain built-in predicates for comparing numbers. Actually these
predicates evaluate terms that are treated as arithmetic expressions. The arguments

co
could be variables instantiated to integers, or they could be integers written as con-
stants, or they could be more general expressions. Here we will use these predicates
just for comparing numbers, but later we’ll use them in general arithmetic expres-

a.
sions. Note that we are allowed to write them as infix operators:
X =:=Y X and Y stand for the same number
X =\=Y X and Y stand for different numbers
X<Y
X>Y
X is less than Y
X is greater than Y
iy
un
X =< Y X is less than or equal to Y
X >= Y X is greater than or equal to Y
Note that the “less than or equal to” symbol is not written as “<=” as in many pro-
sD

gramming languages. This is done so that the Prolog programmer is free to use the
“<=” atom, which looks like an arrow, for other purposes.
As these comparison operators are predicates, one might think it possible to
write a Prolog fact as follows,
al

2 > 3.
in order to assert that 2 is actually greater than 3. A fact like this one is perfectly well-
ri

formed Prolog. However, Prolog will not allow further facts to be added to predicates
that are “built in” to Prolog. This prevents you from changing the meaning of built-in
to

predicates in unexpected ways. In Chapter 6 we shall describe many of the built-in


predicates, including all those we have met thus far.
As a first example of using numbers, suppose we have a database of the reigns
Tu

of the Sovereign Princes of Wales in the 9th and 10th Centuries. The predicate reigns
is defined such that the goal reigns(X,Y,Z) is true if the prince named X reigned from
year Y to year Z. The list of facts in the database looks like this:

reigns(rhodri, 844, 878).


reigns(anarawd, 878, 916).
reigns(hywel_dda, 916, 950).
reigns(lago_ap_idwal, 950, 979).

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

2.5 Arithmetic 35

reigns(hywel_ap_ieuaf, 979, 985).


reigns(cadwallon, 985, 986).
reigns(maredudd, 986, 999).

Now suppose we want to ask who was on the Welsh throne during a particular year.
We could define a rule, which given a name and a date, would search the database,
and compare the given date aginst the dates of the reign. Let us define the predicate

m
prince(X,Y), which is true if the prince named X was on the throne during year Y:

X was a prince during year Y if:

co
X reigned between years A and B, and
Y is between A and B, inclusive.

a.
Now the first goal will be satisfied by using the reigns database above. The second
goal is satisfied if Y is equal to A, or Y is equal to B, or Y lies between A and B. You
can test for this by testing if Y >= A and Y =< B. Translating all this into Prolog, we
obtain:
iy
un
prince(X, Y) :-
reigns(X, A, B),
Y >= A,
Y =< B.
sD

Here are some questions one might ask, with the answers that Prolog gives:

?- prince(cadwallon, 986).
al

yes
?- prince(rhodri, 1979).
no
ri

?- prince(X, 900).
X=anarawd
to

yes
?- prince(X, 979).
X=lago_ap_idwal ;
Tu

X=hywel_ap_ieuaf
yes

Notice the use of variables in the latter examples. Make sure you know how Prolog’s
searching mechanism allows questions like these to be answered.
Arithmetic can also be used for calculating. For example, if we know the pop-
ulation and ground area of a country, we can calculate the population density of the
country. The population density tells us how crowded the country would be if all the

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

36 Chapter 2 A Closer Look

people were evenly spread throughout the country. Consider the following database
about the population and area of various countries in 1976. We will use the predicate
pop to represent the relationship between a country and its population. Nowadays,
the populations of countries are generally quite large numbers. So, we will represent
population figures in millions: pop(X, Y) means “the population of country X is about
Y million people”. The predicate area will denote the relationship between a country
and its area (in millions of square miles). The numbers given here are not exact, but

m
they will do for the purpose of demonstrating arithmetic:

pop(usa, 203).

co
pop(india, 548).
pop(china, 800).
pop(brazil, 108).

a.
area(usa, 3).
area(india, 1).
area(china, 4).
area(brazil, 3). iy
un
Now to find the population density of a country, we must use the rule that the density
is the population divided by the area. This can be represented as the predicate density,
where the goal density(X,Y) succeeds for country X having Y as the population density
sD

of that country. A Prolog rule for this is:

density(X, Y) :-
pop(X, P),
al

area(X, A),
Y is P / A.
ri

The rule is read as follows:


to

The population density of country X is Y, if:


The population of X is P, and
The area of X is A, and
Tu

Y is calculated by dividing P by A.
The “is” operator is new. The “is” operator is an infix operator. Its right-hand
argument is a term which is interpreted as an arithmetic expression. To satisfy an “is”,
Prolog first evaluates its right-hand argument according to the rules of arithmetic.
The answer is unified with the left-hand argument to determine whether the goal
succeeds. In the above example, Y is unknown when the “is” is encountered, and it
is up to the “is” to evaluate the expression, and let Y stand for the value. This means
that the values of all the variables on the right of an “is” must be known.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

2.6 Summary of Satisfying Goals 37

We need to use the “is” predicate any time we require to evaluate an arithmetic
expression. Remember that something like P/A is just an ordinary Prolog structure
having the same “shape” as a structure like author(emily, bronte). But if we interpret
a structure as an arithmetic expression, there is a special operation that can be applied
to the structure: that of actually carrying out the bits of arithmetic and calculating the
result. This is called evaluating the arithmetic expression. Not all structures can be
evaluated as arithmetic expressions. Clearly we cannot evaluate structures such as

m
the author one, because author is not defined here as an arithmetic operation.
Getting back to the population density example, it is not hard now to see that
typical questions and their answers are:

co
?- density(china, X).
X=200

a.
yes
?- density(turkey, X).
no

iy
In the first question, the X=200 is Prolog’s answer, meaning 200 people per square
un
mile. The second question failed, because the population of Turkey could not be
found in our example database.
Depending on what computer you use, various arithmetic operators can be used
on the right-hand side of the “is” operator. All Standard Prolog systems, however,
sD

will have the following, as well as many more:


X +Y the sum of X and Y
X -Y the difference of X and Y
al

X *Y the product of X and Y


X /Y the quotient of X divided by Y
X // Y the integer quotient of X divided by Y
ri

X mod Y the remainder of X divided by Y


This list together with the above list of comparison operators should tell you nearly
to

all you need for doing simple arithmetic problems.


Tu

2.6 Summary of Satisfying Goals

Prolog performs a task in response to a question from the programmer (you). A ques-
tion provides a conjunction of goals to be satisfied. Prolog uses the known clauses to
satisfy the goals. A fact can cause a goal to be satisfied immediately, whereas a rule
can only reduce the task to that of satisfying a conjunction of subgoals. However, a
clause can only be used if it unifies the goal under consideration. If a goal cannot be

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

38 Chapter 2 A Closer Look

satisfied, backtracking will be initiated. Backtracking consists of reviewing what has


been done, attempting to re-satisfy the goals by finding an alternative way to satisfy-
ing them. Furthermore, if you are not content with an answer to your question, you
can initiate backtracking yourself by typing a semicolon when Prolog informs you
of a solution. In this section, we present a diagrammatic notation for showing how
and when Prolog attempts to satisfy and re-satisfy goals.

m
2.6.1 Successful satisfaction of a conjunction of goals

co
Prolog attempts to satisfy the goals in a conjunction, whether they appear in a rule
body or in a question, in the order they are written (left to right). This means that
Prolog will not attempt to satisfy a goal until its neighbour on the left has been

a.
satisfied. And, when it has been satisfied, Prolog will attempt to satisfy its neighbour
on the right. Consider the following simple program about family relations:

female(mary).
iy
parent(C, M, F) :- mother(C, M), father(C, F).
un
mother(john, ann).
mother(mary, ann).
sD

father(mary, fred).
father(john, fred).

Let us look at the sequence of events that leads to answering the question:
al

?- female(mary), parent(mary, M, F), parent(john, M, F).


This question is to find whether mary is a sister of john. To do this Prolog needs to
ri

satisfy the following sequence of subgoals shown in Figure 2.1.


We represent goals as boxes distributed down the page. An arrow starting from the
to

top of the page indicates which goals have already been satisfied. Boxes that lie
below the arrowhead represent goals that Prolog has not yet considered. Boxes that
Tu

the arrow has passed through indicate goals that have been satisfied. As a program
runs, the arrow moves up and down the page as Prolog turns its attention to the
various goals. We call this the flow of satisfaction. In the example, the arrow starts at
the top of the page, as shown above. It will extend downwards, moving through the
three boxes as the three goals are satisfied. So the final situation will be as shown in
Figure 2.2.
Notice that values have now been found for the variables M and F. This diagram
shows the coarse structure of what has happened, but it fails to show how these three

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

2.6 Summary of Satisfying Goals 39

female(mary)

om
parent(mary, M, F)

c
parent(john, M, F)

a.
Fig. 2.1. A sequence of subgoals not yet satisfied

iy
un
female(mary)
sD

parent(mary, ann, fred)


al
ri

parent(john, ann, fred)


to

Fig. 2.2. The sequence of subgoals has been satisfied. Note that variables have been instanti-
ated
Tu

goals were satisfied. We can show this by putting more detail inside the boxes. Let us
concentrate on how the second goal is satisfied. Satisfying a goal involves searching
the database for a unifying clause, then marking the place in the database, and then
satisfying any subgoals.
We can show this for the second goal by indicating in the parent box which
clause was chosen and which subgoals had to be satisfied. The clause chosen is
shown by a number in brackets, here (1). This number indicates which clause out

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

40 Chapter 2 A Closer Look

of the set of clauses for the appropriate predicate has been chosen. So the number
1 indicates that the first clause for the predicate has been chosen. This is enough
information to mark the place in the database. The subgoals are shown in small boxes
inside the box for the goal. At the point when the parent clause has been chosen, the
situation looks like Figure 2.3.

m
female(mary)

co
a.
parent(mary, ann, fred)
(1)

iy
un
mother(mary, M)
sD

father(mary, F)
al
ri

parent(john, M, F)
to

Fig. 2.3. The number (1) indicates that the first clause for the predicate has been chosen. The
Tu

subgoals are shown in small boxes inside the box for the goal.

The arrow has entered the parent box and passed through the brackets indicating
that a clause has been chosen. The clause has introduced two subgoals, involving
mother and father, shown as the small boxes inside the goal box. At this point, the
arrow must pass through these two smaller boxes, emerge from the current parent
box and then pass through the second parent box in order for the question to succeed.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions e

2.6 Summary of Satisfying Goals 41

When the arrow passes through the smaller boxes, the same steps of choosing a
clause and satisfying the clause’s subgoals must be performed. In this example, both
of these goals succeed by finding facts in the database, which give instantiations for
variables M and F. So in Figure 2.4 we see a more detailed picture of the situation
when the question succeeds.

m
co
female(mary)

a.
parent(mary, ann, fred)
(1)

iy
un
mother(mary, M)
(2)
sD

father(mary, F)
al

(1)
ri
to

parent(john, ann, fred)


Tu

Fig. 2.4. The question has succeeded

Note that to be precise we should have shown the details of how the goals fe-
male(mary) and parent(john, ann, fred) were satisfied. However, this would have
been too much detail to fit onto one page. This example shows the general pattern of
how Prolog attempts to satisfy goals in a case where the conjunction of goals suc-

Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions e
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

42 Chapter 2 A Closer Look

ceeds. The arrow moves down the page, passing through the boxes in turn. When it
enters a box, a clause is chosen and its position marked. If the clause unifies with the
goal and the clause is a fact, then the arrow can leave the box. This happened for the
mother and father goals. On the other hand, if the clause unifies with the goal and
the clause is a rule then new boxes are created for the subgoals and the arrow must
then pass through all of these before it can leave the original box.

m
2.6.2 Consideration of goals in backtracking
When a failure is generated (because all the alternative clauses for a goal have been

co
tried, or because you type a semicolon), the “flow of satisfaction” passes back along
the way it has come. This involves retreating back into boxes that have previously
been left in order to re-satisfy the goals. When the arrow gets back to a place where

a.
a clause was chosen (represented by a number in brackets), Prolog attempts to find
an alternative clause for the appropriate goal. First, it makes uninstantiated all vari-

iy
ables that had been instantiated in the course of satisfying the goal. Then, it searches
through the database from where the place-marker was put. If it finds another unify-
ing possibility, it marks the place, and things continue as in Section 2.6.1 above.
un
Note that work on any goals “below” this (even if such goals were tackled under
the previous alternative) will always start from scratch. Prolog will try to satisfy, and
not to re-satisfy them. If no other unifying possibility can be found, the goal fails,
sD

and the arrow retreats further until it comes to another place-marker.


In our example, if the goal parent(john, ann, fred) failed, the arrow would
retreat upwards from the parent(john, ann, fred) box. Then it is necessary to re-
enter the big parent(mary, ann, fred) box from below, to attempt to re-satisfy this
al

goal, as shown in Figure 2.5.


Now after this point, it is necessary to retreat further. The arrow needs to reach the
ri

place where the clause for the father goal was chosen. First of all, all variables that
became instantiated as the result of using this clause are set back to uninstantiated.
This means that F in the father goal becomes uninstantiated again. Then Prolog looks
to

through the database, starting after the first father clause (the one marked), trying to
find an alternative clause for this goal. Assuming that mary has only one father (a
Tu

not unreasonable assumption), this will not succeed. So the arrow will have to retreat
further. It retreats upwards, out of the father(mary, F) box (this goal has failed) and
back into the mother(mary, ann) box (to attempt to re-satisfy this goal). We get the
situation shown in Figure 2.6.
We can see from these examples the general pattern of how goals are reconsidered
in backtracking. When a goal fails, the arrow retreats upwards out of the box for the
failing goal and back into the box for the goal above. The arrow continues retreating
until it reaches a place marker. All variables that were instantiated as a result of

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

2.6 Summary of Satisfying Goals 43

female(mary)

parent(mary, ann, fred)

om
(1)

c
mother(mary, ann)
(2)

a.
iy
father(mary, fred)
(1)
un
sD

parent(john, ann, fred)

Fig. 2.5. What happens if a goal fails


al

the previous choice of clause are reset to uninstantiated. Then Prolog searches the
database for a clause after the place marker. If it finds a clause that unifies with
ri

the goal, then a new place mark is recorded, boxes for the subgoals are created and
the arrow starts moving downwards again. Otherwise, the arrow continues to retreat
to

upwards, in search of another place marker.


Tu

2.6.3 Unification

The rules for deciding whether a goal unifies with the head of a use of a clause are
as follows. Note that in the use of a clause, all variables are initially uninstantiated.

An uninstantiated variable will unify with any object. As a result, that object will
be what the variable stands for.
Otherwise, an integer or atom will unify with only itself.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions e

44 Chapter 2 A Closer Look

female(mary)

parent(mary, ann, F)

m
(1)

co
mother(mary, ann)

a.
(2)

iy
father(mary, F)
un
sD

parent(john, ann, F)
al

Fig. 2.6. Attempting to re-satisfy a goal

Otherwise, a structure will unify with another structure with the same functor
ri

and number of arguments, and all the corresponding arguments must unify.

A noteworthy case in unification is one in which two uninstantiated variables are


to

unified. In this case, we say that these variables share (or co-refer). Two sharing
variables are such that as soon as one is instantiated, so is the other (with the same
Tu

value). If you have noticed a similarity between unification and making arguments
equal (Section 2.4), then you are correct. This is because the “=” predicate attempts
to make its arguments equal by unifying them. Now we can bring together what we
have discussed about operators, arithmetic, and unification. Suppose the following
facts are in the database:
sum(5).
sum(3).
sum(X + Y).

Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions e
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

2.6 Summary of Satisfying Goals 45

Consider the question


?- sum(2 + 3).
Now, which one of the facts above will unify with the question? If you think it is
the first one, then you should go back and read about structures and operators. In the
question the argument of the sum structure is a structure having the plus sign as its
functor, and having the 2 and 3 as its components. In fact, the goal shown will unify

m
with the third fact, instantiating X to 2, and Y to 3. On the other hand, if we actually
wanted to compute a sum, we would use the “is” predicate. We would write

co
?- X is 2 + 3.
or, just for fun, we could define a predicate add that relates two integers with their
sum:

a.
add(X, Y, Z) :- Z is X + Y.

iy
In this definition, X and Y must be instantiated.
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

Using Data Structures

m
co
a.
The Oxford English Dictionary defines the word “recursion” in the following way:
RECURSION [Now

iy
rare or obs. 1626]. A backward movement, return.

This definition is cryptic and perhaps outdated. Recursion is now a very popular
un
and powerful technique in the world of non-numerical programming. The idea of
recursion is used in two ways. It can be used to describe structures that have other
structures as components. It can also be used to describe programs that need to satisfy
a copy of themselves before they themselves can succeed. Sometimes, beginners
sD

view recursion with some suspicion, because, how is it possible for some relationship
to be defined in terms of itself? In Prolog, recursion is the normal and natural way
of viewing data structures and programs. We hope that the theme of this chapter,
recursion, will be made explicit in a comfortable and unobtrusive way.
al
ri

3.1 Structures and Trees


to

It is usually easier to understand the form of a complicated structure if we write it


as a tree, in which each functor is a node, and the components are branches. Each
branch may point to another structure, so we can have structures within structures. It
Tu

is customary to write a tree diagram with the root at the top, and the branches at the
bottom. For instance, the structure parents(charles, elizabeth, philip) is written as:

parents

charles elizabeth philip

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

48 Chapter 3 Using Data Structures

The structure a+b*c (or equivalently, +(a, *(b,c))) is written as:

a *

m
b c

co
The structure book(moby_dick, author(herman, melville)) is written as:

book

a.
moby_dick author

herman iy melville
un
Notice that the last two structures have trees of the same shape, although the roots
sD

and leaves are different. Before going further, you should make sure that you can
write tree diagrams for each of the structures you have seen in the previous chapters.
Suppose we are given the sentence “John likes Mary”, and we need to repre-
sent the syntax of the sentence. A very simple syntax for English is that a sentence
al

consists of a noun followed by a verb phrase. Additionally, a verb phrase consists of


a verb and another noun. We can represent the structure of any such sentence by a
structure of the form:
ri

sentence(noun(X), verb_phrase(verb(Y), noun(Z)))


to

which has a tree like this:


Tu

If we take our sentence (“John likes Mary”), and instantiate the variables in the struc-
ture with the words of the sentence, we obtain:

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.1 Structures and Trees 49

sentence

noun verb phrase

X verb noun

m
Y Z

sentence

co
noun verb phrase

a.
john verb noun

likes
iy mary
un
This shows how we can use Prolog structures and variables to represent the syntax of
a class of very simple English sentences. In general, if we know the parts of speech
of words in a sentence, it is possible to write a Prolog structure that makes explicit
sD

the relationships between different words in a sentence. This is an interesting topic


in its own right, and later on we shall return to the question of how we can use Prolog
to make the computer “understand” some simple English sentences.
al

Trees can also give a graphic description of variables inside structures, par-
ticularly showing how like-named variables share. For example, we can depict the
structure of the term f(X, g(X, a)) by the following tree (more precisely, a directed
ri

acyclic graph):
to

f
Tu

X a

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

50 Chapter 3 Using Data Structures

3.2 Lists

The list is a very common data structure in non-numeric programming. The list is
an ordered sequence of elements that can have a any length. “Ordered” means that
the order of the elements in the sequence matters. The “elements” of a list may be
any terms — constants, variables, structures — which of course includes other lists.
These properties are helpful when we cannot predict in advance how big a list should

om
be, and what information it should contain. Furthermore, lists can represent practi-
cally any kind of structure that one may wish to use in symbolic computation. Lists
are widely used to represent parse trees, grammars, city maps, computer programs,
and mathematical entities such as graphs, formulae, and functions. There is a pro-

.c
gramming language called LISP, in which the only data structures available are the
constant and the list. However, in Prolog, the list is simply one particular kind of

ya
structure.
Lists can be represented as a special kind of tree. A list is either an empty list,
having no elements, or it is a structure that has two components: the head and tail.
The end of a list is customarily represented as a tail that is set to the empty list. The
i
empty list is written as [], which is an opening square bracket followed by a closing
un
square bracket. The head and tail of a list are components of the functor named
“.”, which is the dot (called the period or full stop). Thus, the list consisting of one
element “a” is “.(a,[])”, and its tree looks like this:
sD

a []
al

Also, the list consisting of the atoms a, b and c is written .(a,.(b,.(c,[]))), and its tree
ri

looks like this:

.
to

a .
Tu

b .

c []

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.2 Lists 51

Some people like to write the tree diagram of a list with the tree “growing” from left
to right, and with the “branches” hanging down. The above list looks like this in such
a “vine” diagram:

. . . []

om
a b c

In this vine diagram, the head component of the dot functor hangs down, and the tail
component grows to the right. The end of the list is clearly marked by the last tail

c
component being the empty list. The main advantage of the vine diagram for lists is

a.
that it can be written right-to-left on a piece of paper.
The vine diagram may be suitable for writing lists on paper when we need to see
the structure of a list, but such diagrams are not used for writing lists into a Prolog

iy
program. As the dot notation is often awkward for writing complicated lists, there
is another syntax that can be used for writing lists in a Prolog program. This list
un
notation consists of the elements of the list separated by commas, and the whole list
is enclosed in square brackets. For example, the above lists can be written in the list
notation as [a] and [a,b,c].
It is useful for lists to contain other lists and variables. For example, the follow-
sD

ing lists are legal in Prolog:

[]
[the, men, [like, to, fish]]
al

[a, V1, b, [X, Y]]


ri

Variables within lists are treated the same as variables in any other structure. They
can become instantiated at any time, so judicious use of variables can provide a way
to

to put “holes” in lists that can be filled with data at a later time. To show the structure
of lists within lists, the vine diagram for the previous list is:
Tu

. . . . []

a V1 b . . []

X Y

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

52 Chapter 3 Using Data Structures

It is easy to see from this diagram that each horizontal “level” of the vine is a list
having a certain number of elements. The top level is a list having four elements, one
of which is a list. The second level, having two elements, is the fourth element of the
top level list.
Lists are manipulated by splitting them up into a head and a tail. The head of a
list is the first component of the “.” functor that is used to construct lists. Notice that
we speak of the “head” of a rule as well as the “head” of a list. These two things are

m
different, and although they are both called “heads” by historical accident, it is easy
enough to understand which “head” one is talking about at a particular time. The tail
of a list is the second component of the “.” functor. When a list appears in the square

co
bracket notation, the head of the list is the first element of the list. The tail of the list
is a list that consists of every element except the first. Here are some lists with their
heads and tails:

a.
List Head Tail

iy [a, b, c]
[]
a
(none)
[b, c]
(none)
un
[[the, cat], sat] [the, cat] [sat]
[the, [cat, sat]] the [[cat, sat]]
[the, [cat, sat], down] the [[cat, sat], down]
[X+Y, x+y] X+Y [x+y]
sD

Table 3.1. Some lists with their head and tail

Notice that the empty list has neither a head nor a tail. In the last example, the “+”
al

operator is used as a functor for the structures +(X, Y) and +(x ,y).
Because a common operation with lists is to split a list into its head and tail,
ri

there is a special notation in Prolog to represent “the list with head X and tail Y”.
This is written [X|Y], where the symbol separating the X and Y is the vertical bar. Be
careful not to confuse the vertical bar with the digit “1”, the letter “I” and the letter
to

“l”. A pattern of the form [X|Y] will instantiate X to the head of a list, and Y to the tail
of the list, as in the following example:
Tu

p([1,2,3]).
p([the, cat, sat, [on, the, mat]]).
?- p([X|Y]).
X=1 Y = [2, 3] ;
X = the [Y = cat, sat, [on, the, mat]]
?- p([_,_,_,[_|X]]).
X = [the, mat]

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.3 Recursive Search 53

More examples of the list syntax, showing how various lists match, are as follows,
in which we attempt to match the two lists shown, obtaining the instantiations (if
possible), shown in Table 3.2.

List 1 List 2 Instantiations

[X, Y, Z] [john, likes, fish] X = john

m
Y = likes
Z = fish
[cat] [X|Y] X = cat

co
Y = []
[X, Y|Z] [mary, likes, wine] X = mary
Y = likes

a.
Z = [wine]
[[the, Y]|Z] [[X, hare], [is, here]] X = the
Y = hare

iy [golden|T]
[vale, horse]
[golden, norfolk]
[horse, X]
Z = [[is,here]]
T = [norfolk]
(none)
un
[white|Q] [P|horse] P = white
Q = horse
Table 3.2. Pairs of lists and how they match. If they match, variable instantiations are shown.
One example does not match.
sD

As the last example shows, it is possible to use the list notation to create structures
that resemble lists, but which do not terminate with the empty list. One such struc-
al

ture, [white|horse], denotes a structure having head white and tail horse. The con-
stant horse is neither a list nor the empty list, and we shall later see that such struc-
ri

tures should be treated carefully when used at the tail of a list.


to

3.3 Recursive Search


Tu

We frequently need to search inside a Prolog structure to find some desired piece of
information. When the structure may have
a other structures as its components, this
results in a recursive search task.
Suppose, for example, we have a list of the names of those horses sired by
Coriander who all won horse races in Great Britain in the year 1927:
[curragh_tip, music_star, park_mill, portland]

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

54 Chapter 3 Using Data Structures

Now suppose we want to find out if a given horse is in the list. The way we do this
in Prolog is to find out whether the horse is the same as the head of the list: if it is,
we succeed. If it is not, then we check to see if the horse is in the tail of the list. This
means checking the head of the tail next time. And the head of that tail after that. If
we come to the end of the list, which will be the empty list, we must fail: the horse
is not in the list.
To write this in Prolog, we must first recognise that there is a relationship be-

m
tween an object, and a list it might appear in. This relationship, called membership, is
a common enough concept in our everyday lives. We talk about people being mem-
bers of clubs, and so forth. We shall write a predicate member such that the goal

co
member(X,Y) is true if the term that X stands for is a member of the list that Y stands
for. There are two conditions to check. First, it is a fact that X will be a member of Y,
if X is the same as the head of Y. Although you might be tempted to check whether X

a.
and Y are the same by writing X=Y as a subgoal, it is easier simply to check whether
they match by writing them using the same variable name. In Prolog, this fact is:
member(X, [X|_]).
iy
which represents, “X is a member of the list that has X as its head”. Notice that we
un
use the anonymous variable “ ” to stand for the tail of the list. This is because we
do not use the tail for anything in this particular fact. Notice that this rule also could
have been written as:
sD

member(X, [Y|_]) :- X = Y.
By this time you should understand why we can take a shortcut by using X in two
places in the shorter version of the rule.
al

The second, and last, rule says that X is a member of a list providing it is in the
tail, say Y, of that list. And, what better way to find out if X is in the tail of the list,
than to use member itself! This is the essence of recursion. In Prolog:
ri

member(X, [_|Y]) :- member(X, Y).


to

which represents, “X is a member of the list if X is a member of the tail of the list”.
Notice that we have used the anonymous variable “ ”, because we do not care to have
any named variable standing for the head of the list. The two rules together define
Tu

the membership predicate, and they tell Prolog how to search a list from beginning
to end, looking for an item in the list.
The most important point to remember, when encountering a recursively defined
predicate, is to look for the boundary conditions and the recursive case. There are
actually two boundary conditions for the member predicate. Either the object we are
looking for is in the list, or it isn’t in the list. The first boundary condition of member
is recognised by the first clause, which will cause the search through the list to be
stopped if the first argument of member matches the head of the second argument.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.3 Recursive Search 55

The second boundary condition occurs when the second argument of member is the
empty list.
How are we assured that the boundary conditions will ever be satisfied? We
must look at the recursive case, the second rule of member. Notice that each time
member attempts to satisfy itself, the goal is given a shorter list. The tail of a list is
always a shorter list than the original one. Eventually, one of two things will happen:
either the first member rule will match, or member will be given a list of length

m
0, the empty list, as its second argument. When either of these things happens, the
“recurrence” of member goals will come to an end. The first boundary condition is
recognised by a fact, which does not cause any further subgoals to be considered.

co
The second boundary condition is not recognised by any member clause, so member
will fail. In Prolog:
member(X, [X|_]).

a.
member(X, [_|Y]) :- member(X, Y).
?- member(d, [a, b, c, d, e, f, g]).
yes
?- member(2, [3, a, 4, f]). iy
un
no
Suppose we asked the question
?- member(clygate,[curragh_tip,music_star,park_mill,portland]).
sD

The second member rule would match, as clygate does not match curragh_tip. Vari-
able Y becomes instantiated to [music_star, park_mill, portland], and the next goal
is to see if clygate is a member of that. The second rule matches again, and the tail is
al

taken again. The goal becomes member(clygate ,[park_mill, portland]). The process
recurs until we reach the goal where X is clygate, and Y is [portland]. The second rule
matches once more, and now Y becomes the tail of [portland], which is the empty
ri

list, and the next goal becomes member(clygate, []). No rule in the database matches
this, so the goal fails, and the question is false.
to

It is most important to remember that each time that member uses its second
clause to attempt to satisfy member, Prolog treats each recurrence of the member
goal as a different “copy”. This prevents the variables in one use of a clause from
Tu

being confused with variables in another use of a clause.


As the membership predicate is so useful, we shall use it in many places in the
remainder of this book. The member predicate is also important because it is one
of the smallest useful examples of a predicate that is recursive: that is, the defini-
tion of member contains goals that can only be satisfied by member itself. Recursive
definitions are frequently found in Prolog programs, and they are no different than
any other type of definition. However, you must be careful that you do not write
“circular” definitions, for example:

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

56 Chapter 3 Using Data Structures

parent(X, Y) :- child(Y, X).


child(A, B) :- parent(B, A).
In this example, to satisfy parent, we set up child as a goal. However, the definition
for child uses only parent as a goal. You should be able to see that asking a question
about parent or child would lead to a loop in which Prolog would never infer anything
new, and that the loop would never terminate.
One important problem to look out for in recursive definitions is that of left

m
recursion. This arises when a rule causes the invocation of a goal that is essentially
equivalent to the original goal that caused the rule to be used. Thus if we defined:

co
person(adam).
person(X) :- person(Y), mother(X, Y).

and asked

a.
?- person(X).

iy
Prolog would first use the rule, and generate the subgoal person(Y). In trying to
satisfy this, it would again pick the rule first, and generate yet another equivalent
goal. And so it would go on and on, until it ran out of memory space. Of course, if
un
it had a chance to backtrack, it would find the fact about Adam and start producing
solutions. The trouble is, that in order to backtrack, Prolog has to have failed after
trying the first possibility. In this case, the task that it finds is infinitely long, and so
sD

it never gets a chance to succeed or fail. So the moral is:

Don’t assume that, just because you have provided all the relevant
facts and rules, Prolog will always find them. You must bear in mind
al

when you write Prolog programs how Prolog searches through the
database and which variables will be instantiated when one of your
ri

rules is used.
In this example, the simple solution is just to put the fact before the rule, instead of
to

after it:

person(X) :- person(Y), mother(X, Y).


person(adam).
Tu

In fact, as a general heuristic, it is a good idea to put facts before rules whenever pos-
sible. Sometimes putting the rules in a particular order will work if they are used to
solve goals of one form but will not if goals of another form are generated. Consider
the following definition of islist, in which the goal islist(X) succeeds if X is a list in
which the tail of its last element is the empty list:
islist([A|B]) :- islist(B).
islist([]).

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.4 Mapping 57

If we use these rules to answer questions like:


?- islist([a, b, c, d]).
or
?- islist([]).
or

m
?- islist(f(1, 2, 3)).
then the definition will work fine. But when we ask:

co
?- islist(X).
the program will loop. A predicate similar to islist that is not susceptible to loops is

a.
provided by the following two facts:
weak_islist([]).

iy weak_islist([_|_]).
This version just tests the first part of the list, rather than checking whether the last
un
tail is []. This is not as strong a test as islist, but it will not loop if the argument is a
variable.
sD

3.4 Mapping

Given a Prolog structure, we frequently wish to construct a new structure that is simi-
al

lar to the old one but changed in some way. We traverse the old structure component-
by-component, and construct the components of the new structure. We call this map-
ri

ping.
For example, let us consider a Prolog program in which we type an English sentence
to

and Prolog replies with another sentence that is an altered version of the one we typed
in. This program for “talking back” to the programmer might produce a dialogue like
this:
Tu

you are a computer


i am not a computer
do you speak french
no i speak german
Although this dialogue may seem like a forced but sensible conversation, it is very
easy to write a computer program to carry out its “part” of the dialogue simply by
following these steps:

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com
for Notes, books,programs, question paperswith solutions et

58 Chapter 3 Using Data Structures

1. Accept a sentence that is typed in by the user.


2. Change each you in the sentence to the word “i”.
3. Likewise, change any are to am not.
4. Change french to german.
5. Change do to no.

m
When applied to carefully chosen sentences, such as those in the above dialogue, this
scheme will produce a sensible altered sentence. However, it does not work on every

co
sentence, for example:
i do like you

a.
i no like i
Once a simple program is written, it can be modified later to cope with sentences that
produce awkward output.

iy
A Prolog program to change one sentence into another can be written as follows.
First, we need to recognise that there is a relationship between the original sentence
and the altered sentence. So, we need to define a Prolog predicate, called alter, such
un
that alter(X,Y) means that sentence X can be altered to give sentence Y. It is convenient
for X and Y to be lists, with atoms standing for the words, so sentences can be written
like this:
sD

[this, is, a, sentence]


and once alter is defined, we could ask Prolog a question of the form
al

?- alter([do,you,know,french], X).
and Prolog would reply
ri

X=[no,i,know,german].
Don’t be concerned yet that the input and output sentences are not tidy, and do not
to

look like normal sentences. In later chapters we will discuss ways of typing in and
printing out structures in a way that is easy to read. For the moment we will only
worry about changing one list into another.
Tu

Because alter deals with lists, the first fact about alter needs to deal with what
happens if the list is empty. In this case, we will say that an empty list is altered into
an empty list:
alter([],[]).
Or, in words, “it is a fact that altering the empty list gives the empty list”. If the
reason for treating the empty list is not apparent now, it should be clearer later. Next,
we need to recognise that the main job of alter is to:

Visit https://www.tutorialsduniya.com
for Notes, books,programs, question paperswith solutions et
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.4 Mapping 59

1. Change the head of the input list into another word, and let the head of the output
list stand for that word.
2. Use alter on the tail of the input list, and let the tail of the output list stand for
the altered tail.
3. If we have reached the end of the input list, then there is nothing more to go onto
the output list, so we can terminate the output list with an empty list [].

m
Translated into words that are closer to Prolog:

co
Altering a list with head H and tail T gives a list with head X and tail Y if:
changing word H gives word X, and
altering the list T gives the list Y.

a.
Now we need to say what is meant by “changing” one word into another. This can
be done by having a database of facts in which change(X, Y) means word X can be

iy
changed into word Y. At the end of the database we need a “catchall” fact, because
if a word is not changed into another word it needs to be changed into itself. If the
un
reason for a catchall is not apparent now, it should be clearer after we explain how
the program works. The relevant catchall fact is change(X,X), which means word X is
changed into itself. A database to handle the changes listed above is:
change(you, i).
sD

change(are, [am,not]).
change(french, german).
change(do, no).
al

change(X, X). /* this is the "catchall" */


Notice that we have treated the phrase “am not” as a list, so that it occupies only one
ri

argument of the fact.


Now we can translate the pseudo-Prolog text above into pure Prolog, remem-
bering the notation [A|B] for the list with head A and tail B. We get something like
to

this:

alter([], []).
Tu

alter([H|T], [X|Y]) :- change(H, X), alter(T, Y).

The first clause in this procedure checks for an empty list. The same clause also
checks for the end of the list as well. Why? Consider this worked example:
?- alter([you, are, a, computer], Z).
This question would match with the main alter rule, making variable H stand for you,
and the variable T stand for [are, a, computer]. Next, the goal change(you, X) would

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

60 Chapter 3 Using Data Structures

succeed, setting X to stand for the word “i”. As X is the head of the output list (in
the goal of alter), the first word in the output list is “i”. Next, the goal alter([are, a,
computer], Y) would use the same rule. The word are is changed into the list [am,
not] by the database, and another alter goal is generated:
alter([a, computer], Y).
The fact change(a, X) is searched for, but as there is no change fact with “a” as its

m
first argument, the catchall fact at the bottom of the database succeeds, changing “a”
into “a”. The alter rule is called for once again, with computer as the head of the input
list, and the empty list [] as the tail of the input list. As previously, change(computer,

co
X) matches against the catchall. Finally, alter is called with the empty list, which
matches against the very first alter clause. The result is the empty list, which ends
the sentence (remember that a list ends with an empty tail). Finally, Prolog answers

a.
the question by responding
Z = [i,[am,not],a,computer]

iy
Notice that the phrase [am, not] appears in the list exactly as it was inserted. This is
an example of a list that is a member of another list.
un
The reasons for adding the fact alter([], []) and the catchall fact change(X,
X) should now be clear. Facts like these are often included in a program when it is
desirable to check for boundary conditions. It should be clear from the explanation
above that boundary conditions occur when the input list becomes the empty list, and
sD

when all of the change facts have been searched through. In both of these boundary
conditions, we wish certain actions to be performed. When the input list becomes the
empty list, we wish to terminate the output list (by putting an empty list at its end).
al

When all the change facts have been searched through without the given word being
changed into another word, we wish to keep the word unchanged (by changing it into
itself).
ri
to

3.5 Recursive Comparison


Tu

As we saw in Chapter 2, Prolog provides predicates to compare integers. Comparing


structures is generally more complicated, because it is necessary to consider all the
individual components. When the components may themselves be structures, the
comparison may have to be recursive. This happens, for instance, when we wish to
compare lists that may have any length.
Imagine that you are trying to assess the relative fuel economy of a set of differ-
ent cars. To do this, you drive the cars around particular routes and measure the fuel
consumption. With each car, you associate a list of numbers that records the number

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.5 Recursive Comparison 61

of litres of fuel consumed on the different routes. Of course, to compare two given
cars you have to look at the same sequence of routes and it only makes sense to com-
pare consumption on the same route. So we can assume that the lists will all have
the same length and that they will be ordered in the same way, so that the first route
comes first, the second route second, etc. We can define the predicate fuel_consumed
so that the goal fuel_consumed(C,R) succeeds for a model of car C and a list of routes
R. So we may have something like:

m
fuel_consumed(waster, [3.1, 10.4, 15.9, 10.3]).
fuel_consumed(guzzler, [3.2, 9.9, 13.0, 11.6]).

co
fuel_consumed(prodigal, [2.8, 9.8, 13.1, 10.4]).
Here we have four test routes. If we tested the cars on more routes, we could make
the lists longer. We would like to compare these cars in a way that works regardless

a.
of the exact length of the lists.
First of all, we have to decide when to count one amount of fuel consumed as

iy
better than another. Since there is a certain randomness in how much fuel is needed
for a journey, we will take a generous approach which says that one consumption is
“equal or better” than another if it is less than 5 percent more than the average of the
un
two. In Prolog, we can use arithmetic to calculate the threshold (1/20 of the average,
which is the same as 1/40 of the sum) and the number that the better consumption
must fall below; the actual comparison can be made using <.
sD

equal_or_better_consumption(Good, Bad) :-
Threshold is (Good + Bad) / 40,
Worst is Bad + Threshold,
Good < Worst.
al

Thus we have:
?- equal_or_better_consumption(10.5, 10.7).
ri

yes
?- equal_or_better_consumption(10.7, 10.5).
to

yes
?- equal_or_better_consumption(10.1, 10.7).
no
Tu

?- equal_or_better_consumption(10.7, 10.1).
yes
With this in place, we can start to say when one car, Car1, is preferred to another,
Car2:
prefer(Car1, Car2) :-
fuel_consumed(Car1, Con1),
fuel_consumed(Car2, Con2),

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

62 Chapter 3 Using Data Structures

always_better(Con1, Con2).
The remaining thing is to define always_better, which tests whether one
list of consumptions is better than another. We would expect this to call
equal_or_better_consumption to test how the elements of those lists compare, some-
how. As is suggested by its name, always_better is given two lists of consumptions
and succeeds if each element in the first list is “equal or better” than the correspond-

m
ing element of the second. Here is how it could be defined:

always_better([], []).
always_better([Con1|T1], [Con2|T2]) :-

co
equal_or_better_consumption(Con1, Con2),
always_better(T1, T2).

a.
Consider the recursive clause first. It says that one list is always better than another
if its head, Con1, is an equal or better consumption than the other head, Con2, and
if in addition the tail of the first list is always better than the tail of the other (T1 is

iy
always better than T2). The latter is tested by a recursive call of the same predicate.
So when we originally invoke always_better, it will peel off the head elements, test
un
the first for being equal or better than the second and recur with the tails of the
lists. In the recursive call, the second elements of the original lists will be tested
and then another recursive goal will be invoked, with the tails of the tails of the
original lists. Thus the program works its way down the lists in a methodical way.
sD

The program will terminate when it gets to the ends of both lists (which, if they are of
the same length, should happen simultaneously). Then the first clause will match (the
boundary condition), and the original goal will succeed. If during this process any
of the equal_or_better_consumption tests fails, of course the original always_better
al

goal will do so also.


This is not the only criterion we could use for comparing lists of consumptions.
ri

Another thing we could look for is whether in two lists one consumption is some-
times equal or better than the corresponding one.
to

sometimes_better([Con1|_], [Con2|_]) :-
equal_or_better_consumption(Con1, Con2).
sometimes_better([_|Con1], [_|Con2]) :-
Tu

sometimes_better(Con1, Con2).

This definition is subtly different from the last one. As before, we use recursion to
work our way down the lists. Now the boundary condition occurs when an element
of the first list is a better consumption than the corresponding element of the second
list. In this case, we can succeed without going any further down the list. If we got all
the way down to the ends of the lists then we would not have found evidence of one
being sometimes better than the other. Notice that in this case the predicate would

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.6 Joining Structures Together 63

correctly fail, because both clauses require non-empty lists in both arguments. The
recursive clause indicates that one way of establishing that a list is sometimes better
than another is to look for better things in the tails of the lists, regardless of what
relationship there may be between the heads.
Exercise 3.1: Using the sometimes_better definition, almost every car will be pre-
ferred to each other. Change the program so that it prefers a car to another if at least

m
one of the test results is significantly better (where you will have to decide what you
mean by “significant”).

co
3.6 Joining Structures Together

a.
The list processing predicate append is used to join two lists together to form another,
new, list. For example, it is true that

iy
append([a, b, c], [3, 2, 1], [a, b, c, 3, 2, 1]).
The predicate append is most often used to create a new list from concatenating two
un
others, like this:

?- append([alpha, beta], [gamma, delta], X).


X = [alpha, beta, gamma, delta]
sD

But it can also be used in other ways:

?- append(X, [b,c,d], [a,b,c,d]).


al

X=[a]

The predicate append is defined as follows:


ri

append([], L, L).
append([X|L1], L2, [X|L3]) :- append(L1, L2, L3).
to

The boundary condition is when the first list is the empty list. In this case, any list
appended to the empty list is the same list. Otherwise, the following points show the
Tu

principles of the second rule:

1. The first element of the first list (X) will always be the first element of the third
list.
2. The tail of the first list (L1) will always have the second argument (L2) appended
to it to form the tail (L3) of the third argument.
3. You actually have to use append to do the appending mentioned in point (2).

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

64 Chapter 3 Using Data Structures

4. As we are continually taking the head from the remainder of the first argument, it
will gradually be reduced to the empty list, so the boundary condition will occur.

We will refer to append in later examples, with further explanation. In later chapters
we will discuss various properties and applications of the append predicate. But first,
let us put it to work in another simple example of recursion.
Suppose we work in a bicycle factory, where it is necessary to keep an inventory

om
of bicycle parts. If we want to build a bicycle, we need to know which parts to draw
from the supplies. Each part of the bicycle may have sub-parts, for example each
wheel has some spokes, a rim, and a hub. Furthermore, the hub can consist of an axle
and gears. Here is an illustration of these parts:

.c
handles

ya
fork frame

hub
i
un
rim

spoke
sD

Let us consider a tree-structured database that will enable us to ask questions about
which parts are required to build a part of a bicycle. In a subsequent section we shall
al

improve this basic program to calculate how many of each part are required.
There are two kinds of parts that we use to build our bicycle. These are assem-
blies and basic parts. Each assembly consists of a quantity of basic parts, such as
ri

the wheel, which consists of several spokes, a rim, and a hub. Basic parts are not
made up of any smaller parts; they simply combine with other basic parts to form
to

assemblies.
We can represent basic parts simply as facts, as follows:
Tu

basicpart(rim).
basicpart(spoke).
basicpart(rearframe).
basicpart(handles).
basicpart(gears).
basicpart(bolt).
basicpart(nut).
basicpart(fork).

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.6 Joining Structures Together 65

Naturally, this is not a complete list of the basic parts required for a bicycle but it
shows the general idea. Next, an assembly can be represented as the name of the
assembly followed by a list of the basic parts, and the quantity of parts required. For
example, the following fact represents that a bike is an assembly made up of two
wheels and a frame:
assembly(bike, [wheel, wheel, frame]).

m
The database of assemblies required for our simplified bicycle is:

assembly(bike, [wheel, wheel, frame]).

co
assembly(wheel, [spoke, rim, hub]).
assembly(frame, [rearframe, frontframe]).
assembly(frontframe, [fork, handles]).

a.
assembly(hub, [gears, axle]).
assembly(axle, [bolt, nut]).

iy
Notice that this particular set of clauses does not perfectly describe a bicycle. We
have not distinguished between the front hub and the rear hub: both have gears! The
chain and pedals are missing, and there is no place for the rider to sit. Also, there
un
is no indication of how to fit the parts together. This simply lists a few of the parts
required, organised into a hierarchy that looks like this:
sD

bike

wheel frame
al

hub spoke rim frontframe rearframe


ri

gears axle fork handle


to

bolt nut
Tu

Remember that this hierarchy does not reflect the actual shape of the data structure,
but only of what we know about the structure of bicycles.
Now we are ready to write the program that, given a part, will list all the basic
parts required to construct it. If the part we want to construct is a basic part, then
nothing more is required. However, if we want to construct an assembly, then we
need to apply the same process to each part making up the assembly. Let us define a

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

66 Chapter 3 Using Data Structures

predicate partsof, to be used in goals of the form partsof(X, Y), where X is the name
of a part, and Y is the list of basic parts that are required to construct X. In our first
version of this program, we shall ignore how many of each kind of part are required
to form assemblies, but multiple parts will be listed in the answer each time they
occur. A better program will be presented in Chapter 7.
The boundary condition occurs when X is a basic part. In this case, we simply
return X in a list:

m
partsof(X, [X]) :- basicpart(X).
The next condition is if X is an assembly. In this case, we need to find out if there is a

co
matching assembly fact in the database, and if so, to use partsof on each member of
the list of sub-parts. We shall use a predicate called partsoflist to handle this second
task.

a.
partsof(X, P) :-
assembly(X, Subparts),
partsoflist(Subparts, P).
iy
Now partsoflist takes a list of parts (from the second argument of the assembly
un
database above), and finds the partsof of each part. After calling itself to get the
partsof for the tail of the list, partsoflist must glue the lists together with append:

partsoflist([], []).
sD

partsoflist([P|Tail], Total) :-
partsof(P, Headparts),
partsoflist(Tail, Tailparts),
al

append(Headparts, Tailparts, Total).

The list which is constructed by partsof will not contain information about how many
ri

parts are required, and duplicate parts may appear in the list. In Chapter 7, we shall
present an improved version of this program that handles these deficiencies.
There are two insights that indicate how partsof can be used to generate En-
to

glish sentences. First, sentences can be decomposed into hierarchical structures: a


sentence has parts noun_phrase and verb_phrase; a noun_phrase has parts deter-
miner and noun, and so forth. So, any simple grammar can be expressed in terms
Tu

of “parts”. Second, partsoflist always looks at the elements of its first argument
from left to right, and its result is appended together in left-to-right order. These
two properties of partsof show that we can use the same framework to generate sen-
tences from a grammar. Part of a typical “assembly” for a grammar might look like
this:

assembly(sentence,[noun_phrase, verb_phrase]).
assembly(noun_phrase, [determiner, noun]).

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.7 Accumulators 67

assembly(determiner, [the]).
assembly(noun, [apple]).
assembly(noun, [fruit]).

and the words in the lexicon would be defined as basic parts:

basicpart(apples).
basicpart(fruit).

m
At this point you may wish to experiment with generating sentences such as “The
apple is a fruit”. You should provide a reasonable grammar and a vocabulary. Satisfy

co
yourself that this modified program will produce all possible grammatical sentences
from the grammar and vocabulary you provide. As always, Prolog will stop at each
solution, and wait for you to type a semicolon to tell it to backtrack to the next solu-

a.
tion.
This is certainly not the last word on processing English language in this book.

iy
The whole of Chapter 9 is devoted to a more sophisticated treatment of analysing
English language in Prolog.
un
3.7 Accumulators
sD

Frequently we need to traverse a Prolog structure and calculate a result that depends
on what was found in the structure. At intermediate stages of the traversal, we will
have an interim value for the result. A common technique is to use an argument of the
al

predicate to represent “the answer so far”. This argument is called an accumulator.


In the following example, we show a definition of the predicate listlen without
using an accumulator, and then a definition using an accumulator. The goal listlen(L,
ri

N) succeeds if the length of list L is N. Some Prolog systems may have the built-
in predicate length for this purpose. First, a listlen without using an accumulator.
to

There are two clauses, the boundary condition and the recursive case. The boundary
condition is a fact stating that the empty list has length 0. The recursive case is a
rule saying that the length of a non-empty list can be calculated by adding one to the
Tu

length of the tail of the list:


listlen([], 0).
listlen([H|T], N) :- listlen(T, N1), N is N1 + 1.
The alternative way to write this uses the same recursive principle, but the answer is
accumulated at each recurrence in an extra argument used for this purpose. We use
an auxiliary predicate lenacc, which is a generalisation of listlen. The goal lenacc(L,
A, N) means that the length of list L, when added to the number A, is the number N.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

68 Chapter 3 Using Data Structures

Thus to use lenacc to find the length of a list, we need to give it the second argument
0 (zero). This is done in an introductory clause which gives the relation between
listlen and lenacc.
listlen(L, N) :- lenacc(L, 0, N).
lenacc([], A, A).
lenacc([H|T], A, N) :- A1 is A + 1, lenacc(T, A1, N).

m
Predicate lenacc also has two clauses. First, for the empty list, the length of the list
will be whatever has been accumulated so far (A). In the second clause, we add 1
to the accumulated amount given by A, and recur on the tail of the list with a new

co
accumulator value A1.
Note that the final argument of the recursive subgoal (N) is the same as the
final argument in the head of the clause. This means that the length returned for

a.
the whole list will be the number that the recursive subgoal calculates. That is, the
production of the final result is being delegated entirely to the recursive subgoal.
All the extra information that is needed to construct this overall result is provided

iy
by the accumulator. If the second clause is used again for the recursive subgoal,
once again the production of the final result is delegated to a recursive subgoal (with
un
a modified accumulator). Thus we get a sequence of lenacc goals, all sharing the
same last argument, each having the tail of the input list of the previous one and an
accumulator that is one greater than the previous one. Here is what the sequence of
subgoals would look like for finding the length of the list [a, b, c, d, e]:
sD

lenacc([a, b, c, d, e], 0, N)
lenacc([b, c, d, e], 1, N)
lenacc([c, d, e], 2, N)
al

lenacc([d, e], 3, N)
lenacc([e], 4, N)
lenacc([], 5, N)
ri

where all the N’s share (or co-refer). The last goal has now met the boundary con-
dition (the end of the input list has been reached), and so the first lenacc clause is
to

now applicable. This instantiates the final argument to whatever the accumulator is
at that point. Since the initial accumulator was 0 and each time we found an element
in the list we passed on an accumulator 1 greater than the previous one, this value is
Tu

the length of the list (5). Also since all the lenacc goals — including the very first
one introduced by listlen — share their final argument, all these goals immediately
get their final arguments instantiated to the length of the list. In particular, this means
here that N in the listlen clause is instantiated to 5.
Accumulators needn’t be integers. If we are producing a list as a result, an
accumulator will hold the list built so far. This may be useful if we need to inspect
our interim results (for example, to avoid adding duplicate elements to the list). Using
an accumulator in this situation can also avoid much wasteful joining of structures.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.7 Accumulators 69

In general, for efficiency we may wish to avoid joining structures together too often
because these operations are expensive. For instance, if we use append to join two
lists together, we make our way down the first list until it is empty. At each stage, we
construct a new piece of list structure in the third argument. When we finally reach
the end of the list, we fill in the final part of the output list with the second input list.
In order to produce an output list that ends with the second input list, we essentially
have to make a copy of the first input list. If the first input list is long, this can be a

om
lot of work.
Consider what happens in our parts inventory if we wish to find the parts that make
up a bicycle. A bicycle assembly is given by the following:

c
assembly(bike,[wheel,wheel,frame]).

a.
To find the parts of a bicycle, we use partsoflist to find the parts coming from the list
[wheel, wheel, frame]. Because of the way partsoflist is defined, this involves:
finding the parts of a frame,

iy
appending these to the empty list to give the parts of [frame],
un
finding the parts of a wheel,
appending these to the parts of [frame] to get the parts of [wheel ,frame],
finding the parts of a wheel (for the second wheel),
sD

appending these to the parts of [wheel,frame] to get the parts of [wheel, wheel,
frame].
This sequence of operations is wasteful because each list of parts for a subpart of a
al

bicycle has to be built twice. It is built once when it is first worked out, and once
when it is appended to the list of parts obtained so far. Because some of the subparts
ri

of a bicycle are themselves assemblies, this wastefulness will be repeated in deriving


their parts as well.
to

We can avoid this unnecessary extra work by using accumulators. As with the listlen
example, we introduce auxiliary predicates with extra accumulator arguments and
have a starting clause that calls one of these with an appropriately initialised accu-
Tu

mulator. Here is a program for the parts inventory which uses accumulators. The
clauses for basicpart and assembly are unchanged, so are not listed here. Notice that
append is no longer used.
partsof(X, P) :- partsacc(X, [], P).
partsacc(X, A, [X|A]) :- basicpart(X).
partsacc(X, A, P) :-
assembly(X, Subparts),

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

70 Chapter 3 Using Data Structures

partsacclist(Subparts, A, P).
partsacclist([], A, A).
partsacclist([P|Tail], A, Total) :-
partsacc(P, A, Hp),
partsacclist(Tail, Hp, Total).
The predicates partsacc and partsacclist are defined very similarly to the previous

m
versions of partsof and partsoflist, except that they each have an accumulator as
their second argument. This argument represents the list of (basic) parts that have
been found so far. So partsacc(X,A,P) means that the parts of object X, when added

co
to the list A, give the list P. Notice the similarity with the meaning of lenacc. If we
wish to use partsacc to find the parts of an object, we must provide it with the empty
list as its second argument; hence the partsof clause.

a.
The first clause of partsacc simply constructs a new list whose head is the object
given in the first argument, and whose tail is the accumulated list of parts, and this
will succeed if the object is a basic part. The second clause, which applies when the

iy
object is an assembly, first finds the list of subparts, and then uses partsacclist to find
the subparts of each part in the list. Note that the accumulator (A) has been passed to
un
partsacclist.
The first clause of partsacclist is the boundary case, in which the result is the
accumulated list of subparts (A). The recursive case calls partsacc to find the subparts
of the next part on the given list, and the recursive goal deals with the remainder
sD

of the list. Note that the second argument of the second clause (A) is used as the
accumulator for the partsacc goal, and that the result of the partsacc goal (Hp) is
used as the accumulator for the recursive goal.
We make further use of accumulators throughout the book. In particular, we
al

draw your attention to Sections 7.2, 7.5, and 7.8, as well as the next section.
ri

3.8 Difference Structures


to

In the previous section, we used an accumulator to avoid unnecessary joining of


structures. One unmentioned effect of this was to produce a resulting list in which
Tu

the elements were in the reverse order to that in which they were produced from the
original list. Sometimes, however, we may wish to generate elements in the same
order as the original list. Difference structures (here difference lists) allow us to do
this.
If we use the parts inventory program to find the parts of a bicycle, the ver-
sion using accumulators works just as well as the original version, and indeed runs
faster. However, if we use it for generating English sentences as suggested before,
we encounter a problem: the words come out in reverse order! This did not matter

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

3.8 Difference Structures 71

for the bicycles, because the order of the parts is not important, but obviously with
English sentences it matters what order the words come in. If we think about the way
in which the list of “parts” is built, it is not surprising that it ends up in the reverse
order to that in which the parts are originally discovered. For every time we come
to a basic part, we create a new accumulator which has this part before all the parts
found so far.
With accumulators, we use two arguments to organise the building of some

m
output structure. One is for “the result so far” and one is for the “final result”. With
difference lists, we also use two arguments, but with a different interpretation. The
first argument is for the “final result”, and the second argument is for a “hole in the

co
final result where further information can be put”. The way we represent a “hole” in
a structure is by a Prolog variable which shares with a component somewhere in the
structure. Thus the following two terms represent a list together with a named “hole

a.
variable” where further information could be put:
[a, b, c|X] X

iy
If we have a list with a “hole” in it, we can further instantiate the list by passing the
“hole variable” as an argument to a Prolog goal which instantiates this argument. In
un
general, we will be interested in where further information can be inserted after this
goal has succeeded. Thus we will require the goal to pass back a new hole through
another argument. So here is a conjunction of goals that would create a list with a
hole, add some elements to the list using predicate p and then fill in the hole that
sD

remains with the list [z]:


?- Res = [a, b, c|X], p(X, NewHole), NewHole = [z].
al

We can allow for the p goal not instantiating the list further by providing a clause
that causes the original hole to be returned as the new hole:
ri

p(Hole, Hole).
If this clause is chosen, the variable Res will have the value [a, b, c, z] when the
to

question succeeds. Better, we can provide a clause that causes the original hole to
be instantiated to a structure containing a new variable and this variable to be passed
back as the new hole:
Tu

p([d|NewHole], NewHole).
In general, a clause like this will, of course, obtain these results partly through the
effects of subgoals that it invokes. If this clause is chosen, the variable Res will have
the value [a, b, c, d, z] when the question succeeds.
Here is a version of the parts inventory program that uses the difference lists tech-
nique:

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

72 Chapter 3 Using Data Structures

partsof(X, P) :- partsacc(X, P, Hole), Hole = [].


partsacc(X, [X|Hole], Hole) :- basicpart(X).
partsacc(X, P, Hole) :-
assembly(X, Subparts),
partsacclist(Subparts, P, Hole).
partsacclist([], Hole, Hole).

m
partsacclist([P|T], Total, Hole) :-
partsacc(P, Total, Hole1),
partsacclist(T, Hole1, Hole).

co
First consider the partsof clause. When partsacc is initially called from the partsof
clause, it will build its result in the second argument P and will instantiate Hole to
a variable. Because partsof calls partsacc only once, it is necessary to terminate the

a.
difference list by instantiating Hole with []. Note that a perfectly valid alternative
definition of partsof is:
partsof(X, P) :- partsacc(X, P, []).
iy
This more succinct version ensures that the very last hole is filled with [] even before
un
the list contents are known.
The first clause of partsacc returns a difference list containing the object in
the first argument, and this applies if the object is a basic part. The second clause,
sD

for assemblies, finds the list of subparts and delegates the traversal of the list to
partsacclist, passing the two arguments making up the difference list (P and Hole).
The second clause uses partsacc to list the subparts using the difference list with Total
and Hole1. The recursive goal then returns the portion of the difference list starting
al

at Hole1 and ending at Hole. The entire result, the list between Total and Hole, is
the result of the second partsacclist clause. The way that the list is constructed by
“weaving” together partial results can be conveyed with the aid of this illustration:
ri
to

partsacclist([P|Tail],Total,Hole) :-
Tu

partsacc(P,Total,Hole1),

partsacclist(Tail,Hole1,Hole).

We make use of difference lists again in the definition of quisortx in Section 7.7.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

Backtracking and the “Cut”

m
co
a.
Let us summarise what we learned in Chapters 1 and 2 about what can happen to a
goal:

iy
1. An attempt can be made to satisfy a goal. When we satisfy a goal, we search the
database from the top. Two things can happen:
un
a) A unifying fact (or rule head) can be found. In this case, we say the goal
has been matched. We mark the place in the database, and instantiate any
previously uninstantiated variables that have unified. If we matched against
sD

a rule, we shall first have to attempt to satisfy the subgoals introduced by


the rule. If the goal succeeds, we then attempt to satisfy the next goal. In our
diagrams, this is the goal in the next box below the arrow. If the original goal
al

appears in a conjunction, this will be the goal to its right in the program.
b) No unifying fact (or rule head) can be found. In this case, we say the goal has
ri

failed. We then attempt to re-satisfy the goal in the box above the arrowhead.
If the original goal appears in a conjunction, then this will be the goal on its
left in the program.
to

2. We can attempt to re-satisfy a goal. First of all, we attempt to re-satisfy each


of the subgoals in turn, the arrow retreating up the page. If no subgoal can be
Tu

re-satisfied in a suitable way, we attempt to find an alternative clause for the


goal itself. In this case, we must make uninstantiated any variables that became
instantiated when the previous clause was chosen. This is what we mean by
“undoing” all the work previously done by this goal. Next, we resume searching
the database, but we begin the search from where the goal’s place-marker was
previously put. As before, this new “backtracked” goal may either succeed or
fail, and either step (a) or (b) above would occur.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

74 Chapter 4 Backtracking and the “Cut”

This chapter will look at backtracking in more detail. It will also look at a special
mechanism that can be used in Prolog programs: the “cut”. The cut allows you to tell
Prolog which previous choices it need not consider again.

4.1 Generating Multiple Solutions

m
The simplest way a set of facts can allow multiple solutions to a question is when
there are several facts that will match against the question. For instance, if we have

co
the following facts in which father(X, Y) means that the father of X is Y:
father(mary, george).

a.
father(john, george).
father(sue, harry).
father(george, edward).

iy
The question
?- father(X, Y).
un
will have several possible answers. If we prompt with a semicolon, Prolog will give
us the following:
sD

X=mary, Y=george ;
X=john, Y=george ;
X=sue, Y=harry ;
X=george, Y=edward
al

It finds these answers by searching through the database to find the facts and rule
about father in the order in which they were given. Prolog is not particularly clever
ri

about this. It does not remember anything about what it has shown before. So if we
ask
to

?- father(_, X).
(for which X is X a father?) we will get:
Tu

X=george ;
X=george ;
X=harry ;
X=edward
with george repeated twice because George is the father of both Mary and John.
If Prolog has two ways of showing the same thing, it treats them as two different
solutions.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.1 Generating Multiple Solutions 75

Backtracking happens in exactly the same way if the alternatives are embedded
more deeply in the processing. For example, one rule in a definition of “one of the
children of X is Y” might be
child(X, Y) :- father(Y, X).
Then, the question
?- child(X, Y).

om
would give
X=george, Y=mary ;
X=george, Y=john ;

c
X=harry, Y=sue ;
X=edward, Y=george

a.
Because father(Y, X) has four solutions, so does child(X, Y). Moreover, the solutions
are generated in the same order. All that is different is that the order of the arguments

meaning that X is a father,


father(X) :- father(_, X).
iy
is different, as is specified in the definition of child. Similarly, if we defined father(X)
un
then the question
?- father(X).
sD

would evoke:
X=george ;
X=george ;
al

X=harry ;
X=edward
If we mix facts and rules, the alternatives follow again in the order in which things
ri

are presented. Thus we might represent that adam is a person, anything is a person if
it has a mother, and eve is a person. Also, various people have various mothers:
to

person(adam).
person(X) :- mother(X, Y).
person(eve).
Tu

mother(cain, eve).
mother(abel, eve).
mother(jabal, adah).
mother(tubalcain, zillah).
In this case, if we asked the question
?- person(X).
the answers would be:

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

76 Chapter 4 Backtracking and the “Cut”

X=adam ;
X=cain ;
X=abel ;
X=jabal ;
X=tubalcain ;
X=eve
Let us look now at a more interesting case where there are two goals, each of which

om
has several solutions. Let us imagine we are planning a party and want to speculate
about who might dance with whom. We can start writing a program as follows:
possible_pair(X, Y) :- boy(X), girl(Y).

c
boy(john).
boy(marmaduke).

a.
boy(bertram).
boy(charles).
girl(griselda).
girl(ermintrude). iy
un
girl(brunhilde).
This program says that X and Y form a possible pair if X is a boy and Y is a girl. Now
let’s see what possible pairs there are:
sD

?- possible_pair(X, Y).
X = john, Y = griselda ;
X = john, Y = ermintrude ;
X = john, Y = brunhilde ;
al

X = marmaduke, Y = griselda ;
X = marmaduke, Y = ermintrude ;
ri

X = marmaduke, Y = brunhilde ;
X = bertram, Y = griselda ;
X = bertram, Y = ermintrude ;
to

X = bertram, Y = brunhilde ;
X = charles, Y = griselda ;
Tu

X = charles, Y = ermintrude ;
X = charles, Y = brunhilde
You should make sure that you understand why Prolog produces the solutions in
this order. First of all, it satisfies the goal boy(X), finding john, the first boy. Then
it satisfies girl(Y), finding griselda, the first girl. At this point, we ask for another
solution by typing “;” to cause backtracking. Prolog attempts to re-satisfy what it did
last, which is the girl goal within the satisfaction of the possible_pair goal. It finds
the alternative girl ermintrude, and so the second solution is john and ermintrude.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.1 Generating Multiple Solutions 77

Similarly, it generates john and brunhilde as the third solution. The next time it tries
to re-satisfy girl(Y), Prolog finds that its place-marker is at the end of the database,
and so the goal fails. Now it tries to re-satisfy boy(X). The place-marker for this
was placed at the first fact for boy, and so the next solution found is the second boy
(marmaduke). Now that it has re-satisfied this goal, Prolog looks to see what is next:
it must now satisfy girl(Y) from the start again. So it finds griselda, the first girl. The
next three solutions now involve marmaduke and the three girls. Next time we ask

m
for an alternative, the girl goal cannot be re-satisfied again. So another boy is found,
and the search through girls starts again from scratch. And so on. Eventually, the girl
goal fails and there are also no more solutions to the boy goal either. So the program

co
can find no more pairs.
These examples are all very simple. They just involve the specification of many
facts or the use of rules to access those facts. Because of this, they can only generate

a.
a finite number of possible solutions. Sometimes we might want to generate an infi-
nite number of possibilities: not because we want to consider them all, but because

definition (discussed in the previous chapter).


iy
we may not know in advance how many we need. In this case we need a recursive

Consider the following definition of what it is to be a positive integer1. The goal


un
is_integer(N) will succeed providing N is instantiated to a positive integer. If N is not
instantiated at the time the goal is considered, then an is_integer(N) goal will cause
a positive integer to be chosen, and N will be instantiated to it:
sD

/* 1 */ is_integer(0).
/* 2 */ is_integer(X) :- is_integer(Y), X is Y + 1.
If we ask the question
al

?- is_integer(X).
we will get as the possible answers all the integers in ascending order (0, 1, 2, 3,
ri

...), one at a time. Each time we force backtracking to occur (perhaps by typing
semicolon), is_integer will succeed with its argument instantiated to a new integer.
to

So in principle this short definition generates an infinite number of answers. Why?


The sequence of events that leads to the first three solutions is shown in Figures 4.1,
4.2, and 4.3.
Tu

1
By positive integer we mean a whole number not less than 0. You should satisfy yourself
that there are an infinite number of these.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

78 Chapter 4 Backtracking and the “Cut”

is_integer(0)

(1)

m
co
Fig. 4.1. The first solution

a.
is_integer(1)
(2) iy
un
is_integer(0)
sD

(1)
al

1 is 0 + 1
ri
to

Fig. 4.2. The second solution


Tu

At each stage, the lowest (1) is where another choice will be made next. Initially,
we have a choice between fact 1 and rule 2 to answer the question. If we choose
fact 1, no more choices have to be made, and we get X=0. Otherwise we choose rule
2 and have a choice how to satisfy the goal it introduces. If we choose fact 1, we
end up with the answer X=1; otherwise we use rule 2 and must again choose how to
satisfy the subgoal produced. And so on. At each stage, the first thing Prolog does is

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.1 Generating Multiple Solutions 79

is_integer(2)
(2)

m
is_integer(1)
(2)

co
is_integer(0)

a.
(1)

iy
1 is 0 + 1
un
sD

2 is 1 + 1
al
ri
to

Fig. 4.3. The third solution


Tu

to pick fact 1. Only on backtracking does it undo the last choice. Each time it does
this, it goes back to where it last chose fact 1, and instead chooses rule 2. Once it has
decided to use rule 2, a new subgoal is introduced. Fact 1 is the first possibility for
satisfying it.
Most Prolog rules will give rise to alternative solutions if they are used for goals
that contain a lot of uninstantiated variables. For instance, the relation of membership
of a list (from Chapter 3):

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

80 Chapter 4 Backtracking and the “Cut”

member(X, [X|_]).
member(X, [_|Y]) :- member(X, Y).

will generate alternatives. If we ask


?- member(a, X).
(notice X in the question is uninstantiated) then the successive values of X will be

m
partially-defined lists where a is the first, second, third, (and so on) member. See if
you can see why this is.
A further result of allowing this definition of member to backtrack is that the

co
question
?- member(a, [a,b,r,a,c,a,d,a,b,r,a]).

a.
actually can succeed five times. Clearly, there are some applications of member
where we only need it to succeed once, if at all, and then discard the other four

iy
choices. We can tell Prolog to discard choices in this way by using the “cut”.
un
4.2 The “Cut”

This section looks at a special mechanism that can be used in Prolog programs the
sD

“cut”. The “cut” allows you to tell Prolog which previous choices it need not consider
again when it backtracks though the chain of satisfied goals. There are two reasons
why it may be important to do this:
al

Your program will operate faster because it will not waste time attempting to
satisfy goals that you can tell beforehand will never contribute to a solution;
ri

Your program may occupy less of the computer’s memory space because more
economical use of memory can be made if backtracking points do not have to be
to

recorded for later examination.

In some cases, including a “cut” may mean the difference between a program that
Tu

will run and a program that will not.


Syntactically, a use of “cut” in a rule looks just like the appearance of a goal
which has the predicate “!” and no arguments. As a goal, this succeeds immediately
and cannot be re-satisfied. However, it also has side-effects which alter the way back-
tracking works afterwards. The effect is to make inaccessible the place markers for
certain goals so that they cannot be re-satisfied.
Let us see how this works in an example. Imagine that you are running a library
and have a Prolog database containing information about what books there are, who

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.2 The “Cut” 81

has borrowed what. and when books are due back. One thing you might be concerned
about is which of the library facilities should be open to which people. Some facili-
ties, which we might call basic facilities, should be open to everyone. These include
the use of the reference library and the enquiries desk. On the other hand, the library
might want to be selective about which people are allowed to use additional facilities,
such as actually borrowing books or using inter-library loans. One rule that might be
made is that, if a person has a book overdue, then the additional facilities will not be

om
available to the person until the book is returned. Here is part of a program that uses
this rule:

facility(Pers, Fac) :-
book_overdue(Pers, Book),

c
!,

a.
basic_facility(Fac)
facility(Pers, Fac) :- general_facility(Fac).
basic_facility(reference).
basic_facility(enquiries). iy
un
additional_facility(borrowing).
additional_facility(inter_library_loan).
general_facility(X) :- basic_facility(X).
sD

general_facility(X) :- additional_facility(X).

Then we need a database of clients and their borrowing habits, of which only two are
shown here:
al

client(’A. Jones’).
client(’W. Metesk’)
ri

book_overdue(’C. Watzer’, book10089).


book_overdue(’A. Jones’, book29907).
to

Why is there a cut in this program and what effect does it have? Let us assume that
we wish to run through all our clients and find out what facilities are open to them.
Tu

Thus we give Prolog the question:


?- client(X), facility(X, Y).
Prolog will start by finding the first client, ’A. Jones’. Let us assume that this client
has several overdue books. In order to find what facilities are open to him, Prolog
will start by using the first clause for facility. This introduces a new goal to see
whether he has any overdue books. After a short search among the book_overdue
facts, the fact about the first overdue book for A. Jones is found (the second fact for

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

82 Chapter 4 Backtracking and the “Cut”

client(’A. Jones’)

(1)

om
facility(’A. Jones’)

c
(1)

a.
iy
book_overdue(’A. Jones’,book29907)
un
(2)
sD

!
al

basic_facility(Y)
ri

Fig. 4.4. Just before the cut is encountered


to

this predicate). The next goal encountered is the “cut”. This goal succeeds, and the
Tu

effect is to commit the system to all the decisions made since the first facility clause
was chosen. We can show the situation just before the cut is encountered in a diagram
as shown in Figure 4.4.
When the cut is encountered, it “cuts” the flow of satisfaction line so that if it is
forced to retreat beyond this point it will have to take a short cut, as seen in Figure
4.5.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.2 The “Cut” 83

client(’A. Jones’)

(1)

m
co
facility(’A. Jones’)

(1)

a.
iy
book_overdue(’A. Jones’,book29907)
un
(2)
sD

!
al

basic_facility(Y)
ri

Fig. 4.5. The cut commits to the solutions so far, but the flow of satisfaction is altered so that
to

if basic_facility fails, then backtracking takes a shortcut to re-satisfy the client goal
Tu

The effect of the cut in the facility rule (clause 1) is to commit the system to every
choice it has made since it chose that rule. The flow of satisfaction path has been
changed so as to avoid all the place markers between the facility goal and the cut
goal inclusive. Thus if backtracking later causes a retreat back past this point, the
facility goal will immediately fail.
Because of the cut, the system will not consider alternative solutions for the goal
book_overdue(’A. Jones’, Book). This is quite reasonable as we are only interested

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

84 Chapter 4 Backtracking and the “Cut”

in whether the client has any overdue book, not what all the books are. Neither will
the system consider clause 2 for facility, because the choice of the rule that the cut
appears in is also bypassed on backtracking. This is again reasonable here, because
we don’t want to generate solutions that say that all facilities are open to A. Jones.
In summary, the effect of the cut in this example is to say:
If a client is found to have an overdue book, then only allow the client the

m
basic facilities of the library. Don’t bother going through all the client’s
overdue books, and don’t consider any other rule about facilities.
In this example, the cut committed the system to all the decisions made from it back

co
to the facility goal. This is called the parent goal of the cut goal, because it is the goal
that caused the use of the rule containing the cut. In our diagrams, the parent goal
is always the goal whose box is the smallest one enclosing the ”!” box. The formal

a.
definition of the effect of the cut symbol is as follows:

iy When a cut is encountered as a goal, the system thereupon becomes


committed to all choices made since the parent goal was invoked.
All other alternatives are discarded. Hence an attempt to re-satisfy
un
any goal between the parent goal and the cut goal will fail.

There are several ways of describing what has happened to the choices that are af-
sD

fected by a cut. One can say that the choices are cut or frozen, that the system com-
mits itself to the choices made or that the alternatives are discarded. One can also
look at the cut symbol as being rather like a fence that separates goals. In this con-
junction of goals,
al

foo :- a, b, c, !, d, e, f.
Prolog will quite happily backtrack among goals a, b, and c, until the success of
ri

c causes the “fence” to be crossed to the right to reach goal d. Then, backtracking
can occur among d, e, and f, perhaps satisfying the entire conjunction several times.
to

However, if d fails, causing the “fence” to be crossed to the left, then no attempt will
be made to re-satisfy goal c: the entire conjunction of goals will fail, and the goal foo
will also fail.
Tu

One further note before we go on to see more examples of the cut in use. We
have said that if the cut appears in some rule and the cut goal is satisfied then Pro-
log becomes committed to all choices made since the parent goal was invoked. This
means that the choice of that rule, and all other choices made since then, become
fixed. We will see later that it is possible to provide alternatives within a single rule
using the built-in predicate “;” (meaning “or”). The choices introduced by this facil-
ity are affected in exactly the same way. That is, when a cut goal is satisfied, all “or”
choices that have been made since the rule was chosen are fixed.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions et

4.3 Common Uses of the Cut 85

4.3 Common Uses of the Cut

We can divide the common uses of “cut” into three main areas:

The first concerns places where we want to tell the Prolog system that it has
found the right rule for a particular goal. Here, the cut says, “if you get this far,
you have picked the correct rule for this goal.”

m
The second concerns places where we want to tell the Prolog system to fail a
particular goal immediately without trying for alternative solutions. Here, we use

co
the cut in conjunction with the fail predicate to say, “if you get to here, you should
stop trying to satisfy this goal.”
The third concerns places where we want to terminate the generation of alterna-

a.
tive solutions through backtracking. Here, the cut says, “if you get to here, you
have found the only solution to this problem, and there is no point in ever looking
for alternatives.”
iy
We will now look at some examples of these three uses. You should bear in mind,
un
however, that the cut has a single meaning in all these applications. The division
into three main areas of use is purely for tutorial reasons, and to show what kinds of
reasons you might have for putting cuts into your programs.
sD

4.3.1 Confirming the Choice of a Rule


al

Often in a Prolog program, we wish to associate several clauses with the same pred-
icate. One clause will be appropriate if the arguments are of one form, another will
be appropriate if the arguments are of another form, and so on. Often we can specify
ri

which rule should be used for a given goal by providing patterns in the rule heads
that will only match goals of the right types. However, this may not always be possi-
to

ble. If we cannot tell in advance what forms the arguments may take, or if we cannot
specify an exhaustive set of patterns, we may have to compromise. This means giv-
ing rules for some specific argument types and then giving a “catchall” rule at the
Tu

end for everything else.


As an example of this, consider the following program. The rules define the
predicate sum_to such that giving Prolog the goal sum_to(N, X) with N having an
integer value, causes X to be instantiated to the sum of the numbers from 1 to N.
Thus, for instance, it produces the following:

?- sum_to(5, X).
X = 15 ;

Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions et
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

86 Chapter 4 Backtracking and the “Cut”

no

because 1+2+3+4+5 is 15. Here is the program.

sum_to(1, 1) :- !.
sum_to(N, Res) :-
N1 is N - 1,

m
sum_to(N1, Res1),
Res is Res1 + N.

co
This is a recursive definition. The idea is that the boundary condition occurs when
the first number is 1. In that case, the answer is also 1. The second clause introduces
a recursive sum_to goal. However, the first number of the new goal is one less than

a.
the original one. The new goal that this goal will invoke will have its first argument
one less again. And so on until the boundary condition is reached. Since the first
arguments are always getting smaller, the boundary condition must be reached even-

iy
tually (assuming that the original goal has a first argument not less than 1), and the
program will terminate.
un
The interesting thing about this program is how we have handled the two cases:
when the number is 1, and when it is something else. When we defined predicates
that talked about lists, it was easy to specify the two cases that would normally arise:
when the list was [] and when it was of the form [A | B]. With numbers, things are
sD

not so easy, because we cannot specify a pattern that will only match an integer not
equal to 1. The solution adopted in this example is to provide a pattern for the “1”
case and just to leave a variable to match against anything else. We know from the
al

way Prolog searches through the database that it will try to match the number against
1 first and will only try the second rule if this fails. So the second rule should only
be used for numbers not equal to 1.
ri

However, this is not the whole story. If Prolog ever backtracks and comes to
reconsider the choice of rule when applied to the number 1, it will find that the
to

second rule is applicable. As far as it can see, both rules provide alternatives for the
goal sum_to(1, X). We must tell it that on no account is the second rule ever to be
tried if the number is 1. One way of doing this is to put a cut in the first rule (as
Tu

shown). This tells Prolog that, once it has got this far in the first rule, it must never
remake the decision about which rule to use for the sum_to goal. It will only get this
far if the number is in fact 1. Let us see what this looks like in terms of the flow of
satisfaction. If we call sum_to(1,X) in the context:

go :- sum_to(1, X), foo(apples).


?- go.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions e

4.3 Common Uses of the Cut 87

go

(1)

sum_to(1,1)

m
(1)

co
!

a.
iy foo(apples)
un
Fig. 4.6. The goal foo(apples) fails

and the goal foo(apples) fails, then at the point of failure we will have the situation
sD

shown in Figure 4.6.


When Prolog tries to re-satisfy the goals in reverse order, it will find that two of the
goals cannot be re-satisfied because the flow of satisfaction path has been re-routed.
al

Hence it will correctly avoid trying alternative ways of satisfying sum_to(1,X).


Exercise 4.1: What happens if the “cut” is left out here and backtracking gets round
to reconsidering the sum_to goal? What alternative results, if any, are produced, and
ri

why?
The last example showed how “cut” can be used to make Prolog behave sensibly
to

when we cannot distinguish between all the possible cases by specifying patterns in
the heads of the rules. A more usual situation in which we cannot specify patterns to
decide which rule to use arises when we want to provide extra conditions, in the form
Tu

of Prolog goals, which will decide on the appropriate rule. Consider the following
alternative form of the above example:

sum_to(N, 1) :- N =< 1, !.
sum_to(N, R) :-
N1 is N - 1,
sum_to(N1, R1),
R is R1 + N.

Visit https://www.tutorialsduniya.com
for Notes, books,programs,question paperswith solutions e
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

88 Chapter 4 Backtracking and the “Cut”

In this case, we say that the first rule is the one to choose if the number provided is
less than or equal to one. This is slightly better than the previous formulation, because
it means that the program produces an answer (rather than running on indefinitely)
if the first argument is given as 0 or a negative number. If the condition is true, the
result 1 can be produced immediately, and no more recursive goals are necessary.
Only if the condition is not true do we want ever to try the second rule. We must tell
Prolog that once it has proved N =< 1, it must never reconsider the choice about what

om
rule to choose. This is what the cut does.
It is a general principle that uses of cut to tell Prolog when it has picked the only
correct rule can be replaced by uses of \+. This is a Prolog built-in predicate, which
means that its definition is already provided when you start your Prolog session. It

c
is also already declared as a prefix operator. So you can use it in your own programs
without having to write down a definition each time (built-in predicates are described

a.
more fully in Chapter 6). Predicate \+ is defined in such a way that the goal \+X
succeeds only if X, when seen as a Prolog goal, fails. So \+X means that “X is not

iy
satisfiable as a Prolog goal”. As an example of replacing cuts with uses of \+, the
two possibilities given for the sum_to definition can be rewritten as:
un
sum_to(1, 1).
sum_to(N, R) :-
\+(N = 1),
N1 is N - 1,
sD

sum_to(N1, R1),
R is N + R1.
al

or

sum_to(N,1) :- N =< 1.
ri

sum_to(N,R) :-
\+(N =< 1),
to

N1 is N - 1,
sum_to(N1, R1),
R is N + R1.
Tu

In fact, Prolog provides suitable built-in predicates to substitute for both of these
uses. For example, we can replace \+(N=1) by N\=1, and \+(N=<1) by N>1. In gen-
eral, we will not be able to do this with all the conditions we dream up.
It is good programming style to replace cuts by the use of \+. This is because
programs containing cuts are in general harder to read than programs not containing
them. If one can localise all occurrences of cut to inside the definition of \+, then the
program will be easier to understand. However, the definition of \+ involves trying

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.3 Common Uses of the Cut 89

to show that the goal it is given can be satisfied. Therefore, if we have a program of
the general form:

A :- B, C.
A :- \+B, D.

Prolog may well end up trying to satisfy B twice. It will have to try to satisfy B when

m
it looks at the first rule. Also, if it ever backtracks and considers the second rule, it
will have to try to satisfy B again to see if \+B can be satisfied. This duplication could
be very inefficient if the condition B was rather complicated. This would not be the

co
case if instead we had:

A :- B, !, C.

a.
A :- D.

So one must sometimes weigh up the advantages of a clear program against those

iy
of a program that will run quickly. The discussion of efficiency leads us to our last
example of the cut being used to fix the choice of a rule. Consider the definition of
un
append from Chapter 3:

append([], X, X).
append([A|B], C, [A|D]) :- append(B, C, D).
sD

If we are always using append for the case where we have two known lists and want
to know what list consists of the first appended onto the front of the second, we may
feel that it is inefficient that when backtracking gets to reconsider how to deal with
al

a goal like append([],[a,b,c,d],X) it must try to use the second rule, even though the
attempt is bound to fail. We know in this context that if the first list is [] then the
ri

first rule is the only correct one, and this information can be given to Prolog by a use
of the cut. In general, Prolog implementations will be able to make better use of the
available storage if they are told things like this than if they have to keep a record of
to

apparent choices that are not really there. So we could rewrite our definition as:
Tu

append([], X, X) :- !.
append([A|B], C, [A|D]) :- append(B, C, D).

Assuming our restricted use of append, this does not affect at all which solutions
the program finds. It only increases the space and time efficiency to some extent. In
exchange for this, we are liable to find that other kinds of uses of append will no
longer work as expected, as is shown in Section 4.4.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

90 Chapter 4 Backtracking and the “Cut”

4.3.2 The “cut-fail” Combination

In the second major application area, the cut is used in conjunction with the built-in
fail predicate. This is another built-in predicate, like \+. It has no arguments, which
means that the success of the goal fail does not depend on what any variables stand
for. Indeed, fail is defined in such a way that as a goal it always fails and causes
backtracking to take place. This is just like what happens if we try to satisfy a goal

m
for a predicate with no facts or rules. When fail is encountered after a cut, the normal
backtracking behaviour will be altered by the effect of the cut. In fact, the particular

co
combination “cut-fail” turns out to be quite useful in practice.
Let us consider how we might use this combination in a program to calculate
how much tax somebody should pay. One thing we might want to know is whether
the person is an “average taxpayer”. In this case, the calculations might be quite

a.
simple and not have to involve considering lots of special cases. Let us define a
predicate average_taxpayer, where average_taxpayer(X) means that X is an average

iy
taxpayer. For instance, Fred Bloggs, who is married with 2 children and works in a
bicycle factory, might be considered quite average. However, the managing director
of an oil company may be earning too much, and a student may be earning too
un
little for the same kinds of tax calculations to be appropriate. We should start by
considering a possible special case. It may be that special tax laws apply to somebody
who is a native of another country, because he may have obligations to that country
sD

as well. Therefore, however average he may be in other respects, a foreigner will not
be classed as an average taxpayer. We can start writing rules about this as follows:

average_taxpayer(X) :- foreigner(X), fail.


al

average_taxpayer(X) :- . . .

In this extract, which is not correct yet, the first rule attempts to say, “if X is a for-
ri

eigner then the goal average_taxpayer(X) should fail”. The second rule is to apply
the general criterion for being an average taxpayer for the cases when X is not a
to

foreigner. The trouble is that if we asked the question:


?- average_taxpayer(widslewip).
Tu

about a foreigner called widslewip, the first rule would match and the foreigner goal
would succeed. Next, the fail goal would initiate backtracking. In attempting to re-
satisfy the average_taxpayer goal, Prolog would find the second rule and start ap-
plying the general criterion to widslewip. Now it is quite likely that he would pass
the further tests, being average in other ways, in which case the question would
incorrectly be answered “yes”. So our first rule has been completely ineffective in
rejecting our friend as an average taxpayer.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.3 Common Uses of the Cut 91

Why is this? The answer is that during backtracking Prolog tries to re-satisfy
every goal that has succeeded. So in particular it will investigate alternative ways of
satisfying the goal
?- average_taxpayer(widslewip).
In order to stop Prolog finding alternatives for this, we need to “cut” the choice
(freeze the decision) before failing. We can do this by inserting a cut before the fail

m
goal. A slightly more comprehensive definition of average_taxpayer incorporating
this change is shown here:

co
average_taxpayer(X) :- foreigner(X), !, fail.
average_taxpayer(X) :-
spouse(X, Y),

a.
gross_income(Y, Inc),
Inc > 3000,
!, fail.
average_taxpayer(X) :- iy
un
gross_income(X, Inc),
2000 < Inc, 20000 > Inc.
gross_income(X, Y) :-
sD

receives_pension(X, P),
P < 5000,
!, fail.
gross_income(X, Y) :-
al

gross_salary(X, Z),
investment_income(X, W),
ri

Y is Z + W.
investment_income(X, Y) :- ...
to

Note the use in this program of several other “cut-fail” combinations. In the second
rule for average_taxpayer we say that the attempt to show that someone is an average
Tu

taxpayer can be abandoned if we can show that that person’s spouse earns more than
a certain amount. Also, in the definition of the predicate gross_income we say (in the
first rule) that if somebody receives a pension that is below a certain amount then,
whatever their other circumstances, we will consider them not to have any gross
income at all.
An interesting application of the “cut-fail” combination is in the definition of
the predicate \+. Most Prolog implementations provide this already defined, but it
is interesting to consider how we can provide rules for it. We require that the goal

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

92 Chapter 4 Backtracking and the “Cut”

\+P, where P stands for another goal, succeeds if and only if the goal P fails. This is
not exactly in accord with our intuitive notion of “not true”: it is not always safe to
assume that something is not true if we are unable to prove it. However, here is the
definition:

\+P :- call(P), !, fail.


\+P.

m
The definition of \+ involves invoking the argument P as a goal, using the built-in
predicate call. Predicate call simply treats its argument as a goal and attempts to

co
satisfy it. We want the first rule to be applicable if P can be shown, and the second
to be applicable otherwise. So we say that if Prolog can satisfy call(P) it should
thereupon abandon satisfying the \+ goal. The other possibility is that Prolog cannot

a.
show call(P). In this case, it never gets to the cut. Because the call(P) goal failed,
backtracking takes place, and Prolog finds the second rule. Hence the goal \+P will
succeed when P is not provable.

iy
As with the first use of “cut”, we can replace uses of “cut-fail” with uses of
\+. This involves rather more reorganisation of the program than before, but does not
un
introduce the same inefficiency. If we were to rewrite our average_taxpayer program,
it would start off something like:
average_taxpayer(X) :-
\+foreigner(X),
sD

\+((spouse(X, Y), gross_income(Y, Inc), Inc > 3000)),


gross_income(X, Inc1),
.
al

.
.
Note that in this example, we have to enclose a whole conjunction of goals inside the
ri

\+. In order to show unambiguously that the commas join the goals into a conjunction
(rather than separating multiple \+ arguments), we have enclosed the \+ argument in
to

an extra set of brackets.


Tu

4.3.3 Terminating a “generate and test”

Now we come to look at the last major use of “cut” in Prolog programs: to terminate
a “generate and test” sequence. Very often a program will have parts that conform
to the following general model. There will be a sequence of goals that can succeed
in many ways, and which generates many possible solutions on backtracking. Af-
ter this, there are goals that check whether a solution generated is acceptable for
some purpose. If these goals fail, backtracking will lead to another solution being

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.3 Common Uses of the Cut 93

proposed. This will be tested for appropriateness, and so on. This process will stop
when either an acceptable solution is generated (success), or when no more solutions
can be found (failure). We can call the goals that are yielding all the alternatives the
“generator” and those that check whether a solution is acceptable the “tester”. Let
us consider an example of this: a program to play the game Noughts and Crosses,
also known as Tic-Tac-Toe. In case you haven’t come across this game, it involves
two players taking turns to occupy squares on a 3 by 3 board. One player occupies

m
squares with pieces marked o, and the other one with pieces marked x. To illustrate,
here is a board part way through a game:

co
o

a.
x o

x iy
un
To represent a board, we can use a 9-component structure b (for “board”), using
the constants x and o to represent the pieces. If a square is empty, we will mark it
with an e. The components of the board can be ordered in rows from left-to-right, so
sD

for example, the above pictured board is represented like this: b(e,o,e,e,x,o,e,x,e).
The object of the game is to get three of one’s own pieces in a (vertical, horizon-
tal or diagonal) line before the other player does. There are eight ways to make a
al

line of pieces. We can represent the lines of a board in the following way, where
line(B,X,Y,Z) instantiates the arguments X,Y,Z to the three squares that make up a line
in board B:
ri

line(b(X,Y,Z,_,_,_,_,_,_), X, Y, Z).
to

line(b(_,_,_,X,Y,Z,_,_,_), X, Y, Z).
line(b(_,_,_,_,_,_,X,Y,Z), X, Y, Z).
line(b(X,_,_,Y,_,_,Z,_,_), X, Y, Z).
Tu

line(b(_,X,_,_,Y,_,_,Z,_), X, Y, Z).
line(b(_,_,X,_,_,Y,_,_,Z), X, Y, Z).
line(b(X,_,_,_,Y,_,_,_,Z), X, Y, Z).
line(b(_,_,X,_,Y,_,Z,_,_), X, Y, Z).

By now you should immediately recognise that X is a variable denoting a board po-
sition, and x is a constant naming the x-shaped pieces.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

94 Chapter 4 Backtracking and the “Cut”

We shall next look at the game from the point of view of the player who puts
o’s on the board. The predicate forced_move is used to answer the question, “Is the
o-player forced to put a piece in a particular position?” This will be the case if the
o-player cannot immediately win (we do not deal with this here), but the x-player is
threatening to win on its next move:

forced_move(Board) :-

om
line(Board, X, Y, Z),
threatening(X, Y, Z),
!.

Notice that the “cut” here means that one threat is enough to force a move. We now

c
need to determine whether a row of squares constitutes a threat. This will be the case

a.
if two of the squares are x’s and one is empty. There are three possibilities for these
threatening patterns. A suitable definition is:

threatening(e, x, x).
threatening(x, e, x). iy
un
threatening(x, x, e).

For instance, in the position shown below, the o-player is forced to play in the indi-
cated square, because if it does not, the x-player will be able to fill the second column
sD

and win in the next turn: The program works by trying to find a line, two of whose
al

The centre column matches threatening(x,x,e)...


x
ri

x o
to

... so o must play here


Tu

squares are occupied by crosses, and the other of which is empty. If it can, it reports
a forced move situation.
In the clause for forced_move, the goal line(Board,X,Y,Z) serves as a “genera-
tor” of possible lines. This goal can succeed in many ways, with the variables X,Y,Z
instantiated to one of the possible lines of squares. Once a possible line has been
suggested by line, it is necessary to see whether the opponent is threatening to claim

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.3 Common Uses of the Cut 95

this line. This is the purpose of the “tester” goal: threatening(X,Y,Z), which looks for
one of three threatening patterns of squares. The basic idea of the program is very
simple. First, line proposes a line, and then threatening looks to see whether that line
is threatening. If so, the original forced_move goal succeeds. Otherwise, backtrack-
ing occurs and line comes up with another possible line. Now this is tested also, and
maybe backtracking occurs again to find another line. If we get to the point when
line can generate no more lines, then the forced_move goal will correctly fail (there

m
is no forced move).
Now consider what happens if this program, as part of a larger system, success-
fully finds a forced move. Suppose that somewhere later on during the move calcula-

co
tion a failure occurs, and Prolog eventually backtracks to re-satisfy the forced_move
goal. We do not wish line to start producing more possible lines to be checked. This
makes no sense, because it cannot possibly be useful to find alternative forced moves.

a.
If we have found one of them, then we cannot do anything better than carry it out:
failure to do so would guarantee losing the game. Most of the time, there will be no

iy
alternative anyway, and forced_move will search through all the untried lines in vain,
before itself failing. However, in the case of forced moves, we know that even if there
is an alternative solution, it cannot be of any use in a context where a failure occurred
un
in spite of the first solution. We can prevent Prolog from wasting time searching for
different forced moves by putting a “cut” at the end of the clause. This has the effect
of freezing the last successful line solution. Including the “cut” amounts to saying
“when I look for forced moves, it is only the first solution that is important.”
sD

Let us look at another example of a program that works by a “generate and test”
method. We came across the idea of integer division in Section 2.5. Most Prolog
systems provide this facility automatically, but here is a program for integer division
al

that only uses addition and multiplication.

divide(N1, N2, Result) :-


ri

is_integer(Result),
Product1 is Result * N2,
to

Product2 is (Result + 1) * N2,


Product1 =< N1, Product2 > N1,
!.
Tu

This rule uses the predicate is_integer (as defined before) to generate the number
Result which is the result of dividing N1 by N2. For instance, the result of dividing
27 by 6 is 4, because 4 * 6 is less than or equal to 27, and 5 * 6 is greater than 27.
The rule uses is_integer as a “generator”, and the rest of the goals provide the
appropriate “tester”. Now we know in advance that, given specific values of N1 and
N2, divide(N1,N2,Result) can only succeed for one possible value for Result. For
although is_integer can generate infinitely many candidates, only one will ever get

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

96 Chapter 4 Backtracking and the “Cut”

past the tests. We can indicate this knowledge by putting a cut at the end of the
rule. This says that if we ever successfully generate a Result that passes the tests
for being the result of the division, we need never try any more. In particular, we
need never reconsider any of the choices that were involved in looking for rules
for divide, is_integer, and so on. We have found the only solution, and there is no
point in ever looking for another. If we did not put in the cut here, any backtracking
would eventually start finding alternatives for is_integer again. So we would carry

m
on generating possible values for Result again. None of these other values would be
the correct result of the division, and so we would continue generating indefinitely.

co
4.4 Problems with the Cut

a.
We have already seen that we must sometimes take into account the way Prolog
searches the database and what state of instantiation our goals will have in deciding

iy
the order in which to write the clauses of a Prolog program. The problem with in-
troducing cuts is that we have to be even more certain of exactly how the rules of
un
the program are to be used. For, whereas a cut when a rule is used one way can be
harmless or even beneficial, the very same cut can cause strange behaviour if the rule
is suddenly used in another way. Consider the modified append from the last section:
sD

append([], X, X) :- !.
append([A|B], C, [A|D]) :- append(B, C, D).

When we are considering goals like:


al

?- append([a,b,c],[d,e], X)
and
ri

?- append([a,b,c], X, Y)
to

the cut is quite appropriate. If the first argument of the goal already has a value, then
all the cut does is to reaffirm that only the first rule will be relevant if the value is [].
However, consider what happens if we have the goal
Tu

?- append(X, Y, [a,b,c]).
This goal will match the head of the first rule, giving:
X=[], Y=[a,b,c]
but now the cut is encountered. This will freeze all the choices we have made, and so
if we ask for another solution, the answer will be no even though there actually are
other solutions to the question.

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

4.4 Problems with the Cut 97

Here is another interesting example of what can happen if a rule containing a cut
is used in an unexpected way. Let us define a predicate number_of_parents, which
can express information about how many parents somebody has. We can define it as
follows:

number_of_parents(adam, 0) :- !.
number_of_parents(eve, 0) :- !.

m
number_of_parents(X, 2).

That is, the number of parents is 0 for adam and eve, but two for everybody else.

co
Now if we are always using our definition of number_of_parents to find the number
of parents of given people, this is fine. We will get

a.
?- number_of_parents(eve, X).
X=0 ;

iy no
?- number_of_parents(john, X).
un
X=2 ;
no

and so on, as required. The cut is necessary to prevent backtracking ever reaching the
sD

third rule if the person is adam or eve. However, consider what will happen if we use
the same rules to verify whether given people have given numbers of parents. All is
well, except that we get:
al

?- number_of_parents(eve, 2).
ri

yes

You should work out for yourself why this happens. It is simply a consequence of the
to

way Prolog searches through the database. Our implementation of “otherwise” with
a cut simply does not work properly any more. There are two possible modifications
we could make to recover from this:
Tu

number_of_parents(adam, N) :- !, N = 0.
number_of_parents(eve, N) :- !, N = 0.
number_of_parents(X, 2).

or

number_of_parents(adam, 0).

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc

98 Chapter 4 Backtracking and the “Cut”

number_of_parents(eve, 0).
number_of_parents(X, 2) :- \+(X=adam), \+(X=eve).

Of course, these will still not work properly if we give goals such as
?- number_of_parents(X, Y).
expecting backtracking to enumerate all the possibilities. So the moral is:

om
If you introduce cuts to obtain correct behaviour when the goals are of one
form, there is no guarantee that anything sensible will happen if goals of
another form start appearing.

c
It follows that it is only possible to use the cut reliably if you have a clear policy

a.
about how your rules are going to be used. If you change this policy, all the uses of
cut must be reviewed.

iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc
Chapter Topic

5 (5.1 - 5.3) Truth Maintenance System, Default Reasoning

6 (6.1 - 6.3) Probabilistic Reasoning: Bayesian Probabilistic


Inference, Possible World Representations

m
co
a.
TutorialsDuniya.com
iy
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with
un
Solution* etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


lsD

 Programming in Java  Discrete Structures


ia

 Data Structures  Operating Systems


 Computer Networks  Algorithms
or

 Android Programming  DataBase Management Systems


t
Tu

 PHP Programming  Software Engineering


 JavaScript  Theory of Computation
 Java Server Pages  Operational Research
 Python  System Programming
 Microprocessor  Data Mining
 Artificial Intelligence  Computer Graphics
 Machine Learning  Data Science
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Chapter Topic

Parsing Techniques, Context-Free and


12 (12.1 - 12.4) Transformational Grammars, Recursive and
Augmented Transition Nets

m
co
a.
TutorialsDuniya.com
iy
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with
un
Solution* etc of following subjects from https://www.tutorialsduniya.com.
lsD

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
ia

 Data Structures  Operating Systems


 Computer Networks  Algorithms
or

 Android Programming  DataBase Management Systems


t
Tu

 PHP Programming  Software Engineering


 JavaScript  Theory of Computation
 Java Server Pages  Operational Research
 Python  System Programming
 Microprocessor  Data Mining
 Artificial Intelligence  Computer Graphics
 Machine Learning  Data Science
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
sD
al
ri
to
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.

m
co
a.
iy
un
lsD
ia
t or
Tu

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.
TutorialsDuniya.com
Get FREE Compiled Books, Notes, Programs, Books, Question Papers with Solution*
etc of following subjects from https://www.tutorialsduniya.com.

 C and C++  Computer System Architecture


 Programming in Java  Discrete Structures
 Data Structures  Operating Systems

m
 Computer Networks  Algorithms
 Android Programming  DataBase Management Systems

co
 PHP Programming  Software Engineering

a.
 JavaScript  Theory of Computation
 Java Server Pages iy
 Operational Research
un
 Python  System Programming
 Microprocessor  Data Mining
lsD

 Artificial Intelligence  Computer Graphics


 Machine Learning  Data Science
ia
or

 Compiled Books: https://www.tutorialsduniya.com/compiled-books


t
Tu

 Programs: https://www.tutorialsduniya.com/programs
 Question Papers: https://www.tutorialsduniya.com/question-papers
 Python Notes: https://www.tutorialsduniya.com/python
 Java Notes: https://www.tutorialsduniya.com/java
 JavaScript Notes: https://www.tutorialsduniya.com/javascript
 JSP Notes: https://www.tutorialsduniya.com/jsp
 Microprocessor Notes: https://www.tutorialsduniya.com/microprocessor
 OR Notes: https://www.tutorialsduniya.com/operational-research

You might also like