source: project/wiki/new-chapter1 @ 9106

Last change on this file since 9106 was 9106, checked in by etatoby, 12 years ago

Changes applied for etatoby ( through svnwiki:

Done a few TODOs, improved a few parts

File size: 22.6 KB
1== Getting started with Chicken
3TODO: this page is a revision and expansion of the existing Chapter 1
4of the Chicken manual. The goal is to give an overview of the whole
5system, and to give the new user some recipes for common use cases.
6I know this is somewhat verbose, and would be happy to eliminate
7unnecessary verbiage, but I do think the content shown here is
8worthwhile. Your mileage, of course, might vary :-) -- vincent
10''(This document describes version 3.0.0)'' TODO: the version number
11should be moved from here to the cover or index page. ''No, leave the
12Chicken version number on every manual page so that we know if a
13non-updated page exists. --John Cowan'' Agreed, it should be on EVERY
14manual page. Also, it should be on the front cover. That suggests an
15automated way of getting it there? If that's too complicated, I'd
16suggest leaving it here AND putting it on the cover. -- vincent
18'''Chicken is a compiler that translates Scheme source files into
19C''', which in turn can be fed to a C compiler to generate a
20standalone executable.  An interpreter is also available and can be
21used as a scripting environment or for testing programs before
24This chapter is designed to get you started with Chicken programming,
25describing what it is and what it will do for you, and covering basic
26use of the system. With almost everything discussed here, there is
27more to the story, which the remainder of the manual reveals. Here, we
28only cover enough to get you started. Nonetheless, someone who knows
29Scheme already should be able to use this chapter as the basis for
30writing and running small Chicken programs.
32=== Scheme
34Scheme is a member of the Lisp family of languages, of which Common
35Lisp and Emacs Lisp are the other two widely-known members. As with
36Lisp dialects, Scheme features
38* a wide variety of programming paradigms, including imperative, functional, and object-oriented
39* a very simple syntax, based upon nested parenthesization
40* the ability to extend the language in meaningful and useful ways
42In contrast to Common Lisp, Scheme is very minimal, and tries to
43include only those features absolutely necessary in programming. In
44contrast to Emacs Lisp, Scheme is not anchored into any one program
45(Emacs), and has a somewhat more modern language design.
47Scheme is defined in a document called ''The Revised^5 Report on the Algorithmic Language Scheme'', or ''R5RS'' for short. (Yes, it really has been revised five times, so an expanded version of its name would be ''The Revised Revised Revised Revised Revised Report''.)  A newer report, ''R6RS'', was
48released in 2007, but this report has attracted considerable
49controversy, and not all Scheme implementations will be made compliant
50with it. Chicken essentially complies with R5RS.
52Even though Scheme is consciously minimalist, it is recognized that a
53language must be more than a minimal core in order to be
54useful. Accordingly, the Scheme community uses a process known as
55`Scheme Requests For Implementation' (SRFI, pronounced `SUR-fee') to
56define new language features. A typical Scheme system therefore
57complies with one of the Scheme reports plus some or all of the
58accepted SRFIs.
60A good starting point for Scheme knowledge is
61[[]]. There you will find the defining reports,
62FAQs, lists of useful books and other resources, and the SRFIs.
64The Chicken community is at present developing tutorials for
65programmers who are new to Scheme but experienced with Python, Ruby,
66or other languages. These can be found on the Chicken wiki.
68=== Chicken
70Chicken is an implementation of Scheme that has many advantages.
73Chicken Scheme combines an optimising compiler with a reasonably fast
74interpreter.  It supports almost all of R5RS and the important SRFIs.
75The compiler generates portable C code that supports tail recursion,
76first-class continuations, and lightweight threads, and the interface to
77and from C libraries is flexible, efficient, and easy to use.  There are
78hundreds of contributed Chicken libraries that make the programmer's
79task easier.  The interpreter allows interactive use, fast prototyping,
80debugging, and scripting.  The active and helpful Chicken community
81fixes bugs and provides support.  Extensive documentation is supplied.
84Chicken includes
86* a Scheme interpreter that supports almost all of  R5RS Scheme, with
87  only a few relatively minor omissions, and with many extensions
88* a compatible compiler whose target is C, thus making porting to new
89  machines and architectures relatively straightforward
90** the C support allows Scheme code to include `embedded' C code,
91  thus making it relatively easy to invoke host OS or library
92  functions
93* a framework for language extensions, library modules that broaden
94  the functionality of the system
96This package is distributed under the '''BSD license''' and as such is free
97to use and modify.
99Scheme cognoscenti will appreciate the method of compilation and the
100design of the runtime-system, which follow closely Henry Baker's
101[[|CONS Should Not
102CONS Its Arguments, Part II: Cheney on the M.T.A.]] paper and expose a
103number of interesting properties.
105* Consing (creation of data on the heap) is relatively inexpensive,
106  because a generational garbage collection scheme is used, in which
107  short-lived data structures are reclaimed extremely quickly.
109* Moreover, {{call-with-current-continuation}} is practically for free
110  and Chicken does not suffer under any performance penalties if
111  first-class continuations are used in complex ways.
113The generated C code is fully tail-recursive.
115Some of the features supported by Chicken:
117* SRFIs 0, 1, 2, 4, 6-19, 23, 25-31, 37-40, 42, 43, 45, 47, 55, 57,
118  60-63, 66, 69, 72, 78, 85 and 95.
119* Lightweight threads based on first-class continuations
120* Pattern matching with Andrew Wright's {{match}} package
121* Record structures
122* Extended comment- and string-literal syntaxes
123* Libraries for regular expressions, string handling
124* UNIX system calls and extended data structures
125* Create interpreted or compiled shell scripts written in Scheme for
126  UNIX or Windows
127* Compiled C files can be easily distributed
128* Allows the creation of fully self-contained statically linked executables
129* On systems that support it, compiled code can be loaded dynamically
131Chicken has been used in many environments ranging from embedded
132systems through desktop machines to large-scale server deployments. 
133The number of language extensions, or '''eggs''', will soon reach 400,
136* extended language features
137* development tools, such as documentation generators, debugging, and
138  automated testing libraries
139* interfaces to other languages such as Java, Python, and Objective-C
140* interfaces to database systems, GUIs, and other large-scale
141  libraries,
142* network applications, such as servers and clients for ftp,
143  smtp/pop3, irc, and http 
144* web servers and related tools, including URL parsing, HTML
145  generation, AJAX, and HTTP session management
146* data formats, including XML, JSON, and Unicode support
148Chicken is supported by SWIG (Simplified Wrapper and Interface
149Generator), a tool that produces quick-and-dirty interface modules
150for C libraries ([[]]).
152This chapter provides you with an overview of the entire system, with
153enough information to get started writing and running small Scheme
154programs. Subsequent chapters cover
156* [[Basic mode of operation]]: Compiling Scheme files.
158* [[Using the compiler]]: Explains how to use Chicken to compile
159  programs and execute them.
161* [[Using the interpreter]]: Invocation and usage of {{csi}}, the
162  Chicken interpreter
164* [[Supported language]]: The language implemented by Chicken
165  (deviations from the standard and extensions).
167* [[Interface to external functions and variables]]: Accessing C and
168  C++ code and data.
170* [[chicken-setup]]: Packaging and installing extension libraries.
172* [[Data representation]]: How Scheme data is internally represented.
174* [[Bugs and limitations]]: Yes, there are some.
176* [[FAQ]]: A list of Frequently Asked Questions about Chicken (and
177  their answers!).
179* [[Acknowledgements]]: A list of some of the people that have
180  contributed to make Chicken what it is.
182* [[Bibliography]]: Links to documents that may be of interest.
184=== Installing Chicken
186Chicken is available in binary form for Windows and Linux/x86
187systems, and in source form for all other platforms. Refer to the
188{{README}} file in the distribution for instructions on installing it
189on your system.
191Because it compiles to C, Chicken requires that a C compiler be
192installed on your system. (If you're not writing embedded C code, you
193can pretty much ignore the C compiler once you have installed it.)
195* On a Linux system, the GNU Compiler Collection ({{gcc}}) should be
196  installed as part of the basic operating system.
197* On Macintosh OS X, you will need the XCode tools, which are shipped
198  on the OS X DVD with recent versions of the operating system.
199  ''TODO: What about Fink or such? MacPorts would be the other case, I
200  guess. Here we're talking about C compilers, so I'm not sure if
201  people use the Fink or MacPorts gcc compilers.'' 
202* On Windows, you have four choices.
203** Cygwin ([[]]) provides a relatively
204  full-featured Unix environment for Windows. 
205** The GNU Compiler Collection has been ported to Windows, in the
206  MinGW system ([[]]). Unlike Cygwin,
207  executables produced with MinGW do not need the Cygwin DLLs in order
208  to run.   
209*** TODO: explain mingw build here
210** MSys is a companion package to MinGW; it provides a minimum
211  Unix-style development/build environment, again ported from free
212  software.
213*** TODO: explain mingw-msys build here
214** Microsoft Visual Studio will soon be supported, including the
215  Express edition, which is a non-free but no-cost compiler suite
216  available from Microsoft
217  ([[]]). Chicken supports
218  command-line building using the command-line C/C++ compiler.
219*** Visual
220  Studio users will want to install the Unix Utilities, available at
221  [[]],
222  in order to get suitable versions of {{make}}, {{tar}}, {{gzip}}, and
223  similar commands. 
225Refer to the {{README}} file for the version you're installing for
226more information on the installation process.
228TODO: what's the least we can possibly say about PATH,
229CHICKEN_INCLUDE_PATH, DYLD_LIBRARY_PATH, etc?  ''The least we can say
230at this point is nothing at all.  Let's do that.''
231* ''Agreed: the previous paragraph is my substitute for this TODO,
232which we can just delete.''
234=== Development environments
236The simplest development environment is a text editor and terminal
237window (Windows: Command Prompt, OSX: Terminal, Linux/Unix: xterm) for
238using the interpreter and/or calling the compiler. If
239you [[readline#examples|install the {{readline}} egg]], you have all the
240benefits of command history in the interpreter, Emacs or vi-compatible line
241editing, and customization.
243You will need a text editor that knows Scheme; it's just too painful
244with editors that don't do parenthesis matching and proper
245indentation. Some editors allow you to execute Scheme code directly in
246the editor. This makes programming very interactive: you can type in a
247function and then try it right away. This feature is very highly
250As programmers have very specific tastes about editors, the editors
251listed here are shown in alphabetic order. We aren't about to tell you
252which editor to use, and there may be editors not shown here that
253might satisfy your needs.
255; [[|Emacs]] : Emacs is an extensible, customizable, self-documenting editor available for Linux/Unix, Macintosh, and Windows systems; CHICKEN provides Emacs support out of the box, with the {{hen.el}} Emacs Lisp file. Consult the `Emacs Guide for Chicken Users' (TODO: this document doesn't exist yet) for information on setting up and using Emacs with Chicken.
257; [[|Epsilon]] : Epsilon is a commercial text editor whose design was inspired by Emacs. Although Scheme support isn't provided, a Lisp mode is available on Lugaru's FTP site, and could with some work be made to duplicate the Emacs support.
259; [[|Vim]] : Vim is a highly configurable text editor built to enable efficient and fast text editing. It is an improved version of the vi editor distributed with most UNIX systems. Vim comes with generic Lisp (and therefore Scheme) editing capabilities out of the box. [[|Here are a few tips on the matter]]. TODO: the chicken community is in the process of putting together a common set of extensions for editing Chicken with Vim
261TODO: other editors? Slick? Multi-Edit? Visual Studio? Eclipse? TextMate?  jEdit? some other editor? Please fill in anything you know about. Has somebody done a SchemeScript for Chicken?
263TODO: any decent editor with proportional font and automatic Scheme-style indenting? I know it sounds insane, but proportional font Scheme code in printed books looks so nice
265=== Using the interpreter
267In the rest of this chapter, we'll assume that you are using a
268terminal window.
270To invoke the interpreter, you use the {{csi}} command.
272 $ csi
274 (c)2000-2007 Felix L. Winkelmann
275 (c)2008 The Chicken Team
276 Version 3.0.1 - macosx-unix-gnu-x86    [ manyargs dload ptables applyhook ]
277 SVN rev. 8489  compiled 2008-02-15 on argyre.local (Darwin)
278 #;1>
280This brings up a brief banner, and then the prompt. You can use this
281pretty much like any other Scheme system, e.g.,
283 #;1> (define (twice f) (lambda (x) (f (f x))))
284 #;2> ((twice (lambda (n) (* n 10))) 3)
285 300
287Suppose  we have already created a file {{fact.scm}} containing a
288function definition.
290 (define (fact n)
291   (if (= n 0)
292       1
293       (* n (fact (- n 1)))))
295We can now load this file and try out the function.
297 #;3> (load "fact.scm")
298 ; loading fact.scm ...
299 #;4> (fact 3)
300 6
302The '''read-eval-print loop''' ('''REPL''') is the component of the
303Scheme system that ''reads'' a Scheme expression, ''eval''uates it,
304and ''prints'' out the result. The REPL's prompt can be customized
305(TODO: xref),
306but the default prompt, showing the number of the form, is quite
309The REPL also supports debugging commands:
310input lines beginning with a {{,}} (comma) are treated as special
311commands. (See the [[Using the interpreter#toplevel-commands|full list]].) We can
312'''trace''' {{fact}} to see how it works.
314 #;5> ,tr fact
315 #;5> (fact 3)
316 |(fact 3)
317 | (fact 2)
318 |  (fact 1)
319 |   (fact 0)
320 |   fact -> 1
321 |  fact -> 1
322 | fact -> 2
323 |fact -> 6
324 6
326The command number didn't increment, because the {{tr}} command isn't
327actually a Scheme ''form''.
329==== Scripts
331You can use the interpreter to run a Scheme program from the command
332line. Here we create a program that does a quick search-and-replace on
333an input file; the arguments are a regular expression and a
334replacement string.
336 $ cat quickrep.dat
337 xyzabcghi
338 abxawxcgh
339 foonly
340 $ csi -ss quickrep.scm <quickrep.dat 'a.*c' A
341 xyzAghi
342 Agh
343 foonly
345The {{-ss}} option sets several options that work smoothly together to
346execute a script. You can make the command directly executable from
347the shell by inserting a `[[Using the interpreter#writing-scheme-scripts|shebang line]]' at the beginning of the
350{{regex}}, the regular expression library, is one of the libraries
351included with Chicken.
353 (use regex)
354 (define (process-line line re rplc)
355   (string-substitute re rplc line 'all))
356 (define (quickrep re rplc)
357   (let ((line (read-line)))
358     (if (not (eof-object? line))
359         (begin
360           (display (process-line line re rplc))
361           (newline)
362           (quickrep re rplc)))))
363 ;;; Does a lousy job of error checking!
364 (define (main args)
365   (quickrep (regexp (car args)) (cadr args)))
367The {{-ss}} option arranges to call a procedure named {{main}}, with
368the command line arguments, packed in a list, as its arguments. (There
369are a number of ways this program could be made more idiomatic Chicken
370Scheme, see the rest of the manual for details.)
372=== The compiler
374There are several reasons you might want to compile your code.
376* Compiled code executes substantially faster than interpreted
377  code.
378* You might want to deploy an application onto machines where the
379  users aren't expected to have Chicken installed: compiled
380  applications can be self-contained.
382The Chicken compiler is provided as the command {{chicken}}, but in
383almost all cases, you will want to use the {{csc}} command
384instead. {{csc}} is a convenient driver that automates compiling
385Scheme programs into C, compiling C code into object code, and linking
386the results into an executable file. (Note: in a Windows environment
387with Visual Studio, you may find that {{csc}} refers to Microsoft's
388C# compiler. There are a number of ways of sorting this out, of which
389the simplest is to rename one of the two tools, and/or to
390organize your {{PATH}} according to the task at hand.)
392Compiled code can be intermixed with interpreted code on systems that
393support dynamic loading, which includes modern versions of *BSD,
394Linux, Mac OS X, Solaris, and Windows.
396We can compile our factorial function, producing a file named
397{{}} (`shared object' in Linux-ese, the same file type is used
398in OS X and Windows).
400 chicken$ csc -dynamic fact.scm
401 chicken$ csi -quiet
402 #;1> (load "")
403 ; loading ...
404 #;2> (fact 6)
405 720
407On any system, we can just compile a program directly into an
408executable. Here's a program that tells you whether its argument is a
411 (define (palindrome? x)
412   (define (check left right)
413     (if (>= left right)
414         #t
415         (and (char=? (string-ref x left) (string-ref x right))
416              (check (add1 left) (sub1 right)))))
417   (check 0 (sub1 (string-length x))))
418 (let ((arg (car (command-line-arguments))))
419   (display
420    (string-append arg
421                   (if (palindrome? arg)
422                       " is a palindrome\n"
423                       " isn't a palindrome\n"))))
425We can compile this program using {{csc}}, creating an executable
426named {{palindrome}}.
428 $ csc -o palindrome palindrome.scm
429 $ ./palindrome level
430 level is a palindrome
431 $ ./palindrome liver
432 liver isn't a palindrome
434Chicken supports separate compilation, using some extensions to
435Scheme. Let's divide our palindrome program into a library module
436({{pal-proc.scm}}) and a client module ({{pal-user.scm}}).
438Here's the external library. We {{declare}} that {{pal-proc}} is a
439`unit', which is the basis of separately-compiled modules in
440Chicken. (Units deal with separate compilation, but don't involve
441separated namespaces; namespaced module systems are available as
444 ;;; Library pal-proc.scm
445 (declare (unit pal-proc))
446 (define (palindrome? x)
447   (define (check left right)
448     (if (>= left right)
449         #t
450         (and (char=? (string-ref x left) (string-ref x right))
451              (check (add1 left) (sub1 right)))))
452   (check 0 (sub1 (string-length x))))
454Next we have some  client code that `uses' this separately-compiled
457 ;;; Client pal-user.scm
458 (declare (uses pal-proc))
459 (let ((arg (car (command-line-arguments))))
460   (display
461    (string-append arg
462                   (if (palindrome? arg)
463                       " is a palindrome\n"
464                       " isn't a palindrome\n"))))
466Now we can compile and link everything together. (We show the compile
467and link operations separately, but they can of course be combined
468into one command.)
470 $ csc -c pal-proc.scm
471 $ csc -c pal-user.scm
472 $ csc -o pal-separate pal-proc.o pal-user.o
473 $ ./pal-separate level
474 level is a palindrome
476=== Installing an egg
478Installing eggs is quite straightforward on systems that support
479dynamic loading (again, that would include *BSD, Linux, Mac OS X,
480Solaris, and Windows).  The command {{chicken-setup}} will fetch an
481egg from the master Chicken repository, and install it on your local
484In this example, we install the {{uri}} egg, for parsing Uniform
485Resource Identifiers. The installation produces a lot of output, which
486we have edited for space reasons.
488 $ chicken-setup uri
490 The extension uri does not exist.
491 Do you want to download it ? (yes/no/abort) [yes] yes
492 downloading uri.egg from ( eggs/3 80)
493   gzip -d -c ../uri.egg | tar xf -
494 .  /Users/vmanis/local/bin/csc -feature compiling-extension
495      -s -O2 -d1 uri.scm -o -check-imports -emit-exports uri.exports
496 ... (lots of stuff elided)
497 .  rm -fr /Users/vmanis/project/chicken/uri.egg
499First, {{chicken-setup}} asks us if we want to download the egg. It
500then uncompresses the egg, compiles the code, and installs the egg in
501the local Chicken repository.
503Now we can use our new egg.
505 #;1> (use uri)
506 ; loading /Users/vmanis/local/lib/chicken/3/ ...
507 ; loading /Users/vmanis/local/lib/chicken/3/ ...
508 ; loading /Users/vmanis/local/lib/chicken/3/ ...
509 ; loading /Users/vmanis/local/lib/chicken/3/ ...
510 ; loading /Users/vmanis/local/lib/chicken/3/ ...
511 ; loading /Users/vmanis/local/lib/chicken/3/ ...
512 #;2> (uri-host (uri ""))
513 ""
515=== Accessing C libraries
517Because Chicken compiles to C, and because a foreign function
518interface is built into the compiler, interfacing to a C library is
519quite straightforward. This means that nearly any facility available
520on the host system is accessible from Chicken, with more or less
523Let's create a simple C library, to demonstrate how this
524works. Here we have a function that will compute and return the '''n'''th
525Fibonacci number. (This isn't a particularly good use of C here,
526because we could write this function just as easily in Scheme, but a
527real example would take far too much space here.)
529 int fib(int n) {
530   int prev = 0, curr = 1;
531   int next;
532   int i;
533   for (i = 0; i < n; i++) {
534     next = prev + curr;
535     prev = curr;
536     curr = next;
537   }
538   return curr;
539 }
541Now we can call this function from Chicken.
543 #>
544   extern fib(int n);
545 <#
546 (define xfib (foreign-lambda int "fib" int))
547 (do ((i 0 (+ i 1))) ((> i 10))
548   (printf "~A " (xfib i)))
549 (newline)
551The syntax {{#>...<#}} allows you to include literal C (typically
552external declarations) in your Chicken code. We access {{fib}} by
553defining a {{foreign-lambda}} for it, in this case saying that the
554function takes one integer argument (the {{int}} after the function
555name), and that it returns an integer result (the {{int}} before.) Now we can invoke
556{{xfib}} as though it were an ordinary Scheme function.
558 $ gcc -c fib.c
559 $ csc -o fib-user fib.o fib-user.scm
560 $ ./fib-user
561 0 1 1 2 3 5 8 13 21 34 55
563Those who are interfacing to substantial C libraries should consider using the
564easyffi egg, or SWIG.
Note: See TracBrowser for help on using the repository browser.