Exercises in Logic and Knowldege Representation: Brandon Bennett October 6, 2014
Exercises in Logic and Knowldege Representation: Brandon Bennett October 6, 2014
Brandon Bennett
October 6, 2014
Contents
1 Translating from English into First-Order Logic
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
8
8
12
13
13
13
13
13
14
15
16
17
17
18
18
18
19
20
21
22
5 Natural Deduction
5.1 The Rules . . . . . . . . . . . . . . . .
5.2 Derivations of Derived Rules . . . . . .
5.3 Natural Deduction Tips and Examples
5.4 Natural Deduction Exercises . . . . . .
5.4.1 Answers . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
23
23
24
26
28
29
.
.
.
.
.
.
.
.
.
.
2
31
32
Batch 1
Formulate the following English sentences as formulae in classical 1st-order logic
(answers are given at the end of this document).
1. All purple mushrooms are poisonous.
2. No student likes every lecture.
3. Everest is the highest mountain on Earth.
4. There are at least two apples in a barrel.
5. There are at least two apples in every barrel.
6. x is part of y just in case everything that is connected to x is also connected to
y (this is a fundamental definition used in certain theories of spatial relations).
7. No region is part of each of two disjoint regions.
8. Nothing can be inside two different boxes unless one box is inside the other.
(Quite hard)
Batch 2
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
Batch 3
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Here are some possible answers for some, but not all of the translation examples.
There will generally be many acceptable translations of any given English sentence.
The main reasons for this are the following:
Batch 1
1. x[(Mushroom(x) Purple(x)) Poisonous(x)]
2. x[Student(x) y[Lecture(y) Likes(x, y)]]
3. Mountain(everest) x[Mountain(x) Higher(x, everest) On(x, earth)]
4. xyz[Apple(x) Apple(y) (x = y) Barrel(z) In(x, z) In(y, z)]
5. zxy[Barrel(z) (Apple(x) Apple(y) (x = y) In(x, z) In(y, z))]
(Note that the universal quantification over barrels comes before the existential
quantifiers over apples. If we had xyz[. . .] this would mean that the same
two apples were present in every barrel.
6. P(x, y) z[C(z, x) C(z, y)]
(In this formula, the connective is logical equivalence and means the
same as ( ) ( ).)
7. xyz[Reg(x) Reg(y) Reg(z) P(x, y) P(x, z) Disjoint(y, z)]
8. xb1 b2 [(Box(b1 ) Box(b2 ) (b1 = b2 ) In(x, b1 ) In(x, b2 ))
(In(b1 , b2 ) In(b2 , b1 ))]
(There are many equivalent alternative formulations; but there are also many
similar formulae which are incorrect.)
Batch 2
1. Jane ate a mushroom that she had picked herself.
x[Mushroom(x) Ate(jane, x) Picked(jane, x)]
2. No yellow frogs are edible.
x[Frog(x) Yellow(x) Edible(x)]
3. All students that had missed a lecture answered at least one question incorrectly.
4. Young creatures who go up in balloons are liable to giddiness.
x[(Y y Cx Bx) Gx]
(Illustrates a concise representation for the logical form of the sentence.)
5. Every bag contains at least one coin.
x[Bag(x) y[Coin(y) Contains(x, y)]]
Batch 3
1. Every person loves themself.
x[Person(x) Loves(x, x)]
2. Everyone loves some person who loves themself.
x[Person(x) y[Person(y) Loves(x, y) Loves(x, x)]]
3. All gardeners love the sun.
x[Gardener(x) Loves(x, sun)]
(Since sun refers to a unique object, it is most straightforwardly represented
by a constant (sun).)
4. ...
1 (R) = {ha, ai, ha, bi, ha, ci, hb, ai, hb, bi, hb, ci, hc, ai, hc, bi, hc, ci}
2 (R) = {hb, ai, hb, bi, hb, ci}
2 (R) = {ha, bi, hb, ai, hc, ai}
3 (R) = {ha, ai}
For each of the following formulae, state which of the models M1 M4 satisfy
it, if any:
(a) x[R(x, x)]
(b) x[R(x, x)]
(c) xy[R(x, y)]
(d) xy[R(x, y)]
(e) xy[R(x, y)]
(f) xy[R(x, y)]
4.1
A wide variety of automated theorem provers have been developed since their origin
in the 1960s. However, this kind of program is difficult to implement and often
difficult to use. Thus, very few theorem provers have gained many users beyond
the researchers who developed them. One of the more popular was Otter, which
at one time was arguably the most powerful prover around, and was able to verify
a number of theorems that mathematicians had hypothesised but not been able to
prove. Prover9 is an improved version of the original Otter. Although it is not usually
regarded as one of the most powerful, it does have the advantage of being relatively
simple compared to most of the more modern provers.
Documentation and downloads can be found on the Prover9 home page at:
http://www.cs.unm.edu/~mccune/prover9/
Simple Examples
Here is an example of a very simple Prover9 file:
formulas(assumptions).
p & s.
% & symbol is for conjunction and
p -> q.
% -> symbol is for implication implies
q -> r.
end_of_list.
formulas(goals).
r | t.
end_of_list.
If you are using the GUI version you dont need the lines declaring the formula list
and goal list. You just enter the formulae into the appropriate boxes in the interface.
Here is a slightly more complex example using quantifiers (borrowed from the introductory material on the Prover9 web site):
formulas(assumptions).
all x all y (subset(x,y) <-> (all z (member(z,x) -> member(z,y)))).
end_of_list.
formulas(goals).
all x all y all z (subset(x,y) & subset(y,z) -> subset(x,z)).
end_of_list.
Actually Prover9 allows multiple goal formulae, but it is best to use just one, since the semantics
of multiple goals is potentially confusing.
10
formulas(assumptions).
all x (Wolf(x) -> animal(x)).
all x (Fox(x) -> animal(x)).
all x (Bird(x) -> animal(x)).
all x (Caterpillar(x) -> animal(x)).
all x (Snail(x) -> animal(x)).
all x (Grain(x) -> plant(x)).
exists
exists
exists
exists
exists
exists
x
x
x
x
x
x
Wolf(x).
Fox(x).
Bird(x).
Caterpillar(x).
Snail(x).
Grain(x).
Continued Overleaf.
11
% All animals either eat all plants or eat all smaller animals that eat some plants.
all x (animal(x) -> (all y (plant(y)->eats(x,y)))
|
(all z ( animal(z) &
Smaller(z,x) &
(exists u (plant(u)&eats(z,u)))
->
eats(x,z)))).
all
all
all
all
all
x
x
x
x
x
all
all
all
all
all
y
y
y
y
y
4.2
12
Enter propositional logic translations of the following arguments into Prover9 (using Prover9s ASCII representation for the formulae). Then use Prover9s theorem
proving capabilities to show that the arguments are valid:
Remember that when translating to propositional logic you often have to simplify
and/or alter the structure of sentences. You need to look for underlying propositions
(i.e. statements that are true or false) and recast each sentence in terms of these
propositions combined by means of the truth-functional operators.
A)
1.
2.
3.
4.
If Alan did not lock the door he either forgot or lost the key.
If he lost the key he will be in trouble.
If he has a good memory he did not forget.
Alan has a good memory.
Either Alan locked the door or he will be in trouble.
B)
1.
2.
3.
C)
1.
2.
3.
4.
5.
Sunday is a Holiday.
Saturday and Sunday is the weekend.
On holidays the University is closed.
It is the weekend.
It is not Saturday
The University is closed.
Note that in some contexts we can translate names of days or time periods into
propositions. Thus, sometimes Saturday can be translated as a proposition,
i.e. as It is Saturday.
D)
4.3
13
The Syllogisms were probably the first forms of logical inference that were systematically investigated by humans. The Greeks originally studied arguments in the context
of legal disputes and recognised that some (but not all) arguments are based on the
form of sentences with which they are expressed. A syllogism is a basic form of argument in which the meanings of two assertions are used to derive a consequence. More
specifically, the term syllogism is usually applied to a class of inferences involving
sentences constructed from two property terms (predicates) linked by quantifiers and
possibly negations.
The Medieval logicians continued the study of syllogisms and gave them all names
based on a coding of the quantifiers and negations into letters. To find out more about
the valid syllogisms and the coding used to name them take a look at the Wikipedia
article http://en.wikipedia.org/wiki/Syllogism.
4.3.1
Barbara
Celarent
Ferio
1. No homework is fun.
2. Some reading is homework.
Some reading is not fun.
4.3.4
Baroco
4.4
14
The basic syllogisms involve exactly two predicates in each sentence. But we can use
similar reasoning principles when this is not the case.
A)
B)
1.
2.
3.
4.5
15
Here are a couple of Syllogisms that are not valid. Try proving them and see what
happens.
A)
B)
C) Note that an argument can be logically invalid even if the conclusion is true.
The conclusion may happen to be true even though this is not a consequence
of the premisses.
1. Some birds fly.
2. Some flying things eat nuts.
Some birds eat nuts.
D) Conversely an argument can be logically valid even if the conclusion is false.
1. Some fish eat bananas.
2. All banana eaters enjoy backgammon.
Some fish enjoy backgammon.
If an argument is valid but its conclusion is false, then at least one of its premisses must be false.
An argument with one or more false premisses is called unsound.
4.6
A)
1.
2.
3.
Socrates is Greek.
All Greeks are men.
All men are mortal.
Socrates is mortal.
B)
1.
2.
3.
4.
Porky is a pig.
Pigs are animals.
All pigs are fat.
No fat animals run fast.
Porky does not run fast.
C)
1.
2.
3.
16
4.7
17
Translate the following sentences into 1st-order logic and put them in a file using
Prover9 syntax.
1. Someone who lives in Dreadbury Mansion killed Aunt Agatha.
2. Agatha, the butler, and Charles live in Dreadbury Mansion, and are the only
people who live therein.
3. A killer always hates his victim, and is never richer than his victim.
4. Charles hates no one that Aunt Agatha hates.
5. Agatha hates everyone except the butler.
6. The butler hates everyone not richer than Aunt Agatha.
7. The butler hates everyone Aunt Agatha hates.
8. No one hates everyone.
9. Agatha is not the butler.
Now use the Prover9 theorem prover to deduce who killed Aunt Agatha.
(Hint: try for each of the possibilities.)
4.8
Ballooning Pigs
All, who neither dance on tightropes nor eat penny-buns, are old.
Pigs, that are liable to giddiness, are treated with respect.
A wise balloonist takes an umbrella with him.
No one ought to lunch in public who looks ridiculous and eats penny-buns.
Young creatures, who go up in balloons, are liable to giddiness.
Fat creatures, who look ridiculous, may lunch in public, provided that they do
not dance on tightropes.
7. No wise creatures dance on tightropes, if liable to giddiness.
8. A pig looks ridiculous, carrying an umbrella.
9. All, who do not dance on tightropes, and who are treated with respect are fat.
Use Prover9 to determine that, from the propositions given in part (b), the following
conclusion can be inferred:
No wise young pigs go up in balloons.
In order to prove this you will have to add two further formula to the assumptions.
Each of these additional formula is a general axiom which captures a necessary interdependence between two of the concepts that occur in propositions 1-9. One of them
is a general fact about pigs and the other is about age.
3
This exercise is based on a problem set by Lewis Carroll (Author of Alice in Wonderland ) in
his book Symbolic Logic.
4.9
18
Illegible Letters
Encode and prove the following argument. You may assume that the domain of
discourse consists of all the letters in the room.
1.
2.
3.
4.
5.
6.
7.
8.
9.
4.10
All the dated letters in this room are written on blue paper.
None of them are in black ink except those that are written in the third person.
AI have not filed any of them that I can read.
None of them that are written on one sheet are undated.
All of them that are not crossed are in black ink. :
All of them written by Brown begin with Dear Sir :
All of them written on blue paper are filed. :
None of them written on more than one sheet are crossed.
None of them that begin with Dear Sir are written in third person.
The letters written by Brown cannot be read.
Encode the preceeding statements in first-order logic and use an automated theorem
prover to prove that:
Charles has received an email from Deborah.
4.11
In Qualitative Spatial Reasoning the spatial part relation is defined in terms of the
connection relation as follows:
xy[P(x, y) z[C(z, x) C(z, y)]] .
Use a theorem prover to demonstrate that the part relation is transitive.
4.12
19
Bovinophobia
4.13
20
Favourite Colours
a) Translate the following sentences into an appropriate encoding for the first-order
logic theorem prover that you are using:
1. There are three children called Alfred, Betty and Charles.
(Hint: you must explicitly ensure that different names refer to distinct
individuals).
2. Every child is wearing either a hat, coat or a scarf.
3. Alfred is wearing a red hat.
4. Betty is wearing a green coat.
5. Charles is wearing a blue scarf.
6. Each child has a different favourite colour, which is either red, green, or
blue.
7. No child is wearing anything that has their favourite colour.
8. Bettys favourite colour is red.
b) Use the theorem prover to determine what are the favourite colours of Alfred
and Charles.
c) Which proposition is redundant to the proof?
d) By examining the theorem provers output, give relevant information that for
comparing the behaviour of the prover (e.g. run-time, inferences generated etc.)
when generating a proof, with and without the redundant proposition.
4.14
21
A Geographic Example
Exercise Specification
In this exercise you will analyse a set of natural language sentences expressing a
variety of forms of geographic information and translate the core meaning into firstorder logic.
You will then look at a further set of sentences, which intuitively follow as consequences of the first set. Your aim will be to demonstrate that given a suitable logical
representation of the sentences, together with additional axioms specifying interconnections between the concepts that are involved, each of these consequences can be
proved using a first-order logic theorem prover (i.e. Prover9 ).
Step 1
Translate the following sentences into first-order logic and enter their representations
into Prover9 (in the assumptions box).
Given information:
1. Leeds is a city located in Yorkshire.
2. Sheffield is a city located in Yorkshire.
3. Lancaster is a city located in Lancashire.
4. Lancaster has a port.
5. Leeds is the largest city in Yorkshire.
6. Yorkshire is a county of the United Kingdom.
7. Lancashire is a county of the United Kingdom.
8. Yorkshire borders Lancashire.
Step 2
Think about why the following formulae are consequences of the ones you have just
analysed. Then translate them into first-order logic and enter their representations
into Prover9 but enter them into the goals box of the interface.
Consequences:
1. Sheffield is smaller than Leeds.
2. Leeds is located in the United Kingdom.
3. Leeds is not located in Lancashire.
4. Yorkshire is an administrative region.
5. Lancashire borders the sea.
22
Step 3
Try to identify axioms capturing logical relationships between the concepts in the
previously given sentences that are necessary to derive the consequences from the
given information.
Enter your axioms into the assumptions box. But keep them separate from the
given information. For clarity, I advise that you use comments to give an informal
description of each of your axioms. Comments can be given on lines starting with
the% character.
Step 4
Try to prove each of the consequences from the combination of the initial information
and your additional axioms.
To do this you should comment out all the goals apart from the one you wish to
prove. Then press the Start button to run Prover9. It should then tell you whether
the goal follows from your formalisation of the initial information and axioms.
Use comments to make a note of which goals have been proved.
You dont need to worry about the form of the actual proofs as for the purpose of
this exercise we are treating the theorem prover as a black box.
4.15
UK Geography Reasoning
23
Natural Deduction
This section begins with a concise summary of the rules of a Natural Deduction
proof system for propositional logic. Following that, I shall give a number of tips on
how to construct proofs along with some examples. At the end of the section is the
actual exercise, which is a list of sequents that you should try to prove using Natural
Deduction.
5.1
The Rules
P Q
Ir
[ A ]n
..
..
P P
A
Rules for
[ P ]n
..
..
Q
P Q
AB
P Q
Il
[ A ]na
..
..
P
In
El
[ B ]nb
..
..
P
En
[ A ]n
..
..
P P
En
P
In
P Q
Er
P
P Q
P Q
P Q
E
Rules for
[ A ]n
..
..
In
En
EF SQ
P P
5.2
[ A ]n
..
..
24
EM
DN E
The following rules are quite fundamental, but can actually be derived using the basic
introduction and elimination rules. Here are the proofs:
[ A ]n
I
(P P ) A
P P
A
Er
P P
EF SQ
En
Ir
[ (P P ) ]2
I
(P P ) (P P )
I1
P
P P
Il
=
[ (P P ) ]2
I
(P P ) (P P )
P P
E2
P P
EM
25
P P
P
P
DN E
P
E1
[ A ]1
I
A A
EF SL
A B
A B
MP
B
[ B ]2
E1,2
EM
P Q
P Q
[ P ]2
Il
P Q
Ir
E1,2
[ P ]n
..
..
Q
P Q
CPn
5.3
26
P Q
E
QR
E
[ B ]1a
AB
[ C ]1b
AC
Ir
BC
(A B) (A C)
(A B) (A C)
Il
(A B) (A C)
E1
(Note that the assumption A occurs twice. Proofs often contain duplicates of the
assumptions. This just means that the assumption takes part in more than one rule
application.)
27
[ A ]1
BC
C
AC
I1
Ir
(P Q)
I
(P Q) (P Q)
I1
But a similar form of proof using E can be employed to prove an un-negated conclusion:
P ` P
[ P ]1
P P
P
I
E1
5.4
28
Construct Natural Deduction proofs of each of the following sequence, using any of
the rules in the Basic Rule Set and the Rules for (as specified in Section 5.1
above). All the given sequents are valid and therefore provable (since this rule set is
complete for propositional logic reasoning).
Answers to cases af are given below.
a) P Q ` P (Q R)
b) P Q, Q ` P
c) P Q ` Q P
d) P Q, P R, Q R ` R
e) P (Q R), P, Q ` R
f) P (Q R) ` (P Q) R
g) P ` P
h) P Q, P R, Q R ` R
i) P ` Q P
j) ` (P Q) (Q P )
k) ` P (P Q)
l) (P Q) ` P
m) (P Q) ` P Q
n) P Q ` (P Q)
o) P Q ` Q P
p) (P Q) (P R) ` P (Q R)
q) R, (P Q) R ` P
r) P (Q R) ` (P Q) R
29
Answers
a)
P Q
P Q
El
Q
Er
QR
Ir
I
P (Q R)
b) [ P ]1
P Q
E
Q Q
I1
c) [ P ]1
P Q
E
[ Q ]2
Q Q
I1
P
Q P
d)
I2
P R
[ P ]1a
QR
[ Q ]1b
E
P Q
R
E1
e)
[ Q ]2a
[ R ]1
I
Q R
P (Q R)
Q R
Er
Q Q
E1
R
R
[ R ]2b
E2
30
P (Q R)
Er
P Q
P (Q R)
QR
[ Q ]1a
El
[ R ]1b
Ir
(P Q) R
(P Q) R
Il
(P Q) R
E1
31
By default, Prover9 carries out a wide variety of different kinds of inference. Most of
these are varieties of inference, but they may combine several resolution steps together
(e.g. Hyper-resoluton) or they only operate in restricted cases (for instance when the
result of the inference is particularly simple).4 Often a combination of these mutistep and restricted application rules can perform better than using the basic binary
resolution rule. However, depending on which rules are chosen, the resulting system
may or may not provde a complete inference system.
To see how the simple binary resolution rule works, it is easiest if we run Prover9
from the command line rather than using the GUI. That way we have full control
over the input file that the prover will use and can directly specify which rules we
want it to do.
As a first example, put the following into a text file (let us call the file binres test.p9):
set(raw).
%% Clear all default rules
set(binary_resolution). %% Add binary resolution rule
set(factor).
%% Add factoring rule
formulas(assumptions).
A | B.
-A | -C | D.
A | D.
-A | C.
-B | C.
-C | -D.
end_of_list.
The first line removes all the default inference rules and the following two add binary
resolution and factoring rules. (For propositional reasoning we do not actually need
to set the factoring fule, because Prover9 always merges identical literals within each
new formula that it infers; but it is needed to get a complete inference system for
first-order logic (i.e. if we have formula with quantifiers.)
Now from the command prompt (in the same directory as the input file) enter:5
/usr/local/prover9/bin/prover9 < restest.p9
Now look at the output. You can skip past the initial processing and search sections.
The actual proof should be near the end of the file. Here you see that a series of
resolution steps has been applied, resulting in a final cancellation of a literal with its
negation. This results in the so-called empty clause, which in Prover9 is denoted by
$F. This shows that the assumptions are inconsistent.
4
An example of this is so-called unit resulting (UR) resolution, which is only used when the
infered formula consists of a single literal.
5
You may want to redirect the output to a file by adding > output file at the end of the
command.
6.1
32
In the previous example, we gave Prover9 a set of formulae all in clausal form form
and with no conclusion that we wanted to prove. In this case the system simply
applies binary resolution repeatedly. This gives us a complete test for consistency of
the assumptions: the empty clause will be derived if and only if the empty clause $F
can be derived.
In the following example the input is not quite so simple. For one thing, the assumptions are given in terms of arbitrary propositional formulae, not clauses. This
means that, before using resolution, Prover9 must convert the formulae into clausal
form. For another thing, we have a goal, i.e. a conclusion we want to prove. Since
resolution detects inconsistency, we add the negation of the goal to the assumptions.
If the negation of the goal is logically inconsistent with the other assumptions then
the goal must be a logical consequence of the premisses.
Run Prover9 on the following example file (in the same way as for the previous
example):
set(raw).
%% Clear all default rules
set(binary_resolution). %% Add binary resolution rule
set(factor).
%% Add factoring rule
formulas(assumptions).
p & s.
% & symbol is for conjunction and
p -> q.
% -> symbol is for implication implies
q -> r.
end_of_list.
formulas(goals).
r | t.
end_of_list.
If you look through the output, you will see how Prover9 first converts the input into a
set of clauses by adding the the negation of the goal to the assumption and converting
all formulae to clausal form. It then proceeds to compute resolution inferences in order
to construct a proof. The proof consists of all the assumptions that are required for
the proof together with all inferred formulae that contribute to the chain of inference
leading to the derivation of $F.