SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Downloaden Sie, um offline zu lesen
orb@nostacktrace.com




                           The Lambda Calculus
                       λ           and
                              The Javascript
Norman Richards
orb@nostacktrace.com

                                                   Why lambda calculus?

                                                                                                                     Alan TUring
                                             Alonzo CHURCh
                                                                                                              Invented the turing machine, an
                                       Created lambda calculus, the
                                                                                                                imperative computing model
                                     basis for functional programming.




                                                                                         We spend all our time
                                                                                       studying this guy’s work
                                                                                       (because he’s awesome)
Norman Richards




                       http://en.wikipedia.org/wiki/Alonzo_Church    But if we want to think functionally,                 http://en.wikipedia.org/wiki/Alan_Turing

                                                                    we should pay attention to this guy too
                                                                        (because he’s also awesome)
orb@nostacktrace.com
                                       The premise



                                    Functional programming is good

                         Using functional techniques will make your code better

                       Understanding lambda calculus will improve your functional
Norman Richards
orb@nostacktrace.com
                       A Lambda Expression


                           bound variable
                           This is the argument of
                                 the function



                                        λx.x
                                                             body
                                                A lambda expression that uses the
                                                bound variable and represents the
                                                         function value
Norman Richards
orb@nostacktrace.com
                        A Lambda Expression




                                     function(x) {
                       λx.x             return x
                                     }
Norman Richards
orb@nostacktrace.com
                       Not a Lambda Expression




                                λy.x
                                         free variable
                                   This variable isn’t bound, so this
                                      expression is meaningless
Norman Richards
orb@nostacktrace.com
                        A Lambda Expression




                                            function(y) {
                       λy.x                    return x
                                            }
Norman Richards




                               These are
                              meaningless
orb@nostacktrace.com
                       A Lambda Expression


                              parenthesis
                           Not needed, but used for
                                 clarity here



                                  λx.(λ.y.x)
                                                           bound or free?
                                                      x is bound in the outer expression
                                                         and free in inner expression.
Norman Richards
orb@nostacktrace.com
                           A Lambda Expression



                                       function(x) {
                                          return function(y) {
                       λx.λ.y.x               return x
                                          }
                                       }
Norman Richards
orb@nostacktrace.com
                            Lambda Application

                                                                    unicorns don’t exist
                                                            There are no assignments or external references.
                                                             We’ll use upper-case words to stand for some
                                                               valid lambda expression, but it must mean
                                                                              something.



                                          λx.x UNICORN
                       application by substitution

                         Apply UNICORN to the expression,
                          substituting UNICORN for every
                          occurrence of the bound value
Norman Richards
orb@nostacktrace.com
                                 Lambda Application

                          The I (identity) combinator
                       Combinatory logic predates lambda calculus, but
                         the models are quite similar. Many lambda
                        expressions are referred to by their equivalent
                                     combinator names.

                                                                          function(x) {
                       λx.x UNICORN                                          return x
                       ! UNICORN                                          }(UNICORN);

                                                                          ! UNICORN
Norman Richards
orb@nostacktrace.com
                                                 Lambda Application

                         the K (constant) combinator
                         This function takes an input argument and
                       returns a function that returns that same value




                          λx.λy.x UNICORN                                  λy.UNICORN MITT
                        ! λy.UNICORN                                     ! UNICORN
                                                                                always a UNICORN
Norman Richards




                                                                            No matter what argument is passed
                                                                             in, the result is always the same.




                                                                                   http://leftaction.com/action/ask-kansas-mitt-romney-unicorn
orb@nostacktrace.com
                                       Lambda Application


                          self-application
                       This function takes a function and
                               applies it to itself.

                                                            function(f) {
                          λs.(s s)                             return f(f);
                                                            }
Norman Richards
orb@nostacktrace.com
                                                   Lambda Application



                       Substitute λx.x for s   λs.(s s) λx.x             function(f) {
                                               ! λx.x λx.x                  return f(f);
                                                                         }(function (x) {
                                               ! λx1.x1 λx.x                return x;
                       For clarity,
                       rename x as x1          ! λx.x                      });
Norman Richards




                                                       Substitute λx.x
                                                       for x1.
orb@nostacktrace.com
                       Infinite application


                        λs.(s s) λs.(s s)
                        ! λs.(s s) λs.(s s)
                        ! λs.(s s) λs.(s s)
                        ! …
                               the halting problem
