Imperative Programming Chapter 6 1. Local State Real world software like: Banks Course grading system Are state systems. i.e. they change along time:

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Imperative Programming: Mutable data (local state, letrec, set!) Example 1: counter (define counter (let ((count 0)) (lambda () (set! count (+ count 1))
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Evaluators for Functional Programming Chapter 4 1 Chapter 4 - Evaluators for Functional Programming.
Imperative Programming: Mutable data (local state, letrec, set!) Example 1: counter (define counter (let ((count 0)) (lambda () (set! count (+ count 1))
Imperative Programming. Back to scheme Scheme is a functional language In some cases there is a need to capture objects state E.g. bank account.
מבוא מורחב למדעי המחשב תרגול 13 Lazy Evaluation. Lazy Evaluation Implementation Two types of values – Delayed Values – Actual Values Introduce two functions.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
PPL Pairs, lists and data abstraction. Compound Data Until now: atomic, unrelated entities Now: organized into structures Why? – Better conceptual level.
Cs784(Prasad)L123Assg1 Assignments. cs784(Prasad)L123Assg2 l-value vs. r-value Pascal/Ada: x := x + 1 C/Java: x = x + 1 l-value = location, address, reference,
1 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure + symbol.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part I: Concepts and Scheme Programming Languages: Principles and Practice, 2nd Ed. Kenneth.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 Subt. model interpreter: Structure of the interpreter ASP Derived expressions Core Test Data structures Utils A racket lib Proc / Primitive-Proc. Global.
The environment-based operational semantics Chapter
1 Subt. model interpreter: Introduction Abstract Syntax Parser Abstract Syntax Parser Derived Expressions Core Test Data Structures Utils (a racket lib)
Example 1: counter (set! vs set-box!) 1 Imperative Programming: Mutable data and local state (define counter (let ((count 0)) (lambda () (set! count (+
Example 1: counter (set! vs set-box!) 1 Imperative Programming: Mutable data and local state (define counter (let ((count 0)) (lambda () (set! count (+
Fall 2008Programming Development Techniques 1 Topic 16 Assignment and Local State Section 3.1.
Imperative Programming Chapter 6 1. Local State Real world software like: Banks Course grading system Are state systems. i.e. they change along time:
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Evaluators for Functional Programming 1. How to describe (specify) a programming language? 1.Syntax: atoms, primitives, combination and abstraction means.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Chapter 11 - Functional Programming, Part I: Concepts and Scheme
Edited by Original material by Eric Grimson
6.001 SICP Object Oriented Programming
The interpreter.
Introduction to Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Env. Model Implementation
Original material by Eric Grimson
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
Lecture 14: The environment model (cont
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
topics interpreters meta-linguistic abstraction eval and apply
Principles of Programming Languages
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Imperative Programming Chapter 6 1

Local State Real world software like: Banks Course grading system Are state systems. i.e. they change along time: for example: current balance and current grade. 2

What is Imperative Programming? “In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state (Wikipedia)” Not better the FP! Just different 3

The Imperative Programming Model Imperative programming is characterized by: – Understanding variables as storage places (addresses) – Mutation operations – Operational semantics 4

Mutation in Racket Variable assignment: set! Box type Mutable data types (we will not use Racket’s built in, but create our own) 5

Variable Assignment Modification of value of a defined variable > (define my-list (list 1 2 3)) > my-list ’(1 2 3) > (set! my-list (list 1 2 4)) > my-list ’(1 2 4) > (set! a 4).. set!: assignment disallowed; cannot set variable before its definition 6

The Box Type Values are references (or pointers) for values that can be changed during computation. Constructor: box Selector: unbox Mutator: set-box! 7

Box Examples (1) (define a (box 7)) > (* 6 (unbox a)) 42 > a '#&7 > (set-box! a (+ (unbox a) 3)) > (* 6 (unbox a)) 60 8

Box Example (2): > (define circ-l (list (box 1) (box 2) (box 3))) > circ-l '(#&1 #&2 #&3) > (unbox (car circ-l)) 1 > (unbox (cadr circ-l)) 2 > (unbox (caddr circ-l)) 3 > (set-box! (caddr circ-l) circ-l) > circ-l #0='(#&1 #&2 #&#0#) > (unbox (caddr circ-l)) #0='(#&1 #&2 #&#0#) > (eq? circ-l (unbox (caddr circ-l))) #t 9

The Box type The Box type: Type constructor: BOX(T ) proceduretype name Value constructor: box [T → BOX(T )] Selector: unbox [BOX(T ) → T] Mutator: set-box! [BOX(T ) → Void] Identifier: box? [T → Boolean] Value equality: equal? [T1 ∗ T2 → Boolean] Identity equality: eq? [T1 ∗ T2 → Boolean] 10

Side Effects Side effects are "non-functional" There existence does not agree with the FP paradigm set! and set-box! are side effects procedure We have already seen two kinds of side effect procedures: define and the display procedure. Side effects procedures are applied for the sake of their side effects, and not for their returned value, void In imperative programming, it is meaningful to use sequences of expressions 11

State based modeling Defining objects with a modifiable private fields (local variables), and a set of accessors (selectors and mutators) that have controlled access to the object's private fields. All objects may have access to shared (static) variables. 12

Bank Account Withdrawal Example ;; Signature: withdraw(amount) ;; Purpose: Withdraw ’amount’ from a private ’balance’. ;; Type: [Number -> Number] ;; Post-condition: result = if >= amount ;; then - amount ;; else balance (define withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) (begin (display "Insufficient funds") (newline) balance))))) 13

> withdraw # > (withdraw 25) 75 > (withdraw 25) 50 > (withdraw 60) Insufficient funds 50 > (withdraw 15) 35 (define withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) (begin (display "Insufficient funds") (newline) balance))))) 14

Full Bank Account Example ;; Signature: make-account(balance) ;; Type: [Number -> [Symbol -> [Number -> Number]]] (define make-account (λ (balance) (let ((balance balance)) (letrec ((get-balance (λ (amount) balance)) (set-balance! (λ (amount) (set! balance amount))) (withdraw (λ (amount) (if (>= balance amount) (begin (set-balance! (- balance amount)) balance) (begin (display "Insufficient funds") (newline) balance)))) (deposit (λ (amount) (set-balance! (+ balance amount)) balance)) (dispatch (λ (m) (cond ((eq? m ’balance) get-balance) ((eq? m ’withdraw) withdraw) ((eq? m ’deposit) deposit) (else (error ’make-account "Unknown request: ~s" m)))))) dispatch)))) 15

> (define acc (make-account 100)) > acc # > ((acc ’withdraw) 50) 50 > ((acc ’withdraw) 60) Insufficient funds 50 > ((acc ’withdraw) 30) 20 > ((acc ’withdraw) 60) Insufficient funds 20 > (define acc2 (make-account 100)) > ((acc2 ’withdraw) 30) 70 > ((acc ’withdraw) 30) Insufficient funds 20 16

Mutable ADTs : Pair ; Type: [T1*T2 -> [Symbol -> Procedure]] ; The dispatch procedure returns procedures with different arities ; (Currying could help here). (define mpair (λ (x y) (letrec ((get-x (λ () x)) (get-y (λ () y)) (set-x! (λ (v) (set! x v))) (set-y! (λ (v) (set! y v))) (dispatch (lambda (m) (cond ((eq? m 'car) get-x) ((eq? m 'cdr) get-y) ((eq? m 'set-car!) set-x!) ((eq? m 'set-cdr!) set-y!) (else (error 'mpair "Undefined operation ~s" m)))))) dispatch))) 17

Mutable ADTs : Pair (define mutable-car (λ (z) ((z ’car)))) (define mutable-cdr (λ (z) ((z ’cdr)))) (define set-car! (lambda (z new-value) ((z ’set-car!) new-value))) (define set-cdr! (lambda (z new-value) ((z ’set-cdr!) new-value))) 18

Mutable ADTs : Pair > (define p1 (mpair 1 2)) > (mutable-car p1) 1 > (mutable-cdr p1) 2 > (set-car! p1 3) > (mutable-car p1) 3 > (set-cdr! p1 4) > (mutable-cdr p1) 4 > (mutable-cdr (set-cdr! p1 5)).. procedure application: expected procedure, given: # ; arguments were: ’cdr 19

Lazy Pair (define lazy-mpair (λ (x y) (let ((x (box x)) (y (box y))) (λ (sel) (sel x y))))) (define lazy-mcar (λ (z) (z (λ (x y) (unbox x))))) (define lazy-mcdr (λ (z) (z (λ (x y) (unbox y))))) (define lazy-set-car! (λ (z new-value) (z (λ (x y) (set-box! x new-value))))) (define lazy-set-cdr! (λ (z new-value) (z (λ (x y) (set-box! y new-value))))) 20

Lazy Pair > (define p2 (lazy-mpair 1 2)) > (lazy-mcar p2) 1 > (lazy-mcdr p2) 2 > (lazy-set-car! p2 3) > (lazy-mcar p2) 3 21

Value Equality vs. Identity Equality Since we have states, we can check state equality or identity. equal? – State (value) equality eq? – Object equality 22

Value Equality vs. Identity Equality > (define x (list (box 'a) (box 'b))) > (define y (list (car x) (box 'c) (box 'd))) > (define z (list (box 'a) (box 'c) (box 'd)) ) > (define w y) > (eq? y w); identity #t > (eq? y z) #f > (equal? y z) ; value equality #t > (equal? (car z) (car x)) #t > (eq? (car z) (car x)) #f 23

Evaluation of letrec Recall the problem we had: (define fact (lambda (n) (let ((iter (lambda (c p) (if (= c 0) p (iter (- c 1) (* c p)))))) (iter n 1)))) 24 Recursion call will not work

Evaluation of letrec Here’s a solution using imperative programming: (define fact (lambda (n) (let ((iter ‘unassigned)) (set! iter (lambda (c p) (if (= c 0) p (iter (- c 1) (* c p))))) (iter n 1)))) 25 Now iter is in the scope

Evaluation of letrec Now we unveil the secret of letrec … Its semantics cannot be explained in FP (let ((f1 ’unassigned)... (fn ’unassigned)) (set! f1 lambda-exp1)... (set! fn lambda-expn) e1... em)) 26

Extending the Environment Model The substitution model cannot support mutation Semantics of set-binding!(x,val, ) : 27

Extending the Environment Model Add set! Special operator: env−eval[(set! x e),env] = set−binding!(x,env−eval[e,env],env) Add Box type 28

Extending the Interpreter & Analyzer: Data Structures (define set-binding-in-env! (lambda (env var val) (letrec ((defined-in-env (lambda (var env) (if (empty-env? env) env (let ((b (get-value-of-variable (first-frame env) var))) (if (eq? b '_not-found) (defined-in-env var (enclosing-env env)) (first-boxed-frame env))))))) (let ((boxed-frame (defined-in-env var env))) (if (empty? boxed-frame) (error 'set! "variable not found") (let ((frame (unbox boxed-frame))) (set-box! boxed-frame (change-frame (make-binding var val) frame)))))))) 29 Recursive call Creates a new frame by adding a new binding

Extending the Interpreter & Analyzer: Data Structures (define change-frame (lambda (binding frame) (let ((bvar (binding-variable binding)) (bval (binding-value binding))) (change-sub frame bvar bval)))) (define change-sub (lambda (sub var value) (let ((vars (get-variables sub)) (values (get-values sub))) (if (member var vars) (make-sub (cons var vars) (cons value values)) (error 'change-sub "substitution is not defined on variable"))))) 30

Extending the Interpreter & Analyzer: Data Structures (define make-the-global-environment (lambda () (let* ((primitive-procedures (list (list 'car car) (list 'cdr cdr)... (list 'box box) (list 'unbox unbox) (list 'box? box?) (list 'set-box! set-box!)... 31

Extending the Interpreter & Analyzer: ASP (define letrec->let (lambda (exp) (letrec ((make-body (lambda (vars vals) (if (null? vars) (letrec-body exp) (make-sequence (make-assignment (car vars) (car vals)) (make-body (cdr vars) (cdr vals)))))) (make-bindings (lambda (vars) (map (lambda (var) (list var ’unassigned)) vars)))) (let* ((vars (letrec-variables exp)) (vals (letrec-initial-values exp)) ) (make-let (make-bindings vars) (make-body vars vals)))))) (define make-sequence (lambda (exp1 exp2) (cons exp1 exp2))) (define make-assignment (lambda (variable value) (attach-tag (list variable value) 'set!))) 32 Recursive call

Extending the Interpreter: env-eval (define eval-special-form (lambda (exp env) (cond... ((assignment? exp) (eval-assignment exp env))))) (define eval-assignment (lambda (exp env) (set-binding-in-env! env (assignment-variable exp) (env-eval (assignment-value exp) env)) 'ok)) 33

Extending the Analyzer (define analyze-assignment (lambda (exp) (let ((var (assignment-variable exp)) (val (analyze (assignment-value exp)))) (lambda (env) (set-binding-in-env! env var (val env)) ’ok)))) 34

Type Checking (set! x e) is well typed if e is well typed Typing rule for set! : For every: type assignment TA, expression e, and type expression S: If Tenv |- e:S, Tenv |- x:S, Then Tenv |- (set! x e):Void 35

Type Checking Axiom for box For every type environment Tenv and type expression S: Tenv |- box:[S -> BOX(S)] Tenv |- unbox:[BOX(S) -> S] Tenv |- set-box!:[BOX(S)*S -> Void] Tenv |- box?:[S -> Boolean] Tenv |- equal?:[BOX(S)*BOX(S) -> Boolean] Tenv |- eq?:[BOX(S)*BOX(S) -> Boolean] 36

Course summary 1/3 General concepts: semantics, types syntax, CFG(BNF), operational semantics, static analysis, renaming, substitution, interpretation(evaluation) polymorphic types, type safety 37

Course summary 2/3 functional programming model – function definition and application – special forms – derived expressions, ASP – apply-procedure, environments, analysis – high-order procedures – variable binding, lexical scoping (dynamic scoping) – static type inference – pattern matching 38

Course summary 3/3 logic programming model – axioms and queries – proof trees, unification, pruning imperative programming – mutable data types – state based execution of assignments – local state encapsulation Techniques: CPS, ADTs, Currying, Sequences, lazy lists 39

Thank you for listening. Good luck on your exams. 40

41