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

functional programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Functional Programming

Mayer Goldberg \ Ben-Gurion University March 16, 2019

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

slide-2
SLIDE 2

Road Map

🗹 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 Functional Programming March 16, 2019 2 / 12

slide-3
SLIDE 3

Tuples & RTTI

▶ We implemented the ordered pair ⟨A, B⟩ as λx.(x A B)

💤 Our implementation is limited: We cannot implement the

equivalents of pair?, null?, list?, etc

▶ We implemented linked lists as nested, ordered pairs, but unless

we can distinguish between pairs & nil (the empty list, then using pairs to implement lists will be limited

▶ We implemented numerals as λsz.(s · · · (s z) · · · )

💤 We cannot implement the equivalents of number?, integer?,

etc

▶ What we are lacking is run-time type information (RTTI)

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

slide-4
SLIDE 4

Tuples & RTTI

▶ Our goal is to implement RTTI, and thus enabling us to write

expressions in the λ-calculus that closer resemble their LISP equivalents

▶ One trivial way to implement RTTI is known in set-theory &

lattice-theory as a lift:

▶ We can pair every object with an encoding of its type ▶ Let c0 represent Church numerals ▶ Let c1 represent Booleans ▶ Let c2 represent pairs

…etc

▶ Implement a new set of functions on these listed datatypes, so

as to take into account the RTTI both in the domain and the range of the functions

🤕 The sum of two natural numbers that have RTTI is a natural

number that has RTTI

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

slide-5
SLIDE 5

Tuples & RTTI

▶ 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 Functional Programming March 16, 2019 5 / 12

slide-6
SLIDE 6

Tuples & RTTI

A better approach:

☞ Use “double-dispatch” to call the correct “method” 🤕 This is similar to using multiple continuations

(define nil (lambda (k-nil k-pair k-num) (k-nil))) (define kons (lambda (a b) (lambda (k-nil k-pair k-num) (k-pair a b)))) (define number (lambda (x) (lambda (k-nil k-pair k-num) (k-num x))))

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

slide-7
SLIDE 7

Tuples & RTTI

(define kar (lambda (w) (w (lambda () (error 'kar "Undefined␣for␣nil")) (lambda (a b) a) (lambda (x) (error 'kar "Undefined␣for␣number"))))) (define kdr (lambda (w) (w (lambda () (error 'kdr "Undefined␣for␣nil")) (lambda (a b) b) (lambda (x) (error 'kdr "Undefined␣for␣number")))))

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

slide-8
SLIDE 8

Tuples & RTTI

(define is-null? (lambda (w) (w (lambda () #t) (lambda (a b) #f) (lambda (x) #f)))) (define is-pair? (lambda (w) (w (lambda () #f) (lambda (a b) #t) (lambda (x) #f)))) (define is-number? (lambda (w) (w (lambda () #f) (lambda (a b) #f) (lambda (x) #t))))

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

slide-9
SLIDE 9

Tuples & RTTI

(define pair->kpair (lambda (s) (cond ((null? s) nil) ((number? s) (number s)) ((pair? s) (kons (pair->kpair (car s)) (pair->kpair (cdr s)))) (else s)))) (define kpair->pair (lambda (ks) (cond ((is-null? ks) '()) ((is-number? ks) (number->value ks)) ((is-pair? ks) (cons (kpair->pair (kar ks)) (kpair->pair (kdr ks)))) (else ks))))

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

slide-10
SLIDE 10

Tuples & RTTI

Testing our code: > (is-pair? nil) #f > (is-pair? (number 42)) #f > (is-pair? (kons (number 2) (number 3))) #t > (kpair->pair (kons (number 2) (kons (number 3) (kons (number 4) nil)))) (2 3 4)

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

slide-11
SLIDE 11

Tuples & RTTI

▶ 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 Functional Programming March 16, 2019 11 / 12

slide-12
SLIDE 12

Tuples & RTTI

Summary

▶ 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 Functional Programming March 16, 2019 12 / 12