summaryrefslogtreecommitdiff
path: root/chicken-5.org
blob: 3169f2801edcd6a432d56c9fe895b4ba4589f6ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
*          What's cool about CHICKEN 5?
#+STARTUP: inlineimages
                     [[./chicken-logo.png]]


**                   Peter Bex

                    T-DOSE
                   Eindhoven
                May 11th, 2019

* Who am I?

** Peter Bex

*** Work: codeyellow.nl  
  We're hiring! Talk to me or Burhan later
*** Freelancing on the side: pebble-software.nl
*** Blog (mostly about CHICKEN): more-magic.net

*** Fell in love with Scheme at university, ~2002
*** CHICKEN user since 2003
  eggs: postgresql, spiffy, http-client, ...
*** CHICKEN core contributor since late 2009
  regex engine, macros, garbage collector, ...
* Outline

** Short intro to Scheme

** Short intro to CHICKEN

** What's new in CHICKEN 5

* Scheme: intro
 Dialect of /LISP/, a functional language (impure)
 LISP is from 1958, Scheme 1975; still here!

 Other Lisps: Common Lisp, Emacs Lisp, Clojure, ...
 Not for everyone -> Love it or hate it!
#+BEGIN_SRC scheme
 (display "Hello, world!\n") ;; Obligatory

 (define (fib n) ;; Fibonacci, because why not
   (if (<= n 2)
       1
       (+ (fib (- n 1))
          (fib (- n 2)))))
#+END_SRC

* Scheme: philosophy

** Super minimal but powerful language
 User is an equal to the makers of the language

 Very few things *need* "special" support

 Advantages of small extensible core:
 - ease of understanding (fits in your head)
 - embeddability
 - facilitates experimental implementations
 - timelessness; e.g. classes can just be a
   library. C => C++/ObjC, Pascal => Object Pascal
   but Scheme => Scheme
* Scheme superpowers: macros

** Fully hygienic macro system
#+BEGIN_SRC scheme
 (define-syntax swap!
   (syntax-rules ()
     ((_ a b) (let ((x a))
                (set! a b)
                (set! b x)))))

 ;; Does the right thing, i.e. print 1:
 (let ((x 1) (y 2))
   (swap! x y)
   (display y))
#+END_SRC

* Scheme superpowers: closures

** Lexical scoping (from definition, not from call)

 Gives us *closures* (now ubiquitous; first LISP)
#+BEGIN_SRC scheme
 (define (make-counter)
   (let ((val 0))
     (lambda () ; Anonymous fn, closes over "val"
       (set! val (+ val 1))
       val)))
 (define c (make-counter))
 (c) ; 1
 (c) ; 2
#+END_SRC

* Scheme superpowers: continuations

** Continuations

 Can be used to implement exceptions, co-routines,
 serialize full program state, etc etc

 Tail calls "for free" [Lambda the Ultimate Goto]
 Tail recursion is /required by the spec/!
 Bad implementations often conveniently ignore it

 Deep recursion is okay; just eats up more heap

 See also Stackless Python

* Scheme superpowers: numeric tower

** Numeric tower

 Systematic support for /all the numbers/
 Spec defines system, allows subset (see later)
#+BEGIN_SRC scheme
 1           ;; exact integer
 0.2         ;; inexact real number
 1/5         ;; exact rational number
 1+2i        ;; exact complex number
 0.5+0.25i   ;; inexact complex number
 1/2+3/4i    ;; ALSO an exact complex number
 (+ 3/4 1/2) ;; yes, this returns 5/4
#+END_SRC

* Scheme superpowers: REPL

** Read-Eval-Print-Loop

*** Interpreter prompt allows you to explore

  Type an expression  (read)
  Have it evaluated   (eval)
  See the result      (print)
  Go to start         (loop)

  Most interpreters do this now.
  Done right, can shape your way of thinking.
  LISP machines did it best! (SLIME/Geiser)

* CHICKEN: intro

 /Implementation/ of Scheme (one of the few
 serious ones)

 Based on Henry Baker's [Cheney On The MTA] idea:
 Use C stack for initial heap (nursery)

 Been around since 2000.

 Compiles to C.  Uses GCC, clang, ... to generate
 machine code, so: very portable

* CHICKEN: C interop

 Interop with C is trivial; just drop down to it
#+BEGIN_SRC scheme
 (import (chicken foreign))

 (foreign-declare "#include <math.h>")

 (define sin-pi
   (foreign-lambda* double ((double x))
     "double val = x * M_PI;"
     "C_return(sin(val));"))

 (display (sin-pi 0.5)) ;; prints 1.0
#+END_SRC

* CHICKEN: Community

** Perhaps the best "feature" of CHICKEN

** Super friendly and helpful
*** IRC: #chicken on Freenode
*** Mailing list: chicken-users@nongnu.org
*** Regular in-person meetups

** Produced lots of libraries ("eggs")
*** Generally of high quality
*** Small community, responsive authors

* CHICKEN 5: Overhaul of module system

** Most user-visible change
 Old CHICKENs had such memorable module names as
 "utils" and "extras"... Most was in "chicken"

 Hard to remember, inconsistent

 Large libraries (but few imports needed)

** Naming scheme (haha) inspired by R7RS
 (scheme base) => (chicken base)
 (scheme file) => (chicken file) etc
 others: (chicken posix), (chicken foreign) etc
* CHICKEN 5: Overhaul of module system; examples

** A few good examples to give you an idea:
 "sort" procedure in CHICKEN 4: data-structures
 "sort" procedure in CHICKEN 5: (chicken sort)

 "symbol-append" in CHICKEN 4: library?! chicken
 "symbol-append" in CHICKEN 5: (chicken base)

 "->string" in CHICKEN 4: data-structures
 "->string" in CHICKEN 5: (chicken string)

 "read-line" in CHICKEN 4: extras
 "read-line" in CHICKEN 5: (chicken io)

* CHICKEN 5: Numeric tower; old situation

** Older CHICKENs had only "fixnums" and "flonums"

*** Quite fast (compile to ~standard C float/int)

*** Never felt very "complete" or "serious" to me
 LISP/Scheme is awesome, dammit!

*** Issues with C FFI
 Only 62 (or 30) bits available for ints; 
 precision loss

** So, I made it my mission to fix this

* CHICKEN 5: Numeric tower; new situation

** Most important: large integers ("bignums")
 C FFI finally supports /size_t/ and friends!

** Exact rational numbers are nice too;
 Calculations will always be exact

** Complex numbers mostly for completeness
 Not that many use-cases(?)

** Pretty fast, but a bit slower than CHICKEN 4
 Mostly due to pre-allocation.  Working on it!
*** Enough self-promotion, on to other features
* CHICKEN 5: static compilation

** Great for binary deployment / distribution
 No need to mess around with "virtualenvs" etc

 Just plop the binary onto a server; done

 Killer feature of Go, for example

** CHICKEN always supported static compilation!
 Sucked for eggs (nobody tests it)

** Solution: declarative egg format
 Makes it harder to mess up
* CHICKEN 5: static compilation; old situation

** Old (imperative) .setup-file format:
#+BEGIN_SRC scheme
 (compile -s ncurses.scm -L -lncurses -j ncurses)
 (compile -s ncurses.import.scm)
 (compile -c ncurses.scm -L -lncurses 
          -unit ncurses) ; Almost nobody did this

 (install-extension
  'ncurses
  '("ncurses.o" "ncurses.so" "ncurses.import.so")
  '((version "1.6")
    (static "ncurses.o"))) ; And this
#+END_SRC scheme

* CHICKEN 5: static compilation; new situation

** New (declarative) .egg-file format:
#+BEGIN_SRC scheme
 ((synopsis "An interface to ncurses")
  (category ui)
  (license "BSD")
  (build-dependencies bind)
  (author "felix winkelmann")
  (maintainer "The CHICKEN Team")
  ;; This replaces the .setup file:
  (components (extension ncurses
                (link-options -L -lncurses))))
#+END_SRC scheme

* CHICKEN 5: declarative egg files; sh scripts

** Generates sh scripts (batch files on Windows)
#+BEGIN_SRC sh
 chicken-do ncurses.so ncurses.import.scm : \
  ncurses.scm ncurses.egg : \
  csc -host -J -s -setup-mode -I . -C -I. \
  -L -lncurses ncurses.scm -o ncurses.so

 chicken-do ncurses.static.o ncurses.link : \
  ncurses.scm ncurses.egg : \
  csc -setup-mode -static -I . -emit-link-file \
  ncurses.link -host -c -unit ncurses \
  -C -I. ncurses.scm -o ncurses.static.o
#+END_SRC

* CHICKEN 5: declarative egg files; advantages

** Advantages of declarative format

 Static linking is *always* built-in

 Cross-compilation is made easier

 Support for new features can be retroactively
 added to all eggs

 Security

 Less typing!

* CHICKEN 5: binary reproducible builds

** Both CHICKEN itself and eggs

 Made output of Scheme to C conversion stable
 C to binary is left to the C tool chain

** Gives us (wider FOSS community) verifiability

 See also [reproducible-builds.org], Debian, ...

** Nice bonus: Faster builds with ccache

 Generated C is identical!

* CHICKEN 5: Odds and ends

** Proper random number generator
*** Not using libc rand() anymore, but WELL512
*** Seeded from OS entropy pool (/dev/urandom)

** Symbols are garbage collected
*** Prevents obscure resource consumption attack

** Removed bloat
*** Several core functionalities have been
*** moved out of core into eggs

** Internals have been restructured/cleaned up

* Final words

** Most important eggs (~300) have been ported!
 CHICKEN 5 is recommended for new code

** Start porting your CHICKEN 4 code
 How-to guide: wiki.call-cc.org/porting-c4-to-c5

* Thank you!
                     [[./chicken-logo.png]]

**                 CHICKEN website
                   call-cc.org

**                   IRC channel
              #chicken on Freenode

**                     My blog
                 more-magic.net