Norman Richards




                         Just as you’d expect, there’s no algorithm to
                          determine whether or not a given lambda
                                   expression will terminate
orb@nostacktrace.com
                       Turing Complete



                       I: λx.x
                       K: λx.λy.x
                       S: λx.λy.λz.x z (y z)
Norman Richards




                                               http://en.wikipedia.org/wiki/SKI_combinator_calculus
orb@nostacktrace.com
                       Actually, just two lambdas
                       S   K K UNICORN
                       !   λx.λy.λz.(x z (y z)) K K UNICORN
                       !   λy.λz.(K z (y z)) K UNICORN
                       !   λz.(K z (K z)) UNICORN
                       !   K UNICORN (K UNICORN)
                       !   λx.λy.x UNICORN (K UNICORN)
                       !   λy.UNICORN (K UNICORN)
Norman Richards




                       !   UNICORN   Thus, S K K is computationally
                                         equivalent to identity
orb@nostacktrace.com
                                  Other Turing Complete things
                                                       Magic: the Gathering

                                                        http://www.toothycat.net/
                                                            ~hologram/Turing/




                         C++ Templates                                                  The number 110
                       http://citeseerx.ist.psu.edu/
Norman Richards




                           viewdoc/summary?                                         http://mathworld.wolfram.com/
                            doi=10.1.1.14.3670                                               Rule110.html
orb@nostacktrace.com
                                      TURING TARPIT



                       Beware of the Turing tar-pit in which everything is
                           possible but nothing of interest is easy.

                                                                   Alan Perlis
Norman Richards




                                                                  http://pu.inf.uni-tuebingen.de/users/klaeren/epigrams.html
orb@nostacktrace.com
                                                                                 Currying
                                                                                       function(x) {
                                                                                         return function(y) {
                                                                                           return function (z) {
                       A function that returns a FUNCTION                                     return UNICORN;
                       .. that returns a function that returns a function. You
                         can think of this as a function of three arguments.               }
                       λx.λy.λz.UNICORN X Y Z                                            }
                                                                                       }(X)(Y)(Z);
Norman Richards




                                                                                       function(x,y,z) {
                                                                                          return UNICORN;
                                                                                       }(X,Y,Z);
orb@nostacktrace.com
                                   Currying

                         Sometimes abbreviated
                            Sometimes, you’ll see lambda
                        expressions written like this for clarity.
                           The arguments are still curried.



                       λx y z.UNICORN X Y Z
                                                                                      HASKELL CURRY
                       ! λy z.UNICORN Y Z                                     Curry studied combinatory logic, a
                                                                                 variant of lambda calculus.

                       ! λz.UNICORN Z
                       ! UNICORN
Norman Richards




                                                                     http://www.haskell.org/haskellwiki/Haskell_Brooks_Curry
orb@nostacktrace.com
                       data structures

                                CHURCH PAIR ENCODING
                         PAIR takes two arguments, x and y and returns
                           a function that takes another function and
                                      applies x and y to it.



                       PAIR: λx.λy.λf.(f x y)
                                       BuILDING UP STATE
                            PAIR takes two arguments, x and y and returns
                              a function that takes another function and
                                         applies x and y to it.
Norman Richards
orb@nostacktrace.com
                       data structures


                       PAIR:   λx.λy.λf.(f x y)
                       FIRST: λx.λy.x
                       SECOND: λx.λy.y

                                  ACCESSOR FUNCTIONS
                          FIRST and SECOND are functions that can be
Norman Richards




                          passed to pair. They take two arguments and
                             return the first and second, respectively.
orb@nostacktrace.com
                               data structures


                       PAIR:   λx.λy.λf.(f x y)

                       DOGCAT: PAIR DOG CAT
                               ! λx.λy.λf.(f x y) DOG CAT
                               ! λy.λf.(f DOG y) CAT
                               ! λf.(f DOG CAT)
Norman Richards
orb@nostacktrace.com
                        data structures

                       DOGCAT: λf.(f DOG CAT)
                       FIRST: λx.λy.x
                       SECOND: λx.λy.y

                       DOGCAT FIRST
                       ! λf.(f DOG CAT) λx.λy.x
                       ! λx.λy.x DOG CAT
Norman Richards




                       ! λy.DOG CAT
                       ! DOG
orb@nostacktrace.com
                         data structures

                       DOGCAT: λf.(f DOG CAT)
                       FIRST: λx.λy.x
                       SECOND: λx.λy.y

                       DOGCAT SECOND
                       ! λf.(f DOG CAT) λx.λy.x
                       ! λx.λy.x DOG CAT
