api-languages.texi 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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, 2016,
  4. @c 2017, 2018 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. In Elisp code, @code{#nil}, @code{#f}, and @code{'()} behave like
  86. @code{nil}, in the sense that they are all interpreted as @code{nil} by
  87. Elisp @code{if}, @code{cond}, @code{when}, @code{not}, @code{null}, etc.
  88. To test whether Elisp would interpret an object as @code{nil} from
  89. within Scheme code, use @code{nil?}:
  90. @deffn {Scheme Procedure} nil? obj
  91. Return @code{#t} if @var{obj} would be interpreted as @code{nil} by
  92. Emacs Lisp code, else return @code{#f}.
  93. @lisp
  94. (nil? #nil) @result{} #t
  95. (nil? #f) @result{} #t
  96. (nil? '()) @result{} #t
  97. (nil? 3) @result{} #f
  98. @end lisp
  99. @end deffn
  100. This decision to have @code{nil} as a low-level distinct value
  101. facilitates interoperability between the two languages. Guile has chosen
  102. to have Scheme deal with @code{nil} as follows:
  103. @example
  104. (boolean? #nil) @result{} #t
  105. (not #nil) @result{} #t
  106. (null? #nil) @result{} #t
  107. @end example
  108. And in C, one has:
  109. @example
  110. scm_is_bool (SCM_ELISP_NIL) @result{} 1
  111. scm_is_false (SCM_ELISP_NIL) @result{} 1
  112. scm_is_null (SCM_ELISP_NIL) @result{} 1
  113. @end example
  114. In this way, a version of @code{fold} written in Scheme can correctly
  115. fold a function written in Elisp (or in fact any other language) over a
  116. nil-terminated list, as Elisp makes. The converse holds as well; a
  117. version of @code{fold} written in Elisp can fold over a
  118. @code{'()}-terminated list, as made by Scheme.
  119. On a low level, the bit representations for @code{#f}, @code{#t},
  120. @code{nil}, and @code{'()} are made in such a way that they differ by
  121. only one bit, and so a test for, for example, @code{#f}-or-@code{nil}
  122. may be made very efficiently. See @code{libguile/boolean.h}, for more
  123. information.
  124. @subsubheading Equality
  125. Since Scheme's @code{equal?} must be transitive, and @code{'()}
  126. is not @code{equal?} to @code{#f}, to Scheme @code{nil} is not
  127. @code{equal?} to @code{#f} or @code{'()}.
  128. @example
  129. (eq? #f '()) @result{} #f
  130. (eq? #nil '()) @result{} #f
  131. (eq? #nil #f) @result{} #f
  132. (eqv? #f '()) @result{} #f
  133. (eqv? #nil '()) @result{} #f
  134. (eqv? #nil #f) @result{} #f
  135. (equal? #f '()) @result{} #f
  136. (equal? #nil '()) @result{} #f
  137. (equal? #nil #f) @result{} #f
  138. @end example
  139. However, in Elisp, @code{'()}, @code{#f}, and @code{nil} are all
  140. @code{equal} (though not @code{eq}).
  141. @example
  142. (defvar f (make-scheme-false))
  143. (defvar eol (make-scheme-null))
  144. (eq f eol) @result{} nil
  145. (eq nil eol) @result{} nil
  146. (eq nil f) @result{} nil
  147. (equal f eol) @result{} t
  148. (equal nil eol) @result{} t
  149. (equal nil f) @result{} t
  150. @end example
  151. These choices facilitate interoperability between Elisp and Scheme code,
  152. but they are not perfect. Some code that is correct standard Scheme is
  153. not correct in the presence of a second false and null value. For
  154. example:
  155. @example
  156. (define (truthiness x)
  157. (if (eq? x #f)
  158. #f
  159. #t))
  160. @end example
  161. This code seems to be meant to test a value for truth, but now that
  162. there are two false values, @code{#f} and @code{nil}, it is no longer
  163. correct.
  164. Similarly, there is the loop:
  165. @example
  166. (define (my-length l)
  167. (let lp ((l l) (len 0))
  168. (if (eq? l '())
  169. len
  170. (lp (cdr l) (1+ len)))))
  171. @end example
  172. Here, @code{my-length} will raise an error if @var{l} is a
  173. @code{nil}-terminated list.
  174. Both of these examples are correct standard Scheme, but, depending on
  175. what they really want to do, they are not correct Guile Scheme.
  176. Correctly written, they would test the @emph{properties} of falsehood or
  177. nullity, not the individual members of that set. That is to say, they
  178. should use @code{not} or @code{null?} to test for falsehood or nullity,
  179. not @code{eq?} or @code{memv} or the like.
  180. Fortunately, using @code{not} and @code{null?} is in good style, so all
  181. well-written standard Scheme programs are correct, in Guile Scheme.
  182. Here are correct versions of the above examples:
  183. @example
  184. (define (truthiness* x)
  185. (if (not x)
  186. #f
  187. #t))
  188. ;; or: (define (t* x) (not (not x)))
  189. ;; or: (define (t** x) x)
  190. (define (my-length* l)
  191. (let lp ((l l) (len 0))
  192. (if (null? l)
  193. len
  194. (lp (cdr l) (1+ len)))))
  195. @end example
  196. This problem has a mirror-image case in Elisp:
  197. @example
  198. (defun my-falsep (x)
  199. (if (eq x nil)
  200. t
  201. nil))
  202. @end example
  203. Guile can warn when compiling code that has equality comparisons with
  204. @code{#f}, @code{'()}, or @code{nil}. @xref{Compilation}, for details.
  205. @node Dynamic Binding
  206. @subsubsection Dynamic Binding
  207. In contrast to Scheme, which uses ``lexical scoping'', Emacs Lisp scopes
  208. its variables dynamically. Guile supports dynamic scoping with its
  209. ``fluids'' facility. @xref{Fluids and Dynamic States}, for more
  210. information.
  211. @node Other Elisp Features
  212. @subsubsection Other Elisp Features
  213. Buffer-local and mode-local variables should be mentioned here, along
  214. with buckybits on characters, Emacs primitive data types, the
  215. Lisp-2-ness of Elisp, and other things. Contributions to the
  216. documentation are most welcome!
  217. @node ECMAScript
  218. @subsection ECMAScript
  219. @url{http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf,ECMAScript}
  220. was not the first non-Schemey language implemented by Guile, but it was
  221. the first implemented for Guile's bytecode compiler. The goal was to
  222. support ECMAScript version 3.1, a relatively small language, but the
  223. implementor was completely irresponsible and got distracted by other
  224. things before finishing the standard library, and even some bits of the
  225. syntax. So, ECMAScript does deserve a mention in the manual, but it
  226. doesn't deserve an endorsement until its implementation is completed,
  227. perhaps by some more responsible hacker.
  228. In the meantime, the charitable user might investigate such invocations
  229. as @code{,L ecmascript} and @code{cat test-suite/tests/ecmascript.test}.
  230. @c Local Variables:
  231. @c TeX-master: "guile.texi"
  232. @c End: