]> gitweb.factorcode.org Git - factor-talks.git/blob - hmc-talk/hmc-talk.factor
Remove "talks." namespace.
[factor-talks.git] / hmc-talk / hmc-talk.factor
1 USING: fry help.markup help.topics kernel locals math
2 math.functions memoize peg.ebnf slides ;
3 IN: hmc-talk
4
5 CONSTANT: hmc-slides
6 {
7     { $slide "Factor!"
8         { $url "http://factorcode.org" }
9         "Development started in 2003"
10         "Open source (BSD license)"
11         "Influenced by Forth, Lisp, and Smalltalk"
12         "Blurs the line between language and library"
13         "Interactive development"
14     }
15
16     { $slide "Concepts"
17         "Concatenative"
18         "Dynamic types"
19         "Extensible syntax"
20         "Fully-compiled"
21         "Cross-platform"
22         "Interactive Development"
23         "Code is data"
24         "Pervasive unit testing"
25         "Clickable"
26     }
27
28     { $slide "Words and the stack"
29         "Stack based, dynamically typed"
30         { $code "{ 1 1 3 4 4 8 9 9 } dup duplicates diff ." }
31         "Words: named code snippets"
32         { $code ": remove-duplicates ( seq -- seq' )" "    dup duplicates diff ;" }
33         { $code "{ 1 1 3 4 4 8 9 9 } remove-duplicates ." }
34     }
35
36     { $slide "Words and the stack"
37         { $code
38             "\"/opt/homebrew/share/games/fortunes/science\""
39             "ascii file-lines"
40             "{ \"%\" } split random"
41             "[ print ] each"
42         }
43         { $code
44             ": fortune ( -- )"
45             "    \"/opt/homebrew/share/games/fortunes/science\""
46             "    ascii file-lines"
47             "    { \"%\" } split random"
48             "    [ print ] each ;"
49         }
50         { $code
51             "5 [ fortune nl ] times"
52         }
53     }
54
55     { $slide "Vocabularies"
56         "Vocabularies: named sets of words"
57         { $link "vocab-index" }
58         { { $link POSTPONE: USING: } " loads dependencies" }
59         "Source, docs, tests in one place"
60     }
61     { $slide "Interactive development"
62         "Programming is hard, let's play tetris"
63         { $vocab-link "tetris" }
64         "Tetris is hard too... let's cheat"
65         "Factor workflow: change code, F2, test, repeat"
66     }
67     { $slide "Quotations"
68         "Quotation: unnamed block of code"
69         "Combinators: words taking quotations"
70         { $code "10 dup 0 < [ 1 - ] [ 1 + ] if ." }
71         { $code "{ -1 1 -2 0 3 } [ 0 max ] map ." }
72         "Partial application:"
73         { $code ": clamp ( seq n -- seq' ) '[ _ max ] map ;" "{ -1 1 -2 0 3 } 0 clamp" }
74     }
75     { $slide "Object system"
76         "CLOS with single dispatch"
77         "A tuple is a user-defined class which holds named values."
78         { $code
79             "TUPLE: rectangle width height ;"
80             "TUPLE: circle radius ;"
81         }
82     }
83     { $slide "Object system"
84         "Constructing instances:"
85         { $code "rectangle new" }
86         { $code "rectangle boa" }
87         "Let's encapsulate:"
88         { $code
89             ": <rectangle> ( w h -- r ) rectangle boa ;"
90             ": <circle> ( r -- c ) circle boa ;"
91         }
92     }
93     { $slide "Object system"
94         "Generic words and methods"
95         { $code "GENERIC: area ( shape -- n )" }
96         "Two methods:"
97         { $code
98             "USE: math.constants"
99             ""
100             "M: rectangle area"
101             "    [ width>> ] [ height>> ] bi * ;"
102             ""
103             "M: circle area radius>> sq pi * ;"
104         }
105     }
106     { $slide "Object system"
107         "We can compute areas now."
108         { $code "100 20 <rectangle> area ." }
109         { $code "3 <circle> area ." }
110     }
111     { $slide "Object system"
112         "Object system handles dynamic redefinition very well"
113         { $code "TUPLE: person name age occupation ;" }
114         "Make an instance..."
115     }
116     { $slide "Object system"
117         "Let's add a new slot:"
118         { $code "TUPLE: person name age address occupation ;" }
119         "Fill it in with inspector..."
120         "Change the order:"
121         { $code "TUPLE: person name occupation address ;" }
122     }
123     { $slide "Object system"
124         "How does it work?"
125         "Objects are not hashtables; slot access is very fast"
126         "Redefinition walks the heap; expensive but rare"
127     }
128     { $slide "Object system"
129         "Supports \"duck typing\""
130         "Two tuples can have a slot with the same name"
131         "Code that uses accessors will work on both"
132         "Accessors are auto-generated generic words"
133     }
134     { $slide "Object system"
135         "Predicate classes"
136         { $code
137             "PREDICATE: positive < integer 0 > ;"
138             "PREDICATE: negative < integer 0 < ;"
139             ""
140             "GENERIC: abs ( n -- )"
141             ""
142             "M: positive abs ;"
143             "M: negative abs -1 * ;"
144             "M: integer abs ;"
145         }
146     }
147     { $slide "Object system"
148         "More: inheritance, type declarations, read-only slots, union, intersection, singleton classes, reflection"
149         "Object system is entirely implemented in Factor"
150     }
151     { $slide "The parser"
152         "All data types have a literal syntax"
153         "Literal hashtables and arrays are very useful in data-driven code"
154         "\"Code is data\" because quotations are objects (enables Lisp-style macros)"
155         { $code "H{ { \"cookies\" 12 } { \"milk\" 10 } }" }
156         "Libraries can define new parsing words"
157     }
158     { $slide "Example: regexp"
159         { $vocab-link "regexp" }
160         "Pre-compiles regexp at parse time"
161         "Implemented with library code"
162         { $code "USE: regexp" }
163         { $code "\"ababbc\" \"[ab]+c\" <regexp> matches? ." }
164         { $code "\"ababbc\" R/ [ab]+c/ matches? ." }
165     }
166     { $slide "Example: memoization"
167         { "Memoization with " { $link POSTPONE: MEMO: } }
168         { $code
169             ": fib ( m -- n )"
170             "    dup 1 > ["
171             "        [ 1 - fib ] [ 2 - fib ] bi +"
172             "    ] when ;"
173         }
174         "Very slow! Let's profile it..."
175     }
176     { $slide "Example: memoization"
177         { "Let's use " { $link POSTPONE: MEMO: } " instead of " { $link POSTPONE: : } }
178         { $code
179             "MEMO: fib ( m -- n )"
180             "    dup 1 > ["
181             "        [ 1 - fib ] [ 2 - fib ] bi +"
182             "    ] when ;"
183         }
184         "Much faster"
185     }
186     { $slide "Meta-circularity"
187         { { $link POSTPONE: MEMO: } " is just a library word" }
188         { "But so is " { $link POSTPONE: : } }
189         "Factor's parser is written in Factor"
190         { "All syntax is just parsing words: " { $link POSTPONE: [ } ", " { $link POSTPONE: " } }
191     }
192     { $slide "Extensible syntax, DSLs"
193         "Most parsing words fall in one of two categories"
194         "First category: literal syntax for new data types"
195         "Second category: defining new types of words"
196         "Some parsing words are more complicated"
197     }
198
199     { $slide "Example: printf"
200         { { $link POSTPONE: EBNF: } ": a complex parsing word" }
201         "Implements a custom syntax for expressing parsers: like OMeta!"
202         { "Example: " { $vocab-link "printf" } }
203         { $code "\"cheese\" \"vegan\" \"%s is not %s\\n\" printf" }
204         { $code "\"Factor\" 5 \"%s is %d years old\\n\" printf" }
205         { $code "[ \"%s monkeys\" printf ] expand-macros" }
206     }
207     { $slide "Locals and lexical scope"
208         "Sometimes, there's no good stack solution to a problem"
209         "Or, you're porting existing code in a quick-and-dirty way"
210         "Our solution: implement named locals as a DSL in Factor"
211         "Influenced by Scheme and Lisp"
212     }
213     { $slide "Locals and lexical scope"
214         { "Define lambda words with " { $link POSTPONE: :: } }
215         { "Establish bindings with " { $link POSTPONE: [let } " and " { $snippet "[let*" } }
216         "Mutable bindings with correct semantics"
217         { "Named inputs for quotations with " { $link POSTPONE: [| } }
218         "Full closures"
219     }
220     { $slide "Locals and lexical scope"
221         "Combinator with 5 parameters!"
222         { $code
223             ":: branch ( a b neg zero pos -- )"
224             "    a b = zero [ a b < neg pos if ] if ; inline"
225         }
226         "Unwieldy with the stack"
227     }
228     { $slide "Locals and lexical scope"
229         { $code
230             ": check-drinking-age ( age -- )"
231             "    21"
232             "    [ \"You're underage!\" print ]"
233             "    [ \"Grats, you're now legal\" print ]"
234             "    [ \"Go get hammered\" print ]"
235             "    branch ;"
236         }
237     }
238     { $slide "Locals and lexical scope"
239         "Locals are entirely implemented in Factor"
240         "Example of compile-time meta-programming"
241         "No performance penalty -vs- using the stack"
242         "In the base image, only 59 words out of 13,000 use locals"
243     }
244     { $slide "More about partial application"
245         { { $link POSTPONE: '[ } " is \"fry syntax\"" }
246         { $code "'[ _ + ] == [ + ] curry" }
247         { $code "'[ @ t ] == [ t ] compose" }
248         { $code "'[ _ nth @ ] == [ [ nth ] curry ] dip compose" }
249         { $code "'[ [ _ ] dip nth ] == [ [ ] curry dip nth ] curry" }
250         { "Fry and locals desugar to " { $link curry } ", " { $link compose } }
251     }
252     { $slide "Help system"
253         "Help markup is just literal data"
254         { "Look at the help for " { $link T{ link f + } } }
255         "These slides are built with the help system and a custom style sheet"
256         { $vocab-link "hmc-talk" }
257     }
258     { $slide "Why stack-based?"
259         "Because nobody else is doing it"
260         "Interesting properties: concatenation is composition, chaining functions together, \"fluent\" interfaces, new combinators"
261         { $vocab-link "simple-rpg" }
262         { $code
263             "{ \"chicken\" \"beef\" \"pork\" \"turkey\" }"
264             "[ 5 short head ] map ."
265         }
266     }
267     { $slide "Implementation"
268         "VM: garbage collection, bignums, ..."
269         "Bootstrap image: parser, hashtables, object system, ..."
270         "Non-optimizing compiler"
271         "Stage 2 bootstrap: optimizing compiler, UI, ..."
272         "Full image contains machine code"
273     }
274     { $slide "Compiler"
275         { "Let's look at " { $vocab-link "benchmark.mandel" } }
276         "A naive implementation would be very slow"
277         "Combinators, partial application"
278         "Boxed complex numbers"
279         "Boxed floats"
280         { "Redundancy in " { $link absq } " and " { $link sq } }
281     }
282     { $slide "Compiler: high-level optimizer"
283         "High-level SSA IR"
284         "Type inference (classes, intervals, arrays with a fixed length, literals, ...)"
285         "Escape analysis and tuple unboxing"
286     }
287     { $slide "Compiler: high-level optimizer"
288         "Loop index becomes a fixnum, complex numbers unboxed, generic arithmetic inlined, higher-order code become first-order..."
289         { $code "[ c pixel ] optimized." }
290     }
291     { $slide "Compiler: low-level optimizer"
292         "Low-level SSA IR"
293         "Alias analysis"
294         "Value numbering"
295         "Linear scan register allocation"
296     }
297     { $slide "Compiler: low-level optimizer"
298         "Redundant stack operations eliminated, intermediate floats unboxed..."
299         { $code "[ c pixel ] regs." }
300     }
301     { $slide "Compiler: assembly"
302         "Generic assembly generated..."
303         { $code "[ c pixel ] disassemble" }
304     }
305     { $slide "Compiler: assembly"
306         "Efficient assembly generated..."
307         { $code "[ { fixnum fixnum } declare c pixel ] disassemble" }
308     }
309     { $slide "Garbage collection"
310         "All roots are identified precisely"
311         "Generational copying for data"
312         "Mark sweep for native code"
313     }
314
315     { $slide "Project infrastructure"
316         { $url "http://factorcode.org" }
317         { $url "http://concatenative.org" }
318         { $url "http://docs.factorcode.org" }
319         { $url "http://planet.factorcode.org" }
320         { $url "http://paste.factorcode.org" }
321         "Uses our HTTP server, SSL, DB, Atom libraries..."
322     }
323     { $slide "Project infrastructure"
324         "Build farm, written in Factor"
325         "Multiple OS and architecture"
326         "Builds Factor and all libraries, runs tests, makes binaries"
327         "Saves us from the burden of making releases by hand"
328         "Maintains stability"
329     }
330
331     { $slide "That's all, folks"
332         "It is hard to cover everything in a single talk"
333         "Factor has many cool things that I didn't talk about"
334         "Questions?"
335     }
336
337     { $slide "Cool things"
338         { $code
339             "USE: xkcd"
340             "XKCD: 138"
341         }
342         { $code
343             "USE: reddit"
344             "\"programming\" subreddit."
345         }
346     }
347
348     { $slide "Cool things"
349         { $vocab-link "minesweeper" }
350         { $vocab-link "game-of-life" }
351         { $vocab-link "boids" }
352     }
353
354     { $slide "Cool things"
355         "8080 cpu emulator"
356         { $code
357             "\"resource:roms\" rom-root set-global"
358         }
359         { $vocab-link "roms.space-invaders" }
360     }
361
362     { $slide "Cool things"
363         { $vocab-link "bloom-filters" }
364         { $vocab-link "cuckoo-filters" }
365         { $vocab-link "persistent" }
366         { $vocab-link "trees" }
367         { $vocab-link "tuple-arrays" }
368         { $vocab-link "specialized-arrays" }
369     }
370
371     { $slide "Cool things"
372         { $code
373             "USE: text-to-speech"
374             "\"hello\" speak-text"
375         }
376         { $code
377             "USE: morse"
378             "\"hello\" play-as-morse"
379         }
380         { $code
381             "USE flip-text"
382             "\"hello\" flip-text ."
383         }
384     }
385
386     { $slide "Cool things"
387         { $code
388             "{ 12 18 24 72 }"
389             "[ \"Bigger\" swap font-size associate format nl ] each"
390         }
391
392         { $code
393             "10 <iota> ["
394             "   \"Hello world\""
395             "   swap 10 / 1 over - over 1 <rgba>"
396             "   background associate format nl"
397             "] each"
398         }
399     }
400
401     { $slide "Cool things"
402         { $code
403             "USE: google.charts"
404             "\"x = \\\\frac{-b \\\\pm \\\\sqrt {b^2-4ac}}{2a}\""
405             "<formula> 200 >>width 75 >>height chart."
406         }
407         { $code
408             "100 [ 100 random ] replicate"
409             "100 [ 100 random ] replicate"
410             "zip <scatter> chart."
411         }
412         { $code
413             "\"/usr/share/dict/words\" utf8 file-lines"
414             "[ >lower 1 head ] histogram-by"
415             "sort-keys <bar>"
416             "    COLOR: green >>foreground"
417             "    400 >>width"
418             "    10 >>bar-width"
419             "chart."
420         }
421     }
422
423     { $slide "Cool things"
424         { $code
425             "USE: http.client"
426             "\\ http-get see"
427         }
428         { $code
429             "\"http\" apropos"
430         }
431         { $code
432             "USE: images.http"
433             "\"https://factorcode.org/logo.png\" http-image."
434         }
435     }
436
437     { $slide "Cool things"
438         "Tab completion"
439         { $code
440             "http"
441         }
442         { $code
443             "P\" vocab:math"
444         }
445         { $code
446             "COLOR: "
447         }
448     }
449
450     { $slide "Cool things"
451         { $code
452             "USE: emojify"
453             "\"I :heart: Factor! :+1!\" emojify ."
454         }
455         { $code
456             "USE: dice"
457             "ROLL: 2d8+4"
458             "\"You do %s points of damage!\" printf"
459         }
460     }
461
462     { $slide "Cool things"
463         { $code
464             "USING: sequences xml.syntax xml.writer ;"
465             "{ \"three\" \"blind\" \"mice\" }"
466             "[ [XML <li><-></li> XML] ] map"
467             "[XML <ul><-></ul> XML]"
468             "pprint-xml"
469         }
470     }
471
472     { $slide "Cool things"
473         { $code
474             "USE: io.streams.256color"
475             "[ listener ] with-256color"
476             "\"math\" about"
477         }
478     }
479
480     { $slide "Cool things"
481         { $code "./factor -run=tetris" }
482         { $code "./factor -run=file-server" }
483         { $code "./factor -run=file-monitor" }
484         { $code "./factor -run=tools.dns microsoft.com" }
485         { $code "./factor -run=tools.cal" }
486     }
487 }
488
489 : hmc-talk ( -- ) hmc-slides "HMC Talk" slides-window ;
490
491 MAIN: hmc-talk