0% found this document useful (0 votes)
43 views

The Design and Implementation of A PROLOG Interpreter

This document describes the design and implementation of a PROLOG interpreter written in PASCAL. It explains how the interpreter handles PROLOG's key features of pattern matching and backtracking. Data structures like the datatree and variable list are used to support these features. The interpreter is intended to help teach PROLOG and compiler design.

Uploaded by

Vyhx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

The Design and Implementation of A PROLOG Interpreter

This document describes the design and implementation of a PROLOG interpreter written in PASCAL. It explains how the interpreter handles PROLOG's key features of pattern matching and backtracking. Data structures like the datatree and variable list are used to support these features. The interpreter is intended to help teach PROLOG and compiler design.

Uploaded by

Vyhx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 140

Lehigh University

Lehigh Preserve

Theses and Dissertations

1985

The design and implementation of a PROLOG interpreter /


Andrew Davison
Lehigh University

Follow this and additional works at: https://preserve.lehigh.edu/etd

Part of the Electrical and Computer Engineering Commons

Recommended Citation
Davison, Andrew, "The design and implementation of a PROLOG interpreter /" (1985). Theses and
Dissertations. 4505.
https://preserve.lehigh.edu/etd/4505

This Thesis is brought to you for free and open access by Lehigh Preserve. It has been accepted for inclusion in
Theses and Dissertations by an authorized administrator of Lehigh Preserve. For more information, please contact
preserve@lehigh.edu.
THE DESIGN ANJ5 IMPLEMENTATION OF

A PROLOG JNTERPRETER.

by

Andrew Davison.

A Thesis

Presented to the Graduate Committee

of Lehigh University

in Candidacy for the Degree of

Master of Science

in the

Department of Computer Science

· and Electrical Engineering

Lehigh University

1985
This thesis is accepted and approved in partial fulfillment of

the requirement for the degree of Master of Science.

?l?~ ~/~~
ate)

~ ~ 1 / lf\'Uw.c,,_
Head of the\-iivision of
Computer Science

·tw ~-°I~
Chairperson of t h ~
·~-

ii
·Table of Contents

1
Abstract. 2
1 • Introduction. 4
2. Syntax. 4
2 .1 Restrictions. 6
2.2 Discussion of EBNF, 12
3. Data Structures. 12
3.1 The datatree. 13
3.2 The database. 13
3 .3 The variable list. 15
3 .4 Examples of the main structures. 20
3 ,5 The printed variable name list. ·21
4. Backtracking. 21
4 .1 An Example. 38
4. 2 Location of code. 42
5. Pattern Matching. 48
5 .1 The different kinds of q and db nodes. 53
5 .2 Location of Code.
59
6. Built in Predicates. 61
6.1 Call. 68
6.2 Not & '; '(disjunction). 69
6 .3 Clause. 70
6 .4 Consult. 72
6 .5 Cut (I). 73
6 .6 Read. 73
6 .7 Name. 74
6.8 Functor. 75
6 ,9 Arg. 75
6.10 Univ(: •• ). 77
6.11 Is. 77
6 .12 Strict (==) • 78
7. Diagnostics. 78
7 .1 Diagnostics for PROLOG. 82
7 .2 Interpreter diagnostics. 82
7 .2 .1 Boolean diagnostics. 83
7 .2 .2 Dat~!-. .:StrucJure diagnostics.
. - -
-- _... . . ----· - ···- -..... ·.. -
,
85 .. -·· --···· ........ · - -- ...........

8. DEC20 dependencies. 85
8 .1 Timing questions. 85
8 .2 Input. 87
9. Comparisons with UNH PROLOG.
88
9 .1 Timing comparisons. 89
9 .2 Ease of use.

iii
93·
10. Improvements. 93
10 .1 Rem aini ng pred icat es. 93
10.2 Language Modules, 95
10 .3 Ext ra pre dica tes. 91
10.4 PROLOG defi ned pre dic ates . 99
10. 5 Tree s and stac ks. 112
11. Con clus ions . 115
Ref eren ces. 117
I. Using PROLOG. 117
I.1 Get ting star ted . 119
I.2 Common mis take s. 122
II. Error mes sage s. 122
II.1 Syn tax erro r mes sage s. 123
sage s.
II.2 Bui lt in pred icat e err or mes 126
II. 3 Fai lure mes sage s.
127
III. Ext end ed BNF, 130
IV. Bui lt in pred icat e words and
sym bols .
131
IV .1 Pre dica te words. 132
IV.2 Pre dica te symbols. 132
IV .3 Oth er sym bols . 133
V. File s. 135
Vita .

iv
Abstract.

The design and implementation of a PROLOG interpreter, written

in PASCAL, is explained. The main design issues of PROLOG are how to

deal with its pattern matching and backtracking features. Solutions

are given to these problems. In particular, the pattern matching of

this interpreter is more powerful than many other PROLC)G systems.

Algorithms are also included which offer a more flexible way of

implementing PROLOG I s search strategies.

It is hoped that this interpreter will help in the teaching of

PROLOG and also compiler design.


1. Intro duct ion.

The purpo se of this repo rt is two fold. Firs tly,


an expl anati on
will be given of how the PROLOG inter pret er
was :implemented. The
program was writt en solel y in PASCAL. It shall
be assumed that the
read er is alrea dy fami liar with PASCAL
and also with basi c
comp iler/ inter prete r desig n. If not then the
book s by Welsh [ 1],
Welsh & McKeag [2] and Gries [3] are recom
mended. Only PASCAL
featu res which are comp licate d or depe nden t on
the host machine ( in
this case the DEC20 ) will be desc ribed . For
more infor mati on on the
DEC20 and its PASCAL, the user manu als for these can be
read [4] [5], Simi larly only desig n featu res uniqu e to the
inter prete r will be desc ribed . These cons idera
tions have dicta ted
the follo wing chap ters which expla in how such
thing s as patte rn
matc hing and back track ing of PROLOG are dealt
with . It is also
assum ed that the reade r is fami liar with PROLO
G, As expla ined in the
follo wing chap ters the versi on of PROLOO imple
ment ed is a larg e
subs et of the one expla ined in the firs t 11
chap ters of the book by
Cloc ksin & Mell ish [6]. This book will be
refer red to using the
abbr eviat ion C&M.

The secon d aim of this repo rt is to expla in


how to use this
PROLOG inter prete r and to expla in what the
user shou ld expe ct. If
th~ reade r is only inter ested in this, then
chap ters 3,4 and 5 can
be igno red. Inste ad, it is recommended that
the read er shoul d firs t

2
read C&M and then appendix I and appendix II.

3
2. Syntax.

The syntax of this PROLOO is laid out in extende d Backus- Naur

form (EBNF) in the third append ix. In general , the syntax is exactly

that of the PROLOO in C&M. However, there are 4 restric tions.

2.1 Restric tions.


The first is that infix and postfix form is not allowed . This

will only become a nuisanc e when arithme tic is being carried out.

Thus

X is 10 + Y - Z

must be written as

is( X, +(10, -(Y,Z)) )

The second restric tion is in the notatio n for lists. C&M allows

[ x,y,z]

and also
.(x, .(y, .(z,[]) ))

These two thing are equival ent. The 1 ,' is also used as a period to

termina te a clause. So for ease of implem entation the


1• 1 functor is

..... not·--all owed. ··Thus

.( x, .(y,[]) )

will give a syntax .error. The only list form allowed is

4
[x,y]

This is perfectly good in nearly all cases. Unfortunatel y in C&M,

the predicates I functor', •arg' and'=•·' ('univ') can use this

first notation. These 3 predicates can manipulate lists by

converting
[x,y]

to • (X 1 • ( Y1 [ ] ) )

In this interpreter if such a thing is tried then an error will be

output.

Another restriction is the use of built in predicate names as

ordinary names, Lists of these built in predicates are given in the

fourth appendix. For example, asserta(X) is an one argument built in

predicate. When the interpreter accepts the word asserta it will

label it as a predicate operator. In this interpreter if a predicate

name or symbol is going to appear as an ordinary name or symbol, it

is necessary to put it into quotes. Thus

?- asserta(fac t(1)).

will call the built in predicate asserta, But

?- 1 asserta 1 (12).

will call the ordinary fact, or rule, called asserta. All bu:il t in

predicates, when used, must use their right number of arguments.

Thus

5
'l- asserta(fact (2)). is correct

'l- asserta( fact ( 3), fact ( 4) ) • is wrong.


but

A small ·restriction is that the grammar rule notation of

chapter 9 of C&M is not implemented. In most PROLOGs, it is a

package built on top of the basic PROLOG, That is what could be done

here, if required.

2.2 Discussion of EBHF.

The notation includes

'X', (X)' {X}' I and/\

1 X1 is used to indicate that X


is a terminal symbol.
(. ,) is used to group strings together.

{ • .} is used to mean O or more strings,

means 'or'.
/\ means the empty string.

Thus 1
( fact I question I rule ) .'

fact 1• 1
means
1
or question .'

or rule '.'

There are some constraints on the syntax which can not be coded in

EBNF. One of these is the fact that comments can appear almost

6
anywhere. Thus you can have

hello(mary). /I comment I/

or /* comment t/ hello(mary).

or hello /I comment*/ (mary).

Comments are consumed in the lexical scanning done by procedure

'nextsymbol I and never reach the syntax analysis sections. Comments

and spaces are ignored in between words, numbers and symbols but

must not appear in the middle of these things. Thus

hel /*comment*/ lo(mary).

or hel lo(mary).

will give syntax errors. A symbol may consist of more than 1

character. For example, the question operator is made up of 2

characters.

'?-'
The set of predicate symbols is defined in procedure

1 initsetofsymbol s 1 , The set for predicate words is also defined

there with the 'predica teop' set being the union of these.

The size of identifiers is limited to 9 characters. Thus

artificial (intelligence)

will become

artificia(intell ige)

in the database. This will become apparent when such a fact is

7
tru nc at ed
rin gs an d wo rds in qu ot es ar e al so
li st ed . St
"i nt el lig e"
"i nt el lig en ce " --> te ll ig e'
ap.d 'in te lli ge nc er --> 'in
h' in th e
be al te re d by ch an gi ng 'a lf al en gt
can
The le ng th li m it
ta ke n te> change
of th e pr og ra m . Care must al so be
cO ns ta nt se ct io n
co ns ta nt st ri ng s.
th e ·1 en gt h of any

e. g sp ac es = '
.
th e new al fa le ng th
must be al te re d to

in t' which is
ite d to va lu es be tw ee n O and 'm ax
In te ge rs ar e lim
st di gm ax ' and
'm ax in t' mu st al so be done to 'la
16383. Changes to
n.
th e co ns ta nt se ct io
al l of which ar e in
'ni ax 10 '

la rg e amount of
is qu ite a
at th e EBNF th er e
Looking
ar e ru le s fo r
re pe tit io n. Th er e
and ar ga tru ct ur e
st ru ct ur e ar gq ue st io n
qu es tio n ar gf ac t
fa ct ar gr ul e
ru le
at th e sy nt ax
ar gu m en t. Th e re as on fo r th is is th
1 ar g' is sh or t fo r
th e
se s is sl ig ht ly more ge ne ra l th an
of cl au
fo r th e ar gu m en ts cl au se
ar gu m en t ma y be a va ria bl e w hi le a
se s. An
sy nt ax fo r th e cl au
st an ce
may not be . For in
h~),Jo (maryJ~.

and ?- he llo (m ar y) .

ar e co rr ec t

bu t
8
X(mary).

and ?- X(mary),

are incorrect.

Arguments , however, can be of this form


e.g
test ( (X (mary))).

and ?- test ( (?- X(mary))) •

are correct.

Notice that such an argument structure must be in parentheses to

deal with the scope of the symbols. For instance

?- test( ?-X,Y ).

will cause an error because it is unclear if the Y is the 2nd

argument of test or the 2nd goal in the question

?- X,Y

Simple variables can be written with or without parentheses.

e.g ?- test (X).

or ?- test( (X)).

are both correct. Parentheses are not ignored when pattern matching

takes place. So a fact

test ((a))

would cause

?- test (X)

to instantiate X to the value (a). In some PROLOGs, variables are

allowed to be the functors of facts, rules and questions.

9
Un for tun ate ly thi s means mo
re che cki ng at pat ter n ma tch
ing tim e to
dea l wit h all pos sib le ma
tch es. See cha pte r 5 for a
more det ail ed
dis cus sio n of thi s. In thi
s PROLOO if a que stio n has
a var iab le
.fu nct or the n it can be sat
isf ied by bei ng exe cut ed in
the 'ca ll'
pre dic ate .

Thus
?- X(m ary ).
can be coded as

?- cal l( X(mary) ).
X mu st hav e a val ue so a
qu est ion lik e the one above
would sti ll
t'a il, but fai l wit h a sem ant
ic err or not a syn tat ica l one
.

Not inc lud ed in the EBNF


are the sym bol s which den ote
end of
ses sio n and end of fil e. The
end of ses sio n symbol is 1$ 1
• It cau ses
a jump from I nextsymbol I to
the end of the int erp ret er.
The end of
fil e symbol '"'r must be inc
lud ed at the end of eve ry fil
e, It cau ses
the inp ut stre am to sw itch
bac k to the ter mi nal keyboa
rd aft er a
fil e has fin ish ed. Th is hap
pen s in pro ced ure I nex tlin e'.

The pro ced ure s in the int


erp ret er to dea l wit h the
EBNF are
named usi ng the EBNF names,
or min or var iat ion s. Thus
lin e --- > pro ced ure nex tlin
e
ter m- --> pro ced ure term
and str uc tur e ---> pro ced ure str uct ure

For eas e of programming som


e EBNF con str uct s hav e been
merged. Thus

10
fact and rule ---> procedure factorrule

argfact and argrule --,-> procedure argfactorrule

11
3. Data Structures.

There are 3 main data structures in the interpreter

the datatree (see the type called 1 dtree 1 )


the database ('database')
and the variable list ('varpter').

The ·2 minor structures are

the printed variable name list ( 'namepter')

and the list of predicates to be looked


at during execution ( 1 spypter 1 ) .

The first 4 will be discussed here, the last in chapter ·7 o.n

diagnostics.

3 .1 The data tree.

This is the structure that contains the parse of the input line

of PROLOG which can be a question, rule or fact. As expected it is a

tree structure. A tree can contain 3 different sorts of data in a

node, It can contain information on variables, constants or

identifiers. Identifiers are the largest class including atoms,

bull t in predicate words (like 1 asserta 1 and 'listing') and also

symbols such as '= ', '?-' and '= .. '.

A variable node consists of a variable name and a pointer


-~~.;, - .·..... ;-
-,. ~
..
'

('varval 1 ) to the variable list where its value is.

A constant oode contains an integer value.

12
An identi fier node consis ts of its name ( 'idnam e'), and the

number of argume nts associ ated with it ('noof args') .

e. g hello ( 10) has 1 argume nt


~e ~so
hello( man(X ), but(t) ) has 2 argume nts

it was
The node also has a number ( 1 numofmatch ) indica ting when
1

is in
matche d agains t anothe r tree. This is only used when the node
tree
a questi on tree and the node is matche d agains t a fact or rule

in. the databa se. There is also a boolea n called


I cutfla g' which is

used by the predic ate '1 1 ( 1 cut 1 ) . This flag can stop anothe r match

being tried. 1 Dbrule used 1 is a pointe r to the databa se fact or rule

tree which this node of the questi on tree is matche d agains t.

3 .2 The databa se.


This is a doubly linked list which contai ns the fact or rule
is
trees. Each databa se node also contai ns the name of the tree that

hangin g from it. This speeds up the search for a tree. Trees are

put into the databa se in alphab etical order.

3,3 The variab le list.


This is anothe r doubly linked list which contain s values for
not
variab les or possib ly pointe rs to other variab les. A value may

only be a number or identi fier but.... can be an entire tree. Infact , a

value is stored in a 1 dtree 1 that is pointe d to by I stval' . The

variab le's name is also stored and also a flag ('ownc opy') to

13
if 1 stval I is pointi ng to its own value or anoth er
indica te

varia ble I s value. This flag is used at garbag e collec tion time,

d that
'Unin stval' contai ns an intege r. If a variab le is ,to be printe

hasn 1 t a value it will print an uniqu e number inste ad.

e.g
?- test(X ).

may produc e X = _21

This number is what is stored in


1 uninst val 1 • Final ly there are 2

other integ er varia bles, 'creatn um and


1 1 mtnum 1 , 1 Creatnum 1 gets a

ted to
value when a varia ble in a tree has a variab le locat ion alloca
a value or
it. 'Mtnum 1 gets a value when this variab le locat ion gets

gets. a pointe r to anoth er variab le, They are relate d by the fact

that

creatnum ~ mtnum
matchnum'
1
and are used for garbag e coll"l ction. A globa l variab le

~ves them their integ er values .


1 Matchnum 1 incre ases and decre ases
r 4), When
with the size of the curren t quest ion tree (see chapte
a varia ble
'matchnum 1 becomes less than or equal to a 'mtnum ' in

locat ion then its 1 stval 1 can be delete d, When 'matchnum' becomes
be delete d
less than 'creatn um' for that locati on, the locat ion can
chapt er 4
and its space in the variab le list freed, The examp le in

will make this cl ear.

14
3.4 Examples of the main structures.
The data structures created after two ruies and 1 question have

been typed will be shown. The question tree and variable list will

be shown just before question satisfaction begins. Assume that

append([],X,X).
append ( [A IB], C, [A ID]) :- append(B, C,D).
and
?- append([a],[b,c],Q).

are typed. The first 2 rules are put into the database which will

now have 2 trees hanging from it.

C1ppe.1"1d
I
l-J-)-)
I

I ><I
.)(

The words and symbols represent the things stored in the 1 dtree'

records. Not all the values in a node are shown. For instance, each

of the 'append' oodes will have 3 as their 1 noofarg' values.

15
If 2 rul es hav e the same fun
cto r, the ir ord er in the dat aba
se
is the ord er in which the y wer
e typ ed.

No tice how lis ts are sto red .


The empty lis t [] becomes a
'. 1
nod e. A lis t in head and tai l
not atio n [A IB] becomes

·-1 3
I
A

The que stio n when typ ed wil l


pro duc e a que stio n tre e •.

~ l h , root
?-
I
0.'ffLhJ
I
C.-----J----,.--)
I I I
i~i-i-· i-i-r-· ~
Q..~c. de .F

Not ice how the lis t [a, b, c] is


sto red as

·- ·-
I ·-
I ·
_... _~·-··- (.l. b C. ... ........ -_
~
--··"··

The onl y var iab le, Q is sto red


in the var iab le lis t.

16
Q
00

The notation 00 indicates that the •creatnum' = O and the •mtnum• =

O. Not shown is a pointer from the Q in the question tree to the Q

in the variable list.

In future the 1 qhead', 1 vhead 1 and 1 dbhead 1 will not be

included in a drawing, Where necessary a 1 db' or 1q 1 will be

written next to the structure.

e,g
sent( (?- N2(N1))) :- sent(F).

becomes
~Tt- ----
C.-) :-

1 I
c.-) Sen+
I I
?r (,-)
I I
N2
F
I
C..-)
I
NI

e.g
?- is(X, ~dd(Y, minus(Z,S ))),
becomes ·

17
?-
I
\~
I
(.-,-)
II I II
X Y
oo
Z
oo
S

I I 00 00

X ~,~,-)

C 1'111m.1S
I I
1 f-,-)
2 S
e,g
ad( [a,b,[d,cle] If]),
becomes

~d
I
C,-)

I
,-··-·-F
I I I
~ b •-,-e.
I I
d C,

e.g
?- ad( [ [thelY]IZ] ).
becomes

18
7-
1
11d
I
ITz]
y
C-) -~ 00 00
I·-2.
I
I I ·
·-·-
tha. '/.

parse trees but this


Ther e is a certa in amount of redundancy in the
quite simp le.
means the program code to gene rate the tree is

db', 1 rein itdb ',


Proc edure s which crea te the datab ase are 'init
ode'. Proce dures
'find dbpl ace', 'inse rtdb ', 'addn ode' and 'find freen
ion• , 'addn ode' and
which creat e the ques tion tree are 'initq uest
are
'find freen ode'. The proce dures which creat e the varia ble list

'find varp lace '. Proce dures which delet e the


'init ques tion ' and
are 'rem ovetr ee',
ques tion tree and occa siona lly the data base trees
1 delet edb 1 and 'dele tequ estio n'.

secti ons are 3


Rela ted to the 3 struc tures of the previ ous
will- p~in t out the
proce dures 'ptre e', 'pba se' and pvar pter' which
1

value s of these data struc tures . They are in the inter prete r as

and are curre ntly not used. During


diag nosti c proce dures

19
if valu es were bein g assi gne d
imp lem enta tion they were used to see
can also be used .
corr ectl y. On the DEC20, pasd dt [5]

3.5 The prin ted vari able name list .


able s which have had thei r
This list is used to stor e the vari
n. Thus
valu es prin ted at the end of a que stio

?- test (X,X ,Y).


. With out the name list the X
will prin t the X and Y valu es only once
ctur e is used in the group of
valu e would be prin ted twic e. This stru
1 prin tsuc cess I which prin ts out
proc edu res just befo re proc edur e

valu es if a que stio n has succ eede d.

20
4. Backtrac king.

The reader should be familiar with the idea of backtrac king in

PROLOG. Section 2 .6 of C&M runs through a small example of this.

The aim of this chapter is to show how backtrac king is achieved in

this interpre ter. One example will be given, using the notation

introduc ed in the previous chapter. Then the procedur es within the

interpre ter which do these things will be named. For the moment only

a brief outline of pattern matching will be given. A more complete

discussio n will be appear in chapter 5. In chapter 10, backtrac king

and the more general subject of PROLOG I s search strategi es are

returned to. Algori thins are presente d which build on the ideas of

this chapter and offer more flexible solution s than those given

here.

4 .1 An Example.
The followin g is typed

append([ ], X,X).
append([ AIB],C,[ AID]) :- append(B ,C,D).
and
?- append(X ,Y,[a,b,c ]).

If this goal is resatisf ied until it fails,

it will have given 4 answers.

X= [] Y= [a,b,c]
X= [a] Y= [b, c]
X= [a,b] Y= [c]
X= [a,b,c] Y= []

In the interpre ter the two rules will be stored as

21

. .. . - . . .... . . .! . "l. • • • •
• :. •• ~ '." ' .; ' ' • ', ~ • 'i':. • ...

°'~ vo..nd
I
C.-1-,-)
db

I• '}(I X'I
Ru\Q. I,

and
~ t:'und·o.-- o+ !tie rul~

C\frnJ----------
( . - J - 1-) :

I I I I
i-S i-p C o.fren~
A A I
C.-1->-)

I I I
B c. D

The question will be stored as

22

. : . . ~ ' •. . :. -: .
7~
./ Fu11c~or of: f-he
I IC. ~IAll,hoY l

o.rr:2.VI&\
I
c-,-)-)
l ,_.
I YI o-,-
X I b
Cl
l

The var list will be

[[ ] X y
00 oo

st rule 1.
When execu tion begins the questi on will try to match again
r of the
Match ing starts from the functo r of the rule and the functo

questi on. The globa l variab le 1 matchnum 1 , which has an initia l value
are added to
of O, is increm ented to 1. The variab les of the rule __ ___. ---·- ···----. -· ···--- ... ~ ····--#'•,

the var list. Thus the var list becomes

23
X
I I I
y X
00 00 II

The 2nd X being from rule 1 and having nothing to do with the 1st X

from the question, Since there will now be some confusion over which

variable is being referred to a variable may be subscripted with its


1 1
'creatnum and mtnum 1 e.g X(OO) and X(11). During pattern matching
in the question in the rule
X matches I • I

Making the var


' .
list

I· I
>< y
~ X
I
0/ 00 11

y matches X,

Making the var list

24
X '/ X
II
X oo

i-·-·-· Y)Jarch es X
q b C.

Makin g the var list

. ;-j-j-'
G b C.

X '/ Jr-. X
01 o, II

X
·---· _......... '. ·-·-.

When X (in the ques tion) match es with the ' .


' (in the rule) , X(OO)

25
gets a copy of the list. When Y matches with X (in the rule), X's

pointer is moved from the X( 11) in the var list to the Y in the var

list. When the


·-·-·- ·
I
0. I, "

matches with X (in the rule) which is X( 11) it is actually matching

with Y. So Y gets a copy of the list. The functor append in the

question now has its I numofmatch' set to 1. Since there is no right

hand branch leaving this functor or leaving the matched rule, the

question is finished. Thus the variables from the question are

printed.

X = X(01) = • = []

y = ·-·-·-· = [a·,b,c]
I
a b C

The question could now be deleted (automatic ally) and another

question typed in and that satisfied. Instead the user types a ';

<er>' and the question backtracks for another answer. What now

happens is that the question tree is searched from the root until it

gets to a functor node whose 'numofmatc h' equals the 1 matchnum 1 • In

this case it is the very first functor node. Its 'numofmatc h' is set

to O and 1 matchnum 1 is decremente d by 1, back to O. The right hand

side of the functor is removed which in this case is empty already.

Then the var list is cleaned up. All var list elements which have a

26

:.. . . . "';. ,, .· ,~ .. ~· --~~·.:-- : .......


•mtnum• greater than •matchnum• have their values removed. Then
their •mtnum• s are set to the value of their I creatnum 1 s. Thus with
•matchnum' equal to O

I · ln-rll I
X 'I >(

01 Cl II

becomes

X y
~ I
X
00 00 II

Then those var elements whose 1 mtnum 1 s are still greater than
1
matchnum• are deleted from the list. Thus with 1matchnum• = 0

I I ~ I
X 'I X
II
oo 00

becomes

rn X
oo
'/
oO

27
So the question tree and its var list are back to the state they

were in initially. There is 1 difference. When the first match took

place the functor •append' in the question tree set its pointer,

'dbruleused', to point at the first 'append' rule in the database.

Now when the question is resatisfied another rule is searched for in

the datatbase starting from just after this rule. In other words,

the ·first 'append' rule will not be matched with again. Now the 2nd

'append' rule :l,s matched against. This rule is

qppend-------- clb
!
c.--1-1-)
.
.• -

'
•-e, (
I I
·,-D I
qf penJ
I ·1
A A c.-J-1-)
.I I I
B (, D

'Ma.tchnum' is incremented back to 1. A, B, C and D are added to the

var list.

28

·- . .'. \·- ....... ,•. . '\"-,.· .. ·· . . ..


I I I I I I I
)( '/ A 8 c.. D
oc 00 /I II II II

This rule is matched against the question tree

7-
1
~r piu,d
r
C-J-1-) 9
I I I
X y ·•-·-·-·
I I
~ b C.

So

in the question in the rule

X matches ·-B
I
A

The var list becomes

29
lrffl X
01 00
y A
II
B
II
I Ip I
C
h II

The_n

y matches C

So ~ /

r!J X
01 Y,y•
00 . It It It
IID

C. c po,ri~ey n,ove.d
1-o 'I

''-·-·-· matches ·-1)


I I I I
0. b C. A
- ··-- " .. --···· '-~ .-
--····-- - - ·- ..... , __., ... - ,_..,, __....
_.,. - .. ,_, ___ . -
~----~~ ·---: ___ .,,._,. ·-· - ····---··"· .. ······

30
- • •-0,0~~ • •' ,,r ~ ' ' o • • • U • '- .. • r

and so

a matches A

mat.ches D

b C.

Giving

C D
II II

Now, because everything ioatched, a copy of the right hand side of the

database rule is added to the right hand side of the question tree,

to give

31

\. .. . " - .. • ';. . ~:of_~,..,.... . . • . • .- . . • • , .. : •


?-

c.- ,-,- ) .-
I I I I ~ 2nd A.Incl-a y-
·-· -·- · "'ffend
I
c.-, -,-)
I I I
(, C. D

thei r valu es in the var


The B, C and D of this copy stil l poin t at
'num ofma tch' = 1 and also
list . The firs t func tor, 'app end' , gets a
'appe nd I rule and goal
its 'dbr ul euse d I is set to poin t to the 2nd
emented to 2, Now the new
satis fact ion cont inue s. Matchnum is inCI'
1 1

sfie d. The next func tor,


righ t hand side of the ques tion tree is sati
It is just star ting its
also call ed 'app end' , is now matched.
g af the datab ase and so
matc hing so it will star t from the begi nnin
matc h with
match agai nst the firs t 'appe nd rule . It will
I

.. ; ......... -- -····-·. '..I#


., · - - - - ··---·-- ~· - - - - ~ - - - -

32
a.y~inJ
db
I
C-,-,-)
I I I
X X

The question's 'dbruleused' is set to point at this db rule, Now

B matches I , I

C matches X
D matches X
G~vin~

