home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd3.lzh / SBPROLOG2.2 / DOC / sbprolog_doc.me next >
Text File  |  1991-08-10  |  146KB  |  4,507 lines

  1. .ds f. init
  2. .nr _0 \n(c.
  3. .\"  Setup for thesis.
  4. .\"    This file should be modified to keep up with the standard
  5. .\"    for a doctoral thesis at Berkeley.  Other macros which may
  6. .\"    be useful for a thesis are defined here.
  7. .\"    @(#)thesis.me    2.1    8/18/80
  8. .
  9. .\" This version is updated to reflect the ridiculous margin requirements of
  10. .\"    the graduate school at Stony Brook.
  11. .\" It also has comments supplied by PKH to allow for easier upgrading in the
  12. .\"     future.
  13. .
  14. .
  15. .if n .if \n(_o \
  16. .po 1.00i            \" nroff with page offset, reset to 1.5 inches
  17. .if t .po 0.85i            \" want 1.5 Canon and Troff loose .25
  18. .ll 6.5i            \" for precise 1.5 margins right and left
  19. .if n .if 1n=0.1i \
  20. .    ll 8.8i            \" for nroff and pica, line has even # of char
  21. .lt 7.5i            \" pageno close as possible to 1" off rt edge
  22. .if n .ls 2            \" double space for nroff (was orig. for both)
  23. .
  24. .\" time to reset some "PARAMETRIC INITIALIZATIONS"
  25. .
  26. .ie t \
  27. \{\
  28. .                \" for troff and Canon printer--fudges
  29. .nr hm 5i/12        \" page number offset 5/12{8} inch top{8}
  30. .nr tm 1.00i        \" margins 1.00{1.5} inches top{1.5}
  31. .nr bm 1.00i        \" and 1.00{1.5} bottom{1.5} 
  32. .nr fm 5i/12        \" page number offset 5/12{8} inch bottom{8}
  33. .                \" nullify cut mark macro for canon
  34. .rm @m
  35. .\}
  36. .el \
  37. \{\
  38. .nr hm 5i/12        \" page number offset 5/12{8} inch top{8}
  39. .nr tm 1.00i        \" margins 1.00{1.5} inches top{1.5}
  40. .nr bm 1.00i        \" and 1.00{1.5} bottom{1.5} 
  41. .nr fm 5i/12        \" page number offset 5/12{8} inch bottom{8}
  42. .\}
  43. .if t \
  44. \{\
  45. .nr pp 11            \" set pointsize
  46. .nr sp 11            \" set header pointsize
  47. .vs 16                \" line spacing 1-1/2 [12=single @ 10 points]
  48. .nr $r \n(.v/\n(.s        \" ratio of vert space to point size 
  49. .nr $R \n($r            \"      same, but for another use
  50. .nr fp 10
  51. .\}
  52.  
  53. .de IP
  54. .ip \\$1 \\$2
  55. ..
  56. .de LP
  57. .lp
  58. ..
  59. .de SH
  60. .sp
  61. .ti \\n(.iu
  62. .if \\n(1T .sp
  63. .ne 3.1
  64. .ft B
  65. ..
  66.  
  67. .EQ
  68. .nr 99 \n(.s
  69. .nr 98 \n(.f
  70. .ps 10
  71. .ft 2
  72. .ps 11
  73. .ps \n(99
  74. .ft \n(98
  75. .EN
  76. .de $0
  77. \\*(st
  78. .(x c
  79. .if '\\$3'1' \ 
  80. .if '\\$3'2' \ \ \ 
  81. .if '\\$3'3' \ \ \ \ \ \ 
  82. .if '\\$3'4'\ \ \ \ \ \ \ \ \ 
  83. .if '\\$3'5'\ \ \ \ \ \ \ \ \ \ \ \ 
  84. \\$2
  85. .if !'\\$3'' :\ 
  86. \\$1  \\fR
  87. .)x \\n%
  88. ..
  89.  
  90. .de $1
  91. .ds co " \\fB
  92. .ds st \\fR
  93. .br
  94. ..
  95. .de $2
  96. .ds co "   
  97. .ds st \\fR
  98. .br
  99. ..
  100. .de $3
  101. .ds co "     \\fB
  102. .ds st \\fR
  103. .br
  104. ..
  105. .de $4
  106. .ds co " \\fB
  107. .ds st \\fR
  108. .br
  109. ..
  110.  
  111. \" .de lf
  112. \" Figure \\n($1.\\n+(fn\\$1
  113. \" .ds \\$4 Figure \\n($1.\\n(fn
  114. \" .ie '\\$3'p' \{\
  115. \" .nr % +1 \}
  116. \" .el \{\
  117. \" .ie '\\$3'n' \{\\}
  118. \" .el \{\
  119. \" .(z
  120. \" .sp \\$3
  121. \" .ce 100
  122. \" \\$2
  123. \" Figure \\n($1.\\n(fn
  124. \" .ce 0
  125. \" .)z \} \}
  126. \" .(x f
  127. \" Figure \\n($1.\\n(fn
  128. \" \\$2
  129. \" .)x \\n%
  130. \" ..
  131. .ds f. sec0.t
  132. .tp
  133. .nr pp 11
  134. .nr $r 10
  135. .fo ''''
  136. .EQ
  137. .nr 99 \n(.s
  138. .nr 98 \n(.f
  139. .ps 11
  140. .ft 2
  141. .ps 11
  142. .ps \n(99
  143. .ft \n(98
  144. .EN
  145. .pp
  146. \ \ \ 
  147. \ \ 
  148. .(l
  149. .ce 20
  150. \fBThe SB-Prolog System, Version 2.2
  151. .sp
  152. A User Manual\fR
  153.  
  154. edited by
  155. \fISaumya K. Debray\fR
  156. .sp
  157. from material by
  158. .sp
  159. \fIDavid Scott Warren
  160. Suzanne Dietrich\fR
  161. SUNY at Stony Brook
  162. .sp
  163. \fIFernando Pereira\fR
  164. SRI International
  165. .sp 3
  166. \fIMarch 1987\fR
  167. .sp 2
  168. Department of Computer Science
  169. University of Arizona
  170. Tucson, AZ 85721
  171. .)l
  172. .ce 0
  173. .fo ''%''            \" page number on middle of bottom
  174. .bp
  175. .pp
  176. \ \ \ 
  177. \ \ 
  178. .(l
  179. .ce 20
  180. \fBThe SB-Prolog System, Version 2.2
  181. .sp
  182. A User Manual\fR
  183. .)l
  184. .ce 0
  185. .sp 3
  186. .lp
  187. \fBAbstract\fR: SB-Prolog is a public-domain Prolog system for Unix\(dg
  188. based systems originally developed at SUNY, Stony Brook.  The core of
  189. the system is an emulator, written in C for portability, of a Prolog
  190. virtual machine that is an extension of the Warren Abstract Machine.
  191. The remainder of the system, including the translator from
  192. Prolog to the virtual machine instructions, is written in Prolog.
  193. Parts of this
  194. manual, specifically the sections on Prolog syntax and descriptions of some
  195. of the builtins, are based on the C-Prolog User Manual by Fernando Pereira.
  196. .(f
  197. \(dg Unix is a trademark of AT&T.
  198. .)f
  199. .fo ''%''            \" page number on middle of bottom
  200. .bp
  201. .ds f. sec1.t
  202. .lp
  203. .bp
  204. .lp
  205. .sh 1 "Introduction"
  206. .pp
  207. SB-Prolog is a public domain Prolog system based on an extension of the Warren
  208. Abstract Machine\**.
  209. .(f
  210. \** D. H. D. Warren, ``An Abstract Prolog Instruction Set'',
  211. Tech. Note 309, SRI International, 1983.
  212. .)f
  213. The WAM simulator is written in C to enhance
  214. portability.  Prolog source programs can be compiled into \fIbyte code\fR
  215. files, which contain encodings of WAM instructions and are
  216. interpreted by the simulator.  Programs can also be interpreted via \fIconsult\fR.
  217. .pp
  218. SB-Prolog offers several features that are not found on most Prolog systems
  219. currently available.  These include: compilation to object files; dynamic
  220. loading of predicates; provision for generating executable code on the
  221. global stack, which can be later be reclaimed; an \fIextension table\fR
  222. facility that permits memoization of relations.  Other features include
  223. full integration between compiled and interpreted code, and a facility for the
  224. definition and expansion of macros that is fully compatible with the runtime system.
  225. .pp
  226. The following features are absent: we expect to
  227. incorporate them in future versions.
  228. .ip \(de
  229. A ``listing'' feature, which produces a listing of clauses in the system
  230. database.
  231. .ip \(de
  232. A garbage collector for the global stack.
  233. .ip \(de
  234. The \fIrecord\fR/\fIrecorded\fR/\fIerase\fR and
  235. the \fIcurrent_\|atom\fR/\fIcurrent_\|functor\fR/\fIcurrent_\|predicate\fR
  236. facilities of C-Prolog.
  237. .pp
  238. In addition, it should be mentioned that while interpreted and compiled code
  239. behave similarly in almost all cases, there are a few cases that we are aware
  240. of where they behave differently:
  241. .np
  242. In cuts buried within \fIif-then-else\fRs (\->): such cuts are treated as
  243. ``hard'' cuts in interpreted code (i.e. cuts away alternative clauses as well), but
  244. as ``soft'' cuts in compiled code (i.e. cuts as far back as the head of the
  245. clause, but does not cut away alternative clauses).
  246. .np
  247. In the evaluation of arithmetic expressions involving compound subexpressions created dynamically: in compiled code,
  248. this cannot be handled using \fBis\fR/2, and must be handled using
  249. \fBeval\fR/2 (see the section on Arithmetic predicates), while in interpreted code either
  250. \fBis\fR/2 or \fBeval\fR/2 is acceptable.
  251. .bp
  252. .ds f. sec2.t
  253. .sh 1 "Getting Started"
  254. .pp
  255. This section is intended to give a broad overview of the SB-Prolog system,
  256. so as to enable the new user to begin using the system with a minimum of
  257. delay.  Many of the topics touched on here are covered in greater depth
  258. in later sections.
  259. .sh 2 "The Dynamic Loader Search Path"
  260. .pp
  261. In SB-Prolog, it is not necessary for the user to load all the
  262. predicates necessary to execute a program.  Instead, if an undefined predicate \fIfoo\fR is encountered during execution, the
  263. system searches the user's directories in the order specified by 
  264. the environment variable SIMPATH until it finds a directory containing a file \fIfoo\fR
  265. whose name is that of the undefined predicate.  It then dynamically loads and
  266. links the file \fIfoo\fR (which is expected to be a byte code file
  267. defining the predicate \fIfoo\fR), and continues with execution; if no such file can be found, an error message is given and execution fails.
  268. This feature makes it unnecessary for the user to have to explicitly
  269. link in all the predicates that might be necessary in a
  270. program: instead, only those files are loaded which are necessary to have the
  271. program execute.  This can significantly reduce the memory requirements of
  272. programs.
  273. .pp
  274. The key to this dynamic search-and-load behaviour is the SIMPATH environment
  275. variable, which specifies the order in which directories are to be searched.
  276. It may be set by adding the following line to the user's .\fIcshrc\fR file:
  277.  
  278. .(l
  279.     setenv SIMPATH \fIpath\fR
  280. .)l
  281.  
  282. where \fIpath\fR is a sequence of directory names separated by colons:
  283.  
  284. .(l
  285.     \fIdir\fR\*<1\*>:\fIdir\fR\*<2\*>: ... :\fIdir\*<n\*>\fR
  286. .)l
  287.  
  288. and \fIdir\*<i\*>\fR are \fIfull path names\fR to the respective
  289. directories.
  290. For example, executing the command
  291. .(l
  292.     setenv SIMPATH .:$HOME/prolog/modlib:$HOME/prolog/lib
  293. .)l
  294.  
  295. sets the search order for undefined predicates to the following: first, the
  296. directory in which the program is executing is searched; if the appropriate file is not found in this
  297. directory, the directories searched are, in order, ~/prolog/modlib and
  298. ~/prolog/lib.  If the appropriate file is not found in any
  299. of these directories, the system gives an error message and execution fails.
  300. .pp
  301. The beginning user is advised to include the system directories (listed in
  302. the next section) in his SIMPATH, in order to be able to access the system
  303. libraries (see below).
  304. .sh 2 "System Directories"
  305. .pp
  306. There are four basic system directories: cmplib, lib, modlib and sim.
  307. \fIcmplib\fR contains the Prolog to byte code translator;
  308. \fIlib\fR and \fImodlib\fR contain library routines.  The \fIsrc\fR
  309. subdirectory in each of these contains the corresponding Prolog source programs.  The
  310. directory \fIsim\fR contains the simulator, the subdirectory \fIbuiltin\fR
  311. contains code for the builtin predicates of the system.
  312. .pp
  313. It is recommended that the beginning user include the system directories in
  314. his SIMPATH, by setting SIMPATH to
  315. .(l C
  316.  .\|:\|\fISBP\fR/modlib\|:\|\fISBP\fR/lib\|:\|\fISBP\fR/cmplib
  317. .)l
  318. where \fISBP\fR denotes the path to the root of the SB-Prolog system directories.
  319. .sh 2 "Invoking the Simulator"
  320. .pp
  321. The simulator is invoked by the command
  322. .(l
  323.     sbprolog\fIbc_\|\^file\fR
  324. .)l
  325. where \fIbc_\|\^file\fR is a byte code file resulting from the compilation of a
  326. Prolog program.  In almost all cases, the user will wish to interact with the
  327. SB-Prolog \fIquery evaluator\fR, in which case \fIbc_\|file\fR will be
  328. \fI$readloop\fR, and the command will be
  329.  
  330. .(l
  331. .nr 99 \n(.s
  332. .nr 98 \n(.f
  333. .rm 11 
  334. .as 11 "     sbprolog 
  335. .ps 11
  336. .ft 2
  337. .ds 12 "\s-5\b'\(sl\e'\s0
  338. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  339. .as 11 \*(12
  340. .ps \n(99
  341. .ft \n(98
  342. .as 11 " path 
  343. .ps 11
  344. .ft 2
  345. .ds 12 "\s-5\b'\e\(sl'\s0
  346. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  347. .as 11 \*(12
  348. .ps \n(99
  349. .ft \n(98
  350. .as 11 "/\\\\$\$\readloop
  351. .ps \n(99
  352. .ft \n(98
  353. \*(11
  354. .)l
  355.  
  356. .nr 99 \n(.s
  357. .nr 98 \n(.f
  358. .rm 11 
  359. .as 11 "where 
  360. .ps 11
  361. .ft 2
  362. .ds 12 "\s-5\b'\(sl\e'\s0
  363. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  364. .as 11 \*(12
  365. .ps \n(99
  366. .ft \n(98
  367. .as 11 "\fIpath\fR
  368. .ps 11
  369. .ft 2
  370. .ds 12 "\s-5\b'\e\(sl'\s0
  371. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  372. .as 11 \*(12
  373. .ps \n(99
  374. .ft \n(98
  375. .as 11 " is the path to the directory containing the
  376. .ps \n(99
  377. .ft \n(98
  378. \*(11
  379. command interpreter \fI$readloop\fR.  This directory, typically, is \fImodlib\fR
  380. (see Section 2.2 above).
  381. .pp
  382. The command interpreter reads in a query typed in by the user, evaluates it and
  383. prints the answer(s), repeating this until it encounters an end-of-file
  384. (the standard end-of-file character on the system, e.g. ctrl-D), or the user types
  385. in \fIend_\|of_\|file\fR or \fIhalt\fR.
  386. .pp
  387. The user should ensure that the the directory containing the executable file \fIsim\fR
  388. (typically, the system directory \fIsim\fR: see Section 2.2
  389. above) is included in the shell variable \fIpath\fR; if not, the full path
  390. to the simulator will have to be specified.
  391. .pp
  392. In general, the simulator may be invoked with a variety of options, as
  393. follows:
  394. .(l
  395.      sbprolog\-\fIoptions\fR \fIbc_\|\^file\fR
  396. or
  397.      sbprolog \-\fIoption\fR\*<1\*> \-\fIoption\fR\*<2\*> ... \-\fIoption\*<n\*>\fR \fIbc_\|\^file\fR
  398. .)l
  399. The options recognized by the simulator are described in Section 4.2.
  400. .pp
  401. When called with a byte code file \fIbc_\|\^file\fR, the simulator begins
  402. execution with the first clause in that file.  The first clause in such a
  403. file, therefore, should be a clause without any arguments in the head
  404. (otherwise, the simulator will attempt to dereference argument pointers
  405. in the head
  406. that are really pointing into deep space, and usually come to a sad end).
  407. If the user is executing a file in this manner rather than using the
  408. command interpreter, he should also be careful to include the undefined
  409. predicate handler `_\|\fI$undefined_\|pred\fR'/1, which is normally defined
  410. in the file \fImodlib/$init_\|sys.P\fR.
  411. .(x b
  412. (L)     _\|$undefined_\|pred/1
  413. .)x
  414. .sh 2 "Executing Programs"
  415. .pp
  416. There are two ways of executing a program: a source file may be compiled
  417. into a byte-code file, which can then be loaded and executed; or, the source file may be
  418. interpreted via \fIconsult\fR.  The system supports full integration of compiled and
  419. interpreted code, so that some predicates of a program may be compiled, while
  420. others may be interpreted.  However, the unit of compilation or consulting
  421. remains the file.  The remainder of this section describes each of these procedures in
  422. more detail.
  423. .sh 3 "Compiling Programs"
  424. .pp
  425. The compiler is invoked through the Prolog predicate \fIcompile\fR.  It translates Prolog
  426. source programs into byte code that can then be executed on the simulator.
  427. .(x b
  428. (L)     compile/1
  429. .)x
  430. .(x b
  431. (L)     compile/2
  432. .)x
  433. .(x b
  434. (L)     compile/3
  435. .)x
  436. .(x b
  437. (L)     compile/4
  438. .)x
  439. The compiler may be invoked as follows:
  440.  
  441. .(l
  442.     | ?- compile(\fIInFile\fR [, \fIOutFile\fR ] [, \fIOptionsList\fR ]).
  443. or
  444.     | ?- compile(\fIInFile\fR, \fIOutFile\fR, \fIOptionsList\fR, \fIPredList\fR).
  445. .)l
  446.  
  447. where optional parameters are enclosed in brackets.
  448. \fIInFile\fR is the name of the input (i.e. source) file; \fIOutFile\fR is the
  449. name of the output file (i.e. byte code) file; \fIOptionsList\fR is a list of compiler options,
  450. and \fIPredList\fR is a list of terms \fIP\fR/\fIN\fR denoting the
  451. predicates defined in \fIInFile\fR, where \fIP\fR is a predicate name and \fIN\fR
  452. its arity.  
  453. .pp
  454. The input and output file names must be Prolog atoms, i.e. either
  455. begin with a lower case letter and consist only of letters, digits,
  456. dollar signs and underscores; or, be enclosed within single quotes.
  457. If the output file name is not specified, it defaults to
  458. \fIInFile\fB.out\fR.  The list of options, if specified, is
  459. a Prolog list, i.e. a term of the form
  460.  
  461. .(l
  462.     [ \fIoption\fR\*<1\*>, \fIoption\fR\*<2\*>, ..., \fIoption\*<n\*>\fR ].
  463. .)l
  464.  
  465. If left unspecified, it defaults to the empty list [\^].
  466. \fIPredList\fR, if specified, is usually given as an uninstantiated
  467. variable; its principal use is for setting trace points on the predicates in the file (see Sections 6 and 8).
  468. Notice that \fIPredList\fR can only appear in \fIcompile\fR/4.
  469. .pp
  470. A list of compiler options appears in Section 8.3.
  471. .sh 3 "Loading Byte Code Files"
  472. .lp
  473. Byte code files may be loaded into the simulator using the
  474. predicate \fIload\fR:
  475.  
  476. .(l
  477.     | ?- load(\fIByteCode_\|File\fR).
  478. .)l
  479.  
  480. where \fIByteCode_\|File\fR is a Prolog atom (see Section 3.1) that is the name of a byte code
  481. file.
  482. .(x b
  483. (B)     load/1
  484. .)x
  485. .pp
  486. The \fIload\fR predicate invokes the dynamic loader, which carries out a search according to
  487. the sequence specified by the environment variable SIMPATH (see Section
  488. 2.1).  It is therefore not necessary to always specify the full path name to the file to be
  489. loaded.  
  490. .pp
  491. Byte code files may be concatenated together to produce other byte code files.  Thus,
  492. for example, if \fIfoo1\fR and \fIfoo2\fR are byte code files resulting
  493. from the compilation of two Prolog source programs, then the file \fIfoo\fR,
  494. obtained by executing the shell command
  495. .(l
  496.      cat foo1 foo2 > foo
  497. .)l
  498. is a byte code file as well, and may be loaded and executed.  In this case,
  499. loading and executing the file \fIfoo\fR would give the same result as
  500. loading \fIfoo1\fR and \fIfoo2\fR separately, which in turn would be the same as
  501. concatenating the original source files and compiling this larger file.  This
  502. makes it easier to compile large programs: one need only break them into smaller
  503. pieces, compile the individual pieces, and concatenate the resulting byte code files together.
  504. .sh 3 "Consulting Programs"
  505. .pp
  506. Instead of compiling a file to generate a byte code file which then has to be loaded,
  507. a program may be executed interpretively by ``consulting'' the corresponding
  508. source file:
  509. .(x b
  510. (L)     consult/1
  511. .)x
  512. .(x b
  513. (L)     consult/2
  514. .)x
  515. .(l
  516.     | ?- consult(\fISourceFile\fR [, \fIOptionList\fR ] ).
  517. or
  518.     | ?- consult(\fISourceFile\fR, \fIOptionList\fR, \fIPredList\fR).
  519. .)l
  520. where \fISourceFile\fR is a Prolog atom which is the name of a file
  521. containing a Prolog source program; \fIOptionList\fR is a list of options
  522. to consult; and \fIPredList\fR is a list of terms \fIP\fR/\fIN\fR, where \fIP\fR is a
  523. predicate name and \fIN\fR its arity, specifying which predicates have been consulted
  524. from \fISourceFile\fR; its principal use is for setting trace points
  525. on the predicates in the file (see Section 6).  Notice that \fIPredList\fR can only appear in \fIconsult\fR/3.
  526. .pp
  527. At this point, the options recognized for \fIconsult\fR are
  528. the following:
  529. .lp
  530. \fBfirst\fR
  531. .ip
  532. If on, causes each clause read in to be added as the first clause of
  533. the database (so that effectively, the clauses appear in the reverse
  534. order as in the source file).  Default: off.
  535. .lp
  536. \fBindex\fR
  537. .ip
  538. If on, causes an index to be generated on the first argument of each
  539. predicate.  Default: off.
  540. .ip \fBindex(N)\fR
  541. If on, causes an index to be generated on the N\*[th\*] argument of each
  542. predicate.  Default: off.
  543. .ip \fBq\fR
  544. ``quick''.  If on, invokes a consultation algorithm that is simpler and
  545. more efficient than
  546. the general one.  However, the code generated with the simpler algorithm
  547. may not be correct if there are repeated variables within compound terms
  548. in the body of the clause, e.g. in
  549. .(l
  550.     p(X) :\- q([X, X]).
  551. .)l
  552. Default: off.
  553. .ip \fBt\fR
  554. ``trace''.  Causes a trace point to be set on any predicate in the current
  555. file that does not already have a trace point set.
  556. .ip \fBv\fR
  557. ``verbose''.  Causes information regarding which predicates have been
  558. consulted to be printed out. Default: off.
  559. .lp
  560. In addition to the above, the options specified for the macro expander
  561. are also recognized (see Section 10)).
  562. .pp
  563. It is important to note that SB-Prolog's \fIconsult\fR predicate is similar
  564. to that of Quintus Prolog, and behaves like C-Prolog's \fIreconsult\fR.
  565. This means that if a predicate is defined across two or more files,
  566. consulting them will result in only the clauses in the file consulted last
  567. being used.  
  568. .sh 2 "Execution Directives"
  569. .pp
  570. Execution directives may be specified to \fIcompile\fR and \fIconsult\fR through :\-/1.  If, in the read
  571. phase of \fIcompile\fR or \fIconsult\fR, a term with principal
  572. functor :\-/1 is read in, this term is executed directly via \fIcall\fR/1.
  573. This enables the user to dynamically modify the environment, e.g. via
  574. \fIop\fR declarations (see Section 3.2), \fIassert\fRs etc.
  575. .(x b
  576. (P)     :\-/1
  577. .)x
  578. .pp
  579. A point to note is that if the environment is modified as a result of an execution
  580. directive, the modifications are visible only in that environment.  This
  581. means that consulted code, which runs in the environment in which
  582. the source program is read in (and which is modified by such execution directives) feel the effects of such
  583. execution directives.  However, byte code resulting from compilation, which,
  584. in general, executes in an environment different from that in which the
  585. source was compiled, does not inherit the effects of such directives.  Thus,
  586. an \fIop\fR declaration can be used in a source file to change the syntax and
  587. allow the remainder of the program to be parsed according to the modified
  588. syntax; however, these modifications will not, in general, manifest themselves
  589. if the byte code is executed in another environment.  Of course, if the byte code
  590. is loaded into the same environment as that in which the source program was
  591. compiled, e.g. through
  592. .(l
  593.     | ?- compile(foo, bar), load(bar).
  594. .)l
  595. the effects of execution directives will continue to be felt.
  596. .sh 1 "Syntax"
  597. .pp
  598. .sh 2 "Terms"
  599. .pp
  600. The syntax of SB-Prolog is by and large compatible with that of
  601. C-Prolog.
  602. The data objects of the language are called  \fIterm\fPs.
  603. A  term  is either a \fIconstant\fP, a \fIvariable\fP or a \fIcompound term\fP.
  604. Constants can be \fIintegers\fR or \fIatoms\fR.
  605. The symbol for an atom must begin with a lower case letter or the dollar
  606. sign $, and consist of any number of letters, digits, underscores
  607. and dollar signs; if it contains any character other than these, it must be
  608. enclosed within single quotes.\**
  609. .(f
  610. \** Users are advised against using symbols beginning with `$' or `_\|$',
  611. however, in order to minimize the possibility of conflicts with symbols
  612. internal to the system.
  613. .)f
  614. As in other  programming languages,  constants  are definite elementary objects.
  615. .pp
  616. Variables are distinguished by an initial capital  letter  or  by
  617. the initial character \*(lq\(ul\|\*(rq, for example
  618. .(l
  619. X   Value   A   A1   \(ul\|3   \(ul\|RESULT   \(ul\|result
  620. .)l
  621. If a variable is only referred to once, it does not need to  be  named
  622. and  may  be  written as an \fIanonymous\fP variable, indicated by the
  623. underline character \*(lq\(ul\|\*(rq.
  624. .pp
  625. A variable should be thought of as  standing  for  some  definite  but
  626. unidentified  object.
  627. A variable  is  not  simply  a  writeable
  628. storage  location  as  in  most programming languages;  rather it is a
  629. local name for some data object, cf.  the variable of  pure  LISP  and
  630. constant declarations in Pascal.
  631. .pp
  632. The structured data objects of the language are the compound terms.  A
  633. compound term comprises a \fIfunctor\fP (called the \fIprincipal\fP functor of
  634. the term) and a sequence of one or more terms called \fIarguments\fP.
  635. A functor is characterised by its \fIname\fP, which is an atom, and its
  636. \fIarity\fP or number of arguments.
  637. For example the compound term whose functor is
  638. named `point' of arity 3, with arguments X, Y and Z, is written
  639. .(l
  640. point(X,Y,Z)
  641. .)l
  642. An atom is considered to be a functor of arity 0.
  643. .pp
  644. A functor or predicate symbol is uniquely identified by its name and arity
  645. (in other words, it is possible for different symbols having different
  646. arities to share the same name).  A functor or predicate symbol \fIp\fR
  647. with arity \fIn\fR is usually written \fIp\fR/\fIn\fR.
  648. .pp
  649. One  may  think  of  a  functor  as  a record type and the
  650. arguments of a compound term as the  fields  of  a  record.   Compound
  651. terms are usefully pictured as trees.  For example, the term
  652. .(l
  653. s(np(john),vp(v(likes),np(mary)))
  654. .)l
  655. would be pictured as the structure
  656. .(b C
  657.        s
  658.      /   \e
  659.    np      vp   
  660.    |      /  \e
  661.  john    v     np
  662.          |     |
  663.        likes  mary
  664. .)b
  665. .pp
  666. Sometimes it is convenient to write certain functors as \fIoperators\fP
  667. \*- 2-ary functors may be declared as \fIinfix\fP operators and 1-ary functors
  668. as \fIprefix\fP or \fIpostfix\fP operators.
  669. Thus it is possible to write
  670. .(l
  671. X+Y     (P;Q)     X<Y      +X     P;
  672. .)l
  673. as optional alternatives to
  674. .(l
  675. +(X,Y)   ;(P,Q)   <(X,Y)   +(X)   ;(P)
  676. .)l
  677. Operators are described fully in the next section.
  678. .pp
  679. \fIList\fPs form an important class of data structures in Prolog.
  680. They  are essentially  the  same  as  the  lists  of LISP:
  681. a list either is the atom 
  682. [],
  683. representing the empty list, or is a compound term  with  functor  `.'/2
  684. and  two  arguments  which  are  respectively the head and tail of the
  685. list.  Thus  a  list  of  the  first  three  natural  numbers  is  the
  686. structure
  687. .(b C
  688.   .
  689.  / \e
  690. 1    .
  691.     / \e
  692.    2    .
  693.        / \e
  694.       3   []
  695. .)b
  696. which could be written, using the standard syntax, as
  697. .(l
  698. \&.(1,.(2,.(3,[])))
  699. .)l
  700. but which is normally written, in a special list notation, as
  701. [1,2,3].
  702. The special list notation in the case when the tail of  a  list  is  a
  703. variable is exemplified by
  704. .(l
  705. [X|L]     [a,b|L]
  706. .)l
  707. representing
  708. .(b C
  709.    .                .
  710.   / \e             / \e
  711. X     L          a    .
  712.                      / \e
  713.                    b     L
  714. .)b
  715. respectively.
  716. .pp
  717. Note that this list syntax is only syntactic sugar for terms of the form
  718. \&`.'(\(ul\|,\(ul\|) and does not provide any new facilities that were not
  719. available otherwise.
  720. .pp
  721. For convenience, a further  notational  variant  is  allowed  for
  722. lists  of  integers  which correspond to ASCII character codes.  Lists
  723. written in this notation are called \fIstring\fPs.
  724. For example,
  725. .(l
  726. "Prolog"
  727. .)l
  728. represents exactly the same list as
  729. .(l
  730. [80,114,111,108,111,103]
  731. .)l
  732. .sh 2 "Operators"
  733. .pp
  734. Operators in Prolog are simply a notational convenience.
  735. For example, the expression
  736. .(l
  737. 2 + 1
  738. .)l
  739. could also be written +(2,1).
  740. It should be noticed that this expression represents the structure
  741. .(b C
  742.    +
  743.  /   \e
  744. 2     1
  745. .)b
  746. and not the number 3.
  747. The addition would only be performed if the structure was passed as an
  748. argument to an appropriate procedure (such as \fBeval\fP/2 \- see Section 5.2).
  749. .pp
  750. The Prolog syntax caters for operators  of  three  main  kinds  \-
  751. \fIinfix\fR, \fIprefix\fR and \fIpostfix\fR.
  752. An infix operator appears between its two arguments, while a prefix operator
  753. precedes its single argument and a postfix operator is written after its
  754. single argument.
  755. .pp
  756. Each operator has a \fIprecedence\fP, which is a
  757. number from  1  to  1200.  The  precedence  is  used  to  disambiguate
  758. expressions  where  the  structure  of  the  term  denoted is not made
  759. explicit through parenthesization.   The  general  rule
  760. is that the operator with the
  761. \fIhighest\fP precedence is the principal functor.  Thus if `+'  has  a
  762. higher precedence than `/', then ``a+b/c'' and ``a+(b/c)''
  763. are equivalent and denote the term \*(lq+(a,/(b,c))\*(rq.
  764. Note that the  infix
  765. form of the term \*(lq/(+(a,b),c)\*(rq must be written with explicit
  766. parentheses, ``(a+b)/c''.
  767. .pp
  768. If there are two operators in the subexpression having  the  same
  769. highest  precedence,  the ambiguity must be resolved from the \fItypes\fP of
  770. the operators.  The possible types for an infix operator are
  771. .(l
  772. xfx     xfy     yfx
  773. .)l
  774. With an operator of type `xfx', it is a requirement that both  of  the
  775. two  subexpressions which are the arguments of the operator must be of
  776. \fIlower\fR precedence  than  the  operator  itself,  i.e.   their  principal
  777. functors  must  be  of  lower  precedence, unless the subexpression is
  778. explicitly  bracketed  (which  gives  it  zero  precedence).  With  an
  779. operator of type `xfy', only the first or left-hand subexpression must
  780. be of lower precedence;  the second can be of the \fIsame\fP  precedence  as
  781. the main operator;  and vice versa for an operator of type `yfx'.
  782. .pp
  783. For example, if the operators `+' and `\-' both  have  type  `yfx'
  784. and are of the same precedence, then the expression
  785. ``a\-b+c'' is valid, and means ``(a\-b)+c'', i.e. ``+(\-(a,b),c)''.
  786. Note that the expression would be invalid if the  operators  had  type
  787. \&`xfx', and would mean ``a\-(b+c)'', i.e. ``\-(a,+(b,c))'',
  788. if the types were both `xfy'.
  789. .pp
  790. The possible types for a prefix operator are
  791. .(l
  792. fx        fy
  793. .)l
  794. and for a postfix operator they are
  795. .(l
  796. xf        yf
  797. .)l
  798. The meaning of the types should be clear by  analogy  with  those  for
  799. infix  operators.   As  an example, if `not' were declared as a prefix
  800. operator of type `fy', then
  801. .(l
  802. not not P
  803. .)l
  804. would be a permissible way to write \*(lqnot(not(P))\*(rq. If  the  type  were
  805. \&`fx', the preceding expression would not be legal, although
  806. .(l
  807. not P
  808. .)l
  809. would still be a permissible form for \*(lqnot(P)\*(rq.
  810. .pp
  811. In SB-Prolog, a functor named \fIname\fP is  declared  as  an
  812. operator of type \fItype\fP and precedence \fIprecedence\fP by calling
  813. the evaluable predicate \fBop\fP:
  814. .(l
  815.     | ?- op(\fIprecedence\fP,\fItype\fP,\fIname\fP).
  816. .)l
  817. .(x b
  818. (L)     op/3
  819. .)x
  820. The argument \fIname\fP can also be a list of names of operators of the same
  821. type and precedence.
  822. .pp
  823. It is possible to have more than one operator of the  same  name,
  824. so long as they are of different kinds, i.e.  infix, prefix or postfix.
  825. An operator of any kind may be redefined by a new declaration  of  the
  826. same  kind.   This  applies equally to operators which are provided as
  827. \fIstandard\fP in SB-Prolog, namely:
  828. .(l
  829. .TS
  830. .if \n+(b.=1 .nr d. \n(.c-\n(c.-1
  831. .de 35
  832. .ps \n(.s
  833. .vs \n(.vu
  834. .in \n(.iu
  835. .if \n(.u .fi
  836. .if \n(.j .ad
  837. .if \n(.j=0 .na
  838. ..
  839. .nf
  840. .nr #~ 0
  841. .if n .nr #~ 0.6n
  842. .ds #d .d
  843. .if \(ts\n(.z\(ts\(ts .ds #d nl
  844. .fc
  845. .nr 33 \n(.s
  846. .rm 80 81 82 83
  847. .nr 80 0
  848. .nr 38 \w:\- op(
  849. .if \n(80<\n(38 .nr 80 \n(38
  850. .nr 38 \w:\- op(
  851. .if \n(80<\n(38 .nr 80 \n(38
  852. .nr 38 \w:\- op(
  853. .if \n(80<\n(38 .nr 80 \n(38
  854. .nr 38 \w:\- op(
  855. .if \n(80<\n(38 .nr 80 \n(38
  856. .nr 38 \w:\- op(
  857. .if \n(80<\n(38 .nr 80 \n(38
  858. .nr 38 \w:\- op(
  859. .if \n(80<\n(38 .nr 80 \n(38
  860. .nr 38 \w:\- op(
  861. .if \n(80<\n(38 .nr 80 \n(38
  862. .nr 38 \w:\- op(
  863. .if \n(80<\n(38 .nr 80 \n(38
  864. .nr 38 \w\&
  865. .if \n(80<\n(38 .nr 80 \n(38
  866. .nr 38 \w:\- op(
  867. .if \n(80<\n(38 .nr 80 \n(38
  868. .nr 38 \w:\- op(
  869. .if \n(80<\n(38 .nr 80 \n(38
  870. .nr 38 \w:\- op(
  871. .if \n(80<\n(38 .nr 80 \n(38
  872. .nr 38 \w:\- op(
  873. .if \n(80<\n(38 .nr 80 \n(38
  874. .nr 38 \w:\- op(
  875. .if \n(80<\n(38 .nr 80 \n(38
  876. .nr 38 \w:\- op(
  877. .if \n(80<\n(38 .nr 80 \n(38
  878. .80
  879. .rm 80
  880. .nr 81 0
  881. .nr 38 \w1200,
  882. .if \n(81<\n(38 .nr 81 \n(38
  883. .nr 38 \w1200,
  884. .if \n(81<\n(38 .nr 81 \n(38
  885. .nr 38 \w1198,
  886. .if \n(81<\n(38 .nr 81 \n(38
  887. .nr 38 \w1100,
  888. .if \n(81<\n(38 .nr 81 \n(38
  889. .nr 38 \w1050,
  890. .if \n(81<\n(38 .nr 81 \n(38
  891. .nr 38 \w1000,
  892. .if \n(81<\n(38 .nr 81 \n(38
  893. .nr 38 \w900,
  894. .if \n(81<\n(38 .nr 81 \n(38
  895. .nr 38 \w700,
  896. .if \n(81<\n(38 .nr 81 \n(38
  897. .nr 38 \w\&
  898. .if \n(81<\n(38 .nr 81 \n(38
  899. .nr 38 \w661,
  900. .if \n(81<\n(38 .nr 81 \n(38
  901. .nr 38 \w500,
  902. .if \n(81<\n(38 .nr 81 \n(38
  903. .nr 38 \w500,
  904. .if \n(81<\n(38 .nr 81 \n(38
  905. .nr 38 \w400,
  906. .if \n(81<\n(38 .nr 81 \n(38
  907. .nr 38 \w300,
  908. .if \n(81<\n(38 .nr 81 \n(38
  909. .nr 38 \w200,
  910. .if \n(81<\n(38 .nr 81 \n(38
  911. .81
  912. .rm 81
  913. .nr 82 0
  914. .nr 38 \wxfx,
  915. .if \n(82<\n(38 .nr 82 \n(38
  916. .nr 38 \wfx,
  917. .if \n(82<\n(38 .nr 82 \n(38
  918. .nr 38 \wxfx,
  919. .if \n(82<\n(38 .nr 82 \n(38
  920. .nr 38 \wxfy,
  921. .if \n(82<\n(38 .nr 82 \n(38
  922. .nr 38 \wxfy,
  923. .if \n(82<\n(38 .nr 82 \n(38
  924. .nr 38 \wxfy,
  925. .if \n(82<\n(38 .nr 82 \n(38
  926. .nr 38 \wfy,
  927. .if \n(82<\n(38 .nr 82 \n(38
  928. .nr 38 \wxfx,
  929. .if \n(82<\n(38 .nr 82 \n(38
  930. .nr 38 \w\&
  931. .if \n(82<\n(38 .nr 82 \n(38
  932. .nr 38 \wxfy,
  933. .if \n(82<\n(38 .nr 82 \n(38
  934. .nr 38 \wyfx,
  935. .if \n(82<\n(38 .nr 82 \n(38
  936. .nr 38 \wfx,
  937. .if \n(82<\n(38 .nr 82 \n(38
  938. .nr 38 \wyfx,
  939. .if \n(82<\n(38 .nr 82 \n(38
  940. .nr 38 \wxfx,
  941. .if \n(82<\n(38 .nr 82 \n(38
  942. .nr 38 \wxfy,
  943. .if \n(82<\n(38 .nr 82 \n(38
  944. .82
  945. .rm 82
  946. .nr 83 0
  947. .nr 38 \w[ :\-, --> ]).
  948. .if \n(83<\n(38 .nr 83 \n(38
  949. .nr 38 \w[ :\-, ?- ]).
  950. .if \n(83<\n(38 .nr 83 \n(38
  951. .nr 38 \w[ ::\- ]).
  952. .if \n(83<\n(38 .nr 83 \n(38
  953. .nr 38 \w[ ; ]).
  954. .if \n(83<\n(38 .nr 83 \n(38
  955. .nr 38 \w[ \-> ]).
  956. .if \n(83<\n(38 .nr 83 \n(38
  957. .nr 38 \w[ ',' ]).   /* See note below */
  958. .if \n(83<\n(38 .nr 83 \n(38
  959. .nr 38 \w[ not, \e+, spy, nospy ]).
  960. .if \n(83<\n(38 .nr 83 \n(38
  961. .nr 99 \n(.s
  962. .nr 98 \n(.f
  963. .rm 11 
  964. .as 11 ".nr 38 \w[ =, is, =.., ==, \e==, 
  965. .ps 11
  966. .ft 2
  967. .ds 12 "<\f1,\fP
  968. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  969. .as 11 \*(12
  970. .ps \n(99
  971. .ft \n(98
  972. .as 11 ">, 
  973. .ps 11
  974. .ft 2
  975. .ds 12 "\(eq<\f1,\fP
  976. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  977. .as 11 \*(12
  978. .ps \n(99
  979. .ft \n(98
  980. .as 11 ">=,
  981. .ps \n(99
  982. .ft \n(98
  983. \*(11
  984. .if \n(83<\n(38 .nr 83 \n(38
  985. .nr 38 \w  =:=, =\e=, <, >, =<, >= ]).
  986. .if \n(83<\n(38 .nr 83 \n(38
  987. .nr 38 \w[ `.' ]).
  988. .if \n(83<\n(38 .nr 83 \n(38
  989. .nr 38 \w[ +, \-, /\e, \e/ ]).
  990. .if \n(83<\n(38 .nr 83 \n(38
  991. .nr 38 \w[ +, \- ]).
  992. .if \n(83<\n(38 .nr 83 \n(38
  993. .nr 38 \w[ *, /, //, <<, >> ]).
  994. .if \n(83<\n(38 .nr 83 \n(38
  995. .nr 38 \w[ mod ]).
  996. .if \n(83<\n(38 .nr 83 \n(38
  997. .nr 38 \w[ ^ ]).
  998. .if \n(83<\n(38 .nr 83 \n(38
  999. .83
  1000. .rm 83
  1001. .nr 38 1n
  1002. .nr 79 0
  1003. .nr 40 \n(79+(0*\n(38)
  1004. .nr 80 +\n(40
  1005. .nr 41 \n(80+(3*\n(38)
  1006. .nr 81 +\n(41
  1007. .nr 42 \n(81+(3*\n(38)
  1008. .nr 82 +\n(42
  1009. .nr 43 \n(82+(3*\n(38)
  1010. .nr 83 +\n(43
  1011. .nr TW \n(83
  1012. .if t .if (\n(TW+\n(.o)>7.65i .tm Table at line 549 file sec2.t is too wide - \n(TW units
  1013. .fc  
  1014. .nr #T 0-1
  1015. .nr #a 0-1
  1016. .eo
  1017. .de T#
  1018. .ds #d .d
  1019. .if \(ts\n(.z\(ts\(ts .ds #d nl
  1020. .mk ##
  1021. .nr ## -1v
  1022. .ls 1
  1023. .ls
  1024. ..
  1025. .ec
  1026. .ta \n(80u \n(81u \n(82u \n(83u 
  1027. .nr 31 \n(.f
  1028. .nr 35 1m
  1029. \&\h'|\n(40u':\- op(\h'|\n(41u'1200,\h'|\n(42u'xfx,\h'|\n(43u'[ :\-, --> ]).
  1030. .ta \n(80u \n(81u \n(82u \n(83u 
  1031. .nr 31 \n(.f
  1032. .nr 35 1m
  1033. \&\h'|\n(40u':\- op(\h'|\n(41u'1200,\h'|\n(42u'fx,\h'|\n(43u'[ :\-, ?- ]).
  1034. .ta \n(80u \n(81u \n(82u \n(83u 
  1035. .nr 31 \n(.f
  1036. .nr 35 1m
  1037. \&\h'|\n(40u':\- op(\h'|\n(41u'1198,\h'|\n(42u'xfx,\h'|\n(43u'[ ::\- ]).
  1038. .ta \n(80u \n(81u \n(82u \n(83u 
  1039. .nr 31 \n(.f
  1040. .nr 35 1m
  1041. \&\h'|\n(40u':\- op(\h'|\n(41u'1100,\h'|\n(42u'xfy,\h'|\n(43u'[ ; ]).
  1042. .ta \n(80u \n(81u \n(82u \n(83u 
  1043. .nr 31 \n(.f
  1044. .nr 35 1m
  1045. \&\h'|\n(40u':\- op(\h'|\n(41u'1050,\h'|\n(42u'xfy,\h'|\n(43u'[ \-> ]).
  1046. .ta \n(80u \n(81u \n(82u \n(83u 
  1047. .nr 31 \n(.f
  1048. .nr 35 1m
  1049. \&\h'|\n(40u':\- op(\h'|\n(41u'1000,\h'|\n(42u'xfy,\h'|\n(43u'[ ',' ]).   /* See note below */
  1050. .ta \n(80u \n(81u \n(82u \n(83u 
  1051. .nr 31 \n(.f
  1052. .nr 35 1m
  1053. \&\h'|\n(40u':\- op(\h'|\n(41u'900,\h'|\n(42u'fy,\h'|\n(43u'[ not, \e+, spy, nospy ]).
  1054. .ta \n(80u \n(81u \n(82u \n(83u 
  1055. .nr 31 \n(.f
  1056. .nr 35 1m
  1057. .nr 99 \n(.s
  1058. .nr 98 \n(.f
  1059. .rm 11 
  1060. .as 11 "\&\h'|\n(40u':\- op(\h'|\n(41u'700,\h'|\n(42u'xfx,\h'|\n(43u'[ =, is, =.., ==, \e==, 
  1061. .ps 11
  1062. .ft 2
  1063. .ds 12 "<\f1,\fP
  1064. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  1065. .as 11 \*(12
  1066. .ps \n(99
  1067. .ft \n(98
  1068. .as 11 ">, 
  1069. .ps 11
  1070. .ft 2
  1071. .ds 12 "\(eq<\f1,\fP
  1072. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  1073. .as 11 \*(12
  1074. .ps \n(99
  1075. .ft \n(98
  1076. .as 11 ">=,
  1077. .ps \n(99
  1078. .ft \n(98
  1079. \*(11
  1080. .ta \n(80u \n(81u \n(82u \n(83u 
  1081. .nr 31 \n(.f
  1082. .nr 35 1m
  1083. \&\h'|\n(40u'\&\h'|\n(41u'\&\h'|\n(42u'\&\h'|\n(43u'  =:=, =\e=, <, >, =<, >= ]).
  1084. .ta \n(80u \n(81u \n(82u \n(83u 
  1085. .nr 31 \n(.f
  1086. .nr 35 1m
  1087. \&\h'|\n(40u':\- op(\h'|\n(41u'661,\h'|\n(42u'xfy,\h'|\n(43u'[ `.' ]).
  1088. .ta \n(80u \n(81u \n(82u \n(83u 
  1089. .nr 31 \n(.f
  1090. .nr 35 1m
  1091. \&\h'|\n(40u':\- op(\h'|\n(41u'500,\h'|\n(42u'yfx,\h'|\n(43u'[ +, \-, /\e, \e/ ]).
  1092. .ta \n(80u \n(81u \n(82u \n(83u 
  1093. .nr 31 \n(.f
  1094. .nr 35 1m
  1095. \&\h'|\n(40u':\- op(\h'|\n(41u'500,\h'|\n(42u'fx,\h'|\n(43u'[ +, \- ]).
  1096. .ta \n(80u \n(81u \n(82u \n(83u 
  1097. .nr 31 \n(.f
  1098. .nr 35 1m
  1099. \&\h'|\n(40u':\- op(\h'|\n(41u'400,\h'|\n(42u'yfx,\h'|\n(43u'[ *, /, //, <<, >> ]).
  1100. .ta \n(80u \n(81u \n(82u \n(83u 
  1101. .nr 31 \n(.f
  1102. .nr 35 1m
  1103. \&\h'|\n(40u':\- op(\h'|\n(41u'300,\h'|\n(42u'xfx,\h'|\n(43u'[ mod ]).
  1104. .ta \n(80u \n(81u \n(82u \n(83u 
  1105. .nr 31 \n(.f
  1106. .nr 35 1m
  1107. \&\h'|\n(40u':\- op(\h'|\n(41u'200,\h'|\n(42u'xfy,\h'|\n(43u'[ ^ ]).
  1108. .fc
  1109. .nr T. 1
  1110. .T# 1
  1111. .35
  1112. .TE
  1113. .if \n-(b.=0 .nr c. \n(.c-\n(d.-17
  1114. .)l
  1115. .pp
  1116. Operator declarations are most usefully placed in directives at the top
  1117. of your program files. In this case the directive should be a command as
  1118. shown above. Another common method of organisation is to have one file
  1119. just containing commands to declare all the necessary operators. This file
  1120. is then always consulted first.
  1121. .pp
  1122. Note that a comma written literally as  a  punctuation  character
  1123. can be used as though it were an infix operator of precedence 1000 and
  1124. type `xfy':
  1125. .(l
  1126. X,Y    ','(X,Y)
  1127. .)l
  1128. represent the same compound term.  But note that a comma written as  a
  1129. quoted atom is \fInot\fP a standard operator.
  1130. .pp
  1131. Note also that the  arguments  of  a  compound  term  written  in
  1132. standard  syntax must be expressions of precedence \fIbelow\fP 1000. Thus it
  1133. is necessary to parenthesize the expression \*(lqP :\- Q\*(rq in
  1134. .(l
  1135. assert((P :\- Q))
  1136. .)l
  1137. The following syntax restrictions serve  to
  1138. remove potential ambiguity associated with prefix operators.
  1139. .ip -
  1140. In a term written in standard syntax, the  principal  functor  and
  1141. its  following  \*(lq(\*(rq  must  \fInot\fP be separated by any whitespace.  Thus
  1142. .(l
  1143. point (X,Y,Z)
  1144. .)l
  1145. is invalid syntax (unless \fIpoint\fR were declared as a prefix operator).
  1146. .ip -
  1147. If the argument of a prefix operator starts with a \*(lq(\*(rq,  this  \*(lq(\*(rq
  1148. must  be  separated  from  the operator by at least one space or other
  1149. non-printable character.  Thus
  1150. .(l
  1151. :\-(p;q),r.
  1152. .)l
  1153. (where `:\-' is the prefix operator) is invalid syntax, and must be written as
  1154. .(l
  1155. :\- (p;q),r.
  1156. .)l
  1157. .ip -
  1158. If a prefix  operator  is  written  without  an  argument,  as  an
  1159. ordinary  atom,  the  atom  is  treated  as  an expression of the same
  1160. precedence as the prefix operator, and  must  therefore  be  bracketed
  1161. where necessary.  Thus the brackets are necessary in
  1162. .(l
  1163. X = (?-)
  1164. .)l
  1165. .sh 1 "SB-Prolog: Operational Semantics"
  1166. .sh 2 "Standard Execution Behaviour"
  1167. .pp
  1168. The normal execution behaviour of SB-Prolog follows the usual left to right
  1169. order of literals within a clause, and the textual top to bottom order of
  1170. clauses for a predicate.  This corresponds to a depth first search of the
  1171. leftmost SLD-tree for the program and the given query.  Unification without
  1172. occurs check is used, and execution backtracks to the most recent choice
  1173. point when unification fails.
  1174. .sh 2 "Cuts and If-Then-Else"
  1175. .pp
  1176. This standard execution behaviour of SB-Prolog can be changed using
  1177. constructs like \fIcut\fR (`!') and \fIif-then-else\fR (\^`\->'\^).  In
  1178. SB-Prolog, cuts are usually treated as \fIhard\fR, i.e. discard choice points
  1179. of all the literals to the left of the cut in the clause containing the cut
  1180. being executed, and also the choice point for the parent predicate, i.e.
  1181. any remaining clauses for the predicate containing the cut being executed.
  1182. There are some situations, however, where the scope of a cut is restricted
  1183. to be smaller than this.  Restrictions apply under the following conditions:
  1184. .np
  1185. The cut occurs in a term which has been constructed at runtime and called
  1186. through \fIcall\fR/1, e.g. in
  1187. .(l C
  1188.  ..., X = (p(Y), !, q(Y)), ..., call(X), ...
  1189. .)l
  1190. In this case, the scope of the cut is restricted to be within the \fIcall\fR,
  1191. unless one of the following cases also apply and serve to restrict its
  1192. scope further.
  1193. .np
  1194. The cut occurs in a negated goal, or within the scope of an if-then-else.
  1195. In these cases, the scope of the cut is restricted to be within the
  1196. negation or the if-then-else.
  1197. .lp
  1198. In cases involving nested occurrences of these situations, the scope of the
  1199. cut is restricted to that for the deepest such nested construct, i.e. most
  1200. restricted.  For example, in the construct
  1201. .(l C
  1202.  ..., not( (p(X) \-> not( (q(X), (r(X) \-> s(X) ; (t(X), !, u(X)))) )) ), ...
  1203. .)l
  1204. the scope of the cut is restricted to the inner if-then-else, and does not
  1205. affect any choice point that may have been set up for q(X).  
  1206. .pp
  1207. The behaviour of the \fIif-then-else\fR operator `\->' is as follows: when
  1208. executing the goal
  1209. .(l C
  1210. \fIP\fR \-> \fIQ\fR ; \fIR\fR
  1211. .)l
  1212. if \fIP\fR succeeds, then any alternatives for \fIP\fR are discarded and \fIQ\fR
  1213. is tried, while if \fIP\fR fails then \fIR\fR is tried.  Thus, \-> may be
  1214. thought of as
  1215.  
  1216. .(l
  1217.     \fIP\fR \-> \fIQ\fR ; \fIR\fR :\- \fIP\fR, !, \fIQ\fR.
  1218.     \fIP\fR \-> \fIQ\fR ; \fIR\fR :\- \fIR\fR.
  1219. .)l
  1220.  
  1221. The scoping of cuts within if-then-elses is consistent with this
  1222. conceptualization.
  1223. .sh 2 "Unification of Floating Point Numbers"
  1224. .pp
  1225. As far as unification is concerned, no type distinction is made between
  1226. integers and floating point numbers, and no explicit type conversion is necessary
  1227. when unifying an integer with a float.  However, due to the finite precision
  1228. representation of floating point numbers and cumulative round-off errors in
  1229. floating point arithmetic, comparisons involving floating point numbers may
  1230. not always give the expected results.  An effort is made to minimize surprises
  1231. by considering two numbers \fIx\fR and \fIy\fR (at least one of which is a float)
  1232. to be unifiable if |\^|\^\fIx\fR\^| \- |\^\fIy\fR\^|\^|/\fImin\fR(|\^\fIx\fR\^|, |\^\fIy\fR\^|)
  1233. to be less than 10\*[\-5\*].  However, this does not guarantee
  1234. immunity against round-off errors.  For the same reason, users are warned
  1235. that indexing on predicate arguments (see Section 13) may not give the
  1236. expected results if floating point numbers are involved.
  1237. .sh 1 "Evaluable Predicates"
  1238. .pp
  1239. This section describes (most of) the evaluable predicates provided by
  1240. SB-Prolog.  These can be divided into three classes: \fIinline\fR
  1241. predicates, \fIbuiltin\fR predicates and \fIlibrary\fR predicates.
  1242. .pp
  1243. Inline predicates represent ``primitive'' operations in the WAM.
  1244. Calls to inline predicates are compiled into a sequence of WAM
  1245. instructions in-line, i.e. without actually making a call to the predicate.  Thus, for
  1246. example, relational predicates (>/2, >=/2, etc.) compile to, essentially, a
  1247. subtraction and a conditional branch.  Inline predicates cannot be redefined by the
  1248. user.  Table 1 lists the SB-Prolog inline predicates.
  1249. .(z
  1250. .TS
  1251. .if \n+(b.=1 .nr d. \n(.c-\n(c.-1
  1252. .de 35
  1253. .ps \n(.s
  1254. .vs \n(.vu
  1255. .in \n(.iu
  1256. .if \n(.u .fi
  1257. .if \n(.j .ad
  1258. .if \n(.j=0 .na
  1259. ..
  1260. .nf
  1261. .nr #~ 0
  1262. .if n .nr #~ 0.6n
  1263. .ds #d .d
  1264. .if \(ts\n(.z\(ts\(ts .ds #d nl
  1265. .fc
  1266. .nr 33 \n(.s
  1267. .rm 80 81 82 83
  1268. .nr 80 0
  1269. .nr 38 \w=/2
  1270. .if \n(80<\n(38 .nr 80 \n(38
  1271. .nr 38 \w>/2
  1272. .if \n(80<\n(38 .nr 80 \n(38
  1273. .nr 38 \w>>/2
  1274. .if \n(80<\n(38 .nr 80 \n(38
  1275. .nr 38 \w\\/1
  1276. .if \n(80<\n(38 .nr 80 \n(38
  1277. .nr 38 \w`_\|$call'/1
  1278. .if \n(80<\n(38 .nr 80 \n(38
  1279. .nr 38 \whalt/0
  1280. .if \n(80<\n(38 .nr 80 \n(38
  1281. .80
  1282. .rm 80
  1283. .nr 81 0
  1284. .nr 38 \w</2
  1285. .if \n(81<\n(38 .nr 81 \n(38
  1286. .nr 38 \w/\e/2
  1287. .if \n(81<\n(38 .nr 81 \n(38
  1288. .nr 38 \w=:=/2
  1289. .if \n(81<\n(38 .nr 81 \n(38
  1290. .nr 38 \w`_\|\|$savecp'/1
  1291. .if \n(81<\n(38 .nr 81 \n(38
  1292. .nr 38 \wnonvar/1
  1293. .if \n(81<\n(38 .nr 81 \n(38
  1294. .nr 38 \wtrue/0
  1295. .if \n(81<\n(38 .nr 81 \n(38
  1296. .81
  1297. .rm 81
  1298. .nr 82 0
  1299. .nr 38 \w=</2
  1300. .if \n(82<\n(38 .nr 82 \n(38
  1301. .nr 38 \w`\e/'/2
  1302. .if \n(82<\n(38 .nr 82 \n(38
  1303. .nr 38 \w=\e=/2
  1304. .if \n(82<\n(38 .nr 82 \n(38
  1305. .nr 38 \w`_\|\|$cutto'/1
  1306. .if \n(82<\n(38 .nr 82 \n(38
  1307. .nr 38 \wvar/1
  1308. .if \n(82<\n(38 .nr 82 \n(38
  1309. .nr 38 \w 
  1310. .if \n(82<\n(38 .nr 82 \n(38
  1311. .82
  1312. .rm 82
  1313. .nr 83 0
  1314. .nr 38 \w>=/2
  1315. .if \n(83<\n(38 .nr 83 \n(38
  1316. .nr 38 \w<</2
  1317. .if \n(83<\n(38 .nr 83 \n(38
  1318. .nr 38 \wis/2
  1319. .if \n(83<\n(38 .nr 83 \n(38
  1320. .nr 38 \w`_\|\|$builtin'/1
  1321. .if \n(83<\n(38 .nr 83 \n(38
  1322. .nr 38 \wfail/0
  1323. .if \n(83<\n(38 .nr 83 \n(38
  1324. .83
  1325. .rm 83
  1326. .nr 38 0+\n(80+\n(81+\n(82+\n(83
  1327. .nr 38 \n(.l-\n(.i-\n(38
  1328. .nr 38 \n(38/9
  1329. .if \n(38<1n .nr 38 1n
  1330. .nr 79 0
  1331. .nr 40 \n(79+(0*\n(38)
  1332. .nr 80 +\n(40
  1333. .nr 41 \n(80+(3*\n(38)
  1334. .nr 81 +\n(41
  1335. .nr 42 \n(81+(3*\n(38)
  1336. .nr 82 +\n(42
  1337. .nr 43 \n(82+(3*\n(38)
  1338. .nr 83 +\n(43
  1339. .nr TW \n(83
  1340. .if t .if (\n(TW+\n(.o)>7.65i .tm Table at line 695 file sec2.t is too wide - \n(TW units
  1341. .nr #I \n(.i
  1342. .in +(\n(.lu-\n(TWu-\n(.iu)/2u
  1343. .fc  
  1344. .nr #T 0-1
  1345. .nr #a 0-1
  1346. .eo
  1347. .de T#
  1348. .ds #d .d
  1349. .if \(ts\n(.z\(ts\(ts .ds #d nl
  1350. .mk ##
  1351. .nr ## -1v
  1352. .ls 1
  1353. .ls
  1354. ..
  1355. .ec
  1356. .ta \n(80u \n(81u \n(82u \n(83u 
  1357. .nr 31 \n(.f
  1358. .nr 35 1m
  1359. \&\h'|\n(40u'=/2\h'|\n(41u'</2\h'|\n(42u'=</2\h'|\n(43u'>=/2
  1360. .ta \n(80u \n(81u \n(82u \n(83u 
  1361. .nr 31 \n(.f
  1362. .nr 35 1m
  1363. \&\h'|\n(40u'>/2\h'|\n(41u'/\e/2\h'|\n(42u'`\e/'/2\h'|\n(43u'<</2
  1364. .ta \n(80u \n(81u \n(82u \n(83u 
  1365. .nr 31 \n(.f
  1366. .nr 35 1m
  1367. \&\h'|\n(40u'>>/2\h'|\n(41u'=:=/2\h'|\n(42u'=\e=/2\h'|\n(43u'is/2
  1368. .ta \n(80u \n(81u \n(82u \n(83u 
  1369. .nr 31 \n(.f
  1370. .nr 35 1m
  1371. \&\h'|\n(40u'\\/1\h'|\n(41u'`_\|\|$savecp'/1\h'|\n(42u'`_\|\|$cutto'/1\h'|\n(43u'`_\|\|$builtin'/1
  1372. .ta \n(80u \n(81u \n(82u \n(83u 
  1373. .nr 31 \n(.f
  1374. .nr 35 1m
  1375. \&\h'|\n(40u'`_\|$call'/1\h'|\n(41u'nonvar/1\h'|\n(42u'var/1\h'|\n(43u'fail/0
  1376. .ta \n(80u \n(81u \n(82u \n(83u 
  1377. .nr 31 \n(.f
  1378. .nr 35 1m
  1379. \&\h'|\n(40u'halt/0\h'|\n(41u'true/0\h'|\n(42u' \h'|\n(43u'
  1380. .fc
  1381. .nr T. 1
  1382. .T# 1
  1383. .in \n(#Iu
  1384. .35
  1385. .TE
  1386. .if \n-(b.=0 .nr c. \n(.c-\n(d.-9
  1387. .sp
  1388. .ce
  1389. Table 1: Inline Predicates of SB-Prolog
  1390. .sp 2
  1391. .)z
  1392. .pp
  1393. Unlike inline predicates, builtin predicates are implemented by C
  1394. functions in the simulator, and accessed via the inline predicate
  1395. `\fI_\|\|$builtin\fR'/1.
  1396. Thus, if a builtin predicate \fIfoo\fR/3 was defined
  1397. as builtin number 38, there would be a definition in the system
  1398. of the form
  1399.  
  1400. .(l
  1401.     foo(X,Y,Z) :\- '_\|\|$builtin'(38).
  1402. .)l
  1403.  
  1404. .pp
  1405. In effect, a builtin is simply a segment of code in a large case
  1406. (i.e. \fIswitch\fR) statement.  Each builtin is identified internally by an
  1407. integer, referred to as its ``builtin number'', associated with it.  The code for a builtin with
  1408. builtin number \fIk\fR corresponds to the \fIk\*[th.\*]\fR case in the switch
  1409. statement.
  1410. SB-Prolog limits the total number of builtins to 256.
  1411. .pp
  1412. Builtins, unlike inline predicates,  can be redefined by the user.  For example, the
  1413. predicate \fIfoo\fR/3 above can be redefined simply by compiling the new definition into a
  1414. directory such that during dynamic loading, the new definition would be
  1415. encountered first and loaded.
  1416. .pp
  1417. A list of the builtins currently provided is listed in Appendix 1.
  1418. Section 7.4 describes the procedure to be followed in order to define new
  1419. builtin predicates.
  1420. .pp
  1421. Like builtin predicates, library predicates may also be redefined by the
  1422. user.  The essential difference between builtin and library predicates is
  1423. that whereas the former are coded into the simulator in C, the latter are
  1424. written in Prolog.
  1425. .sh 2 "Input and Output"
  1426. .pp
  1427. Input and output are done with respect to the current input and output
  1428. streams.  These can be set, reset or checked using the file handling
  1429. predicates described below.  The default input and output streams are
  1430. denoted by \fBuser\fR, and refer to the user's terminal.
  1431. .sh 3 "File Handling"
  1432. .ip \fBsee\fR(\fIF\fR\|)
  1433. \fIF\fR becomes the current input stream.  \fIF\fR must be instantiated to
  1434. an atom at the time of the call.
  1435. .(x b
  1436. (B)     see/1
  1437. .)x
  1438. .ip \fBseeing\fR(\fIF\fR\|)
  1439. \fIF\fR is unified with the name of the current input file.
  1440. .ip \fBseen\fR
  1441. Closes the current input stream.
  1442. .(x b
  1443. (B)     seen
  1444. .)x
  1445. .ip \fBtell\fR(\fIF\fR\^)
  1446. \fIF\fR becomes the current output stream.  \fIF\fR must be instantiated to
  1447. an atom at the time of the call.
  1448. .(x b
  1449. (B)     tell/1
  1450. .)x
  1451. .ip \fBtelling\fR(\fIF\fR\^)
  1452. \fIF\fR is unified with the name of the current output file.
  1453. .(x b
  1454. (B)     telling/1
  1455. .)x
  1456. .lp
  1457. \fBtold\fR
  1458. .ip
  1459. Closes the current output stream.
  1460. .(x b
  1461. (B)     told/0
  1462. .)x
  1463. .ip \fB$exists\fR(\fIF\fR\^)
  1464. Succeeds if file \fIF\fR exists.
  1465. .(x b
  1466. (B)     $exists/1
  1467. .)x
  1468. .sh 3 "Term I/O"
  1469. .ip \fBread\fR(\fIX\fR\^)
  1470. The
  1471. next term, delimited by a full stop (i.e.  a \*(lq.\*(rq followed  by a carriage-return
  1472. or  a  space),  is  read  from the current input stream and
  1473. unified with \fIX\fP. The syntax of the term must accord  with  current
  1474. operator declarations.  If a call \fBread\fP(\fIX\fP) causes the end of the
  1475. current input stream to be reached, \fIX\fP is unified  with  the  atom
  1476. \&`end\(ul\|of\(ul\|file'.  Further  calls  to \fBread\fP for the same stream will then
  1477. cause an error failure.
  1478. .(x b
  1479. (L)     read/1
  1480. .)x
  1481. .ip \fBwrite\fP(\fIX\fP)
  1482. The
  1483. term \fIX\fP is written to the current output stream  according  to
  1484. operator declarations in force.
  1485. .(x b
  1486. (L)     write/1
  1487. .)x
  1488. .ip \fBdisplay\fP(\fIX\fP)
  1489. .(x b
  1490. (L)     display/1
  1491. .)x
  1492. The
  1493. term \fIX\fP is displayed on the terminal in standard parenthesised
  1494. prefix notation.
  1495. .ip \fBwriteq\fP(\fITerm\fP)
  1496. Similar
  1497. to \fBwrite\fP(\fITerm\fP), but the names of atoms and functors
  1498. are quoted where necessary to make the result acceptable as input to \fBread\fP.
  1499. .(x b
  1500. (L)     writeq/1
  1501. .)x
  1502. .ip \fBprint\fP(\fITerm\fP)
  1503. Prints out the term \fITerm\fR onto the user's terminal.
  1504. .(x b
  1505. (L)     print/1
  1506. .)x
  1507. .ip \fBwritename\fR(\fITerm\fR\^)
  1508. .(x b
  1509. (B)     writename/1
  1510. .)x
  1511. If \fITerm\fR is an uninstantiated variable, its name, which
  1512. looks a lot like an address in memory, is written out; otherwise, the
  1513. principal functor of \fITerm\fR is written out.
  1514. .ip \fBwriteqname\fR(\fITerm\fR)
  1515. .(x b
  1516. (B)     writeqname/1
  1517. .)x
  1518. As for \fBwritename\fR, but the names are quoted where necessary.
  1519. .sh 3 "Character I/O"
  1520. .ip \fBnl\fP
  1521. A new line is started on the current output stream.
  1522. .(x b
  1523. (B)     nl/0
  1524. .)x
  1525. .ip \fBget0\fP(\fIN\fP)
  1526. \fIN\fP
  1527. is the ASCII code of the next character from the current  input
  1528. stream. If the current input stream reaches its end of file,
  1529. a \-1 is returned (however, unlike in C-Prolog, the input stream is not closed on encountering
  1530. end-of-file).
  1531. .(x b
  1532. (B)     get0/1
  1533. .)x
  1534. .ip \fBget\fP(\fIN\fP)
  1535. \fIN\fP is the ASCII code of the  next  non-blank  printable  character
  1536. from the current input stream. It has the same behaviour as \fBget0\fP
  1537. on end of file.
  1538. .(x b
  1539. (B)     get/1
  1540. .)x
  1541. .ip \fBput\fP(\fIN\fP)
  1542. ASCII character code \fIN\fP is output to the current output stream.
  1543. \fIN\fP must be an integer.
  1544. .(x b
  1545. (B)     put/1
  1546. .)x
  1547. .ip \fBtab\fP(\fIN\fP)
  1548. \fIN\fP
  1549. spaces are output to the current output stream.  \fIN\fP
  1550. must be an integer.
  1551. .(x b
  1552. (B)     tab/1
  1553. .)x
  1554. .sh 2 "Arithmetic"
  1555. .pp
  1556. Arithmetic is performed by  evaluable predicates  which  take  as
  1557. arguments   \fIarithmetic expressions\fP   and  \fIevaluate\fP  them.
  1558. An  arithmetic expression is a term  built  from  \fIevaluable functors\fP,  
  1559. numbers  and variables.
  1560. At  the  time  of evaluation, each variable in an arithmetic
  1561. expression must be bound to a number or
  1562. to  an  arithmetic  expression.
  1563. Each evaluable functor stands for an arithmetic  operation.
  1564. .pp
  1565. The
  1566. evaluable  functors  are  as  follows,  where  \fIX\fP  and  \fIY\fP  are  
  1567. arithmetic expressions.
  1568. .ip \fIX\fP\ +\ \fIY\fP
  1569. addition.
  1570. .ip \fIX\fP\ \-\ \fIY\fP
  1571. subtraction.
  1572. .ip \fIX\fP\ *\ \fIY\fP
  1573. multiplication.
  1574. .ip \fIX\fP\ /\ \fIY\fP
  1575. division.\**  Amounts to integer division if both \fIX\fR and \fIY\fR are
  1576. integers.
  1577. .(f
  1578. \** The ``integer division'' operator `//' of C-Prolog is not supported;
  1579. it can be faked using \fIfloor\fR/2 if necessary.
  1580. .)f
  1581. .ip \fIX\fP\ mod\ \fIY\fP
  1582. \fIX\fP (integer) modulo \fIY\fP.
  1583. .ip \-\fIX\fP
  1584. unary minus.
  1585. .ip \fIX\fP\ /\e\ \fIY\fP
  1586. integer bitwise conjunction.
  1587. .ip \fIX\fP\ \e/\ \fIY\fP
  1588. integer bitwise disjunction.
  1589. .ip \fIX\fP\ <<\ \fIY\fP
  1590. integer bitwise left shift of \fIX\fP by \fIY\fP places.
  1591. .ip \fIX\fP\ >>\ \fIY\fP
  1592. integer bitwise right shift of \fIX\fP by \fIY\fP places.
  1593. .ip \e\fIX\fP
  1594. integer bitwise negation.
  1595. .pp
  1596. As far as unification is concerned, no type distinction is made between
  1597. integers and floating point numbers, and no explicit type conversion is necessary
  1598. when unifying an integer with a float.  However, due to the finite precision
  1599. representation of floating point numbers and cumulative round-off errors in
  1600. floating point arithmetic, comparisons involving floating point numbers may
  1601. not always give the expected results.  An effort is made to minimize surprises
  1602. by considering two numbers \fIx\fR and \fIy\fR (at least one of which is a float)
  1603. to be unifiable if |\fIx\fR \- \fIy\fR|/\fImin\fR(|\fIx\fR|, |\fIy\fR|)
  1604. to be less than 10\*[\-5\*].  The user should note, however, that this does
  1605. not guarantee immunity against round-off errors.
  1606. .pp
  1607. The arithmetic evaluable predicates are as follows, where \fIX\fP and  
  1608. \fIY\fP stand for arithmetic expressions, and \fIZ\fP for some term.
  1609. Note that this means that \fBis\fP only evaluates one of its arguments
  1610. as an arithmetic expression (the right-hand side one),
  1611. whereas all the comparison
  1612. predicates evaluate both their arguments.
  1613. .lp
  1614. \fIZ\fP \fBis\fP \fIX\fP
  1615. .(x b
  1616. (I)     is/2
  1617. .)x
  1618. .ip
  1619. Arithmetic
  1620. expression \fIX\fP is evaluated and the result,
  1621. is unified with
  1622. \fIZ\fP. Fails if \fIX\fP is not an arithmetic expression.  One point to note is
  1623. that in compiled code, the current implementation of \fBis\fR/2 cannot
  1624. handle compound expressions created dynamically: these have to be handled using \fBeval\fR/2.  Thus, for example, the goals
  1625. .(l C
  1626.  ..., X = Y + Z, Y = 3, Z = 2, W is X, ...
  1627. .)l
  1628. would fail, whereas
  1629. .(l C
  1630.  ..., X = Y + Z, Y = 3, Z = 2, eval(X,W), ...
  1631. .)l
  1632. could succeed.
  1633. .ip \fBeval\fR(\fIE\fR,\ \fIX\fR\^)
  1634. .(x b
  1635. (L)     eval/2
  1636. .)x
  1637. Evaluates the arithmetic expression \fIE\fR and unifies the result with the term
  1638. \fIX\fR.  Fails if \fIE\fR is not an arithmetic expression.  The principal difference between \fBeval\fR/2 and \fBis\fR/2 is that in compiled code,
  1639. \fBis\fR/2 cannot handle arithmetic expressions that are compound
  1640. terms constructed at runtime, but \fBeval\fR/2 can.  On the other hand,
  1641. \fBis\fR/2 is more efficient for the evaluation of static arithmetic expressions (i.
  1642. e. expressions that do not have compound subterms created at runtime).
  1643. Another difference is that while \fBis\fR/2 is an inline predicate,
  1644. \fBeval\fR/2 is not; thus, \fBis\fR/2 cannot be redefined by the user,
  1645. but \fBeval\fR/2 can.
  1646. .lp
  1647. \fIX\fP \fB=:=\fP \fIY\fP
  1648. .(x b
  1649. (I)     =:=/2
  1650. .)x
  1651. .ip
  1652. The values of \fIX\fP and \fIY\fP are equal.  If either \fIX\fR or \fIY\fR
  1653. involve compound subexpressions that are created at runtime, they should first be evaluated
  1654. using \fBeval\fR/2.
  1655. .lp
  1656. \fIX\fP \fB=\\=\fP \fIY\fP
  1657. .ip
  1658. .(x b
  1659. (I)     =\e=/2
  1660. .)x
  1661. The values of \fIX\fP and \fIY\fP are not equal.  If either \fIX\fR or \fIY\fR
  1662. involve compound subexpressions that are created at runtime, they should first be evaluated
  1663. using \fBeval\fR/2.
  1664. .lp
  1665. \fIX\fP\fB < \fP\fIY\fP
  1666. .(x b
  1667. (I)     </2
  1668. .)x
  1669. .ip
  1670. The
  1671. value of \fIX\fP is less than the value of \fIY\fP.  If either \fIX\fR or \fIY\fR
  1672. involve compound subexpressions that are created at runtime, they should first be evaluated
  1673. using \fBeval\fR/2.
  1674. .lp
  1675. \fIX\fP\fB > \fP\fIY\fP
  1676. .(x b
  1677. (I)     >/2
  1678. .)x
  1679. .ip
  1680. The
  1681. value of \fIX\fP is greater than the value of \fIY\fP.  If either \fIX\fR or \fIY\fR
  1682. involve compound subexpressions that are created at runtime, they should first be evaluated
  1683. using \fBeval\fR/2.
  1684. .lp
  1685. \fIX\fP\fB =<\fP \fIY\fP
  1686. .(x b
  1687. (I)     =</2
  1688. .)x
  1689. .ip
  1690. The
  1691. value of \fIX\fP is less than or equal to the value of \fIY\fP.  If either \fIX\fR or \fIY\fR
  1692. involve compound subexpressions that are created at runtime, they should first be evaluated
  1693. using \fBeval\fR/2.
  1694. .lp
  1695. \fIX\fP\fB >=\fP \fIY\fP
  1696. .(x b
  1697. (I)     >=/2
  1698. .)x
  1699. .ip
  1700. The
  1701. value of \fIX\fP is greater than or equal to the value of \fIY\fP.  If either \fIX\fR or \fIY\fR
  1702. involve compound subexpressions that are created at runtime, they should first be evaluated
  1703. using \fBeval\fR/2.
  1704. .lp
  1705. \fBfloor\fR(\fIX\fR, \fIY\fR\^)
  1706. .(x b
  1707. (B)    floor/2
  1708. .)x
  1709. .ip
  1710. If \fIX\fR is a floating point number in the call and \fIY\fR is free,
  1711. then \fIY\fR is instantiated to the largest integer whose absolute value
  1712. is not greater than the absolute value of \fIX\fR; if \fIX\fR is
  1713. uninstantiated in the call and \fIY\fR is an integer, then \fIX\fR is instantiated to
  1714. the smallest float not less than \fIY\fR.
  1715. .lp
  1716. \fBfloatc\fR(\fIF\fR, \fIM\fR, \fIE\fR\^)
  1717. .(x b
  1718. (B)    floatc/3
  1719. .)x
  1720. .ip
  1721. If \fIF\fR is a number while \fIM\fR and \fIE\fR are uninstantiated in the call, then
  1722. \fIM\fR is instantiated to a float \fIm\fR (of magnitude less than 1), and \fIE\fR to an
  1723. integer \fIn\fR, such that
  1724. .(l C
  1725. \fIF\fR = \fIm\fR * 2\*[\fIn\fR\*].
  1726. .)l
  1727. If \fIF\fR is uninstantiated in the call while \fIM\fR is a float and \fIE\fR
  1728. an integer, then \fIF\fR becomes instantiated to \fIM\fR * 2\*[\fIE\fR\*].
  1729. .lp
  1730. \fBexp\fR(\fIX\fR, \fIY\fR\^)
  1731. .(x b
  1732. (B)    exp/2
  1733. .)x
  1734. .ip
  1735. If \fIX\fR is instantiated to a number and \fIY\fR is uninstantiated in the call, then \fIY\fR
  1736. is instantiated to \fIe\*[X\*]\fR (where \fIe\fR = 2.71828...); if \fIX\fR
  1737. is uninstantiated in the call while \fIY\fR is instantiated to a positive
  1738. number, then \fIX\fR is instantiated to \fIlog\*<e\*>\fR(\fIY\fR\^).
  1739. .lp
  1740. \fBsquare\fR(\fIX\fR, \fIY\fR\^)
  1741. .(x b
  1742. (B)    square/2
  1743. .)x
  1744. .ip
  1745. If \fIX\fR is instantiated to a number while \fIY\fR is uninstantiated in
  1746. the call, then \fIY\fR becomes instantiated to \fIX\fR\^\*[2\*]; if \fIX\fR
  1747. is uninstantiated in the call while \fIY\fR is instantiated to a positive number, then
  1748. \fIX\fR becomes instantiated to the positive square root of \fIY\fR (if \fIY\fR
  1749. is negative in the call, \fIX\fR becomes instantiated to 0.0).
  1750. .lp
  1751. \fBsin\fR(\fIX\fR, \fIY\fR\^)
  1752. .(x b
  1753. (B)    sin/2
  1754. .)x
  1755. .ip
  1756. If \fIX\fR is instantiated to a number (representing an angle in radians)
  1757. and \fIY\fR is uninstantiated in the call, then \fIY\fR becomes
  1758. instantiated to \fIsin\fR(\fIX\fR\^) (the user should check the magnitude
  1759. of \fIX\fR to make sure that the result is meaningful).  If \fIY\fR is
  1760. instantiated to a number between \-\(*p/2 and \(*p/2 and \fIX\fR is
  1761. uninstantiated in the call, then \fIX\fR becomes instantiated to
  1762. \fIsin\fR\*[\-1\*](\fIY\fR\^).
  1763. .lp
  1764. .sh 2 "Convenience"
  1765. .ip \fIP\fP\ \fB,\fP\ \fIQ\fP
  1766. \fIP\fP and then \fIQ\fP.
  1767. .(x b
  1768. (I)     `,'/2
  1769. .)x
  1770. .ip \fIP\fP\ \fB;\fP\ \fIQ\fP
  1771. \fIP\fP or \fIQ\fP.
  1772. .(x b
  1773. (I)     `;'/2
  1774. .)x
  1775. .lp \fBtrue\fP
  1776. .ip
  1777. Always  succeeds.
  1778. .(x b
  1779. (I)     true/0
  1780. .)x
  1781. .ip \fIX\fP\ \fB=\fP\ \fIY\fP
  1782. Defined as if by the clause \*(lq Z=Z \*(rq, i.e. \fIX\fP and \fIY\fP are unified.
  1783. .(x b
  1784. (I)     =/2
  1785. .)x
  1786. .sh 2 "Extra Control"
  1787. .ip \fB!\fP
  1788. Cut (discard) all choice points made since
  1789. the parent goal started execution.  (The scope of cut in different contexts is discussed in Section 4.2).
  1790. .(x b
  1791. (P)     !/0
  1792. .)x
  1793. .ip \fBnot\fP\ \fIP\fP
  1794. If
  1795. the goal \fIP\fP has a solution, fail,  otherwise  succeed.   It  is
  1796. defined as if by
  1797. .(l
  1798. not(P) :\- P, !, fail.
  1799. not(\(ul\|\|).
  1800. .)l
  1801. .(x b
  1802. (P)     not/1
  1803. .)x
  1804. .ip \fIP\fP\ \fB\->\fP\ \fIQ\fP\ \fB;\fP\ \fIR\fP
  1805. Analogous to 
  1806. \*(lqif \fIP\fP then \fIQ\fP else \fIR\fP\*(rq
  1807. i.e.  defined as if by
  1808. .(l
  1809. P \-> Q ; R :\- P, !, Q.
  1810. P \-> Q ; R :\- R.
  1811. .)l
  1812. .ip \fIP\fP\ \fB\->\fP\ \fIQ\fP
  1813. When
  1814. occurring other  than  as  one  of  the  alternatives  of  a
  1815. disjunction, is equivalent to
  1816. .(l
  1817. \fIP\fP \-> \fIQ\fP ; fail.
  1818. .)l
  1819. .(x b
  1820. (P)     \->/2
  1821. .)x
  1822. .ip \fBrepeat\fP
  1823. Generates an  infinite  sequence  of  backtracking  choices.   It
  1824. is defined by the clauses:
  1825. .(l
  1826. repeat.
  1827. repeat :\- repeat.
  1828. .)l
  1829. .(x b
  1830. (L)     repeat/0
  1831. .)x
  1832. .ip \fBfail\fP
  1833. Always fails.
  1834. .(x b
  1835. (I)     fail/0
  1836. .)x
  1837. .sh 2 "Meta-Logical"
  1838. .ip \fBvar\fP(\fIX\fP\^)
  1839. Tests whether \fIX\fP is currently instantiated to a variable.
  1840. .(x b
  1841. (I)     var/1
  1842. .)x
  1843. .ip \fBnonvar\fP(\fIX\fP\^)
  1844. Tests whether \fIX\fP is currently instantiated to a non-variable term.
  1845. .(x b
  1846. (I)     nonvar/1
  1847. .)x
  1848. .ip \fBatom\fP(\fIX\fP\^)
  1849. .(x b
  1850. (B)     atom/1
  1851. .)x
  1852. Checks
  1853. that \fIX\fP is  currently  instantiated  to  an  atom  (i.e.   a
  1854. non-variable term of arity 0, other than a number).
  1855. .ip \fBinteger\fP(\fIX\fP\^)
  1856. Checks that \fIX\fP is currently instantiated to an integer.
  1857. .(x b
  1858. (B)     integer/1
  1859. .)x
  1860. .lp
  1861. \fBreal\fR(\fIX\fR\^)
  1862. .(x b
  1863. (B)    real/1
  1864. .)x
  1865. .ip
  1866. Checks that \fIX\fP is currently instantiated to a floating point number..
  1867. .)x
  1868. .ip \fBnumber\fP(\fIX\fP\^)
  1869. Checks that \fIX\fP is currently instantiated to a number, i.e. that it is
  1870. either an integer or a real.
  1871. .(x b
  1872. (B)     number/1
  1873. .)x
  1874. .ip \fBatomic\fP(\fIX\fP\^)
  1875. Checks that \fIX\fP is currently instantiated to an atom or number.
  1876. .(x b
  1877. (B)     atomic/1
  1878. .)x
  1879. .lp
  1880. \fBstructure\fR(\fIX\fR\^)
  1881. .(x b
  1882. (B)    structure/1
  1883. .)x
  1884. .ip
  1885. Checks that \fIX\fP is currently instantiated to a compound term, i.e. to a
  1886. nonvariable term that is not atomic.
  1887. .ip \fBfunctor\fP(\fIT\fP,\fIF\fP,\fIN\fP\^)
  1888. The
  1889. principal functor of term \fIT\fP has name \fIF\fP and arity
  1890. \fIN\fP,  where  \fIF\fP
  1891. is  either  an  atom or, provided \fIN\fP is 0, a number.  
  1892. Initially,
  1893. either \fIT\fP must be instantiated to a non-variable, or \fIF\fP and 
  1894. \fIN\fP  must
  1895. be   instantiated   to,   respectively,  either  an  atom  and  a
  1896. non-negative integer or an integer and 0. If these conditions are
  1897. not satisfied, an error message is given.  In the case where \fIT\fP is
  1898. initially instantiated to a variable, the result of the  call  is
  1899. to  instantiate  \fIT\fP  to the most general term having the principal
  1900. functor indicated.
  1901. .(x b
  1902. (L)     functor/3
  1903. .)x
  1904. .ip \fBarg\fP(\fII\fP,\fIT\fP,\fIX\fP\^)
  1905. Initially, \fII\fP must be instantiated to a positive integer and
  1906. \fIT\fP  to
  1907. a  compound  term.  The result of the call is to unify \fIX\fP with the
  1908. \fII\fPth argument of term  \fIT\fP.  (The  arguments  are  numbered
  1909. from  1
  1910. upwards.) If the initial conditions are not satisfied or \fII\fP is out
  1911. of range, the call merely fails.
  1912. .(x b
  1913. (B)     arg/3
  1914. .)x
  1915. .ip \fIX\fP\ \fB=..\fP\ \fIY\fP
  1916. \fIY\fP is a list whose head is the atom corresponding to the principal
  1917. functor  of \fIX\fP and whose tail is the argument list of that functor
  1918. in \fIX\fP. E.g.
  1919. .(x b
  1920. (L)     =../2
  1921. .)x
  1922. .(l
  1923. product(0,N,N\-1) =.. [product,0,N,N\-1]
  1924.  
  1925. N\-1 =.. [\-,N,1]
  1926.  
  1927. product =.. [product]
  1928. .)l
  1929. If \fIX\fP is instantiated to a variable, then \fIY\fP must be instantiated
  1930. either to a list of determinate length whose head is an atom, or to a list of
  1931. length 1 whose head is a number.
  1932. .ip \fBname\fP(\fIX\fP,\fIL\fP)
  1933. If \fIX\fP is an atom or a number then \fIL\fP is a list of the 
  1934. ASCII codes of the characters comprising the name of \fIX\fP. E.g.
  1935. .(x b
  1936. (B)     name/2
  1937. .)x
  1938. .(l
  1939. name(product,[112,114,111,100,117,99,116])
  1940. .)l
  1941. i.e.  name(product,"product").
  1942. .lp
  1943. If \fIX\fP is instantiated to a variable, \fIL\fP must be instantiated
  1944. to a list of ASCII character codes.  E.g.
  1945. .(l
  1946. | ?- name(X,[104,101,108,108,111])).
  1947.  
  1948. X = hello
  1949.  
  1950. | ?- name(X,"hello").
  1951.  
  1952. X = hello
  1953. .)l
  1954. .ip \fBcall\fP(\fIX\fP\^)
  1955. If \fIX\fR is a nonvariable term in the program text, then it is executed exactly as if \fIX\fR appeared in the program
  1956. text instead of \fIcall\fR(\fIX\fR\^), e.g.
  1957. .(x b
  1958. (P)     call/1
  1959. .)x
  1960. .(l
  1961.  ..., p(a), call( (q(X), r(Y)) ), s(X), ...
  1962. .)l
  1963. is equivalent to
  1964. .(l
  1965.  ..., p(a), q(X), r(Y), s(X), ...
  1966. .)l
  1967. However, if X is a variable in the program text, then if at runtime
  1968. \fIX\fP is instantiated to a term which would be acceptable as the body
  1969. of a clause, the goal \fBcall\fP(\fIX\fP) is executed as if that
  1970. term appeared textually in place of the \fBcall\fP(\fIX\fP), \fIexcept that\fR
  1971. any  cut (`!') occurring in \fIX\fP will remove only those choice points
  1972. in \fIX\fP.
  1973. If \fIX\fP is not  instantiated  as  
  1974. described above, an error message is printed and \fBcall\fP fails.
  1975. .ip \fIX\fP
  1976. (where
  1977. \fIX\fP is a variable) Exactly the same as \fBcall\fP(\fIX\fP).  However, we prefer the
  1978. explicit usage of \fIcall\fR/1 as good programming practice, and the use of a
  1979. top level variable subgoal elicits a warning from the compiler.
  1980. .lp
  1981. \fBconlength\fR(\fIC\fR,\fIL\fR\^)
  1982. .ip
  1983. Succeeds if the length of the print name of the constant
  1984. \fIC\fR (which can be an atom, buffer or integer), in bytes, is \fIL\fR.
  1985. .(x b
  1986. (B)     conlength/2
  1987. .)x
  1988. If \fIC\fR is a buffer (see Section 5.8), it
  1989. is the length of the buffer; if \fIC\fR is an integer, it is the
  1990. length of the decimal representation of that integer, i.e., the
  1991. number of bytes that a $\fIwritename\fR will use.
  1992. .sh 2 "Sets"
  1993. .pp
  1994. When  there  are  many solutions to a problem, and when all those solutions are
  1995. required  to  be  collected  together,  this  can  be  achieved  by  repeatedly
  1996. backtracking  and gradually building up a list of the solutions.  The following
  1997. evaluable predicates are provided to automate this process.
  1998. .ip \fBsetof\fP(\fIX\fP,\fIP\fP,\fIS\fP)
  1999. Read this as \*(lq\fIS\fP is the set of all instances of \fIX\fP  such  that
  2000. \fIP\fP  is
  2001. provable''.  If \fIP\fR is not provable, \fBsetof\fP(\fIX\fP,\fIP\fP,\fIS\fP)
  2002. succeeds with \fIS\fR instantiated to the empty list [\^].
  2003. .(x b
  2004. (L)     setof/3
  2005. .)x
  2006. The term \fIP\fP specifies a
  2007. goal or goals as in \fBcall\fP(\fIP\fP). 
  2008. \fIS\fP is a set of terms represented as  a
  2009. list  of those terms, without duplicates, in the standard order for
  2010. terms (see Section 5.7).  
  2011. If there are uninstantiated variables in \fIP\fP which do not also appear
  2012. in \fIX\fP, then a  call  to  this  evaluable  predicate  may  backtrack,
  2013. generating  alternative  values  for  \fIS\fP  corresponding to different
  2014. instantiations of the free variables of \fIP\fP.  Variables occurring in \fIP\fP will not be treated as free if they are
  2015. explicitly bound within \fIP\fP by an existential quantifier.  An
  2016. existential quantification is written:
  2017. .(l
  2018. \fIY\fP^\fIQ\fP
  2019. .)l
  2020. meaning \*(lqthere exists a \fIY\fP such that \fIQ\fP is true\*(rq,
  2021. where  \fIY\fP is some Prolog term (usually, a variable, or tuple or list of
  2022. variables).
  2023. .ip \fBbagof\fP(\fIX\fP,\fIP\fP,\fIBag\fP)
  2024. This is the same as \fBsetof\fP except that the list (or
  2025. alternative lists) returned will not be ordered,  and  may  contain
  2026. duplicates.  If \fIP\fR is unsatisfiable, \fIbagof\fR succeeds binding
  2027. \fIBag\fR to the empty list.
  2028. .(x b
  2029. (L)     bagof/3
  2030. .)x
  2031. The effect of this relaxation is to save considerable
  2032. time and space in execution.
  2033. .ip \fBfindall\fR(\fIX\fR,\fIP\fR,\fIL\fR)
  2034. Similar to \fIbagof\fR/3, except that variables in \fIP\fR that do not occur in \fIX\fR are
  2035. treated as local, and alternative lists are not returned for different bindings of such
  2036. variables.  The list \fIL\fR is, in general, unordered, and may contain duplicates.  If \fIP\fR
  2037. is unsatisfiable, \fIfindall\fR succeeds binding \fIS\fR to the empty list.
  2038. .lp
  2039. \fIX\fP\fB^\fP\fIP\fP
  2040. .ip
  2041. The system recognises this as meaning \*(lqthere exists an \fIX\fP  such
  2042. that  \fIP\fP  is true\*(rq, and treats it as equivalent to \fBcall\fP(\fIP\fP).
  2043. The use of this explicit existential
  2044. quantifier outside the \fBsetof\fP and \fBbagof\fP
  2045. constructs is superfluous.
  2046. .sh 2 "Comparison of Terms"
  2047. .pp
  2048. These  evaluable  predicates  are  meta-logical.    They  treat  uninstantiated
  2049. variables as objects  with  values  which  may  be  compared,  and  they  never
  2050. instantiate those variables.  They should \fInot\fP be used when what you really want
  2051. is arithmetic comparison (Section 5.2) or unification.
  2052. .pp
  2053. The  predicates  make reference to a standard total ordering of terms, which is
  2054. as follows:
  2055. .ip \(de
  2056. variables, in a standard order (roughly, oldest first \- the order  is
  2057. \fInot\fP related to the names of variables);
  2058. .ip \(de
  2059. numbers, from \-\*(lqinfinity\*(rq to +\*(lqinfinity\*(rq;
  2060. .ip \(de
  2061. atoms, in alphabetical (i.e. ASCII) order;
  2062. .ip \(de
  2063. complex  terms, ordered first by arity, then by the name of principal
  2064. functor, then by the arguments (in left-to-right order).
  2065. .pp
  2066. For example, here is a list of terms in the standard order:
  2067. .(l
  2068. [ X, \-9, 1, fie, foe, fum, X = Y, fie(0,2), fie(1,1) ]
  2069. .)l
  2070. These are the basic predicates for comparison of arbitrary terms:
  2071. .lp 
  2072. \fIX\fP \fB==\fP \fIY\fP
  2073. .ip
  2074. Tests if the terms currently instantiating \fIX\fP and  \fIY\fP
  2075. are  literally
  2076. identical  (in particular, variables in equivalent positions in the
  2077. two terms must be identical).
  2078. .(x b
  2079. (B)     ==/2
  2080. .)x
  2081. For example, the question
  2082. .(l
  2083. | ?- X == Y.
  2084. .)l
  2085. fails (answers \*(lqno\*(rq) because \fIX\fP and \fIY\fP
  2086. are  distinct  uninstantiated
  2087. variables.  However, the question
  2088. .(l
  2089. | ?- X = Y, X == Y.
  2090. .)l
  2091. succeeds because the first goal unifies the two variables (see page 42).
  2092. .lp
  2093. \fIX\fP \fB\e==\fP \fIY\fP
  2094. .ip
  2095. Tests  if  the  terms  currently  instantiating  \fIX\fP  and  \fIY\fP  are 
  2096. not literally identical.
  2097. .(x b
  2098. (B)     \e==/2
  2099. .)x
  2100. .EQ
  2101. .nr 99 \n(.s
  2102. .nr 98 \n(.f
  2103. .ps 11
  2104. .ft 2
  2105. .ps \n(99
  2106. .ft \n(98
  2107. .EN
  2108. .lp
  2109. \fIT1\fP \fB@<\fP \fIT2\fP
  2110. .ip
  2111. Term \fIT1\fP is before term \fIT2\fP in the standard order.
  2112. .(x b
  2113. (B)     @</2
  2114. .)x
  2115. .lp
  2116. \fIT1\fP \fB@>\fP \fIT2\fP
  2117. .ip
  2118. Term \fIT1\fP is after term \fIT2\fP in the standard order.
  2119. .(x b
  2120. (B)     @>/2
  2121. .)x
  2122. .lp
  2123. \fIT1\fP \fB@=<\fP \fIT2\fP
  2124. .ip
  2125. Term \fIT1\fP is not after term \fIT2\fP in the standard order.
  2126. .(x b
  2127. (B)     @=</2
  2128. .)x
  2129. .lp
  2130. \fIT1\fP \fB@>=\fP \fIT2\fP
  2131. .ip
  2132. Term \fIT1\fP is not before term \fIT2\fP in the standard order.
  2133. .(x b
  2134. (B)     @>=/2
  2135. .)x
  2136. .sp
  2137. .lp
  2138. Some further predicates involving comparison of terms are:
  2139. .lp
  2140. \fBcompare\fP(\fIOp\fP,\fIT1\fP,\fIT2\fP)
  2141. .ip
  2142. The  result  of comparing terms \fIT1\fP and \fIT2\fP is \fIOp\fP,
  2143. where the possible
  2144. values for \fIOp\fP are:
  2145. .(l
  2146. \&`='   if \fIT1\fP is identical to \fIT2\fP,
  2147.  
  2148. \&`<'   if \fIT1\fP is before \fIT2\fP in the standard order,
  2149.  
  2150. \&`>'   if \fIT1\fP is after \fIT2\fP in the standard order.
  2151. .)l
  2152. Thus \fBcompare\fP(=,\fIT1\fP,\fIT2\fP) is equivalent to
  2153. \fIT1\fP \fB==\fP \fIT2\fP.
  2154. .(x b
  2155. (B)     compare/3
  2156. .)x
  2157. .lp
  2158. \fBsort\fP(\fIL1\fP,\fIL2\fP)
  2159. .ip
  2160. The elements of the list \fIL1\fP are sorted into the standard order, and
  2161. any identical (i.e. `==') elements are merged,  yielding  the  list
  2162. \fIL2\fP.
  2163. .(x b
  2164. (L)    sort/2
  2165. .)x
  2166. .EQ
  2167. .nr 99 \n(.s
  2168. .nr 98 \n(.f
  2169. .ps 11
  2170. .ft 2
  2171. .ps \n(99
  2172. .ft \n(98
  2173. .EN
  2174. .sh 2 "Buffers"
  2175. .pp
  2176. SB-Prolog supports the concept of buffers.  A buffer is actually a
  2177. constant and the characters that make up the buffer is the name of
  2178. the constant.  However, the
  2179. symbol table entry for a buffer is not hashed and thus is not added to
  2180. the obj-list, so two different buffers will never unify.
  2181. Buffers can be allocated either in permanent space or
  2182. on the heap.  Buffers in permanent space stay there forever; buffers
  2183. on the heap are deallocated when the ``allocate buffer'' goal is
  2184. backtracked over.  
  2185. .pp
  2186. A buffer allocated on the heap can either be a simple buffer, or it
  2187. can be allocated as a subbuffer of another buffer already on the
  2188. heap.  A subbuffer will be deallocated when its superbuffer is
  2189. deallocated.  
  2190. .pp
  2191. There are occasions when it is not known, in advance, exactly how much
  2192. space will be required and so how big a buffer should be allocated.
  2193. Sometimes this problem can be overcome by allocating a large buffer
  2194. and then, after using as much as is needed, returning the rest of the
  2195. buffer to the system. This can be done, but only under \fIvery\fR limited
  2196. circumstances: a buffer is allocated from the end of the permanent space,
  2197. the top of the heap, or from the next available space in the superbuffer;
  2198. if no more space has been used beyond the end of the buffer, a tail 
  2199. portion of the buffer can be returned to the system. This operation
  2200. is called ``trimming'' the buffer. 
  2201. .pp
  2202. The following is a list of library predicates for buffer management
  2203. (predicates whose names begin with `$' are low-level routines intended only
  2204. for serious hackers willing to put up with little or no protection):
  2205. .sp
  2206. .ip \fBalloc_\|perm\fR(\fISize\fR,\ \fIBuff\fR)
  2207. Allocates a buffer with a length \fISize\fR in the permanent (i.e. program) area. \fISize\fR must be bound to
  2208. a number. On successful return, \fIBuff\fR will be bound to the allocated buffer.
  2209. .(x b
  2210. (L)     alloc_\|perm/2
  2211. .)x
  2212. The buffer, being in the permanent area, is never de-allocated.
  2213. .ip \fBalloc_\|heap\fR(\fISize\fR,\ \fIBuff\fR)
  2214. Allocates a buffer of size \fISize\fR on the heap and binds \fIBuff\fR to
  2215. it. Since it is on the heap, it will be deallocated on backtracking. 
  2216. .(x b
  2217. (L)     alloc_\|heap/2
  2218. .)x
  2219. .lp
  2220. \fB$alloc_\|buff\fR(\fISize\fR,\fIBuff\fR,\fIType\fR,\fISupbuff\fR,\fIRetcode\fR)
  2221. .ip
  2222. Allocates a buffer.  \fISize\fR is the length (in bytes) of the buffer to
  2223. allocate;
  2224. .(x b
  2225. (L)     $alloc_\|heap/5
  2226. .)x
  2227. \fIBuff\fR is the buffer allocated, and should be unbound at
  2228. the time of the call; \fIType\fR indicates where to allocate the
  2229. buffer: a value of 0 indicates that the buffer is to be allocated
  2230. in permanent space, 1 that it should be on the heap, and 2 indicates
  2231. that it should be allocated from a larger heap buffer; \fISupbuff\fR is
  2232. the larger  buffer to allocate a subbuffer out of, and is only looked at
  2233. if the value of \fIType\fR is 2; \fIRetcode\fR is the return code: a
  2234. value of 0 indicates that the buffer has been allocated, while a value
  2235. of 1 indicates that the buffer could not be allocated due to lack of
  2236. space.  The arguments \fISize\fR, \fIType\fR, and \fISupbuff\fR (if
  2237. \fIType\fR = 2) are input arguments, and should be bound at the time
  2238. of the call; \fIBuff\fR and \fIRetcode\fR are output
  2239. arguments, and should be unbound at the time of the call.
  2240. .ip \fBtrimbuff\fR(\fIType\fR,\ \fIBuff\fR,\ \fINewlen\fR)
  2241. This allows (in some very restricted circumstances) the changing of
  2242. the size of a buffer. \fIType\fR is 0 if the buffer is permanent, 1 if the buffer
  2243. is on the heap. \fIBuff\fR is the buffer.
  2244. .(x b
  2245. (L)     trimbuff/3
  2246. .)x
  2247. \fINewlen\fR is an integer: the size (which
  2248. should be smaller than the original length of the buffer) to make
  2249. the buffer. If the buffer is at the top of the heap (if heap buffer) or the
  2250. end of the program area (if permanent) then the heap-top (or program area top)
  2251. will be readjusted down.  The length of the buffer will be modified to
  2252. \fINewlen\fR.  This is (obviously) a very low-level
  2253. primitive and is for hackers only to implement grungy stuff.
  2254. .lp
  2255. \fB$trim_\|buff\fR(\fISize\fR,\fIBuff\fR,\fIType\fR,\fISupbuff\fR\^)
  2256. .ip
  2257. Trims the buffer \fIBuff\fR (possibly contained in superbuffer
  2258. \fISupbuff\fR) of type \fIType\fR to \fISize\fR bytes. 
  2259. .(x b
  2260. (L)     $trim_\|buff/4
  2261. .)x
  2262. The value of
  2263. \fIType\fR indicates where the buffer is located: a value of 0 denotes a buffer in permanent
  2264. space, a value of 1 a buffer on the heap, and a value of 2 a buffer within
  2265. another buffer (the superbuffer).
  2266. All the arguments are input arguments,
  2267. and should be instantiated at the time of call (with the possible exception
  2268. of \fISupbuff\fR, which is looked at only if \fIType\fR is 2).
  2269. The internal length of the buffer \fIBuff\fR is changed to
  2270. \fISize\fR.
  2271. .lp
  2272. \fBconlength\fR(\fIConstant\fR,\fILength\fR\^)
  2273. .ip
  2274. Succeeds if the length of the print name of the constant
  2275. \fIConstant\fR (which can be an atom, buffer
  2276. or integer), in bytes, is \fILength\fR.
  2277. If \fIConstant\fR is a buffer, it
  2278. is the length of the buffer; if \fIConstant\fR is an integer, it is the
  2279. length of the decimal representation of that integer, i.e., the
  2280. number of bytes that a $\fIwritename\fR will use.
  2281. .sh 2 "Modification of the Program"
  2282. .pp
  2283. The predicates defined in this section allow modification of the program
  2284. as it is actually running.
  2285. Clauses can be added to the program (\fIasserted\fP) or removed from the program
  2286. (\fIretracted\fP).
  2287. At the lowest level, the system supports the asserting of clauses with upto
  2288. one literal in the body.  It
  2289. does this by allocating a buffer and compiling code for the clause into
  2290. that buffer.  Such a buffer is called a ``clause reference'' (\fIclref\fR\|).
  2291. The clref is then added to a chain of clrefs.  The chain of clrefs
  2292. has a header, which is a small buffer called a ``predicate reference''
  2293. (\fIprref\fR\|), which contains pointers to the beginning and end of its
  2294. chain of clrefs.  Clause references are quite similar to ``database references'' of C-Prolog, and can be called.
  2295. .pp
  2296. A prref is normally associated with a predicate symbol and contains
  2297. the clauses that have been asserted for that symbol.  However, prrefs
  2298. can  be constructed and clrefs added to them, and such clauses can be
  2299. called, all without associating that prref with any particular predicate
  2300. symbol.  A predicate symbol that  has an associated
  2301. prref is called a ``dynamic'' predicate (other  predicate types are
  2302. ``compiled'' and ``undefined'').  
  2303. .pp
  2304. There are options as to where clrefs are  allocated and  how they are
  2305. linked to a chain of  clrefs associated  with a  prref.   A clref can
  2306. either be allocated in permanent space (the normal arrangement) or as
  2307. a sub-buffer of a superbuffer on the heap.  When adding a  clref to a
  2308. prref chain, it can be be added at the beginning  of the  chain or at
  2309. the end of the chain.  In addition, one of the argument positions can
  2310. be used to index, so that subsequent retrievals will try  to index on
  2311. that position.
  2312. .sp
  2313. .lp
  2314. \fBassert\fP(\fIC\fP)
  2315. .(x b
  2316. (L)     assert/1
  2317. .)x
  2318. .ip
  2319. The
  2320. current instance of \fIC\fP is interpreted as a clause and is added
  2321. to the program (with new private variables
  2322. replacing any uninstantiated variables), at the end of the list of
  2323. clauses for that predicate.
  2324. \fIC\fP must be instantiated to a non-variable.
  2325. .lp
  2326. \fBassert\fR(\fIC\fR, \fIOpts\fR)
  2327. .(x b
  2328. (L)     assert/2
  2329. .)x
  2330. .ip
  2331. As for \fIassert\fR/1, with \fIOpts\fR a list of options.  The options
  2332. currently recognized are:
  2333. .lp
  2334.    \fBfirst\fR
  2335. .ip
  2336. If on, causes the asserted clause to be added as the first clause of
  2337. the database.  Default: off.
  2338. .lp
  2339.    \fBindex\fR
  2340. .ip
  2341. If on, causes an index to be generated on the first argument of the
  2342. clause.  Default: off.
  2343. .lp
  2344.    \fBindex(N)\fR
  2345. .ip
  2346. If on, causes an index to be generated on the N\*[th\*] argument of the
  2347. clause.  Default: off.
  2348. .lp
  2349.    \fBq\fR
  2350. .ip
  2351. ``quick''.  If on, invokes an assertion algorithm that is simpler and
  2352. more efficient than
  2353. the general one.  However, the code generated with the simpler algorithm
  2354. may not be correct if there are repeated variables within compound terms
  2355. in the body of the clause, e.g. in
  2356. .(l
  2357.     p(X) :\- q([X, X]).
  2358. .)l
  2359. Default: off.
  2360. .lp
  2361. \fBasserti\fR(\fIC\fR,\fIN\fR\^)
  2362. .(x b
  2363. (L)     asserti/2
  2364. .)x
  2365. .ip
  2366. The current instance of \fIC\fR, interpreted as a clause, is asserted to
  2367. the program with an index on its \fIN\^\*[th\*]\fR argument.  If \fIN\fR is
  2368. zero, no index is created.
  2369. .lp
  2370. \fBasserta\fR(\fIC\fR\^)
  2371. .(x b
  2372. (L)    asserta/1
  2373. .)x
  2374. .ip
  2375. Similar to \fBassert\fR(\fIC\fR\^), except that the new clause becomes the
  2376. \fIfirst\fR clause of the procedure concerned.
  2377. .lp
  2378. \fBasserta\fR(\fIC\fR, \fIRef\fR\^)
  2379. .(x b
  2380. (L)    asserta/2
  2381. .)x
  2382. .ip
  2383. Similar to \fBasserta\fR(\fIC\fR\^), but also unifies \fIRef\fR with the
  2384. clause reference of the clause asserted.
  2385. .lp
  2386. \fBassertz\fR(\fIC\fR\^)
  2387. .(x b
  2388. (L)    assertz/1
  2389. .)x
  2390. .ip
  2391. Similar to \fBassert\fR(\fIC\fR\^), except that the new clause becomes the
  2392. \fIlast\fR clause of the procedure concerned.
  2393. .lp
  2394. \fBassertz\fR(\fIC\fR, \fIRef\fR\^)
  2395. .(x b
  2396. (L)    assertz/2
  2397. .)x
  2398. .ip
  2399. Similar to \fBassertz\fR(\fIC\fR\^), but also unifies \fIRef\fR with the
  2400. clause reference of the clause asserted.
  2401. .lp
  2402. \fBassert_\|union\fR(\fIP\fR, \fIQ\fR)
  2403. .ip
  2404. The clauses for \fIQ\fR are added to the clauses for \fIP\fR.
  2405. .(x b
  2406. (L)     assert_\|union/2
  2407. .)x
  2408. For example, the call
  2409. .(l
  2410.     | ?- assert_\|union(p(X,Y),q(X,Y)).
  2411. .)l
  2412. has the effect of adding the rule
  2413. .(l
  2414. p(X,Y) :\- q(X,Y).
  2415. .)l
  2416. as the last rule defining \fIp\fR/2.  If \fIP\fR is not defined, it results
  2417. in the call to \fIQ\fR being the only clause for \fIP\fR.
  2418.  
  2419. The variables in the arguments to
  2420. \fIassert_\^union\fR/2 are not significant, e.g. the above would have been
  2421. equivalent to
  2422. .(l
  2423.     | ?- assert_\|union(p(Y,X),q(X,Y)).
  2424. or
  2425.     | ?- assert_\|union(p(_\|,_\|),q(_\|,_\|)).
  2426. .)l
  2427. However, the arities of the two predicates involved must match, e.g.
  2428. even though the goal
  2429. .(l
  2430.     | ?- assert_\|union(p(X,Y), r(X,Y,Z)).
  2431. .)l
  2432. will succeed, the predicate \fIp\fR/2 will not in any way depend on the
  2433. clauses for \fIr\fR/3.
  2434. .lp
  2435. \fBassert\fR(\fIClause\fR,\fIAZ\fR,\fIIndex\fR,\fIClref\fR\^)
  2436. .ip
  2437. Asserts a clause to a predicate.  \fIClause\fR is the clause to assert.
  2438. .(x b
  2439. (L)     assert/4
  2440. .)x
  2441. \fIAZ\fR is 0 for insertion as the first clause, 1 for insertion as the last clause.
  2442. \fIIndex\fR is the number of the argument on which to  index (0 for
  2443. no indexing).  \fIClref\fR is returned  as the  clause reference of
  2444. the fact newly asserted.  If the main functor symbol of \fIClause\fR has
  2445. been declared (by \fI$assertf_\|alloc_\|t\fR/2, see below) to have its
  2446. clauses on the heap, the clref will be allocated there. If the predicate
  2447. symbol of \fIClause\fR is undefined, it will be initialized and \fIClause\fR
  2448. added. If the predicate symbol has compiled clauses, it is first
  2449. converted to be dynamic (see \fIsymtype\fR/2, Section 5.10) by adding a special clref that calls the
  2450. compiled clauses.  \fIFact\fR, \fIAZ\fR and \fIIndex\fR are input arguments,
  2451. and should be instantiated at the time of call; \fIClref\fR is an output
  2452. argument, and should be uninstantiated at the time of call.
  2453. .lp
  2454. \fBretract\fP(\fIHead\fP\^)
  2455. .ip
  2456. The first clause in the program whose head matches \fIHead\fP is erased.
  2457. This predicate may be used in a non-deterministic fashion, i.e. it will
  2458. successively backtrack to retract clauses whose heads match \fIHead\fR.
  2459. .(x b
  2460. (L)     retract/1
  2461. .)x
  2462. \fIHead\fP must be initially instantiated to a non-variable.
  2463. .lp
  2464. \fBabolish\fP(\fIP\fP)
  2465. .ip
  2466. Completely
  2467. remove all clauses for the procedure with head \fIP\fP (which should be
  2468. a term).  
  2469. .(x b
  2470. (L)     abolish/1
  2471. .)x
  2472. For example, the goal
  2473. .(l
  2474. | ?- abolish( p(_\|, _\|, _\|) ).
  2475. .)l
  2476. removes all clauses for the predicate \fIp\fR/3.
  2477. .lp
  2478. \fBabolish\fR(\fIP\fR, \fIN\fR\^)
  2479. .ip
  2480. Completely remove all clauses for the predicate \fIP\fR (which should be an
  2481. atom) with arity \fIN\fR (which should be an integer).
  2482. .(x b
  2483. (L)     abolish/2
  2484. .)x
  2485. .lp
  2486. \fBcall_\^ref\fR(\fICall\fR, \fIRef\fR)
  2487. .(x b
  2488. (L)     call_\|ref/2
  2489. .)x
  2490. .ip
  2491. Calls the predicate whose database reference (prref) is \fIRef\fR, using the
  2492. literal \fICall\fR as the call.  This is similar to
  2493. \fBcall_\|ref\fR(\fICall\fR, \fIRef\fR, 0).
  2494. .lp
  2495. \fBcall_\^ref\fR(\fICall\fR, \fIRef\fR, \fITr\fR\^)
  2496. .(x b
  2497. (L)     call_\|ref/3
  2498. .)x
  2499. .ip
  2500. Calls the predicate whose database reference (prref) is \fIRef\fR, using the
  2501. literal \fICall\fR as the call.  \fITr\fR must be either 0 or 1: if \fITr\fR
  2502. is 0 then the call \fICall\fR is made assuming the ``trust'' optimization
  2503. will be made; if \fITr\fR is 1 then the ``trust'' optimization is not used,
  2504. so that any new fact added
  2505. before final failure will be seen by \fICall\fR.  (Also, this currently
  2506. does not take advantage of any indexing that might have been constructed,
  2507. while \fI$db_\|call_\|prref\fR does.)  \fICall\fR, \fIRef\fR and \fITr\fR
  2508. are all input arguments, and should be instantiated at the time of call.
  2509. .sp
  2510. .lp
  2511. The basic library predicates that support the manipulation of
  2512. prrefs and clrefs are as follows:  
  2513. .lp
  2514. \fB$db_\|new_\|prref\fR(\fIPrref\fR,\fIWhere\fR,\fISupbuff\fR\^)
  2515. .ip
  2516. Creates an empty Prref, i.e. one with no facts in it. 
  2517. .(x b
  2518. (L)     $db_\|new_\|prref/3
  2519. .)x
  2520. If called, it will simply fail.  \fIWhere\fR
  2521. indicates where the prref should be allocated:  a value of
  2522. 0 indicates the permanent area, while a value of 2 indicates that
  2523. it is to be allocated as a subbuffer.  \fISupbuff\fR is the superbuffer
  2524. from which to allocate \fIPrref\fR if \fIWhere\fR is 2. \fIWhere\fR
  2525. should be instantiated at the time of call, while \fIPrref\fR should be
  2526. uninstantiated; in addition, if \fIWhere\fR is 2, \fISupbuff\fR
  2527. should be instantiated at the time of call.
  2528. .lp
  2529. \fB$db_\|assert_\|fact\fR(\fIFact\fR,\fIPrref\fR,\fIAZ\fR,\fIIndex\fR,\fIClref\fR,\fIWhere\fR,\fISupbuff\fR)
  2530. .ip
  2531. \fIFact\fR is a fact to be asserted; \fIPrref\fR is a predicate
  2532. reference to which to add the asserted fact;
  2533. .(x b
  2534. (L)     $db_\|assert_\|fact/5
  2535. .)x
  2536. \fIAZ\fR is either 0,
  2537. indicating the fact should be inserted as the first clause in \fIPrref\fR,
  2538. or 1, indicating it should be inserted as the last; \fIIndex\fR is 0
  2539. if no index is to be built, or \fIn\fR if an index on the \fIn\fR\*[th\*]
  2540. argument of the fact is to be used.  (Asserting at the beginning of the
  2541. chain with indexing is not yet supported.)  \fIWhere\fR indicates where
  2542. the clref is to be allocated: a value of 0 indicates that it should be
  2543. in the permanent area, while a value of 2 indicates that it should be
  2544. allocated
  2545. as a subbuffer of \fISupbuff\fR.  \fIClref\fR is returned and
  2546. it  is  the  clause reference  of the  asserted fact.   \fIFact\fR, \fIPrref\fR, \fIAZ\fR,
  2547. \fIIndex\fR and \fIWhere\fR are input arguments, and should be instantiated at the
  2548. time of call; in addition, if \fIWhere\fR is 2, then \fISupbuff\fR should also
  2549. be instantiated.  \fIClref\fR is an output argument, and should be uninstantiated at the
  2550. time of call.
  2551. .lp
  2552. \fB$db_\|add_\|clref\fR(\fIFact\fR,\fIPrref\fR,\fIAZ\fR,\fIIndex\fR,\fIClref\fR,\fIWhere\fR,\fISupbuff\fR\^)
  2553. .ip
  2554. Adds the clref \fIClref\fR to the prref \fIPrref\fR.  
  2555. .(x b
  2556. (L)     $db_\|add_\|clref/7
  2557. .)x
  2558. \fIFact\fR is the fact that has been compiled into \fIClref\fR
  2559. (used only to get the arity and for indexing).  The other parameters
  2560. are as for \fI$db_\|assert_\|fact\fR/7.
  2561. .lp
  2562. \fB$db_\|call_\|prref\fR(\fICall\fR,\fIPrref\fR)
  2563. .ip
  2564. Calls the prref \fIPrref\fR using the literal \fICall\fR as the call.
  2565. .(x b
  2566. (L)     $db_\|call_\|prref/2
  2567. .)x
  2568. The call is done by simply branching to the first
  2569. clause.  New facts added to \fIPrref\fR after the last fact has been retrieved
  2570. by \fICall\fR, but before \fICall\fR is failed through, will \fInot\fR
  2571. be used.  Both \fICall\fR and \fIPrref\fR are
  2572. input arguments, and should be instantiated at the time of call.
  2573. .lp
  2574. \fB$db_\|call_\|prref_\|s\fR(\fICall\fR,\fIPrref\fR)
  2575. .ip
  2576. This also  calls the prref \fIPrref\fR using \fICall\fR as the call.
  2577. .(x b
  2578. (L)     $db_\|call_\|prref_\|s/2
  2579. .)x
  2580. The  difference from \fI$db_\|call_\|prref\fR is that this does not use
  2581. the ``trust'' optimization, so that any new fact added
  2582. before final failure will be seen by \fICall\fR.  (Also, this currently
  2583. does not take advantage of any indexing that might have been constructed,
  2584. while \fI$db_\|call_\|prref\fR does.)  Both \fICall\fR and \fIPrref\fR are
  2585. input arguments, and should be instantiated at the time of call.
  2586. .lp
  2587. \fB$db_\|get_\|clauses\fR(\fIPrref\fR,\fIClref\fR,\fIDir\fR)
  2588. .ip
  2589. This returns, nondeterministically, all the clause references \fIClref\fR
  2590. for clauses asserted to prref \fIPrref\fR.
  2591. .(x b
  2592. (L)     $db_\|get_\|clauses/3
  2593. .)x
  2594. If \fIDir\fR is 0, then the
  2595. first clref on the list is returned first; if \fIDir\fR is 1, then they
  2596. are returned in reverse order.  \fIPrref\fR and \fIDir\fR are input
  2597. arguments, and should be instantiated at the time of call; \fIClref\fR
  2598. is an output argument, and should be uninstantiated at the time of call.
  2599. .lp
  2600. \fB$db_\|kill_\|clause\fR(\fIClref\fR\^)
  2601. .ip
  2602. This predicate retracts the fact referenced by clref \fIClref\fR.
  2603. .(x b
  2604. (L)     $db_\|kill_\|clause/1
  2605. .)x
  2606. It does this by simply making the first instruction of the clause
  2607. a \fIfail\fR instruction.  This means that this clause will fail
  2608. whenever it is called in the future, no matter how it is accessed.
  2609. \fIClref\fR should be instantiated at the time of call.
  2610. .sh 2 "Environmental"
  2611. .lp
  2612. \fBop\fR(\fIpriority\fR, \fItype\fR, \fIname\fR\^)
  2613. .ip
  2614. Treat \fIname\fR as an operator of the stated \fItype\fR and \fIpriority\fR (see
  2615. Section 3.2).  \fIname\fR may also be a list of names, in which all are to
  2616. be treated as operators of the stated \fItype\fR and \fIpriority\fR.
  2617. .lp
  2618. \fBbreak\fR
  2619. .(x b
  2620. (L)    break/0
  2621. .)x
  2622. .ip
  2623. Causes the current execution to be suspended at the next procedure call.
  2624. Then the message ``[ Break (level 1) ]'' is displayed.  The interpreter is
  2625. then ready to accept input as though it was at the top level (except that
  2626. at break level \fIn\fR > 0, the prompt is ``\fIn\fR: ?- '').  If another
  2627. call of \fBbreak\fR is encountered, it moves up to level 2, and so on.  To
  2628. close the break and resume the execution which was suspended, type the
  2629. \s-2END-OF-INPUT\s+2 character.  Execution will be resumed at the procedure
  2630. call where it had been suspended.  Alternatively, the suspended execution
  2631. can be aborted by calling the evaluable predicate \fBabort\fR, which
  2632. causes a return to the top level.
  2633. .lp
  2634. \fBabort\fR
  2635. .(x b
  2636. (B)    abort/0
  2637. .)x
  2638. .ip
  2639. Aborts the current execution, taking you back to top level.
  2640. .lp
  2641. \fBsave\fR(\fIF\fR\^)
  2642. .(x b
  2643. (B)    save/1
  2644. .)x
  2645. .ip
  2646. The system saves the current state of the system into file \fIF\fR.
  2647. .lp
  2648. \fBrestore\fR(\fIF\fR\^)
  2649. .(x b
  2650. (B)    restore/1
  2651. .)x
  2652. .ip
  2653. The system restores the saved state in file \fIF\fR to be the current state.
  2654. One restriction imposed by the current system is that various system parameters
  2655. (e.g. stack sizes, permanent space, heap space, etc.) of the saved state
  2656. have to be the same as that of the current invocation.  Thus, it is not
  2657. possible to save a state from an invocation where 50000 words of permanent
  2658. space had been allocated, and then restore the same state in an invocation
  2659. with 100000 words of permanent space.
  2660. .lp
  2661. \fBcputime\fR(\fIX\fR)
  2662. .ip
  2663. Unifies \fIX\fR with the time elapsed, in milliseconds, since
  2664. the system was started up.
  2665. .(x b
  2666. (B)     cputime/1
  2667. .)x
  2668. .lp
  2669. \fB$getenv\fR(\fIVar\fR,\fIVal\fR\^)
  2670. .ip
  2671. \fIVal\fR is unified with the value of the Unix environment variable \fIVar\fR.
  2672. .(x b
  2673. (L)     $getenv/2
  2674. .)x
  2675. .lp
  2676. \fBstatistics\fR
  2677. .ip
  2678. Prints out the current allocations and amounts of space used for each of
  2679. the four main areas: the permanent area, the local stack, the global stack
  2680. and the trail stack.
  2681. .(x b
  2682. (B)     statistics/0
  2683. .)x
  2684. Does not work well unless the simulator has been called
  2685. with the -\fBs\fR option (see Section 7.2).
  2686. .lp
  2687. \fBsymtype\fR(\fIT\fR,\fIN\fR\^)
  2688. .ip
  2689. Unifies \fIN\fR with the ``internal type'' of the principal functor of the term \fIT\fR,
  2690. which must be instantiated at the time of the call.
  2691. .(x b
  2692. (B)     symtype/2
  2693. .)x
  2694. \fIN\fR is bound to 0 if
  2695. \fIT\fR does not have an entry point defined (i.e. cannot be executed);
  2696. to 1 if the principal functor of \fIT\fR is ``dynamic'', i.e. has asserted code; to 2
  2697. if the principal functor for \fIT\fR is a compiled predicate; and
  2698. 3 if \fIT\fR denotes a buffer.  Thus, for example, if the predicate \fIp\fR/2
  2699. is a compiled predicate which has been loaded into the system, the goal
  2700. .(l
  2701.     | ?- symtype(p(_\|,_\|), X).
  2702. .)l
  2703. will succeed binding X to 2; on the other hand, the goal
  2704. .(l
  2705.     | ?- assert(q(a,b,c)), symtype(q(_\|,_\|,_\|), X).
  2706. .)l
  2707. will succeed binding X to 1.
  2708. .lp
  2709. \fBsystem\fR(\fICall\fR)
  2710. .ip
  2711. Calls the operating system with the atom \fICall\fR as argument.
  2712. .(x b
  2713. (B)     system/1
  2714. .)x
  2715. For example, the call
  2716. .(l
  2717. | ?- system('ls').
  2718. .)l
  2719. will produce a directory listing.  Since \fIsystem\fR/1 is executed by
  2720. forking off a shell process, it cannot be used, for example, to change the working
  2721. directory of the simulator.
  2722. .lp
  2723. \fBsyscall\fR(\fIN\fR,\fIArgs\fR,\fIRes\fR)
  2724. .ip
  2725. Executes the Unix system call number \fIN\fR with
  2726. arguments \fIArgs\fR, and returns the result in \fIRes\fR.  
  2727. .(x b
  2728. (B)     syscall/3
  2729. .)x
  2730. \fIN\fR is an integer, and \fIArgs\fR a Prolog
  2731. list of the arguments to the system call.  For example, to execute the system call
  2732. ``\fIcreat\fR(\fIFile\fR,\fIMode\fR)'', knowing that the syscall number for
  2733. the Unix command \fIcreat\fR(2) is 8, we execute the goal
  2734. .(l
  2735. | ?- syscall(8, [\fIFile\fR, \fIMode\fR], \fIDes\fR).
  2736. .)l
  2737. where \fIDes\fR is the file descriptor returned by \fIcreat\fR.  The syscall numbers
  2738. for some Unix system calls are given in Table 2.
  2739. .(z
  2740. .TS
  2741. .if \n+(b.=1 .nr d. \n(.c-\n(c.-1
  2742. .de 35
  2743. .ps \n(.s
  2744. .vs \n(.vu
  2745. .in \n(.iu
  2746. .if \n(.u .fi
  2747. .if \n(.j .ad
  2748. .if \n(.j=0 .na
  2749. ..
  2750. .nf
  2751. .nr #~ 0
  2752. .if n .nr #~ 0.6n
  2753. .ds #d .d
  2754. .if \(ts\n(.z\(ts\(ts .ds #d nl
  2755. .fc
  2756. .nr 33 \n(.s
  2757. .rm 80 81 82 83
  2758. .nr 80 0
  2759. .nr 38 \wexit
  2760. .if \n(80<\n(38 .nr 80 \n(38
  2761. .nr 38 \wread
  2762. .if \n(80<\n(38 .nr 80 \n(38
  2763. .nr 38 \wopen
  2764. .if \n(80<\n(38 .nr 80 \n(38
  2765. .nr 38 \wcreat
  2766. .if \n(80<\n(38 .nr 80 \n(38
  2767. .nr 38 \wunlink
  2768. .if \n(80<\n(38 .nr 80 \n(38
  2769. .nr 38 \wchmod
  2770. .if \n(80<\n(38 .nr 80 \n(38
  2771. .nr 38 \waccess
  2772. .if \n(80<\n(38 .nr 80 \n(38
  2773. .nr 38 \wwait
  2774. .if \n(80<\n(38 .nr 80 \n(38
  2775. .nr 38 \wconnect
  2776. .if \n(80<\n(38 .nr 80 \n(38
  2777. .nr 38 \wsend
  2778. .if \n(80<\n(38 .nr 80 \n(38
  2779. .nr 38 \wbind
  2780. .if \n(80<\n(38 .nr 80 \n(38
  2781. .nr 38 \wlisten
  2782. .if \n(80<\n(38 .nr 80 \n(38
  2783. .nr 38 \wsendmsg
  2784. .if \n(80<\n(38 .nr 80 \n(38
  2785. .nr 38 \wrecvfrom
  2786. .if \n(80<\n(38 .nr 80 \n(38
  2787. .nr 38 \wsocketpair
  2788. .if \n(80<\n(38 .nr 80 \n(38
  2789. .nr 38 \wrmdir
  2790. .if \n(80<\n(38 .nr 80 \n(38
  2791. .80
  2792. .rm 80
  2793. .nr 81 0
  2794. .nr 31 0
  2795. .nr 32 0
  2796. .nr 38 \w1
  2797. .if \n(31<\n(38 .nr 31 \n(38
  2798. .nr 38 \w3
  2799. .if \n(31<\n(38 .nr 31 \n(38
  2800. .nr 38 \w5
  2801. .if \n(31<\n(38 .nr 31 \n(38
  2802. .nr 38 \w8
  2803. .if \n(31<\n(38 .nr 31 \n(38
  2804. .nr 38 \w10
  2805. .if \n(31<\n(38 .nr 31 \n(38
  2806. .nr 38 \w15
  2807. .if \n(31<\n(38 .nr 31 \n(38
  2808. .nr 38 \w33
  2809. .if \n(31<\n(38 .nr 31 \n(38
  2810. .nr 38 \w84
  2811. .if \n(31<\n(38 .nr 31 \n(38
  2812. .nr 38 \w98
  2813. .if \n(31<\n(38 .nr 31 \n(38
  2814. .nr 38 \w101
  2815. .if \n(31<\n(38 .nr 31 \n(38
  2816. .nr 38 \w104
  2817. .if \n(31<\n(38 .nr 31 \n(38
  2818. .nr 38 \w106
  2819. .if \n(31<\n(38 .nr 31 \n(38
  2820. .nr 38 \w114
  2821. .if \n(31<\n(38 .nr 31 \n(38
  2822. .nr 38 \w125
  2823. .if \n(31<\n(38 .nr 31 \n(38
  2824. .nr 38 \w135
  2825. .if \n(31<\n(38 .nr 31 \n(38
  2826. .nr 38 \w137
  2827. .if \n(31<\n(38 .nr 31 \n(38
  2828. .81
  2829. .rm 81
  2830. .nr 61 \n(31
  2831. .nr 38 \n(61+\n(32
  2832. .if \n(38>\n(81 .nr 81 \n(38
  2833. .if \n(38<\n(81 .nr 61 +(\n(81-\n(38)/2
  2834. .nr 82 0
  2835. .nr 38 \wfork
  2836. .if \n(82<\n(38 .nr 82 \n(38
  2837. .nr 38 \wwrite
  2838. .if \n(82<\n(38 .nr 82 \n(38
  2839. .nr 38 \wclose
  2840. .if \n(82<\n(38 .nr 82 \n(38
  2841. .nr 38 \wlink
  2842. .if \n(82<\n(38 .nr 82 \n(38
  2843. .nr 38 \wchdir
  2844. .if \n(82<\n(38 .nr 82 \n(38
  2845. .nr 38 \wlseek
  2846. .if \n(82<\n(38 .nr 82 \n(38
  2847. .nr 38 \wkill
  2848. .if \n(82<\n(38 .nr 82 \n(38
  2849. .nr 38 \wsocket
  2850. .if \n(82<\n(38 .nr 82 \n(38
  2851. .nr 38 \waccept
  2852. .if \n(82<\n(38 .nr 82 \n(38
  2853. .nr 38 \wrecv
  2854. .if \n(82<\n(38 .nr 82 \n(38
  2855. .nr 38 \wsetsockopt
  2856. .if \n(82<\n(38 .nr 82 \n(38
  2857. .nr 38 \wrecvmsg
  2858. .if \n(82<\n(38 .nr 82 \n(38
  2859. .nr 38 \wgetsockopt
  2860. .if \n(82<\n(38 .nr 82 \n(38
  2861. .nr 38 \wsendto
  2862. .if \n(82<\n(38 .nr 82 \n(38
  2863. .nr 38 \wmkdir
  2864. .if \n(82<\n(38 .nr 82 \n(38
  2865. .nr 38 \wgetsockname
  2866. .if \n(82<\n(38 .nr 82 \n(38
  2867. .82
  2868. .rm 82
  2869. .nr 83 0
  2870. .nr 31 0
  2871. .nr 32 0
  2872. .nr 38 \w2
  2873. .if \n(31<\n(38 .nr 31 \n(38
  2874. .nr 38 \w4
  2875. .if \n(31<\n(38 .nr 31 \n(38
  2876. .nr 38 \w6
  2877. .if \n(31<\n(38 .nr 31 \n(38
  2878. .nr 38 \w9
  2879. .if \n(31<\n(38 .nr 31 \n(38
  2880. .nr 38 \w12
  2881. .if \n(31<\n(38 .nr 31 \n(38
  2882. .nr 38 \w19
  2883. .if \n(31<\n(38 .nr 31 \n(38
  2884. .nr 38 \w37
  2885. .if \n(31<\n(38 .nr 31 \n(38
  2886. .nr 38 \w97
  2887. .if \n(31<\n(38 .nr 31 \n(38
  2888. .nr 38 \w99
  2889. .if \n(31<\n(38 .nr 31 \n(38
  2890. .nr 38 \w102
  2891. .if \n(31<\n(38 .nr 31 \n(38
  2892. .nr 38 \w105
  2893. .if \n(31<\n(38 .nr 31 \n(38
  2894. .nr 38 \w113
  2895. .if \n(31<\n(38 .nr 31 \n(38
  2896. .nr 38 \w118
  2897. .if \n(31<\n(38 .nr 31 \n(38
  2898. .nr 38 \w133
  2899. .if \n(31<\n(38 .nr 31 \n(38
  2900. .nr 38 \w136
  2901. .if \n(31<\n(38 .nr 31 \n(38
  2902. .nr 38 \w150
  2903. .if \n(31<\n(38 .nr 31 \n(38
  2904. .83
  2905. .rm 83
  2906. .nr 63 \n(31
  2907. .nr 38 \n(63+\n(32
  2908. .if \n(38>\n(83 .nr 83 \n(38
  2909. .if \n(38<\n(83 .nr 63 +(\n(83-\n(38)/2
  2910. .nr 38 0
  2911. .if \n(80>\n(38 .nr 38 \n(80
  2912. .if \n(82>\n(38 .nr 38 \n(82
  2913. .nr 80 \n(38
  2914. .nr 82 \n(38
  2915. .nr 38 0+\n(80+\n(81+\n(82+\n(83
  2916. .nr 38 \n(.l-\n(.i-\n(38
  2917. .nr 38 \n(38/11
  2918. .if \n(38<1n .nr 38 1n
  2919. .nr 79 0
  2920. .nr 40 \n(79+(1*\n(38)
  2921. .nr 80 +\n(40
  2922. .nr 41 \n(80+(3*\n(38)
  2923. .nr 81 +\n(41
  2924. .nr 61 +\n(41
  2925. .nr 42 \n(81+(3*\n(38)
  2926. .nr 82 +\n(42
  2927. .nr 43 \n(82+(3*\n(38)
  2928. .nr 83 +\n(43
  2929. .nr 63 +\n(43
  2930. .nr TW \n(83
  2931. .nr TW +1*\n(38
  2932. .if t .if (\n(TW+\n(.o)>7.65i .tm Table at line 1969 file sec2.t is too wide - \n(TW units
  2933. .ne 16v+0p
  2934. .nr #I \n(.i
  2935. .in +(\n(.lu-\n(TWu-\n(.iu)/2u
  2936. .fc  
  2937. .nr #T 0-1
  2938. .nr #a 0-1
  2939. .nr #a 0-1
  2940. .eo
  2941. .de T#
  2942. .ds #d .d
  2943. .if \(ts\n(.z\(ts\(ts .ds #d nl
  2944. .mk ##
  2945. .nr ## -1v
  2946. .ls 1
  2947. .if \n(#T>=0 .nr #a \n(#T
  2948. .if \n(T. .vs \n(.vu-\n(.sp
  2949. .if \n(T. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  2950. .if \n(T. .vs
  2951. .if \n(#a>=0 .sp -1
  2952. .if \n(#a>=0 \h'|0'\s\n(33\h'-\n(#~u'\L'|\n(#au-1v'\s0\v'\n(\*(#du-\n(#au+1v'\h'|\n(TWu'
  2953. .if \n(#a>=0 .sp -1
  2954. .if \n(#a>=0 \h'(|\n(42u+|\n(81u)/2u'\s\n(33\h'-\n(#~u'\L'|\n(#au-1v'\s0\v'\n(\*(#du-\n(#au+1v'\h'|\n(TWu'
  2955. .if \n(#a>=0 .sp -1
  2956. .if \n(#a>=0 \h'|\n(TWu'\s\n(33\h'-\n(#~u'\L'|\n(#au-1v'\s0\v'\n(\*(#du-\n(#au+1v'
  2957. .ls
  2958. ..
  2959. .ec
  2960. .nr 36 \n(.v
  2961. .vs \n(.vu-\n(.sp
  2962. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  2963. .vs \n(36u
  2964. .mk #a
  2965. .ta \n(80u \n(61u \n(82u \n(63u 
  2966. .nr 31 \n(.f
  2967. .nr 35 1m
  2968. \&\h'|\n(40u'exit\h'|\n(41u'1\h'|\n(42u'fork\h'|\n(43u'2
  2969. .ta \n(80u \n(61u \n(82u \n(63u 
  2970. .nr 31 \n(.f
  2971. .nr 35 1m
  2972. \&\h'|\n(40u'read\h'|\n(41u'3\h'|\n(42u'write\h'|\n(43u'4
  2973. .ta \n(80u \n(61u \n(82u \n(63u 
  2974. .nr 31 \n(.f
  2975. .nr 35 1m
  2976. \&\h'|\n(40u'open\h'|\n(41u'5\h'|\n(42u'close\h'|\n(43u'6
  2977. .ta \n(80u \n(61u \n(82u \n(63u 
  2978. .nr 31 \n(.f
  2979. .nr 35 1m
  2980. \&\h'|\n(40u'creat\h'|\n(41u'8\h'|\n(42u'link\h'|\n(43u'9
  2981. .ta \n(80u \n(61u \n(82u \n(63u 
  2982. .nr 31 \n(.f
  2983. .nr 35 1m
  2984. \&\h'|\n(40u'unlink\h'|\n(41u'10\h'|\n(42u'chdir\h'|\n(43u'12
  2985. .ta \n(80u \n(61u \n(82u \n(63u 
  2986. .nr 31 \n(.f
  2987. .nr 35 1m
  2988. \&\h'|\n(40u'chmod\h'|\n(41u'15\h'|\n(42u'lseek\h'|\n(43u'19
  2989. .ta \n(80u \n(61u \n(82u \n(63u 
  2990. .nr 31 \n(.f
  2991. .nr 35 1m
  2992. \&\h'|\n(40u'access\h'|\n(41u'33\h'|\n(42u'kill\h'|\n(43u'37
  2993. .ta \n(80u \n(61u \n(82u \n(63u 
  2994. .nr 31 \n(.f
  2995. .nr 35 1m
  2996. \&\h'|\n(40u'wait\h'|\n(41u'84\h'|\n(42u'socket\h'|\n(43u'97
  2997. .ta \n(80u \n(61u \n(82u \n(63u 
  2998. .nr 31 \n(.f
  2999. .nr 35 1m
  3000. \&\h'|\n(40u'connect\h'|\n(41u'98\h'|\n(42u'accept\h'|\n(43u'99
  3001. .ta \n(80u \n(61u \n(82u \n(63u 
  3002. .nr 31 \n(.f
  3003. .nr 35 1m
  3004. \&\h'|\n(40u'send\h'|\n(41u'101\h'|\n(42u'recv\h'|\n(43u'102
  3005. .ta \n(80u \n(61u \n(82u \n(63u 
  3006. .nr 31 \n(.f
  3007. .nr 35 1m
  3008. \&\h'|\n(40u'bind\h'|\n(41u'104\h'|\n(42u'setsockopt\h'|\n(43u'105
  3009. .ta \n(80u \n(61u \n(82u \n(63u 
  3010. .nr 31 \n(.f
  3011. .nr 35 1m
  3012. \&\h'|\n(40u'listen\h'|\n(41u'106\h'|\n(42u'recvmsg\h'|\n(43u'113
  3013. .ta \n(80u \n(61u \n(82u \n(63u 
  3014. .nr 31 \n(.f
  3015. .nr 35 1m
  3016. \&\h'|\n(40u'sendmsg\h'|\n(41u'114\h'|\n(42u'getsockopt\h'|\n(43u'118
  3017. .ta \n(80u \n(61u \n(82u \n(63u 
  3018. .nr 31 \n(.f
  3019. .nr 35 1m
  3020. \&\h'|\n(40u'recvfrom\h'|\n(41u'125\h'|\n(42u'sendto\h'|\n(43u'133
  3021. .ta \n(80u \n(61u \n(82u \n(63u 
  3022. .nr 31 \n(.f
  3023. .nr 35 1m
  3024. \&\h'|\n(40u'socketpair\h'|\n(41u'135\h'|\n(42u'mkdir\h'|\n(43u'136
  3025. .ta \n(80u \n(61u \n(82u \n(63u 
  3026. .nr 31 \n(.f
  3027. .nr 35 1m
  3028. \&\h'|\n(40u'rmdir\h'|\n(41u'137\h'|\n(42u'getsockname\h'|\n(43u'150
  3029. .fc
  3030. .nr T. 1
  3031. .T# 1
  3032. .in \n(#Iu
  3033. .35
  3034. .nr #a 0
  3035. .TE
  3036. .if \n-(b.=0 .nr c. \n(.c-\n(d.-19
  3037. .sp
  3038. .ce
  3039. Table 2: Some Syscall Numbers for Unix Systems Calls
  3040. .sp 2
  3041. .)z
  3042. .sh 2 "Global Values"
  3043. .pp
  3044. SB-Prolog has some primitives that permit the programmer to manipulate global
  3045. values.  These are provided primarily as an efficiency hack, and needless to say, should
  3046. be used with a great deal of care.
  3047. .lp
  3048. \fBglobalset\fR(\fITerm\fR)
  3049. .ip
  3050. Allows the user to save a global value.
  3051. .(x b
  3052. (L)     globalset/1
  3053. .)x
  3054. \fITerm\fR must be bound
  3055. to a compound term, say \fIp\fR(\fIV\fR\^). \fIV\fR must be a number or
  3056. a constant or a variable.
  3057. If \fIV\fR is a number or a constant, the effect of \fIglobalset\fR(\fIp\fR(\fIV\fR\^)) can be
  3058. described as:
  3059. .(l
  3060. retract(p(_\|)), assert(p(V)).
  3061. .)l
  3062. I.e., \fIp\fR is a predicate that when called will, from now on (until some other
  3063. change by \fIglobalset\fR/1), deterministically return \fIV\fR.
  3064. If \fIV\fR is a variable, the effect is to make \fIV\fR a global variable whose value is
  3065. accessible by calling \fIp\fR. For example, executing
  3066. ``\fIglobalset\fR(\fIp\fR(\fIX\fR\^))'' makes \fIX\fR a global
  3067. variable. \fIX\fR can be set by unification with some other term.
  3068. On backtracking, \fIX\fR will be restored to its earlier value. 
  3069. .lp
  3070. \fBgennum\fR(\fINewnum\fR)
  3071. .ip
  3072. gennum/1 sets its argument to a new integer every time it is invoked.
  3073. .(x b
  3074. (L)     gennum/1
  3075. .)x
  3076. .lp
  3077. \fBgensym\fR(\fIC\fR,\fINewsym\fR)
  3078. .ip
  3079. gensym/2 sets its second argument to an atom whose name is made by
  3080. concatenating the name of the atom \fIC\fR to the current gennum number. 
  3081. .(x b
  3082. (L)     gensym/2
  3083. .)x
  3084. This new constant is bound to \fINewsym\fR.  For example, if the current
  3085. \fIgennum\fR number is 37, then the call
  3086. .(l
  3087. | ?- gensym(aaa,X)
  3088. .)l
  3089. will succeed binding \fIX\fR to the atom `aaa37'.
  3090. .ds f. sec3.t
  3091. .sh 1 "Debugging"
  3092. .sh 2 "High Level Tracing"
  3093. .pp
  3094. The preferred method of tracing execution is through the predicate
  3095. \fItrace\fR/1.
  3096. .(x b
  3097. (L)     trace/1
  3098. .)x
  3099. This predicate takes as argument a term \fIP\fR/\fIN\fR, where
  3100. \fIP\fR is a predicate name and \fIN\fR its arity, and sets a ``trace
  3101. point'' on the corresponding predicate; it can also be given a
  3102. list of such terms, in which case a trace point is set on each member of the list.
  3103. For example, executing
  3104. .(l
  3105.     | ?- trace(pred1/2), trace([pred2/3, pred3/2]).
  3106. .)l
  3107. sets trace points on predicates \fIpred1\fR/2, \fIpred2\fR/3 and
  3108. \fIpred3\fR/2.
  3109. Only those predicates are traced that have trace points set on them.
  3110. .pp
  3111. If all the predicates in a file are to be traced, it is usually convenient to use the
  3112. \fIPredList\fR parameter of \fIcompile\fR/4 or \fIconsult\fR/3, e.g.:
  3113. .(l
  3114.     | ?- compile(foo, 'foo.out', [t,v], Preds), load('foo.out'), trace(Preds).
  3115. or
  3116.     | ?- consult(foo, [v], Preds), trace(Preds).
  3117. .)l
  3118. Notice that in the first case, the \fBt\fR compiler option (see Section 8.3)
  3119. should be specified in
  3120. order to turn off certain assembler optimizations and facilitate tracing.
  3121. In the second case, the same effect may be achieved by specifying the
  3122. \fBt\fR option to \fIconsult\fR.
  3123. .pp
  3124. The trace points set on predicates may be overwritten by loading byte code
  3125. files via \fIload\fR/1, and in this case it may be necessary to explicitly
  3126. set trace points again on the loaded predicates.  This does not happen with
  3127. \fIconsult\fR: predicates that were being traced continue to have trace points
  3128. set after consulting.
  3129. .pp
  3130. The tracing facilities of SB-Prolog are in many ways very similar to those of
  3131. C-Prolog.  However, leashing is not supported, and only those predicates
  3132. can be traced which have had trace points
  3133. set on them through \fItrace\fR/1.  This makes \fItrace\fR/1 and \fIspy\fR/1
  3134. very similar: essentially, trace amounts to two levels of spy points.
  3135. In SB-Prolog, tracing occurs at \fICall\fR (i.e. entry to a predicate),
  3136. successful \fIExit\fR from a clause, and \fIFailure\fR of the entire call.  The tracing options available during debugging are the following:
  3137. .ip \fBc\fR,\ \s-2NEWLINE\s+2:\ Creep
  3138. Causes the system to single-step to the next port (i.e. either the entry to a traced
  3139. predicate called by the executed clause, or the success or failure exit from that
  3140. clause).
  3141. .ip \fBa\fR:\ Abort
  3142. Causes execution to abort and control to return to the top level interpreter.
  3143. .ip \fBb\fR:\ Break
  3144. Calls the evaluable predicate \fIbreak\fR, thus invoking recursively
  3145. a new incarnation of the system interpreter.  The
  3146. command prompt at break level \fIn\fR is
  3147. .(l
  3148.     \fIn\fR: ?-
  3149. .)l
  3150. The user may return to the previous break level by entering the system end-of-file character (e.g. ctrl-D), or
  3151. typing in the atom \fIend_\^of_\^file\fR; or to the top level interpreter by typing in \fIabort\fR.
  3152. .ip \fBf\fR:\ Fail
  3153. Causes execution to fail, thus transferring control to the Fail port of the current execution.
  3154. .ip \fBh\fR:\ Help
  3155. Displays the table of debugging options.
  3156. .ip \fBl\fR:\ Leap
  3157. Causes the system to resume running the program, only stopping when a spy-point
  3158. is reached or the program terminates.  This allows the user to follow the execution at a higher level than
  3159. exhaustive tracing.
  3160. .ip \fBn\fR:\ Nodebug
  3161. Turns off debug mode.
  3162. .ip \fBq\fR:\ Quasi-skip
  3163. This is like Skip except that it does not mask out spy points.
  3164. .ip \fBr\fR:\ Retry\ (fail)
  3165. Transfers to the Call port of the current goal.  Note, however, that side effects, such as
  3166. database modifications etc., are not undone.
  3167. .ip \fBs\fR:\ Skip
  3168. Causes tracing to be turned off for the entire execution of the procedure.  Thus,
  3169. nothing is seen until control comes back to that procedure, either at the Success or the Failure port.
  3170. .sp
  3171. .lp
  3172. Other predicates that are useful in debugging are:
  3173. .sp
  3174. .ip \fBuntrace\fR(\fIPreds\fR)
  3175. .(x b
  3176. (L)     untrace/1
  3177. .)x
  3178. where Preds is a term \fIP\fR/\fIN\fR, where \fIP\fR is a predicate name and \fIN\fR its arity,
  3179. or a list of such terms.  Turns off tracing on the specified predicates.
  3180. \fIPreds\fR must be instantiated at the time of the call.
  3181. .ip \fBspy\fR(\fIPreds\fR)
  3182. .(x b
  3183. (L)     spy/1
  3184. .)x
  3185. where Preds is a term \fIP\fR/\fIN\fR, where \fIP\fR is a predicate name and \fIN\fR its arity,
  3186. or a list of such terms.  Sets spy points on the specified predicates.
  3187. \fIPreds\fR must be instantiated at the time of the call.
  3188. .ip \fBnospy\fR(\fBPreds\fR)
  3189. .(x b
  3190. (L)     nospy/1
  3191. .)x
  3192. where Preds is a term \fIP\fR/\fIN\fR, where \fIP\fR is a predicate name and \fIN\fR its arity,
  3193. or a list of such terms.  Removes spy points on the specified predicates.
  3194. \fIPreds\fR must be instantiated at the time of the call.
  3195. .ip \fBdebug\fR
  3196. Turns on debugging mode.  This causes subsequent execution of predicates with trace or spy
  3197. points to be traced, and is a no-op if there are no such predicates.
  3198. .(x b
  3199. (L)     debug/0
  3200. .)x
  3201. The predicates \fItrace\fR/1 and \fIspy\fR/1 cause debugging mode to be turned on automatically.
  3202. .ip \fBnodebug\fR
  3203. Turns off debugging mode.  This causes trace and spy points to be ignored.
  3204. .(x b
  3205. (L)     nodebug/0
  3206. .)x
  3207. .ip \fBdebugging\fR
  3208. Displays information about whether debug mode is on or not, and lists
  3209. predicates that have trace points or spy points set on them.
  3210. .(x b
  3211. (L)     debugging/0
  3212. .)x
  3213. .ip \fBtracepreds\fR(\fIL\fR\|)
  3214. Binds \fIL\fR to a list of terms \fIP\fR/\fIN\fR where the predicate
  3215. \fIP\fR of arity \fIN\fR has a trace point set on it.
  3216. .(x b
  3217. (L)     tracepreds/1
  3218. .)x
  3219. .ip \fBspypreds\fR(\fIL\fR\|)
  3220. Binds \fIL\fR to a list of terms \fIP\fR/\fIN\fR where the predicate
  3221. \fIP\fR of arity \fIN\fR has a spy point set on it.
  3222. .(x b
  3223. (L)     spypreds/1
  3224. .)x
  3225. .sp
  3226. .pp
  3227. There is one known bug in the package: attempts to set trace points, via
  3228. \fItrace\fR/1, on system and library predicates that are used by the trace
  3229. package can cause bizarre behaviour.
  3230. .sh 2 "Low Level Tracing"
  3231. .pp
  3232. SB-Prolog also provides a facility for low level tracing of execution.  This can
  3233. be activated by invoking the simulator with the \-T option, or through the
  3234. predicate \fItrace\fR/0.  It causes trace information to be printed out at every
  3235. call (including those to system trap handlers).  The volume of such trace information can very become large very
  3236. quickly, so this method of tracing is not recommended in general.
  3237. .(x b
  3238. (L)     trace/0
  3239. .)x
  3240. .pp
  3241. Low level tracing may be turned off using the predicate \fIuntrace\fR/0.
  3242. .(x b
  3243. (L)     untrace/0
  3244. .)x
  3245. .ds f. sec4.t
  3246. .sh 1 "The Simulator"
  3247. .pp
  3248. The simulator resides in the SB-Prolog system directory \fIsim\fR.  The following
  3249. sections describe various aspects of the simulator.
  3250. .sh 2 "Invoking the Simulator"
  3251. .pp
  3252. The simulator is invoked by the command
  3253.  
  3254. .(l
  3255.     sbprolog \fIbc_\^file\fR
  3256. .)l
  3257.  
  3258. where \fIbc_\^file\fR is a byte code file resulting from the compilation of a
  3259. Prolog program.  In almost all cases, the user will wish to interact with the
  3260. SB-Prolog \fIquery evaluator\fR, in which case \fIbc_\|file\fR will be
  3261. \fI$readloop\fR, and the command will be
  3262.  
  3263. .(l
  3264. .nr 99 \n(.s
  3265. .nr 98 \n(.f
  3266. .rm 11 
  3267. .as 11 "    sbprolog 
  3268. .ps 11
  3269. .ft 2
  3270. .ds 12 "\s-5\b'\(sl\e'\s0
  3271. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  3272. .as 11 \*(12
  3273. .ps \n(99
  3274. .ft \n(98
  3275. .as 11 " path 
  3276. .ps 11
  3277. .ft 2
  3278. .ds 12 "\s-5\b'\e\(sl'\s0
  3279. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  3280. .as 11 \*(12
  3281. .ps \n(99
  3282. .ft \n(98
  3283. .as 11 "/\\\\$\$\readloop
  3284. .ps \n(99
  3285. .ft \n(98
  3286. \*(11
  3287. .)l
  3288.  
  3289. .nr 99 \n(.s
  3290. .nr 98 \n(.f
  3291. .rm 11 
  3292. .as 11 "where 
  3293. .ps 11
  3294. .ft 2
  3295. .ds 12 "\s-5\b'\(sl\e'\s0
  3296. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  3297. .as 11 \*(12
  3298. .ps \n(99
  3299. .ft \n(98
  3300. .as 11 "\fIpath\fR
  3301. .ps 11
  3302. .ft 2
  3303. .ds 12 "\s-5\b'\e\(sl'\s0
  3304. .ds 12 \x'0'\f2\s11\*(12\s\n(99\f\n(98
  3305. .as 11 \*(12
  3306. .ps \n(99
  3307. .ft \n(98
  3308. .as 11 " is the path to the directory containing the
  3309. .ps \n(99
  3310. .ft \n(98
  3311. \*(11
  3312. command interpreter \fI$readloop\fR.  This directory, typically, is the
  3313. system directory \fImodlib\fR.
  3314. .pp
  3315. The command interpreter reads in a query typed in by the user, evaluates it and
  3316. prints the answer(s), repeating this until it encounters an end-of-file
  3317. (the standard end-of-file character on the system, e.g. ctrl-D), or the user types
  3318. in \fIend_\|of_\|file\fR or \fIhalt\fR.
  3319. .pp
  3320. The user should ensure that the the directory containing the executable file \fIsim\fR
  3321. (typically, the system directory \fIsim\fR) is included in the shell variable
  3322. \fIpath\fR; if not, the full path to the simulator will have to be specified.
  3323. .pp
  3324. In general, the simulator may be invoked with a variety of options, as
  3325. follows:
  3326.  
  3327. .(l
  3328.     sbprolog \-\fIoptions\fR \fIbc_\^file\fR
  3329. or
  3330.     sbprolog \-\fIoption\fR\*<1\*> \-\fIoption\fR\*<2\*> ... \-\fIoption\*<n\*>\fR \fIbc_\^file\fR
  3331. .)l
  3332.  
  3333. The options recognized by the simulator are described below.
  3334. .pp
  3335. When called with a byte code file \fIbc_\^file\fR, the simulator begins
  3336. execution with the first clause in that file.  The first clause in such a
  3337. file, therefore, should be a clause without any arguments in the head
  3338. (otherwise, the simulator will attempt to dereference argument pointers
  3339. in the head
  3340. that are really pointing into deep space, and usually come to a sad end).
  3341. If the user is executing a file in this manner rather than using the
  3342. command interpreter, he should also be careful to include the undefined
  3343. predicate handler `_\|\fI$undefined_\|pred\fR'/1, which is normally defined
  3344. in the file \fImodlib/$init_\|sys.P\fR.
  3345. .sh 2 "Simulator Options"
  3346. .lp
  3347. The following is a list of options recognized by the simulator.
  3348. .ip \fBT\fR
  3349. Generates a trace at entry to each called routine.
  3350. .ip \fBd\fR
  3351. Produces a disassembled dump of \fIbc_\|file\fR into a file named
  3352. `dump.pil' and exits.
  3353. .ip \fBn\fR
  3354. Adds machine addresses when producing trace and dump.
  3355. .ip \fBs\fR
  3356. Maintains information for the builtin \fIstatistics\fR/0.  Default: off.
  3357. .ip \fBm\fR\ \fIsize\fR
  3358. Allocates \fIsize\fR words (4 bytes) of space to the local stack and heap
  3359. together.  Default: 100000.
  3360. .ip \fBp\fR\ \fIsize\fR
  3361. Allocates \fIsize\fR words of space to the program area.  Default: 100000.
  3362. .ip \fBb\fR\ \fIsize\fR
  3363. Allocates \fIsize\fR words of space to the trail stack.  Default: \fIm\fR/5,
  3364. where \fIm\fR is the amount of space allocated to the local stack and heap
  3365. together.  This parameter, if specified, must follow the -m parameter.
  3366. .sp
  3367. .lp
  3368. As an example, the command
  3369. .(l
  3370.     sbprolog -s -p 60000 -m 150000 \e$readloop
  3371. .)l
  3372. starts the simulator executing the command interpreter with 60000 bytes of
  3373. program space, 150000 bytes of local and global stack space and (by default)
  3374. 30000 bytes of trail stack space; the \fIs\fR option also results in
  3375. statistics information being maintained.
  3376. .sh 2 "Interrupt Handling"
  3377. .pp
  3378. SB-Prolog provides a facility for exception
  3379. handling using user-definable interrupt handlers.  This can be used both
  3380. for external interrupts, e.g. those generated from the keyboard by the user
  3381. or from signals other processes; or internal traps, e.g. those caused by stack overflows, encountering undefined predicates, etc.
  3382. For example, the ``undefined predicate'' interrupt is handled, by default, by the
  3383. predicate `\fI_\|$undefined_\|pred\fR'/1, which is defined in the file
  3384. \fImodlib/$init_\|sys.P\fR.  The default action on encountering an undefined
  3385. predicate is to attempt to dynamically load a file whose name matches that of the
  3386. undefined predicate.  However, the user may easily alter this behaviour by
  3387. redefining the undefined predicate handler.
  3388. .ds f. sec5.t
  3389. .sh 1 "The Compiler"
  3390. .lp
  3391. The compiler translates Prolog source files into byte-code object files.  It is written
  3392. entirely in Prolog.  The byte code for the compiler can be found in the
  3393. SB-Prolog system directory \fIcmplib\fR, with the source code resident in
  3394. \fIcmplib/src\fR.  
  3395. .pp
  3396. Byte code files may be concatenated together to produce other byte code files.  Thus,
  3397. for example, if \fIfoo1\fR and \fIfoo2\fR are byte code files resulting
  3398. from the compilation of two Prolog source programs, then the file \fIfoo\fR,
  3399. obtained by executing the shell command
  3400. .(l
  3401.     cat foo1 foo2 > foo
  3402. .)l
  3403. is a byte code file as well, and may be loaded and executed.  In this case,
  3404. loading and executing the file \fIfoo\fR would give the same result as
  3405. loading \fIfoo1\fR and \fIfoo2\fR separately, which in turn would be the same as
  3406. concatenating the original source files and compiling this larger file.  This
  3407. makes it easier to compile large programs: one need only break them into smaller
  3408. pieces, compile the individual pieces, and concatenate the byte files together.
  3409. .pp
  3410. The following sections describe the various aspects of
  3411. the compiler in more detail.
  3412. .sh 2 "Structure of the Compiler"
  3413. .lp
  3414. The compilation of a Prolog program proceeds in four phases.  In the first
  3415. phase, the source program is read in and  passed through a preprocessor.
  3416. The output of the preprocessor is essentially another Prolog source
  3417. program.  Next, this program is transformed into an internal representation
  3418. (essentially an annotated abstract syntax tree).  The third phase
  3419. involves generating WAM ``assembly'' code and peephole optimization.
  3420. Finally, the WAM assembly code is processed by an assembler which generates
  3421. byte code.
  3422. .sh 2 "Invoking the Compiler"
  3423. .lp
  3424. The compiler is invoked through the Prolog predicate \fIcompile\fR:
  3425. .(l
  3426.     | ?- compile(\fIInFile\fR [, \fIOutFile\fR ] [, \fIOptionsList\fR ]).
  3427. .)l
  3428. where optional parameters are enclosed in brackets.
  3429. \fIInFile\fR is the name of the input (i.e. source) file; \fIOutFile\fR is the
  3430. name of the output file (i.e. byte code) file; \fIOptionsList\fR is a list of compiler options (see
  3431. below).
  3432. .pp
  3433. The input and output file names must be Prolog atoms, i.e. either
  3434. begin with a lower case letter or dollar sign `$', and consist only of letters, digits,
  3435. and underscores; or, be enclosed within single quotes.
  3436. If the output file name is not specified, it defaults to
  3437. \fIInFile\fB.out\fR.  The list of options, if specified, is
  3438. a Prolog list, i.e. a term of the form
  3439. .(l
  3440.     [ \fIoption\fR\*<1\*>, \fIoption\fR\*<2\*>, ..., \fIoption\*<n\*>\fR ].
  3441. .)l
  3442. If left unspecified, it defaults to the empty list [\^].
  3443. .pp
  3444. In fact, the output file name and the options list may be specified in any order.  Thus,
  3445. for example, the queries
  3446. .(l
  3447.     | ?- compile('/usr/debray/foo', foo_\|out, [v]).
  3448. and
  3449.     | ?- compile('/usr/debray/foo', [v], foo_\|out).
  3450. .)l
  3451. are equivalent, and specify that the Prolog source file
  3452. `\fI/usr/debray/foo\fR' is to be compiled in verbose mode (see ``Compiler
  3453. Options'' below), and that the byte code is to be generated into the file
  3454. \fIfoo_out\fR.
  3455. .pp
  3456. The \fIcompile\fR predicate may also be called with a fourth parameter:
  3457. .(l
  3458.     | ?- compile(\fIInFile\fR, \fIOutFile\fR, \fIOptionsList\fR, \fIPredList\fR).
  3459. .)l
  3460. where \fIInFile\fR, \fIOutFile\fR and \fIOptionsList\fR are as before;
  3461. \fIcompile\fR/4 unifies \fIPredList\fR with a list of terms \fIP\fR/\fIN\fR denoting the
  3462. predicates defined in \fIInFile\fR, where \fIP\fR is a predicate name and \fIN\fR its arity.  
  3463. \fIPredList\fR, if specified, is usually given as an uninstantiated
  3464. variable; its principal use is for setting trace points on the predicates in the file (see Section 6),
  3465. e.g. by executing
  3466. .(l
  3467.     | ?- compile('/usr/debray/foo', foo_\|out, [v], L), load(foo_\|out), trace(L).
  3468. .)l
  3469. Notice that \fIPredList\fR can only appear in \fIcompile\fR/4.
  3470. .sh 2 "Compiler Options"
  3471. .lp
  3472. The following options are currently recognized by the compiler:
  3473. .ip \fBa\fR
  3474. Specifies that an ``assembler'' file is to be created.  The name of the
  3475. assembler file is obtained by appending ``.asl'' to the source file name.
  3476. While the writing out of assembly code slows down the compilation process
  3477. to some extent, it allows the assembler to do a better job of optimizing
  3478. away indirect subroutine linkages (since in this case the assembler has
  3479. assembly code for the entire program to work with at once, not just a single
  3480. predicate).  This results in code that is faster and more compact.
  3481. .ip \fBd\fR
  3482. Dumps expanded macros to the user (see Section 10).
  3483. .ip \fBe\fR
  3484. Expand macros (see Section 10).
  3485. .ip \fBi\fR
  3486. Specifies that indexes are to be generated.  The pragma file (see Section 12) corresponding to
  3487. the source file is consulted for information regarding predicates which should have
  3488. indexes constructed, and the arguments on which indexes are to be
  3489. constructed.
  3490. .ip \fBt\fR
  3491. If specified, turns off assembler optimizations that eliminate indirect branches through the symbol table in
  3492. favour of direct branches.  This is useful in debugging compiled code.  It
  3493. is \fInecessary\fR if the extension table feature is to be used.
  3494. .ip \fBv\fR
  3495. If specified, compiles in ``verbose'' mode, which causes messages regarding progress
  3496. of compilation to be printed out.
  3497. .sh 2 "A Note on Coding for Efficiency"
  3498. .pp
  3499. The SB-Prolog system tends to favour programs that are relatively pure.  Thus, for
  3500. example, \fIassert\fRs tend to be quite expensive, encouraging the user to avoid them
  3501. if possible.  This section points out some syntactic constructs that lead to the generation
  3502. of efficient code.  These involve (\fIi\fR\^) avoiding the creation of backtrack points; and (\fIii\fR\^)
  3503. minimizing data movement between registers.
  3504. Optimization of logic programs is an area of ongoing research,
  3505. and we expect to enhance the capabilities of the system further in future
  3506. versions.
  3507. .sh 3 "Avoiding Creation of Backtrack Points"
  3508. .pp
  3509. Since the creation of backtrack points is relatively expensive, program efficiency may
  3510. be improved substantially by using constructs that avoid the creation of backtrack points where possible.
  3511. The SB-Prolog compiler recognizes conditionals involving certain
  3512. complementary inline tests, and generates code that does not create
  3513. choice points for such cases.
  3514. .nr 99 \n(.s
  3515. .nr 98 \n(.f
  3516. .rm 11 
  3517. .as 11 "Two inline tests \fIp\fR(
  3518. .ps 11
  3519. .ft 2
  3520. .ds 12 "X
  3521. .nr 12 \w'\s11\*(12'
  3522. .nr 10 0u
  3523. .if \n(ct>1 .nr 10 \n(10+\s11.25m\s0
  3524. .nr 14 \s11.1m\s0
  3525. .if \n(ct>1 .nr 14 \s11.15m\s0
  3526. .ds 13 \s11\v'.18m'\h'.05m'\l'\n(12u-.1m\(rn'\h'.05m'\v'-.18m'\s0
  3527. .nr 13 \w'\s11\*(13'
  3528. .as 12 \h'-\n(12u-\n(13u/2u+\n(14u'\v'0-\n(10u'\*(13\v'\n(10u'\h'-\n(13u+\n(12u/2u-\n(14u'
  3529. .ds 12 \x'0'\f2\s11\*(12\|\s\n(99\f\n(98
  3530. .as 11 \*(12
  3531. .ps \n(99
  3532. .ft \n(98
  3533. .as 11 ") and \fIq\fR(
  3534. .ps 11
  3535. .ft 2
  3536. .ds 12 "X
  3537. .nr 12 \w'\s11\*(12'
  3538. .nr 10 0u
  3539. .if \n(ct>1 .nr 10 \n(10+\s11.25m\s0
  3540. .nr 14 \s11.1m\s0
  3541. .if \n(ct>1 .nr 14 \s11.15m\s0
  3542. .ds 13 \s11\v'.18m'\h'.05m'\l'\n(12u-.1m\(rn'\h'.05m'\v'-.18m'\s0
  3543. .nr 13 \w'\s11\*(13'
  3544. .as 12 \h'-\n(12u-\n(13u/2u+\n(14u'\v'0-\n(10u'\*(13\v'\n(10u'\h'-\n(13u+\n(12u/2u-\n(14u'
  3545. .ds 12 \x'0'\f2\s11\*(12\|\s\n(99\f\n(98
  3546. .as 11 \*(12
  3547. .ps \n(99
  3548. .ft \n(98
  3549. .as 11 ") are complementary
  3550. .ps \n(99
  3551. .ft \n(98
  3552. \*(11
  3553. .nr 99 \n(.s
  3554. .nr 98 \n(.f
  3555. .rm 11 
  3556. .as 11 "if and only if \fIp\fR(
  3557. .ps 11
  3558. .ft 2
  3559. .ds 12 "X
  3560. .nr 12 \w'\s11\*(12'
  3561. .nr 10 0u
  3562. .if \n(ct>1 .nr 10 \n(10+\s11.25m\s0
  3563. .nr 14 \s11.1m\s0
  3564. .if \n(ct>1 .nr 14 \s11.15m\s0
  3565. .ds 13 \s11\v'.18m'\h'.05m'\l'\n(12u-.1m\(rn'\h'.05m'\v'-.18m'\s0
  3566. .nr 13 \w'\s11\*(13'
  3567. .as 12 \h'-\n(12u-\n(13u/2u+\n(14u'\v'0-\n(10u'\*(13\v'\n(10u'\h'-\n(13u+\n(12u/2u-\n(14u'
  3568. .ds 12 \x'0'\f2\s11\*(12\|\s\n(99\f\n(98
  3569. .as 11 \*(12
  3570. .ps \n(99
  3571. .ft \n(98
  3572. .as 11 ") \(== \fInot\fR(\fIq\fR(
  3573. .ps 11
  3574. .ft 2
  3575. .ds 12 "X
  3576. .nr 12 \w'\s11\*(12'
  3577. .nr 10 0u
  3578. .if \n(ct>1 .nr 10 \n(10+\s11.25m\s0
  3579. .nr 14 \s11.1m\s0
  3580. .if \n(ct>1 .nr 14 \s11.15m\s0
  3581. .ds 13 \s11\v'.18m'\h'.05m'\l'\n(12u-.1m\(rn'\h'.05m'\v'-.18m'\s0
  3582. .nr 13 \w'\s11\*(13'
  3583. .as 12 \h'-\n(12u-\n(13u/2u+\n(14u'\v'0-\n(10u'\*(13\v'\n(10u'\h'-\n(13u+\n(12u/2u-\n(14u'
  3584. .ds 12 \x'0'\f2\s11\*(12\|\s\n(99\f\n(98
  3585. .as 11 \*(12
  3586. .ps \n(99
  3587. .ft \n(98
  3588. .as 11 ")).
  3589. .ps \n(99
  3590. .ft \n(98
  3591. \*(11
  3592. For example, the literals `\fIX\fR > \fIY\fR\^' and `\fIX\fR =< \fIY\fR\^'
  3593. are complementary.  At this point, complementary tests are recognized as such only
  3594. if their argument tuples are identical.  The inline predicates that are
  3595. treated in this manner, with their corresponding complementary literals,
  3596. are shown in Table 3.
  3597. .(z
  3598. .sp
  3599. .TS
  3600. .if \n+(b.=1 .nr d. \n(.c-\n(c.-1
  3601. .de 35
  3602. .ps \n(.s
  3603. .vs \n(.vu
  3604. .in \n(.iu
  3605. .if \n(.u .fi
  3606. .if \n(.j .ad
  3607. .if \n(.j=0 .na
  3608. ..
  3609. .nf
  3610. .nr #~ 0
  3611. .if n .nr #~ 0.6n
  3612. .ds #d .d
  3613. .if \(ts\n(.z\(ts\(ts .ds #d nl
  3614. .fc
  3615. .nr 33 \n(.s
  3616. .rm 80 81
  3617. .nr 80 0
  3618. .nr 38 \w\fIInline Test
  3619. .if \n(80<\n(38 .nr 80 \n(38
  3620. .nr 38 \w>/2
  3621. .if \n(80<\n(38 .nr 80 \n(38
  3622. .nr 38 \w=</2
  3623. .if \n(80<\n(38 .nr 80 \n(38
  3624. .nr 38 \w>=/2
  3625. .if \n(80<\n(38 .nr 80 \n(38
  3626. .nr 38 \w</2
  3627. .if \n(80<\n(38 .nr 80 \n(38
  3628. .nr 38 \w=:=/2
  3629. .if \n(80<\n(38 .nr 80 \n(38
  3630. .nr 38 \w=\e=/2
  3631. .if \n(80<\n(38 .nr 80 \n(38
  3632. .nr 38 \wvar/1
  3633. .if \n(80<\n(38 .nr 80 \n(38
  3634. .nr 38 \wnonvar/1
  3635. .if \n(80<\n(38 .nr 80 \n(38
  3636. .80
  3637. .rm 80
  3638. .nr 81 0
  3639. .nr 38 \wComplementary Test\fR
  3640. .if \n(81<\n(38 .nr 81 \n(38
  3641. .nr 38 \w=</2
  3642. .if \n(81<\n(38 .nr 81 \n(38
  3643. .nr 38 \w>/2
  3644. .if \n(81<\n(38 .nr 81 \n(38
  3645. .nr 38 \w</2
  3646. .if \n(81<\n(38 .nr 81 \n(38
  3647. .nr 38 \w>=/2
  3648. .if \n(81<\n(38 .nr 81 \n(38
  3649. .nr 38 \w=\e=/2
  3650. .if \n(81<\n(38 .nr 81 \n(38
  3651. .nr 38 \w=:=/2
  3652. .if \n(81<\n(38 .nr 81 \n(38
  3653. .nr 38 \wnonvar/1
  3654. .if \n(81<\n(38 .nr 81 \n(38
  3655. .nr 38 \wvar/1
  3656. .if \n(81<\n(38 .nr 81 \n(38
  3657. .81
  3658. .rm 81
  3659. .nr 38 0
  3660. .if \n(80>\n(38 .nr 38 \n(80
  3661. .if \n(81>\n(38 .nr 38 \n(81
  3662. .nr 80 \n(38
  3663. .nr 81 \n(38
  3664. .nr 38 1n
  3665. .nr 79 0
  3666. .nr 40 \n(79+(1*\n(38)
  3667. .nr 80 +\n(40
  3668. .nr 41 \n(80+(3*\n(38)
  3669. .nr 81 +\n(41
  3670. .nr TW \n(81
  3671. .nr TW +1*\n(38
  3672. .if t .if (\n(TW+\n(.o)>7.65i .tm Table at line 147 file sec5.t is too wide - \n(TW units
  3673. .ne 9v+0p
  3674. .nr #I \n(.i
  3675. .in +(\n(.lu-\n(TWu-\n(.iu)/2u
  3676. .fc  
  3677. .nr #T 0-1
  3678. .nr #a 0-1
  3679. .nr #a 0-1
  3680. .eo
  3681. .de T#
  3682. .ds #d .d
  3683. .if \(ts\n(.z\(ts\(ts .ds #d nl
  3684. .mk ##
  3685. .nr ## -1v
  3686. .ls 1
  3687. .if \n(#T>=0 .nr #a \n(#T
  3688. .if \n(T. .vs \n(.vu-\n(.sp
  3689. .if \n(T. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3690. .if \n(T. .vs
  3691. .if \n(#a>=0 .sp -1
  3692. .if \n(#a>=0 \h'|0'\s\n(33\h'-\n(#~u'\L'|\n(#au-1v'\s0\v'\n(\*(#du-\n(#au+1v'\h'|\n(TWu'
  3693. .if \n(#a>=0 .sp -1
  3694. .if \n(#a>=0 \h'(|\n(41u+|\n(80u)/2u'\s\n(33\h'-\n(#~u'\L'|\n(#au-1v'\s0\v'\n(\*(#du-\n(#au+1v'\h'|\n(TWu'
  3695. .if \n(#a>=0 .sp -1
  3696. .if \n(#a>=0 \h'|\n(TWu'\s\n(33\h'-\n(#~u'\L'|\n(#au-1v'\s0\v'\n(\*(#du-\n(#au+1v'
  3697. .ls
  3698. ..
  3699. .ec
  3700. .nr 36 \n(.v
  3701. .vs \n(.vu-\n(.sp
  3702. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3703. .vs \n(36u
  3704. .mk #a
  3705. .ta \n(80u \n(81u 
  3706. .nr 31 \n(.f
  3707. .nr 35 1m
  3708. \&\h'|\n(40u'\fIInline Test\h'|\n(41u'Complementary Test\fR
  3709. .nr 36 \n(.v
  3710. .vs \n(.vu-\n(.sp
  3711. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3712. .vs \n(36u
  3713. .ta \n(80u \n(81u 
  3714. .nr 31 \n(.f
  3715. .nr 35 1m
  3716. \&\h'|\n(40u'>/2\h'|\n(41u'=</2
  3717. .nr 36 \n(.v
  3718. .vs \n(.vu-\n(.sp
  3719. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3720. .vs \n(36u
  3721. .ta \n(80u \n(81u 
  3722. .nr 31 \n(.f
  3723. .nr 35 1m
  3724. \&\h'|\n(40u'=</2\h'|\n(41u'>/2
  3725. .nr 36 \n(.v
  3726. .vs \n(.vu-\n(.sp
  3727. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3728. .vs \n(36u
  3729. .ta \n(80u \n(81u 
  3730. .nr 31 \n(.f
  3731. .nr 35 1m
  3732. \&\h'|\n(40u'>=/2\h'|\n(41u'</2
  3733. .nr 36 \n(.v
  3734. .vs \n(.vu-\n(.sp
  3735. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3736. .vs \n(36u
  3737. .ta \n(80u \n(81u 
  3738. .nr 31 \n(.f
  3739. .nr 35 1m
  3740. \&\h'|\n(40u'</2\h'|\n(41u'>=/2
  3741. .nr 36 \n(.v
  3742. .vs \n(.vu-\n(.sp
  3743. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3744. .vs \n(36u
  3745. .ta \n(80u \n(81u 
  3746. .nr 31 \n(.f
  3747. .nr 35 1m
  3748. \&\h'|\n(40u'=:=/2\h'|\n(41u'=\e=/2
  3749. .nr 36 \n(.v
  3750. .vs \n(.vu-\n(.sp
  3751. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3752. .vs \n(36u
  3753. .ta \n(80u \n(81u 
  3754. .nr 31 \n(.f
  3755. .nr 35 1m
  3756. \&\h'|\n(40u'=\e=/2\h'|\n(41u'=:=/2
  3757. .nr 36 \n(.v
  3758. .vs \n(.vu-\n(.sp
  3759. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3760. .vs \n(36u
  3761. .ta \n(80u \n(81u 
  3762. .nr 31 \n(.f
  3763. .nr 35 1m
  3764. \&\h'|\n(40u'var/1\h'|\n(41u'nonvar/1
  3765. .nr 36 \n(.v
  3766. .vs \n(.vu-\n(.sp
  3767. \h'|0'\s\n(33\l'|\n(TWu\(ul'\s0
  3768. .vs \n(36u
  3769. .ta \n(80u \n(81u 
  3770. .nr 31 \n(.f
  3771. .nr 35 1m
  3772. \&\h'|\n(40u'nonvar/1\h'|\n(41u'var/1
  3773. .fc
  3774. .nr T. 1
  3775. .T# 1
  3776. .in \n(#Iu
  3777. .35
  3778. .nr #a 0
  3779. .TE
  3780. .if \n-(b.=0 .nr c. \n(.c-\n(d.-13
  3781. .sp
  3782. .ce
  3783. Table 3: Complementary tests recognized by the compiler
  3784. .sp
  3785. .)z
  3786. The syntactic constructs recognized are:
  3787. .ip (\fIi\fR\^)
  3788. Disjuncts of the form
  3789. .(l
  3790. .nr 99 \n(.s
  3791. .nr 98 \n(.f
  3792. .rm 11 
  3793. .as 11 "\fIhead\fR :\- (( \fItest\fR(
  3794. .ps 11
  3795. .ft 2
  3796. .ds 12 "X
  3797. .nr 12 \w'\s11\*(12'
  3798. .nr 10 0u
  3799. .if \n(ct>1 .nr 10 \n(10+\s11.25m\s0
  3800. .nr 14 \s11.1m\s0
  3801. .if \n(ct>1 .nr 14 \s11.15m\s0
  3802. .ds 13 \s11\v'.18m'\h'.05m'\l'\n(12u-.1m\(rn'\h'.05m'\v'-.18m'\s0
  3803. .nr 13 \w'\s11\*(13'
  3804. .as 12 \h'-\n(12u-\n(13u/2u+\n(14u'\v'0-\n(10u'\*(13\v'\n(10u'\h'-\n(13u+\n(12u/2u-\n(14u'
  3805. .ds 12 \x'0'\f2\s11\*(12\|\s\n(99\f\n(98
  3806. .as 11 \*(12
  3807. .ps \n(99
  3808. .ft \n(98
  3809. .as 11 "), ... ) ; ( not(\fItest\fR(
  3810. .ps 11
  3811. .ft 2
  3812. .ds 12 "X
  3813. .nr 12 \w'\s11\*(12'
  3814. .nr 10 0u
  3815. .if \n(ct>1 .nr 10 \n(10+\s11.25m\s0
  3816. .nr 14 \s11.1m\s0
  3817. .if \n(ct>1 .nr 14 \s11.15m\s0
  3818. .ds 13 \s11\v'.18m'\h'.05m'\l'\n(12u-.1m\(rn'\h'.05m'\v'-.18m'\s0
  3819. .nr 13 \w'\s11\*(13'
  3820. .as 12 \h'-\n(12u-\n(13u/2u+\n(14u'\v'0-\n(10u'\*(13\v'\n(10u'\h'-\n(13u+\n(12u/2u-\n(14u'
  3821. .ds 12 \x'0'\f2\s11\*(12\|\s\n(99\f\n(98
  3822. .as 11 \*(12
  3823. .ps \n(99
  3824. .ft \n(98
  3825. .as 11 ")), ...)).
  3826. .ps \n(99
  3827. .ft \n(98
  3828. \*(11
  3829. .)l
  3830. or
  3831. .(l
  3832. .nr 99 \n(.s
  3833. .nr 98 \n(.f
  3834. .rm 11 
  3835. .as 11 "\fIhead\fR :\- (( \fItest\fR(
  3836. .ps 11
  3837. .ft 2
  3838. .ds 12 "X
  3839. .nr 12 \w'\s11\*(12'
  3840. .nr 10 0u
  3841. .if \n(ct>1 .nr 10 \n(10+\s11.25m\s0
  3842. .nr 14 \s11.1m\s0
  3843. .if \n(ct>1 .nr 14 \s11.15m\s0
  3844. .ds 13 \s11\v'.18m'\h'.05m'\l'\n(12u-.1m\(rn'\h'.05m'\v'-.18m'\s0
  3845. .nr 13 \w'\s11\*(13'
  3846. .as 12 \h'-\n(12u-\n(13u/2u+\n(14u'\v'0-\n(10u'\*(13\v'\n(10u'\h'-\n(13u+\n(12u/2u-\n(14u'
  3847. .ds 12 \x'0'\f2\s11\*(12\|\s\n(99\f\n(98
  3848. .as 11 \*(12
  3849. .ps \n(99
  3850. .ft \n(98
  3851. .as 11 "), ... ) ; ( \fIcomp_\|test\fR(
  3852. .ps 11
  3853. .ft 2
  3854. .ds 12 "X
  3855. .nr 12 \w'\s11\*(12'
  3856. .nr 10 0u
  3857. .if \n(ct>1 .nr 10 \n(10+\s11.25m\s0
  3858. .nr 14 \s11.1m\s0
  3859. .if \n(ct>1 .nr 14 \s11.15m\s0
  3860. .ds 13 \s11\v'.18m'\h'.05m'\l'\n(12u-.1m\(rn'\h'.05m'\v'-.18m'\s0
  3861. .nr 13 \w'\s11\*(13'
  3862. .as 12 \h'-\n(12u-\n(13u/2u+\n(14u'\v'0-\n(10u'\*(13\v'\n(10u'\h'-\n(13u+\n(12u/2u-\n(14u'
  3863. .ds 12 \x'0'\f2\s11\*(12\|\s\n(99\f\n(98
  3864. .as 11 \*(12
  3865. .ps \n(99
  3866. .ft \n(98
  3867. .as 11 "), ...)).
  3868. .ps \n(99
  3869. .ft \n(98
  3870. \*(11
  3871. .)l
  3872. where \fItest\fR is one of the inline tests in the table above, and
  3873. \fIcomp_\|test\fR the corresponding complementary test (note that the
  3874. arguments to \fItest\fR and \fIcomp_\|test\fR have to be identical).
  3875. .ip (\fIii\fR\^)
  3876. Conditionals of the form
  3877. .(l
  3878. \fIhead\fR :\- (\fItest\fR\*<1\*>\fB,\fR ... \fB,\fR \fItest\*<n\*>\fR) \-> \fITrue_\|Case\fR ; \fIFalse_\|Case\fR.
  3879. .)l
  3880. or
  3881. .(l
  3882. \fIhead\fR :\- (\fItest\fR\*<1\*>\fB;\fR ... \fB;\fR \fItest\*<n\*>\fR) \-> \fITrue_\|Case\fR ; \fIFalse_\|Case\fR.
  3883. .)l
  3884. where each \fItest\*<i\*>\fR is an inline test, as mentioned in the table
  3885. above.
  3886. .pp
  3887. The code generated for these cases involves a test and conditional branch, and no
  3888. choice point is created.  We expect future versions of the translator to
  3889. recognize a wider class of complementary tests.
  3890. .pp
  3891. Notice that this discourages the use of explicit cuts.  For example,
  3892. whereas a choice point will be created for
  3893. .(l
  3894. part(M,[E|L],U1,U2) :\-
  3895.    ((E =< M, !, U1 = [E|U1a], U2 = U2a) ;
  3896.     (U1 = U1a, U2 = [E|U2a])),
  3897.    part(M,L,U1a,U2a).
  3898. .)l
  3899. no choice point will be created for either
  3900. .(l
  3901. part(M,[E|L],U1,U2) :\-
  3902.    (E =< M \->
  3903.       (U1 = [E|U1a], U2 = U2a) ;
  3904.       (U1 = U1a, U2 = [E|U2a])),
  3905.    part(M,L,U1a,U2a).
  3906. .)l
  3907. or
  3908. .(l
  3909. part(M,[E|L],U1,U2) :\-
  3910.    ((E =< M, U1 = [E|U1a], U2 = U2a) ;
  3911.     (E > M,  U1 = U1a, U2 = [E|U2a])),
  3912.    part(M,L,U1a,U2a).
  3913. .)l
  3914. Thus, either of the two later versions will be more efficient than the version with the
  3915. explicit cut.
  3916. .sh 3 "Minimizing Data Movement Between Registers"
  3917. .pp
  3918. Data movement between registers for parameter passing may be minimized by leaving variables in
  3919. the same argument position wherever possible.  Thus, the clause
  3920. .(l
  3921.     p(X,Y) :\- p1(X,Y,0).
  3922. .)l
  3923. is preferable to
  3924. .(l
  3925.     p(X,Y) :\- p1(0,X,Y).
  3926. .)l
  3927. because the first definition leaves the variables \fIX\fR and \fIY\fR
  3928. in the same argument positions (first and second, respectively), while the second definition does
  3929. not.
  3930. .sh 2 "Assembly"
  3931. .pp
  3932. The SB-Prolog assembler can be invoked by loading the compiler and using the
  3933. predicate \fI$asm\fR/3:
  3934. .(l
  3935.     | ?- $asm(\fIInFile\fR, \fIOutFile\fR, \fIOptionsList\fR).
  3936. .)l
  3937. where \fIInFile\fR is a Prolog atom which is the name of a WAM assembly source file (e.g.
  3938. the ``.asl'' file generated when a Prolog program is compiled with the ``a'' option),
  3939. \fIOutFile\fR is an atom which is the name of the intended byte code file, and
  3940. \fIOptionsList\fR is a list of options.  The options recognized by the
  3941. assembler are:
  3942. .ip \fBv\fR
  3943. ``Verbose'' mode.  Prints out information regarding progress of assembly.
  3944. .ip \fBt\fR
  3945. ``Trace''.  If specified, the assembler generates code to force procedure calls
  3946. to branch indirectly via the symbol table, instead of using a direct branch.
  3947. This is Useful for tracing compiled code.  It is \fInecessary\fR if the extension table
  3948. feature is to be used.
  3949. .lp
  3950. The assembler is intended primarily to support the compiler, so the assembly language
  3951. syntax is quirky in places.  The interested reader is advised to look at
  3952. the assembly files resulting from compilation with the ``a'' option for more on SB-Prolog assembler
  3953. syntax.
  3954. .ds f. sec6.t
  3955. .sh 1 "Modules"
  3956. .pp
  3957. To describe how modules (or maybe more properly, just libraries) are
  3958. currently supported in our system, we must describe the \fI_\|$undefined_\|pred\fR/1
  3959. interrupt handler.  The system keeps a table of modules and routines that
  3960. are needed from each.  When a predicate is found to be undefined, the table
  3961. is searched to see if it is defined by some module.  If so, that module is
  3962. loaded (if it hasn't been previously loaded) and the association is made
  3963. between the routine name as defined in the module, and the routine name as
  3964. used by the invoker.  
  3965. .pp
  3966. The table of modules and needed routines is:
  3967. .(l C
  3968. defined_\|mods(\fIModname\fR, [\fIpred\fR\*<1\*>/\fIarity\fR\*<1\*>, ..., \fIpred\*<n\*>\fR/\fIarity\*<n\*>\fR]).
  3969. .)l
  3970. where \fIModname\fR is the name of the module.  The module exports \fIn\fR
  3971. predicate definitions.  The first exported pred is of arity \fIarity\fR\*<\*>1,
  3972. and needs to be invoked by the name of \fIpred\fR\*<1\*>.  
  3973. .pp
  3974. The table of modules that have already been loaded is given by
  3975. .(l
  3976.     loaded_\|mods(\fIModname\fR).
  3977. .)l
  3978. A module is a file of predicate definitions, together with a fact defining a list
  3979. of predicates exported by that module, and a set of facts, each of which specifies, for some
  3980. other module, the predicates imported from that module.  For example, consider a module
  3981. name `\fIp\fR'. It contains a single fact, named \fIp_\|export\fR, that is true of the
  3982. list of predicate/arity's that are exported.  E.g.
  3983. .(l
  3984.     p_\|export([p1/2, p2/4])
  3985. .)l
  3986. indicates that the module \fIp\fR exports the predicates \fIp1\fR/2 and \fIp2\fR/4.
  3987. For each module \fIm\fR which contains predicates needed by the module \fIp\fR, there
  3988. is a fact for \fIp_use\fR, describing what module is needed and the names of the
  3989. predicates defined there that are needed.  For example, if module \fIp\fR needs
  3990. to import predicates \fIip1\fR/2 and \fIip2\fR/3 from module \fIq\fR, there would be a fact
  3991. .(l
  3992.     p_\|use(q,[ip1/2, ip2/3])
  3993. .)l
  3994. where \fIq\fR is a module that exports two predicates: one 2-ary and one
  3995. 3-ary.  This list corresponds to the export list of module \fIq\fR.  
  3996. .pp
  3997. The correspondence between the predicates in the export list of an exporting
  3998. module, and those in the import or \fIuse\fR list of a module which imports one or
  3999. more of them, is by position, i.e. the predicate names at the exporting and importing names may be different, and
  4000. the association between names in the two lists is by the position in the list.  If the importing
  4001. module does not wish to import one or more of the predicates exported by the
  4002. exporting module, it may put an anonymous variable in the corresponding position in
  4003. its \fIuse\fR list.  Thus, for example, if module \fIp\fR above had wished to import only
  4004. the predicate \fIip2\fR/3 from module \fIq\fR, the corresponding use fact would
  4005. be
  4006. .(l
  4007.     p_\|use(q, [_\|\|, ip2/3]).
  4008. .)l
  4009. .pp
  4010. The initial set of predicates and the modules from which they are to be
  4011. loaded is set up by an initial call to \fI$prorc\fR/0 (see the SB-Prolog
  4012. system file \fImodlib/src/$prorc.P\fR).
  4013. This predicate makes initial calls to the predicate $define_\|mod which set up
  4014. the tables described above so that the use of standard predicates will cause
  4015. the correct modules to be loaded in the \fI_\|$undefined_\|pred\fR routine, and the
  4016. correct names to be used.
  4017. .ds f. sec7.t
  4018. .sh 1 "Macros"
  4019. .pp
  4020. SB-Prolog features a facility for the definition and expansion of macros that is fully compatible with
  4021. the runtime system.  Its basic mechanism is a simple partial evaluator.  It
  4022. is called by both \fIconsult\fR and \fIcompile\fR, so that macro expansion
  4023. occurs independently of whether the code is interpreted or compiled (but not when
  4024. asserted).  Moreover, the macro definitions are retained as clauses at runtime, so
  4025. that invocation of macros via \fIcall\fR/1 at runtime (or from asserted clauses)
  4026. does not pose a problem.  This means, however, that if the same macro is
  4027. used in many different files, it will be loaded more than once, thus leading to
  4028. wasetd space.  This ought to be thought about and fixed.
  4029. .pp
  4030. The source for the macro expander is in the SB-Prolog system file
  4031. \fImodlib/src/$mac.P\fR.
  4032. .sh 2 "Defining Macros"
  4033. .pp
  4034. `Macros', or predicates to be evaluated at compile-time, are defined 
  4035. by clauses of the form
  4036. .(l
  4037.     Head ::\- Body
  4038. .)l
  4039. where facts have `true' as their body.
  4040. .(x b
  4041. (P)     ::\-/2
  4042. .)x
  4043. The partial evaluator will expand any call to a
  4044. predicate defined by ::\-/2 that
  4045. unifies with the head of only one clause in ::\-/2. If a call unifies with the
  4046. head of more than one clause in ::\-/2, it will not be expanded
  4047. Notice that this is not a
  4048. fundamental restriction, since `;' is permitted in the body of a
  4049. clause.  The partial evaluator also converts each definition of the form
  4050. .(l
  4051.     Head ::\- Body.
  4052. .)l
  4053. to a clause of the form
  4054. .(l
  4055.     Head :\- Body.
  4056. .)l
  4057. and adds this second clause to the other ``normal'' clauses that were read from
  4058. the file.  This ensures that calls to the macro at runtime, e.g. through
  4059. \fIcall\fR/1 or from unexpanded calls in the program do not cause any
  4060. problems.
  4061. .pp
  4062. The partial evaluator is actually a Prolog interpreter written
  4063. `purely' in  Prolog, i.e., variable assignments are explicitly
  4064. handled.  This is necessary to be able to handle impure constructs
  4065. such as `var(X), X=a'.  As a result this is a \fIvery slow\fR Prolog
  4066. evaluator.  
  4067. .pp
  4068. Since naive partial evaluation can go into an infinite loop, SB-Prolog's
  4069. partial evaluator
  4070. maintains a depth-bound and will not expand recursive calls deeper
  4071. than that.  The depth is determined by the globalset predicate
  4072. \fI$mac_\|depth\fR.  The default value for \fI$mac_\|depth\fR
  4073. is 50  This can be changed to some other value \fIn\fR by executing
  4074. .(l
  4075.     | ?- globalset($mac_\|depth(\fIn\fR)).
  4076. .)l.
  4077. .sh 2 "Macro Expander Options"
  4078. .lp
  4079. The following options are recognized by the macro expander:
  4080. .ip \fBd\fR
  4081. Dumps all clauses to the user after expansion.  Useful for debugging.
  4082. .ip \fBe\fR
  4083. Expand macros.  If omitted, the expander simply converts each ::\-/2 clause to a normal :\-/2 clause.
  4084. .ip \fBv\fR
  4085. ``Verbose'' mode.  Prints macros that are/are not being expanded.
  4086. .ds f. sec8.t
  4087. .sh 1 "Extension Tables"
  4088. .pp
  4089. Extension tables store the calls and answers for a predicate. If a
  4090. call has been made before, answers are retrieved from the extension
  4091. table instead of being recomputed. Extension tables provide a
  4092. caching mechanism for Prolog. In addition, extension tables affect
  4093. the termination characteristics of recursive programs. Some Prolog
  4094. programs, which are logically correct, enter an infinite loop due
  4095. to recursive predicates. An extension table saved on recursive 
  4096. predicates can find all answers (provided the set of such answers is
  4097. finite) and terminate 
  4098. for some logic programs for which Prolog's evaluation strategy 
  4099. enters an infinite loop. Iterations over the extension table 
  4100. execution strategy provides complete evaluation of queries over
  4101. function-free Horn clause programs. 
  4102. .pp    
  4103. To be able to use the simple extension table evaluation on a set of
  4104. predicates, the source file should either be consulted, or 
  4105. compiled with the \fBt\fR option (the \fBt\fR option 
  4106. keeps the assembler from optimizing subroutine linkage and allows 
  4107. the extension table facility to intercept calls to predicates). 
  4108. .pp
  4109. To use extension table execution, all predicates that are to be 
  4110. saved in the extension table must be passed to \fIet\fR/1. For example,
  4111. .(l
  4112.     | ?\- et([pred1/1, pred2/2]), et(pred3/2)
  4113. .)l
  4114. will set up ``ET-points'' for the for predicates \fIpred1\fR/1, \fIpred2\fR/2 and
  4115. \fIpred3\fR/2, which will cause extension tables for these predicates to be
  4116. maintained during execution. At the time of the call to \fIet\fR/1, these predicates
  4117. must be defined, either by having been loaded, or through \fIconsult\fR.
  4118. .pp
  4119. The predicate \fInoet\fR/1 takes a list of predicate/arity pairs for
  4120. which ET-points should be deleted.  Notice that
  4121. once an ET-point has been set up for a predicate, it will be maintained
  4122. unless explicitly deleted via \fInoet\fR/1.
  4123. If the definition of a predicate which has an ET-point defined is to be updated,
  4124. the ET-point must first be deleted via \fInoet\fR/1.
  4125. The predicate can then be reloaded and a new ET-point established.
  4126. This is enforced by the failure of the goal ``et(P/N)'' if an
  4127. ET-point already exists for the argument predicate.  In this case,
  4128. the following error message will be displayed:
  4129. .(l
  4130.     *et* already defined for: P/N
  4131. .)l
  4132. .pp            
  4133. There are, in fact, two extension table algorithms: a simple one, which
  4134. simply caches calls to predicates which have ET-points defined; and a
  4135. complete ET algorithm, which iterates the simple extension table algorithm until
  4136. no more answers can be found.  The simple algorithm is more efficient than the
  4137. complete one; however, the simple algorithm is not complete for certain 
  4138. especially nasty forms of mutual recursion, while the complete
  4139. algorithm is.  To use the simple extension table algorithm,
  4140. predicates can simply be called as usual.
  4141. The complete extension table algorithm may be used via the query
  4142. .(l
  4143.     | ?\- et_\|star(Query).
  4144. .)l
  4145. .lp
  4146. The extension table algorithm is intended for predicates that are ``essentially
  4147. pure'', and results are not guaranteed for code using impure code.
  4148. The extension table algorithm saves only those answers which are
  4149. not instances of what is already in the
  4150. table, and uses these answers if the current call is an instance of a call
  4151. already made.  For example, if a call \fIp\fR(\fIX\fR, \fIY\fR\^), with
  4152. \fIX\fR and \fIY\fR uninstantiated, is encountered and inserted into the
  4153. extension table, then a subsequent call \fIp\fR(\fIX\fR, \fIb\fR) will be
  4154. computed using the answers for \fIp\fR(\fIX\fR, \fIY\fR\^) already in the
  4155. extension table.  Notice that this might not work
  4156. if var/nonvar tests are used on the second argument
  4157. in the evaluation of \fIp\fR.
  4158. .pp
  4159. Another problem with using impure code is that if an ET predicate is cut
  4160. over, then the saved call implies that all answers for that predicate were
  4161. computed,
  4162. but there are only partial results in the ET because of the cut.
  4163. So on a subsequent call the incomplete extension table answers are used
  4164. when all answers are expected.
  4165. .(l
  4166. Example:
  4167.     r(X,Y) :\- p(X,Y),q(Y,Z),!,fail.
  4168.     | ?\-  r(X,Y) ; p(X,Y).
  4169. .)l
  4170. .lp
  4171. Let p be an ET predicate whose evaluation yields many tuples.
  4172. In the evaluation of the query, r(X,Y) makes a call to p(X,Y).
  4173. Assuming that there is a tuple such that q(Y,Z) succeeds with the
  4174. first p tuple then the evaluation of p is cut over. The call to p(X,Y)
  4175. in the query uses the extension table because of the previous call
  4176. in the evaluation of r(X,Y). Only one answer is found, whereas the
  4177. relation p contains many tuples, so the computation is not complete.
  4178. Note that "cuts" used within the evaluation of an ET predicate are ok,
  4179. as long as they don't cut over the evaluation of another ET predicate. 
  4180. The evaluation of the predicate that uses cuts does not cut over any et
  4181. processing (such as storing or retrieving answers) so that the tuples that
  4182. are computed are saved. In the following example, the ET is used to generate
  4183. prime numbers where an ET point is put on prime/1.
  4184. .(l
  4185. Example:
  4186.  
  4187. prime(I) :\- globalset(globalgenint(2)),fail.            /* Generating Primes */
  4188. prime(I) :\- genint(I), not(div(I)).
  4189. div(I) :\- prime(X), 0 is I mod X.
  4190.  
  4191. genint(N) :\- 
  4192.     repeat,
  4193.     globalgenint(N),
  4194.     N1 is N+1,
  4195.     globalset(globalgenint(N1)).
  4196. .)l
  4197. .lp
  4198. The following summarizes the library predicates supporting the extension
  4199. table facility:
  4200. .sp
  4201. .lp
  4202. \fBet\fR(\fIL\fR)
  4203. .ip
  4204. Sets up an ET-point on the predicates \fIL\fR, which causes calls and
  4205. anwers to these predicates to be saved in an ``extension table''.
  4206. .(x b
  4207. (L)     et/1
  4208. .)x
  4209. \fIL\fR is either a term \fIPred\fR/\fIArity\fR, where \fIPred\fR is a predicate
  4210. symbol and \fIArity\fR its arity, or a set of such terms represented as a list.
  4211. \fIL\fR must be
  4212. instantiated, and the predicates specified in it defined, at the time of
  4213. the call to \fIet\fR/1.  Gives error messages and fails if any of
  4214. the predicates in \fIL\fR is undefined, or if an ET-point
  4215. already exists on any of them; in this case, no ET-point
  4216. is set up on any of the predicates in \fIL\fR.
  4217. .lp
  4218. \fBet_\|\fBstar\fR(Goal\fR\^)
  4219. .ip
  4220. Invokes the complete extension table algorithm on the goal \fIGoal\fR.
  4221. .(x b
  4222. (L)     et_\|star/1
  4223. .)x
  4224. .lp
  4225. \fIet_\^points\fR(\fIL\fR)
  4226. .ip
  4227. Unifies \fIL\fR with a list of predicates for which an ET-point is
  4228. defined.  \fIL\fR is the empty list [] if there are no ET-points defined.
  4229. .(x b
  4230. (L)     et_\^points/1
  4231. .)x
  4232. .lp
  4233. \fBnoet\fR(\fIL\fR)
  4234. .ip
  4235. Deletes ET-points on the predicates specified in \fIL\fR.
  4236. .(x b
  4237. (L)     noet/1
  4238. .)x
  4239. \fIL\fR is either a term \fIP\fR/\fIN\fR, where \fIP\fR is the name of a
  4240. predicate and \fIN\fR its arity, or a set of such terms represented as a
  4241. list.  Gives error messages and
  4242. fails if there is no ET-point on any of the predicates specified in
  4243. \fIL\fR.  Deleting an ET-point for a predicate also removes the calls and
  4244. answers stored in the extension table for that predicate.  The extension
  4245. tables for all predicates for which ET-points are defined may be deleted
  4246. using \fIet_\^points\fR/1 in cojnunction with \fInoet\fR/1.
  4247.  
  4248. \fIL\fR must be instantiated at the time of the call to \fInoet\fR/1.
  4249. .lp
  4250. \fBet_\|remove(\fIL\^)
  4251. .ip
  4252. Removes both calls and answers for the predicates specified in \fIL\fR.  In
  4253. effect, this results in the extension table for these predicates to be set
  4254. to empty.  \fIL\fR must be instantiated at the time of the call to
  4255. either a term \fIP\fR/\fIN\fR, where \fIP\fR is a
  4256. predicate with arity \fIN\fR, or a list of such terms.  An error occurs if
  4257. any of the predicates in \fIL\fR does not have an ET-point set.
  4258.  
  4259. All extension tables can be emptied by using \fIet_\^points\fR/1 in
  4260. conjunction with \fIet_\^remove\fR/1.
  4261. .(x b
  4262. (L)     et_\|remove/1
  4263. .)x
  4264. .lp
  4265. \fBet_\|answers\fR\^(\fIP\fR/\fIN\fR, \fITerm\fR\^)
  4266. .ip
  4267. Retrieves the answers stored in the extension table for the predicate \fIP\fR/\fIN\fR
  4268. in \fITerm\fR one at a time.  \fITerm\fR is of the form
  4269. \fIP\fR(\fIt\fR\*<1\*>, ..., \fIt\*<N\*>\fR).  An error results and
  4270. \fIet_\^answers\fR/2 fails if \fIP\fR/\fIN\fR is not fully specified (ground),
  4271. or if \fIP\fR/\fIN\fR does not have an ET-point set.
  4272. .lp
  4273. \fBet_\|calls\fR(\fIP/N\fR, \fITerm\fR\^)
  4274. .ip
  4275. Retrieves the calls stored in the extension table for the predicate \fIP/N\fR
  4276. in \fITerm\fR one at a time.  \fITerm\fR is of the form
  4277. \fIP\fR(\fIt\fR\*<1\*>, ..., \fIt\*<N\*>\fR).  An error results and
  4278. \fIet_\^calls\fR/2 fails if \fIP\fR/\fIN\fR is not fully specified (ground),
  4279. or if \fIP\fR/\fIN\fR does not have an ET-point set.
  4280. .(x b
  4281. (L)     et_\|calls/2
  4282. .)x
  4283. .lp
  4284. .ds f. sec9.t
  4285. .sh 1 "Other Library Utilities"
  4286. .pp
  4287. The SB-Prolog library contains various other utilities, some of which are
  4288. listed below.
  4289. .lp
  4290. \fB$append\fR(\fIX\fR, \fIY\fR, \fIZ\fR)
  4291. .ip
  4292. Succeeds if list \fIZ\fR is the concatenation of lists \fIX\fR and \fIY\fR.
  4293. .(x b
  4294. (L)     $append/3
  4295. .)x
  4296. .lp
  4297. \fB$member\fR(\fIX\fR, \fIL\fR)
  4298. .ip
  4299. Checks whether \fIX\fR unifies with any element of list \fIL\fR, succeeding more than
  4300. once if there are multiple such elements.
  4301. .(x b
  4302. (L)     $member/2
  4303. .)x
  4304. .lp
  4305. \fB$memberchk\fR(\fIX\fR, \fIL\fR)
  4306. .ip
  4307. Similar to $\fImember\fR/2, except that $\fImemberchk\fR/2 is
  4308. deterministic, i.e. does not succeed more than once for any call.
  4309. .lp
  4310. \fB$reverse\fR(\fIL\fR, \fIR\fR)
  4311. .ip
  4312. Succeeds if \fIR\fR is the reverse of list \fIL\fR.  If \fIL\fR is not a
  4313. fully determined list, i.e. if the tail of \fIL\fR is a variable, this
  4314. predicate can succeed arbitrarily many times.
  4315. .(x b
  4316. (L)     $reverse/2
  4317. .)x
  4318. .lp
  4319. \fB$merge\fR(\fIX\fR, \fIY\fR, \fIZ\fR\^)
  4320. .ip
  4321. Succeeds if \fIZ\fR is the list resulting from ``merging'' lists
  4322. \fIX\fR and \fIY\fR, i.e. the elements of \fIX\fR together with any
  4323. element of \fIY\fR not occurring in \fIX\fR.  If \fIX\fR or \fIY\fR contain
  4324. duplicates, \fIZ\fR may also contain duplicates.
  4325. .(x b
  4326. (L)     $merge/3
  4327. .)x
  4328. .lp
  4329. \fB$absmember\fR(\fIX\fR, \fIL\fR\^)
  4330. .ip
  4331. Similar to \fI$member\fR/2, except that it checks for identity (through ==/2)
  4332. rather than unifiability (through =/2) of \fIX\fR with elements of \fIL\fR.
  4333. .(x b
  4334. (L)     $absmember/2
  4335. .)x
  4336. .lp
  4337. \fB$nthmember\fR(\fIX\fR, \fIL\fR, \fIN\fR)
  4338. .ip
  4339. Succeeds if the \fIN\fR\*[th.\*] element of the list \fIL\fR unifies with
  4340. \fIX\fR.  Fails if \fIN\fR is greater than the length of \fIL\fR.  Either
  4341. \fIX\fR and \fIL\fR, or \fIL\fR and \fIN\fR, should be instantiated at the
  4342. time of the call.
  4343. .(x b
  4344. (L)     $nthmember/3
  4345. .)x
  4346. .lp
  4347. \fB$member2\fR(\fIX\fR, \fIL\fR)
  4348. .ip
  4349. Checks whether \fIX\fR unifies with any of the actual elements of \fIL\fR.
  4350. The only difference between this and \fI$member\fR/2 is on lists with
  4351. a variable tail, e.g. [a, b, c | _ ]: while \fI$member\fR/2 would insert
  4352. \fIX\fR at the end of such a list if it did not find it, \fI$member2\fR/2
  4353. only checks for membership but does not insert it into the list if it is
  4354. not there.
  4355. .lp
  4356. .ds f. seca.t
  4357. .sh 1 "Pragma Files"
  4358. .pp
  4359. Users may specify pragma information about SB-Prolog programs in a file called the
  4360. \fIpragma file\fR for the program.  The pragma file corresponding to a
  4361. Prolog source file \fIfoo\fR is a file \fIfoo.def\fR which is either in the
  4362. same directory as the source file, or is in a subdirectory \fIdefs\fR in the
  4363. directory containing the source file.  In other words, relative to the
  4364. directory containing the source file \fIfoo\fR, the pragma file can be either
  4365. \fIfoo.def\fR or \fIdefs/foo.def\fR.
  4366. .pp
  4367. Pragma information in such a file is specified as a set of facts
  4368. \fIprag/3\fR.  The first and second arguments are, respectively, the name and arity
  4369. of the predicate for which information is being specified.  The third
  4370. argument is the pragma being specified, and can be either a term, or a list of
  4371. terms.  Thus, for example, to specify that an index is to be created on the
  4372. first argument position for a predicate \fIbar\fR/3 in the file \fIfoo\fR, we would enter, in a file \fIfoo.def\fR, the fact ``prag(bar, 3, index)'' 
  4373. or ``prag(bar, 3, [index])''.
  4374. .pp
  4375. The pragma information that may be specified is limited, at this point, to
  4376. information about indexing.  If an index is to be created on argument \fIn\fR
  4377. of a predicate, the corresponding pragma is a term ``index(\fIn\fR)''.
  4378. In the special case where \fIn\fR = 1, the pragma ``\fIindex\fR(1)''
  4379. may be abbreviated to ``\fIindex\fR''.
  4380. .sp
  4381. .lp
  4382. \fBAcknowledgements\fR
  4383. .pp
  4384. A large number of people were involved, at some time or another, with the
  4385. Logic Programming group at SUNY, Stony Brook, and  deserve credit for
  4386. bringing the SB-Prolog system to its present form.  The following names, in particular, ought
  4387. to be mentioned: David Scott Warren, Weidong Chen, Suzanne Dietrich,
  4388. Sanjay Manchanda, Jiyang Xu and David Znidarsic.  The author is also
  4389. grateful to Fernando Pereira for permission to use material from the
  4390. C-Prolog manual for the descriptions of Prolog syntax and many of the builtins.
  4391. .ds f. secb.t
  4392. .nr % 53
  4393. junk
  4394. .bp
  4395. .uh "Appendix 1: Evaluable Predicates of SB-Prolog"
  4396. .sp
  4397. .pp
  4398. An entry of ``B'' indicates a builtin predicate, ``I'' an inline predicate,
  4399. and ``L'' a library predicate.  A ``P'' indicates that the predicate is
  4400. handled by the preprocessor during compilation and/or consulting.
  4401. .sp
  4402. .2c
  4403. .lp
  4404. .xp b
  4405. .1c
  4406. .lp
  4407. .bp
  4408. .lp
  4409. .uh "Appendix 2: Adding Builtins to SB-Prolog"
  4410. .EQ
  4411. .nr 99 \n(.s
  4412. .nr 98 \n(.f
  4413. .ps 11
  4414. .ft 2
  4415. .ps \n(99
  4416. .ft \n(98
  4417. .EN
  4418. .pp
  4419. Adding a builtin involves writing the C code for the desired case and installing
  4420. it into the simulator.  The files in the irectory \fIsim/builtin\fR
  4421. contain the C code for the builtin predicates supported by the system.
  4422. The following procedure is to be followed when adding a builtin to the system:
  4423. .sp
  4424. .lp
  4425. \fII. Installing C Code\fR:
  4426. .np
  4427. Go to the directory \fIsim/builtin\fR.
  4428. .np
  4429. Look at the \fB#define\fRs in the file \fIbuiltin.h\fR, and choose a number \fIN1\fR
  4430. (between 0 and 255) which is not in use to be the builtin number for the new builtin.
  4431. .np
  4432. Add to the file \fIbuiltin.h\fR the line
  4433. .(l
  4434.     #define NEWBUILTIN \fIN1\fR
  4435. .)l
  4436. .np
  4437. The convention is that the code for builtin will be in a parameterless
  4438. procedure named \fIb_\|NEWBUILTIN\fR.  Modify the file
  4439. \fIinit_\|\^branch.c\fR in the directory \fIsim/builtin\fR by adding these lines:
  4440. .(l
  4441.     extern int b_\|NEWBUILTIN();
  4442. and
  4443.     set_\|b_\|inst ( NEWBUILTIN, b_\|NEWBUILTIN );
  4444. .)l
  4445. in the appropriate places.
  4446. .np
  4447. The builtins are compiled together into one object file, \fIbuiltin\fR.
  4448. Update the file \fIMakefile\fR by appending the name of your object code file at the
  4449. end of the line ``OBJS = ... '' and insert the appropriate commands to compile
  4450. your C source file, e.g.:
  4451. .(l
  4452. OBJS = [ ... \fIother file names\fR ... ] newbuiltin.o
  4453.  
  4454.  ...
  4455.  
  4456. newbuiltin.o: $(HS)
  4457.     cc $(CFLAGS) newbuiltin.c
  4458. .)l
  4459. .np
  4460. Execute the updated make file to create an updated object file \fIbuiltin\fR.
  4461. .np
  4462. Go to the directory \fIsim\fR and execute \fImake\fR to install the new file \fIbuiltin\fR.
  4463. .sp
  4464. .lp
  4465. \fIII. Installing Prolog Code\fR:
  4466. .pp
  4467. Assume that the builtin predicate to be added is \fInewbuiltin\fR/4.  The
  4468. procedure for installing the Prolog code for this is as follows:
  4469. .np
  4470. Go to the SB-Prolog system directory \fIlib/src\fR, where the Prolog source for
  4471. the library routines is kept.
  4472. .np
  4473. Each builtin definition is of the form
  4474.  
  4475. .(l
  4476.     pred( ... ) :\- '_\|$builtin'(\fIN\fR\^).
  4477. .)l
  4478.  
  4479. where \fIN\fR is an integer, the builtin number of \fIpred\fR.
  4480. .np
  4481. Create a Prolog source file \fInewbuiltin.P\fR (notice correspondence with
  4482. the name of the predicate being defined) containing the definition
  4483.  
  4484. .(l
  4485.     newbuiltin(A,B,C,D) :\- '_\|$builtin'(\fIN1\fR\^).
  4486. .)l
  4487.  
  4488. where \fIN1\fR is the builtin number of the predicate \fInewbuiltin\fR,
  4489. obtained when installing the C code for the builtin (see above).
  4490. .np
  4491. Compile this Prolog predicate, using the simulator and the \fIcompile\fR
  4492. predicate, into a file \fInewbuiltin\fR (notice correspondence with the name of
  4493. the predicate being defined) in the SB-Prolog directory \fIlib\fR.
  4494. .sp
  4495. .fo ''''
  4496. .bp
  4497. .ce
  4498. \fBTABLE OF CONTENTS\fR
  4499. .sp
  4500. .xp c
  4501.