source: project/wiki/faq @ 2905

Last change on this file since 2905 was 2905, checked in by felix winkelmann, 14 years ago

manual updates

  • Property svnwiki:title set to Chicken Scheme: Frequently Asked Questions
File size: 23.0 KB
Line 
1[[toc:]]
2[[tags:faq]]
3
4== Introduction
5
6This is the list of Frequently Asked Questions about Chicken Scheme.
7If you have a question not answered here, feel free to post to the chicken-users mailing list;
8if you consider your question general enough, feel free to add it to this list.
9
10== General
11
12=== Why yet another Scheme implementation?
13
14Since Scheme is a relatively simple language, a large number of implementations exist and
15each has its specific advantages and disadvantages. Some are fast, some provide a rich
16programming environment. Some are free, others are tailored to specific domains, and so on. The reasons
17for the existence of CHICKEN are:
18
19* CHICKEN is portable because it generates C code that runs on a large number of platforms.
20
21* CHICKEN is extendable, since its code generation scheme and runtime system/garbage collector fits neatly into a C environment.
22
23* CHICKEN is free and can be freely distributed, including its source code.
24
25* CHICKEN offers better performance than nearly all interpreter based implementations, but still provides full Scheme semantics.
26
27* As far as we know, CHICKEN is the first implementation of Scheme that uses Henry Baker's [[http://home.pipeline.com/~hbaker1/CheneyMTA.html|Cheney on the M.T.A]] concept.
28
29=== What should I do if I find a bug?
30
31Send e-mail to {{felix@@call-with-current-continuation.org}}
32with some hints about the problem, like
33version/build of the compiler, platform, system configuration, code that
34causes the bug, etc.
35
36=== Why are values defined with {{define-foreign-variable}} or {{define-constant}} or {{define-inline}} not seen outside of the containing source file?
37
38Accesses to foreign variables are translated directly into C constructs that access the variable,
39so the Scheme name given to that variable does only exist during compile-time.
40The same goes for constant- and inline-definitions: The name is only there to tell the compiler
41that this reference is to be replaced with the actual value.
42
43=== How does {{cond-expand}} know which features are registered in used units?
44
45Each unit used via {{(declare (uses ...))}} is registered as a feature and
46so a symbol with the unit-name can be tested by {{cond-expand}} during macro-expansion-time.
47Features registered using the {{register-feature!}} procedure are only
48available during run-time of the compiled file. You can use the {{eval-when}} form
49to register features at compile time.
50
51=== Why are constants defined by {{define-constant}} not honoured in {{case}} constructs?
52
53{{case}} expands into a cascaded {{if}} expression, where the first item in each arm
54is treated as a quoted list. So the {{case}} macro can not infer wether
55a symbol is to be treated as a constant-name (defined via {{define-constant}}) or
56a literal symbol.
57
58
59=== How can I enable case sensitive reading/writing in user code?
60
61To enable the {{read}} procedure to read symbols and identifiers case sensitive, you can set the
62parameter {{case-sensitivity}} to {{#t}}.
63
64
65=== How can I change {{match-error-control}} during compilation?
66
67Use {{eval-when}}, like this:
68
69<enscript highlight=scheme>
70(eval-when (compile)
71(match-error-control #:unspecified) )
72</enscript>
73
74
75=== Why doesn't CHICKEN support the full numeric tower by default?
76
77The short answer:
78
79<enscript highlight=scheme>
80% chicken-setup numbers
81% csi -q
82#;1> (use numbers)
83</enscript>
84
85The long answer:
86
87There are a number of reasons for this:
88
89- For most applications of Scheme fixnums (exact word-sized integers) and flonums (64-bit floating-point
90numbers) are more than sufficient;
91
92- Interfacing to C is simpler;
93
94- Dispatching of arithmetic operations is more efficient.
95
96There is an extension based on the GNU Multiprecision Package that implements most of the full
97numeric tower, see http://www.call-with-current-continuation.org/eggs/numbers.html.
98
99
100=== How can I specialize a generic function method to match instances of every class?
101
102Specializing a method on {{<object>}} doesn't work on primitive data objects like
103numbers, strings, etc. so for example
104
105<enscript highlight=scheme>
106(define-method (foo (x <my-class>)) ...)
107(define-method (foo (x <object>)) ...)
108(foo 123)
109</enscript>
110
111will signal an error, because to applicable method can be found. To specialize a method for primitive
112objects, use {{<top>}}:
113
114<enscript highlight=scheme>
115(define-method (foo (x <top>)) ...)
116</enscript>
117
118
119=== Does CHICKEN support native threads?
120
121Currently native threads are not supported. The runtime system is not reentrant, and the garbage-collection
122algorithm would be made much more complicated, since the location of every object (whether it is allocated
123on the stack or on the heap or completely outside the GC-able data space) has to be checked - this would
124be rather complex and inefficient in a situation where multiple threads are involved.
125
126
127== Platform specific
128
129=== How do I generate a DLL under MS Windows (tm) ?
130
131Use {{csc}} in combination with the {{-dll}} option:
132
133{{C:\> csc foo.scm -dll}}
134
135=== How do I generate a GUI application under Windows(tm)?
136
137Invoke {{csc}} with the {{-windows}} option. Or pass the {{-DC_WINDOWS_GUI}}
138option to the C compiler and link with the GUI version of the runtime system (that's {{libchicken-gui[-static].lib}}.
139The GUI runtime displays error messages in a message box and does some rudimentary command-line
140parsing.
141
142=== Compiling very large files under Windows with the Microsoft C compiler fails with a message indicating insufficient heap space.
143
144It seems that the Microsoft C compiler can only handle files up to a certain size, and it doesn't utilize virtual memory as
145well as the GNU C compiler, for example. Try closing running applications. If that fails, try to break up the Scheme code
146into several library units.
147
148=== When I run {{csi}} inside an emacs buffer under Windows, nothing happens.
149
150Invoke {{csi}} with the {{-:c}} runtime option. Under Windows the interpreter thinks it
151is not running under control of a terminal and doesn't print the prompt and does not flush the output stream properly.
152
153=== I load compiled code dynamically in a Windows GUI application and it crashes.
154
155Code compiled into a DLL to be loaded dynamically must be linked with the same runtime system as the loading
156application. That means that all dynamically loaded entities (including extensions built and installed with
157{{chicken-setup}}) must be compiled with the {{-windows}} {{csc}} option.
158
159=== On Windows, {{csc.exe}} seems to be doing something wrong.
160
161The Windows development tools include a C# compiler with the same name. Either invoke {{csc.exe}} with a full
162pathname, or put the directory where you installed CHICKEN in front of the MS development tool path in the {{PATH}}
163environment variable.
164
165
166== Customization
167
168
169=== How do I run custom startup code before the runtime-system is invoked?
170
171When you invoke the C compiler for your translated Scheme source program, add the C compiler option
172{{-DC_EMBEDDED}}, or pass {{-embedded}} to the {{csc}}
173driver program, so no entry-point function will be generated ({{main()}}).
174When your are finished with your startup processing, invoke:
175
176<enscript highlight=c>
177CHICKEN_main(argc, argv, C_toplevel);
178</enscript>
179
180where {{C_toplevel}} is the entry-point into the compiled Scheme code. You
181should add the following  declarations at the head of your code:
182
183<enscript highlight=c>
184#include "chicken.h"
185extern void C_toplevel(C_word,C_word,C_word) C_noret;
186</enscript>
187
188=== How can I add compiled user passes?
189
190To add a compiled user pass instead of an interpreted one, create a library unit and recompile
191the main unit of the compiler (in the file {{chicken.scm}}) with an additional {{uses}}
192declaration. Then link all compiler modules and your (compiled) extension to create a new version of
193the compiler, like this (assuming all sources are in the
194current directory):
195
196<enscript highlight=scheme>
197% cat userpass.scm
198;;;; userpass.scm - My very own compiler pass
199
200(declare (unit userpass))
201
202;; Perhaps more user passes/extensions are added:
203(let ([old (user-pass)])
204(user-pass
205(lambda (x)
206(let ([x2 (do-something-with x)])
207          (if old
208              (old x2)
209              x2) ) ) ) )
210</enscript>
211
212 % csc -c -x userpass.scm
213 % csc chicken.scm -c -o chicken-extended.o -uses userpass
214 % gcc chicken-extended.o support.o easyffi.o compiler.o optimizer.o batch-driver.o c-platform.o \
215 c-backend.o userpass.o `csc -ldflags -libs` -o chicken-extended
216
217On platforms that support it (Linux ELF, Solaris, Windows + VC++), compiled code can be loaded via {{-extend}}
218just like source files (see {{load}} in the User's Manual).
219
220
221== Macros
222
223=== Why doesn't my fancy macro work in compiled code?
224
225Macro bodies that are defined and used in a compiled source-file are
226evaluated during compilation and so have no access to definitions in the
227compiled file. Note also that during compile-time macros are only available in
228the same source file in which they are defined. Files included via {{include}}
229are considered part of the containing file.
230
231=== Why are macros not visible outside of the compilation unit in which they are defined?
232
233Macros are defined during compile time, so when a file has been compiled, the definitions are gone. An exception
234to this rule are macros defined with {{define-macro}}, which are also visible at run-time, i.e.
235in {{eval}}. To use macros defined in other files, use the {{include}} special
236form.
237
238== Warnings and errors
239
240=== Why does my program crash when I use callback functions (from Scheme to C and back to Scheme again)?
241
242There are two reasons why code involving callbacks can crash out of no apparent reason:
243
244# It is important to use {{foreign-safe-lambda/foreign-safe-lambda*}} for the C code that is to call back into Scheme. If this is not done than sooner or later the available stack space will be exhausted.
245
246# If the C code uses a large amount of stack storage, or if Scheme-to-C-to-Scheme calls are nested deeply, then the available nursery space on the stack will run low. To avoid this it might be advisable to run the compiled code with a larger nursery setting, i.e. run the code with {{-:s...}} and a larger value than the default (for example {{-:s300k}}), or use the {{-nursery}} compiler option.  Note that this can decrease runtime performance on some platforms.
247
248=== Why does the linker complain about a missing function {{_C_..._toplevel}}?
249
250This message indicates that your program uses a library-unit, but that the
251object-file or library was not supplied to the linker. If you have the unit
252{{foo}}, which is contained in {{foo.o}} than you have to supply it to the
253linker like this (assuming a GCC environment):
254
255{{% csc program.scm foo.o -o program}}
256
257=== Why does the linker complain about a missing function {{_C_toplevel}}?
258
259This means you have compiled a library unit as an application. When a unit-declaration (as in {{(declare (unit ...))}})
260is given, then this file has a specially named toplevel entry procedure. Just remove the declaration,
261or compile this file to an object-module and link it to your application code.
262
263=== Why does my program crash when I compile a file with {{-unsafe}} or unsafe declarations?
264
265The compiler option {{-unsafe}} or the declaration {{(declare (unsafe))}} disable
266certain safety-checks to improve performance, so code that would normally
267trigger an error will work unexpectedly or even crash the running application.
268It is advisable to develop and debug a program in safe mode (without unsafe
269declarations) and use this feature only if the application works properly.
270
271=== Why do I get a warning when I define a global variable named {{match}}?
272
273Even when the {{match}} unit is not used, the macros from that package are visible in the compiler.
274The reason for this is that macros can not be accessed from library units (only when explicitly evaluated in running
275code). To speed up macro-expansion time, the compiler and the interpreter both already provide the compiled
276{{match-...}} macro definitions. Macros shadowed lexically are no problem, but global definitions
277of variables named identically to (global) macros are useless - the macro definition shadows the global
278variable.
279
280This problem can be solved using a different name or undefining the macro, like this:
281
282<enscript highlight=scheme>
283(eval-when (compile eval) (undefine-macro! 'match))
284</enscript>
285=== Why don't toplevel-continuations captured in interpreted code work?
286
287Consider the following piece of code:
288
289<enscript highlight=scheme> 
290(define k (call-with-current-continuation (lambda (k) k)))
291(k k)
292</enscript>
293
294When compiled, this will loop endlessly. But when interpreted, {{(k k)}} will return
295to the read-eval-print loop! This happens because the continuation captured will eventually read the
296next toplevel expression from the standard-input (or an input-file if loading from a file). At the moment
297{{k}} was defined, the next expression was {{(k k)}}. But when {{k}}
298is invoked, the next expression will be whatever follows after {{(k k)}}.
299In other words, invoking a captured continuation will not rewind the file-position of the input source.
300A solution is to wrap the whole code into a {{(begin ...)}} expression, so all toplevel
301expressions will be loaded together.
302
303=== Why does {{define-reader-ctor}} not work in my compiled program?
304
305The following piece of code does not work as expected:
306
307<enscript highlight=scheme>
308(eval-when (compile)
309(define-reader-ctor 'integer->char integer->char) )
310(print #,(integer->char 33))
311</enscript>
312
313The problem is that the compiler reads the complete source-file before doing any processing on it,
314so the sharp-comma form is encountered before the reader-ctor is defined. A possible solution is to include
315the file containing the sharp-comma form, like this:
316
317<enscript highlight=scheme>
318(eval-when (compile)
319(define-reader-ctor 'integer->char integer->char) )
320
321(include "other-file")
322</enscript>
323
324<enscript highlight=scheme>
325;;; other-file.scm:
326(print #,(integer->char 33))
327</enscript>
328
329=== Why do built-in units, such as srfi-1, srfi-18, and posix fail to load?
330
331When you try to {{use}} a built-in unit such as {{srfi-18}}, you may get the following error:
332
333<enscript highlight=scheme>
334#;1> (use srfi-18)
335; loading library srfi-18 ...
336Error: (load-library) unable to load library
337srfi-18
338"dlopen(libchicken.dylib, 9): image not found"                ;; on a Mac
339"libchicken.so: cannot open shared object file: No such file or directory"  ;; Linux
340</enscript>
341
342Another symptom is that {{(require 'srfi-18)}} will silently fail.
343
344This typically happens because the Chicken libraries have been installed in a non-standard location, such as your home directory.  The workaround is to explicitly tell the dynamic linker where to look for your libraries:
345
346 export DYLD_LIBRARY_PATH=~/scheme/chicken/lib:$DYLD_LIBRARY_PATH ;; Mac
347 export LD_LIBRARY_PATH=~/scheme/chicken/lib:$LD_LIBRARY_PATH    ;; Linux
348
349=== How can I increase the size of the trace shown when runtime errors are detected?
350
351When a runtime error is detected, Chicken will print the last entries from the trace of functions called
352(unless your executable was compiled with the {{-no-trace}} option.
353By default, only 16 entries will be shown.
354To increase this number pass the {{-:aN}} parameter to your executable.
355
356== Optimizations
357
358=== How can I obtain smaller executables?
359
360If you don't need {{eval}} or the stuff in the {{extras}} library unit,
361you can just use the {{library}} unit:
362
363<enscript highlight=scheme>
364        (declare (uses library))
365        (display "Hello, world!\n")
366</enscript>
367
368(Don't forget to compile with the {{-explicit-use}} option)
369Compiled with Visual C++ this generates an executable of around 240 kilobytes.
370It is theoretically possible to compile something without the library, but
371a program would have to implement quite a lot of support code on its own.
372
373=== How can I obtain faster executables?
374
375There are a number of declaration specifiers that should be used to speed up
376compiled files: declaring {{(standard-bindings)}} is mandatory, since this enables
377most optimizations. Even if some standard procedures should be redefined, you can
378list untouched bindings in the declaration.
379Declaring {{(extended-bindings)}} lets the compiler choose faster versions of certain
380internal library functions. This might give another speedup. You can also use the
381the {{usual-integrations}} declaration, which is identical to declaring
382{{standard-bindings}} and {{extended-bindings}}
383(note that {{usual-integrations}} is set by default).
384Declaring {{(block)}} tells the compiler that global procedures are not changed
385outside the current compilation unit, this gives the compiler some more
386opportunities for optimization.
387If no floating point arithmetic is required, then declaring {{(number-type fixnum)}}
388can give a big performance improvement, because the compiler can now inline
389most arithmetic operations.
390Declaring {{(unsafe)}} will switch off most safety checks.
391If threads are not used, you can declare {{(disable-interrupts)}}.
392You should always use maximum optimizations settings for your C compiler.
393Good GCC compiler options on Pentium (and compatible) hardware are:
394{{-Os -fomit-frame-pointer -fno-strict-aliasing}}
395Some programs are very sensitive to the setting of the nursery (the first heap-generation). You
396should experiment with different nursery settings (either by compiling with the {{-nursery}}
397option or by using the {{-:s...}} runtime option).
398
399=== Which non-standard procedures are treated specially when the {{extended-bindings}} or {{usual-integrations}} declaration or compiler option is used?
400
401The following standard bindings are handled specially, depending on optimization options
402and compiler settings:
403
404 + * - / quotient eq? eqv? equal? apply c...r values call-with-values
405 list-ref null? length not char? string? symbol? vector? pair? procedure?
406 boolean? number? complex? rational? real? exact? inexact? list? eof-object?
407 string-ref string-set! vector-ref vector-set! char=? char<? char>? char<=? char>=?
408 char-numeric? char-alphabetic? char-whitespace? char-upper-case?
409 char-lower-case? char-upcae char-downcase list-tail assv memv memq assoc
410 member set-car! set-cdr! abs exp sin cos tan log asin acos atan sqrt
411 zero? positive? negative? vector-length string-length char->integer
412 integer->char inexact->exact = > < >= <= for-each map substring
413 string-append gcd lcm list exact->inexact string->number number->string
414 even? odd? remainder floor ceiling truncate round cons vector string
415 string=? string-ci=? make-vector call-with-current-continuation
416
417The following extended bindings are handled specially:
418
419{{bitwise-and}} {{bitwise-ior}} {{bitwise-xor}} {{bitwise-not}}
420{{bit-set?}} {{add1}} {{sub1}}
421{{fx+}}
422{{fx-}} {{fx*}} {{fx/}} {{fxmod}}
423{{fx=}} {{fx>}} {{fx>=}} {{fixnum?}} {{fxneg}} {{fxmax}} {{fxmin}}
424{{fxand}} {{fxior}} {{fxxor}} {{fxnot}} {{fxshl}} {{fxshr}}
425{{flonum?}} {{fp+}}
426{{fp-}} {{fp*}} {{fp/}} {{atom?}}
427{{fp=}} {{fp>}} {{fp>=}} {{fpneg}} {{fpmax}} {{fpmin}}
428{{arithmetic-shift}} {{signum}} {{flush-output}} {{thread-specific}} {{thread-specific-set!}}
429{{not-pair?}} {{null-list?}} {{print}} {{print*}} {{u8vector->bytevector}}
430{{s8vector->bytevector}} {{u16vector->bytevector}} {{s16vector->bytevector}}
431{{u32vector->bytevector}}
432{{s32vector->bytevector}} {{f32vector->bytevector}} {{f64vector->bytevector}} {{block-ref}}
433{{byte-vector-length}}
434{{u8vector-length}}
435{{s8vector-length}}
436{{u16vector-length}}
437{{s16vector-length}}
438{{u32vector-length}}
439{{s32vector-length}}
440{{f32vector-length}}
441{{f64vector-length}}
442{{u8vector-ref}}
443{{s8vector-ref}}
444{{u16vector-ref}}
445{{s16vector-ref}}
446{{u32vector-ref}}
447{{s32vector-ref}}
448{{f32vector-ref}}
449{{f64vector-ref}}
450{{u8vector-set!}}
451{{s8vector-set!}}
452{{u16vector-set!}}
453{{s16vector-set!}}
454{{u32vector-set!}}
455{{s32vector-set!}}
456{{hash-table-ref}}
457{{block-set!}} {{number-of-slots}}
458{{first}} {{second}} {{third}} {{fourth}} {{null-pointer?}} {{pointer->object}}
459{{make-record-instance}}
460{{locative-ref}} {{locative-set!}} {{locative?}} {{locative->object}} {{identity}}
461{{cpu-time}} {{error}} {{call/cc}} {{any?}}
462
463== Garbage collection
464
465=== Why does a loop that doesn't {{cons}} still trigger garbage collections?
466
467Under CHICKENs implementation policy, tail recursion is achieved simply by avoiding to
468return from a function call. Since the programs is CPS converted, a continuous
469sequence of nested procedure calls is performed. At some stage the stack-space
470has to run out and the current procedure and its parameters (including the current continuation) are stored somewhere
471in the runtime system. Now a minor garbage collection occurs and rescues all live
472data from the stack (the first heap generation) and moves it into the the second heap generation. Than the stack is cleared (using
473a {{longjmp}}) and execution can continue from the saved state.
474With this method arbitrary recursion (in tail- or non-tail position) can happen,
475provided the application doesn't run out of heap-space.
476(The difference between a tail- and a non-tail call is that the tail-call has no
477live data after it invokes its continuation - and so the amount of heap-space needed stays constant)
478
479=== Why do finalizers not seem to work in simple cases in the interpeter?
480
481Consider the following interaction in CSI:
482
483 #;1> (define x '(1 2 3))
484 #;2> (define (yammer x) (print x " is dead"))
485 #;3> (set-finalizer! x yammer)
486 (1 2 3)
487 #;4> (gc #t)
488 157812
489 #;5> (define x #f)
490 #;6> (gc #t)
491 157812
492 #;7>
493
494While you might expect objects to be reclaimed and "''(1 2 3) is dead''" printed, it won't happen:
495the literal list gets held in the interpreter history, because it is the
496result value of the set-finalizer! call.
497Running this in a normal program will work fine.
498
499When testing finalizers from the interpreter, you might want to define a trivial macro such as
500
501 (define-macro (v x) `(begin (print ,x) (void)))
502
503and wrap calls to {{set-finalizer!}} in it.
504
505== Interpreter
506
507=== Does CSI support history and autocompletion?
508
509CSI doesn't support it natively but it can be activated with the [[readline]] egg.
510After installing the egg, add the following to your {{~/.csirc}} or equivalent file:
511
512<enscript highlight=scheme>
513; The regex egg is required for ##csi#name-of-symbols-starting-with
514(require 'readline)
515(current-input-port (make-gnu-readline-port))
516(gnu-history-install-file-manager (string-append (or (getenv "HOME") ".") "/.csi.history"))
517</enscript>
518
519== Extensions ==
520
521=== How can I install Chicken eggs to a non-default location?
522
523You can just set the {{CHICKEN_REPOSITORY}} environment variable.
524It should contain the path where you want eggs to be installed:
525
526 $ export CHICKEN_REPOSITORY=~/chicken/
527 $ chicken-setup extensionname
528
529In order to make programs (including csi) see these eggs, you should set this variable when you run them.
530Alternatively, you can call the {{repository-path}} Scheme procedure before loading the eggs, as in:
531
532<enscript highlight=scheme>
533(repository-path "/home/azul/chicken")
534(use format-modular)
535</enscript>
536
537Note, however, that using {{repository-path}} as above hard-codes the location of your eggs in your source files.  While this might not be an issue in your case, it might be safe to keep this configuration outside of the source code (that is, specifying it as an environment variable) to make it easier to maintain.
538
539=== Can I install chicken eggs as a non-root user?
540
541Yes, just [[#How can I install Chicken eggs to a non-default location?|install them in a directory you can write]].
542
543----
544
545Previous: [[Bugs and limitations]]
546
547Next: [[Acknowledgements]]
Note: See TracBrowser for help on using the repository browser.