;-;-·
b (,

(. D X
,, II 22

C. X ;,< po, nl-ev- vnove.d


f-o 'j

1
When B matched with , ', B got a copy of the ',' and B, s mtnum is

set to 2, When C matched with X(22), the X pointer was moved to

point at C which meant pointing at Y. When D matched with X(22),

X(22) got a copy of' D 1 s value, which is

33
b C

'mtnum'
2) is ac tua lly Y the n Y got the va lue wi th its
But sin ce X(2
sid e of the 1st
the re is no rig ht hand
bei ng set to 2. Sin ce
ish es.
e to cop y ont o the qu est ion tre e, the qu est ion fin
'ap pen d rul
I

are now pri nte d,


The va lue s of X(0 1) and Y
=
= (a]
X=· ·- B
I l\
A

Y: = [b, c]

res ati sfi ed .


r the n typ es '; <e r>' so the qu est ion wi ll be
The use
the fun cto r
est ion tre e is sea rch ed from the roo t ag ain un til
The qu
1 matchnum 1 (which is 2)
is rea ch ed . The
wh ose 1 numofmatch' equ als
by 1, the rig ht
mo fm atc h' is se t to O, •matchnum' decremented
'nu
r lis t is res et.
ssi ble ) and the n the va
han d sid e removed (if po

Gi vin g

34
'-1- ,
I I
b C.
X 't A C D
01 co II It II II

No tice how X and D sti ll hav


e the ir val ues but Y and B do
not . Th is
was bec aus e the ir 'mt num 's wer
e gre ate r tha n 'matchnum' (
which is
1). The 2nd fun cto r of the
que stio n tre e can now sea rch
for ano the r
rul e in the dat aba se. While doi
ng so 1matchnum' is inc rem ent
ed to 2.
The 2nd rul e wi ll be fou nd
and so car ry on to get ano the
r ans wer .
Ins tea d let us assume tha t a
2nd rul e is not found. In thi
s case the
'ma tch ' fla g is set to fal se.
'Matchnum' is decremented bac
k to 1.
Any new var iab les and val
ues cre ate d bef ore the fai lur
e are del ete d.
Thi s is bec aus e a fai lur e to
match may not occ ur imm edi ate
ly,
'/_

I
°'ffe.hJ
I
C.-1-1-)
I I I
X )(

35
will almost match

°-ff 0,/\d
I db
C.-J ->-)

Y
I
I It-, •-o
I bI
0.

value on the var


X will get a value and Y will get a space and a
the datab ase is
list befor e failu re occur s. When failu re occur s
of the datab ase
searc hed again from the rule that faile d to the end

or upto anoth er matching rule. If no rule is found then the

ques tion tree will be

?- _/ l1urn0Fn,11+th
I /£ =I C D
II 11 ri II
°'?Ye.i,,1 01 00

I
C-)-,-)

X
I yI Ij_j_,_,
0. \, (. I
a.yyei-d
/·1c.-,
1111~0/:rnal-cJ.
<=
2
I I
l3 c·

.... ,.,,. .,..,

36
as fals e. The top leve l
At this poin t the 'mat ch' flag is retu rned
is sear ch the tree from the
gets this flag back . What it then does
whose I numofmatch' equa ls 1
root agai n but look ing for the func tor
find the firs t func tor of
(the curr ent valu e of matc hnum '). It will
1

will be set to O, 1 matchnum


1
1 Numofmatch'
the ques tion tree .
of the func tor dele ted, the
decr eme nted to O, the righ t hand side
try to be resa tisf ied. At
var list clea ned up and then the goal will
wil l be
this poin t the que stio n tree and var list

?-
t
a.rr~J DJ X y
I
C.. -J-i '-)
oo 00

I I I
X 'j ·-·
I I
-·-·
0.. ~ C.

func tor is alre ady poin ting


Unf ortu nate ly, the 'dbr uleu sed' of this
a 3rd rule will fail . With
at the last 'app end' rule . The sear ch for
this ques tion tree can not
·--··--·····
0 -this --f·a ilure and- 'match1;mm' equa l to O, ··-·····-..

be sati sfie d in any way. Thus the


inte rpre ter will retu rn an answer

37
of I no 1 , delete the questio n tree and the var .list and prompt for

another line of PROLOG.

4.2 Locatio n of code.

Inside the interpr eter the executi on of a tree begins with a

call to procedu re •answer •. The questio n tree and var list have

already been built using procedu res 'initqu estion' ,


1 findfre enode',

1 addnode 1 and 1 findvar place 1 • 'Answer ' calls 1 dogoal 1 • If 1 dogoal 1

succeed s then the variabl es are printed out and then the questio n

may be reanswe red in procedu re 1 reanswe r 1 • Otherwi se failure is

printed . Procedu re •reansw er• is like procedu re •answer ' but has a

call to •redo goal 1 • Also it loops if another try is require d.

1 Dogoal I searchs the tree for the first time. It does this by

calling •satisf y'. •Redogoal 1 does the same thing but because it is

resatis fying and thus searchi ng for a


1 numofmatch 1 that is alre~dy

in a functor node, 1 satisfy ' is called with 1 nodefou nd' set to

false. The 1 numofmatch 1 being looked for is equal to


1 matchnum 1 • If

'satisf y' fails, 1 matchnum 1 will have been decreme nted and so

1 redo goal' loops and calls I satisfy ' again. Only when 'matchnum I is

O does 1 redogoa l 1 stop looping . At this point failure is final.

So it is clear that backtra cking is handled by procedu re

'satisfy •. When 1 nodefou nd' is true 'satisfy ' is searchin g a

questio n tree for the first time. When 1 nodefou nd' is false it is

searchi ng for the last match to a functor so the match can be undone

38
and ano the r one atte mp ted .

to
'sa tis fy' keeps cal lin g its elf
When 'no def oun d' is fal se,
che s the
dow n to the des ired bra nch of the tre e. When it rea
get
1 to tru e, the n doe s all the
cle ani ng
rig ht node, it set s nod efo und
1

elf aga in.


var lis t and the n cal ls its
up of the que stio n tre e and
re pat ter n
the 2nd hal f of its body whe
Now 'sa tis fy' wil l exe cut e
I ide val ' or
ll eith er cal l pro ced ure s
ma tch ing tak es pla ce. It wi
efi ned one
er the fun cto r is an use r-d
'pr ede val ' dep end ing on wh eth

or a pre dic ate name.

iou s
'sa tis fy' are to dea l wit h var
Oth er par ts of the cod e of
ma, rul ~op ,
r is a sym bol suc h as a com
.sp eci al cas es. If the fun cto
nil poi nte r
ore d. A lef t par ent hes is or
or que stio nop it must be ign
bei ng use d
e sin ce a pre ord er sea rch is
sto ps the sea rch down a tre
l be loo ked
the lef t tre e of app end ' wil
I
and so in the example below
thi s
'ap pen d' its elf . The tes t for 1 ( ' or nil sto ps
at bef ore

sea rch .

e.g

__
- · , ,·-·
- ···--·
~·of
·-··-- --- ~ ----- ----- ----- -.

39
or
7-
1
"rfehJ
I
c.-,-,-)
I I I
• )( X

There is code in 'satisfy·' so that diagnostics predicates can print

out. These code segments are preceded by a test of the flag

'debugon'. There is also a test for the 'cutflag'. It stops pattern

matching occuring.

Procedure 'ideval' finds a rule in the database to match with

the question. It does this by calling 'findclause'. It then calls


I
procedure setdbvar' which adds the variables in the database rule

to the var list.

Pattern matching is done through procedure 1 ignoreruleop 1 (see

chapter 5). If this is successful and 'match' is true then the right

hand side of the rule is added to the question tree. If 'match' is


1
false then matchnum 1 is decremented and the var list is cleaned up.

'Findclause' is called again until success or the database is


exhausted.

Procedure 'predeval' is called when a built in predicate is

40
being executed (see chapter 6) ,

To summerise, backtracking is done by searching the question

tree from its root. When the last matched functor node is found then

the node will try to be matched against a new rule. During the

execution of a question, the question tree will grow and shrink

depending on what rules it matches against and what backtracking

takes place, This searching from the root is necessary to build up a

stack of calls of 'satisfy' so that when a question node matches,

the stack can be popped to get the parent node of the current

question. Consider
1-
I
numoF~qf.h
-..,

I ~ pap bad~
C.q \- +o h{/.(J2

I ------- /- - - - - - - - J
C..-) nex~ p,m,J-or
I I / /-o be ma~hed
l'>,011)<.e.~

nv.moFr,,q~cJ,
/'19~ I
C-)
C-) :-
"'
2
I I I
E
c porrol'
/
11urnofm~r,1,
l-)
I
=3 D

After the 'parrot I subgoal matches, the hardware stack will pop off

three 'satisfy' calls to get back to the ':-' node at which point

'satisfy' will call itself twice to get to the 'monkey' node. In

most PROLOG implementations this pushing and popping of stack


environments is coded explici tely [7],

41
5. Patt ern Matc hing.

ter is more powe rful


The patte rn matc hing deve lope d in this chap
icul ar it is poss ible to
than that found in many PROLOGs. In part

have argu ment s whic h have vari able func tors.

Cons ider

?- num (X),c all( (X(2 )) ).

with the claus es

num (val) .
val( 2).

The argum ent of 'cal l' is X(2) . X is a vari able func tor whic h,

the exec utio n of the ques tion, is insta ntia ted to the
duri ng
pred icate , then exec utes
cons tant· 1 va1 1 • 'Cal l', which is a buil t in

this goal which in its simp le form is


val(2 )

val(2 ) 1 is in the datab ase.


A 'yes ' answer is retur ned sinc e
1

rn matc hing begi ns


When back track ing and sear chin g occu rs, patte
tree and data base rule s
by comp aring the func tors of the ques tion
of each . Thus, for the
and then moves on to comparing the argum ents

follo wing ,

42
7-
1
G\ ff1U1d
llfptnJ
I
I c..-.1-1-)
c.-;-;-}
·1 I I
I I I
•-o •-o ~
x x
I
(). b
Jb

pattern matching starts by comparing the functor, 1


append 1 , of the

question tree with that of the database rule. When these match,

pattern matching is done on the arguments of each. So,

•-• matches
I
0.

,-, matches X

Q matches X

Arguments can be considerably more complicated than this. Most of

the complication arises when 2 arguments are matched which are made

up of variables.

An argument may be an identifier, integer, string, a simple

variable or an argument structure. An argument structure must be in

43
up
n, fact or rul e which can be made
par ent hes es and can be a que stio

of var iab les .

e.g ?- tes t( (?- Q,b(C)) ).

?-d ept h( (X( S) :-b (c) ) ).

or beg ( (X(s, b, c)) ) •

ch is of the form
An argu men t may also be a lis t whi
?-te st([ X,Y ]).

whi ch is sto red as


7-
1
+e.,r
I
C.-)
I
r-a -o
I I
X 'I

tree
Aft er fun cto rs have matched, both que stio n tree and data bas e
r.
of eac h compared wit h one ano the
must be des cen ded and the nod es
e
a node wit h a var iab le the n mor
When pat tern mat chin g get s to
to
The var iab le may be ins tan tiat ed
com plic ated thin gs must be don e.
the
ant iate d. Thi s is also tru e of
many dif fer ent thin gs or uni nst
e
othe r tre e. Only some of thes
cor resp ond ing node in the
se
ere nt allo wab le com bina tion s cau
com bin atio ns are allo wed and diff

dif fer ent thin gs to be done.

tion s wil l be disc uss ed is to


The way the se allo wab le com bina
44
typica l node
consid er a typic al node in the questi on tree (q) and a

the datab ase tree (db) and then let these nodes be
in
leavin g them.
variab le, ident ifier or integ er, with O, 1 or 2 branch es

all combi nation s of these possi biliti es and


After exhau sting
then e~sy to
specif ying the action s taken when they occur , it is
notati on will
show how code has been writte n for them. The follow ing

be use'd.

var = variab le
unins t = unins tantia ted variab le
When a variab le is unins tantia ted it
means that it has a variab le locati on
in the var list but no value in it

inst instan tiated variab le


= When a varia ble is instan tiated it
means that it has a value in its
variab le locati on

ident = ident ifier


int = intege r

The follow ing pointe rs will be abbre viated as

qv = q's point er to its variab le locati on


dbv = db' s point er to its variab le locati on
qvs = q 1 s point er to its value
where q is a variab le

dbvs = db's point er to its value


where db is a variab le

These distin ction s are clear er from the diagra m

45
(,

l
.Q.)(qr

I
C..-J-1-)

I I I
C a G_ -------

VO.f /1sr
Q
en.
Vana,i,le
IDCCl hc,n

The following procedure s will be used

changevars (X, Y)
Move all pointers, X, so they are pointing
to the same place that Y is.

evaluate(X ,Y,match)
Carry out pattern matching on the structures
represente d by X and Y. Return success
or failure in the boolean called 'match'.

evalleftdb (X,Y,matc h)
The structure Y is a node with a right child.
e.g

'/~.,- ...
I ',

46

.: . ~ ~ . . . ~ ',. .. .. .. . ., . . ' ... - ..


'Evalleft db' carries out pattern matching on
the structur es represen ted by X and by Y
(with its right child ignored) .
•Match' returns success or failure.

copy(X, Y, nil)
Copy the structur e with Y as its root to
the location pointed at by X. The nil is for
what the copied Y tree should have as
its new parent node - nothing.

copy1 (X, Y, nil)


Copy only the node Y to the location pointed
at by X. Y may have children
e.g

Y---,
I ·1- . . . . .......
C.-·-··
II
'

but these are not to be copied over to X.

copyleftd b(X, Y, nil)


Copy only the node Y and its left children
to the location pointed at by X. From the
example above X would get the tree

y
I
C,-·····
r

fail
Return with 'match' set to false.

47
5 .1 The diff eren t kind s of q and db node
s.
For a typi cal q node and a typi cal db
node cons ider all the
pos sibl e stru ctur es that they coul d have
hang ing from them .

Each q and db stru ctur e allow ed toge ther


will be 1 case . With in
each case , the diff eren t allo wab le type
s of q and db wil l produce
subc ases .

For each subc ase ther e will be an acti on


carr ied out whic h will
affe ct the var list . These acti ons
will be spec ified by the
abbr evia tion s give n in the last sect ion.

Case A.
Q node and db node with no chil d
bran ches
i.e
q db

A1. Let q,db both = var

Subc ase. Acti on.


q unin st db unin st chan geva rs (dbv ,qv)
q inst db unin st dbvs : = qvs
q unin st db inst qvs : = dbvs
q inst db inst eval uate (qvs ,dbv s,ma tch)

A2. Let q,db be eith er var or iden t or


both
Subc ase. Acti on.
q var db var --se e case A1--
q iden t db var- unin st copy (dbv s, q, nil)
-ins t eval uate ( q,d bvs,m atch )
q var- unin st db iden t copy (qvs ,db, nil)

48
-inst evaluate(qvs,db,match)
q ident db ident does q = db ?

A3. Let q, db be either var or int


Subcase. Action.
q int db int does q = db ?

q int db var-uninst copy(dbvs, q, nil)


-inst evaluate(q,dbvs,match)

q var-uninst db int copy(qvs,db, nil)


-inst evaluate ( qvs, db, match)

Case B.
One of the two nodes q and db do not have children.
Let q be the one without children.
All subcases given will have a symmetrical subcase where
db is the childless node.
Only 3 structures are allowed with db as the parent node.
i.e

db
and 1 I
C.-·-·
of I

B1. Let q,db both = var.


Subcase. Action.
q uninst db uninst copy(qvs,db, nil)
q uninst db inst copy(qvs,db,nil)
q inst db uninst evaluate (qv s, db, mat ch)
q inst db inst evaluate(qvs,db,match)

49

' I • ..,. ~ • ' • "• 1,"),-i.


B2. Let q,db = var,ident or both
Subcase. Action.
q var db var --see case B1--

q ident db var-uninst fail


-.inst fail

q var-uninst db ident copy(qvs,db, nil)


-inst evaluate(qvs,db,match)

q ident db ident fail

B3. Let q,db = var,int or both int


All subcases fail.

Case C.
Both q and db have 1 or 2 children.
i.e

~"
I 7.
·1"'..

1 ? 1 can be 1 ( 1 , 1 : - 1 or nothing.'(' must be on the

left, 1 : - 1 on the right.

There are only 5 cases (out of the possible 9) that can


succeed. They are

50

' • • ,. I ' • • • • ~ • ._ "'. • • ' - • • •


C1 C2 Jb C3 db
q db 4"..- '·.- \.,- 1'
I I (.
(.
C.

and
C5 9 db,
C4.
9 db I :-
1"- ,_
\,,- I'·-'
(.,
C.
I
c..

It is clear that case C4 is the same as case C3 but with q and db

switched. We shall only consider case c3.

Also case C1, C2 and C5 are similar in that for them to be

considered, the children of q and the children of db must be the

same. We shall assume such tests will be done in the code and so

only consider case C1 here.

For the cases C1 and C3 we must now consider the different

types that q and db can be.

Case C1. db
q
I I
C,
.c

51
C1 .1 Let q and db = var
Subcase. Action.
q uninst db uninst changevars(dbv,qv)
q inst db uninst dbvs : = qvs
q uninst db inst qvs : = dbvs
q inst db inst evaluate (qvs, d bv s, match)

C1 .2 Let q and db = var, ident or both


Subcase. Action.
q var db var --see case C1 .1--

q ident db var-uninst copy1 (dbvs, q, nil)


-inst evaluate (q, dbvs, match)

q var-uninst db ident copy1 (qvs,db, nil)


-inst evaluate(qvs,db,match)

q ident db ident does q = db ?

C1 .3 Let q and db = var, int or both int


All subcases fail.

Case c3.
db,
I ":-
c..

52
c3.1 Let q,db = var
Subcase. Action.
q uninst db uninst copyleftdb(q vs,db,nil)
q uninst db inst copyleftdb(q vs,db, nil)
q inst db uninst evalleftdb(q vs,db,match)
q inst db inst evalleftdb(q vs,d b,match)

C3 .2 Let q, db = var, ident or both


Subcase. Action.
q var db var --see case C3 .1--

q .Var-uninst db ident copyleftdb(q vs,db, nil)


-inst evalleftdb(q vs, db, match)

q ident db var fail


q ident db ident fail

C3 .3 Let q,db = var,int or both int


All subcases fail.

5 .2 Location of Code.

Having concluded an exhaustive analysis, the interpreter co.de

for each case is easily written.

Pattern matching starts from procedure 'ideval 1 when procedure

'ignoreruleo p' is called. 1 Ignoreruleop 1 removes the right hand side

of the database rule so that pattern matching will not fail when,

for instance

53
1_I
I
qfptnJ i
I
C:.-J-)
I I

is matched with

"freriJ----1 dh
I '1-'··.
C.-J-)
I I

Procedure I evaluate I separates the cases as have been done here.

Some extra tests are done to deal with symbols which are to be

ignored, such as 1 ?- 1 , 1( 1, ') 1 and 1 ,'. The cases become the

following procedures

procedure 1 qdbend' = case A


procedure 1 qend' = case B
procedure 1 beval 1 = case C

Within procedure 'qdbend'

procedure I qdbintend 1 = case A.3


procedure 1 qdbidend 1 = case A.2
procedure 1 qdbvarend' = case A.1

Procedure I stvcopy' is used to do the job of copy(qvs,db, nil) and

54
also cop y(d bvs , q, nil ).
1
In pro ced ure •bev al ,
cas e C1 ,C2 and C5
pro ced ure 'be val sam e• 1 =
cas e C3 and C4
pro ced ure 1 bev alq rul eop =

t •.
wit h the per iod s ins ide a lis
Ex tra code is nee ded to dea l

e.g cat ([x ,a] ) bec om es


CG\t
I
C.- .)
I _,.
,_,

Ins ide pro ced ure 'be val sam e•,

pro ced ure I qdb var end 1 - cas e Cl.1

; nil ) and als o


Pro ced ure 1 stv oop yid ' doe s the job of oop y1( qvs ,db

co PY 1 ( d bv s, q, nil ) •

To
the cas es rel ate to the cod e.
It sho uld be cle ar by now how
1 tes ts. The
var iab le, req uir es 2 if
1
tes t for an uni nst ant iat ed
node is a var iab le
fir st to check to see if the

if q"' .kin d = var s the n


nst ant iat ed
The sec ond to see if it is uni
n
if q"' .va rva l". atv al = nil the

From thi s, cle arl y

qv = q". var val

55
va l
qv s = q" . va rv al ". st
et c
ha n~ va rs '
no t be en ex pl ai ne d is pr oc ed ur e 'c
ha s
The on ly code which
ch an ge s'.
te , pr oc ed ur e un do
1
nc tio na l op po si
an d it s fu
us if
le po in te rs to a new lo ca tio n. Th
va ri ab
'C ha ng ev ar s' moves ar ' in
po in te r X ''. va rv al (c al le d 'd bv
va ri ab le
node X ha s a ri ab le
be mo ve d to po in t at a new va
it w ill
'c ha ng ev ar s') th en po in te r.
fo r al l no de s w ith an X va ri ab le
th is
'q va r'. It must do an d th e
ar s' se ar ch es th e da ta ba se tr ee
ov ea llv
Th at is why 'm th e va r li st .
he ck va rl is t I do es th e same fo r
d 'c
qu es tio n tr ee an
de al w ith sh ar ed
co ns um in g op er at io n and is to
Th is is a tim e

va ri ab le s •

e. g
append ( [] ,X, X) •

w ith ]) .
?- ap pe nd (Z ,Y ,[a ,b
same va ri ab le
an d X w il l bo th. po in t to th e
Y
Du rin g ex ec ut io n, ts
Y and X ar e sh
ar in g. When X ge
ill be em pt y.
lo ca tio n which w
w ill su cc ee d,
, b] so w il l Y and th e qu es tio n
[a
in st an ti at ed to

re tu rn in g
z = []
Y = [a ,b ]
l th e X
t po in t to th e same lo ca tio n as al
in te rs m us
A ll Y va ri ab le po le Q th en
la te r sh ar ed w ith an ot he r va ri ab
. If Y is
va ri ab le po in te rs
56
moved to the Q vari able loca tion
not only must all the Y poin ters be
time consuming par t of this
but so must all the X poin ters . The
poin ters in the tree s and also
ope rati on is find ing all the X and Y
PROLOGs, vari able s like thes e
in the var list stru ctur es. In some
have valu es [8], They are then
are set asid e from othe rs whic h do

eas ier to deal with .

I s vari able poi nter back to


Proc edur e 'undo chan ges' sets a node
assu mpt ions can be made abou t
its own vari able in the var list . Some
undo ne when a goa l is bein g
undo ing chan ges. Changes are only
t' wil l remove the poin ters to
resa tisf ied , Proc edur e dele teva rlis
I

goa l, Shared var iabl es which


vari able s crea ted afte r the pres ent
be lef t as they are sinc e they
occu r earl ier than this goa l shou ld
vari able s crea ted duri ng the
do not want to be resa tisf ied, Only
need to be look ed for.
matc h of this goa l whic h are shar ed

equ alle d 6 the match


For exam ple, assume that when matchnum
•tes t(Y, Z, Y) 1 and the rule
betw een the que stio n
to give
•tes t(X ,X,[ a,b] ):-c at(X )' took plac e,

57
.I

\
+e.sr~
l-i -J-}
'"" -.,

\
I I yI
y z
!
CQ 1-
1
C .- )
'I X
I
X

ri ab le . When
ar e now ai m ed at th e Z va
d X po in te rs
Thus th e Y an 1 s va ri ab le po
in te r must be
e st ' no de , Y
ac he s th e 't
ba ck tr ac ki ng re
va ri ab le .
t ba ck to po in t at it s own
se

58
P r e d ic a te s.
6 . B u il t in th is . in te rp
re te r
d ic a te s in
o f' th e b u
il t i n p re
p le te li s t
A com
ix.
in th e fo u rt h append
a re g iv e n
a ll e d from
e p re d ic a te s is c
fo r th
in te rp re te r_ ---~Pe ts a p re d ic a te
is
Th e im p le m e n
u re th a t
. Each p ro c e d y'
p ro c e d u re 'p
re d e v a l'
b y a 'x '. Thus 'd is p la
preceded
p re d ic a te name r q is
named b y u si
n g th e
,m a tc h ) '. The p a ra m e te
d is p la y (q
in p ro c e d u re 'x to th e
is im p le m e n te d
h c o n ta in s the c a ll
tr e e wh ic
tr e e o f th e q u e st io n h )' is c a ll e d ,
q
th e s u b (q , m a tc
'x c la u se
w h en th e p ro c e d u re
hus
p re d ic a te . T

p o in ts to

r~sf oF' 1111~s


f,c,,\,
J-rt.;_ \
cl,rns <?.
I
C-1-)
I I
b z.
l
C-)
I
y

n i f th e
o r fa ls e depending o
rn s tr u e
is a b o o le a n th a t re tu
'M a tc h '
,
oes i t s jo b
p re d ic a te d
f a
fo r th e a rg u m e n ts o
en u se d
n o ta ti o n h a s a ls o be
A common

59
predicate.
The first argument is
X1 = qA,leftA.left

The 2nd argument is


X2 = qA ,left" .rightA ~left

and so on,

In the above example

)( I :: b
I
c..-)
I
V
f

X2 = ·Z
arid

To go from the graphs to pointer values is simple

r down = left pointer

+ across = right pointer

Chapter 6 of C&M gtves a full discussion of what the built in

preQJ.oates are meant to do, Of the ones written al)Out in that

chapter, this interpreter does not have

op
reconsult
seeing
see
tell
seen
60
to ld
te ll in g
to be
it w as no t co ns id er ed
emented si n ce
I is no t im pl
1 R ec on su l t 6 fi le
ne ce ss ar y to ha ve th e
t seem
al so d id no u tp u t o f th e
u se fu l. It th e in p u t and o
th is is th at
e re as o n fo r of
p re d ic at es . Th e d is cu ss ed in ch ap te r 5
th an th e on
te r is a lo t si m pl er u t is
in te rp re
se nt to th e sc re en . In p
eant to be
u tp u t is o n ly m ad
C& M . Th e o
hile it is p o ss ib le to re
ke yb oa rd . W
th e te rm in al " ') ,
u su al ly from le sy m bol is re a d ('
o f fi
ce th e en d
'c o n su lt ', on
fi le s u si n g in al .
on ce ag ai n se t to th e te rm
is
th e in p u t fi le
o si ti o n and
to se t th e pr ec ed en ce , p
ic at e is us ed
The 1 op p re d rs a re in p re
fi x
1

th e o p er at o
r. S in ce a ll
so ci at iv it y o f an o p er at o
as
t needed.
d ic at e was no
form th is p re
hich ha ve
o n ly th e p re d ic at es w
w in g ch ap te r
In th e fo ll o in C&M w il l
e ir o p er at io n from th os e
in th
de o r d if fe r
co m pl ic at ed co

be d es cr ib ed
.

6.1 C a ll . us ed ,
n th e p re d ic at e 'c a ll ' is
whe
in te rp re te rs ,
In most PROLOG
o f th e form
it is u su al ly
or ca ll (X )
ca ll (s (T ))
li k e
a n ti a te d to a q u es ti o n
v ar ia b le in st
w he re X is a
p( Y )

61
t.
e q u e st io n is a c o n st a n
th
n c to r 'P ' o f
where th e fu
o f a more
t of
I c a ll ' can be
th e ar g u m en
In th is in te rp re te r,

. So in
g e n e ra l fo rm
c a ll (X )
ke
n ti a te d to a q u e st io n li
n be in s ta
is a v a ri a b le which ca
X
Z(Y)
te d v a ri a b le ,
u e st io n is a n in s ta n ti a
f th e q
n c to r 1 Z ' o
where th e fu
,
More si m p ly
)
c a ll { (Z (Y ))
ry to p u t
it is n e c e ss a
th is ca se ,
N o ti c e in
can be w ri tt e n . s.
au se o f th e sy n ta x ru le
) b ec
around Z (Y
p a re n th e se s
si m u la te th is
p o ss ib le to
it is
o th e r in te rp re te rs In th is
'=··' ( u n iv
1
In 1 ).
to r' o r
'c a ll ' by u si n g 'f u n c
g e n e ra li ty in
a u se r can
w ri te
in te rp re te r,
v a l{ 2 ).

n u m (v a l) .
),
? - num(X),
c a ll ( (X (Y ))

to o b ta in
X = val
y = 2

62
• • • ...1•;..z :~·..... i, .. .

In a more simple interpreter, a user must write

val(2).

num(val).

?- num(X), functor(F,X,1), call(F).

to obtain

X = val
F = val(2)

In both cases, 'call' has executed the goal 'val' to find a value of

2. An alternative approach is to write

?- num(X), : •• (F,[X,Y]), call(F).

to get

X = val
y =2
F = val(2)

In other words, in this interpreter it is posssi ble to execute a

question which has an uninstantiated variable as its functor. To

deal with this complication, procedure 'xcall 1 calls a procedure

'getcallarg' which makes a simplified copy of the 'call' argument

and puts it into the variable 'newq'. 'Newq' will be the argument

with the functor variable replaced by its value • Thus in the case

of

call( (Z(Y)) )

where Z is instantiated to 'P' then •newq' will contain

p(Y)

63

' . . .~ . . . . . ' .. : . ; ~ .. . . ~
The argum ent is then satisf ied as if it were a questi
on using
proced ure 'satis fy'. As descri bed earlie r 'satis fy' will add
subtre es to the origin al questi on tree. Contin uing the
example, at
the end of 'satis fy' the questi on tree 1 p(Y)' which origin ally
looked like

f
f
C-)
I
y

