SlideShare a Scribd company logo
1 of 119
Download to read offline
Implementing Untyped λ-Calculus




       Implementing Untyped λ-Calculus
                                  in Haskell




slide: Jaiyalas
Lin Jen-Shin (godfat)                          1 / 94
Implementing Untyped λ-Calculus




     godfat.org/slide/2012-05-08-
     lambda-draft.pdf



                                    2 / 94
Implementing Untyped λ-Calculus
   Who Am I?




                     Who Am I?


                                  3 / 94
Implementing Untyped λ-Calculus
   Who Am I?




What I learned?



        ▶   2007∼present: (learning) Haskell
        ▶   2006∼present: Ruby
        ▶   2005∼2008: C++
        ▶   2001∼2004: C



                                               4 / 94
Implementing Untyped λ-Calculus
   Who Am I?




What I worked?




        ▶   roodo.com
        ▶   cardinalblue.com




                                  5 / 94
Implementing Untyped λ-Calculus
   Who Am I?




Where you can find me



        ▶   github.com/godfat
        ▶   twitter.com/godfat
        ▶   profiles.google.com/godfat




                                        6 / 94
Implementing Untyped λ-Calculus
   Who Am I?




How I started to learn Haskell



        ▶   PLT at ssh://bbs@ptt.cc




                                      7 / 94
Implementing Untyped λ-Calculus
   Who Am I?




How I started to learn Haskell



        ▶   PLT at ssh://bbs@ptt.cc
        ▶   IIS at Sinica




                                      7 / 94
Implementing Untyped λ-Calculus
   Who Am I?




How I started to learn Haskell



        ▶   PLT at ssh://bbs@ptt.cc
        ▶   IIS at Sinica
        ▶   FLOLAC




                                      7 / 94
Implementing Untyped λ-Calculus




Table of Contents


        ▶   What can Haskell do?
        ▶   What is λ-Calculus?
        ▶   Why Implement λ-Calculus?
        ▶   Let's Implement λ-Calculus
        ▶   Questions?
        ▶   References


                                         8 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




dummy


        ▶   What can Haskell do?
        ▶   What is λ-Calculus?
        ▶   Why Implement λ-Calculus?
        ▶   Let's Implement λ-Calculus
        ▶   Questions?
        ▶   References


                                         9 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




          Defined in 1990


                                  10 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




               Successor of
               Miranda from
                   1985

                                  11 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




                      Haskell 98


                                   12 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




                Haskell 2010


                                  13 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




GHC (Glasgow Haskell Compiler)



        ▶   STM (Software Transactional Memory)




                                                  14 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




GHC (Glasgow Haskell Compiler)



        ▶   STM (Software Transactional Memory)
        ▶   Template Haskell




                                                  14 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




GHC (Glasgow Haskell Compiler)



        ▶   STM (Software Transactional Memory)
        ▶   Template Haskell
        ▶   GADT (Generalized Algebraic Data Type)




                                                     14 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




Notable Projects



        ▶   Audrey Tang's (唐鳳) Pugs




                                      15 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




Notable Projects



        ▶   Audrey Tang's (唐鳳) Pugs
        ▶   xmonad




                                      15 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




Notable Projects



        ▶   Audrey Tang's (唐鳳) Pugs
        ▶   xmonad
        ▶   Darcs




                                      15 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




Parallelism vs Concurrency?



        ▶   par-tutorial




                                  16 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




Parallelism vs Concurrency?



        ▶   par-tutorial
        ▶   Parallelism ̸= Concurrency




                                         16 / 94
Implementing Untyped λ-Calculus
   What can Haskell do?




Parallelism vs Concurrency?



        ▶   par-tutorial
        ▶   Parallelism ̸= Concurrency
        ▶   Parallelism is not concurrency




                                             16 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




dummy


        ▶   What can Haskell do?
        ▶   What is λ-Calculus?
        ▶   Why Implement λ-Calculus?
        ▶   Let's Implement λ-Calculus
        ▶   Questions?
        ▶   References


                                         17 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




What is λ-Calculus?




            An important formal system
            for functional programming




                                         18 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine




            Turing Machine


                                  19 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine




              by Alan Turing
                 in 1936


                                  20 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine




                                  21 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine



Turing Machine


        ▶   Infinite tape which stores symbols (memory)




                                                         22 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine



Turing Machine


        ▶   Infinite tape which stores symbols (memory)
        ▶   Finite action table which represents
            (state, symbol) → action (program)




                                                         22 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine



Turing Machine


        ▶   Infinite tape which stores symbols (memory)
        ▶   Finite action table which represents
            (state, symbol) → action (program)
        ▶   Robotic arm (CPU with a register which stores
            current state)




                                                            22 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine



Turing Machine


        ▶   Infinite tape which stores symbols (memory)
        ▶   Finite action table which represents
            (state, symbol) → action (program)
        ▶   Robotic arm (CPU with a register which stores
            current state)
               ■   Read the symbol on the tape at current position




                                                                 22 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine



Turing Machine


        ▶   Infinite tape which stores symbols (memory)
        ▶   Finite action table which represents
            (state, symbol) → action (program)
        ▶   Robotic arm (CPU with a register which stores
            current state)
               ■   Read the symbol on the tape at current position
               ■   Write a symbol on the tape at current position




                                                                 22 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine



Turing Machine


        ▶   Infinite tape which stores symbols (memory)
        ▶   Finite action table which represents
            (state, symbol) → action (program)
        ▶   Robotic arm (CPU with a register which stores
            current state)
               ■   Read the symbol on the tape at current position
               ■   Write a symbol on the tape at current position
               ■   Move the tape left or right



                                                                 22 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Turing Machine




         Turing Complete


                                  23 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




                     So what is
                     λ-calculus?


                                   24 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




Alligator Eggs!




                    http://worrydream.com/AlligatorEggs/

                                                           25 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




       by Alonzo Church
           in 1930s


                                  26 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




                         Also Turing
                          complete


                                       27 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




                         but why?


                                    28 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete




                      Define
                    λ-Complete


                                  29 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete




           Whatever
       λ-calculus can do


                                  30 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete



Implement Turing machine in λ-calculus




                                   31 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete



λ-Complete



        ▶   So λ-calculus can do anything TM can do




                                                      32 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete



λ-Complete



        ▶   So λ-calculus can do anything TM can do
        ▶   Plus what λ-calculus can do without a TM




                                                       32 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete



λ-Complete



        ▶   So λ-calculus can do anything TM can do
        ▶   Plus what λ-calculus can do without a TM
        ▶   Thus λ-calculus is Turing complete




                                                       32 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete




                  On the other
                    hand...


                                  33 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete



Implement λ-calculus in Turing machine




                                   34 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete




             So Turing
           machine is also
            λ-complete


                                  35 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     λ-Complete



They have the same computability




                                   36 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




What exactly is λ-calculus?



        ▶   (λx. x+1)




                                  37 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




What exactly is λ-calculus?



        ▶   (λx. x+1)
        ▶   (λx. x+1) 1




                                  37 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




What exactly is λ-calculus?



        ▶   (λx. x+1)
        ▶   (λx. x+1) 1
        ▶   = (1+1)




                                  37 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




What exactly is λ-calculus?



        ▶   (λx. x+1)
        ▶   (λx. x+1) 1
        ▶   = (1+1)
        ▶   = 2




                                  37 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




Does it look like Lisp?




            (λf.(λx.f (x x)) (λx.f (x x)))




                                             38 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Church Encoding




        Church Encoding


                                  39 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Church Encoding



