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 a user can implement other
17 languages on top of Guile. This section describes the languages that
18 have been implemented.
20 (For details on how to implement a language, @xref{Compiling to the
24 * Using Other Languages:: How to use other languages.
25 * Emacs Lisp:: The dialect of Lisp used in Emacs.
26 * ECMAScript:: As seen on television.
30 @node Using Other Languages
31 @subsection Using Other Languages
33 There are currently only two ways to access other languages from within
34 Guile: at the REPL, and via @code{compile} or @code{compile-file}.
36 The REPL is Guile's command prompt (@pxref{Using Guile Interactively}).
37 The REPL has a concept of the ``current language'', which defaults to
38 Scheme. The user may change that language, via the meta-command
41 For example, the following meta-command enables Emacs Lisp input:
44 scheme@@(guile-user)> ,language elisp
45 Happy hacking with Emacs Lisp! To switch back, type `,L scheme'.
46 elisp@@(guile-user)> (eq 1 2)
50 Each language has its short name: for example, @code{elisp}, for Elisp.
51 The same short name may be used to compile source code programmatically,
55 elisp@@(guile-user)> ,L scheme
56 Happy hacking with Guile Scheme! To switch back, type `,L elisp'.
57 scheme@@(guile-user)> (compile '(eq 1 2) #:from 'elisp)
61 Granted, as the input to @code{compile} is a datum, this works best for
62 Lispy languages, which have a straightforward datum representation.
63 Other languages that need more parsing are better dealt with as strings.
65 The easiest way to deal with syntax-heavy language is with files, via
66 @code{compile-file} and friends. However it is possible to invoke a
67 language's reader on a port, and then compile the resulting expression
68 (which is a datum at that point). For more information,
71 For more details on introspecting aspects of different languages,
72 @xref{Compiler Tower}.
75 @subsection Emacs Lisp
77 Emacs Lisp (Elisp) is a dynamically-scoped Lisp dialect used in the
78 Emacs editor. @xref{top,,Overview,elisp,Emacs Lisp}, for more
79 information on Emacs Lisp.
81 We hope that eventually Guile's implementation of Elisp will be good
82 enough to replace Emacs' own implementation of Elisp. For that reason,
83 we have thought long and hard about how to support the various features
84 of Elisp in a performant and compatible manner.
86 Readers familiar with Emacs Lisp might be curious about how exactly
87 these various Elisp features are supported in Guile. The rest of this
88 section focuses on addressing these concerns of the Elisp elect.
91 * Nil:: A third boolean.
92 * Dynamic Binding:: Threadsafe bindings with fluids.
93 * Other Elisp Features:: Miscellany.
100 @code{nil} in ELisp is an amalgam of Scheme's @code{#f} and @code{'()}.
101 It is false, and it is the end-of-list; thus it is a boolean, and a list
104 Guile has chosen to support @code{nil} as a separate value, distinct
105 from @code{#f} and @code{'()}. This allows existing Scheme and Elisp
106 code to maintain their current semantics. @code{nil}, which in Elisp
107 would just be written and read as @code{nil}, in Scheme has the external
108 representation @code{#nil}.
110 This decision to have @code{nil} as a low-level distinct value
111 facilitates interoperability between the two languages. Guile has chosen
112 to have Scheme deal with @code{nil} as follows:
115 (boolean? #nil) @result{} #t
116 (not #nil) @result{} #t
117 (null? #nil) @result{} #t
123 scm_is_bool (SCM_ELISP_NIL) @result{} 1
124 scm_is_false (SCM_ELISP_NIL) @result{} 1
125 scm_is_null (SCM_ELISP_NIL) @result{} 1
128 In this way, a version of @code{fold} written in Scheme can correctly
129 fold a function written in Elisp (or in fact any other language) over a
130 nil-terminated list, as Elisp makes. The converse holds as well; a
131 version of @code{fold} written in Elisp can fold over a
132 @code{'()}-terminated list, as made by Scheme.
134 On a low level, the bit representations for @code{#f}, @code{#t},
135 @code{nil}, and @code{'()} are made in such a way that the differ by
136 only one bit, and so a test for, for example, @code{#f}-or-@code{nil}
137 may be made very efficiently. See @code{libguile/boolean.h}, for more
140 @subsubsection Equality
142 Since Scheme's @code{equal?} must be transitive, and @code{'()}
143 is not @code{equal?} to @code{#f}, to Scheme @code{nil} is not
144 @code{equal?} to @code{#f} or @code{'()}.
147 (eq? #f '()) @result{} #f
148 (eq? #nil '()) @result{} #f
149 (eq? #nil #f) @result{} #f
150 (eqv? #f '()) @result{} #f
151 (eqv? #nil '()) @result{} #f
152 (eqv? #nil #f) @result{} #f
153 (equal? #f '()) @result{} #f
154 (equal? #nil '()) @result{} #f
155 (equal? #nil #f) @result{} #f
158 However, in Elisp, @code{'()}, @code{#f}, and @code{nil} are all
159 @code{equal} (though not @code{eq}).
162 (defvar f (make-scheme-false))
163 (defvar eol (make-scheme-null))
164 (eq f eol) @result{} nil
165 (eq nil eol) @result{} nil
166 (eq nil f) @result{} nil
167 (equal f eol) @result{} t
168 (equal nil eol) @result{} t
169 (equal nil f) @result{} t
172 These choices facilitate interoperability between Elisp and Scheme code,
173 but they are not perfect. Some code that is correct standard Scheme is
174 not correct in the presence of a second false and null value. For
178 (define (truthiness x)
184 This code seems to be meant to test a value for truth, but now that
185 there are two false values, @code{#f} and @code{nil}, it is no longer
188 Similarly, there is the loop:
191 (define (my-length l)
192 (let lp ((l l) (len 0))
195 (lp (cdr l) (1+ len)))))
198 Here, @code{my-length} will raise an error if @var{l} is a
199 @code{nil}-terminated list.
201 Both of these examples are correct standard Scheme, but, depending on
202 what they really want to do, they are not correct Guile Scheme.
203 Correctly written, they would test the @emph{properties} of falsehood or
204 nullity, not the individual members of that set. That is to say, they
205 should use @code{not} or @code{null?} to test for falsehood or nullity,
206 not @code{eq?} or @code{memv} or the like.
208 Fortunately, using @code{not} and @code{null?} is in good style, so all
209 well-written standard Scheme programs are correct, in Guile Scheme.
211 Here are correct versions of the above examples:
214 (define (truthiness* x)
218 ;; or: (define (t* x) (not (not x)))
219 ;; or: (define (t** x) x)
221 (define (my-length* l)
222 (let lp ((l l) (len 0))
225 (lp (cdr l) (1+ len)))))
228 This problem has a mirror-image case in Elisp:
237 Guile can warn when compiling code that has equality comparisons with
238 @code{#f}, @code{'()}, or @code{nil}. @xref{Compilation}, for details.
240 @node Dynamic Binding
241 @subsubsection Dynamic Binding
243 @node Other Elisp Features
244 @subsubsection Other Elisp Features
248 @subsection ECMAScript
250 ECMAScript was not the first non-Schemey language implemented by Guile,
251 but it was the first implemented for Guile's bytecode compiler. The goal
252 was to support ECMAScript version 3.1, a relatively small language, but
253 the implementor was completely irresponsible and got distracted by other
254 things before finishing the standard library, and even some bits of the
255 syntax. So, ECMAScript does deserve a mention in the manual, but it
256 doesn't deserve an endorsement until its implementation is completed,
257 perhaps by some more responsible hacker.
259 In the meantime, the charitable user might investigate such invocations
260 as @code{,L ecmascript} and @code{cat test-suite/tests/ecmascript.test}.
264 @c TeX-master: "guile.texi"