]> gitweb.factorcode.org Git - factor.git/blob - extra/otug-talk/otug-talk.factor
231700c0e0c5ccaac5cee2479d22591ac65d7eea
[factor.git] / extra / otug-talk / otug-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 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 ;
10 IN: otug-talk
11
12 : $tetris ( element -- )
13     drop [ <default-tetris> <tetris-gadget> gadget. ] ($block) ;
14
15 CONSTANT: otug-slides
16 {
17     { $slide "Factor!"
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"
24     }
25     { $slide "Part 1: the language" }
26     { $slide "Basics"
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"
31         "Example:"
32         { $code "2 3 + ." }
33     }
34     { $slide "Quotations"
35         { "A " { $emphasis "quotation" } " is a block of code pushed on the stack" }
36         { "Syntax: " { $snippet "[ ... ]" } }
37         "Example:"
38         { $code
39             "\"/etc/passwd\" ascii file-lines"
40             "[ \"#\" head? not ] filter"
41             "[ \":\" split first ] map"
42             "."
43         }
44     }
45     { $slide "Words"
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" }
52     }
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"
59     }
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 ." }
65     }
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? } ", ..." }
72     }
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 ." }
78     }
79     { $slide "Data flow combinators - cleave family"
80         { { $link bi } ", " { $link tri } ", " { $link cleave } }
81         { $image "resource:extra/otug-talk/bi.tiff" }
82     }
83     { $slide "Data flow combinators - cleave family"
84         { { $link 2bi } ", " { $link 2tri } ", " { $link 2cleave } }
85         { $image "resource:extra/otug-talk/2bi.tiff" }
86     }
87     { $slide "Data flow combinators"
88         "First, let's define a data type:"
89         { $code "TUPLE: person first-name last-name ;" }
90         "Make an instance:"
91         { $code "person new" "    \"Joe\" >>first-name" "    \"Sixpack\" >>last-name" }
92     }
93     { $slide "Data flow combinators"
94         "Let's do stuff with it:"
95         { $code
96             "[ first-name>> ] [ last-name>> ] bi"
97             "[ 2 head ] [ 5 head ] bi*"
98             "[ >upper ] bi@"
99             "\".\" glue ."
100         }
101     }
102     { $slide "Data flow combinators - spread family"
103         { { $link bi* } ", " { $link tri* } ", " { $link spread } }
104         { $image "resource:extra/otug-talk/bi_star.tiff" }
105     }
106     { $slide "Data flow combinators - spread family"
107         { { $link 2bi* } }
108         { $image "resource:extra/otug-talk/2bi_star.tiff" }
109     }
110     { $slide "Data flow combinators - apply family"
111         { { $link bi@ } ", " { $link tri@ } ", " { $link napply } }
112         { $image "resource:extra/otug-talk/bi_at.tiff" }
113     }
114     { $slide "Data flow combinators - apply family"
115         { { $link 2bi@ } }
116         { $image "resource:extra/otug-talk/2bi_at.tiff" }
117     }
118     { $slide "Shuffle words"
119         "When data flow combinators are not enough"
120         { $link "shuffle-words" }
121         "Lower-level, Forth/PostScript-style stack manipulation"
122     }
123     { $slide "Locals"
124         "When data flow combinators and shuffle words are not enough"
125         "Name your input parameters"
126         "Used in about 1% of all words"
127     }
128     { $slide "Locals example"
129         "Area of a triangle using Heron's formula"
130         { $code
131             """:: area ( a b c -- x )
132     a b c + + 2 / :> p
133     p
134     p a - *
135     p b - *
136     p c - * sqrt ;"""
137         }
138     }
139     { $slide "Previous example without locals"
140         "A bit unwieldy..."
141         { $code
142             """: area ( a b c -- x )
143     [ ] [ + + 2 / ] 3bi
144     [ '[ _ - ] tri@ ] [ neg ] bi
145     * * * sqrt ;""" }
146     }
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 ;
150
151 : area ( points -- x )
152     [ 0 suffix ] [ sum 2 / ] bi
153     v-n product sqrt ;""" }
154     }
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"
160     ! }
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" }
167     }
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" }
172     }
173     { $slide "“Perfect” factoring"
174         { $table
175             { { $link head } { $link head-slice } }
176             { { $link tail } { $link tail-slice } }
177         }
178         { "Modifier: " { $link from-end } }
179         { "Modifier: " { $link short } }
180         "4*2*2=16 operations, 6 words!"
181     }
182     { $slide "Modifiers"
183         "“Modifiers” can express MN combinations using M+N words"
184         { $code
185             "\"Hello, Joe\" 4 head ."
186             "\"Hello, Joe\" 3 tail ."
187             "\"Hello, Joe\" 3 from-end tail ."
188         }
189         { $code
190             "\"Hello world\" 5 short head ."
191             "\"Hi\" 5 short tail ."
192         }
193     }
194     { $slide "Modifiers"
195         { "C-style " { $snippet "while" } " and " { $snippet "do while" } " loops" }
196     }
197     { $slide "Modifiers"
198         { $code ": bank ( n -- n )" "    readln string>number +" "    dup \"Balance: $\" write . ;" }
199         { $code "0 [ dup 0 > ] [ bank ] while" }
200     }
201     { $slide "Modifiers"
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 } }
205     }
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):" }
208         { $code
209             "dup [ orders>> ] when"
210             "dup [ first ] when"
211             "dup [ price>> ] when"
212         }
213     }
214     { $slide "This is hard with mainstream syntax!"
215         { $code
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);""" }
220     }
221     { $slide "An ad-hoc solution"
222         "Something like..."
223         { $code "var price = customer.?orders.?[0].?price;" }
224     }
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"
230     ! }
231     { $slide "Part 2: the implementation" }
232     { $slide "Interactive development"
233         { $tetris }
234     }
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"
240     }
241     { $slide "The UI"
242         "Renders with OpenGL"
243         "Backends for Cocoa, Windows, X11: managing windows, input events, clipboard"
244         "Cross-platform API"
245     }
246     { $slide "UI example"
247         { $code
248     """<pile>
249     { 5 5 } >>gap
250     1 >>fill
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""" }
256     }
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" }
262     }
263     { $slide "The VM"
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" }
270     }
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"
275     }
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, ..."
281     }
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"
286     }
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"
294     }
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"
299     }
300     { $slide "Optimizing compiler"
301         "Eliminate redundant memory allocation (escape analysis)"
302         "Eliminate redundant loads/stores (alias analysis)"
303         "Eliminate redundant computations (value numbering)"
304     }
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..."
311     }
312     { $slide "Project infrastructure"
313         "Build farm, written in Factor"
314         "12 platforms"
315         "Builds Factor and all libraries, runs tests, makes binaries"
316         "Good for increasing stability"
317     }
318     { $slide "Community"
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..."
323     }
324     { $slide "Selling points"
325         "Expressive language"
326         "Comprehensive library"
327         "Efficient implementation"
328         "Powerful interactive tools"
329         "Stand-alone application deployment"
330         "Moving fast"
331     }
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"
335         "Questions?"
336     }
337 }
338
339 : otug-talk ( -- ) otug-slides slides-window ;
340
341 MAIN: otug-talk