2012 05-08-lambda-draft

864 views

Published on

Published in: Spiritual, Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
864
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
13
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

2012 05-08-lambda-draft

  1. 1. Implementing Untyped λ-Calculus Implementing Untyped λ-Calculus in Haskellslide: JaiyalasLin Jen-Shin (godfat) 1 / 94
  2. 2. Implementing Untyped λ-Calculus godfat.org/slide/2012-05-08- lambda-draft.pdf 2 / 94
  3. 3. Implementing Untyped λ-Calculus Who Am I? Who Am I? 3 / 94
  4. 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. 5. Implementing Untyped λ-Calculus Who Am I?What I worked? ▶ roodo.com ▶ cardinalblue.com 5 / 94
  6. 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. 7. Implementing Untyped λ-Calculus Who Am I?How I started to learn Haskell ▶ PLT at ssh://bbs@ptt.cc 7 / 94
  8. 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. 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. 10. Implementing Untyped λ-CalculusTable of Contents ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Lets Implement λ-Calculus ▶ Questions? ▶ References 8 / 94
  11. 11. Implementing Untyped λ-Calculus What can Haskell do?dummy ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Lets Implement λ-Calculus ▶ Questions? ▶ References 9 / 94
  12. 12. Implementing Untyped λ-Calculus What can Haskell do? Defined in 1990 10 / 94
  13. 13. Implementing Untyped λ-Calculus What can Haskell do? Successor of Miranda from 1985 11 / 94
  14. 14. Implementing Untyped λ-Calculus What can Haskell do? Haskell 98 12 / 94
  15. 15. Implementing Untyped λ-Calculus What can Haskell do? Haskell 2010 13 / 94
  16. 16. Implementing Untyped λ-Calculus What can Haskell do?GHC (Glasgow Haskell Compiler) ▶ STM (Software Transactional Memory) 14 / 94
  17. 17. Implementing Untyped λ-Calculus What can Haskell do?GHC (Glasgow Haskell Compiler) ▶ STM (Software Transactional Memory) ▶ Template Haskell 14 / 94
  18. 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. 19. Implementing Untyped λ-Calculus What can Haskell do?Notable Projects ▶ Audrey Tangs (唐鳳) Pugs 15 / 94
  20. 20. Implementing Untyped λ-Calculus What can Haskell do?Notable Projects ▶ Audrey Tangs (唐鳳) Pugs ▶ xmonad 15 / 94
  21. 21. Implementing Untyped λ-Calculus What can Haskell do?Notable Projects ▶ Audrey Tangs (唐鳳) Pugs ▶ xmonad ▶ Darcs 15 / 94
  22. 22. Implementing Untyped λ-Calculus What can Haskell do?Parallelism vs Concurrency? ▶ par-tutorial 16 / 94
  23. 23. Implementing Untyped λ-Calculus What can Haskell do?Parallelism vs Concurrency? ▶ par-tutorial ▶ Parallelism ̸= Concurrency 16 / 94
  24. 24. Implementing Untyped λ-Calculus What can Haskell do?Parallelism vs Concurrency? ▶ par-tutorial ▶ Parallelism ̸= Concurrency ▶ Parallelism is not concurrency 16 / 94
  25. 25. Implementing Untyped λ-Calculus What is λ-Calculus?dummy ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Lets Implement λ-Calculus ▶ Questions? ▶ References 17 / 94
  26. 26. Implementing Untyped λ-Calculus What is λ-Calculus?What is λ-Calculus? An important formal system for functional programming 18 / 94
  27. 27. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Machine 19 / 94
  28. 28. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine by Alan Turing in 1936 20 / 94
  29. 29. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine 21 / 94
  30. 30. Implementing Untyped λ-Calculus What is λ-Calculus? Turing MachineTuring Machine ▶ Infinite tape which stores symbols (memory) 22 / 94
  31. 31. Implementing Untyped λ-Calculus What is λ-Calculus? Turing MachineTuring Machine ▶ Infinite tape which stores symbols (memory) ▶ Finite action table which represents (state, symbol) → action (program) 22 / 94
  32. 32. Implementing Untyped λ-Calculus What is λ-Calculus? Turing MachineTuring 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. 33. Implementing Untyped λ-Calculus What is λ-Calculus? Turing MachineTuring 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. 34. Implementing Untyped λ-Calculus What is λ-Calculus? Turing MachineTuring 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. 35. Implementing Untyped λ-Calculus What is λ-Calculus? Turing MachineTuring 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. 36. Implementing Untyped λ-Calculus What is λ-Calculus? Turing Machine Turing Complete 23 / 94
  37. 37. Implementing Untyped λ-Calculus What is λ-Calculus? So what is λ-calculus? 24 / 94
  38. 38. Implementing Untyped λ-Calculus What is λ-Calculus?Alligator Eggs! http://worrydream.com/AlligatorEggs/ 25 / 94
  39. 39. Implementing Untyped λ-Calculus What is λ-Calculus? by Alonzo Church in 1930s 26 / 94
  40. 40. Implementing Untyped λ-Calculus What is λ-Calculus? Also Turing complete 27 / 94
  41. 41. Implementing Untyped λ-Calculus What is λ-Calculus? but why? 28 / 94
  42. 42. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete Define λ-Complete 29 / 94
  43. 43. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete Whatever λ-calculus can do 30 / 94
  44. 44. Implementing Untyped λ-Calculus What is λ-Calculus? λ-CompleteImplement Turing machine in λ-calculus 31 / 94
  45. 45. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Completeλ-Complete ▶ So λ-calculus can do anything TM can do 32 / 94
  46. 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. 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. 48. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete On the other hand... 33 / 94
  49. 49. Implementing Untyped λ-Calculus What is λ-Calculus? λ-CompleteImplement λ-calculus in Turing machine 34 / 94
  50. 50. Implementing Untyped λ-Calculus What is λ-Calculus? λ-Complete So Turing machine is also λ-complete 35 / 94
  51. 51. Implementing Untyped λ-Calculus What is λ-Calculus? λ-CompleteThey have the same computability 36 / 94
  52. 52. Implementing Untyped λ-Calculus What is λ-Calculus?What exactly is λ-calculus? ▶ (λx. x+1) 37 / 94
  53. 53. Implementing Untyped λ-Calculus What is λ-Calculus?What exactly is λ-calculus? ▶ (λx. x+1) ▶ (λx. x+1) 1 37 / 94
  54. 54. Implementing Untyped λ-Calculus What is λ-Calculus?What exactly is λ-calculus? ▶ (λx. x+1) ▶ (λx. x+1) 1 ▶ = (1+1) 37 / 94
  55. 55. Implementing Untyped λ-Calculus What is λ-Calculus?What exactly is λ-calculus? ▶ (λx. x+1) ▶ (λx. x+1) 1 ▶ = (1+1) ▶ = 2 37 / 94
  56. 56. Implementing Untyped λ-Calculus What is λ-Calculus?Does it look like Lisp? (λf.(λx.f (x x)) (λx.f (x x))) 38 / 94
  57. 57. Implementing Untyped λ-Calculus What is λ-Calculus? Church Encoding Church Encoding 39 / 94
  58. 58. Implementing Untyped λ-Calculus What is λ-Calculus? Church EncodingNatural Numbers ▶ 0 ≡ λf.λx. x ▶ 1 ≡ λf.λx. f x ▶ ... ▶ n ≡ λf.λx. fn x 40 / 94
  59. 59. Implementing Untyped λ-Calculus What is λ-Calculus? Church EncodingComputation with Natural Numbers ▶ succ ≡ λn.λf.λx. f (n f x) ▶ plus ≡ λm.λn.λf.λx. m f (n f x) 41 / 94
  60. 60. Implementing Untyped λ-Calculus What is λ-Calculus? Church EncodingBooleans ▶ true ≡ λa.λb. a ▶ false ≡ λa.λb. b 42 / 94
  61. 61. Implementing Untyped λ-Calculus What is λ-Calculus? Church EncodingComputation 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. 62. Implementing Untyped λ-Calculus What is λ-Calculus? Recursion? No problems 44 / 94
  63. 63. Implementing Untyped λ-Calculus What is λ-Calculus? Y combinatorY combinator ▶ Discovered by Haskell Curry ▶ y = (λf.(λx.f (x x)) (λx.f (x x))) 45 / 94
  64. 64. Implementing Untyped λ-Calculus What is λ-Calculus? Y combinatorY 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. 65. Implementing Untyped λ-Calculus What is λ-Calculus? Y combinatorY 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. 66. Implementing Untyped λ-Calculus What is λ-Calculus? Y combinator Paul Graham, I know what is Y combinator! 46 / 94
  67. 67. Implementing Untyped λ-Calculus What is λ-Calculus? Fixed Point CombinatorFixed 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. 68. Implementing Untyped λ-Calculus What is λ-Calculus? Fixed Point CombinatorFixed 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. 69. Implementing Untyped λ-Calculus Why Implement λ-Calculus?dummy ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Lets Implement λ-Calculus ▶ Questions? ▶ References 49 / 94
  70. 70. Implementing Untyped λ-Calculus Why Implement λ-Calculus?dummy ▶ My interest in researching programming languages 50 / 94
  71. 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. 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 ▶ Its my most influential Haskell exercise 50 / 94
  73. 73. Implementing Untyped λ-Calculus Why Implement λ-Calculus?dummy Lets Learn Haskell The Hard Way You a Haskell For Great Good! 51 / 94
  74. 74. Implementing Untyped λ-Calculus Why Implement λ-Calculus? But before we started... 52 / 94
  75. 75. Implementing Untyped λ-Calculus Why Implement λ-Calculus?dummy ▶ No parsing here 53 / 94
  76. 76. Implementing Untyped λ-Calculus Why Implement λ-Calculus?dummy ▶ No parsing here ▶ Parse tree (S-expression) interpreter only 53 / 94
  77. 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. 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. 79. Implementing Untyped λ-Calculus Lets Implement λ-Calculusdummy ▶ What can Haskell do? ▶ What is λ-Calculus? ▶ Why Implement λ-Calculus? ▶ Lets Implement λ-Calculus ▶ Questions? ▶ References 54 / 94
  80. 80. Implementing Untyped λ-Calculus Lets Implement λ-CalculusFirst 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. 81. Implementing Untyped λ-Calculus Lets Implement λ-CalculusFirst 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. 82. Implementing Untyped λ-Calculus Lets Implement λ-CalculusFirst 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. 83. Implementing Untyped λ-Calculus Lets Implement λ-CalculusFirst 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. 84. Implementing Untyped λ-Calculus Lets Implement λ-CalculusAlgebraic 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. 85. Implementing Untyped λ-Calculus Lets Implement λ-CalculusPattern 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. 86. Implementing Untyped λ-Calculus Lets Implement λ-CalculusFirst 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. 87. Implementing Untyped λ-Calculus Lets Implement λ-CalculusVariable 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. 88. Implementing Untyped λ-Calculus Lets Implement λ-CalculusVariable 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. 89. Implementing Untyped λ-Calculus Lets Implement λ-CalculusVariable 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. 90. Implementing Untyped λ-Calculus Lets Implement λ-CalculusVariable 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. 91. Implementing Untyped λ-Calculus Lets Implement λ-CalculusVariable and Environment (source) test0 = evaluate (Variable "var") [("var", 1)] test1 = evaluate (Plus (Variable "var") (Literal 2)) [("var", 1)] 66 / 94
  92. 92. Implementing Untyped λ-Calculus Lets Implement λ-CalculusType 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. 93. Implementing Untyped λ-Calculus Lets Implement λ-CalculusPair of a and b ("var", 2) :: (String, Integer) (2, "var") :: (Integer, String) 68 / 94
  94. 94. Implementing Untyped λ-Calculus Lets Implement λ-CalculusList of a [1,2,3] :: [Integer] ["a","b","c"] :: [String] [("var", 1)] :: [(String, Integer)] 69 / 94
  95. 95. Implementing Untyped λ-Calculus Lets Implement λ-CalculusCurried Functions evaluate :: Expression -> Environment -> Integer 70 / 94
  96. 96. Implementing Untyped λ-Calculus Lets Implement λ-CalculusCurried Functions evaluate :: Expression -> (Environment -> Integer) 71 / 94
  97. 97. Implementing Untyped λ-Calculus Lets Implement λ-CalculusUncurried Functions evaluate :: (Expression, Environment) -> Integer 72 / 94
  98. 98. Implementing Untyped λ-Calculus Lets Implement λ-CalculusUncurried Functions threeArguments :: (String, String, String) -> Integer 73 / 94
  99. 99. Implementing Untyped λ-Calculus Lets Implement λ-CalculusCurried Function threeArguments :: String -> String -> String -> Integer 74 / 94
  100. 100. Implementing Untyped λ-Calculus Lets Implement λ-CalculusCurried Function threeArguments :: String -> (String -> String -> Integer) 75 / 94
  101. 101. Implementing Untyped λ-Calculus Lets Implement λ-CalculusCurried Function threeArguments :: String -> (String -> (String -> Integer)) 76 / 94
  102. 102. Implementing Untyped λ-Calculus Lets Implement λ-Calculus (->) is right-associative 77 / 94
  103. 103. Implementing Untyped λ-Calculus Lets Implement λ-CalculusPartially 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. 104. Implementing Untyped λ-Calculus Lets Implement λ-CalculusPartially 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. 105. Implementing Untyped λ-Calculus Lets Implement λ-CalculusFunction 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. 106. Implementing Untyped λ-Calculus Lets Implement λ-CalculusFunction Composition compose :: (b -> c) -> (a -> b) -> a -> c compose g f x = g (f x) 81 / 94
  107. 107. Implementing Untyped λ-Calculus Lets Implement λ-Calculus Curried functions make function composition powerful, function composition makes curried function even more useful. 82 / 94
  108. 108. Implementing Untyped λ-Calculus Lets Implement λ-CalculusList 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. 109. Implementing Untyped λ-Calculus Lets Implement λ-CalculusException 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. 110. Implementing Untyped λ-Calculus Lets Implement λ-CalculusException 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. 111. Implementing Untyped λ-Calculus Lets Implement λ-CalculusException 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. 112. Implementing Untyped λ-Calculus Lets Implement λ-CalculusMaybe Monad with do notation evaluate (Plus expr0 expr1) env = do val0 <- evaluate expr0 env val1 <- evaluate expr1 env return (val0 + val1) 87 / 94
  113. 113. Implementing Untyped λ-Calculus Lets Implement λ-CalculusDo notation underneath evaluate (Plus expr0 expr1) env = evaluate expr0 env >>= val0 -> evaluate expr1 env >>= val1 -> return (val0 + val1) 88 / 94
  114. 114. Implementing Untyped λ-Calculus Lets Implement λ-CalculusliftM2 evaluate (Plus expr0 expr1) env = evaluate expr0 env `plus` evaluate expr1 env where plus = liftM2 (+) 89 / 94
  115. 115. Implementing Untyped λ-Calculus Lets Implement λ-Calculus Sorry! To be continued... 90 / 94
  116. 116. Implementing Untyped λ-Calculus Lets Implement λ-CalculusPeek the final work https://github.com/godfat/sandbox/blob/master/haskell/fpug/01/la 91 / 94
  117. 117. Implementing Untyped λ-CalculusWere hiring cblue.tw/jobs 92 / 94
  118. 118. Implementing Untyped λ-CalculusFeel free to ask me questions online ▶ github.com/godfat ▶ twitter.com/godfat ▶ profiles.google.com/godfat 93 / 94
  119. 119. Implementing Untyped λ-CalculusReferences ▶ To be listed... ▶ ▶ ▶ ▶ ▶ 94 / 94

×