cs 251 fall 2019 cs 251 fall 2019 principles of
play

CS 251 Fall 2019 CS 251 Fall 2019 Principles of Programming - PowerPoint PPT Presentation

CS 251 Fall 2019 CS 251 Fall 2019 Principles of Programming Languages Principles of Programming Languages Ben Wood Ben Wood Immutability and Referential Transparency https://cs.wellesley.edu/~cs251/f19/ 1 Immutability Topics


  1. λ λ CS 251 Fall 2019 CS 251 Fall 2019 Principles of Programming Languages Principles of Programming Languages Ben Wood Ben Wood Immutability and Referential Transparency https://cs.wellesley.edu/~cs251/f19/ 1 Immutability

  2. Topics • Mutation is unnecessary. • Immutability offers referential transparency. • Mutation complicates aliasing. • Broader design considerations Immutability 2

  3. Is immutability an obstacle or a tool? – Programming experience in 251 and previously – Readings about language implementation – Efficiency in space and time – Reliability – Maintainability – Ease of making/avoiding mistakes – Clarity – … Immutability 3

  4. Mutation is unnecessary. Patterns for accumulating results without mutation : – Build recursively – Create fresh copy with changes – Explicitly thread state through (e.g., fold): • Function does one step, from arguments to result. • HOF passes result on to the next step. Immutability 4

  5. Immutability offers referential transparency (define (sort-pair p) (if (< (car p) (cdr p)) p (cons (cdr p) (car p)))) (define (sort-pair p) (if (< (car p) (cdr p)) (cons (car p) (cdr p)) (cons (cdr p) (car p)))) Cons cells are immutable. Cannot tell if you copy or alias. Immutability 5

  6. Consider mutation Mutable cons cell (define x (mcons 3 4)) x (define y (sort-mpair x)) 3 4 ? y ; mutate car of x to hold 5 ? (set-mcdr! x 5) 3 4 (define z (mcdr y)) What is z ? Immutability 6

  7. append (define (append xs ys) (if (null? xs) ys (cons (car xs) (append (cdr xs) ys)))) (define x (list 2 4) (define y (list 5 3 0)) (define z (append x y)) 2 4 x 5 3 0 y 2 4 z or ? 2 4 x y 5 3 0 2 4 5 3 0 z Immutability 7

  8. Java security nightmare class ProtectedResource { private Resource theResource = ...; private String[] allowedUsers = ...; public String[] getAllowedUsers() { return allowedUsers; } public String currentUser() { ... } public void useTheResource() { for (int i = 0; i < allowedUsers.length; i++) { if (currentUser().equals(allowedUsers[i])) { ... // access allowed: use it return; } } throw new IllegalAccessException(); } } Immutability 8

  9. Mutant users! The problem: p.getAllowedUsers()[0] = p.currentUser(); p.useTheResource(); The fix: public String[] getAllowedUsers() { … return a copy of allowedUsers … } Could this happen without mutability? Immutability 9

  10. A biasing on aliasing Immutabili Im lity Aliasing do does s not affect correctness, just performance. cannot break your code, regardless of aliasing. Other code ca cannot break other code. Changing your aliasing ca Document what, no not how. Saf Safe by defau ault lt, optimize for performan ance. Mu Mutability Aliasing do does affect both correctness and performance. Other code ca can break your code, depending on your aliasing. Changing your aliasing ca can break other code. Document what an and how. Uns Unsafe by y defaul ult, optimize for performanc nce and nd safety. All the more important for parallelism and concurrency… Immutability 10

  11. What must we inspect to Wh Identify dependences between ________. Racket: immutable Ra le natural l recursion (define (fib n) recursiv re ive (if (< n 2) ca calls n (+ (fib (- n 1)) (fib (- n 2))))) Ra Racket: immutable le tail l recursion (define (fib n) (define (fib-tail n fibi fibi+1) L (if (= 0 n) a s fibi t (fib-tail (- n 1) fibi+1 (+ fibi fibi+1)))) W (fib n 0 1)) e e e def fib(n): Py Python: loop iteration with mutation h k t e b fib_i = 0 y a m fib_i_plus_1 = 1 m loop lo d n a A r for i in range(n): g o r p fib_i_prev = fib_i iterat it ratio ions e l o fib_i = fib_i_plus_1 h w fib_i_plus_1 = fib_i_prev + fib_i_plus_1 return fib_i Tail Recursion 11

  12. A broader PL design theme Design choices matter. Less can be more (reliable). Immutability + recursion (vs. mutability + loops) are central: – Limiting ho how programs can be expressed (a.k.a., not giving programmers unmitigated access – Making elements more transparent/explicit to dangerous volatile weapons) (a.k.a., not further obscuring subtle/tricky program elements This style of design choice often supports: through layers of implicitness) – Simple reasoning – Strong default guarantees – Automated optimization opportunities what computable functions can be It does no not t mean limiting wh implemented, just ho how . Immutability 12

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend