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