Lab 11 - Logic Programming Languages (Answers)
Lab 11 - Logic Programming Languages (Answers)
Lab 11 - Logic Programming Languages (Answers)
As this is an 'odd' week we'll be investigating the theory side of logic programming with regard to the
Prolog programming language (which is pretty much the only 'mainstream' programming language in
common use!)…
… but, it's also the last lab of the course (no week 12 lab!) – so
what the heck – let's go out with a bang and get our logic on like
Mr. Spock via the joys of Prolog! =D
http://sourceforge.net/projects/gprolog/
mortal(X) :- person(X).
Rather than typing in facts and rules each time, it's far better to place them in a file with a .pl extension
and then open them with GNU Prolog – for example, we can enter the following into a file call
SiblingTest.pl:
% Facts
parent(bill, jake).
parent(bill, shelley).
% Queries to try:
% sibling(X,Y). % X = jake, Y = shelley
% sibling(bill, X). % No.
% sibling(jake, X). % X = shelley
% sibling(X, X). % no
Give it a go and try using some of the above queries (you have to type them in – including the full
stop!). Hit Ctrl+D if you get 'stuck' and can't enter any more commands (or restart GNU Prolog!)
When tracing is enabled it steps through all the steps that occur in the deduction process – so we can
examine the truth of the deduction in detail and see why something is so, not just that it happens to be!
As a final example, try this – there's a sketch in Monty Python about deductive logic, where the
characters logically deduce that you can tell if someone is a witch by whether they weigh the same as a
duck or not!
% X burns if X is wooden
burns(X) :- wooden(X).
% X is wooden if X floats
wooden(X) :- floats(X).
% girl is a female
female(girl).
% Try:
% trace.
% witch(girl).
This is just some basic aspects of Prolog, there's a lot more to it than this – as a final task before we hit
the questions, take a look at the following 'quick 'n dirty' prolog tutorial:
http://www.cs.utexas.edu/~cannata/cs345/Class%20Notes/12%20prolog_intro.pdf
It guides you through some simple ancestry tests, but also, there's a great little section at the end
which ties in predicate calculus and explains why horn clauses are so important. The section is:
Connecting Prolog to Predicate Logic (page 5), and it's definitely worth reading if you want to get
better at computer science and climb the ranks of this here pyramid!
1. In logic programming, what are the two parts of a compound term? Give at least one example.
[Q2-Mod]
A compound term is one element of a mathematical relationship, written in a form that has the appearance of
mathematical function notation. Compound terms are composed of two parts:
- A functor - which is the functional symbol that names the relation, and
- An ordered list of parameters - which together represent an element of the relation.
A compound term with a single parameter is a 1-tuple; one with two parameters is a 2-tuple, and so on. For
example, we might have the two propositions:
man(jake)
like(bob, steak)
2. In logic programming, what is the general form of a proposition in clausal form? [Q4]
One problem with predicate calculus is that there are too many different ways of stating propositions that have
the same meaning; that is, there is a great deal of redundancy. To simplify matters, a standard form for
propositions is desirable - and this is called clausal form.
All propositions can be expressed in clausal form, which has the following general syntax:
B1 ∪ B2 ∪ … ∪ Bn ⊂ A1 ∩ A2 ∩ … ∩ Am
The meaning of this is: If all of the A's are true, then at least one of the B's is true.
3. In logic programming, what are antecedents and consequents? Give an example of each.
[Q5-Mod]
The right-hand side of a clausal form is called the antecedent. The left-hand side is called the consequent
(because it is the consequence of the truth of the antecedent).
This states: If bob likes fish and trout is a fish then bob likes trout
In this example likes(bob, trout) is the antecedent, and likes(bob,fish) ∩ fish(trout) are the consequents.
The concept of resolution is the following - suppose there are two propositions with the forms:
P1 ⊂ P2 // P2 implies P1
Q1 ⊂ Q2 // Q2 implies Q1
Their meaning is that P2 implies P1 and Q2 implies Q1. Now suppose that P1 is identical to Q2, so we could
rename P1 and Q2 as T. Then, we could rewrite the two propositions as:
T ⊂ P2 // P2 implies T
Q1 ⊂ T // T implies Q1
Now, because P2 implies T and T implies Q1, it is logically obvious that P2 implies Q1, which we could write as:
Q1 ⊂ P2 // P2 implies Q1
- If joanne is the mother of jake, then joanne is older than jake, AND
- If joanne is older than jake, then joanne is wiser than jake.
T ⊂ P2
Q1 ⊂ T
Which means: If joanne is the mother of jake then joanne is wiser than jake.
Programming in both imperative and functional languages is primarily procedural, which means that:
In other words, the computer is treated as a simple device that obeys orders, and everything that is computed
must have every detail of that computation spelled out, step-by-step!
Programming in a logic programming language is nonprocedural - so programs in such languages do not state
exactly how a result is to be computed but rather describe the form of the result. Instead of providing exact
instructions, a set of rules are provided which may be applied to the data in order to infer a result.
The nature of Prolog’s resolution sometimes creates misleading results, because the only truths, as far as Prolog
is concerned, are those that can be proved using its database, and it has no knowledge of the world other than its
database.
When the system receives a query and the database does not have information to prove the query absolutely, the
query is assumed to be false!
This is a limitation because Prolog can prove that a given goal is true…but it cannot prove that a given goal is
false. Instead, it simply assumes that something is false if it cannot prove it to be true.
Problem Set
1. Write a Prolog description of the Simpsons family tree as depicted below - be sure to include all
mother and father relationships. Add simple parent, sibling, grandparent and cousin rules
and experiment with the data set in Prolog to determine: [PS3-Mod]
- Which family members are siblings,
- Which family members are grandparents, and
- Which family members are cousins.
mother(mona,homer).
mother(jackie,marge).
mother(jackie,patty).
mother(jackie,selma).
mother(marge,bart).
mother(marge,lisa).
mother(marge,maggie).
mother(selma, ling).
% Try:
% trace.
%
% -- sibling tests --
% sibling(bart,lisa). -- answer should be yes
% sibling(marge,patty). -- answer should be yes
% sibling(homer,herb). -- answer should be yes
% sibling(homer,selma). -- answer should be no
% -- grandparent tests --
% grandparent(abraham,bart). -- answer should be yes
% grandparent(abraham,homer). -- answer should be no
% grandparent(jackie,ling). -- answer should be yes
% grandparent(mona,maggie). -- answer should be yes
% grandparent(clancy,marge). -- answer should be no
% -- cousin tests --
% cousin(ling,bart). -- answer should be yes
% cousin(homer,herb). -- answer should be no
a. ancestor(Fred,Mike) :- father(Fred,Mike).