]> gitweb.factorcode.org Git - factor.git/blob - demos/mslug-talk.factor
more sql changes
[factor.git] / demos / mslug-talk.factor
1 ! Slava Pestov's presentation at the Montreal Scheme/Lisp User's
2 ! Group, November 22nd 2006.
3
4 ! http://schemeway.dyndns.org/mslug/mslug-home
5
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
10 hashtables modules ;
11 IN: mslug
12
13 : mslug-stylesheet
14     H{
15         { default-style
16             H{
17                 { font "serif" }
18                 { font-size 24 }
19                 { wrap-margin 700 }
20             }
21         }
22         { code-style
23             H{
24                 { font "monospace" }
25                 { font-size 24 }
26                 { page-color { 0.4 0.4 0.4 0.3 } }
27             }
28         }
29         { table-content-style
30             H{ { wrap-margin 700 } }
31         }
32         { bullet "\u00b7 " }
33     } ;
34
35 : $title ( string -- )
36     [ H{ { font "serif" } { font-size 36 } } format ] ($block) ;
37
38 : $divider ( -- )
39     [
40         <gadget>
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
45         gadget.
46     ] ($block) ;
47
48 : page-theme
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 ;
51
52 : <page> ( list -- gadget )
53     [
54         mslug-stylesheet clone [
55             [ print-element ] with-default-style
56         ] bind
57     ] make-pane
58     dup page-theme ;
59
60 : $slide ( element -- )
61     unclip $title
62     $divider
63     $list ;
64
65 : $module ( element -- )
66     first dup module [ write ] ($code) ;
67
68 : slides
69 {
70 { $slide "Factor programming language"
71     "Inspirations: Forth, Joy, Common Lisp, others..."
72     "Powerful language features"
73     "Interactive development"
74     "Stack-based"
75     "Syntax: ``s-expressions''"
76     "Code is data"
77     { "Simple evaluation semantics; left to right:"
78     { $code "2 3 + 7 * 9 /" } }
79     "Functions are called ``words''"
80     "Vocabularies"
81     "Short definitions, heavy reuse"
82 }
83 { $slide "First program"
84     {
85         "Functional ``hello world'':"
86         { $code
87             ": factorial ( n -- n! )"
88             "    dup 0 <= ["
89             "        drop 1"
90             "    ] ["
91             "        dup 1 - factorial *"
92             "    ] if ;"
93         }
94     }
95     "Shuffle words, conditionals, recursion, code as parameters..."
96     "Bignums!"
97     { $code "100 factorial ." }
98     "Reflection:"
99     { $code "\\ factorial see" }
100 }
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 ." } }
110 }
111 { $slide "Data types - hashtables"
112     "Hashtables: literal syntax, utility words, higher-order combinators..."
113     { $code
114         "H{ { \"grass\" \"green\" } { \"chicken\" \"white\" } }"
115         "H{ { \"carrot\" \"orange\" } }"
116         "hash-union"
117     }
118     "Option+h"
119     "Prettyprinter -vs- inspector"
120 }
121 { $slide "Data types - others"
122     "Queues, graphs, splay trees, double linked lists, lazy lists..."
123 }
124 { $slide "Variables"
125     "Dynamic scope"
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" }
130 }
131 { $slide "Encapsulating variable usage"
132     { "Stream-like sequence construction:"
133     { $code
134         ": print-name ( name -- )"
135         "    [ \"Greetings, \" % % \".\" % ] \"\" make print ;"
136     } }
137     "Key factors:"
138     { $code "%" }
139     { $code "[ % ] \"\" make" }
140     { "Also, I/O:"
141     { $code ": 100-bytes [ 100 read ] with-stream ;" "\"foo.txt\" <file-reader> 100-bytes" } }
142 }
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 ;" }
147     }
148     { $code "100 200 <rectangle>" }
149     { $code "rectangle-w ." }
150 }
151 { $slide "Polymorphism"
152     { "Generic words:"
153         { $code
154             "GENERIC: area ( shape -- n )"
155             "M: rectangle area"
156             "    dup rectangle-w swap rectangle-h * ;"
157             "M: circle area circle-r sq pi * ;"
158         }
159     }
160     { "Methods in classes -vs- methods in functions?" }
161     { "Both: " { $link array } " -vs- " { $link nth } }
162 }
163 { $slide "More polymorphism"
164     "Tuples can have custom constructor words"
165     { "Delegation instead of inheritance"
166     { $code
167         "TUPLE: colored-shape color ;"
168         "C: colored-shape ( shape color -- shape )"
169         "    [ set-colored-shape-color ] keep"
170         "    [ set-delegate ] keep ;"
171     } }
172     { $code "100 200 <rectangle>" "{ 0.5 0.5 1 } <colored-shape>" "area ." }
173     "Advanced features: predicate classes, union classes, method combination"
174 }
175 { $slide "The Factor UI"
176     "Factor UI is totally implemented in Factor"
177     "OpenGL, FreeType, plus platform-specific code"
178     { "Gadgets:"
179     { $code
180         "USING: gadgets gadgets-labels ;"
181         "\"Hello world\" <label> \"Hi\" open-titled-window"
182     } }
183     "Presentations and operations"
184     "Models"
185 }
186 { $slide "Models"
187     { $code
188         "USING: models gadgets-scrolling gadgets-text ;"
189     }
190     "Create an editor, wrap it in a scroller:"
191     { $code
192         "<editor>"
193     }
194     "Length filter:"
195     { $code "dup control-model"
196         "[ concat length number>string ] <filter>"
197     }
198 }
199 { $slide "Models continued"
200     "Layout:"
201     { $code "{"
202     "    { [ <label-control> ] f f @top }"
203     "    { [ <scroller> ] f f @center }"
204     "} make-frame"
205     }
206     "Window:"
207     { $code
208     "\"Model test\" open-titled-window"
209     }
210 }
211 { $slide "This talk"
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" }
217 }
218 { $slide "The compiler"
219     "Performance is a goal"
220     "Compromise: The compiler only compiles words with a static stack effect"
221     "Three stages"
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'')"
227 }
228 { $slide "High-level optimizer"
229     "Quotations <-> dataflow conversion is ``loss-less''"
230     "Really, just rewriting quotations"
231     "Type inference"
232     "Partial evaluation"
233     "Arithmetic identities"
234     "Optimistic specialization"
235 }
236 { $slide "Low level optimizer"
237     "Caching stack in registers"
238     "Shuffling renames registers"
239     "Unboxing floats"
240     "Tail call optimization"
241 }
242 { $slide "Assember DSL"
243     "The compiler emits machine code:"
244     { $code
245         "char-reg PUSH"
246         "\"n\" operand 2 SHR"
247         "char-reg dup XOR"
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"
252         "char-reg POP"
253     }
254 }
255 { $slide "Assember DSL - continued"
256     { "It's all done with " { $link make } }
257     { $code "USE: assembler" "[ EAX ECX MOV ] { } make ." }
258 }
259 { $slide "The end"
260     { $url "http://factorcode.org" }
261     { $url "http://factor-language.blogspot.com" }
262     { "irc.freenode.net #concatenative" }
263 }
264 } ;
265
266 TUPLE: mslug ;
267
268 C: mslug ( -- gadget )
269     slides [ <page> ] map <book>
270     over set-gadget-delegate ;
271
272 : change-page ( book n -- )
273     over control-value + over gadget-children length rem
274     swap control-model set-model ;
275
276 : next-page ( book -- ) 1 change-page ;
277
278 : prev-page ( book -- ) -1 change-page ;
279
280 \ mslug H{
281     { T{ key-down f f "DOWN" } [ next-page ] }
282     { T{ key-down f f "UP" } [ prev-page ] }
283 } set-gestures
284
285 PROVIDE: demos/mslug-talk ;
286
287 MAIN: demos/mslug-talk
288     <mslug> "Presentation" open-titled-window ;