fold in feedback to api-languages
[bpt/guile.git] / doc / ref / api-languages.texi
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
7 @page
8 @node Other Languages
9 @section Support for Other Languages
10
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
13 Lisp and ECMAScript.
14
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.
20
21 (For details on how to implement a language, @xref{Compiling to the
22 Virtual Machine}.)
23
24 @menu
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.
28 @end menu
29
30
31 @node Using Other Languages
32 @subsection Using Other Languages
33
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}.
37
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
41 @code{,language}.
42
43 For example, the following meta-command enables Emacs Lisp input:
44
45 @example
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)
49 $1 = #nil
50 @end example
51
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,
54 via @code{compile}:
55
56 @example
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)
60 $2 = #nil
61 @end example
62
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.
66
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,
71 @xref{Compilation}.
72
73 For more details on introspecting aspects of different languages,
74 @xref{Compiler Tower}.
75
76 @node Emacs Lisp
77 @subsection Emacs Lisp
78
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.
82
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.
87
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.
91
92 @menu
93 * Nil:: A third boolean.
94 * Dynamic Binding:: Threadsafe bindings with fluids.
95 * Other Elisp Features:: Miscellany.
96 @end menu
97
98
99 @node Nil
100 @subsubsection Nil
101
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
104 as well.
105
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}.
111
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:
115
116 @example
117 (boolean? #nil) @result{} #t
118 (not #nil) @result{} #t
119 (null? #nil) @result{} #t
120 @end example
121
122 And in C, one has:
123
124 @example
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
128 @end example
129
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.
135
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
140 information.
141
142 @subsubsection Equality
143
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{'()}.
147
148 @example
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
158 @end example
159
160 However, in Elisp, @code{'()}, @code{#f}, and @code{nil} are all
161 @code{equal} (though not @code{eq}).
162
163 @example
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
172 @end example
173
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
177 example:
178
179 @example
180 (define (truthiness x)
181 (if (eq? x #f)
182 #f
183 #t))
184 @end example
185
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
188 correct.
189
190 Similarly, there is the loop:
191
192 @example
193 (define (my-length l)
194 (let lp ((l l) (len 0))
195 (if (eq? l '())
196 len
197 (lp (cdr l) (1+ len)))))
198 @end example
199
200 Here, @code{my-length} will raise an error if @var{l} is a
201 @code{nil}-terminated list.
202
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.
209
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.
212
213 Here are correct versions of the above examples:
214
215 @example
216 (define (truthiness* x)
217 (if (not x)
218 #f
219 #t))
220 ;; or: (define (t* x) (not (not x)))
221 ;; or: (define (t** x) x)
222
223 (define (my-length* l)
224 (let lp ((l l) (len 0))
225 (if (null? l)
226 len
227 (lp (cdr l) (1+ len)))))
228 @end example
229
230 This problem has a mirror-image case in Elisp:
231
232 @example
233 (deffn my-falsep (x)
234 (if (eq x nil)
235 t
236 nil))
237 @end example
238
239 Guile can warn when compiling code that has equality comparisons with
240 @code{#f}, @code{'()}, or @code{nil}. @xref{Compilation}, for details.
241
242 @node Dynamic Binding
243 @subsubsection Dynamic Binding
244
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
248 information.
249
250 @node Other Elisp Features
251 @subsubsection Other Elisp Features
252
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!
257
258 @node ECMAScript
259 @subsection ECMAScript
260
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.
270
271 In the meantime, the charitable user might investigate such invocations
272 as @code{,L ecmascript} and @code{cat test-suite/tests/ecmascript.test}.
273
274
275 @c Local Variables:
276 @c TeX-master: "guile.texi"
277 @c End: