guile.texi 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. \input texinfo
  2. @c -*-texinfo-*-
  3. @c %**start of header
  4. @setfilename guile.info
  5. @documentencoding UTF-8
  6. @settitle Guile Reference Manual
  7. @set guile
  8. @set MANUAL-REVISION 1
  9. @c %**end of header
  10. @include version.texi
  11. @include effective-version.texi
  12. @copying
  13. This manual documents Guile version @value{VERSION}.
  14. Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
  15. Inc. @*
  16. Copyright (C) 2021 Maxime Devos
  17. Permission is granted to copy, distribute and/or modify this document
  18. under the terms of the GNU Free Documentation License, Version 1.3 or
  19. any later version published by the Free Software Foundation; with no
  20. Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
  21. copy of the license is included in the section entitled ``GNU Free
  22. Documentation License.''
  23. Additionally, the documentation of the 125, 126, 128, 151 and 160 SRFI
  24. modules is adapted from their specification text, which is made
  25. available under the following Expat license:
  26. Permission is hereby granted, free of charge, to any person obtaining a
  27. copy of this software and associated documentation files (the
  28. "Software"), to deal in the Software without restriction, including
  29. without limitation the rights to use, copy, modify, merge, publish,
  30. distribute, sublicense, and/or sell copies of the Software, and to
  31. permit persons to whom the Software is furnished to do so, subject to
  32. the following conditions:
  33. The above copyright notice and this permission notice shall be included
  34. in all copies or substantial portions of the Software.
  35. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  36. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  37. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  38. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  39. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  40. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  41. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  42. @end copying
  43. @c Notes
  44. @c
  45. @c We no longer use the category "primitive" to distinguish C-defined
  46. @c Scheme procedures from those defined in Scheme. Instead, the
  47. @c reference manual now includes a C declaration as well as a Scheme
  48. @c declaration for each procedure that is available in both Scheme and
  49. @c C.
  50. @c
  51. @c When adding a new reference entry to the Guile manual, please
  52. @c document it with @deffn using one of the following categories:
  53. @c
  54. @c {Scheme Procedure}
  55. @c {Scheme Syntax}
  56. @c {C Function}
  57. @c {C Macro}
  58. @c
  59. @c If the entry is for a new primitive, it should have both a @deffn
  60. @c {Scheme Procedure} line and a @deffnx {C Function} line; see the
  61. @c manual source for plenty of existing examples of this.
  62. @c
  63. @c For {C Function} entries where the return type and all parameter
  64. @c types are SCM, we omit the SCMs. This is easier to read and also
  65. @c gets round the problem that Texinfo doesn't allow a @deftypefnx
  66. @c inside a @deffn.
  67. @c
  68. @c For a list of Guile primitives that are not yet incorporated into the
  69. @c reference manual, see the file `new-docstrings.texi', which holds all
  70. @c the docstrings snarfed from the libguile C sources for primitives
  71. @c that are not in the reference manual. If you have worked with some
  72. @c of these concepts, implemented them, or just happen to know what they
  73. @c do, please write up a little explanation -- it would be a big help.
  74. @c Alternatively, if you know of any reason why some of these should
  75. @c *not* go in the manual, please let the mailing list
  76. @c <guile-devel@gnu.org> know.
  77. @c Define indices that are used in the Guile Scheme part of the
  78. @c reference manual to group stuff according to whether it is R5RS or a
  79. @c Guile extension.
  80. @defcodeindex rn
  81. @c vnew - For (some) new items, indicates the Guile version in which
  82. @c item first appeared. In future, this could be made to expand to
  83. @c something like a "New in Guile 45!" banner.
  84. @macro vnew{VERSION}
  85. @end macro
  86. @c The following, @le{} and @ge{}, are standard tex directives, given
  87. @c definitions for use in non-tex.
  88. @c
  89. @ifnottex
  90. @macro ge
  91. >=
  92. @end macro
  93. @macro le
  94. <=
  95. @end macro
  96. @end ifnottex
  97. @c @cross{} is a \times symbol in tex, or an "x" in info. In tex it works
  98. @c inside or outside $ $.
  99. @tex
  100. \gdef\cross{\ifmmode\times\else$\times$\fi}
  101. @end tex
  102. @ifnottex
  103. @macro cross
  104. x
  105. @end macro
  106. @end ifnottex
  107. @c @m{T,N} is $T$ in tex or @math{N} otherwise. This is an easy way to give
  108. @c different forms for math in tex and info.
  109. @iftex
  110. @macro m {T,N}
  111. @tex$\T\$@end tex
  112. @end macro
  113. @end iftex
  114. @ifnottex
  115. @macro m {T,N}
  116. @math{\N\}
  117. @end macro
  118. @end ifnottex
  119. @c @nicode{S} is plain S in info, or @code{S} elsewhere. This can be used
  120. @c when the quotes that @code{} gives in info aren't wanted, but the
  121. @c fontification in tex or html is wanted. @alias is used rather
  122. @c than @macro because backslashes don't work properly in an @macro.
  123. @ifinfo
  124. @alias nicode=asis
  125. @end ifinfo
  126. @ifnotinfo
  127. @alias nicode=code
  128. @end ifnotinfo
  129. @c @iftex
  130. @c @cropmarks
  131. @c @end iftex
  132. @dircategory The Algorithmic Language Scheme
  133. @direntry
  134. * Guile Reference: (guile). The Guile reference manual.
  135. @end direntry
  136. @setchapternewpage odd
  137. @titlepage
  138. @sp 10
  139. @comment The title is printed in a large font.
  140. @title Guile Reference Manual
  141. @subtitle Edition @value{EDITION}, revision @value{MANUAL-REVISION}, for use with Guile @value{VERSION}
  142. @c @subtitle $Id: guile.texi,v 1.49 2008-03-19 22:51:23 ossau Exp $
  143. @c See preface.texi for the list of authors
  144. @author The Guile Developers
  145. @c The following two commands start the copyright page.
  146. @page
  147. @vskip 0pt plus 1filll
  148. @vskip 0pt plus 1filll
  149. @insertcopying
  150. @end titlepage
  151. @c @smallbook
  152. @finalout
  153. @headings double
  154. @c Where to find Guile examples.
  155. @set example-dir doc/examples
  156. @ifnottex
  157. @node Top, Preface, (dir), (dir)
  158. @top The Guile Reference Manual
  159. @insertcopying
  160. @sp 1
  161. @end ifnottex
  162. @menu
  163. * Preface::
  164. * Introduction::
  165. * Hello Guile!::
  166. * Hello Scheme!::
  167. * Programming in Scheme::
  168. * Programming in C::
  169. * API Reference::
  170. * Guile Modules::
  171. * GOOPS::
  172. * Guile Implementation::
  173. Appendices
  174. * GNU Free Documentation License:: The license of this manual.
  175. Indices
  176. * Concept Index::
  177. * Procedure Index::
  178. * Variable Index::
  179. * Type Index::
  180. * R5RS Index::
  181. @end menu
  182. @contents
  183. @include preface.texi
  184. @include intro.texi
  185. @include tour.texi
  186. @include scheme-ideas.texi
  187. @include scheme-reading.texi
  188. @node Programming in Scheme
  189. @chapter Programming in Scheme
  190. Guile's core language is Scheme, and a lot can be achieved simply by using Guile
  191. to write and run Scheme programs --- as opposed to having to dive into C code.
  192. In this part of the manual, we explain how to use Guile in this mode, and
  193. describe the tools that Guile provides to help you with script writing,
  194. debugging, and packaging your programs for distribution.
  195. For detailed reference information on the variables, functions, and so
  196. on that make up Guile's application programming interface (API), see
  197. @ref{API Reference}.
  198. @menu
  199. * Guile Scheme:: Guile's implementation of Scheme.
  200. * Invoking Guile:: Selecting optional features when starting Guile.
  201. * Guile Scripting:: How to write Guile scripts.
  202. * Using Guile Interactively:: Guile's REPL features.
  203. * Using Guile in Emacs:: Guile and Emacs.
  204. * Using Guile Tools:: A guild of scheming wizards.
  205. * Installing Site Packages:: Installing Scheme code.
  206. * Distributing Guile Code:: Building and distributing your code.
  207. @end menu
  208. @include scheme-intro.texi
  209. @include guile-invoke.texi
  210. @include scheme-scripts.texi
  211. @include scheme-using.texi
  212. @node Programming in C
  213. @chapter Programming in C
  214. This part of the manual explains the general concepts that you need to
  215. understand when interfacing to Guile from C. You will learn about how
  216. the latent typing of Scheme is embedded into the static typing of C, how
  217. the garbage collection of Guile is made available to C code, and how
  218. continuations influence the control flow in a C program.
  219. This knowledge should make it straightforward to add new functions to
  220. Guile that can be called from Scheme. Adding new data types is also
  221. possible and is done by defining @dfn{foreign objects}.
  222. The @ref{Programming Overview} section of this part contains general
  223. musings and guidelines about programming with Guile. It explores
  224. different ways to design a program around Guile, or how to embed Guile
  225. into existing programs.
  226. For a pedagogical yet detailed explanation of how the data representation of
  227. Guile is implemented, @xref{Data Representation}. You don't need to know the
  228. details given there to use Guile from C, but they are useful when you want to
  229. modify Guile itself or when you are just curious about how it is all done.
  230. For detailed reference information on the variables, functions
  231. etc. that make up Guile's application programming interface (API),
  232. @xref{API Reference}.
  233. @menu
  234. * Parallel Installations:: Finding the right Guile.
  235. * Linking Programs With Guile:: More precisely, with the libguile library.
  236. * Linking Guile with Libraries:: To extend Guile itself.
  237. * General Libguile Concepts:: General concepts for using libguile.
  238. * Defining New Foreign Object Types:: Adding new types to Guile.
  239. * Function Snarfing:: A way to define new functions.
  240. * Programming Overview:: An overview of Guile programming.
  241. * Autoconf Support:: Putting m4 to good use.
  242. @end menu
  243. @include libguile-parallel.texi
  244. @include libguile-linking.texi
  245. @include libguile-extensions.texi
  246. @include libguile-concepts.texi
  247. @include libguile-foreign-objects.texi
  248. @include libguile-snarf.texi
  249. @include libguile-program.texi
  250. @include libguile-autoconf.texi
  251. @node API Reference
  252. @chapter API Reference
  253. Guile provides an application programming interface (@dfn{API}) to
  254. developers in two core languages: Scheme and C. This part of the manual
  255. contains reference documentation for all of the functionality that is
  256. available through both Scheme and C interfaces.
  257. @menu
  258. * API Overview:: Overview of the Guile API.
  259. * Deprecation:: Obsolete back-compatible APIs.
  260. * The SCM Type:: The fundamental data type for C code.
  261. * Initialization:: Initializing Guile.
  262. * Snarfing Macros:: Macros for snarfing initialization actions.
  263. * Data Types:: Representing values in Guile.
  264. * Procedures:: Procedures.
  265. * Macros:: Extending the syntax of Scheme.
  266. * Utility Functions:: General utility functions.
  267. * Binding Constructs:: Definitions and variable bindings.
  268. * Control Mechanisms:: Controlling the flow of program execution.
  269. * Input and Output:: Ports, reading and writing.
  270. * Regular Expressions:: Pattern matching and substitution.
  271. * LALR(1) Parsing:: Generating LALR(1) parsers.
  272. * PEG Parsing:: Parsing Expression Grammars.
  273. * Read/Load/Eval/Compile:: Reading and evaluating Scheme code.
  274. * Memory Management:: Memory management and garbage collection.
  275. * Modules:: Designing reusable code libraries.
  276. * Foreign Function Interface:: Interacting with C procedures and data.
  277. * Foreign Objects:: Defining new data types in C.
  278. * Smobs:: Use foreign objects instead.
  279. * Scheduling:: Threads, mutexes, asyncs and dynamic roots.
  280. * Options and Config:: Configuration, features and runtime options.
  281. * Other Languages:: Emacs Lisp, ECMAScript, and more.
  282. * Internationalization:: Support for gettext, etc.
  283. * Debugging:: Debugging infrastructure and Scheme interface.
  284. * Code Coverage:: Gathering code coverage data.
  285. @end menu
  286. @include api-overview.texi
  287. @include api-deprecated.texi
  288. @include api-scm.texi
  289. @include api-init.texi
  290. @include api-snarf.texi
  291. @include api-data.texi
  292. @include api-procedures.texi
  293. @include api-macros.texi
  294. @include api-utility.texi
  295. @include api-binding.texi
  296. @include api-control.texi
  297. @include api-io.texi
  298. @include api-regex.texi
  299. @include api-lalr.texi
  300. @include api-peg.texi
  301. @include api-evaluation.texi
  302. @include api-memory.texi
  303. @include api-modules.texi
  304. @include api-foreign.texi
  305. @include api-foreign-objects.texi
  306. @include api-smobs.texi
  307. @include api-scheduling.texi
  308. @c object orientation support here
  309. @include api-options.texi
  310. @include api-languages.texi
  311. @include api-i18n.texi
  312. @include api-debug.texi
  313. @include api-coverage.texi
  314. @node Guile Modules
  315. @chapter Guile Modules
  316. @menu
  317. * SLIB:: Using the SLIB Scheme library.
  318. * POSIX:: POSIX system calls and networking.
  319. * Web:: HTTP, the web, and all that.
  320. * getopt-long:: Command line handling.
  321. * SRFI Support:: Support for various SRFIs.
  322. * R6RS Support:: Modules defined by the R6RS.
  323. * R7RS Support:: Modules defined by the R7RS.
  324. * Pattern Matching:: Generic pattern matching constructs.
  325. * Readline Support:: Module for using the readline library.
  326. * Pretty Printing:: Nicely formatting Scheme objects for output.
  327. * Formatted Output:: The @code{format} procedure.
  328. * File Tree Walk:: Traversing the file system.
  329. * Queues:: First-in first-out queuing.
  330. * Streams:: Sequences of values.
  331. * Buffered Input:: Ports made from a reader function.
  332. * Expect:: Controlling interactive programs with Guile.
  333. * sxml-match:: Pattern matching of SXML.
  334. * The Scheme shell (scsh):: Using scsh interfaces in Guile.
  335. * Curried Definitions:: Extended @code{define} syntax.
  336. * Statprof:: An easy-to-use statistical profiler.
  337. * SXML:: Parsing, transforming, and serializing XML.
  338. * Texinfo Processing:: Munging documents written in Texinfo.
  339. @end menu
  340. @include slib.texi
  341. @include posix.texi
  342. @include web.texi
  343. @include mod-getopt-long.texi
  344. @include srfi-modules.texi
  345. @include r6rs.texi
  346. @include r7rs.texi
  347. @include match.texi
  348. @include repl-modules.texi
  349. @include misc-modules.texi
  350. @include expect.texi
  351. @c XXX: Would be nicer if it were close to the (sxml simple) documentation.
  352. @include sxml-match.texi
  353. @include scsh.texi
  354. @include curried.texi
  355. @include statprof.texi
  356. @include sxml.texi
  357. @include texinfo.texi
  358. @include goops.texi
  359. @node Guile Implementation
  360. @chapter Guile Implementation
  361. At some point, after one has been programming in Scheme for some time,
  362. another level of Scheme comes into view: its implementation. Knowledge
  363. of how Scheme can be implemented turns out to be necessary to become
  364. an expert hacker. As Peter Norvig notes in his retrospective on
  365. PAIP@footnote{PAIP is the common abbreviation for @cite{Paradigms of
  366. Artificial Intelligence Programming}, an old but still useful text on
  367. Lisp. Norvig's retrospective sums up the lessons of PAIP, and can be
  368. found at @uref{http://norvig.com/Lisp-retro.html}.}, ``The expert Lisp
  369. programmer eventually develops a good `efficiency model'.''
  370. By this Norvig means that over time, the Lisp hacker eventually
  371. develops an understanding of how much her code ``costs'' in terms of
  372. space and time.
  373. This chapter describes Guile as an implementation of Scheme: its
  374. history, how it represents and evaluates its data, and its compiler.
  375. This knowledge can help you to make that step from being one who is
  376. merely familiar with Scheme to being a real hacker.
  377. @menu
  378. * History:: A brief history of Guile.
  379. * Data Representation:: How Guile represents Scheme data.
  380. * A Virtual Machine for Guile:: How compiled procedures work.
  381. * Compiling to the Virtual Machine:: Not as hard as you might think.
  382. @end menu
  383. @include history.texi
  384. @include data-rep.texi
  385. @include vm.texi
  386. @include compiler.texi
  387. @node GNU Free Documentation License
  388. @appendix GNU Free Documentation License
  389. @include fdl.texi
  390. @include indices.texi
  391. @include scheme-indices.texi
  392. @bye