Natural Numbers



        ▶   0 ≡ λf.λx. x
        ▶   1 ≡ λf.λx. f x
        ▶   ...
        ▶   n ≡ λf.λx. fn x



                                  40 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Church Encoding



Computation with Natural Numbers




        ▶   succ         ≡ λn.λf.λx. f (n f x)
        ▶   plus         ≡ λm.λn.λf.λx. m f (n f x)




                                                      41 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Church Encoding



Booleans




        ▶   true ≡ λa.λb. a
        ▶   false ≡ λa.λb. b




                                  42 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Church Encoding



Computation with Booleans



        ▶   and          ≡    λm.λn. m n m
        ▶   or           ≡    λm.λn. m m n
        ▶   not          ≡    λm.λa.λb. m b a
        ▶   if           ≡    λm.λa.λb. m a b



                                                43 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?




                   Recursion?
                  No problems

                                  44 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Y combinator



Y combinator



        ▶   Discovered by Haskell Curry
        ▶   y       = (λf.(λx.f (x x)) (λx.f (x x)))




                                                       45 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Y combinator



Y combinator



        ▶   Discovered by Haskell Curry
        ▶   y       = (λf.(λx.f (x x)) (λx.f (x x)))
        ▶   y f = (λf.(λx.f (x x)) (λx.f (x x))) f




                                                       45 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Y combinator



Y combinator



        ▶   Discovered by Haskell Curry
        ▶   y       = (λf.(λx.f (x x)) (λx.f (x x)))
        ▶   y f = (λf.(λx.f (x x)) (λx.f (x x))) f
        ▶   y f = f (y f)




                                                       45 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Y combinator




               Paul Graham,
              I know what is
              Y combinator!

                                  46 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Fixed Point Combinator



Fixed Point Combinator



        ▶   Strict languages need some delay
        ▶   Z = (λx.f (λv.((x x) v))) (λx.f (λv.((x x) v)))
        ▶   Discovered by Alan Turing
        ▶   Θ = (λx.λy. (y (x x y))) (λx.λy. (y (x x y)))




                                                              47 / 94
Implementing Untyped λ-Calculus
   What is λ-Calculus?
     Fixed Point Combinator



Fixed Point Combinator



        ▶   Constructed by Jan Willem Klop
        ▶   Yk = (L L L L L L L L L L L . . .)

            where L = λabcdefghijklmnopqstuvwxyzr. (r (thisisafixedpointcombinator))




                                                                                       48 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




dummy


        ▶   What can Haskell do?
        ▶   What is λ-Calculus?
        ▶   Why Implement λ-Calculus?
        ▶   Let's Implement λ-Calculus
        ▶   Questions?
        ▶   References


                                         49 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




dummy


        ▶   My interest in researching
            programming languages




                                         50 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




dummy


        ▶   My interest in researching
            programming languages
        ▶   Implementing λ-calculus in Haskell
            could teach us a lot in
            programming languages




                                                 50 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




dummy


        ▶   My interest in researching
            programming languages
        ▶   Implementing λ-calculus in Haskell
            could teach us a lot in
            programming languages
        ▶   It's my most influential Haskell exercise



                                                       50 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




dummy




           Let's Learn Haskell The Hard Way
           You a Haskell For Great Good!




                                              51 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




              But before we
                started...


                                  52 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




dummy



        ▶   No parsing here




                                  53 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




dummy



        ▶   No parsing here
        ▶   Parse tree (S-expression) interpreter only




                                                         53 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




dummy



        ▶   No parsing here
        ▶   Parse tree (S-expression) interpreter only
        ▶   Haskell is also good at parsing




                                                         53 / 94
Implementing Untyped λ-Calculus
   Why Implement λ-Calculus?




dummy



        ▶   No parsing here
        ▶   Parse tree (S-expression) interpreter only
        ▶   Haskell is also good at parsing
        ▶   e.g. parser combinator and parsec



                                                         53 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




dummy


        ▶   What can Haskell do?
        ▶   What is λ-Calculus?
        ▶   Why Implement λ-Calculus?
        ▶   Let's Implement λ-Calculus
        ▶   Questions?
        ▶   References


                                         54 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




First Expression and evaluate (source)
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
     .
     evaluate :: Expression -> Integer
     evaluate (Literal i)         = i
     evaluate (Plus expr0 expr1) =
       evaluate expr0 + evaluate expr1
     .
     test0 = evaluate (Literal 1)
     test1 = evaluate (Plus (Literal 1) (Literal 2))
     test2 = evaluate (Plus (Plus (Literal 1)
                                   (Literal 2))
                            (Literal 3))
                                                       55 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




First Expression and evaluate (source)
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
     .
     evaluate :: Expression -> Integer
     evaluate (Literal i)         = i
     evaluate (Plus expr0 expr1) =
       evaluate expr0 + evaluate expr1
     .
     test0 = evaluate (Literal 1)
     test1 = evaluate (Plus (Literal 1) (Literal 2))
     test2 = evaluate (Plus (Plus (Literal 1)
                                   (Literal 2))
                            (Literal 3))
                                                       56 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




First Expression and evaluate (source)
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
     .
     evaluate :: Expression -> Integer
     evaluate (Literal i)         = i
     evaluate (Plus expr0 expr1) =
       evaluate expr0 + evaluate expr1
     .
     test0 = evaluate (Literal 1)
     test1 = evaluate (Plus (Literal 1) (Literal 2))
     test2 = evaluate (Plus (Plus (Literal 1)
                                   (Literal 2))
                            (Literal 3))
                                                       57 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




First Expression and evaluate (source)
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
     .
     evaluate :: Expression -> Integer
     evaluate (Literal i)         = i
     evaluate (Plus expr0 expr1) =
       evaluate expr0 + evaluate expr1
     .
     test0 = evaluate (Literal 1)
     test1 = evaluate (Plus (Literal 1) (Literal 2))
     test2 = evaluate (Plus (Plus (Literal 1)
                                   (Literal 2))
                            (Literal 3))
                                                       58 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Algebraic Datatypes
     module Main where
     .
     data Expression = Literal Integer
                            | Plus Expression Expression
     .
     evaluate :: Expression -> Integer
     evaluate (Literal i)                     = i
     evaluate (Plus expr0 expr1) =
     Think of interface and subclasses if you like OOP
     .
     test0 = evaluate (Literal 1)
     test1 = evaluate (Plus (Literal 1) (Literal 2))
     test2 = evaluate (Plus (Plus (Literal 1)
                                                (Literal 2))
                                      (Literal 3))
                                                               59 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Pattern Matching
     module Main where
     .
     data Expression = Literal Integer
                           | Plus Expression Expression
     .
     evaluate :: Expression -> Integer
     evaluate (Literal i)                   = i
     evaluate (Plus expr0 expr1) =
       evaluate expr0 + evaluate expr1
     .
     test0 = evaluate (Literal 1)
     Think of dynamic cast or instanceof with a switch if you like OOP
     test2 = evaluate (Plus (Plus (Literal 1)
                                              (Literal 2))
                                     (Literal 3))
                                                                         60 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




