Public TC Slides
Public TC Slides
Public TC Slides
Thinking as Computation
Hector J. Levesque Dept. of Computer Science University of Toronto
Thinking as Computation
c Levesque 2011
c Levesque 2011
Intelligent behaviour
In this course, we will consider what it takes to get a computer to engage in intelligent activities such as
understanding English sentences; recognizing objects in a visual scene; planning courses of actions; solving recreational puzzles; playing strategic games.
What these very different activities all have in common, is that when they are performed by people, they appear to require thought.
c Levesque 2011
found to be simplistic, misleading tell us very little about what we are, and how we do what we do
Why should we think computers are any different??
Chapter 1: Thinking and Computation c Levesque 2011 3
The brain
In this course, as in much of AI, we will be concerned with thinking (of various sorts), but have very little to say about the brain. Here is a useful analogy: the study of ight (before airplanes)
we might like to understand how animals like birds can y we might want to build machines that are capable of ight
Two possible strategies: 1. study birds, their feathers, muscles, very carefully and construct machines to emulate them 2. study aerodynamics: principles of ight applicable to anything We will be following the second strategy here (for thinking, not ight).
c Levesque 2011
Thinking
Q: What is thinking? A: Thinking is a process that occurs in the brain over time, but largely unconsciously. Lets look at thinking in action. . . Read this sentence: The trophy would not t into the brown suitcase because it was too small. Now answer this question: What was too small? What is the it ?
c Levesque 2011
c Levesque 2011
Computer Science
Its not about computers! Having problems with your PC? Dont ask a Computer Scientist!
The electronic / physical properties of computers play little or no role in Computer Science Another analogy to think about: musical instruments vs. music Like music, Computer Science is not about anything physical! Computer Science is about computation: a certain form of manipulation of symbols Modern electronic computers (like an Apple iMac or a Dell PC) just happen to provide a fast, cheap, and reliable medium for computation.
c Levesque 2011
z! 5
Manipulating symbols
The idea: take strings of symbols, break them apart, compare them, and reassemble them according to a recipe called a procedure. It is important to keep track of where you are, and follow the instructions in the procedure exactly. (You may not be able to gure why you are doing the steps involved!) The symbols you have at the start are called the inputs. Some of the symbols you end up with will be designated as the outputs. We say that the procedure is called on the inputs and returns or produces the outputs. Then construct more complex procedures out of simple procedures. In the next few slides, we will look at an interesting special case: arithmetic!
Chapter 1: Thinking and Computation c Levesque 2011 10
Arithmetic procedures
Imagine you are explaining to someone (a young child) how to do subtraction:
52 17
You might use words like this: First, you have to subtract 7 from 2. But since 7 is bigger than 2, you need to borrow 10 from the 5 on the left. That changes the 2 to a 12 and changes the 5 to a 4. So you subtract 7 not from 2 but from 12, which gives you 5, and you subtract 1 not from 5 but from 4 which gives you 3. So the answer is 35.
Chapter 1: Thinking and Computation c Levesque 2011 11
You will be given two digits as input and return two digits as output. (For example, given 7 and 6 as input, you will return 13 as output.) To do so, you will use a table which is on the next page. To add the two digits, nd the row for the rst digit on the table, and the column for the second digit, and return as output the two digit number at the intersection on the table.
c Levesque 2011
12
7 07 08 09 10 11 12 13 14 15 16
8 08 09 10 11 12 13 14 15 16 17
9 09 10 11 12 13 14 15 16 17 18
7 + 6 = 13
3 4 5 6 7 8 9
14 15
c Levesque 2011
13
Procedure PROC1
We can now start building on procedure PROC0 to do more. The rst thing we will need is to be able to add three digits (which will later allow us to handle carry digits). Here is a procedure PROC1 that takes three digits a, t, and b, as input, and returns two digits c and s. 1. Call PROC0 on t and b to get u and v. 2. Call PROC0 on a and v to get u and v . 3. If u = u = 0 then return with c as 0 and s as v . If u = u = 1 then return with c as 2 and s as v . Otherwise, return with c as 1 and s as v . Note that we do not say why we do any of this! (But do try it out!)
Chapter 1: Thinking and Computation c Levesque 2011 14
x1 x2 . . . xk y1 y2 . . . yk
output of PROC2: one string of digits of that length + 1
z0 z1 z2 . . . zk
For example, given 747 and 281 (with three digits each), PROC2 will return 1028 (with four digits).
c Levesque 2011
15
c Levesque 2011
16
Trying PROC2
We can trace what happens with PROC2 in detail. Lets look at what PROC2 does when the xi are 747 and the yi are 281. 1. First, we call PROC1 on 0, 7, 1. It will return 0 and 8. So z3 will be 8. 2. Next, we call PROC1 on 0, 4, 8. It will return 1 and 2. So z2 will be 2. 3. Then, we call PROC1 on 1, 7, 2. It will return 1 and 0. So z1 will be 0 and z0 will be 1. So PROC2 will return 1028, which is indeed the sum of 747 and 281. So even if you dont why you are doing all the steps, if you follow the directions exactly, you will be adding the numbers.
Chapter 1: Thinking and Computation c Levesque 2011 17
And on it goes . . .
Lets assume we already have procedures to do +, , , and , as well as string operations (e.g. u v means concatenate strings u and v). We can use these to dene still more complex procedures. On the next slide, we will consider a mystery procedure called PROCX that takes one positive integer x as input and returns a positive integer y as output. Try to gure out what this procedure does by trying it out on some sample inputs, for example, 137641. You should be able to follow the procedure without knowing what exactly you are supposed to be calculating.
c Levesque 2011
19
starting with very simple operations (such as table lookup), the ability to string and unstring symbols, compare them etc., we can build procedures to do addition using these we can build ever more complex procedures, to do multiplication, division, testing for prime numbers, etc. using pairs of numbers we can deal with fractions (rational numbers); and using numbers arranged into matrices, we can solve systems of equations, perform numerical simulations, etc.
As we will see in this course, there are also many interesting cases of symbol manipulation that are not numeric!
c Levesque 2011
21
A key observation
You dont need to know what youre doing! To get meaningful answers, you do not have to understand what the symbols stand for, or why the manipulations are correct. The symbols can be manipulated completely mechanically and still end up producing signicant and interesting results. This is the trick of computation:
We can get computers to perform a wide variety of very impressive activities precisely because we are able to describe those activities as a type of symbol manipulation that can be carried out purely mechanically.
This trick has turned out to be one of the major inventions of the 20th century. And note: It has nothing to do with electronics!
c Levesque 2011
22
not that the brain is something like an electronic computer (which it is in some ways, but in very many other ways is not) but that the process of thinking can be usefully understood as a form of symbol processing that can be carried out purely mechanically without having to know what you are doing.
This is what we will explore in this course!
c Levesque 2011
23
Thinking as computation
Some thinking clearly appears to be computation:
doing math homework lling out a tax form estimating a grocery bill
But much of our thinking seems to have very little to do with calculations or anything numerical. Unlike arithmetic, thinking seems to involve ideas about an unbounded variety of subjects. You can think about anything; but it is always about something. So in what sense can thinking be computational?
c Levesque 2011
24
An example
Consider the following: I know that my keys are either in my coat pocket or on the fridge. Thats where I always leave them. I feel my coat pocket and I see that there is nothing there. So my keys must be on the fridge. And thats where I should go looking. This is thinking that obviously has nothing to do with numbers. But it is clearly about something: my keys, pocket, refrigerator. Can we understand it as a form of computation?
c Levesque 2011
25
the rules of arithmetic allow us to deal with abstract numbers in terms of concrete symbols
manipulations on numeric symbols mirror relationships among the numbers being represented
the rules of logic allow us to deal with abstract ideas in terms of concrete symbols
manipulations on propositional symbols mirror relationships among ideas being represented
Chapter 1: Thinking and Computation c Levesque 2011 26
They are abstract entities (like numbers) but have special properties:
They are considered to hold or not hold. A sentence is considered to be true if the proposition it expresses holds. They are related to people in various ways: people can believe them, disbelieve them, fear them, worry about them, regret them, etc. They are related to each other in various ways: entail, provide evidence, contradict, etc.
Chapter 1: Thinking and Computation c Levesque 2011 27
A rst clue
We do not necessarily need to know what the terms in a sentence mean to be able to think about it. Example: The snark was a boojum. And now answer the following:
What kind of thing was the snark? Is there anything that was a boojum? Was the snark either a beejum or a boojum? Given that no boojum is every a beejum, was the snark a beejum?
We can still gure out appropriate answers using simple rules. Compare: the PROCX procedure!
c Levesque 2011
28
Compare to this:
Henry is in the basement or in the garden. Nobody is in the basement. So Henry is in the garden.
c Levesque 2011
29
blue thing is my keys or Henry or Jill green thing is in my coat pocket or in the basement or married to George
Note: The thinking is the same! The only thing that matters is that it is the same term (the blue thing) that is used in the rst and third sentences, etc.
Chapter 1: Thinking and Computation c Levesque 2011 30
Entailment
A collection of sentences, S1 , S2 , . . . Sn entail a sentence S if the truth of S is implicit in the truth of the Si . No matter what the terms in the Si really mean, if the Si sentences are all true, then S must also be true. For example, The snark was a boojum. Entails: Something was a boojum. My keys are in my coat pocket or on the fridge. and Entails: Nothing is in my coat pocket. My keys are on the fridge.
c Levesque 2011
31
Somebody is a bachelor. George is either a bachelor or a farmer. Not everyone is not a bachelor. It is not the case that George is not a bachelor. . . .
All true, but very very boring! But when we think about it, we think about
George (who we may know a lot about) being a bachelor (which we may know a lot about)
So our thinking appears to depend on what the terms mean!
Chapter 1: Thinking and Computation c Levesque 2011 32
The terms like George and bachelor and person and stamps appear in many places and link these sentences together.
Chapter 1: Thinking and Computation c Levesque 2011 33
19
igure 1.6.
Each sentence we believe is linked to many others by virtue of the terms that appear in them. It is the job of logic to crawl over this web looking for connections among the terms.
c Levesque crawl over this web looking for connecChapter Thinking and Computation hey use. The1:job of logical entailment is to2011 ons among the nodes, sensitive to the different types of links along the way. Looking t Figure 1.6, we see that there is a certain path from George to male, for example,
34
George has never been the groom at a wedding. Mary has an unmarried son born in Boston. No woman is the wife of any of Freds children.
The conclusions are much more like those made by ordinary people. We nd where the same term appears (like we did with the blue thing, the green thing etc.). We then apply simple rules of logic to draw conclusions. We still do not need to know what George or bachelor means! Now, the big step: Imagine drawing conclusions from millions of such facts.
Chapter 1: Thinking and Computation c Levesque 2011 35
Two hypotheses
1. Much of the richness of meaning that we experience during thinking may be accounted for in terms of simple symbolic manipulations over a rich collection of represented propositions 2. To build computers systems that are versatile, exible, modiable, explainable, . . . it is a good idea to
represent much of what the system needs to know as symbolic sentences perform manipulations over these sentences using the rules of symbolic logic to derive new conclusions have the system act based on the conclusions it is able to derive
Systems built this way are called knowledge-based systems and the collection of sentences is called its knowledge base (KB).
Chapter 1: Thinking and Computation c Levesque 2011 36
Summary
Thinking, as far as we are concerned, means bringing what you know to bear on what you are doing. But how does this work? How do concrete, physical entities like people engage with something formless and abstract like knowledge? The answer (via Leibniz) is that we engage with symbolic representations of that knowledge. We represent knowledge symbolically as a collection of sentences in a knowledge base, and then compute entailments of those sentences, as we need them. So computation over a knowledge base is the direction that we will be pursuing in this course, although we will only ever deal with tiny knowledge bases here.
Chapter 1: Thinking and Computation c Levesque 2011 37
c Levesque 2011
If-then reasoning
A special but very common case of a knowledge base is one consisting of just two sorts of sentences: 1. atomic sentences: simple basic sentences 2. conditional sentences: of the form If P1 and . . . and Pn then Q where the Pi and the Q are atomic sentences. The symbols If, and, and then are special keywords. The other symbols in the sentences are either variables (written here capitalized) or constants (written here uncapitalized).
So john and dog23 are constants. But X and Dog are variables.
Chapter 2: A Procedure for Thinking c Levesque 2011 2
A family example
Some atomic sentences:
john is a child of sue. jane is a child of sue. sue is a child of george. john is male. sam is male. john is a child of sam. jane is a child of sam. sue is a child of gina. june is female. george is male.
Some non-entailments
Here are some examples of atomic sentences that are not logically entailed by the KB:
gina is female
Although this may be true, there is nothing in the KB that forces it.
c Levesque 2011
Computing with a KB
Q: What sort of procedure do we use with a KB like this? A: The simplest is what is called back-chaining. It is used to establish an atomic sentence Q (called a query) = determine whether or not the query Q is entailed by the KB. For example, we will want to be able to establish the following:
jane is a child of sue. sue is a parent of jane.
since these are not entailed (even though they may very well be true).
Chapter 2: A Procedure for Thinking c Levesque 2011 6
Variables
Before we look at back-chaining in action, we need to deal with one complication: variables. Suppose the query Q is george is a parent of sue and we have a sentence in the KB like If . . . then Y is a parent of X. We will consider this to match at Step 2, but at Step 3 we need to remember that the Y we care about is george and the X is sue.
we want: we nd: If X is a child of Y george is a parent of sue (the Q) then Y is a parent of X
matches for Y=george and X=sue then we want: sue is a child of george (the P)
c Levesque 2011
Tracing
We now simulate the execution of the back-chaining procedure on some simple examples. This is called tracing the procedure. This is like what we did for arithmetic with the PROC2 example. Example 1: Establish jane is a child of sue 1. Look for jane is a child of sue in KB. Find it. Return success. Example 2: Establish gina is female 1. Look for gina is female in KB. Nothing found. 2. Look for If . . . then gina is female in KB. Nothing found. Return failure.
c Levesque 2011
Another example
Example 3: Establish george is a father of sue 1. Look for george is a father of sue in KB. Nothing found. 2. Look for If . . . then george is a father of sue in KB. Find If X is a child of Y and Y is male then Y is a father of X. This matches for Y=george and X=sue. 3. Establish sue is a child of george (Why?) 1. Look for sue is a child of george in KB. Find it. Return success.
Establish jane is female. << Stuff left out >> Return success.
Return success overall.
Chapter 2: A Procedure for Thinking c Levesque 2011 11
Queries without variables can be thought of as yes-no-questions. Queries with variables can be thought of as WH-questions.
c Levesque 2011
12
Two complications
This leads to two new complications in the back-chaining procedure: 1. The question: What if the same variable appears in both the KB and in the Q? How to keep straight which is which? The answer: Rename the variables from the sentence in the KB before using it so that this does not happen. Note: If U is a child of V then V is a parent of U says exactly the same thing as If X is a child of Y then Y is a parent of X. 2. The question: What if I choose a value for a variable, but then cannot establish the query that results? The answer: You try the rst value you nd, but if it does not work, you will need to go back and reconsider other possible values. Note: This is just like the choice of conditional sentence in Step 2.
Chapter 2: A Procedure for Thinking c Levesque 2011 13
Want: Y is a parent of John (where Y=sue) Want: John is a child of Y (where Y=sue)
This holds. So we get: Y is a parent of John (where Y=sue) So we get: George is a grandfather of John
Chapter 2: A Procedure for Thinking c Levesque 2011 15
A nal example
Example 6: Establish G is a grandfather of john 1. Look for G is a grandfather of john in KB. Nothing found. 2. Look for If . . . then G is a grandfather of john in KB. Find If X is a father of Y and Y is a parent of Z then X is a grandfather of Z. 3.
We will eventually fail with this Y and G, and must consider other choices! Eventually we will nd sue is a child of george and then succeed as before.
c Levesque 2011
16
it is goal-directed: You work your way back from what you are trying to establish towards what you know. it is sound: Anything that can be established is entailed by the KB. it is (sometimes) complete: Back-chaining does not miss any entailments provided that it does not get stuck in a loop.
Consider this KB: do is a collie. If X is a poodle then X is a dog. If X is a poodle then X is a poodle. If X is a collie then X is a dog.
c Levesque 2011
Prolog in perspective
PROLOG = PROgramming in LOGic. Invented in 1971 by a Frenchman, Alain Colmeraurer, while visiting at the University of Montreal, for the purpose of natural language parsing. Simultaneously invented by an American, Carl Hewitt, at MIT in Boston (under the name Planner there). Mainly developed and promoted in Europe, especially Britain and France. Now a commercial product, with many available systems.
In this course: a system called SWI-Prolog.
Radically different concept of programming from more conventional computer languages such as C++, Java, Python etc. Now widely used for AI applications, as well as many other uses.
c Levesque 2011
Prolog programs
Prolog programs are simply knowledge bases consisting of atomic and conditional sentences as before, but with a sightly different notation. Here is the family example as a Prolog program:
family.pl
% This is the Prolog version of the family example child(john,sue). child(john,sam). child(jane,sue). child(jane,sam). child(sue,george). child(sue,gina). male(john). female(sue). male(sam). female(jane). male(george). female(june).
parent(Y,X) :- child(X,Y). father(Y,X) :- child(X,Y), male(Y). opp_sex(X,Y) :- male(X), female(Y). opp_sex(Y,X) :- male(X), female(Y). grand_father(X,Z) :- father(X,Y), parent(Y,Z).
Atomic sentences
The atomic sentences or atoms of Prolog have the following form: predicate(term1 , . . . , termk ) where the predicate is a constant and the terms are either constants or variables. Note the punctuation:
immediately after the predicate, there must be a left parenthesis; between each term, there must be a comma; immediately after the last term, there must be a right parenthesis.
The number of terms k is called the arity of the predicate. If k = 0, the parentheses can be left out.
Chapter 3: The Prolog Language c Levesque 2011 5
Conditional sentences
The conditional sentences of Prolog have the following form: head : body1 , . . . , bodyn where the head and each element of the body is an atom. Note the punctuation:
immediately after the head, there must be a colon then hyphen, :-. between each element of the body, there must be a comma.
If n = 0, the :- should be omitted. In other words, an atomic sentence is just a conditional sentence where the body is empty!
c Levesque 2011
And a longer one still is the family example from before. Typically, a program is stored in a le with a name like family.pl.
Chapter 3: The Prolog Language c Levesque 2011 7
c Levesque 2011
?- [family]. This loads the family.pl le % family compiled 0.00 sec, 2,724 bytes Yes ?- father(sam,jane). Yes ?- halt. [skywolf]
This is a query to Prolog Prologs answer is yes. This is how we exit Prolog Success!
Simple queries
In its simplest form, a query is just an atom, with or without variables, and terminated with a period. Posing a query is asking Prolog to establish the atom using back-chaining, just as we did by hand before. If the query has no variables, there are three possible outcomes: 1. Prolog answers Yes (or true): the atom can be established;
See the query father(sam,jane) on the previous slide.
2. Prolog answers No (or false): the atom cannot be established, and Prolog runs out of alternatives to try; 3. Prolog does not answer: the atom cannot be established, and Prolog continues to try alternatives.
Chapter 3: The Prolog Language c Levesque 2011 10
type a space or return, in which case, Prolog answers Yes and the query is done; type a semi-colon, in which case, Prolog tries to nd new values for the variables, with the same three possible outcomes.
c Levesque 2011
11
c Levesque 2011
12
Conjunctive queries
More generally, queries can be sequences of atoms separated by commas and again terminated by a period. These queries are understood conjunctively, just like the body of a conditional sentence. (So , plays the role of and.)
?- female(F), parent(sam,F). F = jane ; No
This asks for an F such that F is female and Sam is a parent of F . In other words: Q: Who is a female that Sam is a parent of? A: Jane. And this is the only value that can be found.
Chapter 3: The Prolog Language c Levesque 2011 13
Negation in queries
The special symbol \+ (meaning not) can also be used in front of an atom in a query to ip between Yes and No.
?- child(john,george). No ?- \+ child(john,george). Yes ?- parent(X,john), female(X). X = sue Yes ?- parent(X,john), \+ female(X). X = sam Yes
Is John a child of George
parent(X,john), \+ female(X).
1. We start by replacing variables in the query by new names to make sure they do not conict with variables in the Prolog program.
\+ female(sue).
. . . continued
Chapter 3: The Prolog Language c Levesque 2011 15
4. We start working on the query \+ female(sue). Since this is a negated query, we consider the unnegated version. We start working on the query female(sue), and nd the clause in the program. We return success. Since the unnegated query succeeds, the negated query fails. We go back to our most recent choice point (at step 3) and reconsider. 5. We return to the query child(john, G312), and this time, we nd the clause child(john,sam). This leaves us with:
\+ female(sam).
6. We start working on the query \+ female(sam). Since this is a negated query, we try the unnegated version. We start working on the query female(sam), but we cannot nd anything that matches. We return failure. Since the unnegated query fails, the negated query succeeds. This leaves us nothing left to do so. 7. We return success, noting that the value of X is sam.
Chapter 3: The Prolog Language c Levesque 2011 16
There are 4 types of entries: Call: start working on an atomic query; Exit: success on an atomic query; Fail: failure on an atomic query; Redo: go back to a choice point and reconsider.
c Levesque 2011
17
Moral: When using a negated query with variables, the variables should already have tentative values from earlier queries.
Chapter 3: The Prolog Language c Levesque 2011 18
These answers mean that the values are equal but otherwise unconstrained.
19
Using equality
You should never have to use an unnegated equality predicate: Instead of: child(john,X), child(jane,Y), X=Y. use: child(john,X), child(jane,X). However, negated equality does come in handy:
?- parent(sam,X), \+ X=john. X = jane Is Sam the parent of someone other than John? Yes ?- male(X), male(Y), male(Z), \+ X=Y, \+ X=Z, \+ Y=Z. X = john Are there at least 3 males? Y = sam Z = george Yes
Review: terms
Before looking at Prolog programs in more detail, we review what we have seen so far, with a few minor extensions.
A constant is one of the following: a string of characters enclosed within single quotes. a lower case letter optionally followed by letters, digits and underscores. A variable is an upper case letter optionally followed by letters, digits and underscores. A number is a sequence of one or more digits, optionally preceded by a minus sign, and optionally containing a decimal point. A term is a constant, variable, or number.
c Levesque 2011
21
Back-chaining, revisited
Having looked at Prolog queries in some detail, we now examine Prolog back-chaining, starting with a very simple program.
likes.pl
% This is a program about who likes likes(john,pizza). likes(john,sushi). likes(mary,sushi). likes(paul,X) :- likes(john,X). likes(X,icecream).
what kinds of food. % John likes pizza. % John likes sushi. % Mary likes sushi. % Paul likes what John likes. % Everybody likes ice cream.
We start by looking at how we decide which clauses to use in answering queries. The way clauses are selected during back-chaining is through a matching process called unication.
c Levesque 2011
23
Unication
Two atoms with distinct variables are said to unify if there is a substitution for the variables that makes the atoms identical.
a query such as likes(john,Y) unies with likes(john,pizza) in the rst clause of the program, for Y=pizza. a query such as likes(paul,pizza) unies with likes(paul,X) in the fourth clause, for X=pizza. a query such as likes(jane,Y) unies with likes(X,icecream) in the last clause, for X=jane and Y=icecream.
In all cases, the queries will eventually succeed! Note that both the query and the head of a clause from the program may contain variables.
Chapter 3: The Prolog Language c Levesque 2011 24
Examples of unication
The following pairs of atoms unify:
p(b,X,b) and p(Y,a,b) for X=a and Y=b p(X,b,X) and p(a,b,Y) for X=a and Y=a p(b,X,b) and p(Y,Z,b) for X=Z and Y=b p(X,Z,X,Z) and p(Y,W,a,Y) for X=a, Z=a, Y=a, and W=a.
The following pairs of atoms do not unify:
p(b,X,b) and p(b,Y) p(b,X,b) and p(Y,a,a) p(X,b,X) and p(a,b,b) p(X,b,X,a) and p(Y,Z,Z,Y)
Chapter 3: The Prolog Language c Levesque 2011 25
Renaming variables
Unication is not concerned with where the two atoms come from (which one is from a query, and which one is from a program). However, during back-chaining, Prolog renames the variables in the clauses of the program before attempting unication.
Example: Consider the query likes(X,pizza), \+ X=john. Prolog rst nds likes(john,pizza), but this eventually fails. It eventually considers the clause whose head is likes(paul,X), but this cannot unify with likes(X,pizza) as is. So Prolog renames the variable X in that clause to a totally new variable, for example, X1. Then likes(X,pizza) unies with likes(paul,X1), and the query goes on to eventually succeed.
c Levesque 2011
26
c Levesque 2011
c Levesque 2011
B3 B4 B1 B2 B5 B6 B7
We would like to describe the scene and get Prolog to determine that
Block 3 is above Block 5 Block 1 is to the left of Block 7 Block 4 is to the right of Block 2
Chapter 4: Writing Prolog Programs c Levesque 2011 3
% on(X,Y) means that block X is directly on top of block Y. on(b1,b2). on(b3,b4). on(b4,b5). on(b5,b6). % just left(X,Y) means that blocks X and Y are on the table % and that X is immediately to the left of Y. just_left(b2,b6). just_left(b6,b7). % above(X,Y) means that block X is somewhere above block Y % in the pile where Y occurs. above(X,Y) :- on(X,Y). above(X,Y) :- on(X,Z), above(Z,Y). % left(X,Y) means that block X is somewhere to the left % of block Y but perhaps higher or lower than Y. left(X,Y) :- just_left(X,Y). left(X,Y) :- just_left(X,Z), left(Z,Y). left(X,Y) :- on(X,Z), left(Z,Y). % leftmost is on something. left(X,Y) :- on(Y,Z), left(X,Z). % rightmost is on something. % right(X,Y) is the opposite of left(X,Y). right(Y,X) :- left(X,Y).
1 2 4 5 6 8 9 10 11 13 14 15 16 17 18 20 21
Note: The small line numbers displayed here are not part of Prolog.
Chapter 4: Writing Prolog Programs c Levesque 2011 4
Yes ?T T T T T T T T T T T above(b3,b5). Call: (8) above(b3, b5) Call: (9) on(b3, b5) Fail: (9) on(b3, b5) Redo: (8) above(b3, b5) Call: (9) on(b3, _L205) Exit: (9) on(b3, b4) Call: (9) above(b4, b5) Call: (10) on(b4, b5) Exit: (10) on(b4, b5) Exit: (9) above(b4, b5) Exit: (8) above(b3, b5)
% % % % % % % % % % %
The main query - Try on(b3,b5) This fails Reconsider - Try on(b3,Z) from line 11 This succeeds for Z=b4 - Now try above(Z,b5) for Z=b4 - Try on(b4,b5) This succeeds This succeeds The main query succeeds
Yes
c Levesque 2011
Recursion
The example on the previous slide makes use of recursion. A recursive clause is one in which the predicate of the head is the same as a predicate mentioned in the body. Line 11 in the program:
x z y
Most modern programming languages (Java, Python, etc.) provide recursion, which is usually considered to be an advanced technique. In fact, it is really quite simple and lies at the heart of Prolog programming.
c Levesque 2011
c Levesque 2011
above(X,Y) :- on(X,Y).
2. assume the case for k = n has already been taken care of, and write clauses to handle the case where k = (n + 1); For the predicate above, suppose the number k = (n + 1). Then the x must be directly on some other block z, where there are only n intermediate blocks between this z and y. So we assume that the predicate already works for z and y, and use that to write a clause for x and y.
c Levesque 2011
Yes
Chapter 4: Writing Prolog Programs c Levesque 2011 9
Non-terminating programs
Using recursion, it is possible to write programs that go on forever. For example, instead of line 11, suppose we had written this:
above(Z0,b1), on(b2,Z0)
c Levesque 2011
10
Avoiding non-termination
When writing recursive programs, there is no simple way to guarantee that they will terminate. However, a good rule of thumb is that when a clause is recursive, the body should contain at least one atom before the recursive one to provide a new value for one of the variables. Instead of we should write
These two mean the same thing in English, and together with line 10, they correctly characterize the predicate above. But because of the left-to-right order of Prolog, the second one will try the recursive predicate only after it has found a value for the variable Z.
Chapter 4: Writing Prolog Programs c Levesque 2011 11
Excessive search
A trickier problem is seen with the query left(b3,b3). A trace of it starts as follows
?T T T T T T T T T T T T T left(b3,b3). Call: (7) left(b3, b3) Call: (8) just_left(b3, Fail: (8) just_left(b3, Redo: (7) left(b3, b3) Call: (8) just_left(b3, Fail: (8) just_left(b3, Redo: (7) left(b3, b3) Call: (8) on(b3, _L205) Exit: (8) on(b3, b4) Call: (8) left(b4, b3) Call: (9) just_left(b4, Fail: (9) just_left(b4, Redo: (8) left(b4, b3) % The main query b3) b3) % Reconsider _L205) _L205) % Reconsider again % Try on(b3,Z) % b3) b3) % Reconsider ... Then start left(b4,b3)
and continues this way for a total of 1500 lines before nally failing!
c Levesque 2011
12
for (3,3), we also need to try (4,3) and (3,4); for (4,3), we also need to try (5,3) and (4,4); for (3,4), we also need to try (4,4) and (3,5).
So (4,4) gets considered twice, (5,5) gets considered four times, (6,6) gets considered eight times, and so on.
c Levesque 2011
13
Algorithms
There is no easy way to write programs and be sure that they are not doing excessive work. A good part of Computer Science involves analyzing computational problems and coming up with effective ways of solving them using computers. = comparing different algorithms for solving a problem. Each problem has to be approached on its own terms. In Prolog, we would need to think about what sorts of approaches will allow back-chaining to do its job effectively. In this course, we will not spend a lot of time worrying about algorithms!
c Levesque 2011
14
nd the block below X that is on the table; call it X1; nd the block below Y that is on the table; call it Y1; test whether X1 is to the left of Y1 using only just left.
Here is a fragment of a Prolog program to do this:
left(X,Y) :- bottom(X,X1), bottom(Y,Y1), table_left(X1,Y1). bottom(X,X) :- \+ on(X,Y). % Note the use of \+ ! bottom(X,X1) :- on(X,Y), bottom(Y,X1). table_left(X,Z) :- just_left(X,Z). table_left(X,Z) :- just_left(X,Y), table_left(Y,Z).
c Levesque 2011
15
Thinking revisited
In this course we will look at a number of activities that require thought. Unfortunately, a lot of our own thinking happens so quickly that we have a hard time describing what is actually taking place! For example, guring out what the it means in The trophy would not t into the brown suitcase because it was too small. One example where this is not the case is when we are solving puzzles like those that appear in the recreational section of a newspaper. What we will see is that the thinking needed to solve a wide variety of these reasoning puzzles can be formulated in the same way, in terms of what we will call constraint satisfaction problems.
c Levesque 2011
Constraint satisfaction
Many challenging tasks that appear to require thinking have this form:
we guess at the value of one or more variables; we conrm that we are satised with the values we get; we backtrack and guess at new values if we need to.
We have already seen this behaviour in answering conjunctive queries:
?- male(X), child(john,X).
generate an X that is male: the rst one we nd is john. test that this X satises child(john,X). It does not. So backtrack. generate another X that is male: the next one we nd is sam. test that this X satises child(john,X). It does. We are done.
Chapter 5: Satisfying Constraints c Levesque 2011 4
1. Colouring a map
Five countries: A, B, C, D, E Three colours: red, white, blue Constraint: Neighbouring countries must have different colours on the map.
map.pl
% solution(A,B,C,D,E) holds if A,B,C,D,E are colors % that solve a map-coloring problem from the text. solution(A,B,C,D,E) :color(A), color(B), color(C), color(D), color(E), \+ A=B, \+ A=C, \+ A=D, \+ A=E, \+ B=C, \+ C=D, \+ D=E. % The three colors are these. color(red). color(white). color(blue).
Then we get:
?- solution(A,B,C,D,E). A=red B=white C=blue D=white E=blue % + other solutions too
c Levesque 2011
c Levesque 2011
solution(Variable1 , . . . , Variablen ) :domain1 (Variable1 ), ... domainn (Variablen ), constraint1 (Variable, . . . ,Variable), ... constraintm (Variable, . . . ,Variable). Sometimes the generation is interleaved with the testing.
Chapter 5: Satisfying Constraints c Levesque 2011 7
Output in Prolog
It is often convenient to be able to produce output in Prolog other than just the values of variables. Prolog provides two special atoms for queries or bodies of clauses:
write(term)
always succeeds and has the effect of printing the term
nl
always succeeds and has the effect of starting a new line
?- X=blue, nl, write(The value of X is ), write(X), write(, I believe,), nl, write( and that is it!). The value of X is blue, I believe, and that is it! X = blue Yes
c Levesque 2011
), ), ), ), ),
Then we get:
?- print_colours. Country Country Country Country Country Yes A B C D E is is is is is coloured coloured coloured coloured coloured % Note that the query now has no variables red white blue white blue
c Levesque 2011
2. Mini Sudoku
We now consider a small version of the Sudoku puzzle. The problem: We are given a 4 4 grid, where some of the entries are blank and some contain numbers between 1 and 4. Our job is to ll in all of the entries with numbers between 1 and 4 so that
the numbers in each row (1,2,3,4) are unique, the numbers in each column (1,2,3,4) are unique, the numbers in each square (NW,NE,SW,SE) are unique.
c Levesque 2011
10
Note each occurrence of the anonymous variable can be for a different value. So the last query above behaves like likes(X,Y).
Chapter 5: Satisfying Constraints c Levesque 2011 11
Sudoku queries
What we would like is to provide a partial solution as a query and have Prolog print out a complete solution.
?- sudoku( 1, 4, _, _, _, 4, 2, _, _, _, _, _, ). % Note the use of the anonymous _ variable. _, _, _, 3
So the sudoku predicate will take 16 arguments, each representing a cell of the puzzle.
Chapter 5: Satisfying Constraints c Levesque 2011 12
We will use variants of this idea over and over for different problems. In the case of Sudoku, we also want P, Q, R, S to be numbers from 1 to 4. Here is the full program (its the longest one we will see!) . . .
Chapter 5: Satisfying Constraints c Levesque 2011 13
.
sudoku.pl
. % The main predicate. Solve the puzzle and print the answer. % The variable Rij stands for the number in row i and column j. sudoku(R11,R12,R13,R14,R21,R22,R23,R24,R31,R32,R33,R34, R41,R42,R43,R44) :solution(R11,R12,R13,R14,R21,R22,R23,R24,R31,R32,R33,R34, R41,R42,R43,R44), nl, write(A solution to this puzzle is), nl, printrow(R11,R12,R13,R14), printrow(R21,R22,R23,R24), printrow(R31,R32,R33,R34), printrow(R41,R42,R43,R44). % Print a row of four numbers with spaces between them. printrow(P,Q,R,S) :- write( ), write(P), write( ), write(Q), write( ), write(R), write( ), write(S), nl. %-----------------------------------------------------------------solution(R11,R12,R13,R14,R21,R22,R23,R24,R31,R32,R33,R34, R41,R42,R43,R44) :uniq(R11,R12,R13,R14), uniq(R21,R22,R23,R24), % rows 1,2 uniq(R31,R32,R33,R34), uniq(R41,R42,R43,R44), % rows 3,4 uniq(R11,R21,R31,R41), uniq(R12,R22,R32,R42), % cols 1,2 uniq(R13,R23,R33,R43), uniq(R14,R24,R34,R44), % cols 3,4 uniq(R11,R12,R21,R22), uniq(R13,R14,R23,R24), % NW and NE uniq(R31,R32,R41,R42), uniq(R33,R34,R43,R44). % SW and SE % uniq holds if P,Q,R,S are all distinct nums (from 1 to 4). uniq(P,Q,R,S) :- num(P), num(Q), num(R), num(S), \+ P=Q, \+ P=R, \+ P=S, \+ Q=R, \+ Q=S, \+ R=S. % The four numbers to go into each cell num(1). num(2). num(3). num(4).
c Levesque 2011
14
But will this work? in principle: denitely yes! in practice: denitely no! The obvious 9 9 version did not terminate running Prolog on a Mac G5 over a full weekend (66 hours)! Q: What is the problem? A: The 9 9 Sudoku is much larger than the 4 4 one!
c Levesque 2011
15
Search space
One useful measure of the size of a constraint satisfaction problem is the size of the search space. Search Space: the different ways variables can be assigned values from their domains before considering the constraints. For the mini Sudoku: each of 16 variables can take on 4 values. So there are this many possibilities: 4 4 = 416 which is about 4 109 (4 billion). This is considered small! For the full Sudoku: each of 81 variables can take on 9 values. So there are this many possibilities: 9 9 = 981 which is about 2 1077 This is enormous!
Chapter 5: Satisfying Constraints c Levesque 2011 16
Big numbers
Q: Are super fast computers able to explore these search spaces? A: No!
Suppose we have a computer that can explore 1 billion possibilities in the search space per second. (No existing computer can do this.) Suppose we magically make it one billion times faster. (Things now get done in under 1 second that would have taken over 10 years) Suppose we have 1 billion of these super fast computers all working together, sharing the job of exploring the search space.
Q: How many possibilities would we now be able to handle per second? A: 109 109 109 = 1027 Q: How long would it take to go through all of Sudoku? A: 1077 /1027 = 1050 seconds = 3 1040 centuries!
Chapter 5: Satisfying Constraints c Levesque 2011 17
Sudoku experts do not think this way. They do very little guessing, and only for the most difcult of the puzzles. Instead they repeatedly try to see if the constraints force values for certain entries based on other known values. Programming this type of algorithm for Sudoku would take some work. But we can examine the basic idea by looking at another constraint satisfaction problem.
c Levesque 2011
18
Numbers as terms
Before we go on to look at another constraint satisfaction problem in Prolog, we need to examine numbers in Prolog. Since numbers are terms, they can appear in programs and queries anywhere a constant or variable can appear (as we saw in Sudoku). So we can have a Prolog program that contains clauses like this:
age(donna,23). age(andy,22). current_temperature(-5).
19
Numeric expressions
In addition, Prolog provides some special arithmetic operations that can appear in queries or in the bodies of clauses:
less than greater than less than or equal greater than or equal equal equal
The Ei here are numeric expressions, that is, formulas built out of numbers and variables using +, -, *, / and other operations. Here is an example expression: sqrt(3*(X+Y)/2).
Chapter 5: Satisfying Constraints c Levesque 2011 20
Variables in expressions
The arithmetic operations above require the variables that appear in the expressions to already have values that are numbers. Giving a variable a value is called instantiating the variable.
?- X = 4, X > 2. X = 4 Yes ?- X = 4, X+5 =:= (X-1)**2. X = 4 Yes ?- X > 2, X = 4. ERROR: Arguments are not sufficiently instantiated ?- X = Y, X > 2, Y = 4. ERROR: Arguments are not sufficiently instantiated
Note: expressions are not terms, and so cannot be used as arguments to predicates. this is ok: ?- q(Y), X is Y+4, p(X,Z). this is not ok: ?- q(Y), p(Y+4,Z). Using is is the typical way of getting the value of an arithmetic expression to use with another predicate.
Chapter 5: Satisfying Constraints c Levesque 2011 22
Arithmetic programs
Using the arithmetic operations, we can write Prolog programs that perform numeric calculations. Suppose we have the following sorts of clauses in a program:
birth_year(donna,1986). birth_year(andy,1987). current_year(2009).
c Levesque 2011
23
More arithmetic
Although this will not be the focus in this course, we can also write ordinary numerical procedures in Prolog. Here is how we would compute n! = n (n 1) . . . 2 1. We want a predicate factorial so that the query factorial(5,X) succeeds with X=120 (since 5 4 3 2 1 = 120). Heres the entire program:
% factorial(N,X) holds when X = N! % This recursive program uses the property % that if n > 1, then n! = n * (n-1)! factorial(1,1). % The base case factorial(N,X) :N > 1, % Note: the N must be instantiated N1 is N-1, factorial(N1,X1), % Get (N-1)! recursively X is N*X1. % N! = N*(N-1)!
c Levesque 2011
24
3. Cryptarithmetic
Cryptarithmetic puzzles are puzzles of the form
SEND + MORE -----MONEY % Each letter stands for a distinct digit % Leading digits must not be 0
Variables: S , E , N , etc. and carry digits Domains: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} for digits and {0, 1} for carry digits Constraints: unique digits, S > 0, M > 0, and
% solution(...) holds for a solution to SEND+MORE=MONEY. solution(S,E,N,D,M,O,R,Y) :uniq_digits(S,E,N,D,M,O,R,Y), S > 0, M > 0, Y is (D+E) mod 10, C1 is (D+E) // 10, E is (N+R+C1) mod 10, C10 is (N+R+C1) // 10, N is (E+O+C10) mod 10, C100 is (E+O+C10) // 10, O is (S+M+C100) mod 10, M is (S+M+C100) // 10. % uniq(...) holds if the arguments uniq_digits(S,E,N,D,M,O,R,Y) :dig(S), dig(E), dig(N), dig(D), \+ S=E, \+ S=N, \+ S=D, \+ S=M, \+ E=N, \+ E=D, \+ E=M, \+ N=D, \+ N=M, \+ D=M, are all distinct digits. dig(M), \+ S=O, \+ E=O, \+ N=O, \+ D=O, \+ M=O, dig(O), \+ S=R, \+ E=R, \+ N=R, \+ D=R, \+ M=R, \+ O=R, dig(R), dig(Y), \+ S=Y, \+ E=Y, \+ N=Y, \+ D=Y, \+ M=Y, \+ O=Y, \+ R=Y.
% The digits dig(0). dig(1). dig(2). dig(3). dig(4). dig(5). dig(6). dig(7). dig(8). dig(9).
c Levesque 2011
26
However there is a problem: It takes over 90 seconds to nd the solution, even on a very fast computer. (Try it!) Can we do better? We must try to minimize the guessing.
Chapter 5: Satisfying Constraints c Levesque 2011 27
The rst version has a search space of 10 10 10 = 1000. The second has a search space of 10 10 = 100.
c Levesque 2011
28
we should use
dig(A), dig(B), A > B, dig(C), dig(D), % guess at A and B % test if A > B % guess at C and D
In the rst version, if we guess badly for A and B, we only get to reconsider after we have gone through all the values for C and D. In the second version, if we guess badly for A and B, we reconsider immediately, before we consider any values for C and D.
Chapter 5: Satisfying Constraints c Levesque 2011 29
% solution(...) holds for a solution to SEND+MORE=MONEY. solution(S,E,N,D,M,O,R,Y) :dig(D), dig(E), Y is (D+E) mod 10, C1 is (D+E) // 10, dig(N), dig(R), E is (N+R+C1) mod 10, C10 is (N+R+C1) // 10, dig(E), dig(O), N is (E+O+C10) mod 10, C100 is (E+O+C10) // 10, dig(S), S > 0, dig(M), M > 0, O is (S+M+C100) mod 10, M is (S+M+C100) // 10, uniq_digits(S,E,N,D,M,O,R,Y). % The rest of the program is as before.
It gives the same answer as before, but this time in .07 seconds! Can we do even better?
Chapter 5: Satisfying Constraints c Levesque 2011
c Levesque 2011
31
d
Q
d d
We can reasonably assume that each row will have one queen. So the job is to choose a column for each queen
Variables: C1 , C2 , C3 , C4 , C5 , C6 , C7 , C8 , where Ci is the location for the queen in row i. Domains: the columns 1, 2, 3, 4, 5, 6, 7, 8. So C5 = 3 would mean that the queen in row 5 goes in column 3
Chapter 5: Satisfying Constraints c Levesque 2011 32
What is a diagonal?
Observe:
along a left diagonal, (row column) stays constant along a right diagonal, (row + column) stays constant
row col = 2
d d
3,1 4,2 5,3 6,4 7,5 8,4 8,6 6,6 5,7 4,8
row + col = 12
Each left diagonal and right diagonal has its own constant
c Levesque 2011
33
Capturing a queen
A queen on (row1 , col1 ) can capture a queen on (row2 , col2 ) iff any of the following holds:
they are on the same row: row1 = row2 they are on the same column: col1 = col2 they are on the same left diagonal: row1 col1 = row2 col2 they are on the same right diagonal: row1 + col1 = row2 + col2
In Prolog:
% cap(R1,C1,R2,C2): a queen on (R1,C1) can capture one on (R2,C2). cap(R,_,R,_). % Note the use of the _ variable cap(_,C,_,C). % Here too. cap(R1,C1,R2,C2) :- R1-C1 =:= R2-C2. cap(R1,C1,R2,C2) :- R1+C1 =:= R2+C2.
c Levesque 2011
34
% Solve the 8-queens problem. solution(C1,C2,C3,C4,C5,C6,C7,C8) :col(C1), col(C2), \+ cap(2,C2,1,C1), col(C3), \+ cap(3,C3,1,C1), \+ cap(3,C3,2,C2), col(C4), \+ cap(4,C4,1,C1), \+ cap(4,C4,2,C2), \+ cap(4,C4,3,C3), col(C5), \+ cap(5,C5,1,C1), \+ cap(5,C5,2,C2), \+ cap(5,C5,3,C3), \+ cap(5,C5,4,C4), col(C6), \+ cap(6,C6,1,C1), \+ cap(6,C6,2,C2), \+ cap(6,C6,3,C3), \+ cap(6,C6,4,C4), \+ cap(6,C6,5,C5), col(C7), \+ cap(7,C7,1,C1), \+ cap(7,C7,2,C2), \+ cap(7,C7,3,C3), \+ cap(7,C7,4,C4), \+ cap(7,C7,5,C5), \+ cap(7,C7,6,C6), col(C8), \+ cap(8,C8,1,C1), \+ cap(8,C8,2,C2), \+ cap(8,C8,3,C3), \+ cap(8,C8,4,C4), \+ cap(8,C8,5,C5), \+ cap(8,C8,6,C6), \+ cap(8,C8,7,C7). % The columns col(1). col(2). col(3). col(4). col(5). col(6). col(7). col(8). % cap(R1,C1,R2,C2): a queen on (R1,C1) can capture one on (R2,C2). cap(R,_,R,_). % Note the use of the _ here cap(_,C,_,C). % and here, too. cap(R1,C1,R2,C2) :- R1-C1 =:= R2-C2. cap(R1,C1,R2,C2) :- R1+C1 =:= R2+C2.
c Levesque 2011
35
Q Q Q Q Q Q Q Q
This is the rst solution found. This means that while (2,3) is safe from (1,1), the remaining six queens cannot be safely placed on the board if there is a queen on (1,1) and (2,3). Similarly (2,4) is a dead end, and only (2,5) leads to a solution (requiring (3,8), even though (3,2) and (3,7) are both safe at that point).
Chapter 5: Satisfying Constraints c Levesque 2011 36
5. Logic puzzles
Example:
In conversation, Chris, Sandy and Pat discovered that they had distinct occupations and played distinct musical instruments. Also 1. Chris is married to the doctor. 2. The lawyer plays the piano. 3. Chris is not the engineer. 4. Sandy is a patient of the violinist. Who plays the ute?
To solve puzzles like these, we need to determine what the variables are, what values they can have, and how the given information leads to constraints on possible solutions.
c Levesque 2011
37
c Levesque 2011
38
% A logic puzzle involving jobs and musical instruments, solution(Flute) :% Distinct occupations and instruments uniq_people(Doctor,Lawyer,Engineer), uniq_people(Piano,Violin,Flute), % The four clues \+ chris = Doctor, Lawyer = Piano, \+ Engineer = chris, Violin = Doctor, \+ sandy = Violin. % % % % % Chris is married to the doctor. The lawyer plays the piano. The engineer is not Chris. Sandy is a patient of the violinist.
% uniq(...) is used to generate three distinct people. uniq_people(A,B,C) :- person(A), person(B), person(C), \+ A=B, \+ A=C, \+ B=C. % The three given people person(chris). person(sandy). person(pat).
c Levesque 2011
39
Hidden variables
Sometimes the only way to express the constraints is to imagine that there are variables other than the ones mentioned in the puzzle.
cf. the carry digits in cryptarithmetic
Example: as before with Chris, Sandy, and Pat, except that (3) is
3. Pat is not married to the engineer.
It is useful to think of terms of new variables, for Chris spouse, Sandys spouse, and Pats spouse.
% A second logic puzzle involving jobs and musical instruments solution(Flute) :uniq_people(Doctor,Lawyer,Engineer), uniq_people(Piano,Violin,Flute), % Generate values for the three spouse variables. spouses(Chris_spouse,Sandy_spouse,Pat_spouse), Chris_spouse = Doctor, Lawyer = Piano, \+ Pat_spouse = Engineer, Violin = Doctor, \+ sandy = Violin. uniq_people(A,B,C) :person(A), person(B), person(chris). % % % % % Chris is married to the doctor. The lawyer plays the piano. Pat is not married to the engineer. Sandy is a patient of the violinist.
person(sandy).
% spouses(X,Y,Z): X,Y,Z can be spouses of Chris,Sandy,Pat. spouses(none,none,none). % Nobody is married. spouses(sandy,chris,none). % Chris and Sandy are married. spouses(pat,none,chris). % Chris and Pat are married. spouses(none,pat,sandy). % Sandy and Pat are married.
c Levesque 2011
41
Ukrainian = TeaDrinker
6. The snail owner smokes Winstons.
SnailOwner = WinstonSmoker
A positional domain
As long as we enforce the appropriate equality and inequality constraints, we are free to take the values of the variables from any set. The positional constraints suggest we take values from a positional ordering of the ve houses: 1 (leftmost), 2 (left middle), 3 (middle), 4 (right middle), and 5 (rightmost). Then we use the following:
pos(1). pos(2). pos(3). pos(4). pos(5). left(4,5). left(1,2). left(2,3). left(3,4).
next_to(X,Y) :- left(X,Y). next_to(X,Y) :- left(Y,X). leftmost_pos(1). middle_pos(3). uniq_pos(P1,P2,P3,P4,P5) :- pos(P1), pos(P2), pos(P3), pos(P4), pos(P5), \+ P1=P2, \+ P1=P3, \+ P1=P4, \+ P1=P5, \+ P2=P3, \+ P2=P4, \+ P2=P5, \+ P3=P4, \+ P3=P5, \+ P4=P5.
c Levesque 2011
44
.
zebra.pl
. % This is a solution to the classic zebra puzzle. solution(Zebra,England,Spain,Ukraine,Japan,Norway) :% The fourteen clues England=Red, Spain=Dog, Coffee=Green, Ukraine=Tea, left(Ivory,Green), Winston=Snail, Kool=Yellow, middle_pos(Milk), leftmost_pos(Norway), next_to(Chesterfield,Fox), next_to(Kool,Horse), LuckyStrike=OJ, Japan=Parliament, next_to(Norway,Blue), % The five lists: houses, nations, pets, drinks, cigarettes uniq_pos(Green,Red,Yellow,Ivory,Blue), uniq_pos(England,Spain,Ukraine,Japan,Norway), uniq_pos(Dog,Snail,Zebra,Fox,Horse), uniq_pos(Tea,Milk,OJ,Coffee,OtherDrink), uniq_pos(Winston,Kool,Parliament,Chesterfield,LuckyStrike). %-------------The positional predicates ------------------uniq_pos(P1,P2,P3,P4,P5) :pos(P1), pos(P2), pos(P3), pos(P4), pos(P5), \+ P1=P2, \+ P1=P3, \+ P1=P4, \+ P1=P5, \+ P2=P3, \+ P2=P4, \+ P2=P5, \+ P3=P4, \+ P3=P5, \+ P4=P5. pos(1). pos(2). pos(3). pos(4). pos(5). left(4,5). leftmost_pos(1). left(1,2). middle_pos(3). left(3,4).
left(2,3).
solution(Zebra,England,Spain,Ukraine,Japan,Norway) :... % puzzle solution as before. print_solution :solution(Z,E,S,U,J,N), pmatch(Z,E,englishman), pmatch(Z,S,spaniard), pmatch(Z,U,ukranian), pmatch(Z,J,japanese), pmatch(Z,N,norwegian). pmatch(X,X,Name) :- nl, write(The zebra owner is ), write(Name), nl. pmatch(X,Y,Name) :- \+ X=Y.
Then we get:
?- print_solution. The zebra owner is japanese
Chapter 5: Satisfying Constraints c Levesque 2011 46
.
1. The rst week found EDWARD in DENMARK, the HIGH SCHOOL PRINCIPAL in ENGLAND, the FASHION DESIGNER in FRANCE, INGRID in ITALY, the OGLETHORPEs in NORWAY, and the PSYCHOANALYST in SPAIN. 2. ALAN visited ENGLAND, FRANCE, ITALY, and SPAIN, not necessarily in that order. 3. DENMARK was visited in succession by the PHOTOGRAPHER, JESSICA, BERTRAM, and the COLLEGE PROFESSOR. 4. CHARLES and HELEN and the COLLEGE PROFESSOR are three of the four people who did not visits ENGLAND. 5. GLENDA was in NORWAY after the MAGAZINE EDITOR had been there but before either the NEWKIRKs or the PSYCHOANALYST. 6. FRED and his wife limited their picture taking to black-and-white stills, the ROSENs shot color slides exclusively, and the magazine editor and spouse took only videos. INGRID and her husband were the only couple who didnt take at least one camera on the trip. 7. Mr. PALMER and the PSYCHOANALYST and the PHOTOGRAPHER and LOIS all visited NORWAY, not necessarily in that order. No two were there during the same week. 8. KATE and her husband took both stills shots and videos in DENMARK, FRANCE, ITALY, and SPAIN though they did not necessarily tour the countries in that order. 9. The P. R. DIRECTOR and spouse got a beautiful color shot of Queen Elisabeth leaving Buckingham Palace to address Parliament. The following week, they were so engrossed in further picture making that they barely made the ight back to New York. 10.The NOVELIST, the GOLFER and FRED are three of the four people that did not visit Denmark.
Chapter 5: Satisfying Constraints c Levesque 2011
48
.
11.The SCRIPTWRITER visited DENMARK, ENGLAND, ITALY, and NORWAY, though not necessarily in that order. 12.Just before they reached the midpoint of their trip, HELEN and her husband nished up their last remaining video cartridge on the top of the Eiffel Tower, and they had to record the remainder of their travels via stills. 13.ENGLAND was the last country visited by the NOVELIST and spouse. It had previously been visited, though in no particular order, by Mr. ROSEN, GLENDA, and BERTRAM, all at different times. 14.During the week that the NEWSPAPER COLUMNIST and spouse were in NORWAY, LOIS was in DENMARK, and FRED was in ITALY. 15.SPAIN was visited, in no particular order, by CHARLES, JESSICA, Mrs MORGAN, and the NEWSPAPER COLUMNIST, no two of whom were there at the same time. 16.HELEN went to NORWAY the same week that the PHYSICIAN was in FRANCE. 17.Mrs. NEWKIRK and the PHOTOGRAPHER did not tour ITALY. 18.The NEWSPAPER COLUMNIST, the GOLFER, and the MAGAZINE EDITOR are of the same sex. 19.The NOVELIST and the PHYSICIAN are of the opposite sex. 20.FRANCE was the nal country on the GOLFER itinerary. 21.The P. R. DIRECTOR and the HIGH SCHOOL PRINCIPAL are of the same sex.
c Levesque 2011
49
6. Scheduling
Constraint satisfaction is not just for puzzles. A more practical application is scheduling, that is, nding times and/or places for events. Example: We want to schedule a new class to meet 5 hours each week. Meeting times are on the hour from 9am to 4pm, on weekdays. Some of the periods will be taken by previously scheduled courses. Classes must not meet on successive hours on the same day, or for more than 2 hours total on any one day. The job here is to nd the periods (= days and times) for the 5 classes.
c Levesque 2011
50
not already taken non-consecutive no more than 2 per day all different (this is not explicitly stated!)
51
c Levesque 2011
c Levesque 2011
52
A scheduling program
schedule-top.pl
% This program solves a classroom scheduling problem. solution(P1,P2,P3,P4,P5) :uniq_periods(P1,P2,P3,P4,P5), % All different periods. not_2_in_a_row(P1,P2,P3,P4,P5), % Not two consecutive hrs. not_3_same_day(P1,P2,P3,P4,P5). % Not three on the same day. not_2_in_a_row(P1,P2,P3,P4,P5) :\+ seq(P1,P2), \+ seq(P1,P3), \+ seq(P1,P4), \+ seq(P1,P5), \+ seq(P2,P3), \+ seq(P2,P4), \+ seq(P2,P5), \+ seq(P3,P4), \+ seq(P3,P5), \+ seq(P4,P5). seq(A,B) :- A =:= B-1. seq(A,B) :- A =:= B+1. not_3_same_day(P1,P2,P3,P4,P5) :\+ eqday(P1,P2,P3), \+ eqday(P1,P2,P4), \+ eqday(P1,P2,P5), \+ eqday(P1,P3,P4), \+ eqday(P1,P3,P5), \+ eqday(P1,P4,P5), \+ eqday(P2,P3,P4), \+ eqday(P2,P3,P5), \+ eqday(P2,P4,P5), \+ eqday(P3,P4,P5). eqday(A,B,C) :- Z is A // 100, Z is B // 100, Z is C // 100. % The definition of uniq_periods is elsewhere.
c Levesque 2011
53
An example grid
Mon Tues Wed Thu Fri
9 10
11
12
Then we get
?- solution(P1,P2,P3,P4,P5). P1 = 111, P2 = 113, P3 = 213, % Mon 11, Mon 1pm, Tue 1pm, P4 = 409, Thur 9am, P5 = 411 Thur 11am
c Levesque 2011
54
Visual interpretation
Q: Is constraint satisfaction only useful when we are dealing with a nerdy puzzle of some sort? A: No, it also can apply to ordinary human behaviours like vision. Q: What is involved with vision? with seeing something? A: It involves coming up with an interpretation for a 2-dimensional grid of colours and intensities.
c Levesque 2011
170
Torralba
(a) (b)
Figure 1. The structure of many real-world scenes is governed by strong congurational rules akin to those that apply to a singl In such situations, individual objects can be recognized even when the intrinsic information is impoverished, for instance due to as shown above. In presence of image degradation (due to distance or fast scene scanning), object recognition mechanisms hav clude contextual information in order to make reliable inferences. Recognition based on intrinsic features provides very poor perfo when asking to observers. However, when the object is immersed in its typical environment, subjects experience a vivid recognitio object.
However, in situations with poor viewing (caused, for instance, by large distances, or sh quisition times) context appears to play a maj in enhancing the reliability of recognition. Thi cause, in such circumstances, the analysis of in object information alone cannot yield reliable (Fig. 1(a)). When the object is immersed in i ical environment, recognition of the object be reliable (Fig. 1(b)). Under degradation, purely (a) centered representations are not enough for ac Figure 1. The structure of many real-world scenes is governed by strong congurational rules akin to those that apply to a single ing for the reliable object recognition performa object. c Levesque 2011 3 In such situations, individual objects can be recognized even when the intrinsic information is impoverished, for instance due to blurring observers when the object is presented in con as shown above. In presence of image degradation (due to distance or fast scene scanning), object recognition mechanisms have to in2. Context real-world scenes, intrinsic object information clude contextual information in order to make reliable inferences. Recognition based on intrinsic features provides very poor performances when asking to observers. However, when the object is immersed in its typical environment, subjects experience a vivid recognitiontenthe of degraded due to occlusions, illumination, sh object. 2.1. The Role of Context peripheral vision and distance, leading to poor tion and/or contrast. Therefore, the inclusion o
The paper is organized as follows: in Section 2 we review the role of context and discuss some of the past work on context-based object recognition. Section 3 formalizes the statistical framework used in this work for including context information in the object detection task. Section 4 details the contextual representation. Section 5 describes the image database used for our experiments. Sections 6, 7 and 8 describe respectively object priming, context-driven focus of attention (b) and automatic context-driven scale selection.
a region cannot border or be surrounded by another region with the same label houses cannot be next to or surrounded by water vehicles must be next to or surrounded by pavement pavement cannot be completely inside any other region houses, vehicles and pavement are regular (straight-edged); grass and water are irregular vehicles are small; the other regions are large
Chapter 6: Interpreting visual scenes c Levesque 2011 4
Visual properties
The task here is to determine how properties of the image can be translated into suitable constraints.
R1
The more we extract from the image, the more we are able to rule out incorrect interpretations In our example image: 1. region R5 is small; the others are large
R2 R3
R5
R4
2. region R3 and R5 are regular; R1 and R2 are irregular; R4 could go either way 3. region R1 borders on R2; R2 borders on R4 4. region R3 is inside R2; R5 is inside R4
Chapter 6: Interpreting visual scenes c Levesque 2011 5
Visual constraints
We can handle constraints (1) and (2) with facts like
large(grass). small(vehicle). regular(pavement). irregular(water).
To handle (3), we simply ensure that the two regions do not violate any of the given rules about borders:
the two regions must be different they must not be house and water if one is vehicle, the other must be pavement
Constraint (4) is handled analogously.
c Levesque 2011
% The five types of regions that can appear in an image region(grass). region(water). region(pavement). region(house). region(vehicle). % small(X) holds when region X can be small in an image. small(vehicle). % regular(X) holds when region X can be regular in an image. regular(pavement). regular(house). regular(vehicle). % border(X,Y) holds when region X can border region Y. border(X,Y) :- \+ bad_border(X,Y), \+ bad_border(Y,X). % Unacceptable borders bad_border(X,X). bad_border(house,water). bad_border(vehicle,X) :- \+ X=pavement. % inside(X,Y) holds when region X can be surrounded by Y. inside(X,Y) :- \+ bad_inside(X,Y). % Unacceptable containment bad_inside(X,X). bad_inside(house,water). bad_inside(vehicle,X) :- \+ X=pavement. bad_inside(pavement,_).
c Levesque 2011
solution(R1,R2,R3,R4,R5) :region(R1), region(R2), region(R3), region(R4), region(R5), % Size constraints \+ small(R1), \+ small(R2), \+ small(R3), \+ small(R4), small(R5), % Regularity constraints (none for R4) regular(R3), regular(R5), \+ regular(R2), \+ regular(R1), % Border constraints border(R1,R2), border(R2,R4), % Containment constraints inside(R3,R2), inside(R5,R4). % The definitions of region, small, border, etc. are elsewhere.
R1
R3
R5
R4
7 Lists in Prolog
Chapter 7: Lists
c Levesque 2011
A recurring problem
A problem that comes up again and again in Prolog is that we have to write collections of predicates that are very similar except for the number of arguments they take. For example,
uniq person3(X,Y,Z) :- ... for 3 people uniq person4(X,Y,Z,U) :- ... for 4 people uniq person5(X,Y,Z,U,V) :- ... for 5 people
What would be much clearer and convenient, is if we could write just one such predicate
that would work for any collection L of people, no matter how big or small. Prolog provides such a collection. It is called a list.
c Levesque 2011
Chapter 7: Lists
Lists
A list is a sequence of objects which are called the elements of the list. Here are some examples:
[anna, karenina] A two element list, whose rst element is anna and whose second element is karenina. [intro, to, programming, in, prolog] A ve element list. [1, 2, 3, 3, 2, 1] Lists may have repeated elements. [] A list with no elements. The empty list.
Chapter 7: Lists
c Levesque 2011
Structured lists
Lists may contain other lists as elements:
[[john, 23], [mary, 14], [hello]] A list whose three elements are also lists. [1, john, [199y, john]] Another three element list. [[ ]] A one element list, whose element is the empty list.
Note: A one element list is different from the element itself
[anna] is different from anna; [[ ]] is different from [ ].
Chapter 7: Lists
c Levesque 2011
[a, b, c, d] has head a and tail [b, c, d]; [[a], b, [c]] has head [a] and tail [b, [c]]; [a, [b, c]] has head a and tail [[b, c]]; [[a, b]] has head [a, b] and tail [ ]; [ ] has neither head not tail.
Note that the head of a list can be anything, but the tail is always a list.
Chapter 7: Lists
c Levesque 2011
Chapter 7: Lists
c Levesque 2011
two lists without variables match when they are identical, element for element; two lists with distinct variables match when the variables can be given values that make the two lists identical, element for element.
We will see a number of examples of matching lists on the next slides, but the idea can be induced from the example below: the atom p([X, 3 | Z]) unies with p([b, Y, c, b]) for X=b, Y=3, Z=[c, b].
Chapter 7: Lists c Levesque 2011 7
Chapter 7: Lists
c Levesque 2011
Non-matching lists
Lists do not match if they have different numbers of elements or if at least one corresponding element does not match. Some non-matching pairs:
[a] and [ ] [ ] and [[ ]] [X,Y] and [U,V,W] [a,b,c] and [X,b,X] [X|Y] and [ ]
Observe:
X matches anything, including any list [X] matches any list with exactly one element [X|Y] matches any list with at least one element [X,Y] matches any list with exactly two elements
c Levesque 2011 9
Chapter 7: Lists
write clauses to handle the base case where k = 0 (that is, for the empty list); assume the case for k = n has already been taken care of, and write clauses to handle the case where k = (n + 1);
Assume the list T is handled, and write clauses for [H|T].
If we can do this, we will have handled all lists (by mathematical induction). Now we consider four examples.
Chapter 7: Lists c Levesque 2011 10
1. A list of people
As a rst example of a predicate that deals with lists, consider the following: Imagine that we already have a predicate person(x) that holds when x is a person. We would like to write the clauses for another predicate person list(z) that holds when z is a list whose elements are all people. This will be a recursive predicate:
we want clause(s) for the empty list: person list([]). assuming we have person list(Z) that we can use, we want clause(s) for a list with one more element: person list([P|Z]).
If we can provide clauses for both of these cases, we are done.
Chapter 7: Lists c Levesque 2011 11
A list of people
Here is a denition of person list:
% We assume the person(X) predicate is defined elsewhere. % The empty list is a (trivial) list of persons. person_list([]). % If P is person and Z is a list of persons then [P|Z] is a list of persons. person_list([P|Z]) :- person(P), person_list(Z).
[john,joe,sue] unies with [P|Z] for P=john and Z=[joe,sue]; [joe,sue] unies with [P|Z] for P=joe and Z=[sue]; [sue] unies with [P|Z] for P=sue and Z=[]; [] unies with [] and succeeds immediately.
Note how the two notations for lists work together on this.
Chapter 7: Lists
c Levesque 2011
12
2. List membership
Suppose we want a predicate that will tell us whether or not something is an element of a list. In other words, we want this behaviour:
?- elem(b,[a,b,c,d]). Yes ?- elem(f,[a,b,c,d]). No
Most Prolog systems provide a predened predicate (called member) with this behaviour. Nonetheless, we will dene our own. This will be a recursive predicate:
we write clauses for the empty list
Nothing to write, since the query elem(X,[]) should always fail.
in the rst clause, we dont care what the tail is; in the second, we dont care about the head.
?T T T T T T elem(c,[a,b,c,d,e]). Call: (7) elem(c, [a, Call: (8) elem(c, [b, Call: (9) elem(c, [c, Exit: (9) elem(c, [c, Exit: (8) elem(c, [b, Exit: (7) elem(c, [a, b, c, d, d, c, b, c, d, e]) d, e]) e]) % Here we get to use the 1st clause e]) % which succeeds immediately! d, e]) c, d, e])
14
Chapter 7: Lists
c Levesque 2011
The empty list [] is a (trivial) list of unique people. If L is a list of unique people, and P is a person, and P is not an element of L, then [P|L] is also a list of unique people.
This gives us the following two Prolog clauses:
uniq_people([]). uniq_people([P|L]) :- uniq_people(L), person(P), \+ member(P,L).
Chapter 7: Lists
c Levesque 2011
15
Most Prolog systems provide a predened predicate (called append) with this behaviour. We again dene our own. The predicate will once again be recursive. However, the predicate join needs to work for any two lists: the rst list can be empty or non-empty, and the second list can also be empty or non-empty.
Chapter 7: Lists c Levesque 2011 16
write clauses to handle the case where the rst argument is [ ] and the second argument is any list L; write clauses to handle the case where the rst argument is [H|T] and the second argument is any list L (assuming join already works when the rst argument is T and the second argument is L).
Here is the program we get:
% join(X,Y,Z) means the result of joining X and Y is Z. % Joining [] and any list L gives L itself. join([],L,L). % If joining T and L gives Z, then joining [H|T] and L gives [H|Z]. join([H|T],L,[H|Z]) :- join(T,L,Z).
Chapter 7: Lists
c Levesque 2011
17
Tracing join
?T T T T T T T T T T join([a,b,c,d],[e,f,g],Z). Call: (7) join([a, b, c, d], [e, f, g], _G306) Call: (8) join([b, c, d], [e, f, g], _G378) Call: (9) join([c, d], [e, f, g], _G381) Call: (10) join([d], [e, f, g], _G384) Call: (11) join([], [e, f, g], _G387) % Here we get to use Exit: (11) join([], [e, f, g], [e, f, g]) % the first clause Exit: (10) join([d], [e, f, g], [d, e, f, g]) Exit: (9) join([c, d], [e, f, g], [c, d, e, f, g]) Exit: (8) join([b, c, d], [e, f, g], [b, c, d, e, f, g]) Exit: (7) join([a, b, c, d], [e, f, g], [a, b, c, d, e, f, g])
Z = [a, b, c, d, e, f, g] Yes
Note that the rst argument will be progressively reduced until it becomes [ ]. At this point, the third argument must be equal to the second. Then, each recursive query gets a turn to put a new head onto the third argument. (Note the use of H in the head of the clause.)
Chapter 7: Lists
c Levesque 2011
18
So we can write queries (or bodies of clauses) that go through the elements of a list: . . . member(X,L), p(X) . . .
In this case, X will be assigned to the rst element of list L. But if p(X) fails, X will be assigned to the next element, etc.
?- member(N,[1,2,-3,4,-5,6,7]), N < 0. N = -3 ; N = -5 ; No
Chapter 7: Lists
c Levesque 2011
19
Chapter 7: Lists
c Levesque 2011
20
Chapter 7: Lists
c Levesque 2011
21
Chapter 7: Lists
c Levesque 2011
22
This last predicate says that E is an element of L if L is the result of joining some list to another list whose rst element is E.
In other words: L has some elements, then E, then some other elements.
Chapter 7: Lists
c Levesque 2011
23
Chapter 7: Lists
c Levesque 2011
24
Chapter 7: Lists
c Levesque 2011
25
% This is a list-based version of the blocks-world program. % X appears before Y in list L. before(X,Y,L) :- append(Z,[Y|_],L), append(_,[X|_],Z). % The given blocks-world scene: three stacks of blocks scene([[b1,b2],[b3,b4,b5,b6],[b7]]). % above(X,Y) means that block X is somewhere above block Y. above(X,Y) :- scene(L), member(Stack,L), before(X,Y,Stack). % left(X,Y) means that block X is somewhere left of block Y. left(X,Y) :- scene(L), before(Stack1,Stack2,L), member(X,Stack1), member(Y,Stack2). right(Y,X) :- left(X,Y).
Chapter 7: Lists
c Levesque 2011
26
% generate another pair % the 2nd pair % - test X in first % YES % - test Y in second % YES % the 2nd pair works!
Chapter 7: Lists
c Levesque 2011
27
when using \+ and the arithmetic operations when using predicates recursively (see the predicate above) when generating a value to be tested (see the predicate before)
In some cases, predicates can be dened in a way that allows queries with variables to generate values (e.g. append). In other cases, queries must provide values for one or more of the arguments (e.g. in numerical predicates like factorial).
Chapter 7: Lists c Levesque 2011 28
Natural language
Among all the tasks that appear to require thinking, making sense of a natural language, that is, a language like English or Italian or Swahili that is spoken naturally by people, holds a position of honour.
As with recreational puzzles, it is necessary to use what we know to be able to make sense of English expressions. What does the it mean in
The trophy would not t into the brown suitcase because it was too small.
Who does the American President during the Civil War refer to? However, language also feeds our knowledge!
We acquire much of what we know not through direct experience, but by being told via language!
Chapter 7: Natural Language c Levesque 2011 2
ease of use: not having to learn computer language, especially for non-technical users richness: refer to objects in a rich descriptive way Find me the article written by Jane just before she left on her trip to the Middle East. learning via books: much of what we know about the world is written down in books (or in Web pages) in natural language
c Levesque 2011
Status report
To date, success in limited domains:
c Levesque 2011
Linguistics
Linguistics is the study of natural language at a variety of levels
morphology: roots of words, prexes, sufxes ran = run + PAST children = child + PLURAL syntax: how do the words group together? Mary kicked the boy in the knee. vs. Mary kicked the boy in the rst row.
continued. . .
c Levesque 2011
More linguistics
semantics: what do the words mean? The astronomer spotted a star. The astronomer married a star. vs.
The trophy would not t into the brown suitcase because it was too small. vs. because it was too big. pragmatics: what are the words being used for? Can you juggle? vs. Can you pass the salt? I wouldnt turn around if I were you.
In this course: just syntax and some semantics
Chapter 7: Natural Language c Levesque 2011 6
Syntax
How words group together in a language: phrases, sentences, paragraphs, . . . .
c Levesque 2011
Syntactically well-formed sentences need not be semantically well-formed. Colourless green ideas sleep furiously. Syntactically ill-formed word sequences can sometimes be somewhat meaningful. Accident driver car drunk hospital.
c Levesque 2011
Lexicon
The starting point for the syntactic analysis of a language is a lexicon. This species the word categories of the language:
article: a, the adjective: fat, rich, happy, oldest, orange, . . . proper nouns: Mary, John, Toronto, Great Britain, . . . common nouns: boy, sweater, park, milk, justice, . . . transitive verbs: kick, love, eat, . . . intransitive verbs: swim, walk, sleep, . . . copula verbs: is, seems, . . . prepositions: in, on, from, beside, . . . other word categories: pronouns, adverbs, interjections, . . .
Words can appear in many categories, for example: set, run, fat
Chapter 7: Natural Language c Levesque 2011 9
Grammar
A grammar of a language is a specication of the various types of well-formed word groups. These word groups are called grammatical categories. We need to distinguish between
lexical or terminal categories, like article, or transitive verb. We write these in lower-case. group or non-terminal categories, like phrases or sentences. We will write these in upper case.
It is customary to name them using short cryptic abbreviations: NP instead of noun phrase
c Levesque 2011
10
Grammar rules
Usually grammars are specied by a collection of rules (similar to Prolog clauses) describing how each type of word group is formed from other word groups. A grammar rule will have the following form: category category1 category2 . . . categoryn where the category on the left must be a non-terminal, and the categories on the right (if any) can be either terminals or non-terminals. For example,
PP
preposition NP
says that a prepositional phrase (PP) can be made up of a preposition followed by a noun phrase (NP).
Chapter 7: Natural Language c Levesque 2011 11
Sample grammar
Here is a grammar for simple English declarative sentences:
S VP VP VP Mods Mods PP NP NP NP2 NP2 PP Mods preposition NP proper noun article NP2 adjective NP2 common noun Mods NP VP copula verb Mods transitive verb NP Mods intransitive verb Mods
Parse tree
Parsing is the process of taking a sequence of words and determining how they t into a given grammar.
S
NP NP2
VP Mods
This is done by producing a parse tree, with the words at the leaves, and the desired group category at the root.
article adjective noun
NP2 Mods
PP NP
Mods
NP2
copula preposition article noun
Mods
Ambiguity
A grammar is ambiguous if there is a sequence of words with two distinct parse trees.
NP2 NP2 Mods Mods PP PP NP NP Mods PP NP Mods Mods PP NP NP2 Mods Mods
Our goal
What we will do here, is write a program in Prolog that does simple syntactic and semantic processing of yes-no questions only:
parse the questions according to a given grammar determine referents for noun phrases and answer questions (resolving ambiguities as necessary)
For example:
?- yes_no(is mary in a park with linda). Yes ?- yes_no(is the man with the red hat george). No ?- yes_no(is the small blue hat on the woman beside linda). Yes ?- yes_no(is a red with a woman hat). No % ungrammatical
c Levesque 2011
15
The approach
The Prolog program will have 3 distinct pieces: 1. a world model
These are clauses that represent the knowledge of the world we are interested in: who the people are, where they are, what they are wearing, etc. Nothing in the world model is intended to be language specic.
2. a lexicon
These are clauses that describe the English words we will be using in the noun phrases. It also links these words to their meaning in the predicates and constants in the world model. Nothing in the lexicon depends on the grammar.
3. a parser / interpreter
These are clauses which dene the grammar. It also uses information provided by the lexicon and world model to decide what individual is being referred to.
c Levesque 2011
16
person(john). person(george). person(mary). person(linda). park(kew_beach). park(queens_park). tree(tree01). tree(tree02). tree(tree03). hat(hat01). hat(hat02). hat(hat03). hat(hat04). sex(john,male). sex(mary,female). color(hat01,red). color(hat03,red). sex(george,male). sex(linda,female). color(hat02,blue). color(hat04,blue).
in(john,kew_beach). in(george,kew_beach). in(linda,queens_park). in(mary,queens_park). in(tree01,queens_park). in(tree02,queens_park). in(tree03,kew_beach). beside(mary,linda). beside(linda,mary). on(hat01,john). on(hat02,mary). on(hat03,linda). on(hat04,george). size(john,small). size(mary,small). size(hat01,small). size(hat03,big). size(tree01,big). size(george,big). size(linda,small). size(hat02,small). size(hat04,big). size(tree02,small).
size(tree03,small).
c Levesque 2011
17
Building a lexicon
In the lexicon, we need to describe all the words we intend to use in noun phrases. In addition, for each word, we need to know what constraint (if any) it imposes on the referent we are looking for. For example:
if the word is hat, and the object we are looking for is X, then the query hat(X) should succeed if the word is red and the object we are looking for is X, then the query colour(X,red) should succeed
So when we parse a noun phrase like a red hat we will end up with the conjunctive query colour(X,red), hat(X).
Chapter 7: Natural Language c Levesque 2011 18
if the word is john and the object we are looking for is Y, then the query Y=john should succeed.
For prepositions, there are two referents involved. For example:
if the word is on and the two objects we are looking for are X and Y, then the query on(X,Y) should succeed
So when we parse a red hat on John we will end up with the conjunctive query colour(X,red), hat(X), on(X,Y), Y=john.
c Levesque 2011
19
2. Example lexicon
lexicon.pl
article(a).
article(the).
common_noun(park,X) :- park(X). common_noun(tree,X) :- tree(X). common_noun(hat,X) :- hat(X). common_noun(man,X) :- person(X), sex(X,male). common_noun(woman,X) :- person(X), sex(X,female). adjective(big,X) :- size(X,big). adjective(small,X) :- size(X,small). adjective(red,X) :- color(X,red). adjective(blue,X) :- color(X,blue). preposition(on,X,Y) :- on(X,Y). preposition(in,X,Y) :- in(X,Y). preposition(beside,X,Y) :- beside(X,Y). % The preposition with is flexible in how it is used. preposition(with,X,Y) :- on(Y,X). % Y can be on X preposition(with,X,Y) :- in(Y,X). % Y can be in X preposition(with,X,Y) :- beside(Y,X). % Y can be beside X % Any word that is not in one of the four categories above. proper_noun(X,X) :- \+ article(X), \+ adjective(X,_), \+ common_noun(X,_), \+ preposition(X,_,_).
c Levesque 2011
20
the lexicon describes the words we are interested in using (like the word hat) the world model describes the facts of the world we care about (using the concept of a hat)
The world model is intended to be language neutral. We could have used
common noun(hat,X) :- concept237(X).
Chapter 7: Natural Language c Levesque 2011 21
synonyms
common noun(cap,X) :- hat(X). common noun(bonnet,X) :- hat(X).
other languages
common noun(chapeau,X) :- hat(X).
The parser
For each non-terminal category in the grammar, we will have a predicate in the parser. Each such predicate will take two arguments:
np([Name],X) :- proper_noun(Name,X). np([Art|Rest],X) :- article(Art), np2(Rest,X). np2([Adj|Rest],X) :- adjective(Adj,X), np2(Rest,X). np2([Noun|Rest],X) :- common_noun(Noun,X), mods(Rest,X). mods([],_). mods(Words,X) :append(Start,End,Words), pp(Start,X), mods(End,X).
% Break the words into two pieces. % The first part is a PP. % The last part is a Mods again.
Note how we use append to split a list of words into two pieces.
This will allow us to have [in,the,park,with,a,red,hat] break into two smaller lists: [in,the,park] and [with,a,red,hat].
Now we are ready to load all three pieces (world model, lexicon, parser) into Prolog and try them out. . . .
Chapter 7: Natural Language c Levesque 2011 24
Tracing a parse
?- np([a,big,tree],X). Call: (7) np([a, big, tree], _G322) Call: (8) article(a) Exit: (8) article(a) Call: (8) np2([big, tree], _G322) Call: (9) adjective(big, _G322) Exit: (9) adjective(big, george) % George is something big Call: (9) np2([tree], george) ... this eventually fails and then ... Exit: (9) adjective(big, tree01) % Tree01 is something big Call: (9) np2([tree], tree01) Call: (10) adjective(tree, tree01) Fail: (10) adjective(tree, tree01) Redo: (9) np2([tree], tree01) Call: (10) common_noun(tree, tree01) Exit: (10) common_noun(tree, tree01) % Tree01 is a tree Call: (10) mods([], tree01) Exit: (10) mods([], tree01) Exit: (9) np2([tree], tree01) Exit: (8) np2([big, tree], tree01) Exit: (7) np([a, big, tree], tree01) X = tree01
c Levesque 2011
25
c Levesque 2011
26
Handling ambiguity
Call: Call: Exit: Call: Call: Call: Call: Call: Exit: Call: Fail: Fail: Call: Call: Call: Call: Exit: Call: Exit: Exit: Exit: Exit: (7) np2([man, in, the, park, with, a, tree], _G334) (8) common_noun(man, _G334) (8) common_noun(man, john) (8) mods([in, the, park, with, a, tree], john) % need a split (9) pp([], john) (9) pp([in], john) (9) pp([in, the], john) (9) pp([in, the, park], john) (9) pp([in, the, park], john) % 1st part: yes (9) mods([with, a, tree], john) (9) mods([with, a, tree], john) % 2nd part: no! (9) pp([in, the, park], john) (9) pp([in, the, park, with], john) (9) pp([in, the, park, with, a], john) (9) pp([in, the, park, with, a, tree], john) % this split works (10) preposition(in, john, _G335) (10) preposition(in, john, qbeach) (10) np([the, park, with, a, tree], qbeach) (10) np([the, park, with, a, tree], qbeach) (9) pp([in, the, park, with, a, tree], john) (8) mods([in, the, park, with, a, tree], john) (7) np2([man, in, the, park, with, a, tree], john)
c Levesque 2011
27
np2([man,in,the,park,with,a,tree], ) common noun(man, ), mods([in,the,park,with,a,tree], ) mods([in,the,park,with,a,tree], ) pp([in,the,park,with,a,tree], ), mods([], ) preposition(in, , ), np([the,park,with,a,tree], ) np([the,park,with,a,tree], ) article(the), np2([park,with,a,tree], ) np2([park,with,a,tree], ) common noun(park, ), mods([with,a,tree], ) mods([with,a,tree], ) pp([with,a,tree], ), mods([], ) preposition(with, , ), np([a,tree], ) np([a,tree], ) article(a), np2([tree], ) np2([tree], ) common noun(tree, ), mods([], )
Chapter 7: Natural Language c Levesque 2011
noun PP prep
Mods Mods NP
article
NP2
noun
Mods
PP prep NP
Mods
article
NP2
noun
Mods
28
More examples
?- np([a,man,with,a,big,hat],X). X = george ; No ?- np([the,hat,on,george],X). X = hat04 ; No ?- np([a,man,in,a,park,with,a,big,tree],X). No ?- np([a,woman,in,a,park,with,a,big,tree],X). X = mary ; X = linda ; No ?- np([a,woman,in,a,park,with,a,big,red,hat],X). X = linda ; No ?- np([a,woman,beside,a,woman,with,a,blue,hat],X). X = mary ; % Note: this is not the obvious reading X = linda ; No ?- np([a,woman,with,a,blue,hat,beside,a,woman],X). X = mary ; No
c Levesque 2011
29
c Levesque 2011
30
Interrogative sentences
To handle some simple interrogative sentences, we can use grammar rules similar to those from before:
WH wh word copula verb NP
Who is the woman with Linda? What is the hat on the man in the park?
wh-questions:
WH
What is in Queens Park? Who is beside a man with a small hat? YN copula verb NP NP
Is the man with the blue hat John? Is Mary the woman beside Linda?
yes-no questions:
YN
copula verb NP PP
Is John beside a woman with a blue hat? Is the big red hat on George?
Chapter 7: Natural Language c Levesque 2011 31
The wordUtils.pl le also includes utilities for punctuation and upper and lower case, but these need not concern us here.
A fancier program would convert a string like Is John beside Mary? into the list of words [is,john,beside,mary].
Chapter 7: Natural Language c Levesque 2011 32
yes_no(String) :split_words(String,Words), yn(Words). yn([Verb|Rest]) :Verb=is, append(W1,W2,Rest), np(W1,Ref), np_or_pp(W2,Ref). np_or_pp(W,Ref) :- np(W,Ref). np_or_pp(W,Ref) :- pp(W,Ref).
% Get the list of words. % Use yn on the words. % % % % The first word must be "is". Break the rest into two parts. The first part must be an NP. The second part must be an NP or a PP.
Note: with this version of the parser, we cannot distinguish between a failure (a question that is ill-formed or where no referent can be found) and a legitimate question whose answer is no.
Chapter 7: Natural Language c Levesque 2011 33
After we have determined that the two referents in question are john and queens park, we do not want to simply test that in(john,queens park) holds. Most likely, what is intended is that we should treat this as new information, and add it as a new fact to our world model for later use.
This raises two problems:
How do we get a program to add facts to a Prolog world model? Our lexicon is geared to nding referents only. For example, for the word in we generate a query of the form in(X,Y).
Chapter 7: Natural Language c Levesque 2011 34
Dynamic predicates
Prolog allows the clauses associated with certain predicates to be changed by the program itself. These are called dynamic predicates. For example, suppose we have a predicate my pred( x,y,z) that takes three arguments. To make my pred dynamic, we include the declaration
:- dynamic my pred/3.
in the program le before the predicate is used. We can then have clauses in the le that dene my pred as usual, but we also get to use two special operations: assert and retract.
c Levesque 2011
35
assert(atom) This query always succeeds, and has the effect of adding the atom as a fact to Prologs knowledge base. retract(atom) This query has the effect of removing the rst fact in Prologs knowledge base that matches the atom. It fails if there is no match.
This means we can write Prolog programs like this
get_married(X) :retract(single(X)), assert(married(X)). % X is no longer single % X is now married
The idea is that add for preposition(P,X,Y) tells us how we should change our world model if we nd out that the object X is in the relation denoted by preposition P to object Y.
c Levesque 2011
37
NP copula verb PP
John is in the park with the big tree. The man with the red hat is beside George.
declarative.pl
% Get words from string. % Use sd on the words. % % % % Split words. Find referent for first NP. Find referent for second NP. Add new atom to database.
c Levesque 2011
38
After we have done all the syntactic and semantic analysis (including deciding who the they is), we would want to add a fact like this:
There were two events, e1 and e2 that took place, involving two groups of people, z1 and z2 , and where e1 was a cause of e2 . In addition, z1 is a group of councillors (who are elected representatives)
z2 is a group of demonstrators (participants in a demonstration) e1 is the members of z1 fearing that some event e3 will take place e2 is z1 deciding as a group to prevent some event e4 from taking place
etc.
Chapter 7: Natural Language c Levesque 2011 40
Dealing with complex sentences is still beyond the state of the art.
example: anaphora, as in resolving the word they in the they feared violence example sentence.
Nonetheless, the idea of interpreting a sentence as a query or an update against a background knowledge base, as we have done here, is at the root of most AI natural language systems.
Chapter 7: Natural Language c Levesque 2011 41
Planning
One of the abilities that people have, and an ability that we are especially proud of, is the ability to make plans: invent a sequence of actions to achieve a goal In a sense, this is the core of AI since it deals with generating intelligent behaviour. In some cases, the planning problems we tackle are natural ones
getting the keys out of a locked car obtaining food for lunch
In other cases, we solve articial problems just for fun
By a move is meant turning one of the coins over (so that a head becomes a tail, or a tail becomes a head)
Chapter 8: Planning
c Levesque 2011
Problem 1: A solution
There are many solutions:
(1) ip middle, (2) ip right, (3) ip middle (1) ip left, (2) ip left, (3) ip right
General properties: etc.
after 1 move: have even number of Ts after 2 moves: have odd number of Ts after 3 moves: have even number of Ts all of them move the right coin 1 or 3 times those that move it 1 time, move another coin twice
Chapter 8: Planning c Levesque 2011 4
Chapter 8: Planning
c Levesque 2011
Problem 2: Solution
Here is what the monkey needs to do:
Chapter 8: Planning
c Levesque 2011
what sides the 3 coins are showing location of the monkey, bananas, and box
operators or moves or actions: ways of moving from state to state
HHH or TTT any state where the monkey has the bananas
Chapter 8: Planning
c Levesque 2011
Planning problems
The problem is always the same: nd a way of going from an initial state to a goal state by applying a sequence of operators.
operators operators
initial state
goal state
The path in red is a plan: a sequence of operators that takes you from an initial state to a goal state.
Chapter 8: Planning c Levesque 2011 9
Using Prolog
We would like to solve planning problems of this type using Prolog:
we tell it the initial state, the goal state, the operators; it should tell us a way of getting from the initial state to the goal state using the operators.
How? It will have to think about the problem:
I start in an initial state, and I want to end up in a goal state. If I am in state S and I do move M , that would put me into a new state S . If Im in S and I do M , that will take me to S . etc.
Along the way, it will need to keep track of the moves it needs to make.
Chapter 8: Planning c Levesque 2011 10
If S 1 S 2 . . . S 3 then S 1 . . . S 3
We can encode this directly in Prolog using lists of moves.
Chapter 8: Planning c Levesque 2011 11
M0
M1 , ..., Mn
M0 , M1 , ..., Mn
% This general planner needs the predicates below to be defined: % - legal_move(BeforeState,Move,AfterState) % - initial_state(State) % - goal_state(State) % plan(L): L is a list of moves from the initial state to a goal state. plan(L) :- initial_state(I), goal_state(G), reachable(I,L,G). % reachable(S1,L,S2): S2 is reachable from S1 using moves L. reachable(S,[],S). reachable(S1,[M|L],S3) :- legal_move(S1,M,S2), reachable(S2,L,S3).
This is a general planner. For each specic problem, we need to specify the clauses for initial state, goal state, and legal move.
Chapter 8: Planning
c Levesque 2011
12
% The three-coins problem formulated for the general planner. initial_state([h,h,t]). goal_state([h,h,h]). goal_state([t,t,t]). % The three possible moves. Each changes one of the coins. legal_move([X,Y,Z],flip_left,[X1,Y,Z]) :- opposite(X,X1). legal_move([X,Y,Z],flip_middle,[X,Y1,Z]) :- opposite(Y,Y1). legal_move([X,Y,Z],flip_right,[X,Y,Z1]) :- opposite(Z,Z1). opposite(h,t). opposite(t,h). % Flipping a head gives a tail. % Flipping a tail gives a head.
Chapter 8: Planning
c Levesque 2011
14
b, m and l are the locations of the bananas, the monkey and the box respectively; o is y or n according to whether the monkey is on the box; and h is y or n according to whether the monkey has the bananas.
We name the initial locations of the bananas, monkey, and box as loc1, loc2, and loc3. Nothing else needs a name.
Chapter 8: Planning c Levesque 2011 15
Up until now, it was useful to make a clear separation in our understanding of Prolog between terms and atoms. But now we can see that a constant is just a special case of an atom where the predicate takes no arguments!
Chapter 8: Planning c Levesque 2011 16
climb on (climbing on the box) climb off (climbing off the box) grab (grabbing the bananas) go(X) (going to location X) push(X) (pushing the box to location X)
We need to write clauses for legal move for each of these operators. These clauses should say how a before state is changed to an after state by the operator in question. The clauses should fail if the operator in question cannot be used in the before state. In this case, the move would not be a legal one.
Chapter 8: Planning c Levesque 2011 17
Pushing a box
Consider the push(X) operator. This operator can only be used in states where the monkey is at the same location as the box, but not on the box. After the operators is used, both the monkey and the box will be at location X, and the rest remains unchanged. We can write this as follows:
% before operator after legal_move( [B,M,L,O,H], push(X), [B1,M1,L1,O1,H1]) :M=L, O=n, % provisos on the old state B1=B, M1=X, L1=X, O1=O, H1=H. % values of the new state
% This is the monkey and bananas as a planning problem. % The bananas, monkey, and box are at different locations. % The monkey is not on the box and has no bananas. initial_state([loc1,loc2,loc3,n,n]). % The goal is any state where the monkey has the bananas. goal_state([_,_,_,_,y]). % Climbing on the box causes the monkey to be on the box. legal_move([B,M,M,n,H],climb_on,[B,M,M,y,H]). % Climbing off the box causes the monkey to be off the box. legal_move([B,M,M,y,H],climb_off,[B,M,M,n,H]). % Grabbing the bananas causes the monkey to have the bananas. legal_move([B,B,B,y,n],grab,[B,B,B,y,y]). % Pushing the box changes where the monkey and the box are. legal_move([B,M,M,n,H],push(X),[B,X,X,n,H]). % Going to a location changes where the monkey is. legal_move([B,_,L,n,H],go(X),[B,X,L,n,H]).
% where the box is % where the bananas are % any other plans?
?- plan([M1,M2,M3,M4,M5]), \+ M1 = go(_). No
Chapter 8: Planning
c Levesque 2011
20
Unbounded plans
We cannot ask for a plan without rst restricting its length.
?- plan(L). ERROR: Out of local stack
What is the problem? The general planner looks for a path from an initial state S 0 to a goal state S g . By tracing, we observe the following: 1. the rst action it considers is a go action from S 0 to a state S 1 ; it then needs to nd a path from S 1 to S g ; 2. the rst action it considers is a go action from S 1 to a state S 2 ; it then needs to nd a path from S 2 to S g ; etc. So the planner never ends up considering any other actions.
Chapter 8: Planning
c Levesque 2011
21
Bounding plans
To avoid problems with unbounded plans, we need to ensure that we only work on plan(L) when we know the length of L. This suggests that we should rst look for 0-step plans, then 1-step plans, then 2-step plans, and so on:
bplan.pl
% This looks for plans, short ones first, using the plan predicate. % bplan(L) holds if L is a plan. bplan(L) :- tryplan([],L). % tryplan(X,L): L is a plan and has X as its final elements. tryplan(L,L) :- plan(L). tryplan(X,L) :- tryplan([_|X],L).
Chapter 8: Planning
c Levesque 2011
22
10 2 13 5
7 15
9 10 11 12 13 14 15 goal state
9 14 11 12 initial state
Each move involves shifting a tile up, down, left, or right, assuming the vacant spot is adjacent.
Chapter 8: Planning
c Levesque 2011
23
initial state: [1, 6, 3, 8, 10, 2, 7, 15, 13, 5, 4, 0, 9, 14, 11, 12] goal state: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]
Chapter 8: Planning
c Levesque 2011
24
There will be 12 such locations for each of the 4 operators! But one simplication we can use is that up and down are inverses (as are the left and right operators). So once we have written all the clauses for up, we can write a single clause for down:
legal move(S1,down(X),S2) :- legal move(S2,up(X),S1).
Chapter 8: Planning c Levesque 2011 25
A simplied version
Here is the full program for a 2 3 version of the puzzle:
puzzle2x3.pl
% This is a 2x3 version of the 15 puzzle. initial_state([0,1,5,4,3,2]). %----------------------> goal_state([1,2,3,4,5,0]). legal_move([0,B,C, X,E,F],up(X),[X,B,C, 0,E,F]). legal_move([A,0,C, D,X,F],up(X),[A,X,C, D,0,F]). legal_move([A,B,0, D,E,X],up(X),[A,B,X, D,E,0]). legal_move(S1,down(X),S2) :- legal_move(S2,up(X),S1). legal_move([0,X,C, D,E,F],left(X),[X,0,C, D,E,F]). legal_move([A,0,X, D,E,F],left(X),[A,X,0, D,E,F]). legal_move([A,B,C, 0,X,F],left(X),[A,B,C, X,0,F]). legal_move([A,B,C, D,0,X],left(X),[A,B,C, D,X,0]). legal_move(S1,right(X),S2) :- legal_move(S2,left(X),S1).
%%%%%%%%% % 1 5 % % 4 3 2 % %%%%%%%%%
Chapter 8: Planning
c Levesque 2011
26
Chapter 8: Planning
c Levesque 2011
28
Decomposing a problem into smaller subproblems allows us to keep the length of the paths much smaller.
Chapter 8: Planning c Levesque 2011 29
1 5 9
13
7 10 14
11 12 15 nal goal
rst goal
second goal
Similarly, the rst goal above breaks down into the goal of getting the rst row into place, and then (without moving the rst row) getting the 5, 9, and 13 into place, and so on.
Chapter 8: Planning
c Levesque 2011
30
Depth-rst search
Another search problem we have is that we look for a path (of length n) to the goal as follows: 1. for a given current state S , choose a legal move: get a new S 2. check to see if there is a path (of length n 1) from S to a goal state 3. if that fails, choose another legal move: get S Imagine that there is a path from S to a goal state.
We may end up searching the entire tree below S in step (2) above before even looking at what is below S . This is called depth-rst search.
Chapter 8: Planning
c Levesque 2011
31
Best-rst search
A better strategy is the following: 1. let L be the list containing just S 0 . 2. select from L the state S that is closest to the goal 3. if S is a goal state, then exit with success (and return the plan) 4. otherwise, replace S in L by all the states S you can get to in one move (and note the action that takes you from S to S ) 5. go to step (2) This is called best-rst search. It requires us to be able to determine the distance to the goal.
Chapter 8: Planning
c Levesque 2011
32
This is easy to calculate and is a lower bound on the actual number of moves that will be needed to get to the goal.
Chapter 8: Planning c Levesque 2011 33
Chapter 8: Planning
c Levesque 2011
34
A representation strategy
The purpose of a state in planning is to track various aspects as they are affected by actions
the climb on action changes whether or not the monkey is on the box the push(X) action changes the location of the monkey and of the box
But another possibility is to simply keep track of all the actions that have been performed and then calculate the aspects as needed. For example, suppose we have a list of actions that have been performed so far, and we want to calculate the location of the box:
if there is no push action in the list, then the box is where it was initially if the most recent push action in the list is push(loc3), then the location of the box is loc3 (and it does not matter what the earlier actions were)
Chapter 8: Planning c Levesque 2011 35
Situations
A list of actions used to represent a state is called a situation. The actions appear in the list most recent rst.
[] [a] [ b, a, a ]
the initial situation, before any actions the situation where a single action a has been performed the situation where action a was performed twice, and then action b was performed
Note that situation [ a, b ] is not the same as [ b, a ]. The order can make a difference. In Prolog terms, performing an action A in situation S always takes us to the new situation [A|S].
Chapter 8: Planning
c Levesque 2011
36
Fluents
The various aspects of a state we care about are now represented as predicates that take a situation as a nal argument. We call these predicates uents. For the monkey and bananas problem, we can use three uents:
on box(S) holds when the monkey is on the box in situation S; has bananas(S) holds when the monkey has the bananas in situation S; location(X,L,S) holds when location of X is L in situation S;
We will need to write clauses for each uent that characterize when the uent holds in terms of the situation, i.e., the actions performed so far.
Chapter 8: Planning
c Levesque 2011
37
Dening uents
The easiest way to characterize a uent is to do it in two steps: 1. state what uents hold in the initial situation [ ]
% on_box([]) is false. Nothing to say % has_bananas([]) is false. Nothing to say location(box,loc1,[]). location(monkey,loc2,[]). location(bananas,loc3,[]).
These are called the initial-state axioms for the uent. 2. state what uents hold in any non-initial situation [A|S] This involves cases depending on whether the action A
makes the uent true makes the uent false leaves the uent unchanged from what it was in S
Chapter 8: Planning
c Levesque 2011
38
Similarly, the monkey has the bananas if it just did a grab action or it already had the bananas. (There is no action to drop the bananas.)
has_bananas([grab|_]). has_bananas([_|S]) :has_bananas(S). % the most recent action was grab % the monkey already had the bananas in S
Clauses like these are called the successor-state axiom for the uent.
Chapter 8: Planning c Levesque 2011 39
Changing locations
The successor-state axiom for location is best handled by considering each object separately. The easiest case is the bananas: their location after doing an action is the same as it was before.
location(bananas,L,[_|S]) :- location(bananas,L,S).
For the monkey, the location stays the same unless the monkey does a go or a push action.
location(monkey,L,[go(L)|_]). location(monkey,L,[push(L)|_]). location(monkey,L,[A|S]) :\+ A = go(_), \+ A = push(_), location(monkey,L,S).
Preconditions
The clauses so far tell us the effect of actions, for example, that the monkey will have the bananas after grabbing them. They do not tell us that the monkey has to be on the box in the right location to grab the bananas. To state these preconditions, we use a special predicate poss(A,S) that should hold when it is possible to perform action A in situation S.
poss(climb_off,S) :- on_box(S). poss(climb_on,S) :location(monkey,L,S), location(box,L,S), \+ on_box(S). poss(grab,S) :- location(monkey,L,S), location(bananas,L,S), on_box(S). poss(go(X),S) :- \+ on_box(S). poss(push(X),S) :location(monkey,L,S), location(box,L,S), \+ on_box(S).
3. One or more clauses for the goal state in terms of uents. For example,
goal_state(S) :- has_bananas(S).
Even if we later add a new uent like box colour and an action that changes it like paint box, we can still use the axiom above.
This has the effect of making a very complex state with large numbers of properties conceptually manageable.
Chapter 8: Planning c Levesque 2011 43
Other complications
In reasoning about action and change, a number of other complications need to be dealt with such as:
the durations of actions lling a bathtub vs. turning on a light planning needs to be sensitive to temporal constraints exogenous actions performed by an other agents (or nature) sensing actions some actions, like reading a thermometer, are not used to change the
world, but only to change what is known about the world
Handling these properly in a planning context remains a current very active topic of AI research.
Chapter 8: Planning c Levesque 2011 44
Game Playing
As we said at the end of the planning section, one complication is when there are other agents that interfere with your ability to attain the goal. In a sense, this is what is involved in playing a game like Chess. In this section, we will look at strategies for restricted sorts of games:
discrete move and turn-taking (vs. many video games) deterministic (vs. Backgammon) perfect information (vs. Clue) two person (vs. Solitaire, Poker) zero sum (vs. Diplomacy)
Games as problems
Consider the problem of nding an appropriate next move: Player X to play and win (after at most 3 moves)
X O
X O
Faulty reasoning:
X O O X
@ O O X @X @ X @ @
eventually ties
Because:
X O O X
c Levesque 2011
Features of games
1. do not need to nd a list of moves from an initial state to a nal state
I must nd the best next move, wait for the opponent to make a move, and then nd the best next move again, and so on
2. the best next move must take into account the responses available to the opponent
and these responses will take into account the possible counter-responses, which will take into account the counter-counter-responses, and so on
3. the best move should not rely on the opponent making a mistake
ideally, I will nd a move such that even if you play your best, I can still nd a move such that even if . . . I will still win.
c Levesque 2011
There are two players. one of the players moves rst. There is a space of states. initial state, at the start of the game. nal state, one of the player wins, or the game is a tie. In any state, there is a set of legal moves. moves go from one game state to another game state. throughout the game, players take turns at choosing one of moves that is legal in the current state.
c Levesque 2011
player(player)
player is one of the two players
c Levesque 2011
initial state: 21 nal state: 0 The player whose turn it is to move loses. (There is never a tie.)
Moves: either 1 or 2.
the new state = the old state the move. (It must not be negative.)
c Levesque 2011
Race to 21 in Prolog
raceto21.pl
This is the Prolog definition of the game Race to 21. player(min). % The two players. % Player Max goes first. % If it is Maxs turn to play, % then Min wins, and vice versa.
player(max).
legal_move(OldState,_,Move,NewState) :small_number(Move), % A move is a small number. OldState >= Move, NewState is OldState - Move. small_number(1). small_number(2). % A small number is either % the number 1 or 2.
This denes the game. But how do we play it? And play it well?
c Levesque 2011
Game trees
A player can decide on a best move by considering all possible moves, all possible counter-moves, all possible counter-counter-moves, etc. To see how this works, we can draw a tree called a game tree whose nodes correspond to states of the game:
the root node of the tree is the initial state we start with; each leaf node of the tree is a nal state of the game where one of the players has won or there is a tie; each non-leaf node has as children the game states we can get to by making legal moves;
By imagining that each player chooses a best move, we can label every node on the tree as a winning position for one of the players (or a tie).
Chapter 10: Games c Levesque 2011 9
f f f
W W W
f i W L L d d
W L W
f f
f f
f
W
f f
f f f i W L L d d
L L L
f d fd f d
W L L
c Levesque 2011
10
Once the entire tree is labelled, it was easy to decide how to move!
Chapter 10: Games c Levesque 2011 11
c Levesque 2011
12
% This general game player needs these predicates to be defined: % - player(Player) % - game_over(State,Player,Winner) % - legal_move(BeforeState,Player,Move,AfterState) % label(S,P,W): state S with player P to move is labeled winner W. label(S,P,W) :- game_over(S,P,W). label(S,P,P) :- win_move(S,P,_). label(S,P,neither) :- \+ win_move(S,P,_), tie_move(S,P,_). label(S,P,Q) :- opp(P,Q), \+ tie_move(S,P,_), \+ game_over(S,P,_). % win_move(S,P,M): P can win by making move M. win_move(S,P,M) :- \+ game_over(S,P,_), opp(P,Q), legal_move(S,P,M,New), label(New,Q,P). % tie_move(S,P,M): P can avoid losing by making move M. tie_move(S,P,M) :- \+ game_over(S,P,_), opp(P,Q), legal_move(S,P,M,New), \+ label(New,Q,Q). opp(P,Q) :- player(P), player(Q), \+ P=Q.
This gives us a general game playing program. As with planning, it needs the predicates player, legal move, and game over dened elsewhere.
Chapter 10: Games c Levesque 2011 13
14
A longer trace
Here is a much longer trace, but with many of the details left out!
?- label(5,max,W). Call: (7) label(5, max, _G314) Call: (9) label(4, min, max) Call: (11) label(3, max, max) Fail: (11) label(3, max, max) Fail: (9) label(4, min, max) Call: (9) label(3, min, max) Call: (11) label(2, max, max) Exit: (11) label(2, max, max) Call: (11) label(1, max, max) Exit: (11) label(1, max, max) Exit: (9) label(3, min, max) Exit: (7) label(5, max, max) W = max
Chapter 10: Games c Levesque 2011
% Can Max win with 5 chips left? %--- try picking 1 chip % is 4 a winning position for Max? % | if Min then moves 1 also % | then we get to 3 % | where Max fails to win % No %--- try picking 2 chips % is 3 a winning position for Max? % | if Min moves 1 % | then we get to 2 % | where Max wins % | if Min moves 2 % | then we get to 1 % | where Max wins % Yes % So yes, max can win by picking 2 chips
15
There are no ties in Race to 21. So Max must choose any legal move. % Can Min force a win after Max moves 1?
The winning strategy for a player in Race to 21 is to ensure that the number of chips left for the next player is divisible by 3. So, the rst player cannot force a win, but the second player can!
c Levesque 2011
16
p1 p2 p3 p4 p5 p6 p7 p8 p9
x, o, or - (empty)
initial state: [-,-,-,-,-,-,-,-,-] (We are using - as an atom, not to be confused with _.) game over: any state where there are x marks making a straight line (x wins) there are o marks making a straight line (o wins) every pi is either x or o (a full board) move: a number m where 1 m 9 and pm = Chapter 10: Games c Levesque 2011 17
Tic-Tac-Toe in Prolog
tictactoe.pl
player(x). player(o).
initial_state([-,-,-,-,-,-,-,-,-],x). game_over(S,_,Q) :- three_in_row(S,Q). game_over(S,_,neither) :- \+ legal_move(S,_,_,_). three_in_row([P,P,P,_,_,_,_,_,_],P) three_in_row([_,_,_,P,P,P,_,_,_],P) three_in_row([_,_,_,_,_,_,P,P,P],P) three_in_row([P,_,_,P,_,_,P,_,_],P) three_in_row([_,P,_,_,P,_,_,P,_],P) three_in_row([_,_,P,_,_,P,_,_,P],P) three_in_row([P,_,_,_,P,_,_,_,P],P) three_in_row([_,_,P,_,P,_,P,_,_],P) ::::::::player(P). player(P). player(P). player(P). player(P). player(P). player(P). player(P).
c Levesque 2011
18
Playing Tic-Tac-Toe
?- win_move([x,o,-,-,-,-,-,-,-],x,M). M = 4 ; M = 5 ; M = 7 ; No ?- win_move([x,-,-,-,-,-,-,-,-],o,M). No ?- tie_move([x,-,-,-,-,-,-,-,-],o,M). M = 5 ; No ?- win_move([-,-,-,-,-,-,-,-,-],x,M). No ?- tie_move([-,-,-,-,-,-,-,-,-],x,M). M = 1 ; M = 2 ; % etc. M = 9 ; No % % % % what can x do to win here? a surprising move? center a corner (but not 3 or 9)
% can o force a tie from here? % yes, but this is the only way
% can x force a tie initially? % yes, and any first move is ok!
c Levesque 2011
19
% play_user(U): play entire game, getting moves for U from terminal. play_user(U) :initial_state(S,P), write(The first player is ), write(P), write( and the initial state is ), write_state(S), play_from(S,P,U). % play_from(S,P,U): player P plays from state S with user U. play_from(S,P,_) :% Is the game over? game_over(S,P,W), write(-------- The winner is ), write(W). play_from(S,P,U) :% Continue with next move. opp(P,Q), get_move(S,P,M,U), legal_move(S,P,M,New), write(Player ), write(P), write( chooses move ), write(M), write( and the new state is ), write_state(New), play_from(New,Q,U). write_state(S) :- nl, write( ), write(S), nl. % Get the next move either from the user or from gameplayer.pl. get_move(S,P,M,U) :- \+ P=U, win_move(S,P,M). % Try to win. get_move(S,P,M,U) :- \+ P=U, tie_move(S,P,M). % Try to tie. get_move(S,P,M,U) :- \+ P=U, legal_move(S,P,M,_). % Do anything. get_move(_,P,M,P) :write(Enter user move (then a period): ), read(M).
c Levesque 2011
20
c Levesque 2011
21
Playing against X
This time, the moves for O are entered (using the special predicate read):
?- play_user(o). The first player is x and the initial state is [-, -, -, -, -, -, -, -, -] Player x chooses move 1 and the new state is [x, -, -, -, -, -, -, -, -] Enter user move (then a period): 2. Player o chooses move 2 and the new state is [x, o, -, -, -, -, -, -, -] Player x chooses move 4 and the new state is [x, o, -, x, -, -, -, -, -] Enter user move (then a period): 7. Player o chooses move 7 and the new state is [x, o, -, x, -, -, o, -, -] Player x chooses move 5 and the new state is [x, o, -, x, x, -, o, -, -] Enter user move (then a period): 6. Player o chooses move 6 and the new state is [x, o, -, x, x, o, o, -, -] Player x chooses move 9 and the new state is [x, o, -, x, x, o, o, -, x] -------- The winner is x
% a bad move!
% a fine move
% a good move
c Levesque 2011
22
s s s
s s s
s s
There are 8 dots aligned as in the diagram, making the outline of 3 squares. At each turn, a player draws one of the undrawn edges of a square by connecting two adjacent dots. If this happens to be the last undrawn edge of a square, the player owns the square, and may optionally move again. The rst player to own 2 squares wins. (There are no ties.)
c Levesque 2011
23
q q
max
q q
q q
I @ @
note
c Levesque 2011
24
r r r
2 5 7
r r
A move will be a list of one or more lines drawn. A state will be a list of terms of the form draw(player,line), with the most recent moves rst. This is like the situation representation (from planning), and will allow us to calculate what we need:
player(max).
player(min).
% This is the Boxes game. % Lines for square 1 % Lines for square 2 % Lines for square 3
initial_state([],max). % Initially no lines are drawn. game_over(St,_,W) :% Winner W owns two squares. owns(W,Sq1,St), owns(W,Sq2,St), \+ Sq1 = Sq2. % Player P owns Sq if just drew last line or owned Sq before. owns(P,Sq,[draw(P,L)|St]) :- last_avail_line(L,Sq,St). owns(P,Sq,[_|St]) :- owns(P,Sq,St). % Line L is available and is the last of square not yet drawn. last_avail_line(L,Sq,St) :avail_line(L,Sq,St), \+ avail_line(_,Sq,[draw(_,L)|St]). % Line L is from Sq and not yet drawn in state St. avail_line(L,Sq,St) :square_lines(Sq,Ls), member(L,Ls), \+ member(draw(_,L),St). % The legal moves legal_move(St,P,[L],[draw(P,L)|St]) :% Draw a line and stop. avail_line(L,_,St). legal_move(St,P,[L|Rest],New) :% Draw a line and go on. last_avail_line(L,_,St), legal_move([draw(P,L)|St],P,Rest,New).
c Levesque 2011
26
Running Boxes
?- win_move([ draw(min,6),draw(max,10),draw(min,2),draw(max,4), draw(min,9),draw(max,1) ], max, Move). Move = [3, 8] Yes ?- win_move([ draw(min,5),draw(max,10),draw(min,2),draw(max,4), draw(min,9),draw(max,1) ], max, Move, _). Move = [7] ; % Note: draw one line only! No ?- tie_move([ No ?- win_move([ Move = [8] ; Move = [10] ; No ?- win_move([],max,M). No draw(max,10),draw(min,2),draw(max,4), draw(min,9),draw(max,1) ], min, Move). % So Min loses from here draw(min,2),draw(max,4), draw(min,9),draw(max,1) ], max, Move). % As above
p p p p p p p p p p p p p p p p p p p p p p p p
p p p p p p p p
% Can the first player guarantee a win? % After 2 minutes of hard computing!
c Levesque 2011
27
the rst player can guarantee a win the rst player can guarantee at least a tie the second player can guarantee a win
So what exactly is the fun of playing?? Can we not simply run
?- initial state(S,white), win move(S,white,Move).
to nd out what the best rst move of Chess is, once and for all, etc.?
c Levesque 2011
28
Big games
The problem is that the win move program we have been using is practical only for small games. To decide how to move, the program considers all the states up to the end of the game. For the rst move, this means searching the entire space:
for Checkers: about 1070 states for Chess: about 10120 states for Go: about 10750 states!
Impractical for even the fastest supercomputers! For big games like these, we need to be able to decide on a best move without examining the entire space. This is what makes it challenging!
c Levesque 2011
29
replace the W, L and T (for ties) labels on the tree by numbers, where a large +ve number (say 999) means a winning position for Max, a large -ve number (say -999) means a winning position for Min, 0 means a position that is equally good for Min and Max. allow other numbers to be assigned to nodes on the tree that are not at the end of the game, by estimating how good the position is for Max.
for example, a node labelled 108 is estimated to be better for Max than a node labelled 27, which is better than a node labelled 48.
So Max will be interested in getting as large a value as possible, and Min is interested in getting as small a value as possible.
Chapter 10: Games c Levesque 2011 30
the root node of the tree is the initial state we start with; each leaf node is a state that has been given a numeric value (either at the end of the game, or as an estimate). each non-leaf node has as children the game states we can get to by making legal moves;
By looking at this tree, and the numbers on the leaf nodes, we can determine the largest number that Max can guarantee and the smallest number that Min can guarantee.
Chapter 10: Games c Levesque 2011 31
i -1 ee e
move 1
-1
f
7
f f
f f
f
9
f f
f f
-10 2
f f
-5 3
-1
-2
-12
c Levesque 2011
32
for a leaf node, the backed up value is the given value (at the end of the game or estimated); for a non-leaf Max node, the backed up value is the maximum of the backed value of its children; for a non-leaf Min node, the backed up value is the minimum of the backed value of its children.
This nally explains the names Max and Min!
Chapter 10: Games c Levesque 2011 33
Minimax in Prolog
The easiest way to handle minimax in Prolog is to think of the two players trying to get to a game state with a certain value V or better:
for Max, better means > V : for Min, better means < V :
What are the states where a player can get a value of V or better?
a nal state where the given value is V or better; a state where the player has a legal move to a new state where the opponent is limited: for Max: a new state where Min cannot get V 1. for Min: a new state where Max cannot get V + 1.
These rules will lead us to another general game playing program.
Chapter 10: Games c Levesque 2011 34
% Computing the values of moves for two players, Max and Min. % The game-dependent predicate estval estimates values of states. % The search of the game tree is limited to a depth given by D. % can_get(S,P,D,V): P can get a value of V or better in state can_get(S,max,_,V) :- game_over(S,max,W), winval(W,V1), V1 >= can_get(S,min,_,V) :- game_over(S,min,W), winval(W,V1), V1 =< can_get(S,max,0,V) :- \+ game_over(S,_,_), estval(S,max,E), E can_get(S,min,0,V) :- \+ game_over(S,_,_), estval(S,min,E), E can_get(S,P,D,V) :- \+ game_over(S,_,_), val_move(S,P,D,_,V). S. V. V. >= V. =< V.
% val_move(S,P,D,M,V): P can get a value of V or better with move M. val_move(S,max,D,M,V) :- D>0, D1 is D-1, V1 is V-1, legal_move(S,max,M,S1), \+ can_get(S1,min,D1,V1). val_move(S,min,D,M,V) :- D>0, D1 is D-1, V1 is V+1, legal_move(S,min,M,S1), \+ can_get(S1,max,D1,V1). winval(max,999). winval(min,-999). winval(neither,0). % The value of a state where Max has won % The value of a state where Max has lost % The value of a state with a tie
c Levesque 2011
35
Alpha-Beta cutoffs
...
...
5 B
in state A. Suppose that we want to find the backedup value for awe compute that Min can get Along the way, Max state A. Along the way, we get B = 5,D = 3
C E
... ...
D
Because C is a Min state, it can get value bigger than 3 cannot get a(at least) 3 at state D.
...
...
Chapter 10: Games
"!!cutoff
Since B has a value of 5, there is There is no point of continuing to look below no point in looking further below C So move directly to state E
...
36
...
Suppose that we want to find the backedup value for a Max state A. Along the way, we get B = 5,D = 3 cannot get a value bigger than 3 Since B has a value of 5, there is no point in looking further below C So move directly to state E
"!!cutoff The cutoff we saw in the previous slide is called an alpha cutoff.
...
...
...
Suppose we want to nd out what Min can get in state A. Along the way, we compute that Max can get
A B 4 E
C is now a Max state, and so (at least) 4 at smaller and that Min can get 8 cannot get a valuestate B, than 8
...
D 8
There is no point of continuing to look below state C, since it will be greater than B. Again, we can move directly to state E.
...
Huge savings in search when cutoff is high in the tree These cutoffs can be very signicant, and especially if they happen high up 199Ythe game tree. Hector Levesque 1999 in Artificial Intelligence Game Playing 31
Chapter 10: Games c Levesque 2011 37
Different games will have different methods of estimating how well or how poorly Max is doing at non-nal states of the game. A typical calculation is a sum over a number of factors that contribute to the success of a player.
c Levesque 2011
38
9Q + 5R + 3N + 3B + P
where Q, R, N , B and P is the advantage in queens, rooks, knights, bishops, and pawns, respectively.
c Levesque 2011
39
it uses minimax with alpha-beta cutoffs, but on a special computer to speed up the generation of moves and the evaluation of states
200 million board positions per second!
estimates of values of game states uses 8000 separate factors explores game trees to about depth 14, with occasional very deep searches rating of 2650 in 1996 (grandmaster = 2500)
Note: As of January 2011, the current Chess champion, Magnus Carlsen (born in 1990), had a rating of 2814!
Chapter 10: Games c Levesque 2011 40
2500
1500
...
.. .... .
..
.. .... .
. .... ..
?
The US Chess Federation rating of Chess programs seems to be proportional to the search depth.
500 4 8 12 14
depth of search
This effort so far has not helped with extremely large games like Go.
Chapter 10: Games c Levesque 2011 41
what you know means a KB of atomic and conditional sentences using means applying back-chaining
Even when we said we had to think in different ways, it always meant using back-chaining but over a different KB. But there are other ways to use what is known beyond back-chaining:
the objects: john, sue, sam, . . . the properties and relationships: the facts: child, parent, male, . . .
Prolog terms to stand for the new objects in that domain, and Prolog predicates to stand for the new relations in that domain.
c Levesque 2011
a. b. u :- p, b. p :- a.
We can represent it using a list of lists: [[a], [b], [u,p,b], [p,a]]. Similarly, a query like ?- p, b. can be represented by [p,b].
Chapter 11: Other Ways of Thinking c Levesque 2011 5
Q: How do we get this behaviour? A: We need to state in Prolog how back-chaining works!
c Levesque 2011
Based on this, we can write the clauses that characterize when the est predicate should hold:
% est(K,Q) holds if query Q can be established from KB K. est(_,[]). est(K,[A|T]) :- member([A|B],K), append(B,T,Q), est(K,Q).
These two simple clauses are all we need to get the behaviour we want!
Chapter 11: Other Ways of Thinking c Levesque 2011 7
we can consider other forms of thinking that diverge from Prolog. Here is a very simple variant of est:
% estbf(K,Q) holds if Q can be established from K. % The method is a breadth-first variant of back-chaining estbf(_,[]). estbf(K,[A|T]) :- member([A|B],K), append(T,B,Q), estbf(K,Q).
c Levesque 2011
This is a simple form of explanation: in the example above, we would say that given what is known, b would explain u being true. A simple form of learning is similar except that we need to use variables: What clause (with variables) could I add to my KB to allow observations O1 , . . . , On to be established.
c Levesque 2011
c Levesque 2011
c Levesque 2011
understanding English sentences; making sense of a visual scene; planning how to achieve a goal; solving puzzles like Sudoku; playing games like chess.
What these very different activities all have in common, is that when they are performed by people, they seem to require thought.
c Levesque 2011
One of the major challenges of current AI research is to nd ways of dealing with more realistic problems, and we got a glimpse of some:
how to do better than generate and test how to represent action and change for large worlds how to play games without searching the entire game tree etc.
Chapter 12: Conclusion c Levesque 2011 4
they are not people; they do not have bodies; they do not souls;
How can we resolve this?
Chapter 12: Conclusion c Levesque 2011 5
etc.
c Levesque 2011
Chinese Room
c Levesque 2011
the person in the room does not know how to add there is an instruction book that tells him that to do with the input to produce the output the input is a list consisting of just twenty 10-digit numbers the output is a 12-digit number that happens to be the sum of the input numbers
Summation Room
1 0 1 9 9 7 1 6 6 2 8 6
After doing this for all the 20 numbers in the list, there will be a number written in the book with at most 12 digits. Write that number on a slip of paper and hand that message back outside the room.
Now suppose that the book is constructed so that the 12-digit number is the sum of the 20 numbers required to get there! Then this book allows the person in the room to get the right answers without showing him how to add, just like in the Chinese Room!
Chapter 12: Conclusion c Levesque 2011 11
The catch!
The catch is that this book cannot possibly exist! It would need to contain 1010 1010 1010 = 10200 entries. But our universe only has about 10100 atoms! However, . . . There is a much smaller book that will work: Have a look at PROC0, PROC1, PROC2 etc. on Slides 1218 which we studied at the start of this course. A book that has the instructions on these slides will allow a person who does not know how to add to get the right answers! The big difference: anyone memorizing this book is learning to add. This suggests that if the behaviour is complex enough, it cannot be faked!
Chapter 12: Conclusion c Levesque 2011 12
A nal word
In the end, maybe the real question about the Turing test, the Chinese Room, the Summation Room etc. is not Is a computer that is behaving intelligently really and truly thinking? but rather What does it take to produce that intelligent behaviour? This is not so much a philosophical question as a scientic one! In this course, we saw very simple forms of intelligent behaviour and how to achieve them. But this is really just the start. ...
c Levesque 2011
13
THE END
c Levesque 2011
14