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