Lisp Metaprogramming
          The programmable programming language




Friday, February 19, 2010
I’d rather play COD




Friday, February 19, 2010
“So, Ruby was a Lisp originally, in theory.
                            Let's call it MatzLisp from now on. ;-)”




                                                                  -Matz
Friday, February 19, 2010
"Lisp isn't a language, it's a building material."

                "the greatest single programming language ever
                                    designed"

                      “OOP to me means only messaging, local
                    retention and protection and hiding of state-
                  process, and extreme late-binding of all things.
                   It can be done in Smalltalk and in Lisp. There
                      are possibly other systems in which this is
                        possible, but I'm not aware of them.”


                                                          -Alan Kay
Friday, February 19, 2010
Friday, February 19, 2010
As seen in Lisp since 1958
                                late binding
                           garbage collection
                             dynamic typing
                         if-then-else construct
                               function type
                                  recursion
                                symbol type
                       Interactive development
                              Lisp Machines
                                     ...
Friday, February 19, 2010
As seen in Lisp since 1958
                                late binding
                           garbage collection
                             dynamic typing          Scheme
                         if-then-else construct   Common Lisp
                               function type          Clojure
                                  recursion            Arc
                                symbol type          NewLISP
                       Interactive development     (JavaScript)
                              Lisp Machines
                                     ...
Friday, February 19, 2010
Lisp in 6 slides




Friday, February 19, 2010
Data structure: Linked lists




Friday, February 19, 2010
Data structure: Linked lists



                            ((1 2) (3 4) (5 6))


Friday, February 19, 2010
Syntax: Linked lists (again)

                            (define y-combinator
                              (lambda (f)
                                ((lambda (x) (f (x x)))
                                 (lambda (x) (f (x x))))))

                                    (homoiconicity)
Friday, February 19, 2010
Semantics: λ - Calculus

                            λxy.x




Friday, February 19, 2010
Semantics: λ - Calculus

                                λxy.x
                            (lambda (x y) x)




Friday, February 19, 2010
Semantics: λ - Calculus

                                 λxy.x
                             (lambda (x y) x)

                            (λxy.x 2 4) -β> 2


Friday, February 19, 2010
Semantics: λ - Calculus

                                 λxy.x
                             (lambda (x y) x)

                            (λxy.x 2 4) -β> 2
                       ((lambda (x y) x) 2 4) -> 2
Friday, February 19, 2010
Semantics: evaluation rule

                            Read-Eval-Print-Loop

                               (+ 3 (- 2 1))
                                  (+ 3 1)
                                     4
Friday, February 19, 2010
Semantics: special forms

                 backquote/unquote
                (backquote (+ 2 3)) -> (+ 2 3)
                     `(+ 2 3) -> (+ 2 3)



Friday, February 19, 2010
Semantics: special forms

                 backquote/unquote
                (backquote (+ 2 3)) -> (+ 2 3)
                     `(+ 2 3) -> (+ 2 3)
                            `(unquote (+ 2 3)) -> 5
                               `,(+ 2 3) -> 5
Friday, February 19, 2010
(Common) Lisp Macros




Friday, February 19, 2010
Why macros?
           “Pascal is for building pyramids -- imposing,
           breathtaking, static structures built by armies
       pushing heavy blocks into place. Lisp is for building
          organisms -- imposing, breathtaking, dynamic
       structures built by squads fitting fluctuating myriads
                  of simpler organisms into place.
                                 [...]
               Invent and fit; have fits and reinvent!”

                                       -from SICP foreword
Friday, February 19, 2010
Russian dolls



                                Lisp
                            user forms
                            special forms
                            kernel language




Friday, February 19, 2010
Russian dolls


                            language extensions
                                Lisp
                            user forms
                            special forms
                            kernel language




Friday, February 19, 2010
Functions vs Macros

                            Function

           S-Expressions      (f x)    Values


                             Macro

           S-Expressions      (f x)    S-Expressions

Friday, February 19, 2010
Functions vs Macros
                                        S-Expressions
                              Macro
                            expansion        (f x)
                               time
                                        S-Expressions’
                            Compile /
                            execution        (f’ x)
                              time
                                           Values
Friday, February 19, 2010
The my-unless function
                            (defun my-unless (c ef et)
                               (if c et ef))




Friday, February 19, 2010
The my-unless function
                            (defun my-unless (c ef et)
                               (if c et ef))


                            (my-unless t 1 2) -> 2
                            (my-unless nil 1 2) -> 1




Friday, February 19, 2010
The my-unless function
                             (defun my-unless (c ef et)
                                (if c et ef))


                              (my-unless t 1 2) -> 2
                              (my-unless nil 1 2) -> 1

                            (my-unless t (print “test”) 1)
                            ->”test”
                              1
Friday, February 19, 2010
The my-unless macro
                            (defmacro my-unless (c ef et)
                               (if c et ef))




