The Design and Implementation of A PROLOG Interpreter
The Design and Implementation of A PROLOG Interpreter
Lehigh Preserve
1985
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
of Lehigh University
Master of Science
in the
Lehigh University
1985
This thesis is accepted and approved in partial fulfillment of
?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.
3
2. Syntax.
form (EBNF) in the third append ix. In general , the syntax is exactly
will only become a nuisanc e when arithme tic is being carried out.
Thus
X is 10 + Y - Z
must be written as
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
.( x, .(y,[]) )
4
[x,y]
converting
[x,y]
to • (X 1 • ( Y1 [ ] ) )
output.
?- asserta(fac t(1)).
?- 1 asserta 1 (12).
will call the ordinary fact, or rule, called asserta. All bu:il t in
Thus
5
'l- asserta(fact (2)). is correct
package built on top of the basic PROLOG, That is what could be done
here, if required.
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).
and spaces are ignored in between words, numbers and symbols but
or hel lo(mary).
characters.
'?-'
The set of predicate symbols is defined in procedure
there with the 'predica teop' set being the union of these.
artificial (intelligence)
will become
artificia(intell ige)
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.
are correct.
?- test( ?-X,Y ).
?- X,Y
or ?- test( (X)).
are both correct. Parentheses are not ignored when pattern matching
test ((a))
would cause
?- test (X)
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
.
10
fact and rule ---> procedure factorrule
11
3. Data Structures.
diagnostics.
This is the structure that contains the parse of the input line
12
An identi fier node consis ts of its name ( 'idnam e'), and the
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
used by the predic ate '1 1 ( 1 cut 1 ) . This flag can stop anothe r match
hangin g from it. This speeds up the search for a tree. Trees are
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
e.g
?- test(X ).
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
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
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
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
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
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.
·-1 3
I
A
~ l h , root
?-
I
0.'ffLhJ
I
C.-----J----,.--)
I I I
i~i-i-· i-i-r-· ~
Q..~c. de .F
·- ·-
I ·-
I ·
_... _~·-··- (.l. b C. ... ........ -_
~
--··"··
16
Q
00
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. '/.
value s of these data struc tures . They are in the inter prete r as
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]
20
4. Backtrac king.
this interpre ter. One example will be given, using the notation
interpre ter which do these things will be named. For the moment only
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 ]).
X= [] Y= [a,b,c]
X= [a] Y= [b, c]
X= [a,b] Y= [c]
X= [a,b,c] Y= []
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
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
[[ ] 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 __ ___. ---·- ···----. -· ···--- ... ~ ····--#'•,
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
I· I
>< y
~ X
I
0/ 00 11
y matches X,
24
X '/ X
II
X oo
i-·-·-· Y)Jarch es X
q b C.
. ;-j-j-'
G b C.
X '/ Jr-. X
01 o, II
X
·---· _......... '. ·-·-.
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
hand branch leaving this functor or leaving the matched rule, the
printed.
X = X(01) = • = []
y = ·-·-·-· = [a·,b,c]
I
a b C
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
this case it is the very first functor node. Its 'numofmatc h' is set
Then the var list is cleaned up. All var list elements which have a
26
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
place the functor •append' in the question tree set its pointer,
the datatbase starting from just after this rule. In other words,
the ·first 'append' rule will not be matched with again. Now the 2nd
qppend-------- clb
!
c.--1-1-)
.
.• -
'
•-e, (
I I
·,-D I
qf penJ
I ·1
A A c.-J-1-)
.I I I
B (, D
var list.
28
7-
1
~r piu,d
r
C-J-1-) 9
I I I
X y ·•-·-·-·
I I
~ b C.
So
X matches ·-B
I
A
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
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
c.- ,-,- ) .-
I I I I ~ 2nd A.Incl-a y-
·-· -·- · "'ffend
I
c.-, -,-)
I I I
(, C. D
32
a.y~inJ
db
I
C-,-,-)
I I I
X X
B matches I , I
C matches X
D matches X
G~vin~
;-;-·
b (,
(. D X
,, II 22
1
When B matched with , ', B got a copy of the ',' and B, s mtnum 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
Y: = [b, c]
Gi vin g
34
'-1- ,
I I
b C.
X 't A C D
01 co II It II II
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.
?- _/ 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
?-
t
a.rr~J DJ X y
I
C.. -J-i '-)
oo 00
I I I
X 'j ·-·
I I
-·-·
0.. ~ C.
37
of I no 1 , delete the questio n tree and the var .list and prompt for
call to procedu re •answer •. The questio n tree and var list have
succeed s then the variabl es are printed out and then the questio n
printed . Procedu re •reansw er• is like procedu re •answer ' but has a
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
'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
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
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
matching occuring.
chapter 5). If this is successful and 'match' is true then the right
40
being executed (see chapter 6) ,
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
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
41
5. Patt ern Matc hing.
Cons ider
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
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
question tree with that of the database rule. When these match,
•-• matches
I
0.
,-, matches X
Q matches X
the complication arises when 2 arguments are matched which are made
up of variables.
43
up
n, fact or rul e which can be made
par ent hes es and can be a que stio
ch is of the form
An argu men t may also be a lis t whi
?-te st([ X,Y ]).
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
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
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
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
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
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.
Y---,
I ·1- . . . . .......
C.-·-··
II
'
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 .
Case A.
Q node and db node with no chil d
bran ches
i.e
q db
48
-inst evaluate(qvs,db,match)
q ident db ident does q = db ?
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
49
Case C.
Both q and db have 1 or 2 children.
i.e
~"
I 7.
·1"'..
50
and
C5 9 db,
C4.
9 db I :-
1"- ,_
\,,- I'·-'
(.,
C.
I
c..
same. We shall assume such tests will be done in the code and so
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)
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)
5 .2 Location of Code.
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
Some extra tests are done to deal with symbols which are to be
following procedures
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
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
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
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
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
and so on,
)( I :: b
I
c..-)
I
V
f
X2 = ·Z
arid
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, .. .
val(2).
num(val).
to obtain
X = val
F = val(2)
In both cases, 'call' has executed the goal 'val' to find a value of
to get
X = val
y =2
F = val(2)
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)) )
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
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
66
. . . ... '
." ~
... . . . . -~
s-------- ,-
I
)
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
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.
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
and
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
68
6.3 Clause.
clause ( ( Z (X)), Y)
then X1 = (Z(X))
newx1 = p(X)
which is the head of the rule (or fact) and X2 will contain its
that X2 will only return the first subgoal of any right hand side.
Thus if there is a rule
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') .
inpu t file .
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 ' ] •
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.
'\
.
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
72
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
?- 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
73
Similarly, 'list toa tom I Will convert something like
i_i_i_. to abc
97 qg q'f
6 .8 Functor.
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
wi ll give
X= a
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 ] ]
a lis t.
75
Foo
I ba.c.oma.s I
C-'-1-)
foo t\ b
I I
0. b
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.
.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
?- spy( sort ).
sort , 0
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.
questio n is being treated differe ntly. They are the 4 places that
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
?- des(X) ,fail.
Then when the goal I fail I is reached , the followi ng will be printed
CALL fail
FAIL fail
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
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
the n
81
FAll. c(X)
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
interpreter itself. They were used during development and have been
These are
printeval
helpful printing
and timer
the type
Swop q & db for qend
and
82
eva luat e
83
procedure. Thus if q is nil then
ptree(q"' .left)
debug system [5] and getting the values of the data structure by
variable =
e.g
q =
q"'.varval =
84
. • . - - - .. t.____ . () .. • . .
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.
The run time of questio ns is calcula ted using the DEC20 built
8.2 Input.
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
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 •
85
function 'try again, •
1 I
Input is the variable that is assigned the names of the files
something, input will be taken f'rom the keyboard and the question
will be able to continue.
86
9. Comparisons with UNH PROLOO.
version that was used by the author is located in the CAE lab in the
WI/ 10000 under AOS/VS. Unfortunately, the modifications had not been
The syntax used is like that used in the Edinburgh DEC 10 PROLOG
Lehigh PROLOG. Unlike the Edinburgh PROLOG it does not compile any
Lehigh PROLOG, This did not cause any problems in the tests that
87
9.1 Timing comparisons.
small and medium size programs ran at about the same rate. On large
Both interpreters slowed down but Lehigh PROLOG was almost 15 times
slower. Two of the large size programs used were an elisa program
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
rules to be kept in. mind when writing a program for Lehigh PROLOG.
e.g write
?- recognise(S).
?- parse(T).
iristead of
?- recognise(S),par se(T).
88
through arguments.
These rules will keep the question tree and var list size down
9 .2 Ease of use.
The UNH PROLOG starts very simply, The user must type
x prolog
?-
first type
?- [user], <er>
89
list form.
If the user wants to consu lt a file, he can use the
?- [ 1 atn.i n 1 ] , <er>
e,g
?- [vix] . <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
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
2 <5 • is corre ct
but
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).
':- '(2,3) .
a lot
UNH PROLOG allows real and negati ve numbe rs. It also has
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
92
• • •'
~
I I - .._ • • a - •:
10. Improvements.
practise this user has only found 'op' to be needed in some cases,
X is 5 + Y I 2
10 .2 Language Modules.
systems this user has seen some of the other versions of PROLOG
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
are restric tions on the types and number of paramet ers that can be
it did not work on the Data General MV/10000 that this PROLOG was
on.
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
increas ed modula rity and extens ibility for PROLOG as well as the
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
bago f(X,P ,B)
s.
e.g
?- seto f(X, X like s Y, S).
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
exponent . ·- integer I
1 + 1 integer
'-' integer
atan(X)
X /\ y bitwise conjunction
X \I y bitwise disjunction
96
and negative numbers. The code for the predicates would be similar
deal.
typing
ex prolog. pas
predicates
Listing Clause
Call Consult
97
.. ; ·.. :... . ' ..;. . . . ' ··., ,
Skip ·aet
Get GetO
Tab, nl Put
Nonvar Var
Atomic Atom,integer
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
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
resa ti sfied.
becomes
99
?--
1
-r
l
C,-l-)
l I
X y
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
wou ld be come
?~ ~oor
I\
101
The numbering of the goals is not relevant.
•matched' or 'failed'.
A 1 fresh I goal is one that has not yet been matched with any
clause.
database clauses.
102
Given below is pseudo-code for searching a tree and also for
backtracking along a tree,
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 *)
'answer' which would try to find an answer for the current question
tree.
104
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.
?-Root
1 1
The goals t and 's' are initially 'fresh'.
105
. ' ·. - 1: . l:,-- ' • • ., .• •
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).
question.
e.g
for a question
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'
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
107
1
replaced in the current interpreter would be 'answer' , 'reanswer ,
Also implicit in this new notation is the fact that the actual
e.g
with clauses
1. append([],X,X).
and a question
?- append(X,Y,[a]).
c-)-J-) .-
I yI ,-,
I I
X a.ninJ
I I
G--1-,->
I I
e, C 0
108
which be comes
, like so
All this can be repr esen ted by poin ters
?-Roo \-
1
qpp(!/11J
I
afyUid
109
In the current interpreter, pointers are already used to access
goal.
that case, problems with dependencies between data would arise. The
goals each waiting for each other to finish before they could
rule
sameleaves(X, Y) :- profile_of(W,X),
profile_of(W, Y).
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
111
.. • ~ ~l~l'"~~ •• • • • • ••
11 • Conclusio ns.
clarity. Also since PASCAL was used, the interprete r can be modified
than most other PROLOGs, programs can be written which are much
112
Combin ed with C&M it can be used to teach PROLOG. Also because of
PROLOG, just how powerfu l the languag e is. There are many things
paralle lism and pattern matchin g it provide s all the tools needed
require d [20].
113
possibility and is as quick and convenient as more conventional
114
• ~._ -• ~- • • • ' , ..,1•.i o<, ,
References.
115
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.
18. Cohen, P.R, & Feigenbaum, E.A., The Handbook of AI, Vol .3.,
William Kaufmann, Los Altos, California, 1982.
116
I. Using PROLOG.
Professor s. Gulden
Department of Computer Science
and Electrical Engineering
Lehigh University
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
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
or type a clause
I datum(2). <er>
or ?- hello. <er>
When a question has been satisfied the interpreter will wait for the
e,g
?- beef_stew(X), <er>
X =2
i
cursor
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
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.
119
The interp reter is still waitin g for a ' '
ignore d. Thus
quote,
will cause nothin g to happen . All the input, from the first
g
the third line the interp reter is still waitin g for the closin
e.g
bingbo ng( 1).
bingbo ng( 2) •
no
4. Reserv ed words and symbols can cause errors if they are used
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
bu ll t in pre di ca te
err or 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
Number Meaning
Only pa rt of 1: , , I typ ed
2
122
4 Only part of '?-' typed
16 Leftparent 1( 1
17 Right parent f) I
18 Comma I , f
22 Headop 'I'
23 Period '. 1
word 1 Error 1 •
123
from where they originate. It is obvious from the message or
alphabetic al order.
124
125
. --... ~--..,.._....,-~--
~~,..,...·-----· P--~ 0
-
• -""'
~
4 C,0 " -- -~ -· •
IIIILINE INCOMPLETEIH
printed in procedure •nextterminalch'
and
IIIIFJLE INCOMPLETEIH
printed in procedure·•nextfilech'
126
III. Extended BNF.
argstr uctur e
: := (atom I varid ent) 1 ( ' term { ',' term } •) • I
( atom I varid ent )
l
includ ing p_atoms
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
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
128
. ,,._.:;"'.... \, . ' ' . .
129
. . - .. . J't .
IV. Built in predicate words and symbols.
All the words and symbols used, here are reserved. If a user
•asserta 1 (12).
and not
asserta( 12).
1\: I (jim) •
but not
\=(Jim).
e.g
?- asserta( fact(2) ). is correct
but
So for each word and symbol listed, its number of arguments (arity)
130
IV.1 Predicate words.
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
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.
=< 2 -- 2 - .. 2
= 2 >= 2 > 2
--
\-- 2 \: 2 I 2
< 2 + 2 2
• 2 0 2
necessary to put them into quotes. This is purely to make the syntax
%( 1).
will give a syntax error but
'%'(1).
is correct.
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
File Purpo se
call.i n Te st s of call
133
fini.i n A finite state automata parse r
name. in Te st s of name
prolog .pas
.qas The PROLOG interp reter
.rel
,exe
134
Vita.
October 1980 until June 1983 and I obtained a 1st class B. Sc Honours
degree in Computation.
135