📖Factor: an extensible interactive language

authors
Pestov, Slava
year
2008
url
https://youtu.be/f_0QlhYlS8g
  • Factor is functional, but not purely functional (functions are first-class). Object-oriented (dynamic dispatch).
  • Factor has development environment
    • allows stepping through words execution
  • Factor allows objects on stack (is it GC? yes)
  • { } pushes array of data (as a single stack item)
  • [ ] pushes code into stack (quotation)
  • combinators operate on quotations (on code)
  • if takes then/else branch from the stack
    10 dup 0 < [ 1 - ] [ 1 + ] if .
    
  • times (counted looping)
    10 [ "Hello" print ] times
    
  • support typing (but not shown in this talk)
  • Factor decouples methods from data (Common Lisp Object System, Clojure multi-methods?)
  • Create class (data)
    TUPLE: rectangle  width height ;
    TUPLE: circle     radius ;
    
    rectangle new           ( initialized with f )
    100 200 rectangle boa   ( width 100, height 200 )  ( "Build? Order of Arguments")
    300 >>width             ( update width )
    
    GENERIC: area ( shape -- n)
    
    M: rectangle area
        [ width>> ] [ height>> ] bi * ;
    
    M: circle area   radius>> sq pi * ;
    
    : <circle> ( r -- c ) circle boa ;  ( constructor )
    
    10 <circle> rectangle?  ( check if it's an instance of rectangle )
    
    ( mixins )
    MIXIN: shape
    INSTANCE: rectangle shape
    INSTANCE: circle shape
    13 <circle> shape?
    
    M: shape tell-me
        "My area is " write  area . ;
    
  • Compiler checks stack usage. If anything is wrong, you get a compile error.
  • no separation between int/float stacks
  • have distinct boolean type (t/f)
  • integer, array are built-in classes
  • object system is implemented in Factor
  • generic, classes, slots
  • purely-functional collections
  • (does it support fractions? 10+6/13)
  • strings are 21-bit unicode code points (optimized as ASCII if possible)
  • different encodings. binary is supported
  • :: lambdas. not anonymous function. looks rather like it takes arguments ({ } in Forth/gforth)
    : +-   [ + ] [ - ] 2bi ; inline
    
    :: discriminant  ( a b c -- d )  b sq  4 a c * * - sqrt ;
    
    :: quadratic  ( a b c -- x y )
        [let | d [ a b c discriminant ] |
            b neg d +- [ 2 a * / ] bi@
        ] ;
    
  • [let creates lexical closures
  • Example memoization (MEMO: is a parsing word defined as a library):
    MEMO: fib  ( m -- n)
        dup 1> [
            [ 1 - fib ] [ 2 - fib ] bi +
        ] when ;
    
  • EBNF: a parsing words for grammars
  • has full-featured web framework
  • compiler
    • optimizing compiler
    • runtime VM written in C
    • generational GC
    • multiple passes of optimizations, dead code elimination, inlining, etc.
    • uses SSA (single static assignment)
  • all the project infrastructure (main website, etc) are written in Factor

Backlinks