factor/demos/mslug-talk.factor

289 lines
8.1 KiB
Factor
Raw Normal View History

2006-11-15 00:58:35 -05:00
! Slava Pestov's presentation at the Montreal Scheme/Lisp User's
! Group, November 22nd 2006.
! http://schemeway.dyndns.org/mslug/mslug-home
USING: gadgets gadgets-books gadgets-borders gadgets-buttons
gadgets-text gadgets-labels gadgets gadgets-panes
gadgets-presentations gadgets-theme generic kernel math
namespaces sequences strings styles models help io arrays
hashtables modules ;
IN: mslug
: mslug-stylesheet
H{
2006-11-15 01:02:13 -05:00
{ default-style
H{
{ font "serif" }
{ font-size 24 }
{ wrap-margin 700 }
}
}
2006-11-15 00:58:35 -05:00
{ code-style
H{
{ font "monospace" }
{ font-size 24 }
{ page-color { 0.4 0.4 0.4 0.3 } }
}
}
{ table-content-style
H{ { wrap-margin 700 } }
}
2006-11-22 12:39:29 -05:00
{ bullet "\u00b7 " }
2006-11-15 00:58:35 -05:00
} ;
: $title ( string -- )
[ H{ { font "serif" } { font-size 36 } } format ] ($block) ;
: $divider ( -- )
[
<gadget>
T{ gradient f { { 0.25 0.25 0.25 1.0 } { 1.0 1.0 1.0 1.0 } } }
over set-gadget-interior
{ 750 10 } over set-gadget-dim
{ 1 0 } over set-gadget-orientation
gadget.
] ($block) ;
: page-theme
T{ gradient f { { 0.8 0.8 1.0 1.0 } { 1.0 0.8 1.0 1.0 } } }
swap set-gadget-interior ;
: <page> ( list -- gadget )
2006-11-15 01:02:13 -05:00
[
mslug-stylesheet clone [
[ print-element ] with-default-style
] bind
] make-pane
2006-11-15 00:58:35 -05:00
dup page-theme ;
: $slide ( element -- )
unclip $title
$divider
$list ;
: $module ( element -- )
first dup module [ write ] ($code) ;
: slides
{
{ $slide "Factor programming language"
"Inspirations: Forth, Joy, Common Lisp, others..."
"Powerful language features"
"Interactive development"
2006-11-22 12:39:29 -05:00
"Stack-based"
2006-11-15 00:58:35 -05:00
"Syntax: ``s-expressions''"
"Code is data"
{ "Simple evaluation semantics; left to right:"
{ $code "2 3 + 7 * 9 /" } }
"Functions are called ``words''"
"Vocabularies"
"Short definitions, heavy reuse"
}
{ $slide "First program"
{
"Functional ``hello world'':"
{ $code
": factorial ( n -- n! )"
" dup 0 <= ["
" drop 1"
" ] ["
" dup 1 - factorial *"
" ] if ;"
}
}
"Shuffle words, conditionals, recursion, code as parameters..."
"Bignums!"
{ $code "100 factorial ." }
"Reflection:"
{ $code "\\ factorial see" }
}
{ $slide "Data types - sequences"
{ "Sequences: everything is an array"
{ $code "{ 1 2 3 } { t f } append reverse ." } }
{ "Familiar higher order functions"
{ $code "{ { 1 2 } { 3 4 } } [ reverse ] map ." } }
{ "Integers are sequences, too:"
{ $code "100 [ sq ] map ." }
{ $code "100 [ 4 mod 3 = ] subset ." }
{ $code "0 100 [ 1 + * ] reduce ." } }
}
2006-11-18 03:51:05 -05:00
{ $slide "Data types - hashtables"
2006-11-15 00:58:35 -05:00
"Hashtables: literal syntax, utility words, higher-order combinators..."
{ $code
"H{ { \"grass\" \"green\" } { \"chicken\" \"white\" } }"
"H{ { \"carrot\" \"orange\" } }"
"hash-union"
}
"Option+h"
2006-11-22 12:39:29 -05:00
"Prettyprinter -vs- inspector"
2006-11-15 00:58:35 -05:00
}
{ $slide "Data types - others"
2006-11-18 03:51:05 -05:00
"Queues, graphs, splay trees, double linked lists, lazy lists..."
2006-11-15 00:58:35 -05:00
}
{ $slide "Variables"
2006-11-22 12:39:29 -05:00
"Dynamic scope"
2006-11-15 00:58:35 -05:00
{ $code "SYMBOL: foo" "5 foo set" "foo get ." }
2006-11-22 12:39:29 -05:00
{ $code "SYMBOL: foo" "[ 6 foo set foo get ] with-scope" }
2006-11-15 00:58:35 -05:00
"Dynamic scope can be analyzed statically:"
{ $code "SYMBOL: bar" "bar get sq foo set" }
}
{ $slide "Encapsulating variable usage"
{ "Stream-like sequence construction:"
{ $code
": print-name ( name -- )"
2006-11-22 12:39:29 -05:00
" [ \"Greetings, \" % % \".\" % ] \"\" make print ;"
2006-11-15 00:58:35 -05:00
} }
"Key factors:"
{ $code "%" }
{ $code "[ % ] \"\" make" }
{ "Also, I/O:"
{ $code ": 100-bytes [ 100 read ] with-stream ;" "\"foo.txt\" <file-reader> 100-bytes" } }
}
2006-11-22 12:39:29 -05:00
{ $slide "Custom data types"
2006-11-15 00:58:35 -05:00
{ "Built-in classes: " { $link integer } ", " { $link array } ", " { $link hashtable } "..." }
{ "You can define your own:"
{ $code "TUPLE: rectangle w h ;" "TUPLE: circle r ;" }
}
{ $code "100 200 <rectangle>" }
{ $code "rectangle-w ." }
2006-11-18 03:51:05 -05:00
}
{ $slide "Polymorphism"
2006-11-15 00:58:35 -05:00
{ "Generic words:"
{ $code
"GENERIC: area ( shape -- n )"
"M: rectangle area"
" dup rectangle-w swap rectangle-h * ;"
"M: circle area circle-r sq pi * ;"
}
}
2006-11-18 03:51:05 -05:00
{ "Methods in classes -vs- methods in functions?" }
{ "Both: " { $link array } " -vs- " { $link nth } }
2006-11-15 00:58:35 -05:00
}
2006-11-18 03:51:05 -05:00
{ $slide "More polymorphism"
2006-11-15 00:58:35 -05:00
"Tuples can have custom constructor words"
{ "Delegation instead of inheritance"
{ $code
"TUPLE: colored-shape color ;"
"C: colored-shape ( shape color -- shape )"
" [ set-colored-shape-color ] keep"
" [ set-delegate ] keep ;"
} }
{ $code "100 200 <rectangle>" "{ 0.5 0.5 1 } <colored-shape>" "area ." }
"Advanced features: predicate classes, union classes, method combination"
}
{ $slide "The Factor UI"
"Factor UI is totally implemented in Factor"
"OpenGL, FreeType, plus platform-specific code"
{ "Gadgets:"
{ $code
"USING: gadgets gadgets-labels ;"
"\"Hello world\" <label> \"Hi\" open-titled-window"
} }
"Presentations and operations"
"Models"
}
{ $slide "Models"
{ $code
"USING: models gadgets-scrolling gadgets-text ;"
2006-11-18 03:51:05 -05:00
}
"Create an editor, wrap it in a scroller:"
{ $code
"<editor>"
}
"Length filter:"
{ $code "dup control-model"
2006-11-15 00:58:35 -05:00
"[ concat length number>string ] <filter>"
2006-11-18 03:51:05 -05:00
}
}
{ $slide "Models continued"
"Layout:"
{ $code "{"
" { [ <label-control> ] f f @top }"
2006-11-22 12:39:29 -05:00
" { [ <scroller> ] f f @center }"
2006-11-18 03:51:05 -05:00
"} make-frame"
}
"Window:"
{ $code
"\"Model test\" open-titled-window"
2006-11-15 00:58:35 -05:00
}
}
{ $slide "This talk"
"Uses Factor help markup language with a tweaked stylesheet"
"Each slide is a gadget"
"Slides are grouped in a custom gadget which handles Up/Down arrow keys to move between slides"
"Let's look at the source code:"
2006-11-28 21:57:29 -05:00
{ $module "demos/mslug-talk" }
2006-11-15 00:58:35 -05:00
}
2006-11-18 03:51:05 -05:00
{ $slide "The compiler"
"Performance is a goal"
"Compromise: The compiler only compiles words with a static stack effect"
"Three stages"
{ "First stage transforms a quotation into the " { $emphasis "dataflow representation" } }
"Second stage: high-level optimizations"
"Third stage: register allocation, peephole optimization, code generation"
"Compiled code saved in the image"
"Compiler invoked explicitly (not a ``JIT'')"
}
{ $slide "High-level optimizer"
"Quotations <-> dataflow conversion is ``loss-less''"
"Really, just rewriting quotations"
"Type inference"
"Partial evaluation"
"Arithmetic identities"
"Optimistic specialization"
}
{ $slide "Low level optimizer"
"Caching stack in registers"
"Shuffling renames registers"
"Unboxing floats"
"Tail call optimization"
}
{ $slide "Assember DSL"
"The compiler emits machine code:"
{ $code
"char-reg PUSH"
"\"n\" operand 2 SHR"
"char-reg dup XOR"
"\"obj\" operand \"n\" operand ADD"
"char-reg-16 \"obj\" operand string-offset [+] MOV"
"char-reg tag-bits SHL"
"\"obj\" operand char-reg MOV"
"char-reg POP"
}
}
{ $slide "Assember DSL - continued"
{ "It's all done with " { $link make } }
{ $code "USE: assembler" "[ EAX ECX MOV ] { } make ." }
}
2006-11-15 00:58:35 -05:00
{ $slide "The end"
{ $url "http://factorcode.org" }
{ $url "http://factor-language.blogspot.com" }
{ "irc.freenode.net #concatenative" }
}
} ;
TUPLE: mslug ;
C: mslug ( -- gadget )
slides [ <page> ] map <book>
over set-gadget-delegate ;
: change-page ( book n -- )
over control-value + over gadget-children length rem
swap control-model set-model ;
: next-page ( book -- ) 1 change-page ;
: prev-page ( book -- ) -1 change-page ;
\ mslug H{
{ T{ key-down f f "DOWN" } [ next-page ] }
{ T{ key-down f f "UP" } [ prev-page ] }
} set-gestures
2006-11-28 21:57:29 -05:00
PROVIDE: demos/mslug-talk ;
2006-11-15 00:58:35 -05:00
2006-11-28 21:57:29 -05:00
MAIN: demos/mslug-talk
2006-11-15 00:58:35 -05:00
<mslug> "Presentation" open-titled-window ;