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

cs 251 fall 2019 cs 251 fall 2019 principles of
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 251 Fall 2019 Principles of Programming Languages

Ben Wood

λ

CS 251 Fall 2019

Principles of Programming Languages

Ben Wood

λ

https://cs.wellesley.edu/~cs251/f19/

Immutability

and Referential Transparency

Immutability 1

slide-2
SLIDE 2

Topics

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

Immutability 2

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 6

Consider mutation

(define x (mcons 3 4)) (define y (sort-mpair x)) ; mutate car of x to hold 5 (set-mcdr! x 5) (define z (mcdr y))

x

3 4

y

3 4

? ?

What is z?

Mutable cons cell

Immutability 6

slide-7
SLIDE 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))

x y z 2 4 5 3 2 4 x y z 2 4 5 3 2 4 5 3

  • r

?

Immutability 7

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 10

A biasing on aliasing

Im Immutabili lity

Aliasing do does s not affect correctness, just performance.

Other code ca cannot break your code, regardless of aliasing. Changing your aliasing ca cannot break other code.

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

slide-11
SLIDE 11

Identify dependences between ________.

Tail Recursion 11

(define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (define (fib n) (define (fib-tail n fibi fibi+1) (if (= 0 n) fibi (fib-tail (- n 1) fibi+1 (+ fibi fibi+1)))) (fib n 0 1)) def fib(n): fib_i = 0 fib_i_plus_1 = 1 for i in range(n): fib_i_prev = fib_i fib_i = fib_i_plus_1 fib_i_plus_1 = fib_i_prev + fib_i_plus_1 return fib_i Py Python: loop iteration with mutation Ra Racket: immutable le tail l recursion Ra Racket: immutable le natural l recursion

re recursiv ive ca calls lo loop it iterat ratio ions

Wh What must we inspect to

L a s t W e e k A n d m a y b e t h e w h

  • l

e p r

  • g

r a m

slide-12
SLIDE 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 – Making elements more transparent/explicit

This style of design choice often supports:

– Simple reasoning – Strong default guarantees – Automated optimization opportunities

It does no not t mean limiting wh what computable functions can be implemented, just ho how.

Immutability 12

(a.k.a., not giving programmers unmitigated access to dangerous volatile weapons) (a.k.a., not further obscuring subtle/tricky program elements through layers of implicitness)