First Expression and evaluate (source)
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
     .
     evaluate :: Expression -> Integer
     evaluate (Literal i)         = i
     evaluate (Plus expr0 expr1) =
       evaluate expr0 + evaluate expr1
     .
     test0 = evaluate (Literal 1)
     test1 = evaluate (Plus (Literal 1) (Literal 2))
     test2 = evaluate (Plus (Plus (Literal 1)
                                   (Literal 2))
                            (Literal 3))
                                                       61 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Variable and Environment (source)
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
                     | Variable String
     .
     type Environment = [(String, Integer)]
     .
     evaluate :: Expression -> Environment -> Integer
     evaluate (Literal i)        env = i
     evaluate (Plus expr0 expr1) env =
       evaluate expr0 env + evaluate expr1 env
     evaluate (Variable name)    env =
       case lookup name env of (Just i) -> i

                                                        62 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Variable and Environment (source)
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
                     | Variable String
     .
     type Environment = [(String, Integer)]
     .
     evaluate :: Expression -> Environment -> Integer
     evaluate (Literal i)        env = i
     evaluate (Plus expr0 expr1) env =
       evaluate expr0 env + evaluate expr1 env
     evaluate (Variable name)    env =
       case lookup name env of (Just i) -> i

                                                        63 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Variable and Environment (source)
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
                     | Variable String
     .
     type Environment = [(String, Integer)]
     .
     evaluate :: Expression -> Environment -> Integer
     evaluate (Literal i)        env = i
     evaluate (Plus expr0 expr1) env =
       evaluate expr0 env + evaluate expr1 env
     evaluate (Variable name)    env =
       case lookup name env of (Just i) -> i

                                                        64 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Variable and Environment (source)
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
                     | Variable String
     .
     type Environment = [(String, Integer)]
     .
     evaluate :: Expression -> Environment -> Integer
     evaluate (Literal i)        env = i
     evaluate (Plus expr0 expr1) env =
       evaluate expr0 env + evaluate expr1 env
     evaluate (Variable name)    env =
       case lookup name env of (Just i) -> i

                                                        65 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Variable and Environment (source)



     test0 = evaluate (Variable "var") [("var", 1)]
     test1 = evaluate (Plus (Variable "var") (Literal
     2)) [("var", 1)]




                                                        66 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Type Alias
     module Main where
     .
     data Expression = Literal Integer
                     | Plus Expression Expression
                     | Variable Name
     .
     type Name = String
     type Environment = [(Name, Integer)]
     .
     evaluate :: Expression -> Environment -> Integer
     evaluate (Literal i)        env = i
     evaluate (Plus expr0 expr1) env =
       evaluate expr0 env + evaluate expr1 env
     evaluate (Variable name)    env =
       case lookup name env of (Just i) -> i
                                                        67 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Pair of a and b



     ("var", 2) :: (String, Integer)
     (2, "var") :: (Integer, String)




                                       68 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




List of a



      [1,2,3] :: [Integer]
      ["a","b","c"] :: [String]
      [("var", 1)] :: [(String, Integer)]




                                            69 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Curried Functions




     evaluate :: Expression -> Environment -> Integer




                                                        70 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Curried Functions




     evaluate :: Expression -> (Environment -> Integer)




                                                          71 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Uncurried Functions




     evaluate :: (Expression, Environment) -> Integer




                                                        72 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Uncurried Functions



     threeArguments ::
     (String, String, String) -> Integer




                                           73 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Curried Function



     threeArguments ::
     String -> String -> String -> Integer




                                             74 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Curried Function



     threeArguments ::
     String -> (String -> String -> Integer)




                                               75 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Curried Function



     threeArguments ::
     String -> (String -> (String -> Integer))




                                                 76 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




               (->) is
         right-associative


                                  77 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Partially Applied Function


     filter :: (a -> Bool) -> [a] -> [a]
     .
     above60 :: [a] -> [a]
     above60 = filter (>=60)
     .
     above60 [1..65] -- [60,61,62,63,64,65]




                                              78 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Partially Applied Function


     map :: (a -> b) -> [a] -> [b]
     .
     div2 :: [Integer] -> [Integer]
     div2 = map (`div`2)
     .
     div2 [1..5] -- [0,1,1,2,2]




                                      79 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Function Composition


     (.) :: (b -> c) -> (a -> b) -> a -> c
     .
     above60AndDiv2 :: [Integer] -> [Integer]
     above60AndDiv2 = div2 . above60
     .
     above60AndDiv2 [1..65] -- [30,30,31,31,32,32]




                                                     80 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Function Composition



     compose :: (b -> c) -> (a -> b) -> a -> c
     compose g f x = g (f x)




                                                 81 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




     Curried functions make function composition powerful,
     function composition makes curried function even more useful.




                                                                     82 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




List of Partially Applied Functions

     mapPlus = map (+) -- think of [(+), (+), ...]
     .
     mapPlus1to5 = mapPlus [1..5]
     -- think of [(1+), (2+), ...]
     .
     map ($1) mapPlus1to5 -- [2,3,4,5,6]
     .
     .
     -- applicative functor style
     (+) <$> [1..5] <*> [1] -- [2,3,4,5,6]




                                                     83 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Exception Handling

     data Expression = Literal Integer
                     | Plus Expression Expression
                     | Variable Name
     .
     type Name = String
     type Environment = [(Name, Integer)]
     .
     type Value = Maybe Integer
     .
     evaluate :: Expression -> Environment -> Value




                                                      84 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Exception Handling

     data Expression = Literal Integer
                     | Plus Expression Expression
                     | Variable Name
     .
     type Name = String
     type Environment = [(Name, Integer)]
     .
     type Value = Maybe Integer
     .
     evaluate :: Expression -> Environment -> Value
     evaluate (Literal i)        env = Just i
     evaluate (Variable name)    env = lookup name env


                                                         85 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Exception Handling

     evaluate (Plus expr0 expr1) env =
       let val0 = evaluate expr0 env
           val1 = evaluate expr1 env
       in
           case val0 of
             (Nothing) -> Nothing
             (Just i0) -> case val1 of
                            (Nothing) -> Nothing
                            (Just i1) -> Just (i0 + i1)




                                                          86 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Maybe Monad with do notation



     evaluate (Plus expr0 expr1) env =
       do
          val0 <- evaluate expr0 env
          val1 <- evaluate expr1 env
          return (val0 + val1)




                                         87 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Do notation underneath



     evaluate (Plus expr0 expr1) env =
       evaluate expr0 env >>= val0 ->
         evaluate expr1 env >>= val1 -> return (val0 +
     val1)




                                                          88 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




liftM2



     evaluate (Plus expr0 expr1) env =
       evaluate expr0 env `plus` evaluate expr1 env where
       plus = liftM2 (+)




                                                        89 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




                   Sorry! To be
                   continued...


                                  90 / 94
Implementing Untyped λ-Calculus
   Let's Implement λ-Calculus




Peek the final work




     https://github.com/godfat/sandbox/blob/master/haskell/fpug/01/la




                                                              91 / 94
Implementing Untyped λ-Calculus




We're hiring




           cblue.tw/jobs




                                  92 / 94
Implementing Untyped λ-Calculus




Feel free to ask me questions online



        ▶   github.com/godfat
        ▶   twitter.com/godfat
        ▶   profiles.google.com/godfat




                                        93 / 94
Implementing Untyped λ-Calculus




References


        ▶   To be listed...
        ▶

        ▶

        ▶

        ▶

        ▶




                                  94 / 94

More Related Content

Similar to 2012 05-08-lambda-draft

Quines—Programming your way back to where you were
Quines—Programming your way back to where you wereQuines—Programming your way back to where you were
Quines—Programming your way back to where you wereJean-Baptiste Mazon
 
Faster computation with matlab
Faster computation with matlabFaster computation with matlab
Faster computation with matlabMuhammad Alli
 