Friday, February 19, 2010
The my-unless macro
                            (defmacro my-unless (c ef et)
                               (if c et ef))


                            (my-unless t (print “test”) 1)
                            ->1




Friday, February 19, 2010
The my-unless macro
                            (defmacro my-unless (c ef et)
                               (if c et ef))


                             (my-unless t (print “test”) 1)
                             ->1


                            (define *c* t)
                            (my-unless *c* (print “test”) 1)

Friday, February 19, 2010
The my-unless macro
                            (defmacro my-unless (c ef et)
                               `(if ,c ,et ,ef))


                            (define *c* t)
                            (my-unless *c* (print “test”) 1)




Friday, February 19, 2010
The my-unless macro
                            (defmacro my-unless (c ef et)
                               `(if ,c ,et ,ef))


                            (define *c* t)
                            (my-unless *c* (print “test”) 1)


                            (define *c* t)
                            (if *c* 1 (print “test”))
Friday, February 19, 2010
The my-unless macro
                            (defmacro my-unless (c ef et)
                               `(if ,c ,et ,ef))


                            (define *c* t)
                            (my-unless *c* (print “test”) 1)


                            (define *c* t)
                                                               1
                            (if *c* 1 (print “test”))
Friday, February 19, 2010
Macros taxonomy

                flow modification
                “with” macros -> abstracting pattens (with-file) (with-
                gearman-request)
                Object Oriented Lisp (CLOS) and Meta Object Protocol
                Compilers, pasers, etc.
                Functional lisp: monads, comonads, Tarski arrows,
                currying, lazy evaluation


Friday, February 19, 2010
The Macro Club


                The first rule of the Macro Club is Don’t Write Macros
                The second rule of Macro Club is Write Macros If That
                Is The Only Way to Encapsulate a Pattern




                                         -from Programming Clojure
Friday, February 19, 2010
Ruby metaprogramming?




Friday, February 19, 2010
Ruby metaprogramming is
          broken and cannot be fixed
                            (imho)




Friday, February 19, 2010
(Lisp)               [Ruby]

           Simple regular sintax                Complex sintax

                                              Complex undefined
   Simple defined semantics
                                                 semantics

                            Code = lists        Code = strings

     Code manipulation = list              Code manipulation = string
       manipulation funs.                     manipulation funs.


         Macro expansion time                   Eval at run time
Friday, February 19, 2010
Alternatives: RubyAST, Ruby
          parser


                                 Code = AST objects
                     Code Manipulation = Objects Manipulation




Friday, February 19, 2010
No magic please




Friday, February 19, 2010
References




Friday, February 19, 2010
The Seasoned
                            On Lisp,
                                              Schemer,
                            Paul Graham
                                              Friedman




                                              Programming
                            SICP,
                                              Clojure,
                            Abelson et alt.
                                              Stuart Halloway

Friday, February 19, 2010
λ

Friday, February 19, 2010

lisp (vs ruby) metaprogramming

  • 1.
    Lisp Metaprogramming The programmable programming language Friday, February 19, 2010
  • 2.
    I’d rather playCOD Friday, February 19, 2010
  • 3.
    “So, Ruby wasa Lisp originally, in theory. Let's call it MatzLisp from now on. ;-)” -Matz Friday, February 19, 2010
  • 4.
    "Lisp isn't alanguage, it's a building material." "the greatest single programming language ever designed" “OOP to me means only messaging, local retention and protection and hiding of state- process, and extreme late-binding of all things. It can be done in Smalltalk and in Lisp. There are possibly other systems in which this is possible, but I'm not aware of them.” -Alan Kay Friday, February 19, 2010
  • 5.
  • 6.
    As seen inLisp since 1958 late binding garbage collection dynamic typing if-then-else construct function type recursion symbol type Interactive development Lisp Machines ... Friday, February 19, 2010
  • 7.
    As seen inLisp since 1958 late binding garbage collection dynamic typing Scheme if-then-else construct Common Lisp function type Clojure recursion Arc symbol type NewLISP Interactive development (JavaScript) Lisp Machines ... Friday, February 19, 2010
  • 8.
    Lisp in 6slides Friday, February 19, 2010
  • 9.
    Data structure: Linkedlists Friday, February 19, 2010
  • 10.
    Data structure: Linkedlists ((1 2) (3 4) (5 6)) Friday, February 19, 2010
  • 11.
    Syntax: Linked lists(again) (define y-combinator (lambda (f) ((lambda (x) (f (x x))) (lambda (x) (f (x x)))))) (homoiconicity) Friday, February 19, 2010
  • 12.
    Semantics: λ -Calculus λxy.x Friday, February 19, 2010
  • 13.
    Semantics: λ -Calculus λxy.x (lambda (x y) x) Friday, February 19, 2010
  • 14.
    Semantics: λ -Calculus λxy.x (lambda (x y) x) (λxy.x 2 4) -β> 2 Friday, February 19, 2010
  • 15.
    Semantics: λ -Calculus λxy.x (lambda (x y) x) (λxy.x 2 4) -β> 2 ((lambda (x y) x) 2 4) -> 2 Friday, February 19, 2010
  • 16.
    Semantics: evaluation rule Read-Eval-Print-Loop (+ 3 (- 2 1)) (+ 3 1) 4 Friday, February 19, 2010
  • 17.
    Semantics: special forms backquote/unquote (backquote (+ 2 3)) -> (+ 2 3) `(+ 2 3) -> (+ 2 3) Friday, February 19, 2010
  • 18.
    Semantics: special forms backquote/unquote (backquote (+ 2 3)) -> (+ 2 3) `(+ 2 3) -> (+ 2 3) `(unquote (+ 2 3)) -> 5 `,(+ 2 3) -> 5 Friday, February 19, 2010
  • 19.
  • 20.
    Why macros? “Pascal is for building pyramids -- imposing, breathtaking, static structures built by armies pushing heavy blocks into place. Lisp is for building organisms -- imposing, breathtaking, dynamic structures built by squads fitting fluctuating myriads of simpler organisms into place. [...] Invent and fit; have fits and reinvent!” -from SICP foreword Friday, February 19, 2010
  • 21.
    Russian dolls Lisp user forms special forms kernel language Friday, February 19, 2010
  • 22.
    Russian dolls language extensions Lisp user forms special forms kernel language Friday, February 19, 2010
  • 23.
    Functions vs Macros Function S-Expressions (f x) Values Macro S-Expressions (f x) S-Expressions Friday, February 19, 2010
  • 24.
    Functions vs Macros S-Expressions Macro expansion (f x) time S-Expressions’ Compile / execution (f’ x) time Values Friday, February 19, 2010
  • 25.
    The my-unless function (defun my-unless (c ef et) (if c et ef)) Friday, February 19, 2010
  • 26.
    The my-unless function (defun my-unless (c ef et) (if c et ef)) (my-unless t 1 2) -> 2 (my-unless nil 1 2) -> 1 Friday, February 19, 2010
  • 27.
    The my-unless function (defun my-unless (c ef et) (if c et ef)) (my-unless t 1 2) -> 2 (my-unless nil 1 2) -> 1 (my-unless t (print “test”) 1) ->”test” 1 Friday, February 19, 2010
  • 28.
    The my-unless macro (defmacro my-unless (c ef et) (if c et ef)) Friday, February 19, 2010
  • 29.
    The my-unless macro (defmacro my-unless (c ef et) (if c et ef)) (my-unless t (print “test”) 1) ->1 Friday, February 19, 2010
  • 30.
    The my-unless macro (defmacro my-unless (c ef et) (if c et ef)) (my-unless t (print “test”) 1) ->1 (define *c* t) (my-unless *c* (print “test”) 1) Friday, February 19, 2010
  • 31.
    The my-unless macro (defmacro my-unless (c ef et) `(if ,c ,et ,ef)) (define *c* t) (my-unless *c* (print “test”) 1) Friday, February 19, 2010
  • 32.
    The my-unless macro (defmacro my-unless (c ef et) `(if ,c ,et ,ef)) (define *c* t) (my-unless *c* (print “test”) 1) (define *c* t) (if *c* 1 (print “test”)) Friday, February 19, 2010
  • 33.
    The my-unless macro (defmacro my-unless (c ef et) `(if ,c ,et ,ef)) (define *c* t) (my-unless *c* (print “test”) 1) (define *c* t) 1 (if *c* 1 (print “test”)) Friday, February 19, 2010
  • 34.
    Macros taxonomy flow modification “with” macros -> abstracting pattens (with-file) (with- gearman-request) Object Oriented Lisp (CLOS) and Meta Object Protocol Compilers, pasers, etc. Functional lisp: monads, comonads, Tarski arrows, currying, lazy evaluation Friday, February 19, 2010
  • 35.
    The Macro Club The first rule of the Macro Club is Don’t Write Macros The second rule of Macro Club is Write Macros If That Is The Only Way to Encapsulate a Pattern -from Programming Clojure Friday, February 19, 2010
  • 36.
  • 37.
    Ruby metaprogramming is broken and cannot be fixed (imho) Friday, February 19, 2010
  • 38.
    (Lisp) [Ruby] Simple regular sintax Complex sintax Complex undefined Simple defined semantics semantics Code = lists Code = strings Code manipulation = list Code manipulation = string manipulation funs. manipulation funs. Macro expansion time Eval at run time Friday, February 19, 2010
  • 39.
    Alternatives: RubyAST, Ruby parser Code = AST objects Code Manipulation = Objects Manipulation Friday, February 19, 2010
  • 40.
    No magic please Friday,February 19, 2010
  • 41.
  • 42.
    The Seasoned On Lisp, Schemer, Paul Graham Friedman Programming SICP, Clojure, Abelson et alt. Stuart Halloway Friday, February 19, 2010
  • 43.