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