may become

C:-) I
,- -J-· ··
I I I
y X :
I .
C-)
I
y

The key featur e of this new tree is that the 'P' node
now has a
right hand subtre e. So in 'xcall I after I sa tisfy ' has finish ed,

64
proc edu re save cha nge s I is cal led . 'Sav echa nge I
I
s adds thi s new righ t
han d sub tree to the orig inal 'ca
ll' argu men t. What this sub tree is
actu ally appended to is the fun cto
r con stan t in the I cal l' argu men t.
Thi s mus t firs t be found bec ause
as des crib ed above it may be in a
var iab le. When the fun ctor is fou
nd it may alre ady have a rig ht hand
sub tree , This is pos sibl e if the
'cal l' argu men t has been sati sfie d
befo re and this tim e 'xca ll' is
bein g exe cute d because the argu men
t
is bein g res atis fied , If the fun
ctor alre ady has a rig ht hand
sub tree , it is del ete d and the new
righ t hand sub tree is app end ed.
e.g
call (P) is bein g sati sfie d

where P is inst ant iate d to Z(X, Y)


and Z is inst ant iate d to s
1 Newq' wil l obta in s(X ,Y) in 1 get call arg 1

Sinc e s(X, Y) has .bee n sati sfie d


befo re 'new q' will cact uall y be a
tree like

65
s
I_ - - - - - - - - - -
c.-J-) :---;
I I I I
X y r ~
I I
C-) C-)
I I
X y

After 'satisfy' has worked on 'newq I let ~s assume i.t becomes the tree

,-------_
s
c-,-1 :
I I I
~ i d
I
(,-J-)

I I
X y

In I save changes' the constant functor of I P I is obtained. Thus I


s1

is returned. Actually, the tree's' contains

66

. . . ... '
." ~
... . . . . -~
s-------- ,-
I
)


p q

I I
C.-) C.-)

I I
X 'I

which is 's' with its old right hand subtree. This subtree is
deleted, leaving the tree

The n.ew right hand subtree is appended giving

s~.
,-

d'
I
C"'-i-)

I I
X y

67
This is now what is stored in rz, when 'savech anges' and then
'xcall' finish.

6.2 Not & '; '(disjun ction).

Both these predica tes have been impleme nted using PROLOG rather

than PASCAL. When they are called an error is printed telling the

user to consult the file 1 pred.in ', This file contain s their
definit ions, which are

nott(X) :- call(X) , I ,fail.


nott (X).

and

or (A, B) : - call (A) ,


or(A,B) :- call(B) .

Notice that names other than I not' and '; 1 are used since these are
user defined predica tes. Other built in predica tes could be
impleme nted this way but their PASCAL version s are as simple. For

example , 'nonvar ' is defined in PROLOG as

nonvar(X ) :- var(X), I , fail.


nonvar( X).

Its PASCAL version is in procedu re 'xnonva r I and is also only 2


lines long.

See chapter 10 for further discuss ion of PROLOG defined


predica tes.

68
6.3 Clause.

A variable functor can be assigned to the first argument of

'clause'. To simplify this argument, the procedure 'simplifyhead' is


called. Thus in

clause ( ( Z (X)), Y)

where Z is instantiated to 'P'

then X1 = (Z(X))

After a call to 'simplifyhead'

newx1 = p(X)

It should be clear that X1 contains the first argument of 'clause 1

which is the head of the rule (or fact) and X2 will contain its

right hand side or possibly the value 'truer. The restriction is

that X2 will only return the first subgoal of any right hand side.
Thus if there is a rule

t(X) :- b(X), c(X).


then

?- clause( t(X), W).

will give

W = b(X)

69
6 .4 Con sult .
proc edur e 1 Xc'Jnsult 1 • In
The code for this pred icat e is in
in 1 xsee 1 • Thi s sets the inp ut
actu alit y most of the work is done
argument of I cons ult 1 • A lim itat ion of
file to the filen ame
iden tifie r, shou ld only be 9
1con sult I is that a filen ame , bein g an
ame s must hav e a file end ing
cha ract ers long . Also sinc e DEC20 filen
must be in sing le quo tes or
with 1 .<so meth ing> ' then the filen ame
PROLOG synt ax and an erro r wil l
the 1 • 1 will be take n as a peri od in
ng like
occu r. Thus the user mus t type som ethi
?- con sult ( 'pre d. in') .

rict ion s. If the user type s a


The re is a way to get around thes e rest
the user wil l be que ried for
file name unknown to the syst em,
the ope ra ting system and so
ano ther filen ame . The quer y comes from
rict ion s do not appl y. Thus
the sing le quo tes and 9 char acte r rest
?- con sult ( 1 vbbk.pq ) .
1

? File not foun d - vbbk. pq


in
Try anot her file spec : very _lon g_fi le,

If the file is foun d PROLOG is reen


tere d and con tinu es as norm al.

symbol '" 1 at the end of


All file s must have the end of file
file erro r wil l occu r and the
them. If this is not foun d an end of
used in 'nex tlin e• to res et the
inte rpre ter will fini sh. The "' is
1 1

inpu t file .

ral than the one desc ribe d


This 'con sult ' pred icat e is more gene

70
n the se
be inc lud ed in the file s. Whe
in C&M sin ce que stio ns can
sti ll be
for the m, from the use r, wil l
que stio ns are exe cut ed, inp ut
e bein g
not be loo ked for in the fil
tak en fro m the key boa rd and
sul ted ,
ture is tha t when a file is con
con sul ted . Another use ful fea
tha t if
wil l als o be pri nte d on the term ina l scr een . Thi s means
it
re they
fil e, the use r wil l see whe
the re are any err ors in the

occ ur.

The form
1
?- [ file 1 , -fil e2, 'fre d.1
] •

ulte d of
lud ing the file s to be rec ons
is not allo wed , al thro ugh exc
atio n can be ach iev ed.
the form -fi le, a sim ilar not

e.g
?- [ file 1 , 'fre d.1 ' ] •

cou ld be wri tten as


d.1 ' ] ) •
?- con sul t_l ist ( [ file 1 , 'fre

wit h the rul es


con sul t_l ist ( []) .
con sul t_l ist ( [X IY ]) ':- con sul t(X ),
con sul t_l ist (Y) •

71
6 .5 Cut (I).

The code for this predic ate sets a flag called I cutfla g' which
is in each node of the questi on tree that is of type identi fier.

When a cut symbol is found a search back up the questi on tree


is
carrie d out. The search stops when a questio nop is found or the

identi fier before a ruleop symbol is found. Thus for a questi on


such
as

'\
.

5€llt"di ,---J J
s~rs here I I
X r d
I '\' I
C:-) Sea re~ C.-)
b.i.9inr
I her~ I
X X

A search is starte d at the cut and stops at 'appen d'. Startin


g at
the 'append ' node the 1 cutfla g's in the identi fiers are set to true.
This is done until the cut symbol is reache d. The I cutfla g I is used
in 'satisf y' to stop a goal being resa tisfied .

72

' .__ • ~ .. ' . . • ~ ~ • - ·-~.. • • • • j •


6 .6 Read.

Thia predicate, in C&M, reads the next term from the current
input stream. A term is a line of PROLOG ending in a full atop. The
•read• in this interpreter reads only the next symbol which may be

an identifier, a number or a BYlllbol. A full atop is still required


to complete the line. Thus for

?- read(duck).
the user types
duck. <er>
PROLOG responds with

yes

6 .7 Name.

The code for •name• is long but not particularly complex. The
predicate converts an atom into a list or a list into an atom. Thus
•xname ' co naiat s of 'if' teats which decide if •atomtoliat • or
'listtoatom• should be cal.led. Procedure •atomtoliat • will convert
some thing like

al:lc to

where 97 is the ascii code for 'a' and so on.

73
Similarly, 'list toa tom I Will convert something like

i_i_i_. to abc
97 qg q'f

6 .8 Functor.

'Functor' can be used i.n 2 ways as described in C&M. The


1
procedure 'xfunctor first distingUishs between these 2 cases and

then manipulates the tree using procedure 'build', 1


Build takes a
1

functor (bob, for instance) and an integer representing the arity


(2, for instance) and creates a tree, For 'bob 1 and 2 the tree

bob
I
C-i-)
I I

1 1
will be created. is the anouymous variable.

74
6 .9 Arg,
, arg ' and the
The onl y dif fer enc e between the int erp ret er' s

int erp ret er


'ar g' in C&M is tha t in thi s
?- arg ( 2,f (a) ,X ),

wi ll give
X= a

ins tea d of fai lin g.

pre dic ate the n


If the 1st arg um ent is gre
ate r tha n the ari ty of the

argument of the pre dic ate .


the var iab le ge ts the las t

6 .1 O Univ ( = •• ) •
te lis ts
In C&M, 'un iv' can ma nip ula

e.g
?- =.. ( [a, b,c ,d] ,L) .

·wi ll giv e

L =[ I
I
I
I a , [b, c,d ] ]

the ~ot ',' is


Tll is form is not allo we d
in thi s int erp ret er sin ce
pro ced ure s
e for lis ts. 1 Xuniv' use s 2
not allo we d as a fun cto r nam
1 con ver ts a str uct ure to
1 cre ate str 1 , 1 Cr eat eli st
- 1 cre ate lis t 1 and

a lis t.

75
Foo
I ba.c.oma.s I
C-'-1-)
foo t\ b
I I
0. b

1 Createstr' converts a list to a structure.

F'o o
·-·-·-· bec.om<Z-S I
I c.-J-)
.Coo ~ b
I I
a b

76

·,---.,.1 ·, • ~ : • , • •• ' ••
6.1 1 Is .
Thus
' must be an op er ato r.
The 2nd argument of' 'is
?- is( X, 2) .

be an ar ith me tic
all ow ed . In C&M th e 2nd argument must
is no t
en . In
na tel y, a de fin iti on of' th is is no t giv
ex pr es sio n. Un fo rtu
nd th e
ve fo rm is all ow ed . A us er could ge t rou
some PROLOGs th e abo
ty pi ng
ab ov e re st ric tio n by
?- is( X, +( 0, 2) ).

ai ns t the sp ir it
, ass ign me nts of th is form seem to go ag
In ge ne ral
ou ld be done by
As sig me nts , at le as t s:l.mple on es , sh
of PROLOG.

pa tte rn ma tch ing •

.6.1 2 St ric t (= =) .
es a fla g
'==' is in pr oc ed ur e 'x st rio t'. It us
The co de fo r
is se t
'x st ri ct ' is fi rs t ca lle d, 1 st ric tfl ag '
1 st rio tf' la g' . When
lls 'ev al ua te '.
1 Xs tri ot I th en ca lls 1 xe qo p 1 wh ich ca
to fa lse .
ing ma tch ed ,
1 if tw o di ffe re nt va ria bl es ar e be
In sid e ev alu ate
1 ,

tfl ag 1 wi ll be se t
be ca lle d an d st ric
1
'qd bv are nd ' wi ll
ev en tu all y
co nt ro l re tu rn to
Al l the pr oc ed ur es wi ll fin is h and
to tru e.
il.
'x st ric t' which wi ll fa

77
7. D ia gn os tic s.

PROLOG.
7 .1 D ia gn os tic s fo r te rp re te r
st ar ts to w ar ds th e en d of th e in
e
The code fo r th es 1xn os py 1 • These
1 xt ra ce 1 an d fi ni sh
es w ith pr oc ed ur e
w ith pr oc ed ur e and 'tr ac e' .
al bo ol ea n va ri ab le s - 'debugon'
gl ob
pr oc ed ur es us e 2 ne ra te d du rin g
en ev er di ag no st ic ou tp ut ca n be ge
1 Debugon 1 is tr ue
wh
on
1 Debugon' w ill be
tru e if •t ra ce ' is
a qu es tio n.
th e ex ec ut io n of th e
se t. 1 Tr ac e 1 is tru e on ly if
ha s been
or /a nd a sp yp oi nt s been
sw itc he d on . The di st in ct io n ha
has been
pr ed ic at e 'tr ac e' se e if
ca n be do ne in •s at is fy ' to
qu ic k te st
made so th at a
has to be -c al le d.
pr oc ed ur e de bu g'
I

da ta st ru ct ur e ca lle d
th e sp yp oi nt s, a li st
To re pr es en t
I ad dt os py ' , 'in se rt sp y' ,
us ed . The pr oc ed ur es
's py pt er ' is
lle d
us ed to m an ip ul at e it . They ar e ca
1 ri ds py 1ar e
're m ov es py ' and ar e
th e sp yp oi nt pr ed ic at es which
ur es fo r
from th e pr oc ed
1 xs py 1 and
1 xn os py 1 • The 1 sp yp te r' da ta
1 xd eb ug gi ng ,
1 'xnodebug 1 ,

y he ad '.
g th e va ri ab le sp
I

re fe re nc ed us in
st ru ct ur e is

me an d a number
sp yp1te r 1 co nt ai ns a fu nc to r na
Each node of li st
e pr ed ic at e. Fo r in st an ce , th e
ar it y of th
re pr es en tin g th e er en t
e sam e fu nc to r name bu t w ith di ff
des w ith th
m ig ht co nt ai n 2 no

ar it ys .

e. g sum, 3
sum(X,Y,Z) -->
78
sum(X, Y) --> sum, 2

F.a.ch node is in alph abet ical orde r and if


2 nodes have the saine name
then thei r orde r in 'spy pter 1 is the orde
r in which they were typed
• If a ques tion of the form

?- spy( sort ).

is aske d then the spyp ter node is

sort , 0

Th_e synt ax, spy[ sort( 2),ap pend ] is not allow


ed_. Som ethin g like it
could be adde d usin g

?- spy_ list( [sor t(2), appe nd]) .


with the claus es

spy_ list ( []) •


spy_ list ( [X IY]) :- spy( X),
spy_ list( Y).

Simi lar rest ricti ons appl y to 'nosp y'. Once


again the user could
defin e

nosp y_lis t ( []) •


nosp y_lis t ( [X IY]) :- oosp y(X) ,
nosp y_lis t(Y) .

The PASCAL proc edur es 1xtra ce 1 throu gh to 1 xnospy 1 only deal

79
with setting up the boolean flags and the 'spypte r' data structu re.

The structu res are used by calls to 'debug' when 'debugo n' is true.

'Debug' is called in 'satisfy ' from 4 differe nt places when a

questio n is being treated differe ntly. They are the 4 places that

control can flow through a questio n as outline d in section 8 ,3 of

C&M, called CALL, EXIT, FA IL and REDO •

Since a questio n in this interpr eter is in the form of a tree

and because of the way that a goal is resatis fied by searchi ng from

the root of the tree down to that goal, REDO is dealt with in a

