summaryrefslogtreecommitdiff
path: root/svfig-talk/svfig-talk.factor
blob: 3009b85b486f58fd34763755f99832a532ad5df1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
USING: help.markup namespaces peg.ebnf slides typed ;
IN: svfig-talk

CONSTANT: svfig-slides
{
    { $slide "Factor!"
        { $url "https://factorcode.org" }
        "Development started in 2003"
        "Open source (BSD license)"
        "Influenced by Forth, Lisp, and Smalltalk"
        "Blurs the line between language and library"
        "Interactive development"
        "Upcoming release: 0.99, then... 0.100?"
    }

    { $slide "Concepts"
        "Concatenative"
        "Dynamic types"
        "Extensible syntax"
        "Fully-compiled"
        "Cross-platform"
        "Clickable"
        "Useful?"
    }

    { $slide "Concatenative"
        { $code "1 2 + ." }
        { $code "3 weeks ago noon ." }
        { $code "\"hello\" rot13 ." }
        { $code "URL\" https://factorcode.org\" http-get" }
        { $code "10 [ \"Hello, Factor\" print ] times" }
        { $code "{ 4 8 15 16 23 42 } [ sum ] [ length ] bi / ." }
    }

    { $slide "Words"
        "Defined at parse time"
        "Parts: name, stack effect, definition"
        "Composed of tokens separated by whitespace"
        { $code ": palindrome? ( string -- ? ) dup reverse = ;" }
        "Unit tests"
        { $code "{ f } [ \"hello\" palindrome? ] unit-test"
                ""
                "{ t } [ \"racecar\" palindrome? ] unit-test"
        }
    }

    { $slide "Quotations"
        "Quotation: un-named blocks of code"
        { $code "[ \"Hello, World\" print ]" }
        "Combinators: words taking quotations"
        { $code "10 dup 0 < [ 1 - ] [ 1 + ] if ." }
        { $code "{ -1 1 -2 0 3 } [ 0 max ] map ." }
    }

    { $slide "Vocabularies"
        "Vocabularies: named sets of words"
        { $link "vocab-index" }
        { { $link POSTPONE: USING: } " loads dependencies" }
        "Source, docs, tests in one place"
    }

    { $slide "Debugging Tools"
        { "Let's implement the " { $snippet "fortune" } " program" }
        { $code
            "\"/usr/local/share/games/fortunes/science\""
            "ascii file-lines"
            "{ \"%\" } split random"
            "[ print ] each"
        }
        "Let's try it out!"
    }

    { $slide "Native Performance"
        { $snippet "wc -l factor.image" }
        "Counting lines is reasonably fast..."
        { $code
            ": simple-wc ( path -- n )"
            "    binary <file-reader> ["
            "        0 swap ["
            "            [ CHAR: \\n = ] count +"
            "        ] each-stream-block-slice"
            "    ] with-disposal ;"
        }
        { $code "[ \"factor.image\" simple-wc ] time" }
    }

    { $slide "Native Performance"
        "But it can be even faster!"
        { $code
            "USE: tools.wc"
            "[ \"factor.image\" wc ] time"
        }
        "Deploy as binary"
        { $code "\"tools.wc\" deploy" }
    }

    { $slide "Visual Tools"
        { $code
            "\"Bigger\" { 12 18 24 72 } ["
            "    font-size associate format nl"
            "] with each"
        }
        { $code
            "10 <iota> ["
            "    \"Hello, World!\""
            "    swap 10 / 1 over - over 1 <rgba>"
            "    background associate format nl"
            "] each"
        }
        { $code
            "USE: images.http"
            "\"https://factorcode.org/logo.png\" http-image."
        }
        { $code
            "USE: lcd"
            "<time-display> gadget."
        }
    }

    { $slide "Interactive Development"
        "Programming is hard, let's play tetris"
        { $vocab-link "tetris" }
        "Tetris is hard too... let's cheat"
        { $code "\"tetris.tetromino\" edit" }
        { "Factor workflow: change code, " { $snippet "F2" } ", test, repeat" }
    }

    { $slide "Parsing Words"
        "Extensible syntax, DSLs"
        "Most parsing words fall in one of two categories"
        "First category: literal syntax for new data types"
        "Second category: defining new types of words"
        "Some parsing words are more complicated"
    }

    { $slide "Parsing Words - Pairs"
        "Generic array syntax"
        { $code "{ 1 2 }" }
        { $code "\\ { see" }
        "Custom pair syntax"
        { $code "SYNTAX: => dup pop scan-object 2array suffix! ;" }
        { $code "1 => 2" }
    }

    { $slide "Parsing Words - Dice"
        { $vocab-link "dice" }
        { $code "ROLL: 2d8+5"
                ""
                "\"You do %s points of damage!\\n\" printf" }
        { $code "\\ ROLL: see" }
        { $code "[ ROLL: 2d8+5 ] ." }
    }

    { $slide "Parsing Words - Regexp"
        { $vocab-link "regexp" }
        "Pre-compiles regexp at parse time"
        "Implemented with library code"
        { $code "\"ababbc\" \"[ab]+c\" <regexp> matches? ." }
        { $code "\"ababbc\" R/ [ab]+c/ matches? ." }
    }

    { $slide "Parsing Words - XML"
        { $vocab-link "xml" }
        "Implemented with library code"
        "Useful syntax forms"
        { $code
            "{ \"three\" \"blind\" \"mice\" }"
            "[ [XML <li><-></li> XML] ] map"
            "[XML <ul><-></ul> XML]"
            "pprint-xml"
        }
    }

    { $slide "Local Variables"
        "Sometimes, there's no good stack solution to a problem"
        "Or, you're porting existing code in a quick-and-dirty way"
        "Combinator with 5 parameters!"
        { $code
            ":: branch ( a b neg zero pos -- )"
            "    a b = zero [ a b < neg pos if ] if ; inline"
        }
        "Unwieldy with the stack"
    }

    { $slide "Local Variables"
        { $code
            ": check-voting-age ( age -- )"
            "    18"
            "    [ \"You're underage, sorry...\" print ]"
            "    [ \"Yay, register to vote!\" print ]"
            "    [ \"Participate in democracy!\" print ]"
            "    branch ;"
        }
        "Locals are entirely implemented in Factor"
        "Example of compile-time meta-programming"
        "No performance penalty -vs- using the stack"
    }

    { $slide "Dynamic Variables"
        "Implemented as a stack of hashtables"
        { "Useful words are " { $link get } ", " { $link set } }
        "Input, output, error streams are stored in dynamic variables"
        "Read from a string..."
        { $code "\"cat\\ndog\\nfish\" [ readln ] with-string-reader" }
        "Read from a file..."
        { $code "\"LICENSE.txt\" utf8 [ readln ] with-file-reader" }
    }

    { $slide "Destructors"
        "Deterministic resource disposal"
        "Any step can fail and we don't want to leak resources"
        "We want to conditionally clean up sometimes"
        { $code ": do-stuff ( -- )
    [
        256 malloc &free
        256 malloc &free
        ... work goes here ...
    ] with-destructors ;"
        }
    }

    { $slide "Profiling"
        { $code
            ": fib ( m -- n )"
            "    dup 1 > ["
            "        [ 1 - fib ] [ 2 - fib ] bi +"
            "    ] when ;"
        }
        { $code "[ 40 fib ] time" }
        "Very slow! Let's profile it..."
        { $code "[ 40 fib ] profile" }
        "Not tail recursive"
        "Call tree is huge"
    }

    { $slide "Profiling - Typed"
        { "Type declarations with " { $link POSTPONE: TYPED: } }
        { $code
            "TYPED: fib ( m: fixnum -- n )"
            "    dup 1 > ["
            "        [ 1 - fib ] [ 2 - fib ] bi +"
            "    ] when ;"
        }
        "A bit faster"
    }

    { $slide "Profiling - Memoize"
        { "Memoization using " { $link POSTPONE: MEMO: } }
        { $code
            "MEMO: fib ( m -- n )"
            "    dup 1 > ["
            "        [ 1 - fib ] [ 2 - fib ] bi +"
            "    ] when ;"
        }
        "Much faster"
        { $code "10,000 fib number>string "
                "80 group [ print ] each" }
    }

    { $slide "Macros"
        "Expand at compile-time"
        "Return a quotation to be compiled"
        "Can express non-static stack effects"
        { $code "MACRO: ndup ( n -- quot )"
                "    [ \\ dup ] [ ] replicate-as ;"
        }
        { $code "[ 5 ndup ] infer" }
        { $code "[ 5 ndup ] expand-macros" }
    }

    { $slide "PEG / EBNF"
        { { $link POSTPONE: EBNF: } ": a complex parsing word" }
        "Implements a custom syntax for expressing parsers"
        { "Example: " { $vocab-link "printf" } }
        { $code "\"Factor\""
                "2003 <year> ago duration>years"
                ""
                "\"%s is %d years old\\n\" printf" }
        { $code "[ \"%s monkeys\" printf ] expand-macros" }
    }

    { $slide "Objects"
        "A tuple is a user-defined class which holds named values."
        { $code
            "TUPLE: rectangle width height ;"
            ""
            "TUPLE: circle radius ;"
        }
    }

    { $slide "Objects"
        "Constructing instances:"
        { $code "rectangle new" }
        { $code "rectangle boa" }
        "Let's encapsulate:"
        { $code
            ": <rectangle> ( w h -- r ) rectangle boa ;"
            ""
            ": <circle> ( r -- c ) circle boa ;"
        }
    }

    { $slide "Single Dispatch"
        ! "Generic words and methods"
        { $code "GENERIC: area ( shape -- n )" }
        "Two methods:"
        { $code
            "M: rectangle area"
            "    [ width>> ] [ height>> ] bi * ;"
            ""
            "M: circle area radius>> sq pi * ;"
        }
        "We can compute areas now."
        { $code "100 20 <rectangle> area ." }
        { $code "3 <circle> area ." }
    }

    { $slide "Multiple Dispatch"
        { $code "SINGLETONS: rock paper scissors ;" }
        "Win conditions:"
        { $code "FROM: multi-methods => GENERIC: METHOD: ;"
                ""
                "GENERIC: beats? ( obj1 obj2 -- ? )"
                ""
                "METHOD: beats? { scissors paper } 2drop t ;"
                "METHOD: beats? { rock  scissors } 2drop t ;"
                "METHOD: beats? { paper     rock } 2drop t ;"
                "METHOD: beats? { object  object } 2drop f ;"
        }
    }

    { $slide "Multiple Dispatch"
        "Let's play a game..."
        { $code ": play. ( obj -- )"
                "    { rock paper scissors } random {"
                "        { [ 2dup beats? ] [ \"WIN\" ] }"
                "        { [ 2dup = ] [ \"TIE\" ] }"
                "        [ \"LOSE\" ]"
                "    } cond \"%s vs. %s: %s\\n\" printf ;"
        }
        "With a simple interface:"
        { $code ": rock ( -- ) \\ rock play. ;"
                ": paper ( -- ) \\ paper play. ;"
                ": scissors ( -- ) \\ scissors play. ;"
        }
    }

    { $slide "Object System"
        "Supports \"duck typing\""
        "Two tuples can have a slot with the same name"
        "Code that uses accessors will work on both"
        "Objects are not hashtables; slot access is very fast"
        "Tuple slots can be reordered/redefined"
        "Instances in memory will be updated"
    }

    { $slide "Object System"
        "Predicate classes"
        { $code
            "PREDICATE: positive < integer 0 > ;"
            "PREDICATE: negative < integer 0 < ;"
            ""
            "GENERIC: abs ( m -- n )"
            ""
            "M: positive abs ;"
            "M: negative abs -1 * ;"
            "M: integer abs ;"
        }
    }
    { $slide "Object System"
        "And lots more features..."
        "Inheritance, type declarations, read-only slots, union, intersection, singleton classes, reflection"
        "Object system is entirely implemented in Factor"
    }

    { $slide "Assembly"
        "Access the Time Stamp Counter"
        { $code
"HOOK: rdtsc cpu ( -- n )

M: x86.64 rdtsc
    longlong { } cdecl [
        RAX 0 MOV
        RDTSC
        RDX 32 SHL
        RAX RDX OR
    ] alien-assembly ;" }
    }

    { $slide "FFI"
        { $code "NAME
     sqrt – square root function

SYNOPSIS
     #include <math.h>

     double
     sqrt(double x);" }
        "Let's use it!"
        { $code "FUNCTION: double sqrt ( double x )" }
    }

    { $slide "Infix"
        { "Syntax experiments with " { $vocab-link "infix" } }
        "Infix word definitions:"
        { $code "INFIX:: foo ( x y -- z ) sqrt(x)+y**3 ;" }
        "Inline also:"
        { $code "[let \"hello\" :> seq"
                "    [infix seq[::-1] infix]"
                "]"
        }
    }

    { $slide "Implementation"
        "VM in C++ (12,000 lines of code)"
        "VM features primitives, garbage collection, etc."
        "Lines of code: 300,000"
        "Lines of tests: 80,000"
        "Lines of docs: 70,000"
        "One big repository, and we love contributions!"
    }

    { $slide "Project Infrastructure"
        { $url "https://factorcode.org" }
        { $url "https://concatenative.org" }
        { $url "https://docs.factorcode.org" }
        { $url "https://planet.factorcode.org" }
        { $url "https://paste.factorcode.org" }
        "Uses our HTTP server, SSL, DB, Atom libraries..."
    }

    { $slide "Project Infrastructure"
        "Build farm, written in Factor"
        "Multiple OS and architecture"
        "Builds Factor and all libraries, runs tests, makes binaries"
        "Saves us from the burden of making releases by hand"
        "Maintains stability"
    }

    { $slide "Demo"
        "It is hard to cover everything in a single talk"
        "Factor has many cool things that I didn't talk about"
        { $code "\"demos\" run" }
        "Let's look at a real program!"
    }

    { $slide "Cool Things"
        { $code
            "USE: xkcd"
            "XKCD: 138"
        }
        { $code
            "USE: reddit"
            "\"programming\" subreddit."
        }
    }

    { $slide "Cool Things"
        { $vocab-link "minesweeper" }
        { $vocab-link "game-of-life" }
        { $vocab-link "boids" }
        { $vocab-link "pong" }
    }

    { $slide "Cool Things"
        "8080 cpu emulator"
        { $code
            "\"resource:roms\" rom-root set-global"
        }
        { $vocab-link "roms.space-invaders" }
    }

    { $slide "Cool Things"
        { $vocab-link "bloom-filters" }
        { $vocab-link "cuckoo-filters" }
        { $vocab-link "persistent" }
        { $vocab-link "trees" }
        { $vocab-link "tuple-arrays" }
        { $vocab-link "specialized-arrays" }
    }

    { $slide "Cool Things"
        { $code
            "USE: text-to-speech"
            "\"hello\" speak-text"
        }
        { $code
            "USE: morse"
            "\"hello\" play-as-morse"
        }
        { $code
            "USE: flip-text"
            "\"hello\" flip-text ."
        }
        { $code
            "USE: emojify"
            "\"I :heart: Factor! :+1!\" emojify ."
        }
    }

    { $slide "Cool Things"
        { $code
            "USE: google.charts"
            "\"x = \\\\frac{-b \\\\pm \\\\sqrt {b^2-4ac}}{2a}\""
            "<formula> 200 >>width 75 >>height chart."
        }
        { $code
            "100 [ 100 random ] replicate"
            "100 [ 100 random ] replicate"
            "zip <scatter> chart."
        }
        { $code
            "\"/usr/share/dict/words\" utf8 file-lines"
            "[ >lower 1 head ] histogram-by"
            "sort-keys <bar>"
            "    COLOR: green >>foreground"
            "    400 >>width"
            "    10 >>bar-width"
            "chart."
        }
    }

    { $slide "Cool Things"
        "Tab completion"
        { $code
            "http"
        }
        { $code
            "P\" vocab:math"
        }
        { $code
            "COLOR: "
        }
    }

    { $slide "Cool things"
        { $code "./factor -run=file-server" }
        { $code "./factor -run=file-monitor" }
        { $code "./factor -run=tools.dns microsoft.com" }
        { $code "./factor -run=tools.cal" }
    }
}

: svfig-talk ( -- ) svfig-slides "SVFIG Talk" slides-window ;

MAIN: svfig-talk