FlinkML - Big data application meetup
FlinkML - Big data application meetupFlinkML - Big data application meetup
FlinkML - Big data application meetupTheodoros Vasiloudis
 
Introduction to Machine Learning with Spark
Introduction to Machine Learning with SparkIntroduction to Machine Learning with Spark
Introduction to Machine Learning with Sparkdatamantra
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programmingNico Ludwig
 
FlinkML: Large Scale Machine Learning with Apache Flink
FlinkML: Large Scale Machine Learning with Apache FlinkFlinkML: Large Scale Machine Learning with Apache Flink
FlinkML: Large Scale Machine Learning with Apache FlinkTheodoros Vasiloudis
 
Cilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime SystemCilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime SystemShareek Ahamed
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in collegessuser7a7cd61
 
Command Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongCommand Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongIan Kluft
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16Max Kleiner
 
Manageable Puppet Infrastructure - PuppetConf 2014
Manageable Puppet Infrastructure - PuppetConf 2014Manageable Puppet Infrastructure - PuppetConf 2014
Manageable Puppet Infrastructure - PuppetConf 2014Puppet
 
Near realtime analytics - technology choice (@pavlobaron)
Near realtime analytics - technology choice (@pavlobaron)Near realtime analytics - technology choice (@pavlobaron)
Near realtime analytics - technology choice (@pavlobaron)Pavlo Baron
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsKonrad Malawski
 

Similar to 2012 05-08-lambda-draft (20)

Quines—Programming your way back to where you were
Quines—Programming your way back to where you wereQuines—Programming your way back to where you were
Quines—Programming your way back to where you were
 
vega
vegavega
vega
 
Faster computation with matlab
Faster computation with matlabFaster computation with matlab
Faster computation with matlab
 
FlinkML - Big data application meetup
FlinkML - Big data application meetupFlinkML - Big data application meetup
FlinkML - Big data application meetup
 
Introduction to Machine Learning with Spark
Introduction to Machine Learning with SparkIntroduction to Machine Learning with Spark
Introduction to Machine Learning with Spark
 
Tom Kyte at Hotsos 2015
Tom Kyte at Hotsos 2015Tom Kyte at Hotsos 2015
Tom Kyte at Hotsos 2015
 
Matlab tutorial 3
Matlab tutorial 3Matlab tutorial 3
Matlab tutorial 3
 
Dart workshop
Dart workshopDart workshop
Dart workshop
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
How TDD helps me design - A case study
How TDD helps me design - A case studyHow TDD helps me design - A case study
How TDD helps me design - A case study
 
Introduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimizationIntroduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimization
 
FlinkML: Large Scale Machine Learning with Apache Flink
FlinkML: Large Scale Machine Learning with Apache FlinkFlinkML: Large Scale Machine Learning with Apache Flink
FlinkML: Large Scale Machine Learning with Apache Flink
 
Cilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime SystemCilk - An Efficient Multithreaded Runtime System
Cilk - An Efficient Multithreaded Runtime System
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
Command Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongCommand Line Arguments with Getopt::Long
Command Line Arguments with Getopt::Long
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16
 
Manageable Puppet Infrastructure - PuppetConf 2014
Manageable Puppet Infrastructure - PuppetConf 2014Manageable Puppet Infrastructure - PuppetConf 2014
Manageable Puppet Infrastructure - PuppetConf 2014
 
Garbage
GarbageGarbage
Garbage
 
Near realtime analytics - technology choice (@pavlobaron)
Near realtime analytics - technology choice (@pavlobaron)Near realtime analytics - technology choice (@pavlobaron)
Near realtime analytics - technology choice (@pavlobaron)
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applications
 

More from Lin Jen-Shin

Server Development Workflow For PicCollage
Server Development Workflow For PicCollageServer Development Workflow For PicCollage
Server Development Workflow For PicCollageLin Jen-Shin
 
The Architecture of PicCollage Server
The Architecture of PicCollage ServerThe Architecture of PicCollage Server
The Architecture of PicCollage ServerLin Jen-Shin
 
Concurrent Ruby Application Servers
Concurrent Ruby Application ServersConcurrent Ruby Application Servers
Concurrent Ruby Application ServersLin Jen-Shin
 
2010 04-24-cerealize
2010 04-24-cerealize2010 04-24-cerealize
2010 04-24-cerealizeLin Jen-Shin
 
2008-12-21 Rubinius
2008-12-21 Rubinius2008-12-21 Rubinius
2008-12-21 RubiniusLin Jen-Shin
 
2010-04-13 Reactor Pattern & Event Driven Programming 2
2010-04-13 Reactor Pattern & Event Driven Programming 22010-04-13 Reactor Pattern & Event Driven Programming 2
2010-04-13 Reactor Pattern & Event Driven Programming 2Lin Jen-Shin
 
2010-02-09 Reactor Pattern & Event Driven Programming
2010-02-09 Reactor Pattern & Event Driven Programming2010-02-09 Reactor Pattern & Event Driven Programming
2010-02-09 Reactor Pattern & Event Driven ProgrammingLin Jen-Shin
 
2008-01-25 Tangible Value
2008-01-25 Tangible Value2008-01-25 Tangible Value
2008-01-25 Tangible ValueLin Jen-Shin
 
2007-06-24 The Lost Piece
2007-06-24 The Lost Piece2007-06-24 The Lost Piece
2007-06-24 The Lost PieceLin Jen-Shin
 

More from Lin Jen-Shin (9)

Server Development Workflow For PicCollage
Server Development Workflow For PicCollageServer Development Workflow For PicCollage
Server Development Workflow For PicCollage
 
The Architecture of PicCollage Server
The Architecture of PicCollage ServerThe Architecture of PicCollage Server
The Architecture of PicCollage Server
 
Concurrent Ruby Application Servers
Concurrent Ruby Application ServersConcurrent Ruby Application Servers
Concurrent Ruby Application Servers
 
2010 04-24-cerealize
2010 04-24-cerealize2010 04-24-cerealize
2010 04-24-cerealize
 
2008-12-21 Rubinius
2008-12-21 Rubinius2008-12-21 Rubinius
2008-12-21 Rubinius
 
2010-04-13 Reactor Pattern & Event Driven Programming 2
2010-04-13 Reactor Pattern & Event Driven Programming 22010-04-13 Reactor Pattern & Event Driven Programming 2
2010-04-13 Reactor Pattern & Event Driven Programming 2
 
2010-02-09 Reactor Pattern & Event Driven Programming
2010-02-09 Reactor Pattern & Event Driven Programming2010-02-09 Reactor Pattern & Event Driven Programming
2010-02-09 Reactor Pattern & Event Driven Programming
 
2008-01-25 Tangible Value
2008-01-25 Tangible Value2008-01-25 Tangible Value
2008-01-25 Tangible Value
 
2007-06-24 The Lost Piece
2007-06-24 The Lost Piece2007-06-24 The Lost Piece
2007-06-24 The Lost Piece
 

Recently uploaded

Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️soniya singh
 
Genesis 1:10 || Meditate the Scripture daily verse by verse
Genesis 1:10  ||  Meditate the Scripture daily verse by verseGenesis 1:10  ||  Meditate the Scripture daily verse by verse
Genesis 1:10 || Meditate the Scripture daily verse by versemaricelcanoynuay
 
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...anilsa9823
 
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...Amil Baba Mangal Maseeh
 
St John's Church Parish Diary for May 2024
St John's Church Parish Diary for May 2024St John's Church Parish Diary for May 2024
St John's Church Parish Diary for May 2024Chris Lyne
 
