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 tools.annotations tools.crossref help.topics math.functions
7 compiler.tree.optimizer compiler.cfg.optimizer fry
8 ui.gadgets.panes tetris tetris.game combinators generalizations
9 multiline sequences.private ;
12 : $tetris ( element -- )
13 drop [ <default-tetris> <tetris-gadget> gadget. ] ($block) ;
18 { $url "http://factorcode.org" }
19 "Development started in 2003"
20 "Open source (BSD license)"
21 "Influenced by Forth, Lisp, and Smalltalk"
22 "Blurs the line between language and library"
23 "Interactive development"
25 { $slide "Part 1: the language" }
27 "Stack based, dynamically typed"
28 { "A " { $emphasis "word" } " is a named piece of code" }
29 { "Values are passed between words on a " { $emphasis "stack" } }
30 "Code evaluates left to right"
35 { "A " { $emphasis "quotation" } " is a block of code pushed on the stack" }
36 { "Syntax: " { $snippet "[ ... ]" } }
39 "\"/etc/passwd\" ascii file-lines"
40 "[ \"#\" head? not ] filter"
41 "[ \":\" split first ] map"
46 { "We can define new words with " { $snippet ": name ... ;" } " syntax" }
47 { $code ": remove-comments ( lines -- lines' )" " [ \"#\" head? not ] filter ;" }
48 { "Words are grouped into " { $emphasis "vocabularies" } }
49 { $link "vocab-index" }
50 "Libraries and applications are vocabularies"
51 { $vocab-link "spheres" }
53 { $slide "Constructing quotations"
54 { "Suppose we want a " { $snippet "remove-comments*" } " word" }
55 { $code ": remove-comments* ( lines string -- lines' )" " [ ??? head? not ] filter ;" }
56 { "We use " { $link POSTPONE: '[ } " instead of " { $link POSTPONE: [ } }
57 { "Create “holes” with " { $link _ } }
58 "Holes filled in left to right when quotation pushed on the stack"
60 { $slide "Constructing quotations"
61 { $code ": remove-comments* ( lines string -- lines' )" " '[ _ head? not ] filter ;" "" ": remove-comments ( lines -- lines' )" " \"#\" remove-comments* ;" }
62 { { $link @ } " inserts a quotation" }
63 { $code ": replicate ( n quot -- seq )" " '[ drop @ ] map ;" }
64 { $code "10 [ 1 10 [a,b] random ] replicate ." }
66 { $slide "Combinators"
67 { "A " { $emphasis "combinator" } " is a word taking quotations as input" }
68 { "Used for control flow, data flow, iteration" }
69 { $code "100 [ 5 mod 3 = [ \"Fizz!\" print ] when ] each" }
70 { "Control flow: " { $link if } ", " { $link when } ", " { $link unless } ", " { $link cond } }
71 { "Iteration: " { $link map } ", " { $link filter } ", " { $link all? } ", ..." }
73 { $slide "Data flow combinators - simple example"
74 "All examples so far used “pipeline style”"
75 "What about using a value more than once, or operating on values not at top of stack?"
76 { $code "{ 10 70 54 } [ sum ] [ length ] bi / ." }
77 { $code "5 [ 1 + ] [ sqrt ] [ 1 - ] tri 3array ." }
79 { $slide "Data flow combinators - cleave family"
80 { { $link bi } ", " { $link tri } ", " { $link cleave } }
81 { $image "resource:extra/otug-talk/bi.tiff" }
83 { $slide "Data flow combinators - cleave family"
84 { { $link 2bi } ", " { $link 2tri } ", " { $link 2cleave } }
85 { $image "resource:extra/otug-talk/2bi.tiff" }
87 { $slide "Data flow combinators"
88 "First, let's define a data type:"
89 { $code "TUPLE: person first-name last-name ;" }
91 { $code "person new" " \"Joe\" >>first-name" " \"Sixpack\" >>last-name" }
93 { $slide "Data flow combinators"
94 "Let's do stuff with it:"
96 "[ first-name>> ] [ last-name>> ] bi"
97 "[ 2 head ] [ 5 head ] bi*"
102 { $slide "Data flow combinators - spread family"
103 { { $link bi* } ", " { $link tri* } ", " { $link spread } }
104 { $image "resource:extra/otug-talk/bi_star.tiff" }
106 { $slide "Data flow combinators - spread family"
108 { $image "resource:extra/otug-talk/2bi_star.tiff" }
110 { $slide "Data flow combinators - apply family"
111 { { $link bi@ } ", " { $link tri@ } ", " { $link napply } }
112 { $image "resource:extra/otug-talk/bi_at.tiff" }
114 { $slide "Data flow combinators - apply family"
116 { $image "resource:extra/otug-talk/2bi_at.tiff" }
118 { $slide "Shuffle words"
119 "When data flow combinators are not enough"
120 { $link "shuffle-words" }
121 "Lower-level, Forth/PostScript-style stack manipulation"
124 "When data flow combinators and shuffle words are not enough"
125 "Name your input parameters"
126 "Used in about 1% of all words"
128 { $slide "Locals example"
129 "Area of a triangle using Heron's formula"
131 """:: area ( a b c -- x )
139 { $slide "Previous example without locals"
142 """: area ( a b c -- x )
144 [ '[ _ - ] tri@ ] [ neg ] bi
147 { $slide "More idiomatic version"
148 "But there's a trick: put the points in an array"
149 { $code """: v-n ( v n -- w ) '[ _ - ] map ;
151 : area ( points -- x )
152 [ 0 suffix ] [ sum 2 / ] bi
153 v-n product sqrt ;""" }
155 ! { $slide "The parser"
156 ! "All data types have a literal syntax"
157 ! "Literal hashtables and arrays are very useful in data-driven code"
158 ! { $code "H{ { \"cookies\" 12 } { \"milk\" 10 } }" }
159 ! "Libraries can define new parsing words"
161 { $slide "Programming without named values"
162 "Minimal glue between words"
163 "Easy multiple return values"
164 { "Avoid useless variable names: " { $snippet "x" } ", " { $snippet "n" } ", " { $snippet "a" } ", ..." }
165 { { $link at } " and " { $link at* } }
166 { $code "at* [ ... ] [ ... ] if" }
168 { $slide "Stack language idioms"
169 "Enables new idioms not possible before"
170 "We get the effect of “keyword parameters” for free"
171 { $vocab-link "smtp-example" }
173 { $slide "“Perfect” factoring"
175 { { $link head } { $link head-slice } }
176 { { $link tail } { $link tail-slice } }
178 { "Modifier: " { $link from-end } }
179 { "Modifier: " { $link short } }
180 "4*2*2=16 operations, 6 words!"
183 "“Modifiers” can express MN combinations using M+N words"
185 "\"Hello, Joe\" 4 head ."
186 "\"Hello, Joe\" 3 tail ."
187 "\"Hello, Joe\" 3 from-end tail ."
190 "\"Hello world\" 5 short head ."
191 "\"Hi\" 5 short tail ."
195 { "C-style " { $snippet "while" } " and " { $snippet "do while" } " loops" }
198 { $code ": bank ( n -- n )" " readln string>number +" " dup \"Balance: $\" write . ;" }
199 { $code "0 [ dup 0 > ] [ bank ] while" }
202 { $code "0 [ dup 0 > ] [ bank ] [ ] do while" }
203 { { $link do } " executes one iteration of a " { $link while } " loop" }
204 { { $link while } " calls " { $link do } }
206 { $slide "More “pipeline style” code"
207 { "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):" }
209 "dup [ orders>> ] when"
211 "dup [ price>> ] when"
214 { $slide "This is hard with mainstream syntax!"
216 """var customer = ...;
217 var orders = (customer == null ? null : customer.orders);
218 var order = (orders == null ? null : orders[0]);
219 var price = (order == null ? null : order.price);""" }
221 { $slide "An ad-hoc solution"
223 { $code "var price = customer.?orders.?[0].?price;" }
225 ! { $slide "Stack languages are fundamental"
226 ! "Very simple semantics"
227 ! "Easy to generate stack code programmatically"
228 ! "Everything is almost entirely library code in Factor"
229 ! "Factor is easy to extend"
231 { $slide "Part 2: the implementation" }
232 { $slide "Interactive development"
235 { $slide "Application deployment"
236 { $vocab-link "webkit-demo" }
237 "Demonstrates Cocoa binding"
238 "Let's deploy a stand-alone binary with the deploy tool"
239 "Deploy tool generates binaries with no external dependencies"
242 "Renders with OpenGL"
243 "Backends for Cocoa, Windows, X11: managing windows, input events, clipboard"
246 { $slide "UI example"
251 "Hello world!" <label> add-gadget
252 "Click me!" [ drop beep ]
253 <bevel-button> add-gadget
254 <editor> <scroller> add-gadget
255 "UI test" open-window""" }
257 { $slide "Help system"
258 "Help markup is just literal data"
259 { "Look at the help for " { $link T{ link f + } } }
260 "These slides are built with the help system and a custom style sheet"
261 { $vocab-link "otug-talk" }
264 "Lowest level is the VM: ~12,000 lines of C"
265 "Generational garbage collection"
266 "Non-optimizing compiler"
267 "Loads an image file and runs it"
268 "Initial image generated from another Factor instance:"
269 { $code "\"x86.32\" make-image" }
271 { $slide "The core library"
272 "Core library, ~9,000 lines of Factor"
273 "Source parser, arrays, strings, math, hashtables, basic I/O, ..."
274 "Packaged into boot image because VM doesn't have a parser"
276 { $slide "The basis library"
277 "Basis library, ~80,000 lines of Factor"
278 "Bootstrap process loads code from basis, runs compiler, saves image"
279 "Loaded by default: optimizing compiler, tools, help system, UI, ..."
280 "Optional: HTTP server, XML, database access, ..."
282 { $slide "Non-optimizing compiler"
283 "Glues together chunks of machine code"
284 "Most words compiled as calls, some inlined"
285 "Used for listener interactions, and bootstrap"
287 { $slide "Optimizing compiler"
288 "Converts Factor code into high-level SSA form"
289 "Performs global optimizations"
290 "Converts high-level SSA into low-level SSA"
291 "Performs local optimizations"
292 "Register allocation"
293 "Machine code generation: x86, x86-64, PowerPC"
295 { $slide "Optimizing compiler"
296 "Makes high-level language features cheap to use"
297 "Eliminate redundant method dispatch by inferring types"
298 "Eliminate redundant integer overflow checks by inferring ranges"
300 { $slide "Optimizing compiler"
301 "Eliminate redundant memory allocation (escape analysis)"
302 "Eliminate redundant loads/stores (alias analysis)"
303 "Eliminate redundant computations (value numbering)"
305 { $slide "Project infrastructure"
306 { $url "http://factorcode.org" }
307 { $url "http://concatenative.org" }
308 { $url "http://docs.factorcode.org" }
309 { $url "http://planet.factorcode.org" }
310 "Uses our HTTP server, SSL, DB, Atom libraries..."
312 { $slide "Project infrastructure"
313 "Build farm, written in Factor"
315 "Builds Factor and all libraries, runs tests, makes binaries"
316 "Good for increasing stability"
319 "#concatenative irc.freenode.net: 60-70 users"
320 "factor-talk@lists.sf.net: 189 subscribers"
321 "About 30 people have code in the Factor repository"
322 "Easy to get started: binaries, lots of docs, friendly community..."
324 { $slide "Selling points"
325 "Expressive language"
326 "Comprehensive library"
327 "Efficient implementation"
328 "Powerful interactive tools"
329 "Stand-alone application deployment"
332 { $slide "That's all, folks"
333 "It is hard to cover everything in a single talk"
334 "Factor has many cool things that I didn't talk about"
339 : otug-talk ( -- ) otug-slides slides-window ;