Commit | Line | Data |
---|---|---|
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 | ||
10 | In addition to Scheme, a user may write a Guile program in an increasing | |
11 | number of other languages. Currently supported languages include Emacs | |
12 | Lisp and ECMAScript. | |
13 | ||
14 | Guile is still fundamentally a Scheme, but it tries to support a wide | |
4bd4b6e6 AW |
15 | variety of language building-blocks, so that other languages can be |
16 | implemented on top of Guile. This allows users to write or extend | |
17 | applications in languages other than Scheme, too. This section describes | |
18 | the languages that have been implemented. | |
e6709db6 AW |
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 | |
4bd4b6e6 AW |
34 | Guile: at the REPL, and programmatically, via @code{compile}, |
35 | @code{read-and-compile}, and @code{compile-file}. | |
e6709db6 AW |
36 | |
37 | The REPL is Guile's command prompt (@pxref{Using Guile Interactively}). | |
38 | The REPL has a concept of the ``current language'', which defaults to | |
39 | Scheme. The user may change that language, via the meta-command | |
40 | @code{,language}. | |
41 | ||
42 | For example, the following meta-command enables Emacs Lisp input: | |
43 | ||
44 | @example | |
45 | scheme@@(guile-user)> ,language elisp | |
46 | Happy hacking with Emacs Lisp! To switch back, type `,L scheme'. | |
47 | elisp@@(guile-user)> (eq 1 2) | |
48 | $1 = #nil | |
49 | @end example | |
50 | ||
51 | Each language has its short name: for example, @code{elisp}, for Elisp. | |
52 | The same short name may be used to compile source code programmatically, | |
53 | via @code{compile}: | |
54 | ||
55 | @example | |
56 | elisp@@(guile-user)> ,L scheme | |
57 | Happy hacking with Guile Scheme! To switch back, type `,L elisp'. | |
58 | scheme@@(guile-user)> (compile '(eq 1 2) #:from 'elisp) | |
59 | $2 = #nil | |
60 | @end example | |
61 | ||
62 | Granted, as the input to @code{compile} is a datum, this works best for | |
63 | Lispy languages, which have a straightforward datum representation. | |
64 | Other languages that need more parsing are better dealt with as strings. | |
65 | ||
66 | The 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 | |
68 | language'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 | ||
72 | For more details on introspecting aspects of different languages, | |
73 | @xref{Compiler Tower}. | |
74 | ||
75 | @node Emacs Lisp | |
76 | @subsection Emacs Lisp | |
77 | ||
78 | Emacs Lisp (Elisp) is a dynamically-scoped Lisp dialect used in the | |
79 | Emacs editor. @xref{top,,Overview,elisp,Emacs Lisp}, for more | |
80 | information on Emacs Lisp. | |
81 | ||
82 | We hope that eventually Guile's implementation of Elisp will be good | |
83 | enough to replace Emacs' own implementation of Elisp. For that reason, | |
84 | we have thought long and hard about how to support the various features | |
85 | of Elisp in a performant and compatible manner. | |
86 | ||
87 | Readers familiar with Emacs Lisp might be curious about how exactly | |
88 | these various Elisp features are supported in Guile. The rest of this | |
89 | section 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{'()}. |
102 | It is false, and it is the end-of-list; thus it is a boolean, and a list | |
103 | as well. | |
104 | ||
105 | Guile has chosen to support @code{nil} as a separate value, distinct | |
106 | from @code{#f} and @code{'()}. This allows existing Scheme and Elisp | |
107 | code to maintain their current semantics. @code{nil}, which in Elisp | |
108 | would just be written and read as @code{nil}, in Scheme has the external | |
109 | representation @code{#nil}. | |
110 | ||
bcbfc940 AW |
111 | This decision to have @code{nil} as a low-level distinct value |
112 | facilitates interoperability between the two languages. Guile has chosen | |
3279b6b1 AW |
113 | to 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 |
121 | And in C, one has: |
122 | ||
123 | @example | |
124 | scm_is_bool (SCM_ELISP_NIL) @result{} 1 | |
125 | scm_is_false (SCM_ELISP_NIL) @result{} 1 | |
126 | scm_is_null (SCM_ELISP_NIL) @result{} 1 | |
127 | @end example | |
128 | ||
129 | In this way, a version of @code{fold} written in Scheme can correctly | |
130 | fold a function written in Elisp (or in fact any other language) over a | |
131 | nil-terminated list, as Elisp makes. The converse holds as well; a | |
132 | version of @code{fold} written in Elisp can fold over a | |
133 | @code{'()}-terminated list, as made by Scheme. | |
134 | ||
135 | On 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 |
137 | only one bit, and so a test for, for example, @code{#f}-or-@code{nil} |
138 | may be made very efficiently. See @code{libguile/boolean.h}, for more | |
139 | information. | |
140 | ||
141 | @subsubsection Equality | |
142 | ||
3279b6b1 AW |
143 | Since Scheme's @code{equal?} must be transitive, and @code{'()} |
144 | is 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 | 159 | However, 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 | |
173 | These choices facilitate interoperability between Elisp and Scheme code, | |
bcbfc940 AW |
174 | but they are not perfect. Some code that is correct standard Scheme is |
175 | not correct in the presence of a second false and null value. For | |
176 | example: | |
3279b6b1 AW |
177 | |
178 | @example | |
179 | (define (truthiness x) | |
180 | (if (eq? x #f) | |
181 | #f | |
182 | #t)) | |
183 | @end example | |
184 | ||
185 | This code seems to be meant to test a value for truth, but now that | |
186 | there are two false values, @code{#f} and @code{nil}, it is no longer | |
187 | correct. | |
188 | ||
189 | Similarly, 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 | ||
199 | Here, @code{my-length} will raise an error if @var{l} is a | |
200 | @code{nil}-terminated list. | |
201 | ||
202 | Both of these examples are correct standard Scheme, but, depending on | |
203 | what they really want to do, they are not correct Guile Scheme. | |
204 | Correctly written, they would test the @emph{properties} of falsehood or | |
bcbfc940 AW |
205 | nullity, not the individual members of that set. That is to say, they |
206 | should use @code{not} or @code{null?} to test for falsehood or nullity, | |
207 | not @code{eq?} or @code{memv} or the like. | |
208 | ||
209 | Fortunately, using @code{not} and @code{null?} is in good style, so all | |
210 | well-written standard Scheme programs are correct, in Guile Scheme. | |
3279b6b1 AW |
211 | |
212 | Here 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 | ||
229 | This 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 | ||
238 | Guile 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 |
244 | In contrast to Scheme, which uses ``lexical scoping'', Emacs Lisp scopes |
245 | its variables dynamically. Guile supports dynamic scoping with its | |
246 | ``fluids'' facility. @xref{Fluids and Dynamic States}, for more | |
247 | information. | |
248 | ||
e6709db6 AW |
249 | @node Other Elisp Features |
250 | @subsubsection Other Elisp Features | |
251 | ||
4bd4b6e6 AW |
252 | Buffer-local and mode-local variables should be mentioned here, along |
253 | with buckybits on characters, Emacs primitive data types, the | |
254 | Lisp-2-ness of Elisp, and other things. Contributions to the | |
255 | documentation 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} |
261 | was not the first non-Schemey language implemented by Guile, but it was | |
262 | the first implemented for Guile's bytecode compiler. The goal was to | |
263 | support ECMAScript version 3.1, a relatively small language, but the | |
264 | implementor was completely irresponsible and got distracted by other | |
e6709db6 AW |
265 | things before finishing the standard library, and even some bits of the |
266 | syntax. So, ECMAScript does deserve a mention in the manual, but it | |
267 | doesn't deserve an endorsement until its implementation is completed, | |
268 | perhaps by some more responsible hacker. | |
269 | ||
270 | In the meantime, the charitable user might investigate such invocations | |
271 | as @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: |