madina book to learn arabic part1
madina   book   to  learn  arabic  part1madina   book   to  learn  arabic  part1
madina book to learn arabic part1fa3el khair
 
Lesson 4 - How to Conduct Yourself on a Walk.pptx
Lesson 4 - How to Conduct Yourself on a Walk.pptxLesson 4 - How to Conduct Yourself on a Walk.pptx
Lesson 4 - How to Conduct Yourself on a Walk.pptxCelso Napoleon
 
Codex Singularity: Search for the Prisca Sapientia
Codex Singularity: Search for the Prisca SapientiaCodex Singularity: Search for the Prisca Sapientia
Codex Singularity: Search for the Prisca Sapientiajfrenchau
 
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service 🕶
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service  🕶CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service  🕶
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service 🕶anilsa9823
 
Surah Yasin and Daily Spiritual Practices
Surah Yasin and Daily Spiritual PracticesSurah Yasin and Daily Spiritual Practices
Surah Yasin and Daily Spiritual Practicesaijazuddin14
 
Genesis 1:7 || Meditate the Scripture daily verse by verse
Genesis 1:7  ||  Meditate the Scripture daily verse by verseGenesis 1:7  ||  Meditate the Scripture daily verse by verse
Genesis 1:7 || Meditate the Scripture daily verse by versemaricelcanoynuay
 
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️soniya singh
 
Pradeep Bhanot - Friend, Philosopher Guide And The Brand By Arjun Jani
Pradeep Bhanot - Friend, Philosopher Guide And The Brand By Arjun JaniPradeep Bhanot - Friend, Philosopher Guide And The Brand By Arjun Jani
Pradeep Bhanot - Friend, Philosopher Guide And The Brand By Arjun JaniPradeep Bhanot
 
Genesis 1:8 || Meditate the Scripture daily verse by verse
Genesis 1:8  ||  Meditate the Scripture daily verse by verseGenesis 1:8  ||  Meditate the Scripture daily verse by verse
Genesis 1:8 || Meditate the Scripture daily verse by versemaricelcanoynuay
 
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCRElite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCRDelhi Call girls
 
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCRElite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCRDelhi Call girls
 
MEIDUNIDADE COM JESUS PALESTRA ESPIRITA1.pptx
MEIDUNIDADE COM JESUS  PALESTRA ESPIRITA1.pptxMEIDUNIDADE COM JESUS  PALESTRA ESPIRITA1.pptx
MEIDUNIDADE COM JESUS PALESTRA ESPIRITA1.pptxMneasEntidades
 

Recently uploaded (20)

Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
 
English - The Story of Ahikar, Grand Vizier of Assyria.pdf
English - The Story of Ahikar, Grand Vizier of Assyria.pdfEnglish - The Story of Ahikar, Grand Vizier of Assyria.pdf
English - The Story of Ahikar, Grand Vizier of Assyria.pdf
 
Call Girls In Nehru Place 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In Nehru Place 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In Nehru Place 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In Nehru Place 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
Genesis 1:10 || Meditate the Scripture daily verse by verse
Genesis 1:10  ||  Meditate the Scripture daily verse by verseGenesis 1:10  ||  Meditate the Scripture daily verse by verse
Genesis 1:10 || Meditate the Scripture daily verse by verse
 
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
 
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
 
St John's Church Parish Diary for May 2024
St John's Church Parish Diary for May 2024St John's Church Parish Diary for May 2024
St John's Church Parish Diary for May 2024
 
madina book to learn arabic part1
madina   book   to  learn  arabic  part1madina   book   to  learn  arabic  part1
madina book to learn arabic part1
 
Lesson 4 - How to Conduct Yourself on a Walk.pptx
Lesson 4 - How to Conduct Yourself on a Walk.pptxLesson 4 - How to Conduct Yourself on a Walk.pptx
Lesson 4 - How to Conduct Yourself on a Walk.pptx
 
Codex Singularity: Search for the Prisca Sapientia
Codex Singularity: Search for the Prisca SapientiaCodex Singularity: Search for the Prisca Sapientia
Codex Singularity: Search for the Prisca Sapientia
 
English - The Forgotten Books of Eden.pdf
English - The Forgotten Books of Eden.pdfEnglish - The Forgotten Books of Eden.pdf
English - The Forgotten Books of Eden.pdf
 
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service 🕶
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service  🕶CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service  🕶
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service 🕶
 
Surah Yasin and Daily Spiritual Practices
Surah Yasin and Daily Spiritual PracticesSurah Yasin and Daily Spiritual Practices
Surah Yasin and Daily Spiritual Practices
 
Genesis 1:7 || Meditate the Scripture daily verse by verse
Genesis 1:7  ||  Meditate the Scripture daily verse by verseGenesis 1:7  ||  Meditate the Scripture daily verse by verse
Genesis 1:7 || Meditate the Scripture daily verse by verse
 
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
 
Pradeep Bhanot - Friend, Philosopher Guide And The Brand By Arjun Jani
Pradeep Bhanot - Friend, Philosopher Guide And The Brand By Arjun JaniPradeep Bhanot - Friend, Philosopher Guide And The Brand By Arjun Jani
Pradeep Bhanot - Friend, Philosopher Guide And The Brand By Arjun Jani
 
Genesis 1:8 || Meditate the Scripture daily verse by verse
Genesis 1:8  ||  Meditate the Scripture daily verse by verseGenesis 1:8  ||  Meditate the Scripture daily verse by verse
Genesis 1:8 || Meditate the Scripture daily verse by verse
 
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCRElite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
 
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCRElite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
 
MEIDUNIDADE COM JESUS PALESTRA ESPIRITA1.pptx
MEIDUNIDADE COM JESUS  PALESTRA ESPIRITA1.pptxMEIDUNIDADE COM JESUS  PALESTRA ESPIRITA1.pptx
MEIDUNIDADE COM JESUS PALESTRA ESPIRITA1.pptx
 

