Sie sind auf Seite 1von 6

ITECH5403 Comparative Programming Languages

School of Science, Engineering and Information Technology

Lab 11 – Logic Programming Languages


Introduction

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

Getting and Using Prolog

To start off, go and grab a copy of GNU Prolog from:

http://sourceforge.net/projects/gprolog/

Just hit the [Download] button and you should be happily


downloading setup-gprolog-1.4.4-mingw-x64.exe or a newer
version (if available). Once it's downloaded, install is just a few
clicks, and you should have a GNU Prolog shortcut on your
desktop like this one:

Double click it, and we're ready to go!

So what now?! Well… If you enter something like:

mortal(X) :- person(X).

CRICOS Provider No. 00103D Insert file name here Page 1 of 6


…then GNU Prolog will moan, because it wants all the facts placed in a file for it to read. However, let's
work around that for the moment by typing [user]. to put ourselves in user mode. We can then say that
if X is mortal, then X is a person – and we can say that Socrates is a person – therefore, we can use
logic (or really, we can get Prolog to use logic) to deduce that Socrates is mortal! Poor So-crates! ;-)

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).

% ----- Propositions -----

% X is a sibling of Y if P is the parent of X and P is the parent of Y and X is not


equal to Y
% Note: The X \== Y part is to stop someone from being their own sibling!
sibling(X, Y) :- parent(P, X), parent(P, Y), X \== Y.

% 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!)

CRICOS Provider No. 00103D Page 2 of 6


Let's try another example – put the following into a file called SolarSystem.pl and try out the queries
listed at the bottom of the code:

% ----- Facts -----

% Things that orbit the sun


orbits(mercury, sun).
orbits(venus, sun).
orbits(earth, sun).
orbits(mars, sun).

% Things that orbit the earth


orbits(moon, earth).

% Things that orbit mars


orbits(phobos, mars).
orbits(deimos, mars).

% ----- Propositions -----

% P is a planet if P orbits the sun


planet(P) :- orbits(P, sun).

% S is a satellite if S orbits P, and P is a planet


satellite(S) :- orbits(S, P), planet(P).

% ----- Queries -----


% Try:
% trace.
% satellite(phobos).
%
% Also try:
% satellite(S).

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!

It's a joke – it's funny, roll with it: https://www.youtube.com/watch?v=X2xlQaimsGg

CRICOS Provider No. 00103D Page 3 of 6


We can replicate this ground-breaking scientific breakthrough for ourselves with the following Prolog
code – place it into a file called WitchTest.pl or such and give it a shot!

% ----- Propositions -----

% X is a witch if X burns and X is female


witch(X) :- burns(X), female(X).

% X burns if X is wooden
burns(X) :- wooden(X).

% X is wooden if X floats
wooden(X) :- floats(X).

% X floats if X is the same weight as a duck


floats(X) :- sameweight(duck, X).

% ----- Facts -----

% girl is a female
female(girl).

% girl is the same weight as a duck


sameweight(duck,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!

CRICOS Provider No. 00103D Page 4 of 6


Review Questions

1. In logic programming, what are the two parts of a compound term? Give at least one example
[Q2-Mod]

2. In logic programming, what is the general form of a proposition in clausal form? [Q4]

3. In logic programming, what are antecedents and consequents? Give an example of each.
[Q5-Mod]

4. In logic programming, we can often replace two propositions with a single proposition via the
process of resolution. Explain the general process of resolution and give an example of it in
action. [Q6-Mod]

5. What does it mean for a language to be nonprocedural? [Q9]

6. Explain the closed-world assumption used by Prolog. Why is this a limitation? [Q20]

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.

CRICOS Provider No. 00103D Page 5 of 6


2. Write the following English conditional statements as Prolog headed Horn clauses:
a. If Fred is the father of Mike, then Fred is an ancestor of Mike.
b. If Mike is the father of Joe and Mike is the father of Mary, then Mary is the sister of Joe.
c. If Mike is the brother of Fred and Fred is the father of Mary, then Mike is the uncle of Mary.

CRICOS Provider No. 00103D Page 6 of 6

Das könnte Ihnen auch gefallen