differe nt way than that given in C&M. For example , in C&M, if

, trace' is on and the questio n

?- des(X) ,fail.

is typed with the followi ng clauses in the databas e

des(X) :- b(X),c( X).


b(2).
c(2).

Then when the goal I fail I is reached , the followi ng will be printed

CALL fail
FAIL fail

C&M's diagnos tics will then print

REDO : des(2)

since 'des' is the last goal in the questio n that succeed ed. Then

REDO : c (2)

80
'de s I goa l
was the las t sub goa l in the
wil l be pri nte d sin ce thi s

tha t suc cee ded . Then


FAIL : c(X)

goa l fai ls.


wil l be pri nte d sin ce thi s sub

be see n in
In thi s int erp ~et er, the dif fer enc e in REDO can

ich wil l be
term s of the que stio n tre e wh

?- -- ,
(D
I @I
Fuil

·r~
C- ) ;- -->

I I I
X (3\ (1) c
I I
C- ) C- J
I I
X X

nt
er of the ma tch es. At thi s poi
The numbers ind ica te the ord
CALL fai l
FAIL fai l

ati sf'i ed.


t of the que stio n wil l be res
is pri nte d. Now the fir st par
a goa l whose 'matchnum' equ als
3. When
Thi s is don e by sea rch ing for
is pri nte d
thi s is fou nd, the dia gno stic
REDO: c(2 )

the n

81
FAll. c(X)

Thus the difference between C&M I s REDO diagnostic and this

interpreter's REDO diagnostic is that when backtracking takes place

the parent goals reentered (in order to resatisfy their children)

are not printed. In practice, this means that the number of REDOs

printed is less than in C&M. The same number of CALLs, EXITs and

FAll.s are printed and usually the user is only looking f'or these

three and in particular FAll..

7.2 Interpreter diagnostics.


These are diagnostics which help in the debugging of the

interpreter itself. They were used during development and have been

left in so they can be used in future.

7.2.1 Boolean diagnostics.

These are
printeval

helpful printing

and timer

They are set at the very start of the interpreter. 1


Printeval 1

when set to true will cause •writeln' statements to print things of

the type
Swop q & db for qend

and

82
eva luat e

Gen era lly, the se stat eme nts con


sist of the name of the pro ced
ure
tha t is cur ren tly being exe cute
d. 1 Prin tev al 1 swi tch s on the
•wr itel n 1 stat eme nts for the pat tern mat chin g pro ced ure s.
•He lpfu lpri ntin g' is the boo lean tha t swi tchs on •wr
itel n'
stat eme nts in all the oth er larg e
pro ced ure s in the inte rpr ete r.

Timer• is slig htly dif fer ent in


1
tha t it cau ses the run tim e
for the sat isfa ctio n of a que stio
n to be prin ted . The tim er sta rts
aft er the <er> at the end of a que
stio n and fini she s when an answ er
is prin ted . If a que stio n is res
atis fie d the tim er sta rts from whe
n
the •; <er>• is typ ed.

7.2 .2 Data stru ctu re dia gno stic s.

These dia gno stic s are pro ced ure s


whi ch can be use d to prin t out
val ues in the dat a stru ctu res
of the inte rpr ete r. They are cal
led
'pa l fa 1 , 1 ptre e 1 , 1 phase I and I pva
rpte r 1 • Cal ls to the se pro ced ures
can be ins erte d ins ide new pro ced
ure s tha t are being tes ted .
e.g
ptre e(q )

where q is the cur ren t node of the


que stio n tree . Or
pva rpte r{q A.v arv al)

Care mus t be tak en tha t inv alid


poi nte rs are not pas sed to thes
e

83
procedure. Thus if q is nil then

ptree(q"' .left)

will cause an execution error in PASCAL.

On the DEC20 a similar effect can be achieved by using the

debug system [5] and getting the values of the data structure by

typing a line of the form

variable =

e.g

q =

q"'.varval =

84
. • . - - - .. t.____ . () .. • . .

8. DEC20 depende ncies.

The PASCAL used in this interpr eter is the standar d type. No

DEC20 extensi ons have been used such as 'others ' or 'loop', This

rule has been broken for two cases. The 1 timer 1 diagno stic and also
for input of data.

8.1 Timing questio ns.

The run time of questio ns is calcula ted using the DEC20 built

in variabl e, 'runtim e'. This variabl e is used in 4 places - in


procedu res 1 printsu ccess', 1 printfa ilure 1
, 1 reanswe r 1 and 1 answer 1 •

8.2 Input.

Three input files are used in the interpr eter at differe nt

times - 1 inp 1 , 1 tty 1 and 'input' .

iinp' is defined in 'initlis ting' to be an interac tive file.

The file 'inp' is read using procedu re I readone 1 • Such a file type
was created so that charact ers could be read from the DEC20 keyboard

without having to wait for a carriag e return to release the input

buffer. 1 Readone 1 is used by the input predica te procedu res 1xget0 ',
'xget' and 'xskip' . It is also used in 1 setdiag nostics 1 •

'Tty' is the standar d input file from the keyboar d. It is used

in procedu re 'readx' . The first charact er is read from 1 tty 1 in


'initlis ting'. 'Readx' is also used in 'nextter minalc h' and also

85
function 'try again, •

1 I
Input is the variable that is assigned the names of the files

that are being consulted. It is used in 1 nextfilech'. 'Input' gets a

file name in 'xsee 1 • It is reset at the end of 1 nextline 1 when the


end of file is read.

By having 2 different next character procedures, one for the

keyboard and one for files, it is possible to have questions inside

consulted files. When a question reqUires the user to type

something, input will be taken f'rom the keyboard and the question
will be able to continue.

86
9. Comparisons with UNH PROLOO.

For convenience, in this chapter, this author's PROLOG

interpreter will be called Lehigh PROLOG.

UNH PROLOG is a PROLOG interpreter originally from the

University of New Hampshire. It is written in C to run on UNIX. The

version that was used by the author is located in the CAE lab in the

Civil Engineering department in Fritz lab at Lehigh University. This

version had been modified at Syracuse to run on the Data General

WI/ 10000 under AOS/VS. Unfortunately, the modifications had not been

totally successful and some advanced features do not work or do not

work completely. For example, the ability to include C modules with

the PROLOG code does not work.

The syntax used is like that used in the Edinburgh DEC 10 PROLOG

and thus is very similar to the syntax in C&M and so to that of

Lehigh PROLOG. Unlike the Edinburgh PROLOG it does not compile any

of its code, It also does not do any garbage collection unlike

Lehigh PROLOG, This did not cause any problems in the tests that

were carried out.

87
9.1 Timing comparisons.

In timing comparisons with Lehigh PROLOG it was found that

small and medium size programs ran at about the same rate. On large

programs, the differences in speeds became increasingly noticable.

Both interpreters slowed down but Lehigh PROLOG was almost 15 times

slower. Two of the large size programs used were an elisa program

(to be found in 'elis.in 1 ) and an ATN program ( 'atn.in').

If these test files are looked at, it can be seen that I atn. in. 1

and 1 elis.in 1 are not all that long. The reason why they are large

programs is that when they execute they both create very large

question trees. Time is then spent backtracking along and searching

these trees. 1 Atn. in' is also large in that it passes sizeable

amounts of data about in its variables. This shows a few general

rules to be kept in. mind when writing a program for Lehigh PROLOG.

1. Try to keep questions short

e.g write
?- recognise(S).

?- parse(T).

iristead of

?- recognise(S),par se(T).

2. If data has to be passed between questions, save the data in

the data base using I asserta I and 'assertz' , instead of passing it

88
through arguments.

3. Where possible try to restrict recursion. Each recursive

call of a procedure adds a new subtree to the question tree.

4, Try to instantiate variables as soon as possible, Dont use a

lot of uninstantiated variables that are sharing with each other.

These rules will keep the question tree and var list size down

and so speed up execution.

9 .2 Ease of use.
The UNH PROLOG starts very simply, The user must type

x prolog

The interpreter then starts with the prompt

?-

It expects the user to type a question and so prompts with a

question symbol. In order to type in facts or rules, the user must

first type

?- [user], <er>

to get the prompt

This is slightly inconvenient.

89
list form.
If the user wants to consu lt a file, he can use the

?- [ 1 atn.i n 1 ] , <er>

1 notat ion which is


The system also allow s file names with out the ' ,

usefu l, In that case no quote s are needed.

e,g
?- [vix] . <er>

scree n while being


When a file is consu lted, it is not liste d on the
ge appea rs out of
read in. This means if an error occur s, a messa

Also a file may not conta in quest ions, unlik e Lehig h


nowhere.
a lot of files .
PROLOG. This will cause diffi culti es if a user has
ions to use them.
Each time he consu lts one he must type in the quest
By being able to
He must also remember what form a quest ion· takes .
defin ed, the user
have quest ions in the files where the claus es are
what quest ions to
is spare d a lot of typin g and havin g to remember

add, It is also a good aid to docum entati on.

It is possi ble to leave UNH PROLOG and edit a


file and then

the syste m will


retur n and recon sult it, Unfo rtuna tely, somet imes
Also if the user
not allow this and give an out of space error .
ns, the number
leave s PROLOG comp letely then edits a file and retur

of keyst rokes are not that much great er.

a user can
UNH PROLOG does not allow varia ble funct ors. Thus

90
not write

call ( (X (Y) ) )

sions to
See chapt er 6, sectio n 1 for more detai ls on the exten

'call' in Lehigh PROLOG.

but is
UNH PROLOG allows prefix , infix and postf ix notati ons
postf ix then
limite d in that if a predi cate is define d as infix or

it can not be writte n in its defau lt prefix form. Thus

2 <5 • is corre ct

but

<(2,5 ), gives an error.

ed
There also seems to be some restri ction s on using reserv
ce
words and symbo ls as ordin ary words and symbo ls. For instan

';'(2, 3).

is allowe d but not

':- '(2,3) .

a lot
UNH PROLOG allows real and negati ve numbe rs. It also has

of extra predic ates such as

sin(X ), tan(X ), etc

One of the most usefu l is


statis tics

91
which gives, amongst other things, the amount of CPU time used by a

questio n.

One inconve nience, which isn't the fault of the UNH PROLOG

system, is that the high speed printer connect ed to the MV/ 10000 can

not print all the PROLOG charact er set.

Overal l, UNH PROLOG is a very interes ting system al through some

things on it could be improve d.

92

• • •'
~
I I - .._ • • a - •:
10. Improvements.

Since research is still continuing into the syntax and

semantics of PROLOG as well as the implementation of it, this


chapter has the potential to be infinitely long. Instead, discussion

will be limited to some of the ideas being considered at present. An

excellent overview of these can be found in [9].

10.1 Remaining predicates.

Firstly, the predicates 1 op 1 , 'reconsult, 'seeing', 'see',

'seen', 1 tell 1 , 'telling' and 'told' could be implemented. In

practise this user has only found 'op' to be needed in some cases,

where code has been written using a lot of arithmetic predicates.

is(X, +(5, *(Y,2)))

is a lot less convenient to write than

X is 5 + Y I 2

10 .2 Language Modules.

Having, just recently, had access to two commercial PROLOG

systems this user has seen some of the other versions of PROLOG

available. The two versions seen were

VMS PROLOG-1

and

ONH PROLOG

Both of these are descendents of the Edinburgh DEC10 PROLOG and vary

93

• .. • :r • . •. . ,' . ,: .
only in minor ways from the PROLOG describ ed in C&M. Most of the

variati ons are additio ns to the built in predica tes availab le, Both

version s also offer the incorpo ration of modules of code from other

programming languag es into PROLOG. VMS PROLOG-1 allows the additio n

of assembl y languag e and FORTRAN subrout ines. Unfortu nately, there

are restric tions on the types and number of paramet ers that can be

passed. UNH PROLOG which is an interpr eter written in C, allows the

additio n of C modules . There are obvious ly problem s with this since

it did not work on the Data General MV/10000 that this PROLOG was

on.

The incorpo ration of other languag e modules does not seem to

follow the spirit of PROLOG which is based on 1st order predica te

logic. If the other modules were in the form of abstrac t data

structu res and operati ons, this would make the interfa ce between

PROLOG and the other languag e much more indepen dent of the types of

logic and control that the other languag e uses. Work has been done

on implem enting abstrac t data structu res in PROLOG [ 10] so that

differe nt abstrac t objects , written in PROLOG, can communicate with

each other by message passing alone, This type of system offers

increas ed modula rity and extens ibility for PROLOG as well as the

possib ility of paralle l process ing. From that stage it is simple to

incorpo rate other objects written in differe nt languag es. Such

languag es have been develop ed, such as SMALLTALK [ 11].

94

.- ' .- ~
::. - . . ~
,I ~· .. '
.

10,3 Extr a pred icate s.
r PROLOGs are worth
Some of the buil t in pred icate s iri the othe
gging featu res used in UNH
impl emen ting in this inte rpre ter. The debu
'cre ep', 1 leap 1 , 'ski p', 'brea k',
PROLOG are enha nced to inclu de

and 1 hal t' of secti on 8 ,4 of C&M. Also inclu ded is


'abo rt I
ent claus e
1 ance stors ( L) 1 which pl aces the ance stor goal s of the curr

into the list L.

the idea of sets in


Two othe r inte rest ing pred icate s implement

PROLOG. These are


seto f(X, P,S)

and
bago f(X,P ,B)

that P is prov able , into


1 Seto f' plac es all the insta nces of X, such

s.
e.g
?- seto f(X, X like s Y, S).

migh t prod uce

Y = beer S = [dick ,harr y,tom ]


Y = cide r S = [bill ,jan ,tom ]

red.
The set S must be rx>n-empty and will be orde

list
The pred icate I bago f I does much the same thing but the
dupl icate s.
prod uced will not be orde red and may cont ain

95
Both PROLOGs examined contain a lot more arithmetic predicates.

Connected with this is the fact that both PROLOGs allow real numbers

of the form

real : : = integer 'E' exponent I


integer ' • ' integer I
integer ' • ' integer I E' exponent

exponent . ·- integer I
1 + 1 integer

'-' integer

Negative numbers are also allowed.

Some of the extra predicates are

abs (X) exp (X) log(X)

log10(X) floor (X) ceil (X)

rand sin(X) cos (X)

tan(X) asin(X) acos(X)

atan(X)

Bit operations can also be done

X » y shift X right Y places

X « y shift X left Y places

X /\ y bitwise conjunction

X \I y bitwise disjunction

These additions to the interpreter would not involve a great deal of

work. The syntax analyser would have to be altered to accept real

96
and negative numbers. The code for the predicates would be similar

to that in 1 calcarith 1 • The fact that most of these operations are

defined in PASCAL simplifies their PROLOO implementation a great

deal.

Another useful feature of the UNH PROLOO, if it worked, is the

automatic creation of a prolog.log file during the use of the

interpreter. The log file contains a copy of everything that has

happened during the current job. This feature can be mimicked on

this interpreter by using the DEC20 'photo' command just before

typing

ex prolog. pas

10 .4 PROLOG defined predicates.

Some PROLOG interpreters have been written in PROLOO. Even at

the simpliest level many built in predicates can be defined in terms

of other predicates. Some of these definitions are given in C&M.

For instance, the following predicates can be defined by other

predicates

Predicate written using

Listing Clause

Call Consult

Consul t,reconsul t Basic file predicates

97
.. ; ·.. :... . ' ..;. . . . ' ··., ,

2 out of In terms of.' the 3rd


functor., arg, =.•

Skip ·aet

Get GetO

Tab, nl Put

Nonvar Var

Atomic Atom,integer

Repeat Standard logic

I• I not Call
' '
1\: I I: I

1\:: I I:: I

,., '+'
'I'' mod ,_,' '+'
'=<' I: I
'
I ( I

'>' not, I( I

'>=' '>'' I: I

I• I
In this interpreter it was decided only to def.'ine •not' and
'
in terms of other predicates. They are

nott(X) :- call(X), I , fail.


nott (X).

and

or(X,Y) :- call(X).
or(X,Y) :- call(Y).

98
10 .5 Trees and stacks.
Most of the interpreters or compilers for PROLOO use a stack to

store goals [7]. This makes backtracking very quick and easy since

the current goal is unstacked leaving the previous goal ready to be

resa ti sfied.

The trouble with a stack implementation is that it limits the

types of search and backtracking strategies that can be tested. For

instance, it is very inconvenient to unstack a goal that is not on

the top of the stack.

A tree representation for a question means that many different

types of control strategies can be tried. There is also a vast

amount of literature on efficient tree search algorithms and

representations for trees [12].

Up until now the PROLOG questions have been represented using

the notation below


?- t(X, Y).

becomes

99
?--
1
-r
l
C,-l-)

l I
X y

tis fie d, by ma tch ing


When a go al ha s been sa
t(S ,T ) :- b(S ) ,d( T) ,

nd ed to th e tre e
r ins tan ce , the new su bg oa ls have been ap pe
fo

.
7_
I

i--------
G -i -)
·,'. .,- ·-J

I I I I
d
)< y b
I I
C: -) C. -)

