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