source: project/wiki/new-chapter1 @ 9001

Last change on this file since 9001 was 9001, checked in by svnwiki, 12 years ago

Changes applied for Vincent Manis ( through svnwiki:

Corrected some typos, and other minor things.

File size: 22.0 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). If
238you install the {{readline}} egg (TODO: insert xref), you have all the
239benefits of command history and reentry, Emacs or vi-compatible line
240editing, and customization.
242You will need a text editor that knows Scheme; it's just too painful
243with editors that don't do parenthesis matching and proper
244indentation. Some editors allow you to execute Scheme code directly in
245the editor. This makes programming very interactive: you can type in a
246function and then try it right away. This feature is very highly
249As programmers have very specific tastes about editors, the editors
250listed here are shown in alphabetic order. We aren't about to tell you
251which editor to use, and there may be editors not shown here that
252might satisfy your needs.
254* Emacs ([[]]) is 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.
256* 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.
258* {{vim}} ([[]]) is a descendant of the venerable {{vi}} editor first included with Berkeley versions of Unix.  TODO: say more about using vim with Chicken, all I know about vi is that you can get out by typing :q! .
260TODO: 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?
262=== Using the interpreter
264In the rest of this chapter, we'll assume that you are using a
265terminal window.
267To invoke the interpreter, you use the {{csi}} command.
269 $ csi
271 (c)2000-2007 Felix L. Winkelmann
272 (c)2008 The Chicken Team
273 Version 3.0.1 - macosx-unix-gnu-x86    [ manyargs dload ptables applyhook ]
274 SVN rev. 8489  compiled 2008-02-15 on argyre.local (Darwin)
275 #;1>
277This brings up a brief banner, and then the prompt. You can use this
278pretty much like any other Scheme system, e.g.,
280 #;1> (define (twice f) (lambda (x) (f (f x))))
281 #;2> ((twice (lambda (n) (* n 10))) 3)
282 300
284Suppose  we have already created a file {{fact.scm}} containing a
285function definition.
287 (define (fact n)
288   (if (= n 0)
289       1
290       (* n (fact (- n 1)))))
292We can now load this file and try out the function.
294 #;3> (load "fact.scm")
295 ; loading fact.scm ...
296 #;4> (fact 3)
297 6
299The '''read-eval-print loop''' ('''REPL''') is the component of the
300Scheme system that ''reads'' a Scheme expression, ''eval''uates it,
301and ''prints'' out the result. The REPL's prompt can be customized
302(TODO: xref),
303but the default prompt, showing the number of the form, is quite
306The REPL also supports debugging commands:
307input lines beginning with a {{,}} (comma) are treated as special
308commands. (TODO: insert cross-reference to full list) We can
309'''trace''' {{fact}} to see how it works.
311 #;5> ,tr fact
312 #;5> (fact 3)
313 |(fact 3)
314 | (fact 2)
315 |  (fact 1)
316 |   (fact 0)
317 |   fact -> 1
318 |  fact -> 1
319 | fact -> 2
320 |fact -> 6
321 6
323The command number didn't increment, because the {{tr}} command isn't
324actually a Scheme ''form''.
326==== Scripts
328You can use the interpreter to run a Scheme program from the command
329line. Here we create a program that does a quick search-and-replace on
330an input file; the arguments are a regular expression and a
331replacement string.
333 $ cat quickrep.dat
334 xyzabcghi
335 abxawxcgh
336 foonly
337 $ csi -ss quickrep.scm <quickrep.dat 'a.*c' A
338 xyzAghi
339 Agh
340 foonly
342The {{-ss}} option sets several options that work smoothly together to
343execute a script. You can make the command directly executable from
344the shell by inserting a `shebang line' at the beginning of the
345program (TODO: insert xref).
347{{regex}}, the regular expression library, is one of the libraries
348included with Chicken.
350 (use regex)
351 (define (process-line line re rplc)
352   (string-substitute re rplc line 'all))
353 (define (quickrep re rplc)
354   (let ((line (read-line)))
355     (if (not (eof-object? line))
356         (begin
357           (display (process-line line re rplc))
358           (newline)
359           (quickrep re rplc)))))
360 ;;; Does a lousy job of error checking!
361 (define (main args)
362   (quickrep (regexp (car args)) (cadr args)))
364The {{-ss}} option arranges to call a procedure named {{main}}, with
365the command line arguments, packed in a list, as its arguments. (There
366are a number of ways this program could be made more idiomatic Chicken
367Scheme, see the rest of the manual for details.)
369=== The compiler
371There are several reasons you might want to compile your code.
373* Compiled code executes substantially more quickly than interpreted
374  code.
375* You might want to deploy an application onto machines where the
376  users aren't expected to have Chicken installed: compiled
377  applications can be self-contained.
379The Chicken compiler is provided as the command {{chicken}}, but in
380almost all cases, you will want to use the {{csc}} command
381instead. {{csc}} is a convenient driver that automates compiling
382Scheme programs into C, compiling C code into object code, and linking
383the results into an executable file. (Note: in a Windows environment
384with Visual Studio, you may find that {{csc}} refers to Microsoft's
385C# compiler. There are a number of ways of sorting this out, of which
386the simplest is to make a Windows batch file named {{cshc.bat}} which
387invokes {{csc}} using its absolute path name, and
388organize your {{PATH}} accordingly.)
390Compiled code can be intermixed with interpreted code on systems that
391support dynamic loading, which includes modern versions of *BSD,
392Linux, Mac OS X, Solaris, and Windows.
394We can compile our factorial function, producing a file named
395{{}} (`shared object' in Linux-ese, the same file type is used
396in OS X and Windows).
398 chicken$ csc -dynamic fact.scm
399 chicken$ csi -quiet
400 #;1> (load "")
401 ; loading ...
402 #;2> (fact 6)
403 720
405On any system, we can just compile a program directly into an
406executable. Here's a program that tells you whether its argument is a
409 (define (palindrome? x)
410   (define (check left right)
411     (if (>= left right)
412         #t
413         (and (char=? (string-ref x left) (string-ref x right))
414              (check (add1 left) (sub1 right)))))
415   (check 0 (sub1 (string-length x))))
416 (let ((arg (car (command-line-arguments))))
417   (display
418    (string-append arg
419                   (if (palindrome? arg)
420                       " is a palindrome\n"
421                       " isn't a palindrome\n"))))
423We can compile this program using {{csc}}, creating an executable
424named {{palindrome}}.
426 $ csc -o palindrome palindrome.scm
427 $ ./palindrome level
428 level is a palindrome
429 $ ./palindrome liver
430 liver isn't a palindrome
432Chicken supports separate compilation, using some extensions to
433Scheme. Let's divide our palindrome program into a library module
434({{pal-proc.scm}}) and a client module ({{pal-user.scm}}).
436Here's the external library. We {{declare}} that {{pal-proc}} is a
437`unit', which is the basis of separately-compiled modules in
438Chicken. (Units deal with separate compilation, but don't involve
439separated namespaces; namespaced module systems are available as
442 ;;; Library pal-proc.scm
443 (declare (unit pal-proc))
444 (define (palindrome? x)
445   (define (check left right)
446     (if (>= left right)
447         #t
448         (and (char=? (string-ref x left) (string-ref x right))
449              (check (add1 left) (sub1 right)))))
450   (check 0 (sub1 (string-length x))))
452Next we have some  client code that `uses' this separately-compiled
455 ;;; Client pal-user.scm
456 (declare (uses pal-proc))
457 (let ((arg (car (command-line-arguments))))
458   (display
459    (string-append arg
460                   (if (palindrome? arg)
461                       " is a palindrome\n"
462                       " isn't a palindrome\n"))))
464Now we can compile and link everything together. (We show the compile
465and link operations separately, but they can of course be combined
466into one command.)
468 $ csc -c pal-proc.scm
469 $ csc -c pal-user.scm
470 $ csc -o pal-separate pal-proc.o pal-user.o
471 $ ./pal-separate level
472 level is a palindrome
474=== Installing an egg
476Installing eggs is quite straightforward on systems that support
477dynamic loading (again, that would include *BSD, Linux, Mac OS X,
478Solaris, and Windows).  The command {{chicken-setup}} will fetch an
479egg from the master Chicken repository, and install it on your local
482In this example, we install the {{uri}} egg, for parsing Uniform
483Resource Identifiers. The installation produces a lot of output, which
484we have edited for space reasons.
486 $ chicken-setup uri
488 The extension uri does not exist.
489 Do you want to download it ? (yes/no/abort) [yes] yes
490 downloading uri.egg from ( eggs/3 80)
491   gzip -d -c ../uri.egg | tar xf -
492 .  /Users/vmanis/local/bin/csc -feature compiling-extension
493      -s -O2 -d1 uri.scm -o -check-imports -emit-exports uri.exports
494 ... (lots of stuff elided)
495 .  rm -fr /Users/vmanis/project/chicken/uri.egg
497First, {{chicken-setup}} asks us if we want to download the egg. It
498then uncompresses the egg, compiles the code, and installs the egg in
499the local Chicken repository.
501Now we can use our new egg.
503 #;1> (use uri)
504 ; loading /Users/vmanis/local/lib/chicken/3/ ...
505 ; loading /Users/vmanis/local/lib/chicken/3/ ...
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 #;2> (uri-host (uri ""))
511 ""
513=== Accessing C libraries
515Because Chicken compiles to C, and because a foreign function
516interface is built into the compiler, interfacing to a C library is
517quite straightforward. This means that nearly any facility available
518on the host system is accessible from Chicken, with more or less
521Let's create a simple C library, to demonstrate how this
522works. Here we have a function that will compute and return the '''n'''th
523Fibonacci number. (This isn't a particularly good use of C here,
524because we could write this function just as easily in Scheme, but a
525real example would take far too much space here.)
527 int fib(int n) {
528   int prev = 0, curr = 1;
529   int next;
530   int i;
531   for (i = 0; i < n; i++) {
532     next = prev + curr;
533     prev = curr;
534     curr = next;
535   }
536   return curr;
537 }
539Now we can call this function from Chicken.
541 #>
542   extern fib(int n);
543 <#
544 (define xfib (foreign-lambda int "fib" int))
545 (do ((i 0 (+ i 1))) ((> i 10))
546   (printf "~A " (xfib i)))
547 (newline)
549The syntax {{#>...<#}} allows you to include literal C (typically
550external declarations) in your Chicken code. We access {{fib}} by
551defining a {{foreign-lambda}} for it, in this case saying that the
552function takes one integer argument (the {{int}} after the function
553name), and that it returns an integer result. Now we can invoke
554{{xfib}} as though it were an ordinary Scheme function.
556 $ gcc -c fib.c
557 $ csc -o fib-user fib.o fib-user.scm
558 $ ./fib-user
559 0 1 1 2 3 5 8 13 21 34 55
561Those who are interfacing to substantial C libraries should consider using the
562easyffi egg, or SWIG.
Note: See TracBrowser for help on using the repository browser.