]> gitweb.factorcode.org Git - factor.git/blob - extra/google-tech-talk/google-tech-talk.factor
d5b88c244b953bbaba8dfdf5190b0d882f9576c0
[factor.git] / extra / google-tech-talk / google-tech-talk.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: slides help.markup math arrays hashtables namespaces
4 kernel sequences parser memoize io.encodings.binary
5 locals kernel.private help.vocabs assocs quotations
6 urls peg.ebnf tools.annotations tools.crossref
7 help.topics math.functions compiler.tree.optimizer
8 compiler.cfg.optimizer fry ;
9 IN: google-tech-talk
10
11 CONSTANT: google-slides
12 {
13     { $slide "Factor!"
14         { $url "http://factorcode.org" }
15         "Development started in 2003"
16         "Open source (BSD license)"
17         "First result for \"Factor\" on Google :-)"
18         "Influenced by Forth, Lisp, and Smalltalk (but don't worry if you don't know them)"
19     }
20     { $slide "Language overview"
21         "Words operate on a stack"
22         "Functional"
23         "Object-oriented"
24         "Rich collections library"
25         "Rich input/output library"
26         "Optional named local variables"
27         "Extensible syntax"
28     }
29     { $slide "Example: factorial"
30         "Lame example, but..."
31         { $code "USE: math.ranges" ": factorial ( n -- n! )" "    1 [a,b] product ;" }
32         { $code "100 factorial ." }
33     }
34     { $slide "Example: sending an e-mail"
35         { $vocab-link "smtp-example" }
36         "Demonstrates basic stack syntax and tuple slot setters"
37     }
38     { $slide "Functional programming"
39         "Code is data in Factor"
40         { { $snippet "[ ... ]" } " is a block of code pushed on the stack" }
41         { "We call them " { $emphasis "quotations" } }
42         { "Words which take quotations as input are called " { $emphasis "combinators" } }
43     }
44     { $slide "Functional programming"
45         { $code "10 dup 0 < [ 1 - ] [ 1 + ] if ." }
46         { $code "10 [ \"Hello Googlers!\" print ] times" }
47         { $code
48             "USING: io.encodings.ascii unicode.case ;"
49             "{ \"tomato\" \"orange\" \"banana\" }"
50             "\"out.txt\" ascii ["
51             "    [ >upper print ] each"
52             "] with-file-writer"
53         }
54     }
55     { $slide "Object system: motivation"
56         "Encapsulation, polymorphism, inheritance"
57         "Smalltalk, Python, Java approach: methods inside classes"
58         "Often the \"message sending\" metaphor is used to describe such systems"
59     }
60     { $slide "Object system: motivation"
61         { $code
62             "class Rect {"
63             "  int x, y;"
64             "  int area() { ... }"
65             "  int perimeter() { ... }"
66             "}"
67             ""
68             "class Circle {"
69             "  int radius;"
70             "  int area() { ... }"
71             "  int perimeter() { ... }"
72             "}"
73         }
74     }
75     { $slide "Object system: motivation"
76         "Classical functional language approach: functions switch on a type"
77         { $code
78             "data Shape = Rect w h | Circle r"
79             ""
80             "area s = s of"
81             "  (Rect w h) = ..."
82             "| (Circle r) = ..."
83             ""
84             "perimeter s = s of"
85             "  (Rect w h) = ..."
86             "| (Circle r) = ..."
87         }
88     }
89     { $slide "Object system: motivation"
90         "First approach: hard to extend existing types with new operations (open classes, etc are a hack)"
91         "Second approach: hard to extend existing operations with new types"
92         "Common Lisp Object System (CLOS): decouples classes from methods."
93         "Factor's object system is a simplified CLOS"
94     }
95     { $slide "Object system"
96         "A tuple is a user-defined class which holds named values."
97         { $code
98             "TUPLE: rectangle width height ;"
99             "TUPLE: circle radius ;"
100         }
101     }
102     { $slide "Object system"
103         "Constructing instances:"
104         { $code "rectangle new" }
105         { $code "rectangle boa" }
106         "Let's encapsulate:"
107         { $code
108             ": <rectangle> ( w h -- r ) rectangle boa ;"
109             ": <circle> ( r -- c ) circle boa ;"
110         }
111     }
112     { $slide "Object system"
113         "Generic words and methods"
114         { $code "GENERIC: area ( shape -- n )" }
115         "Two methods:"
116         { $code
117             "USE: math.constants"
118             ""
119             "M: rectangle area"
120             "    [ width>> ] [ height>> ] bi * ;"
121             ""
122             "M: circle area radius>> sq pi * ;"
123         }
124     }
125     { $slide "Object system"
126         "We can compute areas now."
127         { $code "100 20 <rectangle> area ." }
128         { $code "3 <circle> area ." }
129     }
130     { $slide "Object system"
131         "New operation, existing types:"
132         { $code
133             "GENERIC: perimeter ( shape -- n )"
134             ""
135             "M: rectangle perimeter"
136             "    [ width>> ] [ height>> ] bi + 2 * ;"
137             ""
138             "M: circle perimeter"
139             "    radius>> 2 * pi * ;"
140         }
141     }
142     { $slide "Object system"
143         "We can compute perimeters now."
144         { $code "100 20 <rectangle> perimeter ." }
145         { $code "3 <circle> perimeter ." }
146     }
147     { $slide "Object system"
148         "New type, extending existing operations:"
149         { $code
150             "TUPLE: triangle base height ;"
151             ""
152             ": <triangle> ( b h -- t ) triangle boa ;"
153             ""
154             "M: triangle area"
155             "    [ base>> ] [ height>> ] bi * 2 / ;"
156         }
157     }
158     { $slide "Object system"
159         "New type, extending existing operations:"
160         { $code
161             ": hypotenuse ( x y -- z ) [ sq ] bi@ + sqrt ;"
162             ""
163             "M: triangle perimeter"
164             "    [ base>> ] [ height>> ] bi"
165             "    [ + ] [ hypotenuse ] 2bi + ;"
166         }
167     }
168     { $slide "Object system"
169         "We can ask an object if its a rectangle:"
170         { $code "70 65 <rectangle> rectangle? ." }
171         { $code "13 <circle> rectangle? ." }
172         { "How do we tell if something is a " { $emphasis "shape" } "?" }
173     }
174     { $slide "Object system"
175         "We define a mixin class for shapes, and add our existing data types as instances:"
176         { $code
177             "MIXIN: shape"
178             "INSTANCE: rectangle shape"
179             "INSTANCE: circle shape"
180             "INSTANCE: triangle shape"
181         }
182     }
183     { $slide "Object system"
184         "Now, we can ask objects if they are shapes or not:"
185         { $code "13 <circle> shape? ." }
186         { $code "3.14 shape? ." }
187     }
188     { $slide "Object system"
189         "Or put methods on shapes:"
190         { $code
191             "GENERIC: tell-me ( obj -- )"
192             ""
193             "M: shape tell-me"
194             "    \"My area is \" write area . ;"
195             ""
196             "M: integer tell-me"
197             "    \"I am \" write"
198             "    even? \"even\" \"odd\" ? print ;"
199         }
200     }
201     { $slide "Object system"
202         "Let's test our new generic word:"
203         { $code "13 <circle> tell-me" }
204         { $code "103 76 <rectangle> tell-me" }
205         { $code "101 tell-me" }
206         { { $link integer } ", " { $link array } ", and others are built-in classes" }
207     }
208     { $slide "Object system"
209         "Anyone can define new shapes..."
210         { $code
211             "TUPLE: parallelogram ... ;"
212             ""
213             "INSTANCE: parallelogram shape"
214             ""
215             "M: parallelogram area ... ;"
216             ""
217             "M: parallelogram perimeter ... ;"
218         }
219     }
220     { $slide "Object system"
221         "More: inheritance, type declarations, read-only slots, predicate, intersection, singleton classes, reflection"
222         "Object system is entirely implemented in Factor: 2184 lines"
223         { { $vocab-link "generic" } ", " { $vocab-link "classes" } ", " { $vocab-link "slots" } }
224     }
225     { $slide "Collections"
226         "Sequences (arrays, vector, strings, ...)"
227         "Associative mappings (hashtables, ...)"
228         { "More: deques, heaps, purely functional structures, disjoint sets, and more: "
229         { $link T{ vocab-tag f "collections" } } }
230     }
231     { $slide "Sequences"
232         { "Protocol: " { $link length } ", " { $link set-length } ", " { $link nth } ", " { $link set-nth } }
233         { "Combinators: " { $link each } ", " { $link map } ", " { $link filter } ", " { $link produce } ", and more: " { $link "sequences-combinators" } }
234         { "Utilities: " { $link append } ", " { $link reverse } ", " { $link first } ",  " { $link second } ", ..." }
235     }
236     { $slide "Example: bin packing"
237         { "We have " { $emphasis "m" } " objects and " { $emphasis "n" } " bins, and we want to distribute these objects as evenly as possible." }
238         { $vocab-link "distribute-example" }
239         "Demonstrates various sequence utilities and vector words"
240         { $code "20 13 distribute ." }
241     }
242     { $slide "Unicode strings"
243         "Strings are sequences of 21-bit Unicode code points"
244         "Efficient implementation: ASCII byte string unless it has chars > 127"
245         "If a byte char has high bit set, the remaining 14 bits come from auxiliary vector"
246     }
247     { $slide "Unicode strings"
248         "Unicode-aware case conversion, char classes, collation, word breaks, and so on..."
249         { $code "USE: unicode.case" "\"ß\" >upper ." }
250     }
251     { $slide "Unicode strings"
252         "All external byte I/O is encoded/decoded"
253         "ASCII, UTF8, UTF16, EBCDIC..."
254         { $code "USE: io.encodings.utf8" "\"document.txt\" utf8" "[ readln ] with-file-reader" }
255         { "Binary I/O is supported as well with the " { $link binary } " encoding" }
256     }
257     { $slide "Associative mappings"
258         { "Protocol: " { $link assoc-size } ", " { $link at* } ", " { $link set-at } ", " { $link delete-at } }
259         { "Combinators: " { $link assoc-each } ", " { $link assoc-map } ", " { $link assoc-filter } ", and more: " { $link "assocs-combinators" } }
260         { "Utilities: " { $link at } ", " { $link key? } ", ..." }
261     }
262     ! { $slide "Example: soundex"
263     !     { $vocab-link "soundex" }
264     !     "From Wikipedia: \"Soundex is a phonetic algorithm for indexing names by sound, as pronounced in English.\""
265     !     "Factored into many small words, uses sequence and assoc operations, no explicit loops"
266     ! }
267     { $slide "Locals and lexical scope"
268         "Sometimes, there's no good stack solution to a problem"
269         "Or, you're porting existing code in a quick-and-dirty way"
270         "Our solution: implement named locals as a DSL in Factor"
271         "Influenced by Scheme and Lisp"
272     }
273     { $slide "Locals and lexical scope"
274         { "Define lambda words with " { $link POSTPONE: :: } }
275         { "Establish bindings with " { $link POSTPONE: [let } " and " { $snippet "[let*" } }
276         "Mutable bindings with correct semantics"
277         { "Named inputs for quotations with " { $link POSTPONE: [| } }
278         "Full closures"
279     }
280     { $slide "Locals and lexical scope"
281         "Two examples:"
282         { $vocab-link "lambda-quadratic" }
283         { $vocab-link "closures-example" }
284     }
285     { $slide "Locals and lexical scope"
286         "Locals are entirely implemented in Factor: 477 lines"
287         "Example of compile-time meta-programming"
288         "No performance penalty -vs- using the stack"
289         "In the base image, only 59 words out of 13,000 use locals"
290     }
291     { $slide "The parser"
292         "All data types have a literal syntax"
293         "Literal hashtables and arrays are very useful in data-driven code"
294         "\"Code is data\" because quotations are objects (enables Lisp-style macros)"
295         { $code "H{ { \"cookies\" 12 } { \"milk\" 10 } }" }
296         "Libraries can define new parsing words"
297     }
298     { $slide "The parser"
299         { "Example: URLs define a " { $link POSTPONE: URL" } " word" }
300         { $code "URL\" http://paste.factorcode.org/paste?id=81\"" }
301     }
302     { $slide "Example: memoization"
303         { "Memoization with " { $link POSTPONE: MEMO: } }
304         { $code
305             ": fib ( m -- n )"
306             "    dup 1 > ["
307             "        [ 1 - fib ] [ 2 - fib ] bi +"
308             "    ] when ;"
309         }
310         "Very slow! Let's profile it..."
311     }
312     { $slide "Example: memoization"
313         { "Let's use " { $link POSTPONE: : } " instead of " { $link POSTPONE: MEMO: } }
314         { $code
315             "MEMO: fib ( m -- n )"
316             "    dup 1 > ["
317             "        [ 1 - fib ] [ 2 - fib ] bi +"
318             "    ] when ;"
319         }
320         "Much faster"
321     }
322     { $slide "Meta-circularity"
323         { { $link POSTPONE: MEMO: } " is just a library word" }
324         { "But so is " { $link POSTPONE: : } }
325         "Factor's parser is written in Factor"
326         { "All syntax is just parsing words: " { $link POSTPONE: [ } ", " { $link POSTPONE: " } }
327     }
328     { $slide "Extensible syntax, DSLs"
329         "Most parsing words fall in one of two categories"
330         "First category: literal syntax for new data types"
331         "Second category: defining new types of words"
332         "Some parsing words are more complicated"
333     }
334     { $slide "Parser expression grammars"
335         { { $link POSTPONE: EBNF: } ": a complex parsing word" }
336         "Implements a custom syntax for expressing parsers"
337         { "Example: " { $vocab-link "printf-example" } }
338         { $code "\"vegan\" \"cheese\" \"%s is not %s\\n\" printf" }
339         { $code "5 \"Factor\" \"%s is %d years old\\n\" printf" }
340     }
341     { $slide "Input/output library"
342         "One of Factor's strongest points: portable, full-featured, efficient"
343         { $vocab-link "io.files" }
344         { $vocab-link "io.launcher" }
345         { $vocab-link "io.monitors" }
346         { $vocab-link "io.mmap" }
347         { $vocab-link "http.client" }
348         "... and so on"
349     }
350     { $slide "Example: file system monitors"
351         { $code
352             "USE: io.monitors"
353             ""
354             ": forever ( quot -- ) '[ @ t ] loop ; inline"
355             ""
356             "\"/tmp\" t <monitor>"
357             "'[ _ next-change . ] forever"
358         }
359     }
360     { $slide "Example: time server"
361         { $vocab-link "time-server" }
362         { "Demonstrates " { $vocab-link "io.servers" } " vocabulary, threads" }
363     }
364     { $slide "Example: what is my IP?"
365         { $vocab-link "webapps.ip" }
366         "Simple web app, defines a single action, use an XHTML template"
367         "Web framework supports more useful features: sessions, SSL, form validation, ..."
368     }
369     { $slide "Example: Yahoo! web search"
370         { $vocab-link "yahoo" }
371         { "Demonstrates " { $vocab-link "http.client" } ", " { $vocab-link "xml" } }
372     }
373     { $slide "Example: simple web browser"
374         { $vocab-link "webkit-demo" }
375         "Demonstrates Cocoa binding"
376         "Let's deploy a stand-alone binary with the deploy tool"
377         "Deploy tool generates binaries with no external dependencies"
378     }
379     { $slide "Example: environment variables"
380         { $vocab-link "environment" }
381         "Hooks are generic words which dispatch on dynamically-scoped variables"
382         { "Implemented in an OS-specific way: " { $vocab-link "environment.unix" } ", " { $vocab-link "environment.winnt" } }
383     }
384     { $slide "Example: environment variables"
385         "Implementations use C FFI"
386         "Call C functions, call function pointers, call Factor from C, structs, floats, ..."
387         "No need to write C wrapper code"
388     }
389     { $slide "Implementation"
390         "VM: 12,000 lines of C"
391         "Generational garbage collection"
392         "core: 9,000 lines of Factor"
393         "Optimizing native code compiler for x86, PowerPC"
394         "basis: 80,000 lines of Factor"
395     }
396     { $slide "Compiler"
397         { "Let's look at " { $vocab-link "benchmark.mandel" } }
398         "A naive implementation would be very slow"
399         "Combinators, currying, partial application"
400         "Boxed complex numbers"
401         "Boxed floats"
402         { "Redundancy in " { $link absq } " and " { $link sq } }
403     }
404     { $slide "Compiler: front-end"
405         "Builds high-level tree SSA IR"
406         "Stack code with uniquely-named values"
407         "Inlines combinators and calls to quotations"
408         { $code "USING: compiler.tree.builder compiler.tree.debugger ;" "[ c pixel ] build-tree nodes>quot ." }
409     }
410     { $slide "Compiler: high-level optimizer"
411         "12 optimization passes"
412         { $link optimize-tree }
413         "Some passes collect information, others use the results of past analysis to rewrite the code"
414     }
415     { $slide "Compiler: propagation pass"
416         "Propagation pass computes types with type function"
417         { "Example: output type of " { $link + } " depends on the types of inputs" }
418         "Type: can be a class, a numeric interval, array with a certain length, tuple with certain type slots, literal value, ..."
419         "Mandelbrot: we infer that we're working on complex floats"
420     }
421     { $slide "Compiler: propagation pass"
422         "Propagation also supports \"constraints\""
423         { $code "[ dup array? [ first ] when ] optimized." }
424         { $code "[ >fixnum dup 0 < [ 1 + ] when ] optimized." }
425         { $code
426             "["
427             "    >fixnum"
428             "    dup [ -10 > ] [ 10 < ] bi and"
429             "    [ 1 + ] when"
430             "] optimized."
431         }
432     }
433     { $slide "Compiler: propagation pass"
434         "Eliminates method dispatch, inlines method bodies"
435         "Mandelbrot: we infer that integer indices are fixnums"
436         "Mandelbrot: we eliminate generic arithmetic"
437     }
438     { $slide "Compiler: escape analysis"
439         "We identify allocations for tuples which are never returned or passed to other words (except slot access)"
440         { "Partial application with " { $link POSTPONE: '[ } }
441         "Complex numbers"
442     }
443     { $slide "Compiler: escape analysis"
444         { "Virtual sequences: " { $link <slice> } ", " { $link <reversed> } }
445         { $code "[ <reversed> [ . ] each ] optimized." }
446         { "Mandelbrot: we unbox " { $link curry } ", complex number allocations" }
447     }
448     { $slide "Compiler: dead code elimination"
449         "Cleans up the mess from previous optimizations"
450         "After inlining and dispatch elimination, dead code comes up because of unused generality"
451         { "No-ops like " { $snippet "0 +" } ", " { $snippet "1 *" } }
452         "Literals which are never used"
453         "Side-effect-free words whose outputs are dropped"
454     }
455     { $slide "Compiler: low level IR"
456         "Register-based SSA"
457         "Stack operations expand into low-level instructions"
458         { $code "[ 5 ] test-mr mr." }
459         { $code "[ swap ] test-mr mr." }
460         { $code "[ append reverse ] test-mr mr." }
461     }
462     { $slide "Compiler: low-level optimizer"
463         "5 optimization passes"
464         { $link optimize-cfg }
465         "Gets rid of redundancy which is hidden in high-level stack code"
466     }
467     { $slide "Compiler: optimize memory"
468         "First pass optimizes stack and memory operations"
469         { "Example: " { $link 2array } }
470         { { $link <array> } " fills array with initial value" }
471         "What if we immediately store new values into the array?"
472         { $code "\\ 2array test-mr mr." }
473         "Mandelbrot: we optimize stack operations"
474     }
475     { $slide "Compiler: value numbering"
476         "Identifies expressions which are computed more than once in a basic block"
477         "Simplifies expressions with various identities"
478         "Mandelbrot: redundant float boxing and unboxing, redundant arithmetic"
479     }
480     { $slide "Compiler: dead code elimination"
481         "Dead code elimination for low-level IR"
482         "Again, cleans up results of prior optimizations"
483     }
484     { $slide "Compiler: register allocation"
485         "IR assumes an infinite number of registers which are only assigned once"
486         "Real CPUs have a finite set of registers which can be assigned any number of times"
487         "\"Linear scan register allocation with second-chance binpacking\""
488     }
489     { $slide "Compiler: register allocation"
490         "3 steps:"
491         "Compute live intervals"
492         "Allocate registers"
493         "Assign registers and insert spills"
494     }
495     { $slide "Compiler: register allocation"
496         "Step 1: compute live intervals"
497         "We number all instructions consecutively"
498         "A live interval associates a virtual register with a list of usages"
499     }
500     { $slide "Compiler: register allocation"
501         "Step 2: allocate registers"
502         "We scan through sorted live intervals"
503         "If a physical register is available, assign"
504         "Otherwise, find live interval with furthest away use, split it, look at both parts again"
505     }
506     { $slide "Compiler: register allocation"
507         "Step 3: assign registers and insert spills"
508         "Simple IR rewrite step"
509         "After register allocation, one vreg may have several live intervals, and different physical registers at different points in time"
510         "Hence, \"second chance\""
511         { "Mandelbrot: " { $code "[ c pixel ] test-mr mr." } }
512     }
513     { $slide "Compiler: code generation"
514         "Iterate over list of instructions"
515         "Extract tuple slots and call hooks"
516         { $vocab-link "cpu.architecture" }
517         "Finally, we hand the code to the VM"
518         { $code "\\ 2array disassemble" }
519     }
520     { $slide "Garbage collection"
521         "All roots are identified precisely"
522         "Generational copying for data"
523         "Mark sweep for native code"
524     }
525     { $slide "Project infrastructure"
526         { $url "http://factorcode.org" }
527         { $url "http://concatenative.org" }
528         { $url "http://docs.factorcode.org" }
529         { $url "http://planet.factorcode.org" }
530         "Uses our HTTP server, SSL, DB, Atom libraries..."
531     }
532     { $slide "Project infrastructure"
533         "Build farm, written in Factor"
534         "12 platforms"
535         "Builds Factor and all libraries, runs tests, makes binaries"
536         "Saves us from the burden of making releases by hand"
537         "Maintains stability"
538     }
539     { $slide "Community"
540         "#concatenative irc.freenode.net: 50-60 members"
541         "factor-talk@lists.sf.net: 180 subscribers"
542         "About 30 people have code in the Factor repository"
543         "Easy to get started: binaries, lots of docs, friendly community..."
544     }
545     { $slide "Future direction: Factor 1.0"
546         "Continue doing what we're doing:"
547         "Polish off some language features"
548         "Stability"
549         "Performance"
550         "Documentation"
551         "Developer tools"
552     }
553     { $slide "Future direction: Factor 2.0"
554         "Native threads"
555         "Syntax-aware Factor editor"
556         "Embedding Factor in C apps"
557         "Cross-compilation for smaller devices"
558     }
559     { $slide "That's all, folks"
560         "It is hard to cover everything in a single talk"
561         "Factor has many cool things that I didn't talk about"
562         "Put your prejudices aside and give it a shot!"
563     }
564     { $slide "Questions?" }
565 }
566
567 : google-talk ( -- ) google-slides slides-window ;
568
569 MAIN: google-talk