Norman Richards




                       ! λy.y CAT
                       ! CAT
orb@nostacktrace.com
                              data structures

                       RGB:    λr.λg.λb.λf.(f r g b)

                       RED:   λr.λg.λb.r
                       GREEN: λr.λg.λb.g
                       BLUE: λr.λg.λb.b

                       BLACK_COLOR: RGB 255 255 255
Norman Richards




                       WHITE_COLOR: RGB 0 0 0
                                                      NuMBERS?
                                       We haven’t seen how to construct numbers yet,
                                          but for the moment imagine that we did.
orb@nostacktrace.com

                                             data structures
                                                                              AVERAGE?
                       AVG: λx.λy.???                                 If we did have numbers, we could
                                                                    probably do math like simple averaging



                       MIX_RGB:
                          λc1.λc2.λf.
                            (f (AVG (c1 RED) (c2 RED))
                                (AVG (c1 GREEN)(c2 GREEN))
                                (AVG (c1 BLUE) (c2 BLUE)))
Norman Richards




                                    OO LAMBDA?
                       MIX_RGB takes two colors and returns a new
                            color that is the mix of the two
orb@nostacktrace.com
                           CONDITIONALS


                        COND: λe1.λe2.λc.(c e1 e2)
                        TRUE: λx.λy.x
                       FALSE: λx.λy.y
                                        Look familiar?
                              Think of a boolean condition as a PAIR of
                             values. TRUE selects the first one and FALSE
                                       selects the second one.
Norman Richards
orb@nostacktrace.com
                                     CONDITIONALS
                         COND DEAD ALIVE QUBIT
                         ! λe1.λe2.λc.(c e1 e2) DEAD ALIVE QUBIT
                         ! QUBIT DEAD ALIVE



                       ! TRUE DEAD ALIVE          ! FALSE DEAD ALIVE
                       ! λx.λy.x DEAD ALIVE       ! λx.λy.y DEAD ALIVE
Norman Richards




                       ! DEAD                     ! ALIVE


                                                           https://www.coursera.org/course/qcomp
orb@nostacktrace.com
                            BOOLEAN LOGIC


                       NOT: λx.(COND FALSE TRUE x)
                       AND: λx.λy.(COND y FALSE x)
                        OR: λx.λy.(COND TRUE y x)
Norman Richards
orb@nostacktrace.com
                                                         NUMBERS
                       ZERO: λx.x
                       SUCC: λn.λs.(s false n)

                       ONE: SUCC ZERO
                            ! λs.(s false ZERO)
                       TWO: SUCC ONE
                            ! λs.(s false ONE)
                            ! λs.(s false λs.(s false ZERO))
Norman Richards




                              PEANO NUMBERS
                       PEANO numbers are based on a zero function
                               and a successor function
orb@nostacktrace.com
                                       NUMBERS



                                              ISZERO ZERO
                         ZERO: λx.x           ! λn.(n FIRST) λx.x
                       ISZERO: λn.(n FIRST)   ! λx.x FIRST
                                              ! FIRST
Norman Richards




                                              ! TRUE
orb@nostacktrace.com
                                NUMBERS

                         ISZERO: λn.(n FIRST)
                            ONE: λs.(s FALSE ZERO)


                       ISZERO ONE
                       ! λn.(n FIRST) λs.(s FALSE ZERO)
                       ! λs.(s FALSE ZERO) FIRST
                       ! (FIRST FALSE ZERO)
Norman Richards




                       ! FALSE
orb@nostacktrace.com
                                              NUMBERS

                       PRED: λn.(n SECOND)

                       PRED (SUCC NUM)
                       ! λn.(n SECOND) λs.(s FALSE NUM)
                       ! λs.(s FALSE ZERO) SECOND
                       ! SECOND FALSE NUM
                       ! NUM
Norman Richards




                                 but...
                       ZERO isn’t SUCC of a number
orb@nostacktrace.com
                                 NUMBERS



                       PRED: λn.(ISZERO n) ZERO (n SECOND)
                                    What is Pred of zero?
                                    We can at least test for zero and
                                        return another number.
Norman Richards
orb@nostacktrace.com
                                                                    NUMBERS


                       ADD:λx.λy.(ISZERO Y)                                 function add(x,y) {
                           x                                                  if (y==0) {
                           (ADD (SUCC X)                                        return x
                                (PRED Y))                                     } else {
                                                                                return add(x+1,y-1)
                                           NOT VALID
                                Recursive definitions aren’t possible .        }
Norman Richards




                           Remember, names are just syntactics sugar. All
                            definitions must be finite. How do we do this?    }
