1 / 17

Imperative Programming: Mutable data ( local state , letrec, set!)

Imperative Programming: Mutable data ( local state , letrec, set!). Example 1: counter. How would this work in the substitution model?. (define counter (let ((count 0)) (lambda () (set! count (+ count 1)) count))).

reece-lucas
Download Presentation

Imperative Programming: Mutable data ( local state , letrec, set!)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Imperative Programming: Mutable data(local state, letrec, set!) Example 1: counter How would this work in the substitution model? (define counter (let ((count 0)) (lambda () (set! count (+ count 1)) count))) app-eval [ ((lambda (count) (lambda () (begin (set! count (+ count 1)) count)) 0 )] app-eval [ (lambda () (begin (set! 0 (+ 0 1)) 0)) ] app-eval [ (begin (set! 0 (+ 0 1)) 0) ] ==> 0 • The substitution model cannot account for change! change requires objects that keep local states. • The substitution step has to be strengthened to enable binding of variables (parameters) to actual • argument values.

  2. Imperative Programming: Mutable data(local state, letrec, set!) Example 1: counter (define counter (let ((count 0)) (lambda () (set! count (+ count 1)) count))) E1 E2 (lambda () (set!... count=0 (set!...count 1 GE counter P = B= (set!... count P = countB= (lambda () (set!... count • Note that count is the local state of the counter object. • Conclusion: In functional languages, objects are implemented as runtime generated closures. > (counter) ;;; How would the box diagram look like? 1 > (counter) 2

  3. Imperative Programming: Mutable data(local state, letrec, set!) Example 2: letrec • The letrec expression can be used to define local recursive procedures. • The evaluation rule for this expression is not supported by the functional programming • model (e.g, applicative-eval) without rewriting the code of the recursive procedure. • However, it can be supported by the imperative programming with the environment model. To support the evaluation of a letrec expression, it is handled as a derived expression: (let ((f1 ’unassigned) ... (fn ’unassigned)) (set! f1 lambda-exp1) ... (set! fn lambda-expn) e1...em) (letrec ((f1 lambda-exp1) ... (fn lambda-expn)) e1...em)

  4. Imperative Programming: Mutable data(local state, letrec, set!) Example 2: letrec env-eval[(letrec((fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) (fact 3)),GE]==> Assume letrec was translated to let which was translated to an application…  env-eval[ ((lambda (fact) (set! fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1))))) (fact 3)) 'uassigned), GE] ==> env-eval[ (set! fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1))))), E1] (A) GE Params: factBody: (set!... (fact 3) E1 fact='unassigned (B) (set!.. (fact 3) Params: nBody: (if… ) Remains to evaluate… (next slide)

  5. …(* n (fact (- n 1)))… Imperative Programming: Mutable data(local state, letrec, set!) Example 2: letrec ==> env-eval[ (fact 3), E1 ] env-eval[ fact, E1 ] ==> lookup-varibale-value (fact,E1) = (B) env-eval[ 3 , E1 ] ==> 3 Let E2 be the extended environment… env-eval[(* n (fact (- n 1))), E2] ==> env-eval[*,E2] ==> <primitive *> ==> env-eval [n,E2]==> 3 (lookup) ==> env-eval[(fact (- n 1)), E2] ==> env-eval[fact, E2] ==> lookup-variable-value(fact,E2): (B) … (A) GE Params: factBody: (set!... (fact 3) E1 fact='unassigned (B) E2 (set!.. (fact 3) n = 3 Params: nBody: (if… )

  6. Imperative Programming: Mutable data (local state, letrec, set!) Example 2: letrec Compare the last diagram with the diagram resulting from using let instead of letrec: Env-eval[ (letrec ((fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) (fact 3)),GE]==> Env-eval[ ( (lambda (fact) (fact 3)) (lambda (n) (if (= n 0) 1 (* n (fact (- n 1))))) ), GE] • The set statement is removed for A’s body (according to the way a let expression is derived) • Procedure B “points” to the GE! B is not defined within a closure (as earlier), but as an argument evaluated in the GE. • When evaluating B’s body, fact is unbound! (A) GE Params: factBody: (set.. (fact 3) (B) E1 (set.. (fact 3) fact Params: nBody: (if… )

  7. Imperative Programming: Mutable data (local state, letrec, set!) Example 3: A chess player object: A chess player object is represent by a dispatch procedure. Based on a given message it invokes internal procedures of the object. total and steps which keep track of the player’s points and the number of steps respectively, are the object’s local state variables. • (define make-player (lambda (total) • (letrec ((steps 0) • (get-steps (lambda () steps)) • (set-steps (lambda () (set! steps (+ steps 1)))) • (get-total (lambda () total)) • (set-total (lambda (piece) • (let ((piece-value (cond ( (eq? piece 'queen) 9) • ( (eq? piece 'rook) 5) • ( (eq? piece 'bishop) 3) • ( (eq? piece 'knight) 3) • ( (eq? piece 'pawn) 1) • ( else 0)))) • (set! total (- total piece-value))))) • (dispatch (lambda (m) (cond ((eq? m 'set-total) set-total) • ((eq? m 'set-steps) set-steps) • ((eq? m 'get-total) get-total) • ((eq? m 'get-steps) get-steps) • (else (error "Unknown request"))))) ) • dispatch))) We’ll examine a simpler representation of a chess player by using only the procedure set-total.

  8. GE make-player player1 E4 E3 piece = ‘queen m = ‘set-total (let… (cond… P=(total)B=(letrec… E2 steps = ‘unassigned get-steps = ‘unassigned…set-total = ‘unassigneddispatch = ‘unassigned 0 E1 30 total = 39 (letrec… P=B= steps P=get-steps…B= (set! get-steps (lambda() steps)) … dispatch P=pieceB=(let…) P=mB=(cond…) Imperative Programming: Mutable data (local state, letrec, set!) • (define make-player (lambda (total) • (letrec ((steps 0) • (get-steps (lambda () steps)) • … • (set-total (lambda (piece) • (let ((piece-value (cond ( (eq? piece 'queen) 9) • … • ( else 0)))) • (set! total (- total piece-value))))) • (dispatch (lambda (m) (cond ((eq? m 'set-total) set-total) • … • (else (error "Unknown request"))))) ) • dispatch))) ((player1 ‘set-total) ‘queen) (define player1 (make-player 39)) 8

  9. Imperative Programming Evaluator: Adding while expressions We would like to support a while expression with the following Syntax: (while <predicate><body>). Fore example: > (define x 0) > (define n 4) > (while (> n 0) (begin (set! x (+ x n)) (set! n (- n 1))) ) > x 10 Question1: Can a while expression be supported in a functional programming evaluator? NO! in functional programming there is no change of state. Particularly, the result of evaluating the <predicate> clause of a while loop will always remain the same. An ability to change state is required!In the (Meta-Circular Environment Based)interpreter for imperative programming, support for set! expressions is added. This gives motivation for supporting while expressions:

  10. Imperative Programming Evaluator: Adding while expressions Question2: Can while be defined as a user procedure (in the imperative model)? (define while (lambda (pred body) (if pred (begin (body) (while pred body)))) > (define x 2) > (while (> x 0) (lambda () (begin (set! x (+ x n)) (display x))) ) After the first time pred is evaluated to #t, It remains #t in every recursive call. So the answer is?... Yes! BUT, only in the normal evaluation approach. According to the normal evaluation approach, the predicate must be evaluated. (define while (lambda (pred body) (if pred (begin (body) (while pred body)))) The expression constituting pred is passed to recursive calls, as opposed to the value of the pred expression in the eager approach. 1 Endless loop… > (while (> x 0) (lambda () (begin (set! x (+ x n)) (display x)))) 1 0

  11. Imperative Programming Evaluator: Adding while expressions > (env-eval '(define x 0) the-global-env) > (env-eval '(define n 4) the-global-env) > (env-eval '(while (> n 0) (begin (set! x (+ x n)) (set! n (- n 1))) ) the-global-env) ok > (env-eval x the-global-env) 10 To support while expressions, we add the appropriate parser procedures: (define while? (lambda (exp) (tagged-list? exp 'while))) (define while-pred (lambda (exp) (car (get-content exp)))) (define while-body (lambda (exp) (cadr (get-content exp))))

  12. 3. Add the translation procedure: while->iteration-expression… (letrec ((iter (lambda () (if (> n 0) (begin (begin (set! x (+ x n)) (set! n (- n 1))) (iter)) ‘done)))) (iter)) (letrec ((iter (lambda () (if (> (iter n) 0) (begin (set! n (- n (iter n)))) (iter)) ‘done)))) (iter)) (while (> n 0) (begin (set! x (+ x n)) (set! n (- n 1)))) (while (> (iter n) 0) (set! n (- n (iter n)))) Imperative Programming Evaluator: Adding while expressions (option A) Adding while as a derived expression 1. Add to derived? (define derived? (lambda (exp) (or … (while? exp)))) 2. Add to shallow-derive (define shallow-derive (lambda (exp) (cond … ((while? exp) (while->iteration-expression exp))))) Any problem with the solution above? There might be if iter is a predefined procedure!

  13. Imperative Programming Evaluator: Adding while expressions (option A) Adding while as a derived expression 3. Add the translation procedure: while->iteration-expression… Second try: (let ( (pred (lambda () (set! n (- n (iter n))))) (body (lambda () (> (iter n) 0))) ) (letrec ((iter (lambda () ( if (pred) (begin (body) (iter)) 'ok)))) (iter))) (while (> (iter n) 0) (set! n (- n (iter n))))

  14. Imperative Programming Evaluator: Adding while expressions (option A) Adding while as a derived expression The translation procedure: (define while->iteration-expression (lambda (exp) (let ((pred (make-lambda ‘() (list (while-pred exp)))) (body (make-lambda ‘() (list (while-body exp))))) (derive (make-let (list (list ‘body body) (list ‘pred pred)) (list (make-letrec (list (list 'iter (make-lambda (list) (list (make-if (make-application ‘pred (list) ) (make-begin (list (make-application ‘body (list)) (make-application 'iter (list)))) '(quote ok)))))) (list (make-application 'iter (list)))))))))) We construct a let exp which is a derived exp itself. The body of a let exp / letrec exp may include several expressions (just as the body of a lambda exp). Therefore, the body argument for Make-let, make-letrec, is wrapped within a list.

  15. Imperative Programming Evaluator: Adding while expressions (option B)Adding while as a special form 1. Add to special-form?. (define special-form? (lambda (exp) (or (quoted? exp) (lambda? exp) (definition? exp) (if? exp) (begin? exp) (while? exp)))) 2. Add an eval-special-form procedure (define eval-special-form (lambda (exp env) (cond …((while? exp) (eval-while exp env))))) 3. Add an eval-while procedure (define eval-while (lambda (exp env) (let ( (pred (while-pred exp)) (body (while-body exp))) (letrec ( (iter (lambda () (if (env-evalpredenv) (begin (env-eval body env) (iter)) 'ok)))) (iter))))

  16. Imperative Programming Evaluator: Adding while expressions (option B)Adding while as a special form Adding while to the compiler as a special form: 1. Add to analyze-special-form (define analyze-special-form (lambda (exp) (cond …((while? exp) (analyze-while exp))))) 2. Add an analysis procedure: (define analyze-while (lambda (exp) (let ( (pred (analyze (while-pred exp))) (body  (analyze (while-body exp)))) (lambda (env) (letrec ((iter (lambda() (if (predenv) (begin (body env) (iter)) 'ok)))) (iter))))) • The eval-while code has been translated to an analyzer code according to two simple rules: • Recursive application of analyze to the components of the expression. • Currying to produce a procedure that is ready for execution once an environment is given.

  17. Imperative Programming Evaluator: Adding while expressions Not every evaluation procedure can be adjusted to the analyzer by the rules of Recursive application of analyze to the expression components and currying. The following code will correctly evaluate while expressions but cannot be adjusted to the analyzer by the rules noted above: (define eval-while (lambda (exp env) (let ( (pred (while-pred exp)) (body (while-body exp))) (if (env-evalpredenv) (begin (env-eval body env) (eval-while exp env)) 'ok)))) This code cannot be translated to an analyzer code by the above two rules: It involves a recursive call to eval-while which is performed during runtime. Analysis is performed during static time.

More Related