I I
5 .,.

an d I d' be ing
the main go al, wi th ' b'
't' can be tho ug ht of as be ing
qu es tio n tre e be co me s
its su bg oa ls. Thus the

100
7- //..oo/-
1
j--

/ \
0 d

red in this repr esen tatio n.


The argu men ts of the goal s can be igno

A que stio n such as

?- appe nd (X, Y, [a, b]) ,member (X, [c]) .

wou ld be come

?~ ~oor

I\

a que stio n, the tree would grow and


Dur ing the sati sfac tion of

shri nk and may look like

101
The numbering of the goals is not relevant.

Any particular goal G may be in 1 of 3 states - 'fr.esh',

•matched' or 'failed'.

A 1 fresh I goal is one that has not yet been matched with any

clauses in the database.

A •matched' goal is one that has been matched with a database

clause.

A 'failed I goal is a goal that has failed to match against any

database clauses.

102
Given below is pseudo-code for searching a tree and also for
backtracking along a tree,

procedure search(var finished : boolean);


begin
finished := false;
repeat
PICK a I fresh I goal from the tree (a leaf node)
policy : depth-first;
if found one then
FIND a clause that matches the goal;
if successful then
make it a I matched I goal;
add subgoals to the tree below the current
goal - all 'fresh';
else
mark goal as I failed';
end
end
until all goals are I matched I or
got a 'failed' goal;
if all goals are I matched' then
print success;
if question is not to be resatisfied then
finished:= true;
end
end
end; ( • search •)

procedure backtrack(var finished boolean);


begin
finished : = false;
repeat
PICK a I failed I goal from the tree (a leafnode)
policy : the latest;
if found one then
if' parent has all I failed' children then
delete children;
let parent be next 'matched' goal;

103
else
PICK closest 'matched' goal to chosen
'failed' goal
policy : the 'matched' goal that is the
'failed' goal I s sibling or a
sibling's descendent;
end
else
PICK a 'matched I goal
policy : the latest;
end;
if found one then
FIND a clause that may match the goal;
if successful then
make the goal ' fresh 1 ;
any goals that are 'failed' are set to 'fresh';
else
mark goal as 'failed 1 ;
end
end
until empty tree or
no 'failed' goals;
if empty tree then
print failure;
finished:= true;
end
end; (* backtrack *)

Both these pieces of code would be used in a procedure called

'answer' which would try to find an answer for the current question

tree.

104

• . · , , . • • " ' · . . . P~·r , . . r • • • • ~ • • : • •


procedure answer;
begin
repeat
search(finished);
if not finished then
backtrack(finished);
until finished;
end; (* answer *)

Thus a question

?- t (B) , s (D).

would become
1-Roo~

I+- \s
1
where ?-Root 1 is assumed to start as being a 'matched' goal in the
algorithms.

An empty tree is just

?-Root

1 1
The goals t and 's' are initially 'fresh'.

There are 2 key procedures (functions) used in 'search' and

'backtrack'. They are 'find' and 'pick'.

105
. ' ·. - 1: . l:,-- ' • • ., .• •

1 Find 1 choses a clause to match the current goal. In an actual

interpreter different policies can be tested inside this function.

'Find' represents the 1st type of nondeterminism discussed by

Kowal ski [ 13] •

e.g

for a question

?- append(X,Y,[a,b]).

and 2 clauses

1. append([],X,X).

2. append([AIB],C,[AID]) :- apperid(B,C,D).

'find' would decide whether to use clause 1 or 2 to match the

question.

'Pick' is the function that decides which goal to use next.

Once again, 'pick' can be implemented in many different ways to test

different policies. It represents the 2nd sort of nondeterminism

mentioned in Kowalski [ 13],

e.g

for a question

?- append(X, Y, [a, b]) ,member(X, [a]).

'pick' would decide whether to satisfy (or resatisfy)

append(X,Y,[a,b])

or
member(X, [a])

106
One assumpion of 'pick' is that it limits its chose to those goals

that are leaf nodes. This means that only goals with no children (no

subgoals) are picked out. Some of the possible policies for 'pick'

have been included in the algorithms. The most complicated policy is

in 'backtrack' for choosing a 'matched' goal that is closest to the

current 'failed' goal. Graphically, this may be seen as

I
G1 , G4 and G5 are all matched I goals. Thus when backtracking takes

place this policy will decide between G1, G4 and G5 f'or the next

goal to be resa tisfied.

By varying the policies for 'pick' and 'find', all types of

searching and backtracking stratergies can be investigated. Thus a

possible improvement for the interpreter is to replace the current

procedures which handle searching and backtracking with procedures

for the algorithms given here. The procedures which would be

107
1
replaced in the current interpreter would be 'answer' , 'reanswer ,

1 dogoal', 1 redogoal 1 and 'satisfy'. Then the interpreter will

become quite an useful tool for testing different strategies like

those outlined by Pereira [14]. This is only possible because of the

tree structures that are used to represent questions and clauses.

Also implicit in this new notation is the fact that the actual

structure of a goal does not need to be stored in the question tree.

e.g

with clauses

1. append([],X,X).

2. append([A IB],C,[A ID]) :- append(B,C,D).

and a question
?- append(X,Y,[a]).

the final question tree may become

c-)-J-) .-
I yI ,-,
I I
X a.ninJ
I I
G--1-,->
I I
e, C 0

108
which be comes

be igno red. Duri ng exec utio n


The argu men ts and rule oper ator can all
tion tree matc hed with the
what happ ened was that the orig inal ques
2nd clau se was added to the
2nd clau se. The righ t hand side of the
was matc hed with the 1st
ques tion tree . Then this righ t hand side

clau se. This gave the answ er


X = [a]
y = []

, like so
All this can be repr esen ted by poin ters