orb@nostacktrace.com
                            RECURSION DIVERSION
                           If we could pass the function in
                           Now we’ll make a function of three arguments,
                             the first being the function to recurse on



                       ADD 1:λf.λx.λy.(ISZERO                                     Y)
                                   x
                                  (f f (SUCC X) (PRED Y))
                                  Then we could call it RECURSIVELY
                                    Just remember that the function takes three
Norman Richards




                                 arguments, so we should pass the function to itself.
orb@nostacktrace.com
                             RECURSION DIVERSION

                       ADD 1:λf.λx.λy.(ISZERO                           Y)
                                     x
                                    (f f (SUCC X) (PRED Y))

                       ADD:  ADD 1           ADD 1

                       ! λx.λy.(ISZERO Y)
                            x
Norman Richards




                            (ADD 1 ADD1 (SUCC X) (PRED Y))

                                  FINITE DEFINiTION
                           This definition of ADD is a bit repetitive,
                            but it doesn’t recurse infinitely. If only
                                we could clean this up a bit...
orb@nostacktrace.com
                          BEHOLD, the Y combinator
                          Y: λf.(λs.(f (s s)) (λs.(f (s s)))

                       ADD 1:   λf.λx.λy.(ISZERO Y) X
                                  (F (SUCC X) (PRED Y))

                       ADD: Y    ADD 1

                       ! λs.(ADD 1 (s s)) (λs.(ADD1 (s s))

                       ! ADD1 (λs.(ADD1 (s s)) λs.(ADD1 (s s)))
Norman Richards




                       ! ADD1 (Y ADD1)

                       ! ADD1 ADD      Y self replicates
                                         This is similar to the hand prior call, except that
                                          the replication state is in the passed function.
orb@nostacktrace.com
                         CHURCH NUMBERs

                       ZERO:    λf.λx.x
                       ONE:     λf.λx.f                x
                       TWO:     λf.λx.f                (f x)
                       THREE:   λf.λx.f                (f (f x))
                       FOUR:    λf.λx.f                (f (f (f x)))
                                repeated function application
Norman Richards




                                Church numbers take a function and an argument
                                 and apply the function to the argument n times.
                                   So N is defined by doing something N times.
orb@nostacktrace.com
                          CHURCH NUMBERs
                       ZERO:   λn.(n λx.FALSE TRUE)
                       SUCC:   λn.λf.λx.f (n f x)
                       ADD:    λm.λn.λf.λx.m f (n f x)
                       MUL:    λm.λn.λf.m (n f)
                       POW:    λm.λn.m n
                       PRED:   λn.λf.λx.n (λg.λh. h (g f))
                                       λu.x λu.u
Norman Richards




                                No recursion for simple math
                                  Church numbers have some very simple
                               operations, Operations like subtraction are more
                                       complex than Peano numbers.
orb@nostacktrace.com
                              And the rest is functional


                                                                 An introduction to
                                                              Functional Programming
                                                             Through Lambda Calculus

                                                                Greg Michaelson
Norman Richards




                       http://www.amazon.com/dp/0486478831
orb@nostacktrace.com
                                         Leisure
                                   https://github.com/zot/Leisure

                       •Lambda Calculus engine written in Javascript
                       •Intended for programming
                       •REPL environment with node.js
                       •Runs in the browser
Norman Richards




                                      http://tinyconcepts.com/invaders.html
orb@nostacktrace.com




                                          λ       thank you
Norman Richards




                       http://www.slideshare.net/normanrichards/the-lambda-calculus-and-the-javascript

Weitere ähnliche Inhalte

Was ist angesagt?

Fuzzy Set Theory
Fuzzy Set TheoryFuzzy Set Theory
Fuzzy Set Theory
AMIT KUMAR
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
Ronak Thakkar
 
Equivalence relations
Equivalence relationsEquivalence relations
Equivalence relations
Tarun Gehlot
 

Was ist angesagt? (20)

Discrete mathematics Ch1 sets Theory_Dr.Khaled.Bakro د. خالد بكرو
Discrete mathematics Ch1 sets Theory_Dr.Khaled.Bakro د. خالد بكروDiscrete mathematics Ch1 sets Theory_Dr.Khaled.Bakro د. خالد بكرو
Discrete mathematics Ch1 sets Theory_Dr.Khaled.Bakro د. خالد بكرو
 
Limite de função de duas variáveis
Limite de função de duas variáveisLimite de função de duas variáveis
Limite de função de duas variáveis
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
Fuzzy logic
Fuzzy logicFuzzy logic
Fuzzy logic
 
Set in discrete mathematics
Set in discrete mathematicsSet in discrete mathematics
Set in discrete mathematics
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
 
Fuzzy Set Theory
Fuzzy Set TheoryFuzzy Set Theory
Fuzzy Set Theory
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- Basics
 
Chapter 2: Relations
Chapter 2: RelationsChapter 2: Relations
Chapter 2: Relations
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Theory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and ProblemsTheory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and Problems
 
Metric space
Metric spaceMetric space
Metric space
 
Closure properties of context free grammar
Closure properties of context free grammarClosure properties of context free grammar
Closure properties of context free grammar
 
Properties of relations
Properties of relationsProperties of relations
Properties of relations
 
Maximal and minimal elements of poset.pptx
Maximal and minimal elements of poset.pptxMaximal and minimal elements of poset.pptx
Maximal and minimal elements of poset.pptx
 
Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 
What's New in Python 3.10?
What's New in Python 3.10?What's New in Python 3.10?
What's New in Python 3.10?
 
Equivalence relations
Equivalence relationsEquivalence relations
Equivalence relations
 
Context free languages
Context free languagesContext free languages
Context free languages
 
Mathematical preliminaries in Automata
Mathematical preliminaries in AutomataMathematical preliminaries in Automata
Mathematical preliminaries in Automata
 

Andere mochten auch

Interactive Scientific Image Analysis using Spark
Interactive Scientific Image Analysis using SparkInteractive Scientific Image Analysis using Spark
Interactive Scientific Image Analysis using Spark
Kevin Mader
 
Machine Learning with Apache Mahout
Machine Learning with Apache MahoutMachine Learning with Apache Mahout
Machine Learning with Apache Mahout
Daniel Glauser
 
Functional programming
Functional programmingFunctional programming
Functional programming
edusmildo
 

Andere mochten auch (11)

Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Interactive Scientific Image Analysis using Spark
Interactive Scientific Image Analysis using SparkInteractive Scientific Image Analysis using Spark
Interactive Scientific Image Analysis using Spark
 
Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey
 
Machine Learning with Apache Mahout
Machine Learning with Apache MahoutMachine Learning with Apache Mahout
Machine Learning with Apache Mahout
 
Modeling with Hadoop kdd2011
Modeling with Hadoop kdd2011Modeling with Hadoop kdd2011
Modeling with Hadoop kdd2011
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Predictive Analytics Project in Automotive Industry
Predictive Analytics Project in Automotive IndustryPredictive Analytics Project in Automotive Industry
Predictive Analytics Project in Automotive Industry
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 

Mehr von Norman Richards

The Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unificationThe Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unification
Norman Richards
 

Mehr von Norman Richards (8)

An Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScriptAn Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScript
 
Logic programming a ruby perspective
Logic programming a ruby perspectiveLogic programming a ruby perspective
Logic programming a ruby perspective
 
Lisp 1.5 - Running history
Lisp 1.5 - Running historyLisp 1.5 - Running history
Lisp 1.5 - Running history
 
The Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unificationThe Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unification
 
Deconstructing the Functional Web with Clojure
Deconstructing the Functional Web with ClojureDeconstructing the Functional Web with Clojure
Deconstructing the Functional Web with Clojure
 
Immutant
ImmutantImmutant
Immutant
 
core.logic introduction
core.logic introductioncore.logic introduction
core.logic introduction
 
Vert.X mini-talk
Vert.X mini-talkVert.X mini-talk
Vert.X mini-talk
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

The Lambda Calculus and The JavaScript

  • 1. orb@nostacktrace.com The Lambda Calculus λ and The Javascript Norman Richards
  • 2. orb@nostacktrace.com Why lambda calculus? Alan TUring Alonzo CHURCh Invented the turing machine, an Created lambda calculus, the imperative computing model basis for functional programming. We spend all our time studying this guy’s work (because he’s awesome) Norman Richards http://en.wikipedia.org/wiki/Alonzo_Church But if we want to think functionally, http://en.wikipedia.org/wiki/Alan_Turing we should pay attention to this guy too (because he’s also awesome)
  • 3. orb@nostacktrace.com The premise Functional programming is good Using functional techniques will make your code better Understanding lambda calculus will improve your functional Norman Richards
  • 4. orb@nostacktrace.com A Lambda Expression bound variable This is the argument of the function λx.x body A lambda expression that uses the bound variable and represents the function value Norman Richards
  • 5. orb@nostacktrace.com A Lambda Expression function(x) { λx.x return x } Norman Richards
  • 6. orb@nostacktrace.com Not a Lambda Expression λy.x free variable This variable isn’t bound, so this expression is meaningless Norman Richards
  • 7. orb@nostacktrace.com A Lambda Expression function(y) { λy.x return x } Norman Richards These are meaningless
  • 8. orb@nostacktrace.com A Lambda Expression parenthesis Not needed, but used for clarity here λx.(λ.y.x) bound or free? x is bound in the outer expression and free in inner expression. Norman Richards
  • 9. orb@nostacktrace.com A Lambda Expression function(x) { return function(y) { λx.λ.y.x return x } } Norman Richards
  • 10. orb@nostacktrace.com Lambda Application unicorns don’t exist There are no assignments or external references. We’ll use upper-case words to stand for some valid lambda expression, but it must mean something. λx.x UNICORN application by substitution Apply UNICORN to the expression, substituting UNICORN for every occurrence of the bound value Norman Richards
  • 11. orb@nostacktrace.com Lambda Application The I (identity) combinator Combinatory logic predates lambda calculus, but the models are quite similar. Many lambda expressions are referred to by their equivalent combinator names. function(x) { λx.x UNICORN return x ! UNICORN }(UNICORN); ! UNICORN Norman Richards
  • 12. orb@nostacktrace.com Lambda Application the K (constant) combinator This function takes an input argument and returns a function that returns that same value λx.λy.x UNICORN λy.UNICORN MITT ! λy.UNICORN ! UNICORN always a UNICORN Norman Richards No matter what argument is passed in, the result is always the same. http://leftaction.com/action/ask-kansas-mitt-romney-unicorn
  • 13. orb@nostacktrace.com Lambda Application self-application This function takes a function and applies it to itself. function(f) { λs.(s s) return f(f); } Norman Richards
  • 14. orb@nostacktrace.com Lambda Application Substitute λx.x for s λs.(s s) λx.x function(f) { ! λx.x λx.x return f(f); }(function (x) { ! λx1.x1 λx.x return x; For clarity, rename x as x1 ! λx.x }); Norman Richards Substitute λx.x for x1.
  • 15. orb@nostacktrace.com Infinite application λs.(s s) λs.(s s) ! λs.(s s) λs.(s s) ! λs.(s s) λs.(s s) ! … the halting problem Norman Richards Just as you’d expect, there’s no algorithm to determine whether or not a given lambda expression will terminate
  • 16. orb@nostacktrace.com Turing Complete I: λx.x K: λx.λy.x S: λx.λy.λz.x z (y z) Norman Richards http://en.wikipedia.org/wiki/SKI_combinator_calculus
  • 17. orb@nostacktrace.com Actually, just two lambdas S K K UNICORN ! λx.λy.λz.(x z (y z)) K K UNICORN ! λy.λz.(K z (y z)) K UNICORN ! λz.(K z (K z)) UNICORN ! K UNICORN (K UNICORN) ! λx.λy.x UNICORN (K UNICORN) ! λy.UNICORN (K UNICORN) Norman Richards ! UNICORN Thus, S K K is computationally equivalent to identity
  • 18. orb@nostacktrace.com Other Turing Complete things Magic: the Gathering http://www.toothycat.net/ ~hologram/Turing/ C++ Templates The number 110 http://citeseerx.ist.psu.edu/ Norman Richards viewdoc/summary? http://mathworld.wolfram.com/ doi=10.1.1.14.3670 Rule110.html
  • 19. orb@nostacktrace.com TURING TARPIT Beware of the Turing tar-pit in which everything is possible but nothing of interest is easy. Alan Perlis Norman Richards http://pu.inf.uni-tuebingen.de/users/klaeren/epigrams.html
  • 20. orb@nostacktrace.com Currying function(x) { return function(y) { return function (z) { A function that returns a FUNCTION return UNICORN; .. that returns a function that returns a function. You can think of this as a function of three arguments. } λx.λy.λz.UNICORN X Y Z } }(X)(Y)(Z); Norman Richards function(x,y,z) { return UNICORN; }(X,Y,Z);
  • 21. orb@nostacktrace.com Currying Sometimes abbreviated Sometimes, you’ll see lambda expressions written like this for clarity. The arguments are still curried. λx y z.UNICORN X Y Z HASKELL CURRY ! λy z.UNICORN Y Z Curry studied combinatory logic, a variant of lambda calculus. ! λz.UNICORN Z ! UNICORN Norman Richards http://www.haskell.org/haskellwiki/Haskell_Brooks_Curry
  • 22. orb@nostacktrace.com data structures CHURCH PAIR ENCODING PAIR takes two arguments, x and y and returns a function that takes another function and applies x and y to it. PAIR: λx.λy.λf.(f x y) BuILDING UP STATE PAIR takes two arguments, x and y and returns a function that takes another function and applies x and y to it. Norman Richards
  • 23. orb@nostacktrace.com data structures PAIR: λx.λy.λf.(f x y) FIRST: λx.λy.x SECOND: λx.λy.y ACCESSOR FUNCTIONS FIRST and SECOND are functions that can be Norman Richards passed to pair. They take two arguments and return the first and second, respectively.
  • 24. orb@nostacktrace.com data structures PAIR: λx.λy.λf.(f x y) DOGCAT: PAIR DOG CAT ! λx.λy.λf.(f x y) DOG CAT ! λy.λf.(f DOG y) CAT ! λf.(f DOG CAT) Norman Richards
  • 25. orb@nostacktrace.com data structures DOGCAT: λf.(f DOG CAT) FIRST: λx.λy.x SECOND: λx.λy.y DOGCAT FIRST ! λf.(f DOG CAT) λx.λy.x ! λx.λy.x DOG CAT Norman Richards ! λy.DOG CAT ! DOG
  • 26. orb@nostacktrace.com data structures DOGCAT: λf.(f DOG CAT) FIRST: λx.λy.x SECOND: λx.λy.y DOGCAT SECOND ! λf.(f DOG CAT) λx.λy.x ! λx.λy.x DOG CAT Norman Richards ! λy.y CAT ! CAT
  • 27. orb@nostacktrace.com data structures RGB: λr.λg.λb.λf.(f r g b) RED: λr.λg.λb.r GREEN: λr.λg.λb.g BLUE: λr.λg.λb.b BLACK_COLOR: RGB 255 255 255 Norman Richards WHITE_COLOR: RGB 0 0 0 NuMBERS? We haven’t seen how to construct numbers yet, but for the moment imagine that we did.
  • 28. orb@nostacktrace.com data structures AVERAGE? AVG: λx.λy.??? If we did have numbers, we could probably do math like simple averaging MIX_RGB: λc1.λc2.λf. (f (AVG (c1 RED) (c2 RED)) (AVG (c1 GREEN)(c2 GREEN)) (AVG (c1 BLUE) (c2 BLUE))) Norman Richards OO LAMBDA? MIX_RGB takes two colors and returns a new color that is the mix of the two
  • 29. orb@nostacktrace.com CONDITIONALS COND: λe1.λe2.λc.(c e1 e2) TRUE: λx.λy.x FALSE: λx.λy.y Look familiar? Think of a boolean condition as a PAIR of values. TRUE selects the first one and FALSE selects the second one. Norman Richards
  • 30. orb@nostacktrace.com CONDITIONALS COND DEAD ALIVE QUBIT ! λe1.λe2.λc.(c e1 e2) DEAD ALIVE QUBIT ! QUBIT DEAD ALIVE ! TRUE DEAD ALIVE ! FALSE DEAD ALIVE ! λx.λy.x DEAD ALIVE ! λx.λy.y DEAD ALIVE Norman Richards ! DEAD ! ALIVE https://www.coursera.org/course/qcomp
  • 31. orb@nostacktrace.com BOOLEAN LOGIC NOT: λx.(COND FALSE TRUE x) AND: λx.λy.(COND y FALSE x) OR: λx.λy.(COND TRUE y x) Norman Richards
  • 32. orb@nostacktrace.com NUMBERS ZERO: λx.x SUCC: λn.λs.(s false n) ONE: SUCC ZERO ! λs.(s false ZERO) TWO: SUCC ONE ! λs.(s false ONE) ! λs.(s false λs.(s false ZERO)) Norman Richards PEANO NUMBERS PEANO numbers are based on a zero function and a successor function
  • 33. orb@nostacktrace.com NUMBERS ISZERO ZERO ZERO: λx.x ! λn.(n FIRST) λx.x ISZERO: λn.(n FIRST) ! λx.x FIRST ! FIRST Norman Richards ! TRUE
  • 34. orb@nostacktrace.com NUMBERS ISZERO: λn.(n FIRST) ONE: λs.(s FALSE ZERO) ISZERO ONE ! λn.(n FIRST) λs.(s FALSE ZERO) ! λs.(s FALSE ZERO) FIRST ! (FIRST FALSE ZERO) Norman Richards ! FALSE
  • 35. orb@nostacktrace.com NUMBERS PRED: λn.(n SECOND) PRED (SUCC NUM) ! λn.(n SECOND) λs.(s FALSE NUM) ! λs.(s FALSE ZERO) SECOND ! SECOND FALSE NUM ! NUM Norman Richards but... ZERO isn’t SUCC of a number
  • 36. orb@nostacktrace.com NUMBERS PRED: λn.(ISZERO n) ZERO (n SECOND) What is Pred of zero? We can at least test for zero and return another number. Norman Richards
  • 37. orb@nostacktrace.com NUMBERS ADD:λx.λy.(ISZERO Y) function add(x,y) { x if (y==0) { (ADD (SUCC X) return x (PRED Y)) } else { return add(x+1,y-1) NOT VALID Recursive definitions aren’t possible . } Norman Richards Remember, names are just syntactics sugar. All definitions must be finite. How do we do this? }
  • 38. orb@nostacktrace.com RECURSION DIVERSION If we could pass the function in Now we’ll make a function of three arguments, the first being the function to recurse on ADD 1:λf.λx.λy.(ISZERO Y) x (f f (SUCC X) (PRED Y)) Then we could call it RECURSIVELY Just remember that the function takes three Norman Richards arguments, so we should pass the function to itself.
  • 39. orb@nostacktrace.com RECURSION DIVERSION ADD 1:λf.λx.λy.(ISZERO Y) x (f f (SUCC X) (PRED Y)) ADD: ADD 1 ADD 1 ! λx.λy.(ISZERO Y) x Norman Richards (ADD 1 ADD1 (SUCC X) (PRED Y)) FINITE DEFINiTION This definition of ADD is a bit repetitive, but it doesn’t recurse infinitely. If only we could clean this up a bit...
  • 40. orb@nostacktrace.com BEHOLD, the Y combinator Y: λf.(λs.(f (s s)) (λs.(f (s s))) ADD 1: λf.λx.λy.(ISZERO Y) X (F (SUCC X) (PRED Y)) ADD: Y ADD 1 ! λs.(ADD 1 (s s)) (λs.(ADD1 (s s)) ! ADD1 (λs.(ADD1 (s s)) λs.(ADD1 (s s))) Norman Richards ! ADD1 (Y ADD1) ! ADD1 ADD Y self replicates This is similar to the hand prior call, except that the replication state is in the passed function.
  • 41. orb@nostacktrace.com CHURCH NUMBERs ZERO: λf.λx.x ONE: λf.λx.f x TWO: λf.λx.f (f x) THREE: λf.λx.f (f (f x)) FOUR: λf.λx.f (f (f (f x))) repeated function application Norman Richards Church numbers take a function and an argument and apply the function to the argument n times. So N is defined by doing something N times.
  • 42. orb@nostacktrace.com CHURCH NUMBERs ZERO: λn.(n λx.FALSE TRUE) SUCC: λn.λf.λx.f (n f x) ADD: λm.λn.λf.λx.m f (n f x) MUL: λm.λn.λf.m (n f) POW: λm.λn.m n PRED: λn.λf.λx.n (λg.λh. h (g f)) λu.x λu.u Norman Richards No recursion for simple math Church numbers have some very simple operations, Operations like subtraction are more complex than Peano numbers.
  • 43. orb@nostacktrace.com And the rest is functional An introduction to Functional Programming Through Lambda Calculus Greg Michaelson Norman Richards http://www.amazon.com/dp/0486478831
  • 44. orb@nostacktrace.com Leisure https://github.com/zot/Leisure •Lambda Calculus engine written in Javascript •Intended for programming •REPL environment with node.js •Runs in the browser Norman Richards http://tinyconcepts.com/invaders.html
  • 45. orb@nostacktrace.com λ thank you Norman Richards http://www.slideshare.net/normanrichards/the-lambda-calculus-and-the-javascript