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,

Slides:



Advertisements
Similar presentations
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
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.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Variables Six properties: Binding times of properties:
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
OOP Languages: Java vs C++
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Chapter 3 Introduction to Collections – Stacks Modified
By Neng-Fa Zhou1 Evolution of programming languages –Machine language –Assembly language –Sub-routines and loop (Fortran) –Procedures and recursion (Algol,
ISBN Chapter 15 Functional Programming Languages.
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
Peyman Dodangeh Sharif University of Technology Spring 2014.
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Abstract Syntax cs7100 (Prasad) L7AST.
Type Checking and Type Inference
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
6.001 SICP Compilation Context: special purpose vs. universal machines
Introduction to Scheme
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Implementing Recursion
Evolution of programming languages
The Metacircular Evaluator
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 16: Tables and OOP
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Abstract Syntax cs7100 (Prasad) L7AST.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
F II 3. Classes and Objects Objectives
6.001 SICP Variations on a Scheme
Assignments cs7100(Prasad) L13Assg.
Programming Languages and Paradigms
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
Corresponds with Chapter 5
A type is a collection of values
Presentation transcript:

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, … 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

cs784(Prasad)L123Assg3 Sharing 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);

cs784(Prasad)L123Assg4 Side-effect causing Scheme primitives Initialize variable: (define ) Update variable: (set! ) Other ops: display, write, set-car!, set-cdr!,... (set! x y) denotes location denotes value Sequencing: (begin … )

cs784(Prasad)L123Assg5 Extending object (not meta-) language and the interpreter to support variable assignment

cs784(Prasad)L123Assg6 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)))

cs784(Prasad)L123Assg7 Introduction of variable assignment Syntax ::= set = Abstract Syntax varassign-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

cs784(Prasad)L123Assg8 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 )

cs784(Prasad)L123Assg9 Simulating Objects and Classes

cs784(Prasad)L123Assg10 Defining a stack object (define empty? ' ()) (define push! ' ()) (define pop! ' ()) (define top ' ()) stk (let ( (stk ' ()) ) stk (set! empty? (lambda() (null? stk))) (set! push! (lambda(x) stkstk (set! stk (cons x stk)))) (set! pop! (lambda() stkstk (set! stk (cdr stk)))) stk (set! top (lambda() (car stk))) )

cs784(Prasad)L123Assg11 Using the stack object > (empty?) #t > (push! 5) > (top) 5 > (pop!) > (empty?) #t oneOnly one stack object. Scope stk lifetimeScope of stk is limited ( encapsulation ), but its lifetime is not. pvt –representation pvt. public –methods public. PersistentPersistent state. share data change stateOps. share data and change state.

cs784(Prasad)L123Assg12 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 ) ) )))

cs784(Prasad)L123Assg13 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 1 ›( (s2 ' top) ) 2

cs784(Prasad)L123Assg14 Instance/Object vs. Class/Type (define stack (let ((stk ' ()) ) (lambda (msg) (case msg... ) )) ) make- (define make-stack (lambda() (let ((stk ' ()) ) (lambda (msg) (case msg... ) )) ) ))) ))

cs784(Prasad)L123Assg15 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 ) ))) ) )

cs784(Prasad)L123Assg16 class Template for class 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) )) ) )

cs784(Prasad)L123Assg17 (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))))) )))

cs784(Prasad)L123Assg18 make-stack (lambda () (let (…) … ) pushed = 0 (make-stack) (lambda (msg) … ) stk = () local-pushed = 0 (lambda (msg) … ) stk = () local-pushed = 0

cs784(Prasad)L123Assg19 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); }...

cs784(Prasad)L123Assg20... 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; } }

cs784(Prasad)L123Assg21 Approximate Environment

cs784(Prasad)L123Assg22 Rewrite in Java public class Test { public static void main(String [] args) { 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(); }

cs784(Prasad)L123Assg23 Approximate Environment

cs784(Prasad)L123Assg24 Inheritance Sharereuse Share and reuse classes with modifications to fields and methods productivity evolution Improve programmer productivity and aid code evolution

cs784(Prasad)L123Assg25 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)) ) ))))

cs784(Prasad)L123Assg26 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

cs784(Prasad)L123Assg27 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.

cs784(Prasad)L123Assg28 Composition and Delegation in Java class 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 }

cs784(Prasad)L123Assg29 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(); }

cs784(Prasad)L123Assg30 Binding: Dynamic vs Static class 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(); }}

cs784(Prasad)L123Assg31 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.