api-languages.texi 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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, 2010
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Other Languages
  7. @section Support for Other Languages
  8. In addition to Scheme, a user may write a Guile program in an increasing
  9. number of other languages. Currently supported languages include Emacs
  10. Lisp and ECMAScript.
  11. Guile is still fundamentally a Scheme, but it tries to support a wide
  12. variety of language building-blocks, so that other languages can be
  13. implemented on top of Guile. This allows users to write or extend
  14. applications in languages other than Scheme, too. This section describes
  15. the languages that have been implemented.
  16. (For details on how to implement a language, @xref{Compiling to the
  17. Virtual Machine}.)
  18. @menu
  19. * Using Other Languages:: How to use other languages.
  20. * Emacs Lisp:: The dialect of Lisp used in Emacs.
  21. * ECMAScript:: As seen on television.
  22. @end menu
  23. @node Using Other Languages
  24. @subsection Using Other Languages
  25. There are currently only two ways to access other languages from within
  26. Guile: at the REPL, and programmatically, via @code{compile},
  27. @code{read-and-compile}, and @code{compile-file}.
  28. The REPL is Guile's command prompt (@pxref{Using Guile Interactively}).
  29. The REPL has a concept of the ``current language'', which defaults to
  30. Scheme. The user may change that language, via the meta-command
  31. @code{,language}.
  32. For example, the following meta-command enables Emacs Lisp input:
  33. @example
  34. scheme@@(guile-user)> ,language elisp
  35. Happy hacking with Emacs Lisp! To switch back, type `,L scheme'.
  36. elisp@@(guile-user)> (eq 1 2)
  37. $1 = #nil
  38. @end example
  39. Each language has its short name: for example, @code{elisp}, for Elisp.
  40. The same short name may be used to compile source code programmatically,
  41. via @code{compile}:
  42. @example
  43. elisp@@(guile-user)> ,L scheme
  44. Happy hacking with Guile Scheme! To switch back, type `,L elisp'.
  45. scheme@@(guile-user)> (compile '(eq 1 2) #:from 'elisp)
  46. $2 = #nil
  47. @end example
  48. Granted, as the input to @code{compile} is a datum, this works best for
  49. Lispy languages, which have a straightforward datum representation.
  50. Other languages that need more parsing are better dealt with as strings.
  51. The easiest way to deal with syntax-heavy language is with files, via
  52. @code{compile-file} and friends. However it is possible to invoke a
  53. language's reader on a port, and then compile the resulting expression
  54. (which is a datum at that point). For more information,
  55. @xref{Compilation}.
  56. For more details on introspecting aspects of different languages,
  57. @xref{Compiler Tower}.
  58. @node Emacs Lisp
  59. @subsection Emacs Lisp
  60. Emacs Lisp (Elisp) is a dynamically-scoped Lisp dialect used in the
  61. Emacs editor. @xref{top,,Overview,elisp,Emacs Lisp}, for more
  62. information on Emacs Lisp.
  63. We hope that eventually Guile's implementation of Elisp will be good
  64. enough to replace Emacs' own implementation of Elisp. For that reason,
  65. we have thought long and hard about how to support the various features
  66. of Elisp in a performant and compatible manner.
  67. Readers familiar with Emacs Lisp might be curious about how exactly
  68. these various Elisp features are supported in Guile. The rest of this
  69. section focuses on addressing these concerns of the Elisp elect.
  70. @menu
  71. * Nil:: A third boolean.
  72. * Dynamic Binding:: Threadsafe bindings with fluids.
  73. * Other Elisp Features:: Miscellany.
  74. @end menu
  75. @node Nil
  76. @subsubsection Nil
  77. @code{nil} in ELisp is an amalgam of Scheme's @code{#f} and @code{'()}.
  78. It is false, and it is the end-of-list; thus it is a boolean, and a list
  79. as well.
  80. Guile has chosen to support @code{nil} as a separate value, distinct
  81. from @code{#f} and @code{'()}. This allows existing Scheme and Elisp
  82. code to maintain their current semantics. @code{nil}, which in Elisp
  83. would just be written and read as @code{nil}, in Scheme has the external
  84. representation @code{#nil}.
  85. This decision to have @code{nil} as a low-level distinct value
  86. facilitates interoperability between the two languages. Guile has chosen
  87. to have Scheme deal with @code{nil} as follows:
  88. @example
  89. (boolean? #nil) @result{} #t
  90. (not #nil) @result{} #t
  91. (null? #nil) @result{} #t
  92. @end example
  93. And in C, one has:
  94. @example
  95. scm_is_bool (SCM_ELISP_NIL) @result{} 1
  96. scm_is_false (SCM_ELISP_NIL) @result{} 1
  97. scm_is_null (SCM_ELISP_NIL) @result{} 1
  98. @end example
  99. In this way, a version of @code{fold} written in Scheme can correctly
  100. fold a function written in Elisp (or in fact any other language) over a
  101. nil-terminated list, as Elisp makes. The converse holds as well; a
  102. version of @code{fold} written in Elisp can fold over a
  103. @code{'()}-terminated list, as made by Scheme.
  104. On a low level, the bit representations for @code{#f}, @code{#t},
  105. @code{nil}, and @code{'()} are made in such a way that they differ by
  106. only one bit, and so a test for, for example, @code{#f}-or-@code{nil}
  107. may be made very efficiently. See @code{libguile/boolean.h}, for more
  108. information.
  109. @subsubheading Equality
  110. Since Scheme's @code{equal?} must be transitive, and @code{'()}
  111. is not @code{equal?} to @code{#f}, to Scheme @code{nil} is not
  112. @code{equal?} to @code{#f} or @code{'()}.
  113. @example
  114. (eq? #f '()) @result{} #f
  115. (eq? #nil '()) @result{} #f
  116. (eq? #nil #f) @result{} #f
  117. (eqv? #f '()) @result{} #f
  118. (eqv? #nil '()) @result{} #f
  119. (eqv? #nil #f) @result{} #f
  120. (equal? #f '()) @result{} #f
  121. (equal? #nil '()) @result{} #f
  122. (equal? #nil #f) @result{} #f
  123. @end example
  124. However, in Elisp, @code{'()}, @code{#f}, and @code{nil} are all
  125. @code{equal} (though not @code{eq}).
  126. @example
  127. (defvar f (make-scheme-false))
  128. (defvar eol (make-scheme-null))
  129. (eq f eol) @result{} nil
  130. (eq nil eol) @result{} nil
  131. (eq nil f) @result{} nil
  132. (equal f eol) @result{} t
  133. (equal nil eol) @result{} t
  134. (equal nil f) @result{} t
  135. @end example
  136. These choices facilitate interoperability between Elisp and Scheme code,
  137. but they are not perfect. Some code that is correct standard Scheme is
  138. not correct in the presence of a second false and null value. For
  139. example:
  140. @example
  141. (define (truthiness x)
  142. (if (eq? x #f)
  143. #f
  144. #t))
  145. @end example
  146. This code seems to be meant to test a value for truth, but now that
  147. there are two false values, @code{#f} and @code{nil}, it is no longer
  148. correct.
  149. Similarly, there is the loop:
  150. @example
  151. (define (my-length l)
  152. (let lp ((l l) (len 0))
  153. (if (eq? l '())
  154. len
  155. (lp (cdr l) (1+ len)))))
  156. @end example
  157. Here, @code{my-length} will raise an error if @var{l} is a
  158. @code{nil}-terminated list.
  159. Both of these examples are correct standard Scheme, but, depending on
  160. what they really want to do, they are not correct Guile Scheme.
  161. Correctly written, they would test the @emph{properties} of falsehood or
  162. nullity, not the individual members of that set. That is to say, they
  163. should use @code{not} or @code{null?} to test for falsehood or nullity,
  164. not @code{eq?} or @code{memv} or the like.
  165. Fortunately, using @code{not} and @code{null?} is in good style, so all
  166. well-written standard Scheme programs are correct, in Guile Scheme.
  167. Here are correct versions of the above examples:
  168. @example
  169. (define (truthiness* x)
  170. (if (not x)
  171. #f
  172. #t))
  173. ;; or: (define (t* x) (not (not x)))
  174. ;; or: (define (t** x) x)
  175. (define (my-length* l)
  176. (let lp ((l l) (len 0))
  177. (if (null? l)
  178. len
  179. (lp (cdr l) (1+ len)))))
  180. @end example
  181. This problem has a mirror-image case in Elisp:
  182. @example
  183. (defun my-falsep (x)
  184. (if (eq x nil)
  185. t
  186. nil))
  187. @end example
  188. Guile can warn when compiling code that has equality comparisons with
  189. @code{#f}, @code{'()}, or @code{nil}. @xref{Compilation}, for details.
  190. @node Dynamic Binding
  191. @subsubsection Dynamic Binding
  192. In contrast to Scheme, which uses ``lexical scoping'', Emacs Lisp scopes
  193. its variables dynamically. Guile supports dynamic scoping with its
  194. ``fluids'' facility. @xref{Fluids and Dynamic States}, for more
  195. information.
  196. @node Other Elisp Features
  197. @subsubsection Other Elisp Features
  198. Buffer-local and mode-local variables should be mentioned here, along
  199. with buckybits on characters, Emacs primitive data types, the
  200. Lisp-2-ness of Elisp, and other things. Contributions to the
  201. documentation are most welcome!
  202. @node ECMAScript
  203. @subsection ECMAScript
  204. @url{http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf,ECMAScript}
  205. was not the first non-Schemey language implemented by Guile, but it was
  206. the first implemented for Guile's bytecode compiler. The goal was to
  207. support ECMAScript version 3.1, a relatively small language, but the
  208. implementor was completely irresponsible and got distracted by other
  209. things before finishing the standard library, and even some bits of the
  210. syntax. So, ECMAScript does deserve a mention in the manual, but it
  211. doesn't deserve an endorsement until its implementation is completed,
  212. perhaps by some more responsible hacker.
  213. In the meantime, the charitable user might investigate such invocations
  214. as @code{,L ecmascript} and @code{cat test-suite/tests/ecmascript.test}.
  215. @c Local Variables:
  216. @c TeX-master: "guile.texi"
  217. @c End: