functional programming
play

Functional Programming March 16, 2019 Functional Programming March - PowerPoint PPT Presentation

Functional Programming March 16, 2019 Functional Programming March 16, 2019 1 / 12 Mayer Goldberg \ Ben-Gurion University Mayer Goldberg \ Ben-Gurion University Road Map Functional Programming March 16, 2019 2 / 12 Introduction to


  1. Functional Programming March 16, 2019 Functional Programming March 16, 2019 1 / 12 Mayer Goldberg \ Ben-Gurion University Mayer Goldberg \ Ben-Gurion University

  2. Road Map Functional Programming March 16, 2019 2 / 12 🗹 Introduction to Functional Programming 🗹 λ -Defjnability ☞ Tuples & RTTI ▶ Bases ▶ Fixed-Point Theory ▶ Functions as Data ▶ Streams ▶ Maps & Folds ▶ CPS & Threading Code ▶ Monads ▶ Possibly additional topics, depending on time Mayer Goldberg \ Ben-Gurion University

  3. Tuples & RTTI equivalents of pair? , null? , list? , etc using pairs to implement lists will be limited etc Functional Programming March 16, 2019 3 / 12 ▶ We implemented the ordered pair ⟨ A , B ⟩ as λ x . ( x A B ) 💤 Our implementation is limited: We cannot implement the ▶ We implemented linked lists as nested, ordered pairs, but unless we can distinguish between pairs & nil (the empty list, then ▶ We implemented numerals as λ sz . ( s · · · ( s z ) · · · ) 💤 We cannot implement the equivalents of number? , integer? , ▶ What we are lacking is run-time type information (RTTI) Mayer Goldberg \ Ben-Gurion University

  4. Tuples & RTTI as to take into account the RTTI both in the domain and the March 16, 2019 Functional Programming number that has RTTI range of the functions …etc equivalents lattice-theory as a lift: 4 / 12 ▶ Our goal is to implement RTTI, and thus enabling us to write expressions in the λ -calculus that closer resemble their LISP ▶ One trivial way to implement RTTI is known in set-theory & ▶ We can pair every object with an encoding of its type ▶ Let c 0 represent Church numerals ▶ Let c 1 represent Booleans ▶ Let c 2 represent pairs ▶ Implement a new set of functions on these listed datatypes, so 🤕 The sum of two natural numbers that have RTTI is a natural Mayer Goldberg \ Ben-Gurion University

  5. Functional Programming Tuples & RTTI March 16, 2019 5 / 12 ▶ Advantages of this approach ▶ Simplicity & ease ▶ Disadvantages of this approach ▶ Adds the “noise” of constantly having to test the RTTI: 🤕 The tuple-projection function needs to test for pair 🤕 The plus function needs to test for natural numbers Mayer Goldberg \ Ben-Gurion University

  6. Tuples & RTTI (k-pair a b)))) March 16, 2019 Functional Programming (k-num x)))) (lambda (k-nil k-pair k-num) (lambda (x) (define number (lambda (k-nil k-pair k-num) A better approach: (lambda (a b) (define kons (k-nil))) (lambda (k-nil k-pair k-num) (define nil 6 / 12 ☞ Use “double-dispatch” to call the correct “method” 🤕 This is similar to using multiple continuations Mayer Goldberg \ Ben-Gurion University

  7. Tuples & RTTI (w ( lambda () ( error 'kdr "Undefined␣for␣nil")) March 16, 2019 Functional Programming ( error 'kdr "Undefined␣for␣number"))))) ( lambda (x) ( lambda (a b) b) ( lambda (w) ( define kar ( define kdr ( error 'kar "Undefined␣for␣number"))))) ( lambda (x) ( lambda (a b) a) (w ( lambda () ( error 'kar "Undefined␣for␣nil")) ( lambda (w) 7 / 12 Mayer Goldberg \ Ben-Gurion University

  8. Tuples & RTTI (define is-null? March 16, 2019 Functional Programming (lambda (x) #t)))) (lambda (a b) #f) (w (lambda () #f) (lambda (w) (define is-number? (lambda (x) #f)))) (lambda (a b) #t) (w (lambda () #f) (lambda (w) (define is-pair? (lambda (x) #f)))) (lambda (a b) #f) (w (lambda () #t) (lambda (w) 8 / 12 Mayer Goldberg \ Ben-Gurion University

  9. Tuples & RTTI ( cond ((is-null? ks) '()) March 16, 2019 Functional Programming ( else ks)))) (kpair->pair (kdr ks)))) ( cons (kpair->pair (kar ks)) ((is-pair? ks) ((is-number? ks) (number->value ks)) ( lambda (ks) ( define pair->kpair ( define kpair->pair ( else s)))) (pair->kpair (cdr s)))) (kons (pair->kpair (car s)) ((pair? s) ((number? s) (number s)) ( cond (( null ? s) nil) ( lambda (s) 9 / 12 Mayer Goldberg \ Ben-Gurion University

  10. Tuples & RTTI (kons (number 2) March 16, 2019 Functional Programming (2 3 4) (kons (number 4) nil)))) (kons (number 3) > (kpair->pair Testing our code: #t > (is-pair? (kons (number 2) (number 3))) #f > (is-pair? (number 42)) #f > (is-pair? nil) 10 / 12 Mayer Goldberg \ Ben-Gurion University

  11. Functional Programming Tuples & RTTI March 16, 2019 11 / 12 ▶ Advantages of this approach ▶ No “noise” in the form of testing RTTI ☞ Similar to method dispatch ▶ Effjcient ☞ Constant-time dispatching ▶ Disadvantages of this approach ▶ Cumbersome to add new types ▶ Everything is a procedure 😟 We lose procedures, and procedure? 🤕 Solution: Extend the supported types to include procedures! Mayer Goldberg \ Ben-Gurion University

  12. Tuples & RTTI Summary Functional Programming March 16, 2019 12 / 12 ▶ There is more than one way to implement RTTI ▶ RTTI & Refmective operations can be made clean & effjcient ▶ Abstraction wins the day! Mayer Goldberg \ Ben-Gurion University

Recommend


More recommend