]> gitweb.factorcode.org Git - factor-talks.git/blob - vpri-talk/vpri-talk.factor
Remove "talks." namespace.
[factor-talks.git] / vpri-talk / vpri-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 urls
6 peg.ebnf tools.annotations tools.crossref help.topics
7 math.functions compiler.tree.optimizer compiler.cfg.optimizer
8 fry ;
9 IN: vpri-talk
10
11 CONSTANT: vpri-slides
12 {
13     { $slide "Factor!"
14         { $url "http://factorcode.org" }
15         "Development started in 2003"
16         "Open source (BSD license)"
17         "Influenced by Forth, Lisp, and Smalltalk"
18         "Blurs the line between language and library"
19         "Interactive development"
20     }
21     { $slide "Programming is hard"
22         "Let's play tetris instead"
23         { $vocab-link "tetris" }
24         "Tetris is hard too... let's cheat"
25         "Factor workflow: change code, F2, test, repeat"
26     }
27     { $slide "Basics"
28         "Stack based, dynamically typed"
29         { $code "{ 1 1 3 4 4 8 9 9 } dup duplicates diff ." }
30         "Words: named code snippets"
31         { $code ": remove-duplicates ( seq -- seq' )" "    dup duplicates diff ;" }
32         { $code "{ 1 1 3 4 4 8 9 9 } remove-duplicates ." }
33         "Vocabularies: named sets of words"
34         { $link "vocab-index" }
35     }
36     { $slide "Quotations"
37         "Quotation: unnamed block of code"
38         "Combinators: words taking quotations"
39         { $code "{ 1 1 3 4 4 8 9 9 }" "[ { 1 3 8 } member? ] filter ." }
40         { $code "{ -1 1 -2 0 3 } [ 0 max ] map" }
41         "Partial application:"
42         { $code ": clamp ( seq n -- seq' ) '[ _ max ] map" "{ -1 1 -2 0 3 } 0 clamp ;" }
43     }
44     { $slide "Object system"
45         "CLOS with single dispatch"
46         "A tuple is a user-defined class which holds named values."
47         { $code
48             "TUPLE: rectangle width height ;"
49             "TUPLE: circle radius ;"
50         }
51     }
52     { $slide "Object system"
53         "Constructing instances:"
54         { $code "rectangle new" }
55         { $code "rectangle boa" }
56         "Let's encapsulate:"
57         { $code
58             ": <rectangle> ( w h -- r ) rectangle boa ;"
59             ": <circle> ( r -- c ) circle boa ;"
60         }
61     }
62     { $slide "Object system"
63         "Generic words and methods"
64         { $code "GENERIC: area ( shape -- n )" }
65         "Two methods:"
66         { $code
67             "USE: math.constants"
68             ""
69             "M: rectangle area"
70             "    [ width>> ] [ height>> ] bi * ;"
71             ""
72             "M: circle area radius>> sq pi * ;"
73         }
74     }
75     { $slide "Object system"
76         "We can compute areas now."
77         { $code "100 20 <rectangle> area ." }
78         { $code "3 <circle> area ." }
79     }
80     { $slide "Object system"
81         "New operation, existing types:"
82         { $code
83             "GENERIC: perimeter ( shape -- n )"
84             ""
85             "M: rectangle perimeter"
86             "    [ width>> ] [ height>> ] bi + 2 * ;"
87             ""
88             "M: circle perimeter"
89             "    radius>> 2 * pi * ;"
90         }
91     }
92     { $slide "Object system"
93         "We can compute perimeters now."
94         { $code "100 20 <rectangle> perimeter ." }
95         { $code "3 <circle> perimeter ." }
96     }
97     { $slide "Object system"
98         "New type, extending existing operations:"
99         { $code
100             "TUPLE: triangle base height ;"
101             ""
102             ": <triangle> ( b h -- t ) triangle boa ;"
103             ""
104             "M: triangle area"
105             "    [ base>> ] [ height>> ] bi * 2 / ;"
106         }
107     }
108     { $slide "Object system"
109         "New type, extending existing operations:"
110         { $code
111             ": hypotenuse ( x y -- z ) [ sq ] bi@ + sqrt ;"
112             ""
113             "M: triangle perimeter"
114             "    [ base>> ] [ height>> ] bi"
115             "    [ + ] [ hypotenuse ] 2bi + ;"
116         }
117     }
118     { $slide "Object system"
119         "Object system handles dynamic redefinition very well"
120         { $code "TUPLE: person name age occupation ;" }
121         "Make an instance..."
122     }
123     { $slide "Object system"
124         "Let's add a new slot:"
125         { $code "TUPLE: person name age address occupation ;" }
126         "Fill it in with inspector..."
127         "Change the order:"
128         { $code "TUPLE: person name occupation address ;" }
129     }
130     { $slide "Object system"
131         "How does it work?"
132         "Objects are not hashtables; slot access is very fast"
133         "Redefinition walks the heap; expensive but rare"
134     }
135     { $slide "Object system"
136         "Supports \"duck typing\""
137         "Two tuples can have a slot with the same name"
138         "Code that uses accessors will work on both"
139         "Accessors are auto-generated generic words"
140     }
141     { $slide "Object system"
142         "More: inheritance, type declarations, read-only slots, predicate, intersection, singleton classes, reflection"
143         "Object system is entirely implemented in Factor"
144         { { $vocab-link "generic" } ", " { $vocab-link "classes" } ", " { $vocab-link "slots" } }
145     }
146     { $slide "The parser"
147         "All data types have a literal syntax"
148         "Literal hashtables and arrays are very useful in data-driven code"
149         "\"Code is data\" because quotations are objects (enables Lisp-style macros)"
150         { $code "H{ { \"cookies\" 12 } { \"milk\" 10 } }" }
151         "Libraries can define new parsing words"
152     }
153     { $slide "Example: float arrays"
154         { $vocab-link "specialized-arrays.float" }
155         "Avoids boxing and unboxing overhead"
156         "Implemented with library code"
157         { $code "float-array{ 3.14 7.6 10.3 }" }
158     }
159     { $slide "Example: memoization"
160         { "Memoization with " { $link POSTPONE: MEMO: } }
161         { $code
162             ": fib ( m -- n )"
163             "    dup 1 > ["
164             "        [ 1 - fib ] [ 2 - fib ] bi +"
165             "    ] when ;"
166         }
167         "Very slow! Let's profile it..."
168     }
169     { $slide "Example: memoization"
170         { "Let's use " { $link POSTPONE: : } " instead of " { $link POSTPONE: MEMO: } }
171         { $code
172             "MEMO: fib ( m -- n )"
173             "    dup 1 > ["
174             "        [ 1 - fib ] [ 2 - fib ] bi +"
175             "    ] when ;"
176         }
177         "Much faster"
178     }
179     { $slide "Meta-circularity"
180         { { $link POSTPONE: MEMO: } " is just a library word" }
181         { "But so is " { $link POSTPONE: : } }
182         "Factor's parser is written in Factor"
183         { "All syntax is just parsing words: " { $link POSTPONE: [ } ", " { $link POSTPONE: " } }
184     }
185     { $slide "Extensible syntax, DSLs"
186         "Most parsing words fall in one of two categories"
187         "First category: literal syntax for new data types"
188         "Second category: defining new types of words"
189         "Some parsing words are more complicated"
190     }
191     { $slide "Example: printf"
192         { { $link POSTPONE: EBNF: } ": a complex parsing word" }
193         "Implements a custom syntax for expressing parsers: like OMeta!"
194         { "Example: " { $vocab-link "printf-example" } }
195         { $code "\"cheese\" \"vegan\" \"%s is not %s\\n\" printf" }
196         { $code "\"Factor\" 5 \"%s is %d years old\\n\" printf" }
197     }
198     { $slide "Example: simple web browser"
199         { $vocab-link "webkit-demo" }
200         "Demonstrates Cocoa binding"
201         "Let's deploy a stand-alone binary with the deploy tool"
202         "Deploy tool generates binaries with no external dependencies"
203     }
204     { $slide "Locals and lexical scope"
205         "Sometimes, there's no good stack solution to a problem"
206         "Or, you're porting existing code in a quick-and-dirty way"
207         "Our solution: implement named locals as a DSL in Factor"
208         "Influenced by Scheme and Lisp"
209     }
210     { $slide "Locals and lexical scope"
211         { "Define lambda words with " { $link POSTPONE: :: } }
212         { "Establish bindings with " { $link POSTPONE: [let } " and " { $snippet "[let*" } }
213         "Mutable bindings with correct semantics"
214         { "Named inputs for quotations with " { $link POSTPONE: [| } }
215         "Full closures"
216     }
217     { $slide "Locals and lexical scope"
218         "Combinator with 5 parameters!"
219         { $code
220             ":: branch ( a b neg zero pos -- )"
221             "    a b = zero [ a b < neg pos if ] if ; inline"
222         }
223         "Unwieldy with the stack"
224     }
225     { $slide "Locals and lexical scope"
226         { $code
227             "ERROR: underage-exception ;"
228             ""
229             ": check-drinking-age ( age -- )"
230             "    21"
231             "    [ underage-exception ]"
232             "    [ \"Grats, you're now legal\" print ]"
233             "    [ \"Go get hammered\" print ]"
234             "    branch ;"
235         }
236     }
237     { $slide "Locals and lexical scope"
238         "Locals are entirely implemented in Factor"
239         "Example of compile-time meta-programming"
240         "No performance penalty -vs- using the stack"
241         "In the base image, only 59 words out of 13,000 use locals"
242     }
243     { $slide "More about partial application"
244         { { $link POSTPONE: '[ } " is \"fry syntax\"" }
245         { $code "'[ _ + ] == [ + ] curry" }
246         { $code "'[ @ t ] == [ t ] compose" }
247         { $code "'[ _ nth @ ] == [ [ nth ] curry ] dip compose" }
248         { $code "'[ [ _ ] dip nth ] == [ [ ] curry dip nth ] curry" }
249         { "Fry and locals desugar to " { $link curry } ", " { $link compose } }
250     }
251     { $slide "More about partial application"
252         { { $link call } " is fundamental" }
253         { { $link quotation } ", " { $link curry } " and " { $link compose } " are classes" }
254         { $code
255             "GENERIC: call ( quot -- )"
256             "M: curried call uncurry call ;"
257             "M: composed call uncompose slip call ;"
258             "M: quotation call (call) ;"
259         }
260         { "So " { $link curry } ", " { $link compose } " are library features" }
261     }
262     { $slide "Why stack-based?"
263         "Because nobody else is doing it"
264         "Interesting properties: concatenation is composition, chaining functions together, \"fluent\" interfaces, new combinators"
265         { $vocab-link "smtp-example" }
266         { $code
267             "{ \"chicken\" \"beef\" \"pork\" \"turkey\" }"
268             "[ 5 short head ] map ."
269         }
270         "To rattle people's cages"
271     }
272     { $slide "Help system"
273         "Help markup is just literal data"
274         { "Look at the help for " { $link T{ link f + } } }
275         "These slides are built with the help system and a custom style sheet"
276         { $vocab-link "vpri-talk" }
277     }
278     { $slide "Some line counts"
279         "VM: 12,000 lines of C"
280         "core: 9,000 lines of Factor"
281         "basis: 80,000 lines of Factor"
282     }
283     { $slide "More line counts"
284         "Object system (core): 2184 lines"
285         "Dynamic variables (core): 40 lines"
286         "Deterministic scoped destructors (core): 56 lines"
287         "Optimizing compiler (basis): 12938 lines"
288         "Lexical variables and closures (basis): 477 lines"
289         "Fry (basis): 51 lines"
290         "Help system (basis): 1831 lines"
291     }
292     { $slide "Implementation"
293         "VM: garbage collection, bignums, ..."
294         "Bootstrap image: parser, hashtables, object system, ..."
295         "Non-optimizing compiler"
296         "Stage 2 bootstrap: optimizing compiler, UI, ..."
297         "Full image contains machine code"
298     }
299     { $slide "Compiler"
300         { "Let's look at " { $vocab-link "benchmark.mandel" } }
301         "A naive implementation would be very slow"
302         "Combinators, currying, partial application"
303         "Boxed complex numbers"
304         "Boxed floats"
305         { "Redundancy in " { $link absq } " and " { $link sq } }
306     }
307     { $slide "Compiler: front-end"
308         "Builds high-level tree SSA IR"
309         "Stack code with uniquely-named values"
310         "Inlines combinators and calls to quotations"
311         { $code "USING: compiler.tree.builder compiler.tree.debugger ;" "[ c pixel ] build-tree nodes>quot ." }
312     }
313     { $slide "Compiler: high-level optimizer"
314         "12 optimization passes"
315         { $link optimize-tree }
316         "Some passes collect information, others use the results of past analysis to rewrite the code"
317     }
318     { $slide "Compiler: propagation pass"
319         "Propagation pass computes types with type function"
320         { "Example: output type of " { $link + } " depends on the types of inputs" }
321         "Type: can be a class, a numeric interval, array with a certain length, tuple with certain type slots, literal value, ..."
322         "Mandelbrot: we infer that we're working on complex floats"
323     }
324     { $slide "Compiler: propagation pass"
325         "Propagation also supports \"constraints\""
326         { $code "[ dup array? [ first ] when ] optimized." }
327         { $code "[ >fixnum dup 0 < [ 1 + ] when ] optimized." }
328         { $code
329             "["
330             "    >fixnum"
331             "    dup [ -10 > ] [ 10 < ] bi and"
332             "    [ 1 + ] when"
333             "] optimized."
334         }
335     }
336     { $slide "Compiler: propagation pass"
337         "Eliminates method dispatch, inlines method bodies"
338         "Mandelbrot: we infer that integer indices are fixnums"
339         "Mandelbrot: we eliminate generic arithmetic"
340     }
341     { $slide "Compiler: escape analysis"
342         "We identify allocations for tuples which are never returned or passed to other words (except slot access)"
343         { "Partial application with " { $link curry } " and " { $link compose } }
344         "Complex numbers"
345     }
346     { $slide "Compiler: escape analysis"
347         { "Virtual sequences: " { $link <slice> } ", " { $link <reversed> } }
348         { $code "[ <reversed> [ . ] each ] optimized." }
349         { "Mandelbrot: we unbox " { $link curry } ", complex number allocations" }
350     }
351     { $slide "Compiler: dead code elimination"
352         "Cleans up the mess from previous optimizations"
353         "After inlining and dispatch elimination, dead code comes up because of unused generality"
354         { "No-ops like " { $snippet "0 +" } ", " { $snippet "1 *" } }
355         "Literals which are never used"
356         "Side-effect-free words whose outputs are dropped"
357         { $code "[ c pixel ] optimized." }
358     }
359     { $slide "Compiler: low level IR"
360         "Register-based SSA"
361         "Stack operations expand into low-level instructions"
362         { $code "[ 5 ] regs." }
363         { $code "[ swap ] regs." }
364         { $code "[ append reverse ] regs." }
365     }
366     { $slide "Compiler: low-level optimizer"
367         "5 optimization passes"
368         { $link optimize-cfg }
369         "Gets rid of redundancy which is hidden in high-level stack code"
370     }
371     { $slide "Compiler: optimize memory"
372         "First pass optimizes stack and memory operations"
373         { "Example: " { $link 2array } }
374         { { $link <array> } " fills array with initial value" }
375         "What if we immediately store new values into the array?"
376         { $code "\\ 2array regs." }
377         "Mandelbrot: we optimize stack operations"
378     }
379     { $slide "Compiler: value numbering"
380         "Identifies expressions which are computed more than once in a basic block"
381         "Simplifies expressions with various identities"
382         "Mandelbrot: redundant float boxing and unboxing, redundant arithmetic"
383     }
384     { $slide "Compiler: dead code elimination"
385         "Dead code elimination for low-level IR"
386         "Again, cleans up results of prior optimizations"
387     }
388     { $slide "Compiler: register allocation"
389         "IR assumes an infinite number of registers which are only assigned once"
390         "Real CPUs have a finite set of registers which can be assigned any number of times"
391         "\"Linear scan register allocation with second-chance binpacking\""
392     }
393     { $slide "Compiler: register allocation"
394         "3 steps:"
395         "Compute live intervals"
396         "Allocate registers"
397         "Assign registers and insert spills"
398     }
399     { $slide "Compiler: register allocation"
400         "Step 1: compute live intervals"
401         "We number all instructions consecutively"
402         "A live interval associates a virtual register with a list of usages"
403     }
404     { $slide "Compiler: register allocation"
405         "Step 2: allocate registers"
406         "We scan through sorted live intervals"
407         "If a physical register is available, assign"
408         "Otherwise, find live interval with furthest away use, split it, look at both parts again"
409     }
410     { $slide "Compiler: register allocation"
411         "Step 3: assign registers and insert spills"
412         "Simple IR rewrite step"
413         "After register allocation, one vreg may have several live intervals, and different physical registers at different points in time"
414         "Hence, \"second chance\""
415         { "Mandelbrot: " { $code "[ c pixel ] regs." } }
416     }
417     { $slide "Compiler: code generation"
418         "Iterate over list of instructions"
419         "Extract tuple slots and call hooks"
420         { $vocab-link "cpu.architecture" }
421         "Finally, we hand the code to the VM"
422         { $code "\\ 2array disassemble" }
423     }
424     { $slide "Garbage collection"
425         "All roots are identified precisely"
426         "Generational copying for data"
427         "Mark sweep for native code"
428     }
429     { $slide "History"
430         "Started in 2003, implemented in Java"
431         "Scripting language for a 2D shooter game"
432         "Interactive development is addictive"
433         "I wanted to write entire applications in Factor"
434         "Added JVM bytecode compiler pretty early on"
435     }
436     { $slide "History"
437         "Wrote native C implementation, mid-2004"
438         "Added native compiler at some point"
439         "Added an FFI, SDL bindings, then UI"
440         "Switched UI to OpenGL and native APIs"
441         "Generational GC"
442         "Got rid of interpreter"
443     }
444     { $slide "Project infrastructure"
445         { $url "http://factorcode.org" }
446         { $url "http://concatenative.org" }
447         { $url "http://docs.factorcode.org" }
448         { $url "http://planet.factorcode.org" }
449         "Uses our HTTP server, SSL, DB, Atom libraries..."
450     }
451     { $slide "Project infrastructure"
452         "Build farm, written in Factor"
453         "12 platforms"
454         "Builds Factor and all libraries, runs tests, makes binaries"
455         "Saves us from the burden of making releases by hand"
456         "Maintains stability"
457     }
458     { $slide "Community"
459         "#concatenative irc.freenode.net: 50-60 members"
460         "factor-talk@lists.sf.net: 180 subscribers"
461         "About 30 people have code in the Factor repository"
462         "Easy to get started: binaries, lots of docs, friendly community..."
463     }
464     { $slide "Future direction: Factor 1.0"
465         "Continue doing what we're doing:"
466         "Polish off some language features"
467         "Stability"
468         "Performance"
469         "Documentation"
470         "Developer tools"
471     }
472     { $slide "Future direction: Factor 2.0"
473         "Native threads"
474         "Syntax-aware Factor editor"
475         "Embedding Factor in C apps"
476         "Cross-compilation for smaller devices"
477     }
478     { $slide "Research areas"
479         "Identify areas where stack languages are lacking, and try to find idioms, abstractions or DSLs to solve these problems"
480         "Factor is a good platform for DSLs (fry, locals, EBNF, help, ...); what about implementing a complete language on top?"
481         "Static typing, soft typing, for stack-based languages"
482     }
483     { $slide "That's all, folks"
484         "It is hard to cover everything in a single talk"
485         "Factor has many cool things that I didn't talk about"
486         "Questions?"
487     }
488 }
489
490 : vpri-talk ( -- ) vpri-slides "VPRI talk" slides-window ;
491
492 MAIN: vpri-talk