PageRenderTime 41ms CodeModel.GetById 20ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/extra/galois-talk/galois-talk.factor

http://github.com/abeaumont/factor
Unknown | 312 lines | 309 code | 3 blank | 0 comment | 0 complexity | 68e194ebe24106dee464e43bcd801b37 MD5 | raw file
  1! Copyright (C) 2008 Slava Pestov.
  2! See http://factorcode.org/license.txt for BSD license.
  3USING: slides help.markup math arrays hashtables namespaces
  4sequences kernel parser memoize io.encodings.binary
  5locals kernel.private help.vocabs assocs quotations
  6urls peg.ebnf tools.annotations tools.crossref
  7help.topics math.functions compiler.tree.optimizer
  8compiler.cfg.optimizer fry ;
  9IN: galois-talk
 10
 11CONSTANT: galois-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 "Words and the stack"
 22        "Stack based, dynamically typed"
 23        { $code "{ 1 1 3 4 4 8 9 9 } dup duplicates diff ." }
 24        "Words: named code snippets"
 25        { $code ": remove-duplicates ( seq -- seq' )" "    dup duplicates diff ;" }
 26        { $code "{ 1 1 3 4 4 8 9 9 } remove-duplicates ." }
 27    }
 28    { $slide "Vocabularies"
 29        "Vocabularies: named sets of words"
 30        { $link "vocab-index" }
 31        { { $link POSTPONE: USING: } " loads dependencies" }
 32        "Source, docs, tests in one place"
 33    }
 34    { $slide "Interactive development"
 35        "Programming is hard, let's play tetris"
 36        { $vocab-link "tetris" }
 37        "Tetris is hard too... let's cheat"
 38        "Factor workflow: change code, F2, test, repeat"
 39    }
 40    { $slide "Quotations"
 41        "Quotation: unnamed block of code"
 42        "Combinators: words taking quotations"
 43        { $code "10 dup 0 < [ 1 - ] [ 1 + ] if ." }
 44        { $code "{ -1 1 -2 0 3 } [ 0 max ] map ." }
 45        "Partial application:"
 46        { $code ": clamp ( seq n -- seq' ) '[ _ max ] map ;" "{ -1 1 -2 0 3 } 0 clamp" }
 47    }
 48    { $slide "Object system"
 49        "CLOS with single dispatch"
 50        "A tuple is a user-defined class which holds named values."
 51        { $code
 52            "TUPLE: rectangle width height ;"
 53            "TUPLE: circle radius ;"
 54        }
 55    }
 56    { $slide "Object system"
 57        "Constructing instances:"
 58        { $code "rectangle new" }
 59        { $code "rectangle boa" }
 60        "Let's encapsulate:"
 61        { $code
 62            ": <rectangle> ( w h -- r ) rectangle boa ;"
 63            ": <circle> ( r -- c ) circle boa ;"
 64        }
 65    }
 66    { $slide "Object system"
 67        "Generic words and methods"
 68        { $code "GENERIC: area ( shape -- n )" }
 69        "Two methods:"
 70        { $code
 71            "USE: math.constants"
 72            ""
 73            "M: rectangle area"
 74            "    [ width>> ] [ height>> ] bi * ;"
 75            ""
 76            "M: circle area radius>> sq pi * ;"
 77        }
 78    }
 79    { $slide "Object system"
 80        "We can compute areas now."
 81        { $code "100 20 <rectangle> area ." }
 82        { $code "3 <circle> area ." }
 83    }
 84    { $slide "Object system"
 85        "Object system handles dynamic redefinition very well"
 86        { $code "TUPLE: person name age occupation ;" }
 87        "Make an instance..."
 88    }
 89    { $slide "Object system"
 90        "Let's add a new slot:"
 91        { $code "TUPLE: person name age address occupation ;" }
 92        "Fill it in with inspector..."
 93        "Change the order:"
 94        { $code "TUPLE: person name occupation address ;" }
 95    }
 96    { $slide "Object system"
 97        "How does it work?"
 98        "Objects are not hashtables; slot access is very fast"
 99        "Redefinition walks the heap; expensive but rare"
100    }
101    { $slide "Object system"
102        "Supports \"duck typing\""
103        "Two tuples can have a slot with the same name"
104        "Code that uses accessors will work on both"
105        "Accessors are auto-generated generic words"
106    }
107    { $slide "Object system"
108        "Predicate classes"
109        { $code
110            "PREDICATE: positive < integer 0 > ;"
111            "PREDICATE: negative < integer 0 < ;"
112            ""
113            "GENERIC: abs ( n -- )"
114            ""
115            "M: positive abs ;"
116            "M: negative abs -1 * ;"
117            "M: integer abs ;"
118        }
119    }
120    { $slide "Object system"
121        "More: inheritance, type declarations, read-only slots, union, intersection, singleton classes, reflection"
122        "Object system is entirely implemented in Factor"
123    }
124    { $slide "The parser"
125        "All data types have a literal syntax"
126        "Literal hashtables and arrays are very useful in data-driven code"
127        "\"Code is data\" because quotations are objects (enables Lisp-style macros)"
128        { $code "H{ { \"cookies\" 12 } { \"milk\" 10 } }" }
129        "Libraries can define new parsing words"
130    }
131    { $slide "Example: regexp"
132        { $vocab-link "regexp" }
133        "Pre-compiles regexp at parse time"
134        "Implemented with library code"
135        { $code "USE: regexp" }
136        { $code "\"ababbc\" \"[ab]+c\" <regexp> matches? ." }
137        { $code "\"ababbc\" R/ [ab]+c/ matches? ." }
138    }
139    { $slide "Example: memoization"
140        { "Memoization with " { $link POSTPONE: MEMO: } }
141        { $code
142            ": fib ( m -- n )"
143            "    dup 1 > ["
144            "        [ 1 - fib ] [ 2 - fib ] bi +"
145            "    ] when ;"
146        }
147        "Very slow! Let's profile it..."
148    }
149    { $slide "Example: memoization"
150        { "Let's use " { $link POSTPONE: : } " instead of " { $link POSTPONE: MEMO: } }
151        { $code
152            "MEMO: fib ( m -- n )"
153            "    dup 1 > ["
154            "        [ 1 - fib ] [ 2 - fib ] bi +"
155            "    ] when ;"
156        }
157        "Much faster"
158    }
159    { $slide "Meta-circularity"
160        { { $link POSTPONE: MEMO: } " is just a library word" }
161        { "But so is " { $link POSTPONE: : } }
162        "Factor's parser is written in Factor"
163        { "All syntax is just parsing words: " { $link POSTPONE: [ } ", " { $link POSTPONE: " } }
164    }
165    { $slide "Extensible syntax, DSLs"
166        "Most parsing words fall in one of two categories"
167        "First category: literal syntax for new data types"
168        "Second category: defining new types of words"
169        "Some parsing words are more complicated"
170    }
171    { $slide "Example: printf"
172        { { $link POSTPONE: EBNF: } ": a complex parsing word" }
173        "Implements a custom syntax for expressing parsers: like OMeta!"
174        { "Example: " { $vocab-link "printf-example" } }
175        { $code "\"vegan\" \"cheese\" \"%s is not %s\\n\" printf" }
176        { $code "5 \"Factor\" \"%s is %d years old\\n\" printf" }
177    }
178    { $slide "Example: simple web browser"
179        { $vocab-link "webkit-demo" }
180        "Demonstrates Cocoa binding"
181        "Let's deploy a stand-alone binary with the deploy tool"
182        "Deploy tool generates binaries with no external dependencies"
183    }
184    { $slide "Locals and lexical scope"
185        "Sometimes, there's no good stack solution to a problem"
186        "Or, you're porting existing code in a quick-and-dirty way"
187        "Our solution: implement named locals as a DSL in Factor"
188        "Influenced by Scheme and Lisp"
189    }
190    { $slide "Locals and lexical scope"
191        { "Define lambda words with " { $link POSTPONE: :: } }
192        { "Establish bindings with " { $link POSTPONE: [let } " and " { $snippet "[let*" } }
193        "Mutable bindings with correct semantics"
194        { "Named inputs for quotations with " { $link POSTPONE: [| } }
195        "Full closures"
196    }
197    { $slide "Locals and lexical scope"
198        "Combinator with 5 parameters!"
199        { $code
200            ":: branch ( a b neg zero pos -- )"
201            "    a b = zero [ a b < neg pos if ] if ; inline"
202        }
203        "Unwieldy with the stack"
204    }
205    { $slide "Locals and lexical scope"
206        { $code
207            ": check-drinking-age ( age -- )"
208            "    21"
209            "    [ \"You're underage!\" print ]"
210            "    [ \"Grats, you're now legal\" print ]"
211            "    [ \"Go get hammered\" print ]"
212            "    branch ;"
213        }
214    }
215    { $slide "Locals and lexical scope"
216        "Locals are entirely implemented in Factor"
217        "Example of compile-time meta-programming"
218        "No performance penalty -vs- using the stack"
219        "In the base image, only 59 words out of 13,000 use locals"
220    }
221    { $slide "More about partial application"
222        { { $link POSTPONE: '[ } " is \"fry syntax\"" }
223        { $code "'[ _ + ] == [ + ] curry" }
224        { $code "'[ @ t ] == [ t ] compose" }
225        { $code "'[ _ nth @ ] == [ [ nth ] curry ] dip compose" }
226        { $code "'[ [ _ ] dip nth ] == [ [ ] curry dip nth ] curry" }
227        { "Fry and locals desugar to " { $link curry } ", " { $link compose } }
228    }
229    { $slide "Help system"
230        "Help markup is just literal data"
231        { "Look at the help for " { $link T{ link f + } } }
232        "These slides are built with the help system and a custom style sheet"
233        { $vocab-link "galois-talk" }
234    }
235    { $slide "Why stack-based?"
236        "Because nobody else is doing it"
237        "Interesting properties: concatenation is composition, chaining functions together, \"fluent\" interfaces, new combinators"
238        { $vocab-link "smtp-example" }
239        { $code
240            "{ \"chicken\" \"beef\" \"pork\" \"turkey\" }"
241            "[ 5 short head ] map ."
242        }
243    }
244    { $slide "Implementation"
245        "VM: garbage collection, bignums, ..."
246        "Bootstrap image: parser, hashtables, object system, ..."
247        "Non-optimizing compiler"
248        "Stage 2 bootstrap: optimizing compiler, UI, ..."
249        "Full image contains machine code"
250    }
251    { $slide "Compiler"
252        { "Let's look at " { $vocab-link "benchmark.mandel" } }
253        "A naive implementation would be very slow"
254        "Combinators, partial application"
255        "Boxed complex numbers"
256        "Boxed floats"
257        { "Redundancy in " { $link absq } " and " { $link sq } }
258    }
259    { $slide "Compiler: high-level optimizer"
260        "High-level SSA IR"
261        "Type inference (classes, intervals, arrays with a fixed length, literals, ...)"
262        "Escape analysis and tuple unboxing"
263    }
264    { $slide "Compiler: high-level optimizer"
265        "Loop index becomes a fixnum, complex numbers unboxed, generic arithmetic inlined, higher-order code become first-order..."
266        { $code "[ c pixel ] optimized." }
267    }
268    { $slide "Compiler: low-level optimizer"
269        "Low-level SSA IR"
270        "Alias analysis"
271        "Value numbering"
272        "Linear scan register allocation"
273    }
274    { $slide "Compiler: low-level optimizer"
275        "Redundant stack operations eliminated, intermediate floats unboxed..."
276        { $code "[ c pixel ] test-mr mr." }
277    }
278    { $slide "Garbage collection"
279        "All roots are identified precisely"
280        "Generational copying for data"
281        "Mark sweep for native code"
282    }
283    { $slide "Project infrastructure"
284        { $url "http://factorcode.org" }
285        { $url "http://concatenative.org" }
286        { $url "http://docs.factorcode.org" }
287        { $url "http://planet.factorcode.org" }
288        "Uses our HTTP server, SSL, DB, Atom libraries..."
289    }
290    { $slide "Project infrastructure"
291        "Build farm, written in Factor"
292        "12 platforms"
293        "Builds Factor and all libraries, runs tests, makes binaries"
294        "Saves us from the burden of making releases by hand"
295        "Maintains stability"
296    }
297    { $slide "Community"
298        "#concatenative irc.freenode.net: 50-60 members"
299        "factor-talk@lists.sf.net: 180 subscribers"
300        "About 30 people have code in the Factor repository"
301        "Easy to get started: binaries, lots of docs, friendly community..."
302    }
303    { $slide "That's all, folks"
304        "It is hard to cover everything in a single talk"
305        "Factor has many cool things that I didn't talk about"
306        "Questions?"
307    }
308}
309
310: galois-talk ( -- ) galois-slides slides-window ;
311
312MAIN: galois-talk