more nil docs
[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 a user can implement other
17 languages on top of Guile. This section describes the languages that
18 have been implemented.
19
20 (For details on how to implement a language, @xref{Compiling to the
21 Virtual 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
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}.
35
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
39 @code{,language}.
40
41 For example, the following meta-command enables Emacs Lisp input:
42
43 @example
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)
47 $1 = #nil
48 @end example
49
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,
52 via @code{compile}:
53
54 @example
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)
58 $2 = #nil
59 @end example
60
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.
64
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,
69 @xref{Compilation}.
70
71 For more details on introspecting aspects of different languages,
72 @xref{Compiler Tower}.
73
74 @node Emacs Lisp
75 @subsection Emacs Lisp
76
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.
80
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.
85
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.
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
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
102 as well.
103
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}.
109
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:
113
114 @example
115 (boolean? #nil) @result{} #t
116 (not #nil) @result{} #t
117 (null? #nil) @result{} #t
118 @end example
119
120 And in C, one has:
121
122 @example
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
126 @end example
127
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.
133
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
138 information.
139
140 @subsubsection Equality
141
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{'()}.
145
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
158 However, in Elisp, @code{'()}, @code{#f}, and @code{nil} are all
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
171
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
175 example:
176
177 @example
178 (define (truthiness x)
179 (if (eq? x #f)
180 #f
181 #t))
182 @end example
183
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
186 correct.
187
188 Similarly, 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
198 Here, @code{my-length} will raise an error if @var{l} is a
199 @code{nil}-terminated list.
200
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.
207
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.
210
211 Here 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
228 This 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
237 Guile can warn when compiling code that has equality comparisons with
238 @code{#f}, @code{'()}, or @code{nil}. @xref{Compilation}, for details.
239
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
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.
258
259 In the meantime, the charitable user might investigate such invocations
260 as @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: