intro.texi 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Introduction to Guile
  7. @chapter Introduction to Guile
  8. @menu
  9. * What is Guile?::
  10. * Obtaining and Installing Guile::
  11. * Whirlwind Tour::
  12. * Discouraged and Deprecated::
  13. * Reporting Bugs::
  14. @end menu
  15. @node What is Guile?
  16. @section What is Guile?
  17. Guile is an interpreter for the Scheme programming language, packaged
  18. for use in a wide variety of environments. Guile implements Scheme as
  19. described in the
  20. @tex
  21. Revised$^5$
  22. @end tex
  23. @ifnottex
  24. Revised^5
  25. @end ifnottex
  26. Report on the Algorithmic Language Scheme (usually known as @acronym{R5RS}),
  27. providing clean and general data and control structures. Guile goes
  28. beyond the rather austere language presented in @acronym{R5RS}, extending it with
  29. a module system, full access to @acronym{POSIX} system calls, networking support,
  30. multiple threads, dynamic linking, a foreign function call interface,
  31. powerful string processing, and many other features needed for
  32. programming in the real world.
  33. Like a shell, Guile can run interactively, reading expressions from the
  34. user, evaluating them, and displaying the results, or as a script
  35. interpreter, reading and executing Scheme code from a file. However,
  36. Guile is also packaged as an object library, allowing other applications
  37. to easily incorporate a complete Scheme interpreter. An application can
  38. then use Guile as an extension language, a clean and powerful configuration
  39. language, or as multi-purpose ``glue'', connecting primitives provided
  40. by the application. It is easy to call Scheme code from C code and vice
  41. versa, giving the application designer full control of how and when to
  42. invoke the interpreter. Applications can add new functions, data types,
  43. control structures, and even syntax to Guile, creating a domain-specific
  44. language tailored to the task at hand, but based on a robust language
  45. design.
  46. Guile's module system allows one to break up a large program into
  47. manageable sections with well-defined interfaces between them.
  48. Modules may contain a mixture of interpreted and compiled code; Guile
  49. can use either static or dynamic linking to incorporate compiled code.
  50. Modules also encourage developers to package up useful collections of
  51. routines for general distribution; as of this writing, one can find
  52. Emacs interfaces, database access routines, compilers, @acronym{GUI}
  53. toolkit interfaces, and @acronym{HTTP} client functions, among others.
  54. In the future, we hope to expand Guile to support other languages like
  55. Tcl and Perl by translating them to Scheme code. This means that users
  56. can program applications which use Guile in the language of their
  57. choice, rather than having the tastes of the application's author
  58. imposed on them.
  59. @node Obtaining and Installing Guile
  60. @section Obtaining and Installing Guile
  61. Guile can be obtained from the main GNU archive site
  62. @url{ftp://ftp.gnu.org} or any of its mirrors. The file will be named
  63. guile-version.tar.gz. The current version is @value{VERSION}, so the
  64. file you should grab is:
  65. @url{ftp://ftp.gnu.org/pub/gnu/guile-@value{VERSION}.tar.gz}
  66. To unbundle Guile use the instruction
  67. @example
  68. zcat guile-@value{VERSION}.tar.gz | tar xvf -
  69. @end example
  70. which will create a directory called @file{guile-@value{VERSION}} with
  71. all the sources. You can look at the file @file{INSTALL} for detailed
  72. instructions on how to build and install Guile, but you should be able
  73. to just do
  74. @example
  75. cd guile-@value{VERSION}
  76. ./configure
  77. make
  78. make install
  79. @end example
  80. This will install the Guile executable @file{guile}, the Guile library
  81. @file{-lguile} and various associated header files and support
  82. libraries. It will also install the Guile tutorial and reference
  83. manual.
  84. @c [[include instructions for getting R5RS]]
  85. Since this manual frequently refers to the Scheme ``standard'', also
  86. known as R5RS, or the
  87. @iftex
  88. ``Revised$^5$ Report on the Algorithmic Language Scheme'',
  89. @end iftex
  90. @ifnottex
  91. ``Revised^5 Report on the Algorithmic Language Scheme'',
  92. @end ifnottex
  93. we have included the report in the Guile distribution;
  94. @xref{Top, , Introduction, r5rs, Revised(5) Report on the Algorithmic
  95. Language Scheme}.
  96. This will also be installed in your info directory.
  97. @node Whirlwind Tour
  98. @section A Whirlwind Tour
  99. This chapter presents a quick tour of all the ways that Guile can be
  100. used. There are additional examples in the @file{examples/}
  101. directory in the Guile source distribution.
  102. The following examples assume that Guile has been installed in
  103. @code{/usr/local/}.
  104. @menu
  105. * Running Guile Interactively::
  106. * Running Guile Scripts::
  107. * Linking Guile into Programs::
  108. * Writing Guile Extensions::
  109. * Using the Guile Module System::
  110. @end menu
  111. @node Running Guile Interactively
  112. @subsection Running Guile Interactively
  113. In its simplest form, Guile acts as an interactive interpreter for the
  114. Scheme programming language, reading and evaluating Scheme expressions
  115. the user enters from the terminal. Here is a sample interaction between
  116. Guile and a user; the user's input appears after the @code{$} and
  117. @code{guile>} prompts:
  118. @example
  119. $ guile
  120. guile> (+ 1 2 3) ; add some numbers
  121. 6
  122. guile> (define (factorial n) ; define a function
  123. (if (zero? n) 1 (* n (factorial (- n 1)))))
  124. guile> (factorial 20)
  125. 2432902008176640000
  126. guile> (getpwnam "jimb") ; find my entry in /etc/passwd
  127. #("jimb" ".0krIpK2VqNbU" 4008 10 "Jim Blandy" "/u/jimb"
  128. "/usr/local/bin/bash")
  129. guile> @kbd{C-d}
  130. $
  131. @end example
  132. @node Running Guile Scripts
  133. @subsection Running Guile Scripts
  134. Like AWK, Perl, or any shell, Guile can interpret script files. A Guile
  135. script is simply a file of Scheme code with some extra information at
  136. the beginning which tells the operating system how to invoke Guile, and
  137. then tells Guile how to handle the Scheme code.
  138. Here is a trivial Guile script, for more details @xref{Guile Scripting}.
  139. @example
  140. #!/usr/local/bin/guile -s
  141. !#
  142. (display "Hello, world!")
  143. (newline)
  144. @end example
  145. @node Linking Guile into Programs
  146. @subsection Linking Guile into Programs
  147. The Guile interpreter is available as an object library, to be linked
  148. into applications using Scheme as a configuration or extension
  149. language.
  150. Here is @file{simple-guile.c}, source code for a program that will
  151. produce a complete Guile interpreter. In addition to all usual
  152. functions provided by Guile, it will also offer the function
  153. @code{my-hostname}.
  154. @example
  155. #include <stdlib.h>
  156. #include <libguile.h>
  157. static SCM
  158. my_hostname (void)
  159. @{
  160. char *s = getenv ("HOSTNAME");
  161. if (s == NULL)
  162. return SCM_BOOL_F;
  163. else
  164. return scm_from_locale_string (s);
  165. @}
  166. static void
  167. inner_main (void *data, int argc, char **argv)
  168. @{
  169. scm_c_define_gsubr ("my-hostname", 0, 0, 0, my_hostname);
  170. scm_shell (argc, argv);
  171. @}
  172. int
  173. main (int argc, char **argv)
  174. @{
  175. scm_boot_guile (argc, argv, inner_main, 0);
  176. return 0; /* never reached */
  177. @}
  178. @end example
  179. When Guile is correctly installed on your system, the above program
  180. can be compiled and linked like this:
  181. @example
  182. $ gcc -o simple-guile simple-guile.c -lguile
  183. @end example
  184. When it is run, it behaves just like the @code{guile} program except
  185. that you can also call the new @code{my-hostname} function.
  186. @example
  187. $ ./simple-guile
  188. guile> (+ 1 2 3)
  189. 6
  190. guile> (my-hostname)
  191. "burns"
  192. @end example
  193. @node Writing Guile Extensions
  194. @subsection Writing Guile Extensions
  195. You can link Guile into your program and make Scheme available to the
  196. users of your program. You can also link your library into Guile and
  197. make its functionality available to all users of Guile.
  198. A library that is linked into Guile is called an @dfn{extensions}, but
  199. it really just is an ordinary object library.
  200. The following example shows how to write a simple extension for Guile
  201. that makes the @code{j0} function available to Scheme code.
  202. @smallexample
  203. #include <math.h>
  204. #include <libguile.h>
  205. SCM
  206. j0_wrapper (SCM x)
  207. @{
  208. return scm_make_real (j0 (scm_num2dbl (x, "j0")));
  209. @}
  210. void
  211. init_bessel ()
  212. @{
  213. scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
  214. @}
  215. @end smallexample
  216. This C source file needs to be compiled into a shared library. Here is
  217. how to do it on GNU/Linux:
  218. @smallexample
  219. gcc -shared -o libguile-bessel.so -fPIC bessel.c
  220. @end smallexample
  221. For creating shared libraries portably, we recommend the use of GNU
  222. Libtool (@pxref{Top, , Introduction, libtool, GNU Libtool}).
  223. A shared library can be loaded into a running Guile process with the
  224. function @code{load-extension}. The @code{j0} is then immediately
  225. available:
  226. @smallexample
  227. $ guile
  228. guile> (load-extension "./libguile-bessel" "init_bessel")
  229. guile> (j0 2)
  230. 0.223890779141236
  231. @end smallexample
  232. @node Using the Guile Module System
  233. @subsection Using the Guile Module System
  234. Guile has support for dividing a program into @dfn{modules}. By using
  235. modules, you can group related code together and manage the
  236. composition of complete programs from largely independent parts.
  237. (Although the module system implementation is in flux, feel free to use it
  238. anyway. Guile will provide reasonable backwards compatibility.)
  239. Details on the module system beyond this introductory material can be found in
  240. @xref{Modules}.
  241. @menu
  242. * Using Modules::
  243. * Writing new Modules::
  244. * Putting Extensions into Modules::
  245. @end menu
  246. @node Using Modules
  247. @subsubsection Using Modules
  248. Guile comes with a lot of useful modules, for example for string
  249. processing or command line parsing. Additionally, there exist many
  250. Guile modules written by other Guile hackers, but which have to be
  251. installed manually.
  252. Here is a sample interactive session that shows how to use the
  253. @code{(ice-9 popen)} module which provides the means for communicating
  254. with other processes over pipes together with the @code{(ice-9
  255. rdelim)} module that provides the function @code{read-line}.
  256. @smallexample
  257. $ guile
  258. guile> (use-modules (ice-9 popen))
  259. guile> (use-modules (ice-9 rdelim))
  260. guile> (define p (open-input-pipe "ls -l"))
  261. guile> (read-line p)
  262. "total 30"
  263. guile> (read-line p)
  264. "drwxr-sr-x 2 mgrabmue mgrabmue 1024 Mar 29 19:57 CVS"
  265. @end smallexample
  266. @node Writing new Modules
  267. @subsubsection Writing new Modules
  268. You can create new modules using the syntactic form
  269. @code{define-module}. All definitions following this form until the
  270. next @code{define-module} are placed into the new module.
  271. One module is usually placed into one file, and that file is installed
  272. in a location where Guile can automatically find it. The following
  273. session shows a simple example.
  274. @smallexample
  275. $ cat /usr/local/share/guile/foo/bar.scm
  276. (define-module (foo bar))
  277. (export frob)
  278. (define (frob x) (* 2 x))
  279. $ guile
  280. guile> (use-modules (foo bar))
  281. guile> (frob 12)
  282. 24
  283. @end smallexample
  284. @node Putting Extensions into Modules
  285. @subsubsection Putting Extensions into Modules
  286. In addition to Scheme code you can also put things that are defined in
  287. C into a module.
  288. You do this by writing a small Scheme file that defines the module and
  289. call @code{load-extension} directly in the body of the module.
  290. @smallexample
  291. $ cat /usr/local/share/guile/math/bessel.scm
  292. (define-module (math bessel))
  293. (export j0)
  294. (load-extension "libguile-bessel" "init_bessel")
  295. $ file /usr/local/lib/libguile-bessel.so
  296. @dots{} ELF 32-bit LSB shared object @dots{}
  297. $ guile
  298. guile> (use-modules (math bessel))
  299. guile> (j0 2)
  300. 0.223890779141236
  301. @end smallexample
  302. There is also a way to manipulate the module system from C but only
  303. Scheme files can be autoloaded. Thus, we recommend that you define
  304. your modules in Scheme.
  305. @node Discouraged and Deprecated
  306. @section Discouraged and Deprecated
  307. From time to time functions and other features of Guile become
  308. obsolete. Guile has some mechanisms in place that can help you cope
  309. with this.
  310. Guile has two levels of obsoleteness: things can be @emph{deprecated},
  311. meaning that their use is considered harmful and should be avoided,
  312. even in old code; or they can be merely @emph{discouraged}, meaning
  313. that they are fine in and of themselves, but that there are better
  314. alternatives that should be used in new code.
  315. When you use a feature that is deprecated, you will likely get a
  316. warning message at run-time. Also, deprecated features are not ready
  317. for production use: they might be very slow. When something is merely
  318. discouraged, it performs normally and you wont get any messages at
  319. run-time.
  320. The primary source for information about just what things are
  321. discouraged or deprecated in a given release is the file
  322. @file{NEWS}. That file also documents what you should use instead
  323. of the obsoleted things.
  324. The file @file{README} contains instructions on how to control the
  325. inclusion or removal of the deprecated and/or discouraged features
  326. from the public API of Guile, and how to control the warning messages
  327. for deprecated features.
  328. The idea behind those mechanisms is that normally all deprecated and
  329. discouraged features are available, but that you can omit them on
  330. purpose to check whether your code still relies on them.
  331. @node Reporting Bugs
  332. @section Reporting Bugs
  333. Any problems with the installation should be reported to
  334. @email{bug-guile@@gnu.org}.
  335. Whenever you have found a bug in Guile you are encouraged to report it
  336. to the Guile developers, so they can fix it. They may also be able to
  337. suggest workarounds when it is not possible for you to apply the bug-fix
  338. or install a new version of Guile yourself.
  339. Before sending in bug reports, please check with the following list that
  340. you really have found a bug.
  341. @itemize @bullet
  342. @item
  343. Whenever documentation and actual behavior differ, you have certainly
  344. found a bug, either in the documentation or in the program.
  345. @item
  346. When Guile crashes, it is a bug.
  347. @item
  348. When Guile hangs or takes forever to complete a task, it is a bug.
  349. @item
  350. When calculations produce wrong results, it is a bug.
  351. @item
  352. When Guile signals an error for valid Scheme programs, it is a bug.
  353. @item
  354. When Guile does not signal an error for invalid Scheme programs, it may
  355. be a bug, unless this is explicitly documented.
  356. @item
  357. When some part of the documentation is not clear and does not make sense
  358. to you even after re-reading the section, it is a bug.
  359. @end itemize
  360. When you write a bug report, please make sure to include as much of the
  361. information described below in the report. If you can't figure out some
  362. of the items, it is not a problem, but the more information we get, the
  363. more likely we can diagnose and fix the bug.
  364. @itemize @bullet
  365. @item
  366. The version number of Guile. Without this, we won't know whether there
  367. is any point in looking for the bug in the current version of Guile.
  368. You can get the version number by invoking the command
  369. @example
  370. $ guile --version
  371. Guile 1.4.1
  372. Copyright (c) 1995, 1996, 1997, 2000, 2006 Free Software Foundation
  373. Guile may be distributed under the terms of the GNU General Public License;
  374. certain other uses are permitted as well. For details, see the file
  375. `COPYING', which is included in the Guile distribution.
  376. There is no warranty, to the extent permitted by law.
  377. @end example
  378. @item
  379. The type of machine you are using, and the operating system name and
  380. version number. On GNU systems, you can get it with @file{uname}.
  381. @example
  382. $ uname -a
  383. Linux tortoise 2.2.17 #1 Thu Dec 21 17:29:05 CET 2000 i586 unknown
  384. @end example
  385. @item
  386. The operands given to the @file{configure} command when Guile was
  387. installed. It's often useful to augment this with the output of the
  388. command @code{guile-config info}.
  389. @item
  390. A complete list of any modifications you have made to the Guile source.
  391. (We may not have time to investigate the bug unless it happens in an
  392. unmodified Guile. But if you've made modifications and you don't tell
  393. us, you are sending us on a wild goose chase.)
  394. Be precise about these changes. A description in English is not
  395. enough---send a context diff for them.
  396. Adding files of your own, or porting to another machine, is a
  397. modification of the source.
  398. @item
  399. Details of any other deviations from the standard procedure for
  400. installing Guile.
  401. @item
  402. The complete text of any source files needed to reproduce the bug.
  403. If you can tell us a way to cause the problem without loading any source
  404. files, please do so. This makes it much easier to debug. If you do
  405. need files, make sure you arrange for us to see their exact contents.
  406. @item
  407. The precise Guile invocation command line we need to type to reproduce
  408. the bug.
  409. @item
  410. A description of what behavior you observe that you believe is
  411. incorrect. For example, "The Guile process gets a fatal signal," or,
  412. "The resulting output is as follows, which I think is wrong."
  413. Of course, if the bug is that Guile gets a fatal signal, then one can't
  414. miss it. But if the bug is incorrect results, the maintainer might fail
  415. to notice what is wrong. Why leave it to chance?
  416. If the manifestation of the bug is a Guile error message, it is
  417. important to report the precise text of the error message, and a
  418. backtrace showing how the Scheme program arrived at the error.
  419. This can be done using the procedure @code{backtrace} in the REPL.
  420. @item
  421. Check whether any programs you have loaded into Guile, including your
  422. @file{.guile} file, set any variables that may affect the functioning of
  423. Guile. Also, see whether the problem happens in a freshly started Guile
  424. without loading your @file{.guile} file (start Guile with the @code{-q}
  425. switch to prevent loading the init file). If the problem does
  426. @emph{not} occur then, you must report the precise contents of any
  427. programs that you must load into Guile in order to cause the problem to
  428. occur.
  429. @item
  430. If the problem does depend on an init file or other Scheme programs that
  431. are not part of the standard Guile distribution, then you should make
  432. sure it is not a bug in those programs by complaining to their
  433. maintainers first. After they verify that they are using Guile in a way
  434. that is supposed to work, they should report the bug.
  435. @item
  436. If you wish to mention something in the Guile source, show the line of
  437. code with a few lines of context. Don't just give a line number.
  438. The line numbers in the development sources might not match those in your
  439. sources. It would take extra work for the maintainers to determine what
  440. code is in your version at a given line number, and we could not be
  441. certain.
  442. @item
  443. Additional information from a C debugger such as GDB might enable
  444. someone to find a problem on a machine which he does not have available.
  445. If you don't know how to use GDB, please read the GDB manual---it is not
  446. very long, and using GDB is easy. You can find the GDB distribution,
  447. including the GDB manual in online form, in most of the same places you
  448. can find the Guile distribution. To run Guile under GDB, you should
  449. switch to the @file{libguile} subdirectory in which Guile was compiled, then
  450. do @code{gdb guile} or @code{gdb .libs/guile} (if using GNU Libtool).
  451. However, you need to think when you collect the additional information
  452. if you want it to show what causes the bug.
  453. For example, many people send just a backtrace, but that is not very
  454. useful by itself. A simple backtrace with arguments often conveys
  455. little about what is happening inside Guile, because most of the
  456. arguments listed in the backtrace are pointers to Scheme objects. The
  457. numeric values of these pointers have no significance whatever; all that
  458. matters is the contents of the objects they point to (and most of the
  459. contents are themselves pointers).
  460. @end itemize
  461. @c Local Variables:
  462. @c TeX-master: "guile.texi"
  463. @c End: