1 ! Slava Pestov's presentation at the Montreal Scheme/Lisp User's
2 ! Group, November 22nd 2006.
4 ! http://schemeway.dyndns.org/mslug/mslug-home
6 USING: gadgets gadgets-books gadgets-borders gadgets-buttons
7 gadgets-text gadgets-labels gadgets gadgets-panes
8 gadgets-presentations gadgets-theme generic kernel math
9 namespaces sequences strings styles models help io arrays
26 { page-color { 0.4 0.4 0.4 0.3 } }
30 H{ { wrap-margin 700 } }
35 : $title ( string -- )
36 [ H{ { font "serif" } { font-size 36 } } format ] ($block) ;
41 T{ gradient f { { 0.25 0.25 0.25 1.0 } { 1.0 1.0 1.0 1.0 } } }
42 over set-gadget-interior
43 { 750 10 } over set-gadget-dim
44 { 1 0 } over set-gadget-orientation
49 T{ gradient f { { 0.8 0.8 1.0 1.0 } { 1.0 0.8 1.0 1.0 } } }
50 swap set-gadget-interior ;
52 : <page> ( list -- gadget )
54 mslug-stylesheet clone [
55 [ print-element ] with-default-style
60 : $slide ( element -- )
65 : $module ( element -- )
66 first dup module [ write ] ($code) ;
70 { $slide "Factor programming language"
71 "Inspirations: Forth, Joy, Common Lisp, others..."
72 "Powerful language features"
73 "Interactive development"
75 "Syntax: ``s-expressions''"
77 { "Simple evaluation semantics; left to right:"
78 { $code "2 3 + 7 * 9 /" } }
79 "Functions are called ``words''"
81 "Short definitions, heavy reuse"
83 { $slide "First program"
85 "Functional ``hello world'':"
87 ": factorial ( n -- n! )"
91 " dup 1 - factorial *"
95 "Shuffle words, conditionals, recursion, code as parameters..."
97 { $code "100 factorial ." }
99 { $code "\\ factorial see" }
101 { $slide "Data types - sequences"
102 { "Sequences: everything is an array"
103 { $code "{ 1 2 3 } { t f } append reverse ." } }
104 { "Familiar higher order functions"
105 { $code "{ { 1 2 } { 3 4 } } [ reverse ] map ." } }
106 { "Integers are sequences, too:"
107 { $code "100 [ sq ] map ." }
108 { $code "100 [ 4 mod 3 = ] subset ." }
109 { $code "0 100 [ 1 + * ] reduce ." } }
111 { $slide "Data types - hashtables"
112 "Hashtables: literal syntax, utility words, higher-order combinators..."
114 "H{ { \"grass\" \"green\" } { \"chicken\" \"white\" } }"
115 "H{ { \"carrot\" \"orange\" } }"
119 "Prettyprinter -vs- inspector"
121 { $slide "Data types - others"
122 "Queues, graphs, splay trees, double linked lists, lazy lists..."
126 { $code "SYMBOL: foo" "5 foo set" "foo get ." }
127 { $code "SYMBOL: foo" "[ 6 foo set foo get ] with-scope" }
128 "Dynamic scope can be analyzed statically:"
129 { $code "SYMBOL: bar" "bar get sq foo set" }
131 { $slide "Encapsulating variable usage"
132 { "Stream-like sequence construction:"
134 ": print-name ( name -- )"
135 " [ \"Greetings, \" % % \".\" % ] \"\" make print ;"
139 { $code "[ % ] \"\" make" }
141 { $code ": 100-bytes [ 100 read ] with-stream ;" "\"foo.txt\" <file-reader> 100-bytes" } }
143 { $slide "Custom data types"
144 { "Built-in classes: " { $link integer } ", " { $link array } ", " { $link hashtable } "..." }
145 { "You can define your own:"
146 { $code "TUPLE: rectangle w h ;" "TUPLE: circle r ;" }
148 { $code "100 200 <rectangle>" }
149 { $code "rectangle-w ." }
151 { $slide "Polymorphism"
154 "GENERIC: area ( shape -- n )"
156 " dup rectangle-w swap rectangle-h * ;"
157 "M: circle area circle-r sq pi * ;"
160 { "Methods in classes -vs- methods in functions?" }
161 { "Both: " { $link array } " -vs- " { $link nth } }
163 { $slide "More polymorphism"
164 "Tuples can have custom constructor words"
165 { "Delegation instead of inheritance"
167 "TUPLE: colored-shape color ;"
168 "C: colored-shape ( shape color -- shape )"
169 " [ set-colored-shape-color ] keep"
170 " [ set-delegate ] keep ;"
172 { $code "100 200 <rectangle>" "{ 0.5 0.5 1 } <colored-shape>" "area ." }
173 "Advanced features: predicate classes, union classes, method combination"
175 { $slide "The Factor UI"
176 "Factor UI is totally implemented in Factor"
177 "OpenGL, FreeType, plus platform-specific code"
180 "USING: gadgets gadgets-labels ;"
181 "\"Hello world\" <label> \"Hi\" open-titled-window"
183 "Presentations and operations"
188 "USING: models gadgets-scrolling gadgets-text ;"
190 "Create an editor, wrap it in a scroller:"
195 { $code "dup control-model"
196 "[ concat length number>string ] <filter>"
199 { $slide "Models continued"
202 " { [ <label-control> ] f f @top }"
203 " { [ <scroller> ] f f @center }"
208 "\"Model test\" open-titled-window"
212 "Uses Factor help markup language with a tweaked stylesheet"
213 "Each slide is a gadget"
214 "Slides are grouped in a custom gadget which handles Up/Down arrow keys to move between slides"
215 "Let's look at the source code:"
216 { $module "demos/mslug-talk" }
218 { $slide "The compiler"
219 "Performance is a goal"
220 "Compromise: The compiler only compiles words with a static stack effect"
222 { "First stage transforms a quotation into the " { $emphasis "dataflow representation" } }
223 "Second stage: high-level optimizations"
224 "Third stage: register allocation, peephole optimization, code generation"
225 "Compiled code saved in the image"
226 "Compiler invoked explicitly (not a ``JIT'')"
228 { $slide "High-level optimizer"
229 "Quotations <-> dataflow conversion is ``loss-less''"
230 "Really, just rewriting quotations"
233 "Arithmetic identities"
234 "Optimistic specialization"
236 { $slide "Low level optimizer"
237 "Caching stack in registers"
238 "Shuffling renames registers"
240 "Tail call optimization"
242 { $slide "Assember DSL"
243 "The compiler emits machine code:"
246 "\"n\" operand 2 SHR"
248 "\"obj\" operand \"n\" operand ADD"
249 "char-reg-16 \"obj\" operand string-offset [+] MOV"
250 "char-reg tag-bits SHL"
251 "\"obj\" operand char-reg MOV"
255 { $slide "Assember DSL - continued"
256 { "It's all done with " { $link make } }
257 { $code "USE: assembler" "[ EAX ECX MOV ] { } make ." }
260 { $url "http://factorcode.org" }
261 { $url "http://factor-language.blogspot.com" }
262 { "irc.freenode.net #concatenative" }
268 C: mslug ( -- gadget )
269 slides [ <page> ] map <book>
270 over set-gadget-delegate ;
272 : change-page ( book n -- )
273 over control-value + over gadget-children length rem
274 swap control-model set-model ;
276 : next-page ( book -- ) 1 change-page ;
278 : prev-page ( book -- ) -1 change-page ;
281 { T{ key-down f f "DOWN" } [ next-page ] }
282 { T{ key-down f f "UP" } [ prev-page ] }
285 PROVIDE: demos/mslug-talk ;
287 MAIN: demos/mslug-talk
288 <mslug> "Presentation" open-titled-window ;