pul tto i|U4‘ry dtfrcih lo Hic interpreter. Ii will fiml whcihcr n valuc of Z cwm mia which ilu* atprcsaoa u lnu* Setne PROLOG interpreter* liiui all cwiMeniiałly qun?ifi^ ulue>. ( PROLOG rełjuircs rrpcated user prompis (;) to gol nil volues.
14.1.2 Creating, Changing, and Monitoring the PROLOG Emironment
In creafrng a PROLOG program the databasc ot' spccifications is crcatcd fira. In an n&:. aotivc emironment the prediente asserl adils new prcdicatcs to specitientions. THuk
?• asserI(JtXe$(david. sarah)).
jJd< Hus prcdicjtc to the cwnputing spccifications. Now. wjfii the qucry:
?• Ilkes(davkł. X).
X" sarah.
u returned. assert allows further contro! in adding new spećifications to the daotmr assorta(P) asscrts the [Mienie P ni the beginning of all the prcdicatcs P. and asserttfi adds P nt the end of all the prcdicatcs named P. This is importanl for scarch prioritiesni bujlding bcuristics. To remów a prcdicate P Ironi the databasc retract(P) is used (I: should be noted ihai in many PROLOG* assert can be unpredictablc in ihat the axi ctfry timc of the new prcdicate into the em ironment can vary depending on utai ode thmg* art gmg on. aflcctmg both the indaing of asserted elasues and backtncfciagl li soon bccome* tedious to crcalc a set of spccifications using the prcdicatcs assert ind retract. Imtcad. the programmer takes her favorite cditor and creates a file cotaśą all the PROLOG spccifications. Oncc this filc is crcatcd (lets cali it myfile) and PROLOG is callcd (hen the filc is pluccd in the databasc by the PROLOG command consull Ibs
?• consultfmyfile).
p
adds the prcdicate* in myfile to the databasc. A short form of the COnsuR predk*.*1 better for adding multrplc filcs to the databasc. uses the list notation, to be scen sbortf
P
The prcdicate* read and write arc important for user communication. read(X) td* the ant term from the current input stream and binds it to X. Input cxprcssion* are **** ruted «nh awrile(X) puls X in the output stream. If X is unbound then an integetfi' ccdcd by un uaderline is printed l _69i. This integer represents the intcmal boottjęr^ oa vaniMes neccstary in a theorem-prm ing emironment (sec how variab\e$ arc i:tJ aport in Seata 12.2.2).
The PROLOG prcdicatcs SM and tell are uscd to read information from aai f*** infwnution mvi filc* sm(X) opens the filc X and defmes ll« eunent input \treaO ^
PART yi / LAMGUAGES FOR Al PROBLEM SOLVING
originafing in X. If X is noi bound to an availablc lilc SM(X) faiii Similarly. tolf(X) opos a filc for Ihc output stream. If no filc X cxists. tell(X) crcatcs a filc named by the bound (lilie of X. seen(X) and told(X) close the respeethe files.
A numbor of PROLOG prcdicatcs are ńnportant in hclping us kccp track of the State of the PROLOG databasc as wcII as the stale of computing about the databasc; the most imporunt of thcsc are listing, tracę, and apy.w? usc listlng(predicate_namo) w herc predicatc narne is the name of a prcdicate. soch as member (Section 14.1.3;. all the etanes with that prcdicate name in the databasc are returned by the interpreter. Notę that ihc aumber of arguments of the prcdicate is not indicałed; in fact. all ases of the prcdicate. ptgardless of the number of arguments. are returned.
tracę alluws Ihc user to monitor the progres* of the PROLOG interpreter. This monitoring is accompłishcd by printing lo the output filc every goal that PROLOG attemptc. •kich is often morę Information than the user want* to have. The tracing facilitics in many PROLOG cmironmcnts are rather eryptie and lakę same study and expcńence to ■Mfcntand. The Information avai labie in a trące of a running PROLOG program usually ineludes the followjng:
1. The depth !cvcl of rccursńr cali* (marked lefi to rigbt oo linek
2. Wben a goal is tried for the first time (sometńnes cali is usedk
3. When a goal is succcssfully satisfied (with an exit).
4. When a goal has further matches possiblc (a retry).
5. When a goal fails bccausc all attempts to satisfy it ha\c failcd (fali is often uscd).
6. The goal notrace stops the cxhaustiVc tracing.
When a morc sclcc(ivc tracę is rcquircd the goal apy is useful. This prcdicate usually takes a prcdicate name as argument but soinctimes is defincd as a prcfix operator where ihc prcdicate lo be monitored is listed a Her the operator. Th u*, spy member causcs the intcipretcr lo print to output all uses of the predicatc member. apy can also tako a list of prcdicatcs followed by tbcir aritics; spy[momber/2, append/3] sets monitoring of the ■tcrpreicr on all uses of the goals member with two arguments and append w ith three. nospy rcmoves ilicsc spy points.
The prcróus subsections presented PROLOG synux in scvcral simplc cxampfcs. Thcsc Wampie* introduccd PROLOG as an enginc for computing with prcdicate calculus opressłons (in Horn elause form). This is cottsistcnt with all the principlcs of prcdicate colculus mfcrencc presented in Chapter 2. PROLOG uses unificalion for panem matching ad returm the bindings that make an cxprcssion tnie. Thcsc vałucs are unified with the tarubłes in a particular ccprcssion and are not bound in the glohal cnvironmcni.
Rccursion is the primary conirol mecłunism for PROLOG prograiraning. We will demonsiriile this with scvcnl cumplci But first we coowkr sonie simplc li*t-proccs*ing mapie* The list is a data structure consisling of ordered sets of dcments (or. indccd. faul. Rccursion is Ihc natura! way to process the list structure. Uolfication and rccursion
CHAPTER 14 / AN JNTROOUCTIOŃ TO PROLOG 609