cs784(prasad)l123assg1 assignments. cs784(prasad)l123assg2 l-value vs. r-value pascal/ada: x := x +...
TRANSCRIPT
![Page 1: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/1.jpg)
cs784(Prasad) L123Assg 1
Assignments
![Page 2: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/2.jpg)
cs784(Prasad) L123Assg 2
l-value vs. r-value
• Pascal/Ada: x := x + 1• C/Java: x = x + 1
l-value = location, address, reference, …
r-value = int, real, (sometimes even) address, …
Environment binds an identifier to a location.
env : ids locations
Store binds a location to a value.
store : locations values
assign-op : location x value x store store
![Page 3: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/3.jpg)
cs784(Prasad) L123Assg 3
SharingSharing
• For functional subset, it is sufficient to model env as a function from ids to values.
• For imperative programming that has both assignment and sharing, separating env and store is necessary.
E.g., sharing/aliasing Point p = new Point();
Point q = p;
E.g., call by reference
void f(Point p){};
f(q);
![Page 4: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/4.jpg)
cs784(Prasad) L123Assg 4
Side-effect causing Scheme primitives
• Initialize variable: (define <var> <exp>)• Update variable: (set! <var> <exp>)• Other ops: display, write,
set-car!, set-cdr!,...
(set! x y)denotes location denotes value
• Sequencing: (begin <exp1> <exp2> … <expn>)
![Page 5: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/5.jpg)
cs784(Prasad) L123Assg 5
Extending object (not meta-) language and the interpreter to support variable
assignment
![Page 6: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/6.jpg)
cs784(Prasad) L123Assg 6
Modifying environment and the interpreter
• An identifier denotes the address of a mutable data structure that holds a value (that is, it models a memory location).
• This address is called a reference, and the contents of these references are modified by a variable assignment.
• Variable reference
(var-exp (id) (deref (apply-env-ref env id)))
![Page 7: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/7.jpg)
cs784(Prasad) L123Assg 7
Introduction of variable assignment• Syntax <expression> ::= set <identifier> = <expression>
• Abstract Syntaxvarassign-exp (id rhs-exp)
• Semantics (varassign-exp (var exp)
(set-ref! (apply-env-ref env id)
(eval-expression rhs-exp env) ) )
l-value
r-value
![Page 8: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/8.jpg)
cs784(Prasad) L123Assg 8
Simulating letrec using let and assignment
(letrec ((var1 exp1) (var2 exp2)) exp) (* exp1 and exp2 are lambda-forms *)
(let ((var1 ’*) (var2 ’*)) (set! var1 exp1) (set! var2 exp2) exp)
![Page 9: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/9.jpg)
cs784(Prasad) L123Assg 9
Simulating Objects and Classes
![Page 10: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/10.jpg)
cs784(Prasad) L123Assg 10
Defining a stack object
(define empty? '())
(define push! '())
(define pop! '())
(define top '())
(let ( (stkstk '()) )
(set! empty? (lambda() (null? stkstk)))
(set! push! (lambda(x)
(set! stkstk (cons x stkstk))))
(set! pop! (lambda()
(set! stkstk (cdr stkstk))))
(set! top (lambda() (car stkstk)))
)
![Page 11: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/11.jpg)
cs784(Prasad) L123Assg 11
Using the stack object
> (empty?)
#t
> (push! 5)
> (top)
5
> (pop!)
> (empty?)
#t
• Only oneone stack object.• ScopeScope of stkstk is
limited (encapsulation), but its lifetimelifetime is not.– representation pvtpvt.– methods publicpublic.
• PersistentPersistent state.• Ops. share datashare data and
change statechange state.
![Page 12: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/12.jpg)
cs784(Prasad) L123Assg 12
stack object : Message passing style(define stack (let ( (stk '()) ) (lambda (msg) (case msg ((empty?) (lambda () (null? stk)) ) (( push!) (lambda (x) (set! stk (cons x stk))) ) (( pop! ) (lambda () (set! stk (cdr stk))) ) ((top) (lambda () (car stk)) ) (else 'error ) ) )))
![Page 13: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/13.jpg)
cs784(Prasad) L123Assg 13
Object vs. Class
> (stack 'empty?) > ((stack 'empty?)) #t
> ((stack 'push!) 5)
> ((stack 'top)) 5
> ((stack 'empty?)) () or #f
> ((stack 'pop!)) > ((stack 'empty?)) #t
› (define s1 (make-stack))› (define s2 (make-stack))› ((s1 'push!) 1)› ((s2 'push!) 2)› ( (s1 'top) ) 1› ( (s1 'top) ) 1› ( (s2 'top) ) 2
![Page 14: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/14.jpg)
cs784(Prasad) L123Assg 14
Instance/Object vs. Class/Type
(define stack
(let ((stk '()) )
(lambda (msg)
(case msg
...
)
))
)
(define make-make-stack
(lambda()(lambda()
(let ((stk '()) )
(lambda (msg)
(case msg
...
)
))
)))
![Page 15: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/15.jpg)
cs784(Prasad) L123Assg 15
stack class : Message passing style(define make-stack (lambda ()
(let ( (stk '()) )
(lambda (msg)
(case msg
((empty?) (lambda () (null? stk)) )
(( push!) (lambda (x)
(set! stk (cons x stk))) )
(( pop! ) (lambda ()
(set! stk (cdr stk))) )
((top) (lambda () (car stk)) )
(else 'error ) )))
))
![Page 16: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/16.jpg)
cs784(Prasad) L123Assg 16
Template for classclass definition(define class-name (let ((class-var val)) (lambda () (let ((inst-var val)) (lambda (msg) (case msg ((m1) code) ((m2) code) ((c3) code) (else ’error) ))) ) ) )
![Page 17: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/17.jpg)
cs784(Prasad) L123Assg 17
(define make-stack (let ((pushed 0)) (lambda () (let ((stk '()) (local-pushed 0) ) (lambda (message) (case message ((empty?) (lambda () (null? stk))) ((push!) (lambda (x) (set! pushed (+ pushed 1)) (set! local-pushed (+ local-pushed 1)) (set! stk (cons x stk)))) ((pop!) (lambda () (if (null? stk) (error "Stack: Underflow") (begin (set! pushed (- pushed 1)) (set! local-pushed (- local-pushed 1)) (set! stk (cdr stk)))))) ((top) (lambda () (if (null? stk) (error "Stack: Underflow") (car stk)))) ((local-pushed) (lambda () local-pushed)) ((pushed) (lambda () pushed)) (else (error "Stack: Invalid message" message))))) )))
![Page 18: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/18.jpg)
cs784(Prasad) L123Assg 18
make-stack
(lambda () (let (…) … ))
pushed = 0
(make-stack) (make-stack)
(lambda (msg) … )
stk = ()local-pushed = 0
(lambda (msg) … )
stk = ()local-pushed = 0
![Page 19: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/19.jpg)
cs784(Prasad) L123Assg 19
Rewrite in Java public class Stackclass { static int pushed; private Vector stk; private int localpushed; static { pushed := 0 } public Stackclass(){ localpushed = 0; stk = new Vector(); } public boolean empty() { return stk.isEmpty() }; public void push(Object x){ pushed++; localpushed++; stk.addElement(x);
} ...
![Page 20: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/20.jpg)
cs784(Prasad) L123Assg 20
... public void pop(){
if (stk.isEmpty()) throw new Exception(“Stack Empty”); else { --pushed; --localpushed;
stk.removeElementAt(stk.size()-1); }
} public Object top(){ if (empty())
throw new Exception(“Stack Empty”); else return stk.lastElement();
} public int pushed() { return pushed; } public int localpushed(){ return localpushed; }}
![Page 21: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/21.jpg)
cs784(Prasad) L123Assg 21
Approximate Environment
![Page 22: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/22.jpg)
cs784(Prasad) L123Assg 22
Rewrite in Javapublic class Test { public static void main(String [] args) { Stackclass stack1 = new Stackclass(); Stackclass stack1 = new Stackclass(); stack1.push(new Integer(7)); stack1.top(); stack2.push(new Integer(6)); stack2.push(new Integer(8)); stack2.top(); stack1.localpushed(); stack2.localpushed(); stack1.pushed(); stack2.pushed(); }}
![Page 23: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/23.jpg)
cs784(Prasad) L123Assg 23
Approximate Environment
![Page 24: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/24.jpg)
cs784(Prasad) L123Assg 24
Inheritance
ShareShare and reusereuse classes with modifications to fields and methods
Improve programmer productivityproductivity and aid code evolutionevolution
![Page 25: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/25.jpg)
cs784(Prasad) L123Assg 25
Delegation
(define make-bounded-stack (lambda (n) (let ((bound n) (stk (make-stack))) (lambda (message) (case message ((push!) (if (< ((stk 'local-pushed)) bound) (stk 'push!) (error ”Overflow”))) ((set-bound!) (lambda (x) (set! bound x))) ((set-stack!) (lambda (s) (set! stk s))) (else (stk message)) )
))))
![Page 26: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/26.jpg)
cs784(Prasad) L123Assg 26
Delegation vs Inheritance
• Code sharing by organization of objects. Delegate message handling.
• Dynamic and flexible. When and how to delegate can depend on system state.
• E.g., Java 1.1 Event Model
• Code sharing by organization of classes. Single vs multiple inheritance.
• Static but efficient. Type determines message interpretation.
• E.g., Java 1.0 Event Model
![Page 27: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/27.jpg)
cs784(Prasad) L123Assg 27
Inheritance in Java class Boundedstackclass extends Stack { private int bound; Boundedstackclass() { super(); bound = 10; } public void push(Object x) { if (size() < bound) super.push(x); else new Exception(”Overflow”); } public void setbound(int x){ bound = x; } }
• Specify only incremental changes.• Access parent’s version of a method through super.
![Page 28: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/28.jpg)
cs784(Prasad) L123Assg 28
Composition and Delegation in Javaclass Boundedstackclass { private int bound; private Stack stk; Boundedstackclass() { stk = new Stack(); bound = 10; } public void push(Object x) { if (size() < bound) stk.push(x); else new Exception(”Overflow”); } public void setbound(int x){ bound = x; } public Object top() { return stk.top(); } ... Explicit list of other delegated stack methods }
![Page 29: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/29.jpg)
cs784(Prasad) L123Assg 29
Scoping: Lexical vs Static
class A { static int cv; int iv;}
class Test { public static void main(String [] args){ int iv, cv;
class B extends A { void print() { System.out.print( “”+ cv + iv ); /*error*/
System.out.println( “”+this.cv +this.iv ); } } new B(). print(); } }
![Page 30: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/30.jpg)
cs784(Prasad) L123Assg 30
Binding: Dynamic vs Staticclass A { static int cv = 10; int iv = 70; int f() {return 40;} void print() { System.out.println( “”+ cv + iv + f());}}class B extends A { static int cv = 33 ; int iv = 88; int f() {return 55;}}class Test2 { public static void main(String [] args){ new A(). print(); new B(). print();}}
![Page 31: 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,](https://reader030.vdocuments.net/reader030/viewer/2022032802/56649e125503460f94afe381/html5/thumbnails/31.jpg)
cs784(Prasad) L123Assg 31
Advanced Topics
• Multiple Inheritance• E.g., C++, Eiffel.
• Meta-classes• E.g., Smalltalk.
• Inner/Nested classes• E.g., Java.
• Polymorphic Static Typing• E.g., ML, Haskell.