Prolog Coding
Prolog Coding
Nondeterminism
Prolog arithmetic can be a little surprising; you need to be
aware of modes. Sometimes nondeterminism needs to be
managed.
1. Arithmetic
2. Operators
3. Modes and Arithmetic
4. Expressions
5. Managing Nondeterminism
6. Arithmetic and Lists
85
:- 4.1 Arithmetic
In most languages 3 + 4 is an expression that has a value
In Prolog, 3 + 4 is just a term whose functor is + and
arguments are 3 and 4
It’s not code, it’s data
The Prolog builtin = just unifies (matches) two terms, it
does not evaluate expressions
?- X = 3 + 4.
X = 3+4 ;
No
86
:- Arithmetic (2)
Use the built in is/2 predicate to compute the value of an
arithmetic expression:
?- is(X, 3+4).
X = 7 ;
No
87
:- 4.2 Operators
is is actually an infix operator in Prolog
This means that instead of writing is(X,3+4) we could also
have written X is 3+4
This is easier to read, so it preferred, although both work
Similarly, + is an operator; 3+4 is the same as +(3,4)
Operators in Prolog are not exclusively for arithmetic, they
are part of Prolog’s syntax
Prolog has many other standard operators; some we have
already seen, including :- , ; mod
Operators only affect syntax, not meaning
You can define your own operators
88
:- 4.2.1 Precedence and Associativity
Operators have precedence and associativity
Use parentheses for grouping
E.g. * and / have higher precedence than + and -; they all
associate to the left
?- X is 3 + 4 * 5, Y is (3 + 4) * 5.
X = 23
Y = 35
Yes
?- X is 5 - 3 - 1, Y is 5 - (3 - 1).
X = 1
Y = 3
Yes
89
:- 4.2.2 Display
The built-in predicate display/1 is useful for understanding
how your input will be parsed when it includes operators
display/1 prints a term as Prolog understands it, using
only the standard form with the functor first, and
arguments between parentheses
?- display(3 + 4 * 5), nl, display((3 + 4) * 5).
+(3, *(4, 5))
*(+(3, 4), 5)
Yes
90
:- 4.3 Modes and Arithmetic
We could code a predicate to compute a square like this:
square(N, N2) :- N2 is N * N.
?- square(5, X).
X = 25 ;
No
?- square(X, 25).
ERROR: Arguments are not sufficiently instantiated
91
:- Modes and Arithmetic (2)
Unfortunately, is/2 only works when the second argument
is ground
The first argument can be unbound or bound to a number
?- X is 3 + Y.
ERROR: Arguments are not sufficiently instantiated
?- 7 is 3 + 4.
Yes
?- 9 is 3 + 4.
No
?- 2 + 5 is 3 + 4.
No
92
:- 4.4 Expressions
Some arithmetic expressions understood by is/2:
E1 + E2 addition float(E1) float equivalent of E1
E1 - E2 subtraction E1 << E2 left shift
E1 * E2 multiplication E1 >> E2 right shift
E1 / E2 division E1 /\ E2 bitwise and
E1 // E2 integer division E1 \/ E2 bitwise or
E1 mod E2 modulo (sign of E1) \ E1 bitwise complement
-E1 unary minus min(E1, E2) minimum
abs(E1) absolute value max(E1, E2) maximum
integer(E1) truncate toward 0
93
:- Expressions (2)
Some useful arithmetic predicates:
94
:- 4.5 Managing Nondeterminism
A common Prolog programming error is treating multiple
clauses as if they formed an if-then-else construct
This is an erroneous definition of absolute_value:
absolute_value(X, X) :- X >= 0.
absolute_value(X, Y) :- Y is -X.
?- absolute_value(-3, V).
V = 3 ;
No
?- absolute_value(42, V).
V = 42 ;
V = -42 ;
No
95
:- Managing Nondeterminism (2)
The problem is that the second clause promises that
|n| = −n
absolute_value(X, X) :- X >= 0.
absolute_value(X, Y) :- X < 0, Y is -X.
?- absolute_value(-3, V).
V = 3 ;
No
?- absolute_value(42, V).
V = 42 ;
No
96
:- Exercise
Define a Prolog predicate maximum(X,Y,Z) such that Z is the
larger of X and Y
97
:- 4.5.1 Fibonacci numbers
Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, . . .
fib(0, 1).
fib(1, 1).
fib(N, F) :-
N > 1,
N1 is N - 1,
N2 is N - 2,
fib(N1, F1),
fib(N2, F2),
F is F1 + F2.
98
:- 4.6 Lists and Arithmetic
Summing a list combines arithmetic with list processing:
99
:- 4.6.1 List Maximum
Compute the maximum element of a list
maxlist([X], X).
maxlist([X|Xs], Max) :-
maxlist(Xs, Max1),
maximum(Max1, X, Max).
100
:- 4.6.2 List Length
The length/2 built-in relates a list to its length
?- length([a,b,c], 3).
Yes ?- length(L, N).
L = []
?- length([a,b,c], N). N = 0 ;
N = 3 ;
No L = [_G278]
N = 1
?- length(L, 3).
L = [_G263, _G266, _G269] ; Yes
No
It is not straightforward to implement length/2 with
flexible modes; covered later
101
:- 4.6.3 List Handling
Many other list predicates can be defined using append,
length.
member/2 could be defined just in terms of append:
102
:- List Handling (2)
T is a list taking the first N elements of L:
103
104
105
106
107
108
:- 5 Coding in Prolog
We work through examples to illustrate how to write
Prolog code
Our goal: support code for a computerized card game
3. Shuffling
4. Dealing
109
:- 5.1 Data
For any language, first determine:
110
:- Data (2)
Suits and Ranks have no attributes
Their primitive operations are only distinguishing them and
possibly enumerating them
A card comprises a suit and a rank — those are its
attributes
Constructing a card from rank and suit, and finding a
card’s rank and suit are its only primitive operations
A deck is just an (ordered) sequence of cards
Primitive operations include removing the top card or
adding a card on top; all other operations can be
implemented in terms of those
111
:- 5.2 Representation
Simplest Prolog representations for types without attributes
are atoms and numbers, e.g., hearts, jack, 10, etc
Good idea to write a predicate for each type to specify
what are members of the type
Same code can enumerate values
suit(clubs). suit(diamonds).
suit(hearts). suit(spades).
112
:- Representation (2)
Simplest Prolog representation for type with attributes is
compound term, one argument for each attribute
For playing card: card(R, S), where R is a rank and S is a
suit
card(card(R,S)) :-
suit(S),
rank(R).
Since rank and suit will enumerate the ranks and suits, this
code will enumerate the cards of a deck
113
:- Representation (3)
Deck is just a sequence of any number of cards
Prolog has good support for lists — good representation
for sequences
List is a deck if each element is a card, i.e., if empty, or if
first element is a card and rest is a deck
deck([]).
deck([C|Cs]) :-
card(C),
deck(Cs).
114
:- Exercise: Bridge Hand Evaluation
In Bridge, a hand has 4 high card points for each ace, 3,
for each king, 2 for each queen, and 1 for each jack.
Write a predicate to determine how many high card points
a hand has.
115
:- 5.3 Generating a Full Deck of Cards
deck predicate holds for any deck; we need a full deck
card predicate generates all cards in a deck:
?- card(C).
C = card(2, clubs) ;
C = card(3, clubs) ;
C = card(4, clubs) ;
C = card(5, clubs) ;
C = card(6, clubs) ;
C = card(7, clubs)
116
:- 5.3.1 All Solutions Predicates
Prolog has built-in predicates for collecting solutions
findall(T, G, L) — L is a list of all terms T that satisfy
goal G in the order solutions are found; variables in G are
left uninstantiated; deterministic
setof(T, G, L) — L is a non-empty list of all terms T that
satisfy goal G; L is sorted with duplicates removed;
variables appearing only in G can be instantiated; can be
nondeterministic (with different variable instantiations)
bagof(T, G, L) — like setof/3 but without the sorting
NB: Template term T is left uninstantiated by all of these
predicates
117
:- All Solutions Predicates (2)
C = _G151
Deck = [card(2, clubs), card(3, clubs), card(4, clubs),
card(5, clubs), card(6, clubs), card(7, clubs),
card(8, clubs), card(9, clubs), card(..., ...)|...]
C = _G151
Deck = [card(2, clubs), card(2, diamonds), card(2, hearts),
card(2, spades), card(3, clubs), card(3, diamonds),
card(3, hearts), card(3, spades), card(..., ...)|...]
118
:- All Solutions Predicates (3)
We don’t care about order, and card predicate generates
each card exactly once, so either will work
Use bagof: no point sorting if not needed
new_deck(Deck) :-
bagof(C, card(C), Deck).
Call it new deck because it only holds for lists of all 52 cards
in the order of a brand new deck
NB: setof and bagof fail if goal G has no solutions — you
don’t get the empty list
119
:- All Solutions Predicates (4)
NB: When goal G includes some variables not appearing in
template T , setof and bagof generate a different list L for
each distinct binding of those variables
?- setof(S, borders(State,S), Neighbors).
S = _G152
State = vic
Neighbors = [nsw, sa] ;
S = _G152
State = act
Neighbors = [nsw] ;
S = _G152
State = qld
Neighbors = [nsw, nt, sa]
120
:- 5.4 Coding Tips
Prolog lists are very flexible, used for many things
Can do a lot with just length/2 and append/3
When writing list processing predicates, often have one
clause for [] and one for [X|Xs]
It is often easier to write a predicate if you pretend all
arguments are inputs
Think of it as checking correctness of output, rather than
generating output
Then consider whether your code will work in the modes
you want
121
:- Exercise: Selecting the nth Element
Implement select nth1(N, List, Elt, Rest) such that Elt is
the N th element of List, and Rest is all the other elements
of List in order. It need only work when N is bound.
122
:- 5.5 Shuffling
Don’t want to faithfully simulate real shuffling — would
not be random
Shuffling really means randomly permute list order
Simple method: repeatedly randomly select one card from
deck as next card until all cards selected
shuffle([], []).
shuffle(Deck0, [C|Deck]) :-
random_element(Deck0, C, Deck1),
shuffle(Deck1, Deck).
123
:- 5.5.1 Selecting a Random Card
Need to know size of deck to know probability of selecting
any one card
Then we can pick a random number n where 1 ≤ n ≤ length
and remove element n of the deck
Must give back remainder of deck as well as random card,
since we cannot destructively remove card from deck
124
:- 5.5.2 Random Numbers
No such thing as a random number ; it’s how the number is
selected that is random
No standard random number predicate in Prolog
Good practice: define reasonable interface, then look in
manual for built-ins or libraries to implement it
This makes maintenance easier
SWI defines random/1 function (not really a function!)
125
:- 5.6 Dealing
Interface for dealing:
126
:- Dealing (2)
Easiest solution: take first Numcards cards from Deck for
first player, next Numcards for next player, etc
Use append and length to take top (front) of Deck
127
128
129
130
131
132
:- 6 Determinism
Deterministic Prolog code is much more efficient than
nondeterministic code. It can make the difference between
running quickly and running out of stack space or time.
1. Prolog implementation
2. Determinacy
3. If-then-else
4. Indexing
133
:- 6.1 Prolog implementation
Actual implementation of Prolog predicates is stack-based
a :- b, c. c :- fail.
b :- d, e. d :- e.
b :- e. e.
To execute a, we invoke b, then c
While executing b, we must remember to go on to c; Like
most languages, Prolog uses a call stack
While executing first clause of b, we must remember if we
fail, we should try the second
Prolog implements this with a separate stack, the
choicepoint stack
When a choicepoint is created, the call stack must be
frozen: cannot overwrite current call stack entries because
they may be needed after choicepoint is popped
134
:- 6.1.1 Stack Example
0 a :− call choicepoint
1 b, stack stack
2 c. 2
3 b :−
4 d,
5 e.
6 b :−
7 e.
8 d :−
9 e.
10 c :−
11 fail.
12 e.
135
:- Stack Example (2)
0 a :− call choicepoint
1 b, stack stack
2 c. 2 6
3 b :− 5
4 d,
5 e.
6 b :−
7 e.
8 d :−
9 e.
10 c :−
11 fail.
12 e.
136
:- Stack Example (3)
0 a :− call choicepoint
1 b, stack stack
2 c. 2 6
3 b :−
4 d,
5 e.
6 b :−
7 e.
NB: call stack not popped!
8 d :−
9 e.
10 c :−
11 fail.
12 e.
137
:- Stack Example (4)
0 a :− call choicepoint
1 b, stack stack
2 c.
3 b :−
4 d,
5 e.
6 b :−
7 e.
8 d :−
9 e.
10 c :−
11 fail.
12 e.
138
:- 6.2 Determinacy
Pushing a choicepoint causes current call stack to be
frozen
We can’t write over frozen part of call stack, so it must be
kept around indefinitely
One choicepoint can freeze a large amount of stack space
So avoiding choicepoints is important to efficiency
A predicate with multiple solutions should leave a
choicepoint
A predicate with only one solution should not
139
:- 6.3 Indexing
Indexing makes Prolog programs more deterministic than
you might expect them to be
Indexing takes a sequence of adjacent clauses whose
first argument is bound and constructs an index to
quickly select the right clause(s)
When a goal has a non-variable first argument, indexing
allows Prolog to immediately choose the clause(s) whose
first argument matches the goal’s first argument
Other clauses are not even considered
Indices are constructed automatically; no special action
needed
You can ask SWI Prolog to index on arguments other than
the first using the index or hash declarations
140
:- 6.3.1 Indexing Example
For example, for the code
capital(tas, hobart). capital(vic, melbourne).
capital(nsw, sydney). capital(sa, adelaide).
capital(act, canberra). capital(qld, brisbane).
capital(nt, darwin). capital(wa, perth).
?- capital(vic, X)
141
:- 6.3.2 Indexing for Determinism
When possible, use indexing to avoid unnecessary
choicepoints
On most Prolog systems, only the outermost constructor
of the first argument is indexed, e.g. with code:
142
:- Indexing for Determinism (2)
Indexing is used even on predicates with only two clauses:
it allows many predicates to avoid choicepoints
append([], L, L).
append([J|K], L, [J|KL]) :-
append(K, L, KL).
143
:- 6.4 If-Then-Else
Our definition of absolute_value/2 from slide 96 was a bit
unsatisfying because both clauses compare X to 0:
absolute_value(X, X) :- X >= 0.
absolute_value(X, Y) :- X < 0, Y is -X.
144
:- If-Then-Else (2)
Prolog also has an if-then-else construct, whose syntax is
( A -> B ; C )
% "not provable"
\+ G :- (G -> fail ; true).
145
:- If-Then-Else (3)
Defining absolute_value/2 with only one comparison:
absolute_value(X, Y) :-
( X < 0 ->
Y is -X
; Y = X
).
One clause which fills the role of both clauses in prev. defn.
?- absolute_value(-3, V).
V = 3 ;
No
?- absolute_value(42, V).
V = 42 ;
No
146
:- If-Then-Else (4)
If-then-elses can be nested to give an if-then-elseif-else
structure.
comparison(X, Y, Rel) :-
( X < Y -> Rel = less
; X = Y -> Rel = equal
; Rel = greater
).
?- comparison(3, 4, X).
X = less ;
No
?- comparison(4, 3, X).
X = greater ;
No
?- comparison(4, 4, X).
X = equal ;
No
147
:- 6.4.1 Caution
148
:- 6.4.2 Cautionary Example
Example: list_end(List, Sublist, End) holds when Sublist
is the End end of List, and End is front or back
Buggy implementation:
149
:- Cautionary Example (2)
This code works in simple cases:
?- list_end([a,b,c,d], [a,b], End).
End = front ;
No
?- list_end([a,b,c,d], [d], End).
End = back ;
No
150
:- Cautionary Example (3)
There is a bigger flaw: the if-then-else commits to the first
solution of append
In many modes this is completely wrong
Sublist = []
End = front ;
No
?- list_end([a,b,c], Sublist, back).
No
151
:- Cautionary Example (4)
Correct implementation does not use if-then-else at all
list_end(List, Sublist, front) :- append(Sublist, _, List).
list_end(List, Sublist, back) :- append(_, Sublist, List).
?- list_end([a,b], Sublist, End).
Sublist = []
End = front ;
Sublist = [a]
End = front ;
Sublist = [a, b]
End = front ;
Sublist = [a, b]
End = back ;
Sublist = [b]
End = back ;
Sublist = []
End = back ;
No
152
:- 6.4.3 Removing Unnecessary Choicepoints
Suppose a ground list L represents a set
member(foo,L) checks if foo∈L, but leaves a choicepoint
because there may be more than 1 way to prove foo∈ l
153
:- 6.4.4 When to Use If-Then-Else
It’s safe to use if-then-else when:
154
155
156
:- 7 Search
Prolog’s efficient built-in search mechanism makes it ideal
for solving many sorts of problems that are tricky in
conventional languages
1. Path Finding
2. Iterative Deepening
3. 8 Queens
4. Constraint Programming
157
:- 7.1 Path Finding
Many problems come down to searching for a path through
a graph
For example, we may want to know if a person alice is a
descendant of another person zachary
If we have a relation parent/2 that specifies who is a
parent of whom, then ancestor/2 is the transitive closure
of parent/2
Transitive closure follows this pattern:
ancestor(A, A).
ancestor(D, A) :-
parent(D, P),
ancestor(P, A).
158
:- Path Finding (2)
Finding paths in a graph is finding the transitive closure of
the adjacency/edge relation of the graph
Suppose a predicate edge/2 defines a graph:
159
b
d
a
edge(a,b). edge(a,c).
c
edge(b,d). edge(c,d).
edge(d,e). edge(f,g).
g
f
160
connected(N, N).
connected(L, N) :- edge(L, M), connected(M, N).
161
:- Path Finding (3)
A small change in the graph makes it go badly wrong:
a f
b c
g
edge(a,b). edge(a,c).
d edge(b,d). edge(c,d).
edge(d,e). edge(f,g).
e
The graph now has a “cycle” (cannot happen for
ancestors!)
There is nothing to stop the Prolog code exploring the
“infinite” path
162
:- Path Finding (4)
?- trace, connected(a,e).
Call: (8) connected(a, e) ? creep
Call: (9) edge(a, _G215) ? creep
Exit: (9) edge(a, b) ? creep
Call: (9) connected(b, e) ? creep
Call: (10) edge(b, _G215) ? creep
Exit: (10) edge(b, d) ? creep
Call: (10) connected(d, e) ? creep
Call: (11) edge(d, _G215) ? creep
Exit: (11) edge(d, c) ? creep
Call: (11) connected(c, e) ? creep
Call: (12) edge(c, _G215) ? creep
Exit: (12) edge(c, a) ? creep
Call: (12) connected(a, e) ? creep
Call: (13) edge(a, _G215) ? creep
Exit: (13) edge(a, b) ?
163
:- Path Finding (5)
Solution: keep a list of nodes visited along a path, and
don’t revisit a node already on the list
connected(N, _, N).
connected(L, Path, N) :-
edge(L, M),
\+ member(M, Path),
connected(M, [M|Path], N).
?- connected(a, e).
Yes
Remember: \+ is negation
Also note the algorithmic complexity of this code is O(2N )
164
:- 7.2 Word Paths
Sometimes the path is the desired output
Word game: transform one word into another of the same
length by repeatedly replacing one letter of the word with
another so that each step is a valid English word
E.g., tranform “big” to “dog”
165
:- Word Paths (2)
166
:- Word Paths (3)
Also load our strings module from slide 201 for readable
output
167
:- Word Paths (4)
168
:- 7.3 Iterative Deepening
Often we don’t want just any solution; we want a shortest
one
Even if we don’t need a shortest solution, we may want to
avoid infinite (or just huge) branches in the search space
One way to do this is breadth first search:
169
:- Iterative Deepening (2)
Breadth first search is often impractical due to its high
memory usage
Often Iterative Deepening is a simple, practical, efficient
alternative
Iterative deepening works by doing a depth first search for
a short solution
If this works, great; if not, start over looking for a longer
solution
Repeat until a solution is found
Work is repeated, but often the cost of doing a depth n
search is much smaller than the cost of a depth n + 1
search, so the waste is relatively small
170
:- 7.3.1 Word Paths Again
We can implement a predicate to find the shortest
solutions to the word game as follows:
First determine the length of the shortest solution
Then commit to that length and find solutions of exactly
that length
shortest_transform(Word0, Word, Steps) :-
( length(Steps0, Len),
transform(Word0, Word, Steps0) ->
length(Steps, Len),
transform(Word0, Word, Steps)
; fail
).
171
:- Word Paths Again (2)
?- time(shortest_transform("big","dog",X)).
% 100 inferences, 0.00 CPU in 0.00 seconds (0% CPU, Infinite Lips)
No
172
:- 7.4 8 Queens
Classic puzzle: place 8 queens on a chess board such that
none attack any others.
173
:- 8 Queens (2)
174
:- 8 Queens (3)
175
:- 8 Queens (4)
If first queen attacks second, no point placing any more,
so: add queens to board one at a time, checking after each
addition
Sneaky trick: add new queen in first row, sliding other
queens down
Another trick: number rows from 0, so column − row =
column + row = column
Check if queen in row 0 attacks queens in rows 1 . . . n:
noattack(Q, Qs) :- noattack(Q, 1, Qs).
noattack(_, _, []).
noattack(Q0, Row, [Q|Qs]) :-
Q0 =\= Q + Row,
Q0 =\= Q - Row,
Row1 is Row + 1,
noattack(Q0, Row1, Qs).
176
:- 8 Queens (5)
The main body of the code chooses columns from a list of
all possible columns until all queens have been placed.
Note the the last two arguments of queens/3 are an
accumulator pair.
queens(N, Qs) :-
range(1, N, Columns), % Columns = 1..8
queens(Columns, [], Qs).
177
:- 8 Queens (6)
The necessary utility predicates
178
:- 8 Queens (7)
Try it:
?- queens(8, L).
L = [4, 2, 7, 3, 6, 8, 5, 1] ;
L = [5, 2, 4, 7, 3, 8, 6, 1] ;
L = [3, 5, 2, 8, 6, 4, 7, 1]
Yes
?- time(queens(8, L)).
% 4,691 inferences in 0.01 seconds (469100 Lips)
L = [4, 2, 7, 3, 6, 8, 5, 1]
Yes
?- time(queens(20, L)).
% 35,489,394 inferences in 211.64 seconds (167688 Lips)
179
:- 7.5 Constraint Programming
• Much better handled by constraint (logic)
programming.
– Then search
180
:- 8 Efficiency and I/O
Correctness is much more important than efficiency, but
once your code is correct, you may want to make it fast.
You may also want to input or output data.
1. Tail Recursion
2. Accumulators
3. Difference Pairs
181
:- 8.1 Tail Recursion
For efficiency, when calling the last goal in a clause body,
Prolog jumps straight to that predicate without pushing
anything on the call stack
This is called last call optimization or tail recursion
optimization
tail recursive means the recursive call is the last goal in the
body
A tail recursive predicate is efficient, as it runs in constant
stack space; it behaves like a loop in a conventional
language
182
:- 8.2 Accumulators
The natural definition of factorial in Prolog:
% F is N factorial
factorial(0, 1).
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is F1 * N.
183
:- Accumulators (2)
We make factorial tail recursive by introducing an
accumulating parameter, or just an accumulator
This is an extra parameter to the predicate which holds a
partially computed result
Usually the base case for the recursion will specify that the
partially computed result is actually the result
The recursive clause usually computes more of the partially
computed result, and passes this in the recursive goal
The key to getting the implementation correct is specifying
what the accumulator means and how it relates to the final
result
184
:- 8.2.1 Accumulator Example
A tail recursive definition of factorial using an
accumulator:
185
:- Accumulator Example (2)
To see how to add an accumulator, determine what is done
after the recursive call
Respecify the predicate so it performs this task, too
For factorial, we compute factorial(N1, F1), F is F1 * N,
so we want factorial to perform the multiplication for us
too
186
:- Accumulator Example (3)
Replace the call to Simplifying arithmetic:
factorial(N, FN) by the
body: unfold
187
:- Accumulator Example (4)
By associativity of Replace the copy of the
multiplication: definition of new factorial:
factorial(N1, F1), F is F1 * ?
by call to new_factorial:
fold
188
:- Exercise
Rewrite the mult predicate from an earlier exercise to be
tail recursive. Here is the old code:
189
:- 8.3 Difference Pairs
Accumulators can be even more useful for building up lists
Recall our rev/2 predicate:
rev([], []).
rev([A|BC], R) :-
rev(BC, CB),
append(CB, [A], R).
% rev(BC, A, CBA)
% CBA is BC reversed with A appended
% to the end
190
:- 8.3.1 Difference Pairs Example
Next revise the definition of rev
rev([], A, A).
rev([B|CD], A, DCBA) :-
rev(CD, [B|A], DCBA).
191
:- Difference Pairs Example (2)
When working with lists, using accumulators is very
common
Common to have an accumulator for each list being
constructed
Thus lists are often passed as a pair of arguments: one the
actual result, and the other the list to be appended to the
natural result to get the actual result
Can think of the natural result of the operation as the
actual result minus the accumulator argument
Such pairs of arguments are called difference pairs or
difference lists
Common to put such pairs in the opposite order,
accumulator second, to emphasize that the accumulator is
the tail end of the actual result
192
:- Difference Pairs Example (3)
% tree_list(Tree, List)
% List is a list of the elements of tree,
% in order
tree_list(Tree, List) :-
tree_list(Tree, List, []).
193
:- 8.4 Term I/O
?- write(hello).
hello
Yes
?- write(42).
42
Yes
?- write([a,b,c]).
[a, b, c]
Yes
?- write((3+(4*5)*6)).
3+4*5*6
Yes
194
:- 8.4.1 Read Example
?- read(X).
|: [a,b,c].
X = [a, b, c] ;
No
?- read(X).
|: foo(A,3,A /* repeated variable */).
X = foo(_G231, 3, _G231) ;
No
?- read(X).
|: 7
|: .
X = 7 ;
No
195
:- 8.5 Character I/O
An individual character can be written with the put/1
predicate
Prolog represents characters as integers character codes
Can specify character code for a character by preceding it
with “0’” (no matching close quote)
?- put(65), nl.
A
Yes
?- put(0’a), nl.
a
Yes
?- put(0’a), put(0’ ), put(0’z).
a z
Yes
196
:- 8.5.1 Reading Characters
The built-in get0/1 predicate reads a single chararacter
from the current input stream as a character code
get0/1 returns -1 at end of file
C1 = 104
C2 = 105
C3 = 10 ;
No
197
:- 8.5.2 Defining I/O Predicates
A predicate to write a line of text as a list of character
codes:
write_line([]) :- nl.
write_line([C|Cs]) :- put(C), write_line(Cs).
198
:- Exercise: Read a Line of Input
Write a predicate to read a line of input as a list of
character codes. read line(Line) should read a line from
the current input stream, binding Line to the list of
character codes read, without the terminating newline
character.
199
:- 8.5.3 “String” Notation
?- read_line(X), write_line(X).
|: hello world!
hello world!
X = [104, 101, 108, 108, 111, 32, 119, 111, 114|...] ;
No
200
:- 8.6 print and portray
Query and debugger output is done with print/1
Mostly the same as writeq/1, but you can reprogram it by
defining predicate user:portray/1
:- multifile user:portray/1. chars([]).
chars([C|Cs]) :-
user:portray(Term) :- printable(C),
ground(Term), chars(Cs).
chars(Term),
!, printable(C) :-
put(0’"), integer(C),
putchars(Term), ( code_type(C, graph)
put(0’"). ; code_type(C, space)
).
putchars/1 is like write_line/1 without call to nl/0
Note: ! is called “cut”. It prevents backtracking to other
clauses (use -> instead if at all possible)
201
:- print and portray (2)
With this code loaded, Prolog prints lists of character
codes as quoted strings.
Without portray code With portray code
?- print("abcd"). ?- print("abcd").
[97, 98, 99, 100] "abcd"
Yes Yes
?- X = "abcd". ?- X = "abcd".
X = [97, 98, 99, 100] X = "abcd"
Yes Yes
Code is available in strings.pl
202
:- 8.7 Order Sensitivity
Like arithmetic in Prolog, I/O predicates are sensitive to
the order in which they are executed
write(hello), write(world) always prints out helloworld,
and never worldhello, although conjunction is meant to be
commutative
Backtracking cannot undo I/O, e.g. get0(0’a) reads a
character and fails if it is not an “a”, but does not put
back the character if it fails
Term output puts out a term as it is, so
?- write(foo(X)), X=bar. ?- X=bar, write(foo(X)).
foo(_G258) foo(bar)
X = bar ; X = bar ;
No No
203
:- 8.7.1 Best Practice
Due to the procedural nature of Prolog I/O, it is usually
best to keep the I/O part of an application as isolated as
possible
E.g. write predicates that read in all input and build some
appropriate term to represent it, then write code that
processes the term
E.g. write predicates that build up output in some
appropriate term representation, then have separate code
to perform the actual output
This makes debugging easier: you cannot easily retry goals
that read input, as retry does not automatically rewind
input streams
204
:- 9 Parsing in Prolog
Prolog was originally developed for programming natural
language (French) parsing applications, so it is well-suited
for parsing
1. DCGs
2. DCG Translation
3. Tokenizing
4. Example
205
:- 9.1 DCGs
• A parser is a program that reads in a sequence of
characters, constructing an internal representation
206
:- 9.1.1 Simple DCG
Could specify a number to be an optional minus sign, one
or more digits optionally followed by a decimal point and
some more digits, optionally followed by the letter ’e’ and
some more digits:
number -->
( "-"
; ""
),
digits,
( ".", digits number → (-|)digits(. digits|)
; "" (e digits|)
),
( "e", digits
; ""
).
207
:- Simple DCG (2)
Define digits:
digits -->
( "0" ; "1" ; "2" ; "3" ; "4"
; "5" ; "6" ; "7" ; "8" ; "9" digit → 0|1|2|3|4|
), 5|6|7|8|9
( digits
; ""
digits → digit digits|digit
).
A sequence of digits is one of the digit characters followed
by either a sequence of digits or nothing
208
:- 9.1.2 Using DCGs
Test a grammar with phrase(Grammar,Text) builtin, where
Grammar is the grammar element (called a nonterminal)
and Text is the text we wish to check
?- phrase(number, "3.14159").
Yes
?- phrase(number, "3.14159e0").
Yes
?- phrase(number, "3e7").
Yes
?- phrase(number, "37").
Yes
?- phrase(number, "37.").
No
?- phrase(number, "3.14.159").
No
209
:- Using DCGs (2)
It’s not enough to know that "3.14159" spells a number, we
also want to know what number it spells
Easy to add this to our grammar: add arguments to our
nonterminals
Can add “actions” to our grammar rules to let them do
computations
Actions are ordinary Prolog goals included inside braces {}
210
:- 9.1.3 DCGs with Actions
DCG returns a number N
number(N) -->
( "-" ->
{ Sign = -1 }
; { Sign = 1 }
),
digits(Whole),
( ".", frac(Frac),
{ Mantissa is Whole + Frac }
; { Mantissa = Whole }
),
( "e", digits(Exp),
{ N is Sign * Mantissa * (10 ** Exp) }
; { N is Sign * Mantissa }
).
211
:- DCGs with Actions (2)
digits(N) -->
digits(0, N). % Accumulator
digits(N0, N) --> % N0 is number already read
[Char],
{ 0’0 =< Char },
{ Char =< 0’9 },
{ N1 is N0 * 10 + (Char - 0’0) },
( digits(N1, N)
; { N = N1 }
).
"c" syntax just denotes the list with 0’c as its only member
The two comparison goals restrict Char to a digit
212
:- DCGs with Actions (3)
frac(F) -->
frac(0, 0.1, F).
frac(F0, Mult, F) -->
[Char],
{ 0’0 =< Char },
{ Char =< 0’9 },
{ F1 is F0 + (Char - 0’0)*Mult },
{ Mult1 is Mult / 10 },
( frac(F1, Mult1, F)
; { F = F1 }
).
213
:- Exercise: Parsing Identifiers
Write a DCG predicate to parse an identifier as in C: a
string of characters beginning with a letter, and following
with zero or more letters, digits, and underscore
characters. Assume you already have DCG predicates
letter(L) and digit(D) to parse an individual letter or digit.
214
:- 9.2 DCG Translation
• DCGs are just an alternative syntax for ordinary Prolog
clauses
215
:- 9.2.1 DCG Expansion
216
:- 9.2.2 DCG Expansion Example
For example, our digits/2 nonterminal is translated into a
digits/4 predicate:
digits(N0, N) --> digits(N0, N, S, S0) :-
[Char], ’C’(S, Char, S1),
{ 0’0 =< Char }, 48 =< Char,
{ Char =< 0’9 }, Char =< 57,
{ N1 is N0*10 N1 is N0*10
+ (Char-0’0) }, + (Char-48),
( digits(N1, N) ( digits(N1, N, S1, S0)
; "", ; N=N1,
{ N = N1 } S0=S1
). ).
(SWI Prolog’s translation is equivalent, but less clear.)
217
:- 9.3 Tokenizing
• Parsing turns a linear sequence of things into a
structure.
218
:- Tokenizing (2)
Suppose we want to write a parser for English
It is easier to parse words than characters, so we choose
words and punctuation symbols as our tokens
We will write a tokenizer that reads one character at a
time until a full sentence has been read, returning a list of
words and punctuation
219
:- 9.3.1 A Simple Tokenizer
sentence_tokens(Words) :-
get0(Ch),
sentence_tokens(Ch, Words).
sentence_tokens(Ch, Words) :-
( Ch = 10 -> %% end of line
Words = []
; alphabetic(Ch) ->
get_letters(Ch, Ch1, Letters),
atom_codes(Word, Letters),
Words = [Word|Words1],
sentence_tokens(Ch1, Words1)
; get0(Ch1),
sentence_tokens(Ch1, Words)
).
220
:- A Simple Tokenizer (2)
alphabetic(Ch) :-
( Ch >= 0’a, Ch =< 0’z ->
true
; Ch >= 0’A, Ch =< 0’Z ->
true
).
get_letters(Ch0, Ch, Letters) :-
( alphabetic(Ch0) ->
Letters = [Ch0|Letters1],
get0(Ch1),
get_letters(Ch1, Ch, Letters1)
; Ch = Ch0,
Letters = []
).
221
:- 9.4 Parsing Example
• Can write parser using standard approach to English
grammar
222
:- 9.4.1 DCG Phrase Parser
223
:- 9.4.2 Parsing for Meaning
• Two problems:
1. no indication of the meaning of the sentence
224
:- 9.4.3 Parsing for Structure
sentence(action(Verb,Tense,Subject,Object)) -->
noun_phrase(Subject, Number, Person, nominative),
verb_phrase(Verb, Tense, Number, Person, Object).
sentence(action(Verb,Tense,imperative,Object)) -->
verb_phrase(Verb, Tense, _, second, Object).
225
:- Parsing for Structure (2)
determiner(Definite,Number) -->
word1(determiner(Definite,Number)).
noun(Thing, Number) -->
word1(noun(Thing,Number)).
proper_noun(Thing,Number) -->
word1(proper_noun(Thing,Number)).
pronoun(Person, Number, Case, Gender) -->
word1(pronoun(Person,Number,Case,Gender)).
verb(Verb,Number,Person,Tense) -->
word1(verb(Verb,Ending)),
{ verb_agrees(Ending, Number, Person, Tense) }.
226
:- 9.4.4 Parsing Examples
?- phrase(sentence(Meaning), [the,boy,carried,the,book]).
Meaning = action(carry,past,count(boy,definite,singular),
count(book,definite,singular)) ;
No
?- phrase(sentence(Meaning), [walk,the,dog]).
Meaning = action(walk,present,imperative,
count(dog,definite,singular)) ;
No
?- phrase(sentence(Meaning), [mary,walked]).
Meaning = action(walk, past, mary, none) ;
No
?- phrase(sentence(Meaning), [mary,walk]).
No
227
:- 9.4.5 Generation of Sentences
Carefully designed grammar can run “backwards”
generating text from meaning
?- phrase(sentence(action(carry, past,
count(boy,definite,singular),
count(book,definite,singular))), Sentence).
Sentence = [the, boy, carried, the, book] ;
No
?- phrase(sentence(action(walk, present,
pro(third,plural,masculine),
count(dog,definite,singular))), Sentence).
Sentence = [they, walk, the, dog] ;
No
?- phrase(sentence(action(walk, present,
count(dog,definite,singular),
pro(third,plural,masculine))), Sentence).
Sentence = [the, dog, walks, them] ;
No
228
:- 10 More on Terms
1. Handling Terms in General
2. Sorting
3. Term comparison
229
:- 10.1 Handling Terms in General
Sometimes it is useful to be able to write predicates that
will work on any kind of term
The =.. predicate, pronounced “univ,” turns a term into a
list
The first element of the list is the term’s functor; following
elements are the term’s arguments, in order
?- p(1,q(3)) =.. X. ?- 1 =.. X.
X = [p, 1, q(3)] ; X = [1] ;
No No
230
:- 10.1.1 functor/3
Because univ builds a list that is typically not needed, it is
often better to use the separate builtin predicates
functor/3 and arg/3
functor(Term,Name,Arity) holds when the functor of Term is
Name and its arity is Arity
For atomic terms, the whole term is considered to be the
Name, and Arity is 0
Either the Term or both the Name and Arity must be bound,
otherwise functor throws an exception
231
:- 10.1.2 functor/3 in Action
?- functor(p(1,q(3)), F, A).
F = p
A = 2 ;
No
?- functor(T, p, 2).
T = p(_G280, _G281) ;
No
?- functor(1, F, A).
F = 1
A = 0 ;
No
232
:- 10.1.3 arg/3
arg/3 can be used to access the arguments of a term one
at a time
arg(N,Term,Arg) holds if Arg is the Nth argument of Term
(counting from 1)
Term must be bound to a compound term
For most Prolog systems, N must be bound; SWI Prolog
will backtrack over all arguments of term
arg/3 is deterministic when first two arguments are bound
233
:- 10.1.4 arg/3 in Action
No
?- arg(N, foo(a,b), Arg).
N = 1
Arg = a ;
N = 2
Arg = b ;
No
?- arg(N, 3, 3).
ERROR: arg/3: Type error: ‘compound’ expected, found ‘3’
234
:- 10.1.5 Using functor and arg
Collect a list of all the functors appearing anywhere in a
ground term:
all_functors(Term, List) :- all_functors(Term, List, []).
all_functors(Term, List, List0) :-
functor(Term, Name, Arity),
( Arity > 0 ->
List = [Name|List1],
all_functors1(Arity, Term, List1, List0)
; List = List0
).
all_functors1(N, Term, List, List0) :-
( N =:= 0 ->
List = List0
; arg(N, Term, Arg),
all_functors(Arg, List1, List0),
N1 is N - 1,
all_functors1(N1, Term, List, List1)
).
235
:- Using functor and arg (2)
?- all_functors(foo(bar([1,2],zip,baz(3)),bar(7)), L).
L = [foo, bar, ’.’, ’.’, baz, bar] ;
No
236
:- 10.2 Sorting
Our all_functors/2 predicate returns a list of functors
appearing, once for each time they appear
May want to produce a set, with each functor appearing
only once
Easiest way: produce the list and sort it, removing
duplicates
Built-in sort(List,Sorted) predicate holds when Sorted is a
sorted version of List, with duplicates removed
all_functors(Term, List) :-
all_functors(Term, List0, []),
sort(List0, List).
237
:- 10.2.1 keysort/2
keysort(List, Sorted) is like sort, except:
?- keysort([a,a,r,d,v,a,r,k], L).
No
?- keysort([a-1,a-2,r-3,d-4,v-5,a-6,r-7,k-8], L).
L = [a-1, a-2, a-6, d-4, k-8, r-3, r-7, v-5] ;
No
238
:- 10.3 Term Comparison
The sorting predicates work on lists of any kinds of terms
Built on general term comparison predicates
The predicates @<, @=<, @>, @>=, == \== work like <, =<, >, >=,
=:= =\=, but they work on any terms
Atoms are compared lexicographically, numbers numerically
Compound terms are compared first by arity, then by
functor, then by the arguments in order
Variables @< numbers @< atoms @< compound terms
Beware variables in terms being sorted!
== and \== check whether terms are identical — without
binding any variables
239
:- Exercise
Why should you beware variables in terms being sorted?
What could happen if you sort a non-ground list?
240
:- 10.4 Variable and Type Testing
Earlier we saw that the simple definition of rev used in the
“backwards mode” goes into an infinite loop after finding
the correct answer
rev([], []).
rev([A|BC], R) :-
rev(BC, CB),
append(CB, [A], R).
241
:- 10.4.1 Variable Testing Example
Can fix this by exchanging the calls in the clause body
rev([], []).
rev([A|BC], R) :-
append(CB, [A], R),
rev(BC, CB).
242
:- 10.4.2 Variable Testing
To implement a reversible version of rev/2, we need to
decide which order to execute the goals based on which
arguments are bound.
Prolog has a built in predicate var/1 which succeeds if its
argument is an unbound variable, and fails if not.
?- var(_).
Yes
?- var(a).
No
243
:- Exercise
Use the var builtin to implement a version of rev that will
work in any mode. Recall that only this version of rev:
rev([], []).
rev([A|BC], R) :- rev(BC, CB), append(CB, [A], R).
244
:- 10.4.3 What’s Wrong With var/1?
One of the most basic rules of logic is that conjunction is
commutative
p, q should behave exactly the same way as q, p (if they
terminate without error).
var/1 breaks that rule:
?- var(X), X = a.
X = a ;
No
?- X = a, var(X).
No
245
:- 10.4.4 When to use var/1
Irony: sometimes we can only write logical, reversible
predicates by using an extralogical predicate such as var/1
But we must be careful
For rev/2, the code is logically equivalent whether the
var/1 goal succeeds or fails; the only difference is the order
of the goals
Sometimes we must have other difference, such as having
X is Y + 1,
when Y is bound, and
Y is X - 1,
when X is.
A better answer for this is constraint programming
X =Y +1
246
:- 10.4.5 Other Non-logical Builtins
All of the following have the same problem of potentially
making conjunction not commutative
nonvar(X) X is bound.
247
:- 10.4.6 Defining length/2
We can define length/2 to work when the list is supplied:
len1([], 0).
len1([_|L], N) :-
len1(L, N1),
N is N1 + 1.
len2([], 0).
len2([_|L], N) :-
N > 0,
N1 is N - 1,
len2(L, N1).
248
:- Defining length/2 (2)
A version that works in both modes:
len(L,N) :-
( var(N) ->
len1(L,N)
; len2(L,N)
).
NB: don’t check if L is var: even if it’s not, its tail may be!
249
:- Defining length/2 (3)
?- len(L, N).
?- len(L, 4).
L = []
L = [_G242, _G248, _G254, _G260] ; N = 0 ;
No L = [_G257]
?- len([a,b,c], N). N = 1 ;
N = 3 ; L = [_G257, _G260]
N = 2
No
Yes
250
251
252
:- 11 Metaprogramming
Prolog has powerful facilities for manipulating and
generating Prolog programs
2. Interpreters
3. Prolog Interpreter
253
:- 11.1 Higher order programming
One powerful feature of functional languages is
higher order programming: passing functions or
predicates as arguments
Prolog supports this, too
The built-in predicate call/1 takes a term as argument
and executes it as a goal
?- Goal = append([a,b,c],X,[a,b,c,d,e]),
| call(Goal).
Goal = append([a, b, c], [d, e], [a, b, c, d, e])
X = [d, e] ;
No
254
:- 11.1.1 call/n
Some Prolog systems, including SWI, also support call of
higher arities
First argument is goal to execute
Extra arguments are given as arguments to the goal
?- Goal = append,
| call(Goal, [a,b,c], X, [a,b,c,d,e]).
Goal = append
X = [d, e] ;
No
255
:- 11.1.2 Closures
When the goal argument of call/n is a compound term,
later arguments of call are added to the goal
?- Goal = append([a,b,c]),
| call(Goal, X, [a,b,c,d,e]).
Goal = append([a, b, c])
X = [d, e] ;
No
The Goal in this case is a predicate with some, but not all,
of its arguments supplied; other arguments are given when
it is called
This is a closure
256
:- 11.1.3 map/3
Using call/n it is easy to implement the standard higher
order operations of functional languages, e.g.
257
:- Exercise: all/2
Write a predicate all/2 such that all(Pred,L) holds when
every element of L satisfies Pred. For example,
all(member(c), [[a,b,c],[a,e,i],[a,c]]]
fails, and
all(member(X), [[a,b,c],[a,e,i],[a,c]])
succeeds with X=a.
258
:- 11.2 Interpreters
Some programming problems are best solved by
defining a new minilanguage, a small language
targeted at one specific task
Consider the control of a robot vacuum cleaner
Programming this would be rather complex, involving
separate control of several motors, receiving input from
several sensors, etc .
This task would be far simpler to handle if we defined a
minilanguage for robot control, and then implemented an
interpreter for this language
259
:- 11.2.1 Robot Minilanguage
At the simplest level, we can consider primitive commands
to turn and move forward
These could be represented as Prolog terms:
advance(Distance)
left
right
260
:- 11.2.2 Interpreter Design
An interpreter (simulator) for this language would track
the robot’s position
Also needs to keep track of the robot’s heading,
represented as X and Y components, called Hx and Hy
Hx = 0 Hx = 1 Hx = 0 Hx = −1
Hy = 1 Hy = 0 Hy = −1 Hy = 0
Hx and Hy are either -1, 0, or 1, and |Hx | + |Hy | = 1
On moving d units, Δx = dHx and Δy = dHy
To turn clockwise (right): Hx = Hy , Hy = −Hx ,
anticlockwise (left): Hx = −Hy , Hy = Hx
261
:- 11.2.3 Interpreter
262
:- 11.2.4 Robot Programs
Construct a robot program as a list of commands:
263
:- 11.2.5 Walls
The interpreter is simple, so it is easy to extend
Add walls to the room the robot operates in by preventing
X and Y from being less than 0 or more than room width
or height
Just need to replace one sim/9 clause and define room
width and height
room_width(400).
room_height(300).
264
:- 11.2.6 Other Possible Extensions
265
:- 11.3 Prolog meta-interpreter
We can write an interpreter for any language we can
conceive, including Prolog itself
This is made much easier by the fact that Prolog goals are
ordinary Prolog terms
An interpreter for a language written in the language
itself is called a meta-interpreter
Prolog systems have a built-in interpreter invoked by the
call built-in predicates
Need access to the clauses of the program to be
interpreted; built-in predicate clause(Head,Body) gives this
Body is true for unit clauses
Generally you need to declare predicates “dynamic”:
:- dynamic foo/3.
266
:- 11.3.1 Simple meta-interpreter
% solve(Goal)
% interpret Goal, binding variables
% as needed.
solve(true).
solve((G1,G2)) :-
solve(G1),
solve(G2).
solve(Goal) :-
clause(Goal, Body),
solve(Body).
267
:- Simple meta-interpreter (2)
Extend this to handle disjunction, if->then;else, negation:
solve((G1;G2)) :-
( G1 = (G1a->G1b) ->
( solve(G1a) ->
solve(G1b)
; solve(G2)
)
; solve(G1)
; solve(G2)
).
solve(\+(G)) :-
\+ solve(G).
268
:- Simple meta-interpreter (3)
To handle Prolog builtins, modify the last clause on
slide 267:
solve(Goal) :-
( builtin(Goal) ->
call(Goal)
; clause(Goal, Body),
solve(Body)
).
builtin(_ = _).
builtin(_ is _).
builtin(append(_,_,_)).
..
.
269
:- 11.3.2 Extensions
Can modify interpreter to construct a proof
270
:- 11.4 Manipulating Prolog code
The fact that Prolog code is just a Prolog term also makes
it very easy to manipulate
To take advantage of this, the Prolog compiler
automatically calls a predicate term_expansion/2 (if it is
defined) for each term it reads
First argument of term_expansion/2 is a term Prolog read
from a source file; term_expansion/2 should bind the second
to a clause or list of clauses to use in place of what was
read
This is the same mechanism used to convert DCG clauses
into ordinary Prolog clauses
This feature allows programmers to easily write Prolog
code to generate or transform Prolog code
271
:- 11.4.1 Generating Accessor Predicates
Example: automatically generating accessor predicates
Suppose a student is represented as student(Name,Id) in a
large program
Everywhere the name is needed, the code might contain
Student = student(Name,_)
If we later need to also store degree code in student terms,
we must modify every student/2 term in the program
Programmers sometimes define accessor predicates, e.g.:
272
:- Generating Accessor Predicates (2)
Using term_expansion, it is easy to write code to
automatically generate accessor predicates
We will transform a directive like
:- struct student(name,id).
273
:- Generating Accessor Predicates (3)
274
275
276
Q 12 Propositional Logic
• introduce basic ideas of formal logic
277
Q Exercise: Knights and Knaves
On the island of Knights and Knaves, everyone is a knight
or knave. Knights always tell the truth. Knaves always lie.
You are a census taker, going from house to house. Fill in
what you know about each house.
278
Q 12.1 Logic and Reasoning
• Logic is about reasoning
All humans have 2 legs
Jane is a human
Therefore Jane has 2 legs
279
Q 12.1.1 Arguments: Form and Meaning
280
Q 12.1.2 Propositions
•
A proposition is a sentence that is either true or
.
false, but not both
• use letters to represent basic propositions
• e.g. A for “I look into the sky”, B for “I am alert”
• logical connectives: ¬ (not), ∧ (and), ∨ (or), →
(implies), ↔ (iff)
• e.g. A ∧ B for “I look into the sky and I am alert”
• rules for forming propositions
(a) a propositional variable is a proposition
281
Q 12.1.3 Improving Readability
Which is more readable?
(P → (Q → (¬R)) or P → (Q → ¬R)
Rules for improving readability:
Questions:
Is (P ∨ Q) ∨ R ≡ P ∨ (Q ∨ R)? Is (P ∧ Q) ∧ R ≡ P ∧ (Q ∧ R)?
Is (P → Q) → R ≡ P → (Q → R)?
282
Q 12.2 Truth Tables
• propositions can be true (T) or false (F)
A B A∧B A∨B ¬A
T T T T F
T F F T F
F T F T T
F F F F T
283
Q 12.2.1 Terminology
A proposition is a tautology if it is always true, i.e.
each line in the truth table is T . For example
A ∨ B ∨ ¬A.
284
Q 12.2.2 Previous Example
Does (A ∧ B) ∧ C = A ∧ (B ∧ C)?
A B C A∧B B∧C A ∧ (B ∧ C) (A ∧ B) ∧ C
T T T T T T T
T T F T F F F
T F T F F F F
T F F F F F F
F T T F T F F
F T F F F F F
F F T F F F F
F F F F F F F
285
Q 12.3 Implication
Truth Table For Implication:
A B A→B
T T T
T F F
F T T
F F T
Terminology:
286
Q Exercise
Are (A → B) and ¬A ∨ B are equivalent?
287
Q 12.3.1 Material Implication
288
Q 12.3.2 Example (Kelly Example 1.2)
(A ∧ B) → (C ∨ (¬B → ¬C))
289
Q Example (Kelly Example 1.2) (2)
Truth Table (Kelly Fig. 1.13)
A B C A∧B ¬B ¬C ¬B → ¬C C∨ (A ∧ B) → (C ∨
(¬B → ¬C) (¬B → ¬C)
T T T T F F T T T
T T F T F T T T T
T F T F T F F T T
T F F F T T T T T
F T T F F F T T T
F T F F F T T T T
F F T F T F F T T
F F F F T T T T T
290
Q Example (Kelly Example 1.2) (3)
• Thus:
A B C A∧B ¬B ¬C ¬B → ¬C C∨ (A ∧ B) → (C ∨
(¬B → ¬C) (¬B → ¬C))
T T T T F F T T T
T T F T F T T T T
291
Q Exercise
Express in propositional logic:
“If Michelle wins at the Olympics, everyone will admire her,
and she will get rich; but if she does not win, all her effort
was in vain.” Begin by determining the the propositional
variables you will use and what they mean.
292
Q 12.3.3 Knights and Knaves
Express in propositional logic the census taker puzzle, for
house 1.
H ↔ (¬H ∧ ¬W )
H W ¬H ∧ ¬W H ↔ (¬H ∧ ¬W )
T T F F
T F F F
F T F T
F F T F
293
Q 12.4 Equivalences (Kelly Section 1.3)
• lots of them . . . infinitely many!
• especially important:
– ¬ ¬A is equivalent to A
– ¬A ∨ B is equivalent to A → B
294
Q 12.4.1 More Important Equivalences
• Commutativity B∨A≡A∨B
A∧B ≡B∧A
Associativity A ∧ (B ∧ C) ≡ (A ∧ B) ∧ C
A ∨ (B ∨ C) ≡ (A ∨ B) ∨ C
Distributivity A ∧ (B ∨ C) ≡ (A ∧ B) ∨ (A ∧ C)
A ∨ (B ∧ C) ≡ (A ∨ B) ∧ (A ∨ C)
Absorption A ∧ (A ∨ B) ≡ A
A ∨ (A ∧ B) ≡ A
A ∧ (¬A ∨ B) ≡ A ∧ B
A ∨ (¬A ∧ B) ≡ A ∨ B
See Kelly page 12 for further equivalences
295
Q 12.4.2 The Many Faces of Implication
• if P then Q
• P implies Q
• P →Q
• P only if Q
• Q if P
• Q is a necessary condition for P
• P is sufficient for Q
• ¬Q → ¬P (called the contrapositive of P → Q)
• ¬P ∨ Q
296
Q 12.4.3 If and Only If
A “if and only if” B means A if B and A only if B,
that is, (B → A) ∧ (A → B)
297
Q 12.5 How Many Connectives?
• (Kelly Section 1.4)
• started with: ¬ ∧ ∨ → ↔
298
Q 12.5.1 NAND
299
Q 12.6 Summary
• logic is based on the assumption that reasoning is
based on argument FORM
300
Q 13 Arguments
• history of logic and logic programming
301
Q 13.1 History
• Goal: to systematize reasoning
• Propositional logic: George Boole, 1847
– Good for basic understanding of
reasoning, but
302
Q 13.2 Terminology
An assignment gives a truth value to each
•
propositional variable in a formula
303
Q Terminology (2)
• Denoted: S|=A
• e.g., P ∧ Q |= P
304
Q Terminology (3)
305
Q 13.2.1 Example (Kelly Example 1.4)
• S1 is P → (¬H → A)
S2 is P → ¬H
C is P → A
so you can check the truth table of
306
Q Example (Kelly Example 1.4) (2)
P A H ¬H → A P → P → ¬H P →A ¬(P → A) mess
(¬H → A)
T T T T T F T F F
T T F T T T T F F
T F T T T F F T F
T F F F F T F T F
F T T T T T T F F
F T F T T T T F F
F F T T T T T F F
F F F F T T T F F
307
Q 13.2.2 Another Example
H1 : If she studies the sciences then she prepares to earn a
good living.
H2 : If she studies the humanities, then she prepares for a
good life.
H3 : If she prepares for a good living or for a good life then
the years are well spent.
H4 : The years were not well spent.
C: She didn’t study science or humanities.
Is the argument “H1 , H2 , H3 , H4 , therefore C” valid?
H1 is S → G, H2 is H → L, H3 is G ∨ L → W ,
H4 is ¬W , C is ¬(S ∨ H)
Check the 25 = 32 rows of the truth table
(S → G) ∧ (H → L) ∧ (G ∨ L → W ) ∧ (¬W ) ∧ (S ∨ H)
308
Q 13.3 A Better Way
• Often we can check validity more easily than filling out
a truth table
(S → G) ∧ (H → L) ∧ (G ∨ L → W ) ∧ (¬W ) ∧ (S ∨ H)
• So W must be false
309
Q A Better Way (2)
(S → G) ∧ (H → L) ∧ (G ∨ L → W ) ∧ ( ¬ W ) ∧ (S ∨ H)
F T F T F T F T F F T F T T F T F X F
310
Q Exercise
Determine whether or not the following formula is
unsatisfiable. If not, find values for the variables that make
it satisfiable.
311
Q 13.4 Example
WHY WORRY?? There are only two things to worry
about, either you’re healthy or you’re sick. If you’re
healthy there is nothing to worry about and if you’re sick,
there are two things to worry about . . . either you’ll get
well or you won’t. If you get well there is nothing to worry
about, but if you don’t, you’ll have two things to worry
about . . . either you’ll go to heaven or to hell. If you go to
heaven you have nothing to worry about and if you go to
hell you’ll be so busy shaking hands with all of us you’ll
have no time to worry.
CONCLUSION: Either you’ll have nothing to worry about
or you’ll have no time to worry.
H: you are healthy, S you are sick, W you have something
to worry about, A you will get well, B you won’t get well,
C you will go to heaven, D you will go to hell, T you will
have time to worry.
312
Q 13.4.1 Translation to Logic
1. H ∨S healthy or sick
2. H → ¬W if healthy then no worries
3. S → (A ∨ B) if sick then either will get
well or won’t
4. A → ¬W if get well then no worries
5. B → (C ∨ D) if won’t get well then will
either go to heaven or hell
6. C → ¬W if heaven then no worries
7. D → ¬T if hell the no time to worry
8. ∴ ¬W ∨ ¬T therefore, no need to worry
or no time to worry
313
Q 13.4.2 Checking Validity
Begin by negating conclusion and simplifying:
314
Q Checking Validity (2)
Now we know W ∧ T ∧ ¬D ∧ ¬C ∧ ¬A ∧ ¬H
Premise 5 is B → (C ∨ D), but we know ¬C ∧ ¬D, so we
conclude ¬B
Premise 3 is S → (A ∨ B), but now we know ¬A ∧ ¬B, so ¬S
Premise 1 is H ∨ S, but we now know ¬H and ¬S —
contradiction!
Conclusion: either some of our premises were false or our
conclusion was true
Argument was valid
315
Q 13.4.3 Caution!
Why have A mean will get well and B mean won’t get well?
Why not use ¬A for won’t get well?
Answer: “will get well” and “won’t get well” are not the
only two possibilities — could also not be sick
Hint: “if you’re sick, . . . either you’ll get well or you won’t.”
Translation would be S → (A ∨ ¬A) — tautology
Tautologies say nothing new; useless as premise
Could have used ¬H for sick, because sick and healthy are
the only choices
316
Q 13.5 Argument Validity in Prolog
Validity can easily be tested in Prolog
317
Q Argument Validity in Prolog (2)
true(v(true)). false(v(false)).
true(\+ Prop) :- false(\+ Prop) :-
false(Prop). true(Prop).
true((X;Y)) :- false((X;Y)) :-
( true(X) false(X),
; true(Y) false(Y).
). false((X,Y)) :-
true((X,Y)) :- ( false(X)
true(X), ; false(Y)
true(Y). ).
true((X->Y)) :- false((X->Y)) :-
( false(X) true(X),
; true(Y) false(Y).
). false((X<->Y)) :-
true((X<->Y)) :- ( true(X),
( true(X), false(Y)
true(Y) ; false(X),
; false(X), true(Y)
false(Y) ).
).
318
Q Argument Validity in Prolog (3)
Represent a propositional variable as v(t), where t is either
true or false.
?- valid(((v(P)->v(Q)),(v(Q)->v(R))), v(P)->v(R)).
P = _G157
Q = _G159
R = _G166
Yes
?- valid((v(P)->v(Q))->v(R), v(P)->v(R)).
No
Why is last answer No?
319
Q Argument Validity in Prolog (4)
Can use false/1 predicate directly to see why
?- false(((v(P)->v(Q))->v(R))-> (v(P)->v(R))).
P = true
Q = false
R = false ;
No
320
Q 13.6 Summary: Argument Correctness
• general strategy for determining if an argument of the
form
S1 , S2 , · · · , Sn |= C
is valid
321
Q Summary: Argument Correctness (2)
Non truth-table strategies for determining validity
322
323
324
Q 14 Axiom Systems for
Propositional Logic
• notions of axiomatic system, deduction
325
Q Basic Idea (Kelly Ch 4)
• so far . . .
– truth tables used to define
∗ tautologies, (in)consistency, validity, . . .
∗ model theory approach
• now . . .
– a proof theory approach
∗ axioms, inference rules, proofs, theorems, . . .
326
Q 14.1 Axiomatic Systems (Kelly 4.2)
• Proof:
– set of initial hypotheses
• In Propositional Logic
–
start with axioms (simple tautological propositions)
327
Q 14.1.1 Syntactic Description
Axiomatic system has 4 parts:
Σ alphabet of symbols; we use ¬, →, (, ), P, Q, R, . . .
WF the well formed formulae (wff)
Ax the set of axioms, a subset of WF
R the set of rules of deduction
Well Formed Formulae (wff):
• Nothing else is a wff
328
Q 14.1.2 Axioms
There are infinitely many axioms, but all have one of these
forms:
Axiom schemas — allow any wff for A, B, and C
Ax1 A → (B → A)
Ax2 (A → (B → C)) → ((A → B) → (A → C))
Ax3 (¬A → ¬B) → (B → A)
Examples:
• P → (Q → P ) is an instance of Ax1.
329
Q 14.1.3 Inference Rule
One rule of deduction, or inference rule:
modus ponens from A and A → B infer B
Given a set of hypotheses H, where H ⊆ W F , a deduction
is a sequence of wff F1 , F2 , . . . , Fn where each Fi is either:
330
Q Inference Rule (2)
We say a deduction ending in Fn is a deduction of Fn from
H or that Fn is a deductive consequence of H, and write
H Fn
When H = ∅, we write
Fn
and say Fn is a theorem
331
Q 14.1.4 Trivial Examples
P → (Q → P ) Ax1
332
Q 14.1.5 Short Proof (Kelly Theorem 4.1)
A→A
1. A → ((B → A) → A) Ax1
2. (A → ((B → A) → A)) → ((A → (B → A)) → (A → A)) Ax2
3. (A → (B → A)) → (A → A) MP 1, 2
4. A → (B → A) Ax1
5. A→A MP 3, 4
(A ∧ B) → B
Here A ∧ B is defined as ¬(A → ¬B).
333
Q 14.2 Meta-theorems
We have proved A → A, but what about P → P ?
We haven’t proved it, but we could: duplicate proof,
replacing A with P
Works because for every axiom with A, there’s one with P
Modus ponens works for P as well as for A
Take our theorem as a meta-theorem — a theorem
template
As a shortcut, we could use A → A as if it were an axiom
schema, because we could always replace it by its proof
Like using subroutines when programming
334
Q 14.2.1 Using A → A
Prove B → (A → A)
1. A→A Theorem 4.1
2. (A → A) → (B → (A → A)) Ax1
3. B → (A → A) MP 1, 2
335
Q Exercise: Transitive Implication
Fill in the blanks in the following proof of
{A → B, B → C} A → C
1. B→C
2. (B → C) → (A → (B → C))
3. MP 1, 2
4. (A → (B → C)) → ((A → B) → (A → C))
5. (A → B) → (A → C)
6. A→B Hypothesis
7. MP 5, 6
This proves implication is transitive; call it TI
336
Q 14.2.2 Using TI
Can use meta-theorems with hypotheses as rules of
inference
First prove instances of hypotheses, then use
meta-theorem to conclude instance of conclusion
Prove ¬A → (A → B)
337
Q 14.2.3 More Meta-theorems
Kelly proves other useful Meta-theorems
Deduction meta-theorem:
338
Q More Meta-theorems (2)
Use deduction meta-theorem to prove
{A → (B → C)} B → (A → C):
1. A → (B → C) Hypothesis
2. A Hypothesis (for sake of proof
3. B→C MP 1, 2
4. B Hypothesis (for sake of proof
5. C MP 3, 4
So, {A → (B → C), A, B} C
then {A → (B → C), B} A → C Deduction theorem
then {A → (B → C)} B → (A → C) Deduction theorem
339
Q 14.3 Finding axiomatic proofs
• no firm search strategy
340
Q 14.4 Truth and Deduction (Kelly 4.5)
What is the relationship between truth (|=, see Kelly
chapter 1) and deduction (, see Kelly chapter 4)?
What makes an axiom system useful?
Consistency: if A then ¬A
1.
Is it impossible to prove a wff and its negation?
Soundness: A implies |= A
2.
Is it impossible to prove a wff that is not a tautology?
Completeness: |= A implies A
3.
Can everything that is a tautology be proved?
Decidability:
4.
Is there an algorithm to always decide if A?
341
Q Truth and Deduction (Kelly 4.5) (2)
The AL system of Kelly chapter 4 has all these properties
342
Q 14.5 Summary
• proof system based on axioms and inference rules
defines a computational procedure for verifying
deductions . . . though not necessarily practical for
generating deductions
343
344
345
346
347
348
Q 15 Resolution
• Kelly chapter 5
349
Q Terminology
• e.g. (A ∨ ¬B) ∧ (B ∨ C) ∧ A
350
Q 15.1 Converting to CNF or DNF
1. Eliminate ↔ using A ↔ B equivalent to
(A → B) ∧ (B → A).
351
Q 15.1.1 Example (Kelly Example 5.3)
Convert (¬p ∧ (¬q → r)) ↔ s to conjunctive normal form.
1. ((¬p ∧ (¬q → r)) → s) ∧ (s → (¬p ∧ (¬q → r)))
2a. (¬(¬p ∧ (¬q → r)) ∨ s) ∧ (¬s ∨ (¬p ∧ (¬q → r)))
2b. (¬(¬p ∧ (q ∨ r)) ∨ s) ∧ (¬s ∨ (¬p ∧ (q ∨ r)))
3a. ((p ∨ ¬(q ∨ r)) ∨ s) ∧ (¬s ∨ (¬p ∧ (q ∨ r)))
3b. ((p ∨ (¬q ∧ ¬r)) ∨ s) ∧ (¬s ∨ (¬p ∧ (q ∨ r)))
4. not needed
5a. (((p ∨ ¬q) ∧ (p ∨ ¬r)) ∨ s) ∧ ((¬s ∨ ¬p) ∧ ¬s ∨ (q ∨ r)))
5b. (p ∨ ¬q ∨ s) ∧ (p ∨ ¬r ∨ s) ∧ (¬s ∨ ¬p) ∧ (¬s ∨ q ∨ r)
Note that Kelly gives a different derivation of this CNF.
See also Kelly Example 5.2.
352
Q Exercise
Convert the following proposition into CNF:
P ↔ (Q ∨ R)
353
Q 15.2 The Key to Resolution (Kelly
page 99)
• consider the formulae (¬A ∨ B) and (¬B ∨ C)
– If B is true then the truth depends only on C.
354
Q 15.2.1 Resolution Principle.
(Kelly Theorem 5.1, page 102)
If D is a resolvent of clauses C1 and C2 , then C1 ∧ C2 |= D
355
Q 15.3 The Key to Refutations
¬A ∨ B
K ¬B ∨ C both true one f alse
O
KK ss
KK s
KK sss
KK ss
K ss
¬A ∨ C this true thisf alse
356
Q 15.3.1 Refutations
¬A ∨ B
KK ¬B ∨ C one f alse
O
KK ss
KK s
KK sss
KK sss
s
¬A ∨ C this f alse
357
Q 15.3.2 Deductions and Refutations (Kelly page
105)
• A resolution deduction of clause C from a set S of
clauses is a sequence C1 , C2 , . . . , Cn of clauses such that
Cn = C and for each i, 1 ≤ i ≤ n,
– Ci is a member of S, or
358
Q 15.3.3 Resolution Theorem
Given a clause set S, R(S) is the set of all clauses derivable
from S by one resolution step
R∗ (S) is the set of all clauses obtainable after a finite
number of resolution steps, starting with S.
Resolution theorem: S is unsatisfiable iff ⊥ ∈ R∗ (S)
359
Q 15.4 Establishing Validity With
Resolution
• Algorithm for establishing formula A is valid
– put ¬A in conjunctive normal form
360
Q 15.5 Set Representation of Clauses
clause: a finite set of literals representing a
•
disjunction
361
Q Set Representation Examples
362
Q 15.5.1 Example
Remember the disappointed student?
363
Q 15.6 Example Proof
H1 is S→G
H2 is H→L
H3 is G∨L→W
H4 is ¬W
C is ¬(S ∨ H)
We convert
(S → G) ∧ (H → L) ∧ (G ∨ L → W ) ∧ (¬W ) ∧ (S ∨ H)
to CNF.
364
Q 15.6.1 Example Proof Diagram
¬S ∨ G ¬G ∨ W ¬H ∨ L ¬L ∨ W iisis¬W nnS ∨ H
ii
ii ss nnn
ii i
iiiii n nnn
iiii j ¬L
jj nnnn
ii i jj
¬G ¬H n nnn
jj nnn
jjjj n
¬S dddd dd ddd S
ddddddd
⊥
1 ¬L ∨ W a ¬L ∨ W
2 ¬W b ¬W
3 ¬L 1,2 c ¬L a,b
4 ¬G ∨ W d ¬H ∨ L
5 ¬G 2,4 e ¬H c,d
6 ¬S ∨ G
7 ¬S 5,6
365
Q Exercise
Use resolution to show
366
Q 15.6.2 Important Results
367
Q 15.7 Summary: Propositional Logic so
Far
• syntax: the shape of “legal” formulae
• techniques
– truth tables
– resolution
368
Q 15.7.1 Discussion
369
370
371
372
Q 16 Linear Resolution
• present linear resolution – a more efficient form of
resolution
373
Q Linear Resolution (2)
374
Q Linear Resolution (3)
Basic idea: when choosing two clause to resolve, always
make one be the result of the previous resolution
(10.3) centre clauses, side clauses ancestors
375
Q Linear Resolution Illustration
Resolution in general
{¬A, B} {C} WWgWgWg {A} {¬C, ¬B}
OOO gg WWWWWW mmm
ggggg
{B} TTT i {¬B}
TTTT ii
iiii
⊥
versus
Linear Resolution
{¬A, B} l {A}
ll
lll
{B} {¬C, ¬B}
llll
{¬C} k {C}
kk k
kkk
⊥
376
Q 16.1 Horn clauses and Prolog
• conjunctive normal form = conjunction of clauses
B1 ∧ · · · ∧ Bk → A1 ∨ A2 · · · ∨ Am
B1 ∧ · · · ∧ Bk → A
A : − B1 , . . . , B k
377
Q Horn clauses and Prolog (2)
subgoal
A : − B1 , . . . , Bj , .J. . , Bk
JJ
JJ
JJ
JJ
head neck body
378
Q 16.2 Propositional Logic Programs
• a propositional logic program P is a collection of facts
and rules; typically, we want to know if some other
fact(s) are a logical consequence of P
Example:
mg : − mgo, h2. i.e. h2 ∧ mgo → mg
h20 : − mgo, h2. h2 ∧ mgo → h20
co2 : − c, o2. c ∧ o2 → co2
h2co3 : − co2, h2o. co2 ∧ h2o → h2co3
mgo. mgo
h2. h2
o2. o2
c. c
379
Q 16.2.1 Clausal Form of Logic Program
A1 h2 ∧ mgo → mg ¬h2 ∨ ¬mgo ∨ mg
A2 h2 ∧ mgo → h20 ¬h2 ∨ ¬mg0 ∨ h20
A3 c ∧ o2 → co2 ?
A4 co2 ∧ h2o → h2co3 ?
A5 mgo mgo
A6 h2 h2
A7 o2 o2
A8 c c
A9 ¬h2co3 ¬h2co3
380
Q 16.2.2 Propositional Logic Programs and
Resolution
• given a propositional logic program P to know if some
other fact(s) are a logical consequence of P
: − h2co3
381
Q 16.3 Linear Input Resolution
(Nerode and Shore Definition 10.8) A linear input
(LI) resolution of a Goal G from a set of program
• clauses P is a linear resolution refutation of
S = P ∪ {G} that starts with G and in which all the
side clauses are from P (input clauses).
382
Q 16.3.1 Counterexample
383
Q Exercise
Use linear input resolution to refute the clause set:
384
Q 16.4 Connections with Prolog
• representation . . . a program, with a goal
1. {q, ¬p, ¬s} q :- p, s.
2. {p, ¬r} p :- r.
2. {p, ¬s} p :- t.
4. {s} r :- v.
4. {s} s.
3. {t} t.
5. {¬q} :- q
385
Q 16.4.1 (Abstract) Interpreter for Propositional
Prolog
Input: A query Q and a logic program P
Output: TRUE if Q ‘implied’ by P, FALSE otherwise
To Prove(G1,... Gn):
For each rule from P of the form
G1 :- B1, ... ,Bn:
if we can Prove(B1,...Bn,G2,...Gn):
return TRUE
return FALSE (no rule was satisfiable)
386
Q 16.4.2 SLD Resolution
387
Q 16.4.3 SLD Tree
Prove q with earlier example program:
q :- p, s. p :- r. p :- t.
r :- v. s. t.
p, s F
ww FFF
ww
r, s t, s
v, s s
⊥
Branching represents choice points (revisited on
backtracking)
⊥ indicates success; underlined goals indicate failure
388
Q 16.4.4 SLDNF Resolution
• E.g.: p :- t, \+r.
389
Q Negation as Failure Example
Prove q with updated program:
q :- p, s. p :- r. p :- t, \+r.
r :- v. s. t.
p, s L
ww LLL
ww
r, s t, ¬r, s
v, s ¬r, s r
s v
⊥
¬r is proved because separate proof of r fails
390
Q 16.5 Summary: Linear Resolution and
Prolog
• Nerode and Shore Theorem 10.15 ensures that the
resolution method underlying Prolog, SLD-resolution,
is complete; however, Prolog’s search strategy is not
guaranteed to find a refutation even if one exists!
391
Q 16.6 Summary: Prolog “theory”
• SLD resolution – a refinement of linear resolution
– SLD resolution is sound and complete
392
393
394
395
396
∀x 17 Predicate Logic
• Relations and Functions
397
∀x History
• Propositional logic can only make
absolute statements, not statements
about some/all individual(s)
• Can say “if it rains, I will get wet”
• Can’t say “if it rains, anyone who is
outside will get wet”
• Gotlob Frege proposed the Predicate
Calculus in 1879 to solve this problem
• can reason about properties of
individuals
• can make sweeping statements about
all individuals
• can reason about relationships between
individuals
398
∀x Examples of Predicate Logic Formulae
W (colin) ∧ D(colin)
399
∀x 17.1 Relations and Functions
A relation relates objects in the domain
400
∀x 17.1.1 Relations
401
∀x 17.1.2 Functions
402
∀x 17.2 From English to Predicate Logic
• Take predicates (i.e. sentences with the subject
abstracted away) and use symbols for them.
• Quantifier examples:
“Every man is mortal” ∀x(man(x) → mortal(x))
“Some cat is mortal” ∃x(cat(x) ∧ mortal(x))
403
∀x 17.2.1 Examples
Let L(x, y) be “x loves y”; let I(x, y) be “x is y”
L(james, jean) James loves Jean
∀xL(x, jean) Everyone loves Jean (including
Jean!)
∀x(¬I(x, jean) → L(x, jean)) Jean is loved by everyone else
∃x(¬I(x, james) ∧ L(x, james) Someone other than James loves
James
∀x(∃yL(x, y)) Everybody loves somebody
∃y(∀xL(x, y)) Someone is loved by everybody
∃x(∀yL(x, y)) Someone loves everybody
404
∀x Exercise
Translate the following statement to predicate logic:
P (x) x is a person
T (x) x is a footy team
B(x, y) x barracks for y
405
∀x 17.2.2 Word Order
406
∀x 17.2.3 Quantification Order
407
∀x Quantification Order (2)
• If I claim ∀y∀xP (x, y), then you get to pick both x and
y, so again it doesn’t matter what order they appear
408
∀x 17.2.4 Implicit Quantifiers
Sometimes quantifiers are implicit in English
Look for nouns (especially plural) without determiners
(words to indicate which members of a group are intended)
409
∀x 17.2.5 Ambiguity
• Logic is unambiguous
410
∀x 17.2.6 Choice of Predicates
Often there are many ways to encode an idea in predicate
logic
“People who live in glass houses shouldn’t throw stones”
could be expressed in any of these ways:
411
∀x Exercise
Given the following interpretation, translate the formula
to English
P (x) x is a person
x>y x is greater than y
x = y x and y are different
h(x) the height of x
412
∀x 17.2.7 What Logic Cannot Handle
413
∀x 17.3 The Language of Predicate Logic
• Alphabet
– constants (c)
– logical connectives (¬ ∧ ∨ → ↔)
– quantifiers (∀, ∃)
414
∀x The Language of Predicate Logic (2)
• Understanding Quantifiers
– ∀xA(x) “For each and every thing, A(that thing) is
true”
415
∀x The Language of Predicate Logic (3)
416
∀x 17.3.1 Scope, Bound and Free Variables
417
∀x 17.3.2 Bound, Free, Sentences and Formulae
• Examples
– in ∃xA(x, c), x occurs bound
• Sentence
–
a sentence, or closed formula, is a wff where every
variable occurrence is bound
418
∀x 17.4 Summary
• Language of Predicate Logic extends Propositional
Logic with predicates, functions, universal and
existential quantifiers
419
420
∀x 18 Determining Truth
• Domains of Interpretation
• Valuations
421
∀x 18.1 Domains of Interpretation
Truth value of a Predicate Logic formula depends on
• the set of things we limit our attention to, called the
domain of interpretation or domain of discourse
422
∀x 18.1.1 Interpretations
An interpretation relates the constant, function,
and relation symbols of our language to the objects,
functions and relations over the domain
Gives a meaning to our language
Example: language with binary predicate symbols P and Q,
unary function symbol f and constant symbol c
• domain U is the set of integers
• fU (x) = x + 7
• cU is 0
423
∀x Interpretations (2)
424
∀x 18.1.2 Valuations
425
∀x 18.1.3 Relation to Propositional Logic
426
∀x 18.2 Truth and Satisfaction
A formula A is satisfied by an interpretation U and some
valuation θ over that interpretation:
427
∀x 18.2.1 Truth, Satisfiability, Validity
428
∀x Truth, Satisfiability, Validity (2)
example: interpretations U where P (x) is “x is a woman” :
429
∀x Exercise
In the interpretation of slide 423:
• fU (x) = x + 7
1. ∀x∃yP (x, y)
2. ∀x∃yQ(y, x)
430
∀x 18.3 Deciding Validity
To determine validity of a sentence:
431
∀x 18.3.1 The Problems
432
∀x 18.4 Herbrand Approach
Herbrand’s Theorem (1929) A set of clauses is
unsatisfiable iff there is a finite subset of ground instances
of its clauses which is unsatisfiable as propositional logic
formulas
433
∀x 18.4.1 Herbrand Interpretation
434
∀x 18.4.2 Herbrand Universe
To find the Herbrand universe H for a set S of
clauses:
• If c is a constant occurring in a clause in S, H
contains c
• If no constants occur in any clause in S, add a
new constant symbol to H
• if f is a function symbol of arity n occurring in
any clause in S, and H contains t1 , · · · , tn , add
f (t1 , · · · , tn ) to H
• Repeat ad infinitum
435
∀x 18.4.3 Herbrand Base
The Herbrand base B of a set S of clauses is
defined as follows
• If P is a predicate symbol appearing in t1 , · · · , tn
are members of the Herbrand universe of S, then
P (t1 , · · · , tn ) is in B
• Nothing else is in B
436
∀x 18.4.4 Example
• Fido is a dog
All hungry dogs bark
Fido is hungry
Therefore, Fido barks
• D(F )
∀x(H(x) ∧ D(x) → B(x))
H(F )
∴ B(F )
• Herbrand universe = {F }
437
∀x Example (2)
• D(F )
H(F ) ∧ D(F ) → B(F )
H(F )
∴ B(F )
438
∀x 18.4.5 On To Resolution
439
∀x 18.4.6 Summary
440
441
442
443
444
∀x 19 Resolution in Predicate Logic
• Aim: Use resolution to show that a Predicate Logic
formula is unsatisfiable
445
∀x History
446
∀x History (2)
• Prolog: Alain Colmerauer, 1973
– Designed for natural language
understanding
447
∀x 19.0.7 Pre-Processing for Predicate Logic
We extend the algorithm for converting a formula to
conjunctive normal form to enable us to deal with variables
and quantifiers.
For each formula:
448
∀x 19.0.8 Prenex Normal Form Algorithm
Stage 1 Use A ↔ B ≡ (A → B) ∧ (B → A) to eliminate ↔
Stage 2 Use A → B ≡ ¬A ∨ B to eliminate →
Stage 3 Use
a) De Morgan’s Laws
b) ¬∀xA(x) ≡ ∃x¬A(x)
c) ¬∃xA(x) ≡ ∀x¬A(x)
to push ¬ immediately before atomic wff
Simplify formula using ¬¬A ≡ A
Stage 4 Rename bound variables if necessary
Stage 5 Use the equivalences on (Kelly page 192) to bring all
quantifiers to the left
Stage 6 “Distribute” ∧, ∨
449
∀x 19.0.9 Example
Convert to Prenex Normal Form:
∀x A(x) → B(x) ↔ ∃xQ(x)
„ “ ” « „ “ ”«
1. ∀x A(x) → B(x) → ∃xQ(x) ∧ ∃xQ(x) → ∀x A(x) → B(x)
„ “ ” « „ “ ”«
2. ¬ ∀x ¬ A(x) ∨ B(x) ∨ ∃xQ(x) ∧ ¬ ∃xQ(x) ∨ ∀x ¬ A(x) ∨ B(x)
„ “ ” « „ “ ”«
3a. ∃x¬ ¬A(x) ∨ B(x) ∨ ∃xQ(x) ∧ ∀x¬ Q(x) ∨ ∀x ¬A(x) ∨ B(x)
„ “ ” « „ “ ”«
3b. ∃x ¬ ¬A(x) ∧ ¬ B(x) ∨ ∃xQ(x) ∧ ∀x¬Q(x) ∨ ∀x ¬A(x) ∨ B(x)
„ “ ” « „ “ ”«
4. ∃ w A( w ) ∧ ¬B( w ) ∨ ∃xQ(x) ∧ ∀ y ¬Q( y ) ∨ ∀ z ¬A( z ) ∨ B( z )
„“ ” « „ “ ”«
5a. ∃w∃x A(w) ∧ ¬B(w) ∨ Q(x) ∧ ∀y∀z ¬Q(y) ∨ ¬A(z) ∨ B(z)
„“ ” “ ”«
` ´
5b. ∃w∃x∀y∀z A(w) ∧ ¬B(w) ∨ Q(x) ∧ ¬Q(y) ∨ ¬A(z) ∨ B(z)
„ “ ” “ ” “ ”«
6. ∃w∃x∀y∀z A(w) ∨ Q(x) ∧ ¬B(w) ∨ Q(x) ∧ ¬Q(y) ∨ ¬A(z) ∨ B(z)
450
∀x Exercise
Convert the following formula to prenex normal form:
451
∀x 19.0.10 Skolemisation
• Suggested by Thoralf Skolem
• Aim: Get rid of ∃ quantifiers
• Definition:
– ∃ outside ∀: replace by new constant.
∃x∀yA(x, y) becomes ∀yA(c, y).
452
∀x 19.0.11 Intuition
453
∀x Intuition (2)
454
∀x 19.0.12 Important Result
Theorem. Let A be a formula and A its Skolemisation.
Then A is satisfiable iff A is satisfiable.
455
∀x 19.0.13 Clausal Form
To find the clausal form of a formula:
456
∀x Exercise
Skolemize the following formula, then put it into clausal
form:
∀y∃x(P (y) ∨ ¬Q(x, y))
457
∀x An Example
Use resolution to prove that the following argument is valid.
All hungry animals are caterpillars.
All caterpillars have 42 legs.
Edward is a hungry caterpillar.
Therefore, Edward has 42 legs.
Translation to Logic:
458
∀x Prenex Normal Form
459
∀x Clausal form
⎧ ⎫
⎪ {H(Edward)}, ⎪
⎨ {¬H(x), C(x)},
⎪ ⎪
⎬
{¬C(x), L(x)}, {C(Edward)},
⎪
⎪ ⎪
⎪
⎩ ⎭
{¬L(Edward)}
460
∀x Resolution Refutation
8 9
> {H(Edward)}, >
< {¬H(x), C(x)},
> >
=
{¬C(x), L(x)}, {C(Edward)},
>
> >
>
: {¬L(Edward)} ;
~C(Ed)
461
∀x 19.0.14 Summary: Herbrand Approach
Problems
462
∀x 20 Resolution with Unification
How did Robinson solve the problem of detecting
unsatisfiability in predicate logic, and how did that give rise
to logic programming?
• Robinson’s Approach
• Unification
• Resolvents
• Resolution Theorems
463
∀x 20.1 Robinson’s Approach
• Robinson’s Theorem (1965) A set of clauses is
unsatisfiable iff it has a resolution refutation, where
unification is used to match complementary literals.
464
∀x 20.2 Unification
A substitution is a finite set of replacements of
variables by terms, i.e. a set σ of the form
{t1 /x1 , t2 /x2 , · · · , tn /xn }, where the xi are variables
•
and the ti are terms. If A is a formula, Aσ is the
result of simultaneously making the substitutions ti
for each occurrence of xi .
e.g. if A = {P (x), Q(y, y, b)}, σ = {h(y)/x, a/y, c/z} then
Aσ = {P (h(y)), Q(a, a, b)}
Note similar but not the same as a valuation.
A unifier of two literals L1 and L2 is a substitution σ
•
such that L1 σ = L2 σ.
465
∀x Unification (2)
466
∀x 20.2.1 Examples
467
∀x 20.2.2 Unification Algorithm
1. T1 = L1 ; T2 = L2 ; σ0 = {}; i = 0
468
∀x Unification Algorithm (2)
σ1 = {h(y)/x}
T1 = f (h(y), g(h(y))) T2 = f (h(y), g(h(z)))
σ2 = {h(y)/x, y/z}
T1 = f (h(y), g(h(y))) T2 = f (h(y), g(h(y)))
i.e. σ2 is an m.g.u.
469
∀x 20.2.3 Results on Unification
470
∀x Exercise
Find the most general unifier of the following pairs of
terms, if possible. (a, b are constants, x, y, z are variables.)
471
∀x 20.3 Resolvents
• Recall: For Propositional Logic:
– Two literals are complementary if one is L and the
other is ¬L.
472
∀x 20.4 Resolution Theorems
Resolution Principle
If D is a resolvent of clauses C1 and C2 , then C1 ∧ C2 |= D.
Resolution Theorem
A clause set S is unsatisfiable iff ⊥ ∈ R∗ (S), where R∗ (S) is
the set of all clauses obtainable after a finite number of
resolution steps, starting with S.
473
∀x 20.4.1 Example: The Miserable Tadpole
every shark eats a tadpole
∀x(S(x) → ∃y(T (y) ∧ E(x, y))) {¬S(x), T (f (x))}, {¬S(x), E(x, f (x))}
all large white fish are sharks
∀x(W (x) → S(x)) {¬W (x), S(x)}
colin is a large white fish living in deep water
W (colin) ∧ D(colin) {W (colin)}, {D(colin)}
any tadpole eaten by a deep water fish is miserable
∀z(T (z) ∧ ∃y(D(y) ∧ E(y, z) → M (z)) {¬T (z), ¬D(y), ¬E(y, z), M (z)}
negation of: some tadpoles are miserable
¬∃z(T (z) ∧ M (z)) {¬T (z), ¬M (z)}
474
∀x 20.4.2 Refutation
C1 ¬S(x), T (f (x)) C2 ¬S(x), E(x, f (x))
C3 ¬W (x), S(x) C4 W (c)
C5 D(c) C6 ¬T (z), ¬D(y), ¬E(y, z), M (z)
C7 ¬T (z), ¬M (z)
C8 S(c) C4, C3 {c/x}
C9 T (f (c)) C8, C1 {c/x}
C10 E(c, f (c)) C8, C2 {c/x}
C11 ¬D(y), ¬E(y, f (c)), M (f (c)) C9, C6 {f (c)/z}
C12 ¬D(c), M (f (c)) C10, C11 {c/y}
C13 M (f (c)) C5, C12
C14 ¬T (f (c)) C7, C13 {f (c)/z}
C15 ⊥ C9, C14
So there is a miserable tadpole – the one eaten by colin.
475
∀x 20.5 Horn Clauses and Prolog
• Horn clause = clause with at most one positive literal.
e.g. {p(x), ¬q(x, y), ¬r(x, y)}
represents p(x) ∨ ¬q(x, y) ∨ ¬r(x, y)
rewritten as p(x) ∨ ¬(q(x, y) ∧ r(x, y))
rewritten as p(x) ← (q(x, y) ∧ r(x, y))
or, in Prolog notation p(X) :- q(X, Y), r(X, Y).
476
∀x 20.5.1 Example
477
∀x 20.5.2 Linear Resolution Diagram
Example, continued using linear input resolution.
¬q(b) q(x), ¬p(x), ¬s(x) s(b) p(x), ¬r(x) r(b)
pp rrr tt vv
p pp rr tt vv
pp p rr tt vv
r t
ppp rr r tt vvv
¬p(b), ¬s(b) r rr ttt vvv
r rr ttt vvv
r r tt vv
rr r tt v
rr t t vv
rr tt v
t t vvv
¬p(b) ttt vvv
t tt vvv
ttt vvv
ttt vvv
t vv
v
¬r(b)
v vv
vvv
vvv
v
vv
⊥
478
∀x Exercise
Draw a resolution diagram refuting this set of clauses:
{P (a, b)} {¬P (x, y), Q(x, y)} {¬P (x, y), Q(y, x)} {¬Q(b, a)}
479
∀x 20.5.3 (Abstract) Interpreter for Prolog
480
∀x 20.5.4 Prolog in Prolog
A basic Prolog interpreter in Prolog:
solve(true).
solve((G1,G2)) :-
solve(G1),
solve(G2).
solve(Goal) :-
clause(Goal, Body),
solve(Body).
481
∀x 20.5.5 SLD tree
482
∀x 20.5.6 Summary: Robinson’s Approach
483
484
485
486
∀x 21 Semantics of Logic Programs
It is useful to be able to formally characterize what a logic
program means. This allows us to formally analyze
programs.
3. Finding Meaning
4. Approximating Meaning
5. Groundness Analysis
487
∀x 21.1 The Meaning of “Meaning”
What does this C function “mean?”
int sq(int n)
{
return n * n;
}
488
∀x The Meaning of “Meaning” (2)
What does this C function mean?
void hi(void)
{
printf("hello, world!\n");
}
or this:
int count;
void inc(void)
{
count++;
}
489
∀x The Meaning of “Meaning” (3)
Meaning of a program component gives understanding of
that component
Meaning of Haskell function
{0 → 1, 1 → 1, 2 → 2, 3 → 6, 4 → 24, . . .}
490
∀x 21.2 The Meaning of a Logic Program
Meaning of logic program is what it makes true
Ignore Input/Output, assert/retract, etc .
Meaning of a program can be shown as a set of unit
clauses (facts)
This program is its meaning:
capital(tas, hobart). capital(vic, melbourne).
capital(nsw, sydney). capital(sa, adelaide).
capital(act, canberra). capital(qld, brisbane).
capital(nt, darwin). capital(wa, perth).
491
∀x The Meaning of a Logic Program (2)
grandparent(C, G) :-
parent(C, P),
parent(P, G).
Meaning is:
parent(alice, harriet). parent(alice, george).
parent(bob, harriet). parent(bob, george).
parent(harriet, laura). parent(harriet, ken).
492
∀x The Meaning of a Logic Program (3)
Meaning of a recursive predicate is also a set of clauses
E.g., what is the meaning of:
num(0).
num(s(N)) :- num(N).
num(0).
num(s(0)).
num(s(s(0))).
num(s(s(s(0)))).
...
493
∀x 21.3 Finding Meaning
Meaning of program P can be found using immediate
consequence function TP
494
∀x Example
Take program P = {num(0), num(s(N )) : −num(N )}
TP1 (∅) = {num(0)}
(because body of clause is empty)
TP2 (∅) = {num(0), num(s(0))}
TP3 (∅) = {num(0), num(s(0)), num(s(s(0)))}
TP∞ (∅) =
{num(0), num(s(0)), num(s(s(0))), num(s(s(s(0)))), . . .}
Meaning of most programs is infinite
495
∀x Exercise
Give the semantics of the following Prolog program:
p(a,b).
p(b,c).
p(c,d).
q(X,Y) :- p(X,Y).
q(X,Y) :- p(Y,X).
496
∀x 21.4 Why Study Semantics?
• Programming (thinking about your code)
• Implementation
• Language design
• Philosophy
497
∀x 21.4.1 Verification and Debugging
Suppose each clause/predicate of a program is true
according to my intended interpretation
Then everything computed by the program (the meaning,
the set of logical consequences) is true
Correctness can be verified just by reasoning about
individual components
If the program computes something false, one of
clauses/predicates must be false
Declarative debugging systems use the intended
interpretation to find which one
498
∀x 21.5 Approximating Meaning
Can also approximate meaning of a program: gives some
information about the program
Approximation can be made finite: can actually be
computed
Usual approach: replace data in program by an abstraction,
then compute meaning
Many interesting program properties are Boolean: use
propositional logic
E.g., parity (even/odd): abstract 0 by true
Abstract s(X) by ¬x
499
∀x Approximating Meaning (2)
Abstracted num predicate:
num(true) num(0)
num(¬x) ← num(x) num(s(X)) ← num(X)
TP1 (∅) = {num(true)}
TP2 (∅) = {num(true), num(f alse)}
TP3 (∅) = {num(true), num(f alse)}
Can stop here: further repetitions will not add anything
Result is finite
Says that both even and odd numbers satisfy num/1
500
∀x Approximating Meaning (3)
Program:
plus(0, Y, Y).
plus(s(X), Y, s(Z)) :- plus(X, Y, Z).
Abstracted version:
plus(true, y, y) y can be either true or f alse
plus(¬x, y, ¬z) ← plus(x, y, z)
501
∀x Approximating Meaning (4)
This result is better shown as a table:
+ even odd
even even odd
odd odd even
502
∀x Exercise
Use parity analysis to determine the possible parities of the
arguments of p/1 defined as:
p(0,s(0)).
p(s(X), s(s(Y))) :- p(X, Y).
503
∀x 21.6 Groundness Analysis
A more useful property for optimization of Prolog code is
groundness
Again, a Boolean property
append([], Y, Y).
append([U|X], Y, [U|Z]) :- append(X, Y, Z).
Abstracted version:
append(true, y, y)
append(u ∧ x, y, u ∧ z) ← append(x, y, z)
In second clause, u can be true or f alse, so:
append(true, y, y)
append(x, y, z) ← append(x, y, z)
append(f alse, y, f alse) ← append(x, y, z)
504
∀x Groundness Analysis (2)
append(true, y, y)
append(x, y, z) ← append(x, y, z)
append(f alse, y, f alse) ← append(x, y, z)
505
∀x Groundness Analysis (3)
A truth table will help us understand this:
506
∀x Groundness Analysis (4)
So after a call append(X,Y,Z), X and Y will be ground iff Z is
We can use this result in analyzing predicates that call
append
rev([], []).
rev([U|X], Y) :-
rev(X, Z),
append(Z, [U], Y).
Abstracted version:
rev(true, true)
rev(u ∧ x, y) ← (rev(x, z) ∧ append(z, u, y))
507
∀x Groundness Analysis (5)
Using what we’ve learned about append:
rev(true, true)
rev(u ∧ x, y) ← (rev(x, z) ∧ ((z ∧ u) ↔ y))
Simplifying biimplications:
rev(true, true)
rev(x, y) ← rev(x, y)
rev(f alse, f alse) ← rev(x, z)
508
∀x Groundness Analysis (6)
rev(true, true)
rev(x, y) ← rev(x, y)
rev(f alse, f alse) ← rev(x, z)
509
∀x Groundness Analysis (7)
So after rev(X,Y), X is ground iff Y is: x ↔ y
This analysis gives quite precise results
Many other such abstractions have been proposed
This is an area of active research
510
∞ 22 Finite State Machines
What is a program? What is an algorithm? How simple
can we make them?
1. Introduction
3. FSMs in Prolog
4. Formally
5. NFAs
6. Power
511
∞ 22.1 Introduction
There are many programming languages, many
programming paradigms
All have a few things in common; programs:
• Are finite
• Take input
• Produce output
512
∞ 22.2 Finite State Machines
Simplest mechanism we can define to do this always has a
current state, which is one of a finite number of discrete
states
Each input, together with current state, determines next
state and any output
The state captures what is important about the history of
the computation
This is a finite state machine (FSM) or finite automaton
513
∞ Finite State Machines (2)
A touch light is an example of a finite state machine
514
∞ 22.2.1 State Diagrams
We can depict machine as a state diagram or state
transition diagram
touch
0 50
touch touch
150 100
touch
515
∞ 22.2.2 Output
FSMs can produce output
touch/+f1
0 50
touch/−f1,−f2 touch/−f1,+f2
150 100
touch/+f1
516
∞ 22.2.3 Example
This FSM copies its input of binary numbers to its output,
stripping leading 0s:
0/0
1/1
q0 q1
0 1/1
517
∞ Exercise
What output does the following FSM produce with input
aabbaaabbb?
b/b
a/a
q0 q1
b/b b/b
a/a
a q2
518
∞ 22.2.4 Accepting States
Many problems amount to simply determining if the input
is “acceptable” — does it satisfy some criterion
Double circle around state indicates accepting state
When input runs out, if machine is in an accepting state, it
accepts that input string; otherwise it doesn’t
Can have as many accepting states as needed
519
∞ Accepting States (2)
Determine if the input is one a followed by one or more bs
followed by a single c:
b
a b c
q0 q1 q2 q3
a, c a
b, c a, b, c
q4
a, b, c
520
∞ 22.3 FSMs in Prolog
To simulate FSMs in Prolog, we can specify state
transition by a predicate delta(State0, Input, State) which
says that when in State0 we receive Input as input, we
transition into State
Also specify initial and accepting states
initial(q0).
accepting(q3).
521
∞ FSMs in Prolog (2)
Code to similate an FSM is simple:
recognize(Input) :-
initial(State0),
run(Input, State0, State),
accepting(State).
522
∞ Exercise
Design an FSM to recognize strings containing an even
number of as. The input can include both as and bs.
523
∞ 22.4 Formally
What does it take to define a FSM?
Q a set of states
Q, Σ, δ, q0 , F
524
∞ 22.4.1 δ
State transition is specified by a function δ
Given any state and any input, δ yields the state to
transition into
δ : (Q × Σ) → Q
525
∞ 22.5 NFAs
What if state transition were specified by a relation rather
than a function?
δ ⊆Q×Σ×Q
526
∞ 22.5.1 λ transitions
NFAs also often allow λ transitions to be specified
A λ transition is a spontaneous transition — a transition in
response to no input at all
Specify this formally by pretending the input alphabet
contains an extra symbol λ, and there can be as many λs
as you like between any two real symbols
Then, formally δ becomes a relation
δ ⊆ Q × (Σ ∪ {λ}) × Q
527
∞ 22.5.2 Example
This NFA accepts all input that is either some as optionally
followed by some bs or some bs optionally followed by some
as:
a b
a
q1 q2
λ a
b
q0 q5
b a a
λ
b b
q3 q4
528
∞ Example (2)
Important result: any NFA can be converted into an
equivalent DFA
Intuition: state in new DFA corresponds to set of states in
NFA
This is an equivalent DFA to the previous NFA:
a b
b
q1 q2
a a
q0 q5 a,b
b a
b b
a
q3 q4
529
∞ 22.6 Power
FSMs cannot do everything real programs can do
FSMs can recognize ab or aabb or aaabbb:
a a a
q0 q1 q2 q3
a
b b b b
a,b
a,b b b
q5 q5 q4 q3
a
a
530
531
532
533
534
∞ 23 Turing Machines
1. Turing Machines
535
∞ 23.1 Turing Machines
• Introduced by Alan Turing
• No memory limit!
536
∞ Turing Machines (2)
0 1 2 3 4 5 6 ...
...
• A start state q0 ∈ Q
537
∞ 23.1.1 Turing Machines: Initial
0 1 2 n ...
q0
• Initial state q0
538
∞ 23.1.2 Turing Machines: Transition
q q’
• δ(q, x) = [q , y, d]
539
∞ 23.1.3 Turing Machines: Final
... x ...
540
∞ 23.1.4 Turing Machines: Example
Change all a’s to b’s and vice versa.
δ B a b
q0 q1, B, R
q1 q2, B, L q1, b, R q1, a, R
q2 q2, a, L q2, b, L
B/B R B/B L
q0 q1 q2
541
∞ 23.1.5 Turing Machines: Traces
• A configuration uqi vB
– represents the tape uvBBB . . . (v includes
rightmost non blank)
542
∞ 23.1.6 Turing Machines: Example Trace
q0 BabaB
Bq1 abaB
Bbq1 baB
a/b R a/a L
Bbaq1 aB b/a R b/b L
Bbabq1 B
B/B R B/B L
Bbaq2 bB q0 q1 q2
Bbq2 abB
Bq2 babB
q2 BbabB
543
∞ 23.1.7 Turing Machines: Another Example
Machine to add 1 to a binary number
0/0 R
1/1 R 1/0 L
B/1 R
B/B R B/B L 0/1 R
q0 q1 q2 q3
q0 B001B q0 B11B
Bq1 001B Bq1 11B
B0q1 01B B1q1 1B
B00q1 1B B11q1 B
B001q1 B B1q2 1B
B00q2 1B Bq2 10B
B0q2 00B q2 B00B
B01q3 0B 1q3 00B
544
∞ 23.1.8 Turing Machines as Acceptors
0 1 2 3 4 5 6 ...
...
545
∞ Turing Machines as Acceptors (2)
0/0 R 0/0 L
1/1 R 1/1 L
0/0 R B/B R
1/1 R
546
∞ Exercise
Modify the even length palindrome machine to accept odd
length palindromes too e.g. 101
0/0 R 0/0 L
1/1 R 1/1 L
0/0 R B/B R
1/1 R
547
∞ 23.1.9 Turing Machines in Prolog
• given delta(State,Sym,NewState,NewSym,LR)
initial(q0).
548
∞ Main Code
recognize(Input) :-
run(Input, _, State),
accepting(State).
549
∞ State Transition
550
∞ Handling the Tape
551
∞ 23.2 Nondeterministic Turing Machines
• δ maps to a subset of Q × Γ × {L, R} rather than just
one (or zero)
552
∞ Example
a/a R
b/b R
c/c R
553
∞ Example (2)
554
555
556
557
558
∞ 24 Undecidability
Are there some programs that cannot be written?
How many kinds of infinity are there?
559
∞ 24.1 Church-Turing Thesis
There is an effective procedure to solve a decision problem
P if, and only if, there is a Turing machine that answers
yes on inputs p ∈ P and no for p ∈ P .
560
∞ 24.1.1 Evidence for Church-Turing Thesis
– Lambda calculus
– Predicate logic
– Register machines
can be simulated by Turing machine.
561
∞ 24.2 An undecidable problem
• The Halting Problem
– Given an arbitrary Turing machine
M = (Q, Σ, Γ, δ, q0 )
– An input s ∈ Σ∗
562
∞ 24.2.1 Prove the Halting Problem Undecidable
563
∞ Prove the Halting Problem Undecidable (2)
• Example
• M is represented as R(M ) by
00010111011011101100110101010100110110111011011001110110101101000
564
∞ Prove the Halting Problem Undecidable (3)
565
∞ Prove the Halting Problem Undecidable (4)
566
∞ Exercise
Suppose Prolog had a builtin predicate halt(Goal) that
succeeds if Goal would eventually terminate, and fails
otherwise. Write a Prolog predicate contra/0 that calls
halt/1 in such a way that halt/1 cannot possibly work.
567
∞ 24.3 Universal Machines
• We can build a Universal Turing Machine that
simulates any Turing machine:
568
∞ 24.3.1 A Universal Machine U (3 tape machine)
• simulate M on tape 3:
– scan tape 1 for a transition for the state encoded
on tape 2 qi acting on the symbol x under the head
on tape 3
– if no transition, U halts
– otherwise δ(qi , x) = [qj , y, d]
∗ write new state code(qj ) on tape 2
∗ write symbol y to tape 3
∗ move tape head on tape 3 in direction d
569
∞ 24.4 Reducability
• A decision problem P is (Turing) reducible to a
problem P if:
– there is a Turing machine M which maps input p to
p such that p ∈ P iff p ∈ P
s is if even length
accept
M Even length
s length of s in 0s palindrome
reject
s is of odd length
570
∞ 24.4.1 Another Undecidable Problem
– writes s on tape
571
∞ Another Undecidable Problem (2)
572
∞ 24.5 Countability
• There are infinitely many natural numbers {0, 1, 2, . . .}
573
∞ 24.5.1 Integers
−3−2−1 0 1 2 3
• Match nonnegative integers p with natural number 2p
574
∞ 24.5.2 Rationals
575
∞ 24.5.3 Reals
576
∞ 24.5.4 Reals are Uncountable
number
• Contradiction!
577
∞ Exercise
Is the set of integer intervals, e.g. from 1 to 10, or from 0
to 99, or from -78 to -42, countable? Why or why not?
578
579
580
581
582
∞ 25 Complexity
1. What is Complexity?
2. Rates of Growth
3. Tractable Problems
4. NP Problems
583
∞ 25.1 What is Complexity?
• Performance of an algorithm = resources required for
its execution
– Time
– Space
• Examples
– Sorting
– Searching
584
∞ What is Complexity? (2)
585
∞ What is Complexity? (3)
586
∞ 25.1.1 Complexity Example
0/0 R B/B R
1/1 R
587
∞ Complexity Example (2)
588
∞ Complexity Example (3)
589
∞ 25.1.2 Different execution machines
590
∞ Different execution machines (2)
– n for RAM
591
∞ 25.2 Rates of Growth
592
∞ 25.2.1 Big O Notation
f (n) ≤ c × g(n), ∀n ≥ n0
f (n) = n2 + 2n + 5
≤ n2 + 2n2 + 5n2 for n ≥ 1
= 8n2
= 8 × g(n)
593
∞ 25.2.2 Big O Hierarchy
constant O(1) hash table lookup
logarithmic O(log(n)) binary search tree lookup
linear O(n) list lookup
n log n O(nlog(n)) sorting
quadratic O(n2 )
cubic O(n3 ) solving linear equations
polynomial O(nr ) for some r
exponential O(bn ) for some b deciding propositional logic
factorial O(n!)
594
∞ 25.3 Tractable Problems
log2 (n) n n2 n3 2n n!
5 2 5 25 125 32 120
10 3 10 100 1000 1024 3628800
20 4 20 400 8000 1048576 2.4 × 1018
50 5 50 2500 125000 1.1 × 1015 3.0 × 1064
100 6 100 10000 1000000 1.2 × 1030 > 10157
200 7 200 40000 8000000 1.6 × 1060 > 10374
595
∞ 25.4 Nondeterministic Polynomial Time
• A problem is non-deterministic polynomial time if
there is a non-deterministic Turing machine M that
solves the problem of size n in at most a polynomial
number of transitions nr for some fixed r.
596
∞ 25.4.1 SAT
Example: propositional satisfiability
Given a set of propositional clauses, guess a valuation for
all the truth values. Evaluate F under this valuation. If true
answer yes! Given a propositional formula in clausal form
597
∞ 25.4.2 P and N P
• P ⊆ NP
598
∞ 25.4.3 A problem in P
• Algorithm
– For each clause x ← x0 , . . . xn set count = n
599
∞ 25.4.4 P and N P and reducibility
600
∞ 25.4.5 Proving N P completeness
• 3SAT.
– A 3SAT problem is a SAT problem where each
clause has at most 3 literals.
601
∞ 25.4.6 N P completeness
• Thousands of problems have been shown to be N P
complete!
602
∞ 25.5 Summary
• Church Turing thesis: effective procedures
• Turing Machines
603
604
605
606
∞ 26 Review
Cannot cover the whole semester in one lecture, but we’ll
try. . . .
1. Propositional Logic
2. Predicate Logic
3. Automata
607
∞ 26.1 Propositional Logic
A proposition is statement that can be either true or false,
e.g. “interest rates are rising”
Assign propositional variable to simple propositions for
convenience, e.g. R for “interest rates are rising”
Combine propositions using connectives:
¬ (negation, not)
∧ (conjunction, and)
∨ (disjunction, or)
→ (material implication, if)
↔ (biimplication, iff)
608
∞ Exercise: From English
609
∞ 26.1.1 Truth Tables
Truth tables have a row for each combination of values of
propositional variables, column for each formula of interest
610
∞ 26.1.2 Arguments
Argument is a sequence of propositions ending in a
conclusion, e.g.:
When interest rates rise, share prices fall.
Interest rates are rising. Therefore, share prices
will fall.
Conclusion is claimed to follow from premises
To check correctness, see if conjunction of premises imply
conclusion, e.g. ((R → S) ∧ R) → S
R S R→S (R → S) ∧ R ((R → S) ∧ R) → S
T T T T T
T F F F T
F T T F T
F F T F T
611
∞ 26.1.3 Better Way
Usually faster to demonstrate argument validity by
contradiction: assume it is invalid, and show a
contradiction
Simple technique can help with the bookkeeping: write out
the formula and write T or F under each variable and
connective as you determine them, propagating variable
values
((R → S) ∧ R) → S
T X F T T F F
612
∞ 26.1.4 Axiomatic System (AL)
Axiom schemas — allow any wff for A, B, and C
Ax1 A → (B → A)
Ax2 (A → (B → C)) → ((A → B) → (A → C))
Ax3 (¬A → ¬B) → (B → A)
One rule of deduction, or inference rule: modus ponens:
from A and A → B infer B
AL system is:
1. Consistent: A iff ¬A
2. Sound: A implies |= A
3. Complete: |= A implies A
613
∞ Exercise: Complete the proof
{A → B, B → C} A → C
1 B→C Hyp
2 (B → C) → (A → (B → C)) Ax1
3 (a) MP 1,2
4 (A → (B → C)) → ((A → B) → (A → C)) Ax2
5 (A → B) → (A → C) (b)
6 (c) Hyp
7 A→C MP 5,6
(a)
(b)
(c)
614
∞ Exercise: Normal Forms
615
∞ 26.1.5 Resolution
616
∞ Resolution (2)
Resolution Linear Resolution Linear Input
A, B ~A, B A, ~B ~A, ~B A, B ~A, B
Resolution
~q q, ~p, ~s
B A, ~B
B ~B ~p, ~s p, ~r
A ~A, ~B
~r, ~s r
⊥ ~B B
Resolve any 2 ~s s
clauses ⊥
Resolve result of ⊥
last step with any Resolve result of
other clause last step with
clause from
program
All are sound. All are complete, except L.I. Resolution:
complete only for Horn clauses
617
∞ 26.2 Predicate Logic
• Variables stand for anything
618
∞ 26.2.1 Quantifiers
619
∞ 26.2.2 From English
620
∞ Exercise: To and from English
B(x) x is a book
P (x) x is a person
O(x, y) x is older than y
A(x, y) x is the author of y
R(x, y) x is a reader of y
• Everyone is a book
• Everyone reads books
• ∀x(∃yA(x, y) → ∃y(B(y) ∧ R(x, y)))
• Every author is older than the books they write
621
∞ 26.2.3 Normal Forms
622
∞ 26.2.4 Clausal Form
623
∞ 26.2.5 Resolution
624
∞ 26.2.6 Unification
625
∞ Exercise: Resolution Proof
Use resolution to prove the following set of clauses is
unsatisfiable:
{A(c, v, v)}, {A(f (u, x), y, f (u, z)), ¬A(x, y, z)}, {¬A(f (a, c), f (a, c), v)}
626
∞ 26.3 Automata
• Finite Automata (Finite State Machine) handles a
sequence of input symbols from input alphabet set Σ
• Machine’s reaction to input symbol depends on current
state, an element of Q
• Reaction to input specified by function δ : Q × Σ → Q
• Machine must also specify initial state q0
• Machine may act as a recognizer ; then must also
specify a set of states F ; when input runs out, if state
of machine ∈ F then input is accepted by machine
• Machine may produce output; then δ may also specify
an output symbol to produce for given state and input
• Nondeterministic Finite Automaton (NFA) is FA where
δ is a relation rather than a function; Any NFA can be
rewritten as a DFA
627
∞ Exercise
Often drawn as diagram with circles representing states
and arcs labeled by input symbols representing transitions
(δ function)
What input does this FSM accept?
c
b c
b c c
q0 q1 q2 q3
a a,b
a, b
a
q4
a, b, c
628
∞ 26.3.1 Pushdown Automata
629
∞ 26.3.2 Turing Machines
• δ : Q × Γ → Q × Γ × {L, R}
630
∞ Exercise
Which of these inputs does the following machine accept?
More generally, what inputs will the machine accept?
a/a L
a/a R b/b R b/b L
x/x R x/x R x/x L
x/x R B/B R
B/B R B/B R
q3 q5
• abaca x/x R
• abc
• aabbcccc
• aaabbbccc
631
∞ 26.3.3 Undecidability
632
∞ 26.3.4 Countability
633
∞ 26.3.5 Complexity
634
determining satisfiability of propositional logic formulae
635
636