代写代考 CSSE 304 Day 29

CSSE 304 Day 29
Student Questions

Copyright By PowCoder代写 加微信 powcoder

Add set! to the interpreted language

If there is time, call/cc warm-up

Interlude:
The woodpecker may have to go!

Hide this slide when making PFD for students, then unhide for class

A woman and a man are involved in a car accident. Both of their cars are totally demolished but amazingly neither of them is hurt.
 
After they crawl out of their cars, the woman says, “Wow, just look at our cars! There’s nothing left, but fortunately we are unhurt. This must be a sign that we should meet and be friends.”
 
The excited man replied, “I agree with you completely. This must be a sign!”
 

Interlude Part 2
 
The woman continues, “And look at this, here’s another miracle…my car is completely demolished, but this bottle of wine didn’t break. Surely God wants us to drink this wine and celebrate our good fortune.”
 
Then she hands the bottle to the man.

The man nods his head in agreement, opens it, drinks half the bottle and then hands it back to the woman.
 

Interlude Part 3
 
The woman takes the bottle, immediately puts the cap back on, and hands it back to the man.
 
The man asks, “Aren’t you having any?”

She replies, “No. I think I’ll just let you hold the bottle until the police arrive.”

Add assignment to the interpreted language
We need to modify our environment ADT again

Recap: Binding vs. Assignment

A binding creates a new name and associated value.
In Scheme, accomplished by define, let, letrec, or application of a closure
An assignment changes the value of a variable an existing binding.
set!, or top-level define of an already-defined variable.

Add set! to the interpreter
r-values vs l-values
x = x + 1;
We need a way of changing the value of a bound variable
Why won’t the current setup allow this to happen?
How can we fix this?

Add set! to the interpreter
ADT approach: Add a new environment observer:
(apply-env-ref env sym) returns a reference to the variable in question.
(deref ref) gets the value stored in the location that is referenced by ref.
(set-ref! ref value) changes the value stored there
If we have apply-env-ref, then apply-env does not have to be a basic operation of the environment datatype:

(define apply-env
(lambda (env sym)
(deref (apply-env-ref env sym)))

but it may be more efficient to implement apply-env directly (in a representation-dependent way).

Implementing set!
Once we have references, it is easy.
A new clause for eval-exp’s cases:
[set!-exp (id exp)
(apply-env-ref env id)
(eval-exp exp env))]

Now, we only have to implement references.

Implement apply-env-ref, deref, set-ref!, extend-env
Use a cell abstract data type.
(cell value) creates a cell containing the value.
(cell-ref cell) gives us the value in the cell.
(cell-set! cell value) replaces the value in the cell.
(cell? obj) asks if an object is a cell.

Use cells to implement references:

In the extend-env implementation, replace
vals with (map cell vals)

apply-env-ref: same code as old apply-env, but now it returns a cell.
deref: cell-ref
set-ref!: cell-set!
apply-env (two slides earlier)

A summary of the ADTs that we have so far:
Environment
(empty-env)
(extend-env vars vals env)
(apply-env env id)
(apply-env-ref env id)

(deref ref)
(set-ref! ref val)

(cell val)
(cell-ref cell)
(set-cell! cell val)
(cell? obj)

We use references to implement the new environment interface.
We use cells to implement references within an environment.
Now we need to implement cells.
representation? code?

Possible Implementations: cell ADT
Use define-datatype.
Use a pair for each cell and just ignore the cdr.
Use ‘s box data type .
See the Users Guide
Cell ADT Box implementation
(cell value) (box value)
(cell? obj) (box? obj)
(cell-ref cell) (unbox cell)
(cell-set! cell value) (set-box! cell value)

A 17: You will add set! to your interpreter

Define-datatype doesn’t work well : (not mutable)

(define cell (lambda (val) (cons val ‘this-is-a-cell)))
(define cell-ref car)
(define cell-set! set-cdr!)
(define cell? (lambda (c) (and (pair? c) (eq? (cdr c) ‘this-is-a-cell))))))

Can we do mutation in other ways?
ribcage with vectors environment implementation
A reference is a datatype whose fields are a vector and an index. I recommend that you investigate this one.
deref is implemented using vector-ref
set-ref! is implemented using vector-set!

Give students a few minutes to do this one

Can we do mutation in other ways?
More like a “normal” memory model . . .
All data is stored in a single vector of values
A reference is an index into that vector
(representing a memory address)
Garbage Collection?

Give students a few minutes to do this one

Top-level define
Note that define and set! do very different things
dynamic global environment vs static local environments
Should local environments extend the global environment?
If you do it, be careful!

call/cc warm-up
If there is time, go to call/cc warmup, on separate slides.

/docProps/thumbnail.jpeg

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com