2012 05-08-lambda-draft

  • 1. Implementing Untyped λ-Calculus Implementing Untyped λ-Calculus in Haskell slide: Jaiyalas Lin Jen-Shin (godfat) 1 / 94
  • 2. Implementing Untyped λ-Calculus godfat.org/slide/2012-05-08- lambda-draft.pdf 2 / 94
  • 3. Implementing Untyped λ-Calculus Who Am I? Who Am I? 3 / 94
  • 4. Implementing Untyped λ-Calculus Who Am I? What I learned? ▶ 2007∼present: (learning) Haskell ▶ 2006∼present: Ruby ▶ 2005∼2008: C++ ▶ 2001∼2004: C 4 / 94
  • 5. Implementing Untyped λ-Calculus Who Am I? What I worked? ▶ roodo.com ▶ cardinalblue.com 5 / 94
  • 6. Implementing Untyped λ-Calculus Who Am I? Where you can find me ▶ github.com/godfat ▶ twitter.com/godfat ▶ profiles.google.com/godfat 6 / 94
  • 7. Implementing Untyped λ-Calculus Who Am I? How I started to learn Haskell ▶ PLT at ssh://bbs@ptt.cc 7 / 94
  • 8. Implementing Untyped λ-Calculus Who Am I? How I started to learn Haskell ▶ PLT at ssh://bbs@ptt.cc ▶ IIS at Sinica 7 / 94
  • 9. Implementing Untyped λ-Calculus Who Am I? How I started to learn Haskell ▶ PLT at ssh://bbs@ptt.cc ▶ IIS at Sinica ▶ FLOLAC 7 / 94
  • 10. Implementing Untyped λ-Calculus Table of Contents ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Let's Implement λ-Calculus ▶ Questions? ▶ References 8 / 94
  • 11. Implementing Untyped λ-Calculus What can Haskell do? dummy ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Let's Implement λ-Calculus ▶ Questions? ▶ References 9 / 94
  • 12. Implementing Untyped λ-Calculus What can Haskell do? Defined in 1990 10 / 94
  • 13. Implementing Untyped λ-Calculus What can Haskell do? Successor of Miranda from 1985 11 / 94
  • 14. Implementing Untyped λ-Calculus What can Haskell do? Haskell 98 12 / 94
  • 15. Implementing Untyped λ-Calculus What can Haskell do? Haskell 2010 13 / 94
  • 16. Implementing Untyped λ-Calculus What can Haskell do? GHC (Glasgow Haskell Compiler) ▶ STM (Software Transactional Memory) 14 / 94
  • 17. Implementing Untyped λ-Calculus What can Haskell do? GHC (Glasgow Haskell Compiler) ▶ STM (Software Transactional Memory) ▶ Template Haskell 14 / 94
  • 18. Implementing Untyped λ-Calculus What can Haskell do? GHC (Glasgow Haskell Compiler) ▶ STM (Software Transactional Memory) ▶ Template Haskell ▶ GADT (Generalized Algebraic Data Type) 14 / 94
  • 19. Implementing Untyped λ-Calculus What can Haskell do? Notable Projects ▶ Audrey Tang's (唐鳳) Pugs 15 / 94
  • 20. Implementing Untyped λ-Calculus What can Haskell do? Notable Projects ▶ Audrey Tang's (唐鳳) Pugs ▶ xmonad 15 / 94
  • 21. Implementing Untyped λ-Calculus What can Haskell do? Notable Projects ▶ Audrey Tang's (唐鳳) Pugs ▶ xmonad ▶ Darcs 15 / 94
  • 22. Implementing Untyped λ-Calculus What can Haskell do? Parallelism vs Concurrency? ▶ par-tutorial 16 / 94
  • 23. Implementing Untyped λ-Calculus What can Haskell do? Parallelism vs Concurrency? ▶ par-tutorial ▶ Parallelism ̸= Concurrency 16 / 94
  • 24. Implementing Untyped λ-Calculus What can Haskell do? Parallelism vs Concurrency? ▶ par-tutorial ▶ Parallelism ̸= Concurrency ▶ Parallelism is not concurrency 16 / 94
  • 25. Implementing Untyped λ-Calculus What is λ-Calculus? dummy ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Let's Implement λ-Calculus ▶ Questions? ▶ References 17 / 94
  • 26. Implementing Untyped λ-Calculus What is λ-Calculus? What is λ-Calculus? An important formal system for functional programming 18 / 94
  • 27. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Machine 19 / 94
  • 28. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine by Alan Turing in 1936 20 / 94
  • 29. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine 21 / 94
  • 30. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Machine ▶ Infinite tape which stores symbols (memory) 22 / 94
  • 31. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Machine ▶ Infinite tape which stores symbols (memory) ▶ Finite action table which represents (state, symbol) → action (program) 22 / 94
  • 32. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Machine ▶ Infinite tape which stores symbols (memory) ▶ Finite action table which represents (state, symbol) → action (program) ▶ Robotic arm (CPU with a register which stores current state) 22 / 94
  • 33. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Machine ▶ Infinite tape which stores symbols (memory) ▶ Finite action table which represents (state, symbol) → action (program) ▶ Robotic arm (CPU with a register which stores current state) ■ Read the symbol on the tape at current position 22 / 94
  • 34. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Machine ▶ Infinite tape which stores symbols (memory) ▶ Finite action table which represents (state, symbol) → action (program) ▶ Robotic arm (CPU with a register which stores current state) ■ Read the symbol on the tape at current position ■ Write a symbol on the tape at current position 22 / 94
  • 35. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Machine ▶ Infinite tape which stores symbols (memory) ▶ Finite action table which represents (state, symbol) → action (program) ▶ Robotic arm (CPU with a register which stores current state) ■ Read the symbol on the tape at current position ■ Write a symbol on the tape at current position ■ Move the tape left or right 22 / 94
  • 36. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Complete 23 / 94
  • 37. Implementing Untyped λ-Calculus What is λ-Calculus? So what is λ-calculus? 24 / 94
  • 38. Implementing Untyped λ-Calculus What is λ-Calculus? Alligator Eggs! http://worrydream.com/AlligatorEggs/ 25 / 94
  • 39. Implementing Untyped λ-Calculus What is λ-Calculus? by Alonzo Church in 1930s 26 / 94
  • 40. Implementing Untyped λ-Calculus What is λ-Calculus? Also Turing complete 27 / 94
  • 41. Implementing Untyped λ-Calculus What is λ-Calculus? but why? 28 / 94
  • 42. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete Define λ-Complete 29 / 94
  • 43. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete Whatever λ-calculus can do 30 / 94
  • 44. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete Implement Turing machine in λ-calculus 31 / 94
  • 45. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete λ-Complete ▶ So λ-calculus can do anything TM can do 32 / 94
  • 46. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete λ-Complete ▶ So λ-calculus can do anything TM can do ▶ Plus what λ-calculus can do without a TM 32 / 94
  • 47. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete λ-Complete ▶ So λ-calculus can do anything TM can do ▶ Plus what λ-calculus can do without a TM ▶ Thus λ-calculus is Turing complete 32 / 94
  • 48. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete On the other hand... 33 / 94
  • 49. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete Implement λ-calculus in Turing machine 34 / 94
  • 50. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete So Turing machine is also λ-complete 35 / 94
  • 51. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete They have the same computability 36 / 94
  • 52. Implementing Untyped λ-Calculus What is λ-Calculus? What exactly is λ-calculus? ▶ (λx. x+1) 37 / 94
  • 53. Implementing Untyped λ-Calculus What is λ-Calculus? What exactly is λ-calculus? ▶ (λx. x+1) ▶ (λx. x+1) 1 37 / 94
  • 54. Implementing Untyped λ-Calculus What is λ-Calculus? What exactly is λ-calculus? ▶ (λx. x+1) ▶ (λx. x+1) 1 ▶ = (1+1) 37 / 94
  • 55. Implementing Untyped λ-Calculus What is λ-Calculus? What exactly is λ-calculus? ▶ (λx. x+1) ▶ (λx. x+1) 1 ▶ = (1+1) ▶ = 2 37 / 94
  • 56. Implementing Untyped λ-Calculus What is λ-Calculus? Does it look like Lisp? (λf.(λx.f (x x)) (λx.f (x x))) 38 / 94
  • 57. Implementing Untyped λ-Calculus What is λ-Calculus? Church Encoding Church Encoding 39 / 94
  • 58. Implementing Untyped λ-Calculus What is λ-Calculus? Church Encoding Natural Numbers ▶ 0 ≡ λf.λx. x ▶ 1 ≡ λf.λx. f x ▶ ... ▶ n ≡ λf.λx. fn x 40 / 94
  • 59. Implementing Untyped λ-Calculus What is λ-Calculus? Church Encoding Computation with Natural Numbers ▶ succ ≡ λn.λf.λx. f (n f x) ▶ plus ≡ λm.λn.λf.λx. m f (n f x) 41 / 94
  • 60. Implementing Untyped λ-Calculus What is λ-Calculus? Church Encoding Booleans ▶ true ≡ λa.λb. a ▶ false ≡ λa.λb. b 42 / 94
  • 61. Implementing Untyped λ-Calculus What is λ-Calculus? Church Encoding Computation with Booleans ▶ and ≡ λm.λn. m n m ▶ or ≡ λm.λn. m m n ▶ not ≡ λm.λa.λb. m b a ▶ if ≡ λm.λa.λb. m a b 43 / 94
  • 62. Implementing Untyped λ-Calculus What is λ-Calculus? Recursion? No problems 44 / 94
  • 63. Implementing Untyped λ-Calculus What is λ-Calculus? Y combinator Y combinator ▶ Discovered by Haskell Curry ▶ y = (λf.(λx.f (x x)) (λx.f (x x))) 45 / 94
  • 64. Implementing Untyped λ-Calculus What is λ-Calculus? Y combinator Y combinator ▶ Discovered by Haskell Curry ▶ y = (λf.(λx.f (x x)) (λx.f (x x))) ▶ y f = (λf.(λx.f (x x)) (λx.f (x x))) f 45 / 94
  • 65. Implementing Untyped λ-Calculus What is λ-Calculus? Y combinator Y combinator ▶ Discovered by Haskell Curry ▶ y = (λf.(λx.f (x x)) (λx.f (x x))) ▶ y f = (λf.(λx.f (x x)) (λx.f (x x))) f ▶ y f = f (y f) 45 / 94
  • 66. Implementing Untyped λ-Calculus What is λ-Calculus? Y combinator Paul Graham, I know what is Y combinator! 46 / 94
  • 67. Implementing Untyped λ-Calculus What is λ-Calculus? Fixed Point Combinator Fixed Point Combinator ▶ Strict languages need some delay ▶ Z = (λx.f (λv.((x x) v))) (λx.f (λv.((x x) v))) ▶ Discovered by Alan Turing ▶ Θ = (λx.λy. (y (x x y))) (λx.λy. (y (x x y))) 47 / 94
  • 68. Implementing Untyped λ-Calculus What is λ-Calculus? Fixed Point Combinator Fixed Point Combinator ▶ Constructed by Jan Willem Klop ▶ Yk = (L L L L L L L L L L L . . .) where L = λabcdefghijklmnopqstuvwxyzr. (r (thisisafixedpointcombinator)) 48 / 94
  • 69. Implementing Untyped λ-Calculus Why Implement λ-Calculus? dummy ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Let's Implement λ-Calculus ▶ Questions? ▶ References 49 / 94
  • 70. Implementing Untyped λ-Calculus Why Implement λ-Calculus? dummy ▶ My interest in researching programming languages 50 / 94
  • 71. Implementing Untyped λ-Calculus Why Implement λ-Calculus? dummy ▶ My interest in researching programming languages ▶ Implementing λ-calculus in Haskell could teach us a lot in programming languages 50 / 94
  • 72. Implementing Untyped λ-Calculus Why Implement λ-Calculus? dummy ▶ My interest in researching programming languages ▶ Implementing λ-calculus in Haskell could teach us a lot in programming languages ▶ It's my most influential Haskell exercise 50 / 94
  • 73. Implementing Untyped λ-Calculus Why Implement λ-Calculus? dummy Let's Learn Haskell The Hard Way You a Haskell For Great Good! 51 / 94
  • 74. Implementing Untyped λ-Calculus Why Implement λ-Calculus? But before we started... 52 / 94
  • 75. Implementing Untyped λ-Calculus Why Implement λ-Calculus? dummy ▶ No parsing here 53 / 94
  • 76. Implementing Untyped λ-Calculus Why Implement λ-Calculus? dummy ▶ No parsing here ▶ Parse tree (S-expression) interpreter only 53 / 94
  • 77. Implementing Untyped λ-Calculus Why Implement λ-Calculus? dummy ▶ No parsing here ▶ Parse tree (S-expression) interpreter only ▶ Haskell is also good at parsing 53 / 94
  • 78. Implementing Untyped λ-Calculus Why Implement λ-Calculus? dummy ▶ No parsing here ▶ Parse tree (S-expression) interpreter only ▶ Haskell is also good at parsing ▶ e.g. parser combinator and parsec 53 / 94
  • 79. Implementing Untyped λ-Calculus Let's Implement λ-Calculus dummy ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Let's Implement λ-Calculus ▶ Questions? ▶ References 54 / 94
  • 80. Implementing Untyped λ-Calculus Let's Implement λ-Calculus First Expression and evaluate (source) module Main where . data Expression = Literal Integer | Plus Expression Expression . evaluate :: Expression -> Integer evaluate (Literal i) = i evaluate (Plus expr0 expr1) = evaluate expr0 + evaluate expr1 . test0 = evaluate (Literal 1) test1 = evaluate (Plus (Literal 1) (Literal 2)) test2 = evaluate (Plus (Plus (Literal 1) (Literal 2)) (Literal 3)) 55 / 94
  • 81. Implementing Untyped λ-Calculus Let's Implement λ-Calculus First Expression and evaluate (source) module Main where . data Expression = Literal Integer | Plus Expression Expression . evaluate :: Expression -> Integer evaluate (Literal i) = i evaluate (Plus expr0 expr1) = evaluate expr0 + evaluate expr1 . test0 = evaluate (Literal 1) test1 = evaluate (Plus (Literal 1) (Literal 2)) test2 = evaluate (Plus (Plus (Literal 1) (Literal 2)) (Literal 3)) 56 / 94
  • 82. Implementing Untyped λ-Calculus Let's Implement λ-Calculus First Expression and evaluate (source) module Main where . data Expression = Literal Integer | Plus Expression Expression . evaluate :: Expression -> Integer evaluate (Literal i) = i evaluate (Plus expr0 expr1) = evaluate expr0 + evaluate expr1 . test0 = evaluate (Literal 1) test1 = evaluate (Plus (Literal 1) (Literal 2)) test2 = evaluate (Plus (Plus (Literal 1) (Literal 2)) (Literal 3)) 57 / 94
  • 83. Implementing Untyped λ-Calculus Let's Implement λ-Calculus First Expression and evaluate (source) module Main where . data Expression = Literal Integer | Plus Expression Expression . evaluate :: Expression -> Integer evaluate (Literal i) = i evaluate (Plus expr0 expr1) = evaluate expr0 + evaluate expr1 . test0 = evaluate (Literal 1) test1 = evaluate (Plus (Literal 1) (Literal 2)) test2 = evaluate (Plus (Plus (Literal 1) (Literal 2)) (Literal 3)) 58 / 94
  • 84. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Algebraic Datatypes module Main where . data Expression = Literal Integer | Plus Expression Expression . evaluate :: Expression -> Integer evaluate (Literal i) = i evaluate (Plus expr0 expr1) = Think of interface and subclasses if you like OOP . test0 = evaluate (Literal 1) test1 = evaluate (Plus (Literal 1) (Literal 2)) test2 = evaluate (Plus (Plus (Literal 1) (Literal 2)) (Literal 3)) 59 / 94
  • 85. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Pattern Matching module Main where . data Expression = Literal Integer | Plus Expression Expression . evaluate :: Expression -> Integer evaluate (Literal i) = i evaluate (Plus expr0 expr1) = evaluate expr0 + evaluate expr1 . test0 = evaluate (Literal 1) Think of dynamic cast or instanceof with a switch if you like OOP test2 = evaluate (Plus (Plus (Literal 1) (Literal 2)) (Literal 3)) 60 / 94
  • 86. Implementing Untyped λ-Calculus Let's Implement λ-Calculus First Expression and evaluate (source) module Main where . data Expression = Literal Integer | Plus Expression Expression . evaluate :: Expression -> Integer evaluate (Literal i) = i evaluate (Plus expr0 expr1) = evaluate expr0 + evaluate expr1 . test0 = evaluate (Literal 1) test1 = evaluate (Plus (Literal 1) (Literal 2)) test2 = evaluate (Plus (Plus (Literal 1) (Literal 2)) (Literal 3)) 61 / 94
  • 87. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Variable and Environment (source) module Main where . data Expression = Literal Integer | Plus Expression Expression | Variable String . type Environment = [(String, Integer)] . evaluate :: Expression -> Environment -> Integer evaluate (Literal i) env = i evaluate (Plus expr0 expr1) env = evaluate expr0 env + evaluate expr1 env evaluate (Variable name) env = case lookup name env of (Just i) -> i 62 / 94
  • 88. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Variable and Environment (source) module Main where . data Expression = Literal Integer | Plus Expression Expression | Variable String . type Environment = [(String, Integer)] . evaluate :: Expression -> Environment -> Integer evaluate (Literal i) env = i evaluate (Plus expr0 expr1) env = evaluate expr0 env + evaluate expr1 env evaluate (Variable name) env = case lookup name env of (Just i) -> i 63 / 94
  • 89. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Variable and Environment (source) module Main where . data Expression = Literal Integer | Plus Expression Expression | Variable String . type Environment = [(String, Integer)] . evaluate :: Expression -> Environment -> Integer evaluate (Literal i) env = i evaluate (Plus expr0 expr1) env = evaluate expr0 env + evaluate expr1 env evaluate (Variable name) env = case lookup name env of (Just i) -> i 64 / 94
  • 90. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Variable and Environment (source) module Main where . data Expression = Literal Integer | Plus Expression Expression | Variable String . type Environment = [(String, Integer)] . evaluate :: Expression -> Environment -> Integer evaluate (Literal i) env = i evaluate (Plus expr0 expr1) env = evaluate expr0 env + evaluate expr1 env evaluate (Variable name) env = case lookup name env of (Just i) -> i 65 / 94
  • 91. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Variable and Environment (source) test0 = evaluate (Variable "var") [("var", 1)] test1 = evaluate (Plus (Variable "var") (Literal 2)) [("var", 1)] 66 / 94
  • 92. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Type Alias module Main where . data Expression = Literal Integer | Plus Expression Expression | Variable Name . type Name = String type Environment = [(Name, Integer)] . evaluate :: Expression -> Environment -> Integer evaluate (Literal i) env = i evaluate (Plus expr0 expr1) env = evaluate expr0 env + evaluate expr1 env evaluate (Variable name) env = case lookup name env of (Just i) -> i 67 / 94
  • 93. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Pair of a and b ("var", 2) :: (String, Integer) (2, "var") :: (Integer, String) 68 / 94
  • 94. Implementing Untyped λ-Calculus Let's Implement λ-Calculus List of a [1,2,3] :: [Integer] ["a","b","c"] :: [String] [("var", 1)] :: [(String, Integer)] 69 / 94
  • 95. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Curried Functions evaluate :: Expression -> Environment -> Integer 70 / 94
  • 96. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Curried Functions evaluate :: Expression -> (Environment -> Integer) 71 / 94
  • 97. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Uncurried Functions evaluate :: (Expression, Environment) -> Integer 72 / 94
  • 98. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Uncurried Functions threeArguments :: (String, String, String) -> Integer 73 / 94
  • 99. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Curried Function threeArguments :: String -> String -> String -> Integer 74 / 94
  • 100. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Curried Function threeArguments :: String -> (String -> String -> Integer) 75 / 94
  • 101. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Curried Function threeArguments :: String -> (String -> (String -> Integer)) 76 / 94
  • 102. Implementing Untyped λ-Calculus Let's Implement λ-Calculus (->) is right-associative 77 / 94
  • 103. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Partially Applied Function filter :: (a -> Bool) -> [a] -> [a] . above60 :: [a] -> [a] above60 = filter (>=60) . above60 [1..65] -- [60,61,62,63,64,65] 78 / 94
  • 104. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Partially Applied Function map :: (a -> b) -> [a] -> [b] . div2 :: [Integer] -> [Integer] div2 = map (`div`2) . div2 [1..5] -- [0,1,1,2,2] 79 / 94
  • 105. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Function Composition (.) :: (b -> c) -> (a -> b) -> a -> c . above60AndDiv2 :: [Integer] -> [Integer] above60AndDiv2 = div2 . above60 . above60AndDiv2 [1..65] -- [30,30,31,31,32,32] 80 / 94
  • 106. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Function Composition compose :: (b -> c) -> (a -> b) -> a -> c compose g f x = g (f x) 81 / 94
  • 107. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Curried functions make function composition powerful, function composition makes curried function even more useful. 82 / 94
  • 108. Implementing Untyped λ-Calculus Let's Implement λ-Calculus List of Partially Applied Functions mapPlus = map (+) -- think of [(+), (+), ...] . mapPlus1to5 = mapPlus [1..5] -- think of [(1+), (2+), ...] . map ($1) mapPlus1to5 -- [2,3,4,5,6] . . -- applicative functor style (+) <$> [1..5] <*> [1] -- [2,3,4,5,6] 83 / 94
  • 109. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Exception Handling data Expression = Literal Integer | Plus Expression Expression | Variable Name . type Name = String type Environment = [(Name, Integer)] . type Value = Maybe Integer . evaluate :: Expression -> Environment -> Value 84 / 94
  • 110. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Exception Handling data Expression = Literal Integer | Plus Expression Expression | Variable Name . type Name = String type Environment = [(Name, Integer)] . type Value = Maybe Integer . evaluate :: Expression -> Environment -> Value evaluate (Literal i) env = Just i evaluate (Variable name) env = lookup name env 85 / 94
  • 111. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Exception Handling evaluate (Plus expr0 expr1) env = let val0 = evaluate expr0 env val1 = evaluate expr1 env in case val0 of (Nothing) -> Nothing (Just i0) -> case val1 of (Nothing) -> Nothing (Just i1) -> Just (i0 + i1) 86 / 94
  • 112. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Maybe Monad with do notation evaluate (Plus expr0 expr1) env = do val0 <- evaluate expr0 env val1 <- evaluate expr1 env return (val0 + val1) 87 / 94
  • 113. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Do notation underneath evaluate (Plus expr0 expr1) env = evaluate expr0 env >>= val0 -> evaluate expr1 env >>= val1 -> return (val0 + val1) 88 / 94
  • 114. Implementing Untyped λ-Calculus Let's Implement λ-Calculus liftM2 evaluate (Plus expr0 expr1) env = evaluate expr0 env `plus` evaluate expr1 env where plus = liftM2 (+) 89 / 94
  • 115. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Sorry! To be continued... 90 / 94
  • 116. Implementing Untyped λ-Calculus Let's Implement λ-Calculus Peek the final work https://github.com/godfat/sandbox/blob/master/haskell/fpug/01/la 91 / 94
  • 117. Implementing Untyped λ-Calculus We're hiring cblue.tw/jobs 92 / 94
  • 118. Implementing Untyped λ-Calculus Feel free to ask me questions online ▶ github.com/godfat ▶ twitter.com/godfat ▶ profiles.google.com/godfat 93 / 94
  • 119. Implementing Untyped λ-Calculus References ▶ To be listed... ▶ ▶ ▶ ▶ ▶ 94 / 94