?-Roo \-
1
qpp(!/11J
I
afyUid

ss its matc hed clau se in the


Each goal would use one poin ter to acce
which to hang its vari able s
data base and use it as a 'ske leto n' on
is now the poss ibili ty that
(sto red on the othe r poin ter). Ther e
have poin ters to the same
diff eren t part s of a ques tion tree may
clau ses are only bein g used
clau se. This does not matt er sinc e the
stor ed in a sepa rate plac e.
as tem plat es for the vari able s whic h are

109
In the current interpreter, pointers are already used to access

a goal I s variables. Only slight modifications would be needed to

access these variables from a goal head instead of the variable

nodes. Similarly, a pointer is already used to point at the clause

that a goal uses. The pointer is stored in I dbruleused 1 • At the

moment, it is only used to indicate which clause is matching with a

goal.

This idea is already used in most PROLOO interpreters and is

called structure sharing [ 15].

One of the limitations of the algorithms given earlier is that

only one goal will be considered at a time. For certain problems,

parallel processing of goals would greatly speed up the solution. In

that case, problems with dependencies between data would arise. The

problem of deadlock would have to be considered. There may be two

goals each waiting for each other to finish before they could

continue. Some of these problems have been overcome in

IC-prolog [ 16] which allows a certain amount of parallelism. The

rule

sameleaves(X, Y) :- profile_of(W,X),
profile_of(W, Y).

can be speeded up by rewriting it as

sameleaves(X, Y) :- profile_of(W,X)//
profile_of(W, Y).

110
to be eval uate d in para llel.
This caus es the two prof ile_ of subg oals
for W. Thus the two subg oals
Noti ce how both subg oals use the valu e
train ed by a common vari able
altho ugh runn ing inde pend entl y are cons
ible to rest rict the para llel
W whic h eith er migh t chan ge. It is poss
ed to give a valu e to the
eval uati on so that only 1 subg oal is allow
tatin g one of the occu renc es
shar ed vari able W. This is done by anno
1
of W with a "'

e.g
same leav es(X , Y) :- prof ile_ of(W ,X)/ /
prof ile_o f(W ", Y).

must be _unbounded on
The expr esse s the cont rol cond ition that W
1 "' 1

subg oal consumes valu es. A


entr y to the proc edur e. Now only this
tatin g the W in the firs t
simi lar effe ct can be achi eved by anno

subg oal with •?'.


e.g
same leav es(X , Y) :- prof ile_ of(W ?,X) //
prof ile_o f(W , Y).

be bound to a non- vari able


The 1 '? 1 anno tatio n mea ns that the W must

apon entr y to the proc edur e.

licit ly what coul d be


Thes e cont rol anno tatio ns are doin g exp

writ ten into a proc edur e for 'pic k'.

is strea m IO. All thes e new


Also inclu ded in IC-p rolo g

to illu stra te many diff eren t cont rol


con stru cts can be used
l data -flow lang uage s [17] .
alte rnat ives and can even be used to mode

111
.. • ~ ~l~l'"~~ •• • • • • ••

11 • Conclusio ns.

One of the main strengths of this interprete r is also one of

its weaknesse s - it is written in PASCAL. PASCAL is a powerful


language which meant that this interprete r was written using
relatively little. code and yet still retained a fair amount of

clarity. Also since PASCAL was used, the interprete r can be modified

with ease. Unfortuna tely, because PASCAL is so high-level the actual

running time of PROLOG programs on the interprete r is quite slow.

Even so for small to medium size programs the speed is fairly


acceptable and compares favourable with UNH PROLOG.

Since this interprete r offers more powerful pattern matching

than most other PROLOGs, programs can be written which are much

conciser than those written in many other PROLOGs.

If some of the modificati ons discussed in chapter 10 are


implemente d then the interprete r will become an useful research
tool. In particula r the algorithms for searching and backtracki ng

would enhance the interprete r a great deal.

Since the interprete r was written in standard PASCAL, it should

be very simple to move it to another machine - it is almost 100%

machine independe nt.

The greatest use of the interprete r will be as a teaching tool.

112
Combin ed with C&M it can be used to teach PROLOG. Also because of

the use of standar d recursiv e descent design and a LL( 1) grammar,

the interpr .eter can be used to teach compile r design.

It should be clear, from using this interpr eter to write some

PROLOG, just how powerfu l the languag e is. There are many things

that can be improve d in PROLOG -some of which were describ ed in the

previou s chapter . Neverth eless, since PROLOG is grounde d so firmly

in logic, it seems clear that it is the directio n in which

program ming languag es should go [ 18].

PROLOG offers top down inferen ce which unites problem solving

and compute r program ming. By also offerin g non-det erminis m,

paralle lism and pattern matchin g it provide s all the tools needed

for applica tions in artific ial intellig ence [ 19].

By being a languag e which specifi es a problem by what is to


be

done rather than how a thing is to be done, compute r program s become

a lot simplie r to read. More over, since a program is rather like a

specifi cation of what it is suppose d to achieve , it should be

relativ ely easy, just by looking at it (or, perhaps by some

automa tic means) to check that it really does do what is

require d [20].

PROLOG shows that program ming in logic is a practic al

113
possibility and is as quick and convenient as more conventional

languages such as FORTRAN, PASCAL or even LISP [ 8].

114
• ~._ -• ~- • • • ' , ..,1•.i o<, ,

References.

1. Welsh, J, & Elder, J., Introduction to Pascal, Prentice Hall,


London, 1979.

2. Welsh, J, & McKeag, M. , Structured System Programming,


Prentice Hall, London, 1980.

3. Gries, D., Compiler Construction for Ditltal Computers, John


WHey & Sons, New York, New York, 1 971.

4. LUCC, DEC System-~ User'.§. guide, LUCC, Lehigh University,


1979.

5. LUCC, Pascal-20 Introductory User'.§ ~ ' LUCC, Lehigh


University, 1983,

6. Clocksin, W.F, & Mellish, c.s., Programming in Prolog,


Springer-Verlag, Berlin, 1981,

7. Bruynooghe, M., ""The memory management of PROLOG


implementations, 1 ' From : ,K.1, Clark (Ed.), Logic Programming,
Vol. 16, 1982, pp. 83-98.

8, Warren, D.H.D, Pereira, L.M, & Pereira, F., ""PROLOG - the


language and its implementation compared with LISP, 11 JI'..QQ •
..SJm..Q. on AI and Programming Languages, SIGPLAN notices, Vol.
12, no.8, 1977, pp. 109-115,

9. Clark, K.L, & Tarnlund, S.A., Logic Programming, Academic


Press, London, 1982a.

10, Kahn, K.M., "Intermission-Actors in PROLOG, 11 ~ : .K •.l!.


Clark (Ed.), Logic Programming, Vol. 16, 1982, pp. 213-228.

11, The Xerox Learning Research Group, "The &nalltalk-80


System," Byte, Vol. 6, no.8, 1981, pp. 36-48.

12, Tarjan, R.E., Data Structures and Network Algorithms, SIAM,


Philadelphia, 1983.

13, Kowalski, R., 1Q.g;j,_Q. for problem solving, Elsevier North


Holland, New York, New York, 1979.

14. Pereira, L.M, & Porto, A., "'Selective Backtracking,'' From :


Clark (Ed.), ~ Programming, Vol. 16, 1982, PP•
_K •.l!.
107-114.

115

""; • I • ' o .. , •, • I • '·l • ~- p ~ • ' 1, <, > • • • • • \, ',J • -, • • •


15. Mellish, c.s., "An alternative to structure sharing in the
implementation of a PROLOG interpreter, 1 ' ,Erom : .K.1. ~
{g_g_.), 1Q&Q Programming, Vol. 16, 1982, pp, 99-106.

16. Clark, K.L, McCabe, F.G, & Gregory, S,, "IC-PROLOG language
11
features, From : .K.1. Clark (Ed.), 12.&i:..Q. Programming, Vol.
16, 1982, pp. 253-266.

17, Ackerman, W.B., "Data flow languages," Computer, Vol. 15,


no.2, 1982, pp. 15-25.

18. Cohen, P.R, & Feigenbaum, E.A., The Handbook of AI, Vol .3.,
William Kaufmann, Los Altos, California, 1982.

19, Clark, K.L, & McCabe, F.G., Micro-PROLOG Programming .in


Logic, Prentice Hall, Englewood Cliffs, N,J, 1981.

20. Manna, Z., Lectures .Q1l the logic of Computer Programming,


SIAM, Philadelphia, 1980.

116
I. Using PROLOG.

I .1 Get ting started.

A description of how to use the interpreter will be based on

the assumption that the user is running it on the DEC20.

The user must first have a copy of the interpreter. At present

(May 1985) it is called prolog. pas. A copy can be obtained from

Professor s. Gulden
Department of Computer Science
and Electrical Engineering
Lehigh University

Also required is a PASCAL compiler/interpr eter for PROLOG to run on.

If all these requirements are met then the user can start
PROLOG by typing

ex prolog, pas

Alternatively, the user can run the prolog.exe file in this author's

directory by typing

<davison>prolog

No matter which method is used once one of these commands has been

typed the system will load and link the program and then print

output :

The user should type <er>. The program will then begin

LEHIGH-PROLOG 1985

117

. . . . ' . . . . . . : . . '" . . .
Printeval ? n
Helpfulprinting ? N
Timer? Y

Three questions will be asked, The user should reply by typing a

single letter answer for each one which should be Y(y) or N(n), No

<er> is needed after the letter, The prompt will then appear

The user can then consult a file

I ?- consult( 'file1 ,in'), <er>

or type a clause

I datum(2). <er>

or ?- hello. <er>

When a question has been satisfied the interpreter will wait for the

user to decide what to do.

e,g
?- beef_stew(X), <er>

X =2
i
cursor

The user can type <er> to finish the question, or 1; <er>' to

resa tisfy the question.

When built in predicates such as get(X) are being used which

require input, the prompt will not appear, and the cursor will

118
remain at the left hand side of the screen waiting for input.
e.g
I ?- get(X). <er>

i
cursor

After typing input for these kinds of predicates , no <er> is needed.

To leave PROLOG, the user can type

I $ <er>
or

I <ctrl>c

PROLOG files must finish with an end of file symbol "'. if they

do not then the interpret er will give an error and stop execution.

File names are also limited to 9 characters and must be in quotes.

See chapter 6 for the section on I consult' for more details.

I.2 Common mistakes.


1. Always finish a PROLOG clause with a period I . . .If none is
I

supplied the interprete r will consider the next line to be a


continuat ion of the previous one. Such an error explains why no

response occurs when the user types

?- consult( 1 dumbo.in') <er>

119
The interp reter is still waitin g for a ' '

2. Missin g quotes can cause large segments of code to be

ignore d. Thus

?- consul t( 'micke y. in). <er>

?- consul t ( oops). <er>

quote,
will cause nothin g to happen . All the input, from the first

will be consumed as the name of the file in the first


I consul t 1 • By

g
the third line the interp reter is still waitin g for the closin

quote to the file name.

3. Missp elt names can cause strange failur es of questio ns.

e.g
bingbo ng( 1).

bingbo ng( 2) •

ringad ing(X) :- binbon g(X).

?- ringad ing( Q).

no

The rule for 1 ringad ing 1 misspe lls 'bingb ong 1 as


1 binbon g'. Since

there are no 'binbon g 1 facts, the ringad ing questio n fails.


1 1

4. Reserv ed words and symbols can cause errors if they are used

as ordina ry words and symbol s. To get round this, the words or

120
quo tes
symbols must be put in sin gle

e.g
I is( 2,4 ).

d wri te
is a bui lt in pre dic ate . Ins tea
wil l cau se an err or sin ce IS
I 1 is 1 (2, 4).

121
II. Err or me ssa ges .
from the
message tha t can. be iss ueq
There are 3 typ es of err or

int erp ret er


syn tax err or me ssa ges

bu ll t in pre di ca te
err or messages

and fai lur e messages

.
II. 1 Syn tax err or me ssa ges
s wi th an
are in the for m of number
The syn tax err or me ssa ges
or qu est ion tha t
ow tha t app ear s und er the pa rt of a fac t, rul e
arr
is syn tat ica lly wrong.

e.g
I num 23) .
Cla use ign ore d

"'75

or
l ?- num 23 ).
Qu est ion ign ore d
"75

numbers fol low


The meaning of the syn tax err or

Number Meaning

"f Int eg er is too lar ge

Only pa rt of 1: , , I typ ed
2

Only pa rt of 1\: I typ ed


3

122
4 Only part of '?-' typed

5 Only part of ':-' typed

10 Expected an identifier e. g hello

11 Expected a variable e.g X1

12 Expected an integer e.g 17

13 String e.g n .... n


14 Questionop '?-'
15 Ruleop 1
:- '

16 Leftparent 1( 1

17 Right parent f) I

18 Comma I , f

19 Left bracket ' ['

20 End-of-file symbol 1"1

21 Right pracke t ']'

22 Headop 'I'
23 Period '. 1

75 Current symbol in wrong place

II.2 Built in predicate error messages.

The 2nd type of error message is the built in predicate error

message. These messages are in the form of sentences preceded by the

word 1 Error 1 •

Given below is l:,I. list of sentences printed and the procedures

123
from where they originate. It is obvious from the message or

messages printed which predicate has failed. The sentences are in

alphabetic al order.

Sentence From procedure

'; predicate not built in,


1 xdisjunc
CON SULT FRED. IN

1st CLAUSE argument must be xclause


a predicate

1st NAME argument illegal xname

2nd argument of IS must be xis


an operator

2nd argument of NAME must xname


be a list

2nd NAME argument illegal xname

2nd NAME argument must be xname


a list

Arguments must be integers xnonarith

Argument must be numerical xtab


in TAB

Argument of PUT must be xput


numerical

Arithmetic arguments must getvalue


be integers

Assert fails xassert

Atom not allowed as argument xget


of GET

Atom not allowed as argument xgeto


of GETO

Atom not allowed as argument xskip


of SKIP

124

~·, .. ... • ' • • • • \ ~ • l- :



CALL :fails xcall

CON SULT fails xsee

Input must be alphanumeric xread


in READ

List must be numeric in NAME listtoatom

List not allowed copiedid

List not allowed as 1st xuniv


argument of '= •• I

List not allowed as 1st xfunctor


argument of FUNCTOR

List not allowed as 1st xname


argument of NAME

List not allowed as 1st xname


NAME argument

LISTING fails xlisting

NAME argument illegal xname

NOSPY argument must be a xnospy


predicate

NOSPY predicate argument xnospy


must be numeric

NOT predicate not built in, xnot


CON SULT FRED. IN

Number not allowed copiedid

Over:flow in "+" calcarith

Over:flow in "*" oalcarith

Predicate not allowed in xsee


CONSULT

Predicate not allowed in NAME atomtolist

125
. --... ~--..,.._....,-~--
~~,..,...·-----· P--~ 0
-
• -""'
~

4 C,0 " -- -~ -· •

Relational arguments must xcompare


be integers

RETRACT fails xretraot

SPY argument must be a xspy


predicate

SPY predicate argument must xspy


be numeric

Underflow in "-" calcarith

Uninstantiated list in NAME listtoatom

Uninstantiated var copiedid

II.3 Failure messages.


There are only 2 messages of this type. They are

IIIILINE INCOMPLETEIH
printed in procedure •nextterminalch'

and
IIIIFJLE INCOMPLETEIH
printed in procedure·•nextfilech'

They both cause the interpreter to cease execution.

126
III. Extended BNF.

See chapt er 2 for a discus sion of some of these defin itions

line : := ( fact I quest ion I rule ) '. •

term : : = consta nt I variab le I struc ture


list I string I predic ate I
'(' argpa .ttern ')'

struct ure ::= atom 1( 1 term { ',' term} ')'


atom
T
not a p_atom

questi on : := 1 ?-' ( struct ure I predic ate )


{ ',' ( struct ure I predic ate ) }

fact : : = struct ure

rule : := struct ure ':-' ( struct ure I predic ate )


{ ',' ( struct ure I predic ate ) }

comment ::= '/*' { all_c har} '*/'

argpa ttern : := argfa ct I argqu estion I argru le

argstr uctur e
: := (atom I varid ent) 1 ( ' term { ',' term } •) • I
( atom I varid ent )
l
includ ing p_atoms

a.rgqu estion : : = 1?-' argstr uctur e { ',' argstr ucture }

argfa ct .. - argstr uctur e

127
ar gr w. e .. - ar gs tru ct ur e ': -' ar gs tru
ct ur e
ar gs tru ot ur e }
{
'''

co ns ta nt ...·-- atom I in te ge r

ar }
..··-- ( I up pe r_ ca se _c ha r ) { ch
va ria bl e '
li st .. - '['I [ I
term {
'] '
''' term }
(( "' te rm )I /\) '] ' I

st ri ng ··-
,, - '" ' { al l_ ch ar } rn '

I
ter m { ', 1 term } ')
1
1 ('
pr ed ic at e : := p_atom
p_atom

..··-- ar } I
atom lo we r_ ca se _ ch ar {I I ch
I I I { al l_ ch
ar } I

rds & symbols.


p_atom .. -- bu ilt in pr ed ic at e wo
se e 4 th appendix

in te ge r : : = di gi t { di gi t }

ch ar ··-
,, - upper_o as e_ ch ar
lo we r_ oa se _c ha r
di gi t I
t I

::= A I
1 1 1 B1 I .. .. I ,z,
up pe r_ ca se _c ha r

1 a1 I 'b ' I .. .. I 'z '


io we r_ ca se _c ha r : :=

di gi t ..··-- '0 ' I '1 ' I .. .. I '9


'

128
. ,,._.:;"'.... \, . ' ' . .

all_char : : = DEC20 character set

129

. . - .. . J't .
IV. Built in predicate words and symbols.

All the words and symbols used, here are reserved. If a user

wishes to use the word asserta, for instance, as a name for an


ordinary fact, he must write

•asserta 1 (12).

and not

asserta( 12).

which will give a syntax error.

Similarly, a user can write

1\: I (jim) •

but not

\=(Jim).

When a predicate word or symbol is used, it must have the right

number of arguments or an error will be output.

e.g
?- asserta( fact(2) ). is correct
but

?- asserta( fact(1), fact(2) ). is wrong.

So for each word and symbol listed, its number of arguments (arity)

will also be given.

130
IV.1 Predicate words.

·Predicate A:ri ty Predicate Arity

arg 3 asserta 1

assertz 1 atom 1

atomic 1 call 1

clause 2 consult 1

debugging 0 display 1

fail 0 functor 3

get 1 getO 1

integer 1 is 2

listing 1 mod 2

name 2 nl 0

node bug 0 nonvar 1

nospy 1 not 1

notrace 0 put 1

read 1 repeat 0

retract 1 Elkip 1

spy 1 tab 1·

trace 0 true 0

var 1 write 1

131
IV .2 Predicate symbols.

Symbol Arity Symbol Arity Symbol Arity

=< 2 -- 2 - .. 2

= 2 >= 2 > 2

--
\-- 2 \: 2 I 2

< 2 + 2 2

• 2 0 2

IV. 3 Other symbols.

If' other symbols are to be used as atoms then it is still

necessary to put them into quotes. This is purely to make the syntax

of this PROLOG simple. Thus

%( 1).
will give a syntax error but

'%'(1).

is correct.

' can not be used as an atom.

132
V. Files .

this
What will be given here is a quick summary of the files in
wishe s
autho r's direct ory at the curre nt time (May 1985). If a user

access to any of these he shoul d get in touch with


Profe ssors. Gulde n
Department of Computer Science
and Elect rical Engin eering
Lehig h Unive rsity

All files with the '.in' postf ix are PROLOG files.

File Purpo se

arit.i n Tests of arithm etic predic ates

ass.in Tests of asser ta, assert z

atn.in An ATN program

call.i n Te st s of call

chart .in An active chart parser

clas.i n Tests of var, nonvar, atom,


integ er, atomi c

clau.i n Tests of claus e, listin g, retrac t

comp.in Tests of <, >, >=, =<


cut.in Te st s of I Ccut )

diag.i n Tests of predi cate diagn ostics

elis.i n An elisa program

eq.in Tests of =, \=, ==, \==


exap. in Tests of patte rn matching and
backtr acking witho ut built in
predic ates

133
fini.i n A finite state automata parse r

rune. in Tests of functo r, arg, =•• (univ)

more. in More tests of PROLOG but using


bull t in predic ates

name. in Te st s of name

nott.i n Tests of user define d •not•

or.in Tests of user define d •or•

par.in A program that execu tes senten ces


input in list form

pred. in The file for user define d PROLOG


predic ates

prolog .pas
.qas The PROLOG interp reter
.rel
,exe

read.i ii A program that reads in senten ces

rapt.i n Tests of repea t

rev.in A program to create and revers e lists

. rewr. in Tests of getO, get, skip, put, nl,


displa y, write

talk.i n A program to read in senten ces and


execu te them

134
Vita.

My name is Andrew Davison. I was born in Macclesfield, England

on July 23rd 1962. My parents are Stanley and Mary Davison.

As an undergraduate, I attended the University of Manchester

Institute of Science and Technology (UMIST). I was there from

October 1980 until June 1983 and I obtained a 1st class B. Sc Honours

degree in Computation.

I have been at Lehigh University since September 1983 and will

complete my M.S in Computer Science in June 1985.

135

You might also like