tour.texi 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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, 2010, 2011,
  4. @c 2012, 2021 Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @raisesections
  7. @node Hello Guile!
  8. @section Hello Guile!
  9. This chapter presents a quick tour of all the ways that Guile can be
  10. used. There are additional examples in the @file{examples/}
  11. directory in the Guile source distribution. It also explains how best to report
  12. any problems that you find.
  13. The following examples assume that Guile has been installed in
  14. @code{/usr/local/}.
  15. @menu
  16. * Running Guile Interactively::
  17. * Running Guile Scripts::
  18. * Linking Guile into Programs::
  19. * Writing Guile Extensions::
  20. * Using the Guile Module System::
  21. * Reporting Bugs::
  22. @end menu
  23. @node Running Guile Interactively
  24. @subsection Running Guile Interactively
  25. In its simplest form, Guile acts as an interactive interpreter for the
  26. Scheme programming language, reading and evaluating Scheme expressions
  27. the user enters from the terminal. Here is a sample interaction between
  28. Guile and a user; the user's input appears after the @code{$} and
  29. @code{scheme@@(guile-user)>} prompts:
  30. @example
  31. $ guile
  32. scheme@@(guile-user)> (+ 1 2 3) ; add some numbers
  33. $1 = 6
  34. scheme@@(guile-user)> (define (factorial n) ; define a function
  35. (if (zero? n) 1 (* n (factorial (- n 1)))))
  36. scheme@@(guile-user)> (factorial 20)
  37. $2 = 2432902008176640000
  38. scheme@@(guile-user)> (getpwnam "root") ; look in /etc/passwd
  39. $3 = #("root" "x" 0 0 "root" "/root" "/bin/bash")
  40. scheme@@(guile-user)> @kbd{C-d}
  41. $
  42. @end example
  43. @node Running Guile Scripts
  44. @subsection Running Guile Scripts
  45. Like AWK, Perl, or any shell, Guile can interpret script files. A Guile
  46. script is simply a file of Scheme code with some extra information at
  47. the beginning which tells the operating system how to invoke Guile, and
  48. then tells Guile how to handle the Scheme code.
  49. Here is a trivial Guile script. @xref{Guile Scripting}, for more details.
  50. @example
  51. #!/usr/local/bin/guile -s
  52. !#
  53. (display "Hello, world!")
  54. (newline)
  55. @end example
  56. @node Linking Guile into Programs
  57. @subsection Linking Guile into Programs
  58. The Guile interpreter is available as an object library, to be linked
  59. into applications using Scheme as a configuration or extension
  60. language.
  61. Here is @file{simple-guile.c}, source code for a program that will
  62. produce a complete Guile interpreter. In addition to all usual
  63. functions provided by Guile, it will also offer the function
  64. @code{my-hostname}.
  65. @example
  66. #include <stdlib.h>
  67. #include <libguile.h>
  68. static SCM
  69. my_hostname (void)
  70. @{
  71. char *s = getenv ("HOSTNAME");
  72. if (s == NULL)
  73. return SCM_BOOL_F;
  74. else
  75. return scm_from_locale_string (s);
  76. @}
  77. static void
  78. inner_main (void *data, int argc, char **argv)
  79. @{
  80. scm_c_define_gsubr ("my-hostname", 0, 0, 0, my_hostname);
  81. scm_shell (argc, argv);
  82. @}
  83. int
  84. main (int argc, char **argv)
  85. @{
  86. scm_boot_guile (argc, argv, inner_main, 0);
  87. return 0; /* never reached */
  88. @}
  89. @end example
  90. When Guile is correctly installed on your system, the above program
  91. can be compiled and linked like this:
  92. @example
  93. $ gcc -o simple-guile simple-guile.c \
  94. `pkg-config --cflags --libs guile-@value{EFFECTIVE-VERSION}`
  95. @end example
  96. When it is run, it behaves just like the @code{guile} program except
  97. that you can also call the new @code{my-hostname} function.
  98. @example
  99. $ ./simple-guile
  100. scheme@@(guile-user)> (+ 1 2 3)
  101. $1 = 6
  102. scheme@@(guile-user)> (my-hostname)
  103. "burns"
  104. @end example
  105. @node Writing Guile Extensions
  106. @subsection Writing Guile Extensions
  107. You can link Guile into your program and make Scheme available to the
  108. users of your program. You can also link your library into Guile and
  109. make its functionality available to all users of Guile.
  110. A library that is linked into Guile is called an @dfn{extension}, but it
  111. really just is an ordinary object library.
  112. The following example shows how to write a simple extension for Guile
  113. that makes the @code{j0} function available to Scheme code.
  114. @smallexample
  115. #include <math.h>
  116. #include <libguile.h>
  117. SCM
  118. j0_wrapper (SCM x)
  119. @{
  120. return scm_from_double (j0 (scm_to_double (x)));
  121. @}
  122. void
  123. init_bessel ()
  124. @{
  125. scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
  126. @}
  127. @end smallexample
  128. This C source file needs to be compiled into a shared library. Here is
  129. how to do it on GNU/Linux:
  130. @smallexample
  131. gcc `pkg-config --cflags guile-@value{EFFECTIVE-VERSION}` \
  132. -shared -o libguile-bessel.so -fPIC bessel.c
  133. @end smallexample
  134. For creating shared libraries portably, we recommend the use of GNU
  135. Libtool (@pxref{Top, , Introduction, libtool, GNU Libtool}).
  136. A shared library can be loaded into a running Guile process with the
  137. function @code{load-extension}. The @code{j0} is then immediately
  138. available:
  139. @smallexample
  140. $ guile
  141. scheme@@(guile-user)> (load-extension "./libguile-bessel" "init_bessel")
  142. scheme@@(guile-user)> (j0 2)
  143. $1 = 0.223890779141236
  144. @end smallexample
  145. For more on how to install your extension, @pxref{Installing Site
  146. Packages}.
  147. @node Using the Guile Module System
  148. @subsection Using the Guile Module System
  149. Guile has support for dividing a program into @dfn{modules}. By using
  150. modules, you can group related code together and manage the
  151. composition of complete programs from largely independent parts.
  152. For more details on the module system beyond this introductory material,
  153. @xref{Modules}.
  154. @menu
  155. * Using Modules::
  156. * Writing new Modules::
  157. * Putting Extensions into Modules::
  158. @end menu
  159. @node Using Modules
  160. @subsubsection Using Modules
  161. Guile comes with a lot of useful modules, for example for string
  162. processing or command line parsing. Additionally, there exist many
  163. Guile modules written by other Guile hackers, but which have to be
  164. installed manually.
  165. Here is a sample interactive session that shows how to use the
  166. @code{(ice-9 popen)} module which provides the means for communicating
  167. with other processes over pipes together with the @code{(ice-9
  168. rdelim)} module that provides the function @code{read-line}.
  169. @smallexample
  170. $ guile
  171. scheme@@(guile-user)> (use-modules (ice-9 popen))
  172. scheme@@(guile-user)> (use-modules (ice-9 rdelim))
  173. scheme@@(guile-user)> (define p (open-input-pipe "ls -l"))
  174. scheme@@(guile-user)> (read-line p)
  175. $1 = "total 30"
  176. scheme@@(guile-user)> (read-line p)
  177. $2 = "drwxr-sr-x 2 mgrabmue mgrabmue 1024 Mar 29 19:57 CVS"
  178. @end smallexample
  179. @node Writing new Modules
  180. @subsubsection Writing new Modules
  181. You can create new modules using the syntactic form
  182. @code{define-module}. All definitions following this form until the
  183. next @code{define-module} are placed into the new module.
  184. One module is usually placed into one file, and that file is installed
  185. in a location where Guile can automatically find it. The following
  186. session shows a simple example.
  187. @smallexample
  188. $ cat /usr/local/share/guile/site/foo/bar.scm
  189. (define-module (foo bar)
  190. #:export (frob))
  191. (define (frob x) (* 2 x))
  192. $ guile
  193. scheme@@(guile-user)> (use-modules (foo bar))
  194. scheme@@(guile-user)> (frob 12)
  195. $1 = 24
  196. @end smallexample
  197. For more on how to install your module, @pxref{Installing Site
  198. Packages}.
  199. @node Putting Extensions into Modules
  200. @subsubsection Putting Extensions into Modules
  201. In addition to Scheme code you can also put things that are defined in
  202. C into a module.
  203. You do this by writing a small Scheme file that defines the module and
  204. call @code{load-extension} directly in the body of the module.
  205. @smallexample
  206. $ cat /usr/local/share/guile/site/math/bessel.scm
  207. (define-module (math bessel)
  208. #:export (j0))
  209. (load-extension "libguile-bessel" "init_bessel")
  210. $ file /usr/local/lib/guile/@value{EFFECTIVE-VERSION}/extensions/libguile-bessel.so
  211. @dots{} ELF 32-bit LSB shared object @dots{}
  212. $ guile
  213. scheme@@(guile-user)> (use-modules (math bessel))
  214. scheme@@(guile-user)> (j0 2)
  215. $1 = 0.223890779141236
  216. @end smallexample
  217. @xref{Foreign Extensions}, for more information.
  218. @lowersections
  219. @node Reporting Bugs
  220. @section Reporting Bugs
  221. Any problems with the installation should be reported to
  222. @email{bug-guile@@gnu.org}.
  223. If you find a bug in Guile, please report it to the Guile developers, so
  224. they can fix it. They may also be able to suggest workarounds when it
  225. is not possible for you to apply the bug-fix or install a new version of
  226. Guile yourself.
  227. Before sending in bug reports, please check with the following list that
  228. you really have found a bug.
  229. @itemize @bullet
  230. @item
  231. Whenever documentation and actual behavior differ, you have certainly
  232. found a bug, either in the documentation or in the program.
  233. @item
  234. When Guile crashes, it is a bug.
  235. @item
  236. When Guile hangs or takes forever to complete a task, it is a bug.
  237. @item
  238. When calculations produce wrong results, it is a bug.
  239. @item
  240. When Guile signals an error for valid Scheme programs, it is a bug.
  241. @item
  242. When Guile does not signal an error for invalid Scheme programs, it may
  243. be a bug, unless this is explicitly documented.
  244. @item
  245. When some part of the documentation is not clear and does not make sense
  246. to you even after re-reading the section, it is a bug.
  247. @end itemize
  248. Before reporting the bug, check whether any programs you have loaded
  249. into Guile, including your @file{.guile} file, set any variables that
  250. may affect the functioning of Guile. Also, see whether the problem
  251. happens in a freshly started Guile without loading your @file{.guile}
  252. file (start Guile with the @code{-q} switch to prevent loading the init
  253. file). If the problem does @emph{not} occur then, you must report the
  254. precise contents of any programs that you must load into Guile in order
  255. to cause the problem to occur.
  256. When you write a bug report, please make sure to include as much of the
  257. information described below in the report. If you can't figure out some
  258. of the items, it is not a problem, but the more information we get, the
  259. more likely we can diagnose and fix the bug.
  260. @itemize @bullet
  261. @item
  262. The version number of Guile. You can get this information from invoking
  263. @samp{guile --version} at your shell, or calling @code{(version)} from
  264. within Guile.
  265. @item
  266. Your machine type, as determined by the @code{config.guess} shell
  267. script. If you have a Guile checkout, this file is located in
  268. @code{build-aux}; otherwise you can fetch the latest version from
  269. @uref{http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD}.
  270. @example
  271. $ build-aux/config.guess
  272. x86_64-unknown-linux-gnu
  273. @end example
  274. @item
  275. If you installed Guile from a binary package, the version of that
  276. package. On systems that use RPM, use @code{rpm -qa | grep guile}. On systems
  277. that use DPKG, @code{dpkg -l | grep guile}.
  278. @item
  279. If you built Guile yourself, the build configuration that you used:
  280. @example
  281. $ ./config.status --config
  282. '--enable-error-on-warning' '--disable-deprecated'...
  283. @end example
  284. @item
  285. A complete description of how to reproduce the bug.
  286. If you have a Scheme program that produces the bug, please include it in
  287. the bug report. If your program is too big to include, please try to
  288. reduce your code to a minimal test case.
  289. If you can reproduce your problem at the REPL, that is best. Give a
  290. transcript of the expressions you typed at the REPL.
  291. @item
  292. A description of the incorrect behavior. For example, "The Guile
  293. process gets a fatal signal," or, "The resulting output is as follows,
  294. which I think is wrong."
  295. If the manifestation of the bug is a Guile error message, it is
  296. important to report the precise text of the error message, and a
  297. backtrace showing how the Scheme program arrived at the error. This can
  298. be done using the @code{,backtrace} command in Guile's debugger.
  299. @end itemize
  300. If your bug causes Guile to crash, additional information from a
  301. low-level debugger such as GDB might be helpful. If you have built Guile
  302. yourself, you can run Guile under GDB via the
  303. @code{meta/gdb-uninstalled-guile} script. Instead of invoking Guile as
  304. usual, invoke the wrapper script, type @code{run} to start the process,
  305. then @code{backtrace} when the crash comes. Include that backtrace in
  306. your report.
  307. @c Local Variables:
  308. @c TeX-master: "guile.texi"
  309. @c End: