1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs combinators constructors eval help.markup kernel
4 multiline namespaces parser sequences sequences.private slides
5 vocabs.refresh words fry ;
8 CONSTANT: tc-lisp-slides
11 { $url "http://factorcode.org" }
12 "Development started in 2003"
13 "Open source (BSD license)"
14 "Influenced by Forth, Lisp, and Smalltalk"
15 "Blurs the line between language and library"
16 "Interactive development"
18 { $slide "First, some examples"
19 { $code "3 weeks ago noon monday ." }
20 { $code "USE: roman 2009 >roman ." }
21 { $code """: average ( seq -- x )
22 [ sum ] [ length ] bi / ;""" }
23 { $code "1 miles [ km ] undo >float ." }
24 { $code "[ readln eval>string print t ] loop" }
26 { $slide "XML Literals"
28 """USING: splitting xml.writer xml.syntax ;
29 { "one" "two" "three" }
30 [ [XML <item><-></item> XML] ] map
31 <XML <doc><-></doc> XML> pprint-xml"""
34 { $slide "Differences between Factor and Lisp"
35 "Single-implementation language"
36 "Less nesting, shorter word length"
37 { "Dynamic reloading of code from files with " { $link refresh-all } }
38 "More generic protocols -- sequences, assocs, streams"
40 "No standard for the language"
41 "Evaluates left to right"
43 { $slide "Terminology"
44 { "Words - functions" }
45 { "Vocabularies - collections of code in the same namespace" }
46 { "Quotations - blocks of code" { $code "[ dup reverse append ]" } }
47 { "Combinators - higher order functions" }
48 { "Static stack effect - known stack effect at compile-time" }
50 { $slide "Defining a word"
51 "Defined at parse time"
52 "Parts: name, stack effect, definition"
53 "Composed of tokens separated by whitespace"
54 { $code ": palindrome? ( string -- ? ) dup reverse = ;" }
56 { $slide "Non-static stack effect"
57 "Not a good practice, nor useful"
58 "Not compiled by the optimizing compiler"
59 { $code "100 iota [ ] each" }
61 { $slide "Module system"
62 "Code divided up into vocabulary roots"
63 "core/ -- just enough code to bootstrap Factor"
64 "basis/ -- optimizing compiler, the UI, tools, libraries"
65 "extra/ -- demos, unpolished code, experiments"
66 "work/ -- your works in progress"
68 { $slide "Module system (part 2)"
69 "Each vocabulary corresponds to a directory on disk, with documentation and test files"
70 { "Code for the " { $snippet "math" } " vocabulary: " { $snippet "~/factor/core/math/math.factor" } }
71 { "Documentation for the " { $snippet "math" } " vocabulary: " { $snippet "~/factor/core/math/math-docs.factor" } }
72 { "Unit tests for the " { $snippet "math" } " vocabulary: " { $snippet " ~/factor/core/math/math-tests.factor" } }
74 { $slide "Using a library"
75 "Each file starts with a USING: list"
76 "To use a library, simply include it in this list"
77 "Refreshing code loads dependencies correctly"
79 { $slide "Object system"
81 { "We define generic words that operate on the top of the stack with " { $link POSTPONE: GENERIC: } " or on an implicit parameter with " { $link POSTPONE: HOOK: } }
83 { $slide "Object system example: shape protocol"
84 "In ~/factor/work/shapes/shapes.factor"
87 GENERIC: area ( shape -- x )
88 GENERIC: perimeter ( shape -- x )"""
91 { $slide "Implementing the shape protocol: circles"
92 "In ~/factor/work/shapes/circle/circle.factor"
93 { $code """USING: shapes constructors math
97 TUPLE: circle radius ;
98 CONSTRUCTOR: <circle> circle ( radius -- obj ) ;
99 M: circle area radius>> sq pi * ;
100 M: circle perimeter radius>> pi * 2 * ;"""
103 { $slide "Dynamic variables"
104 "Implemented as a stack of hashtables"
105 { "Useful words are " { $link get } ", " { $link set } }
106 "Input, output, error streams are stored in dynamic variables"
107 { $code """"Today is the first day of the rest of your life."
110 ] with-string-reader"""
113 { $slide "The global namespace"
114 "The global namespace is just the namespace at the bottom of the namespace stack"
115 { "Useful words are " { $link get-global } ", " { $link set-global } }
116 "Factor idiom for changing a particular namespace"
117 { $code """SYMBOL: king
118 global [ "Henry VIII" king set ] with-variables"""
120 { $code "with-scope" }
121 { $code "namestack" }
124 "Dispatch on a dynamic variable"
125 { $code """HOOK: computer-name os ( -- string )
126 M: macosx computer-name uname first ;
127 macosx \ os set-global
131 { $slide "Interpolate"
132 "Replaces variables in a string"
139 "Sup ${name}, we heard you liked ${noun}, so we put ${noun} on your car so you can ${verb1} while you ${verb2}."
141 ] with-string-writer print """
144 { $slide "Sequence protocol"
145 "All sequences obey a protocol of generics"
146 { "Is an object a " { $link sequence? } }
147 { "Getting the " { $link length } }
148 { "Accessing the " { $link nth } " element" }
149 { "Setting an element - " { $link set-nth } }
151 { $slide "Examples of sequences in Factor"
153 "Vectors are mutable and growable"
154 { "Arrays " { $code "{ \"abc\" \"def\" 50 }" } }
155 { "Vectors " { $code "V{ \"abc\" \"def\" 50 }" } }
156 { "Byte-arrays " { $code "B{ 1 2 3 }" } }
157 { "Byte-vectors " { $code "BV{ 11 22 33 }" } }
159 { $slide "Specialized arrays and vectors"
160 { "Specialized int arrays " { $code "int-array{ -20 -30 40 }" } }
161 { "Specialized uint arrays " { $code "uint-array{ 20 30 40 }" } }
162 { "Specialized float vectors " { $code "float-vector{ 20 30 40 }" } }
163 "35 others C-type arrays"
165 { $slide "Specialized arrays code"
166 "One line per array/vector"
167 { "In ~/factor/basis/specialized-arrays/float/float.factor"
168 { $code """<< "float" define-array >>""" }
170 { "In ~/factor/basis/specialized-vectors/float/float.factor"
171 { $code """<< "float" define-vector >>""" }
175 { $slide "Speciailzied arrays are implemented using functors"
177 "Eliminate boilerplate in ways other abstractions don't"
178 "Contains a definition section and a functor body"
179 "Uses the interpolate vocabulary"
181 { $slide "Functor for sorting"
183 """FUNCTOR: define-sorting ( NAME QUOT -- )
185 NAME<=> DEFINES ${NAME}<=>
186 NAME>=< DEFINES ${NAME}>=<
190 : NAME<=> ( obj1 obj2 -- <=> ) QUOT compare ;
191 : NAME>=< ( obj1 obj2 -- >=< )
192 NAME<=> invert-comparison ;
197 { $slide "Example of sorting functor"
198 { $code """USING: sorting.functor ;
199 << "length" [ length ] define-sorting >>"""
202 """{ { 1 2 3 } { 1 2 } { 1 } }
203 [ length<=> ] sort"""
206 { $slide "Combinators"
207 "Used to implement higher order functions (dataflow and control flow)"
208 "Compiler optimizes away quotations completely"
209 "Optimized code is just tight loops in registers"
210 "Most loops can be expressed with combinators or tail-recursion"
212 { $slide "Combinators that act on one value"
214 { $code "10 [ 1 - ] [ 1 + ] bi" }
216 { $code "10 [ 1 - ] [ 1 + ] [ 2 * ] tri" }
218 { $slide "Combinators that act on two values"
220 { $code "10 1 [ - ] [ + ] 2bi" }
222 { $code "10 20 [ 1 - ] [ 1 + ] bi*" }
224 { $code "5 9 [ sq ] bi@" }
226 { $slide "Sequence combinators"
229 { $code "{ 1 2 3 4 5 } [ sq . ] each" }
231 { $code "{ 1 2 3 4 5 } [ sq ] map" }
233 { $code "{ 1 2 3 4 5 } [ even? ] filter" }
235 { $slide "Multiple sequence combinators"
238 { $code "{ 1 2 3 } { 10 20 30 } [ + . ] 2each" }
240 { $code "{ 1 2 3 } { 10 20 30 } [ + ] 2map" }
242 { $slide "Control flow: if"
244 { $code """10 random dup even? [ 2 / ] [ 1 - ] if""" }
246 { $code """10 random dup even? [ 2 / ] when""" }
248 { $code """10 random dup even? [ 1 - ] unless""" }
250 { $slide "Control flow: case"
252 { $code """ERROR: not-possible obj ;
262 "Used to construct quotations"
263 { "'Holes', represented by " { $snippet "_" } " are filled left to right" }
264 { $code "10 4 '[ _ + ] call" }
265 { $code "3 4 '[ _ sq _ + ] call" }
268 "When data flow combinators and shuffle words are not enough"
269 "Name your input parameters"
270 "Used in about 1% of all words"
272 { $slide "Locals example"
273 "Area of a triangle using Heron's formula"
275 """:: area ( a b c -- x )
283 { $slide "Previous example without locals"
286 """: area ( a b c -- x )
288 [ '[ _ - ] tri@ ] [ neg ] bi
291 { $slide "More idiomatic version"
292 "But there's a trick: put the lengths in an array"
293 { $code """: v-n ( v n -- w ) '[ _ - ] map ;
296 [ 0 suffix ] [ sum 2 / ] bi
297 v-n product sqrt ;""" }
299 { $slide "Implementing an abstraction"
300 { "Suppose we want to get the price of the customer's first order, but any one of the steps along the way could be a nil value (" { $link f } " in Factor):" }
302 "dup [ orders>> ] when"
304 "dup [ price>> ] when"
307 { $slide "This is hard with mainstream syntax!"
309 """var customer = ...;
310 var orders = (customer == null ? null : customer.orders);
311 var order = (orders == null ? null : orders[0]);
312 var price = (order == null ? null : order.price);""" }
314 { $slide "An ad-hoc solution"
316 { $code "var price = customer.?orders.?[0].?price;" }
318 { $slide "Macros in Factor"
319 "Expand at compile-time"
320 "Return a quotation to be compiled"
321 "Can express non-static stack effects"
322 "Not as widely used as combinators, 60 macros so far"
323 { $code "{ 1 2 3 4 5 } 5 firstn" }
325 { $slide "A macro solution"
326 "Returns a quotation to the compiler"
327 "Constructed using map, fry, and concat"
328 { $code """MACRO: plox ( seq -- quot )
331 ] map [ ] concat-as ;"""
334 { $slide "Macro example"
335 "Return the caaar of a sequence"
336 { "Return " { $snippet f } " on failure" }
337 { $code """: caaar ( seq/f -- x/f )
344 { $code """{ { f } } caaar""" }
345 { $code """{ { { 1 2 3 } } } caaar""" }
347 { $slide "Smart combinators"
348 "Use stack checker to infer inputs and outputs"
349 "Even fewer uses than macros"
350 { $code "{ 1 10 20 34 } sum" }
351 { $code "[ 1 10 20 34 ] sum-outputs" }
352 { $code "[ 2 2 [ even? ] both? ] [ + ] [ - ] smart-if" }
357 { $code """: fib ( n -- x )
359 [ 1 - fib ] [ 2 - fib ] bi +
362 { $code "36 iota [ fib ] map ." }
364 { $slide "Memoized Fibonacci"
365 "Change one word and it's efficient"
366 { $code """MEMO: fib ( n -- x )
368 [ 1 - fib ] [ 2 - fib ] bi +
371 { $code "36 iota [ fib ] map ." }
373 { $slide "Destructors"
374 "Deterministic resource disposal"
375 "Any step can fail and we don't want to leak resources"
376 "We want to conditionally clean up sometimes -- if everything succeeds, we might wish to retain the buffer"
379 { $slide "Example in C"
384 if(!(*obj1 = malloc(256))) goto end;
385 if(!(*obj2 = malloc(256))) goto cleanup1;
386 ... work goes here...
387 cleanup2: free(*obj2);
388 cleanup1: free(*obj1);
393 { $slide "Example: allocating and disposing two buffers"
394 { $code """: do-stuff ( -- )
398 ... work goes here ...
399 ] with-destructors ;"""
402 { $slide "Example: allocating two buffers for later"
403 { $code """: do-stuff ( -- )
407 ... work goes here ...
408 ] with-destructors ;"""
411 { $slide "Example: disposing of an output port"
412 { $code """M: output-port dispose*
415 [ handle>> &dispose drop ]
416 [ buffer>> &dispose drop ]
418 [ handle>> shutdown ]
420 ] with-destructors ;"""
423 { $slide "Rapid application development"
424 "We lost the dice to Settlers of Catan: Cities and Knights"
425 "Two regular dice, one special die"
426 { $vocab-link "dice" }
428 { $slide "The essence of Factor"
429 "Nicely named words abstract away the stack, leaving readable code"
430 { $code """: surround ( seq left right -- seq' )
433 { $code """: glue ( left right middle -- seq' )
438 "a" """""""" surround
443 { $slide "C FFI demo"
444 "Easy to call C functions from Factor"
445 "Handles C structures, C types, callbacks"
446 "Used extensively in the Windows and Unix backends"
448 """FUNCTION: double pow ( double x, double y ) ;
452 { $slide "Windows win32 example"
454 """M: windows gmt-offset
455 ( -- hours minutes seconds )
456 "TIME_ZONE_INFORMATION" <c-object>
457 dup GetTimeZoneInformation {
458 { TIME_ZONE_ID_INVALID [
459 win32-error-string throw
461 { TIME_ZONE_ID_STANDARD [
462 TIME_ZONE_INFORMATION-Bias
464 } case neg 60 /mod 0 ;"""
467 { $slide "Struct and function"
468 { $code """C-STRUCT: TIME_ZONE_INFORMATION
470 { { "WCHAR" 32 } "StandardName" }
471 { "SYSTEMTIME" "StandardDate" }
472 { "LONG" "StandardBias" }
473 { { "WCHAR" 32 } "DaylightName" }
474 { "SYSTEMTIME" "DaylightDate" }
475 { "LONG" "DaylightBias" } ;"""
477 { $code """FUNCTION: DWORD GetTimeZoneInformation (
478 LPTIME_ZONE_INFORMATION
479 lpTimeZoneInformation
485 { $code """IMPORT: NSAlert [
488 "Raptor" <CFString> &CFRelease
491 "Look out!" <CFString> &CFRelease
492 -> setInformativeText:
493 ] tri -> runModal drop
494 ] with-destructors"""
497 { $slide "Deployment demo"
498 "Vocabularies can be deployed"
499 "Standalone .app on Mac"
500 "An executable and dll on Windows"
501 { $vocab-link "webkit-demo" }
503 { $slide "Interesting programs"
504 { $vocab-link "terrain" }
505 { $vocab-link "gpu.demos.raytrace" }
506 { $vocab-link "gpu.demos.bunny" }
508 { $slide "Factor's source tree"
509 "Lines of code in core/: 9,500"
510 "Lines of code in basis/: 120,000"
511 "Lines of code in extra/: 51,000"
512 "Lines of tests: 44,000"
513 "Lines of documentation: 44,500"
516 "Lines of C++ code: 12860"
517 "Generational garbage collection"
518 "Non-optimizing compiler"
519 "Loads an image file and runs it"
521 { $slide "Why should I use Factor?"
522 "More abstractions over time"
523 "We fix reported bugs quickly"
524 "Stackable, fluent language"
525 "Supports extreme programming"
526 "Beer-friendly programming"
528 { $slide "Questions?"
532 : tc-lisp-talk ( -- ) tc-lisp-slides slides-window ;