1 Structure and Interpretation of Computer Programs Modularity, Objects, and State (part 1) Abelson & Sussman 2 UC San Diego CSE 294 May 11, 2011 Barry.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Advertisements

Programming Languages and Paradigms
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Road Map Introduction to object oriented programming. Classes
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Runtime Environments Source language issues Storage organization
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Comparison of OO Programming Languages © Jason Voegele, 2003.
1 Chapter 11 Abstract Data Types and Encapsulation Constructs.
Eclipse – making OOP Easy
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie.
CS 403 – Programming Languages Class 25 November 28, 2000.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Inheritance in the Java programming language J. W. Rider.
Introduction to Object Oriented Programming CMSC 331.
Programming Languages and Paradigms Imperative Programming.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Copyright 2005, The Ohio State University CSE – Introduction to C++ Name: Shirish Tatikonda Time: T 3:30 PM Room: BE 394
OOP Basics Classes & Methods (c) IDMS/SQL News
CSE 341 Section 10 Subtyping, Review, and The Future.
COP 4331 – OOD&P Lecture 7 Object Concepts. What is an Object Programming language definition: An instance of a class Design perspective is different.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ISBN Chapter 12 Support for Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
CS314 – Section 5 Recitation 9
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
Principles of programming languages 4: Parameter passing, Scope rules
11.1 The Concept of Abstraction
Object Oriented Programming in Java
Programming Language Concepts (CIS 635)
Object Oriented Analysis and Design
Important Concepts from Clojure
Important Concepts from Clojure
Support for Object-Oriented Programming in Ada 95
FP Foundations, Scheme In Text: Chapter 14.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Object-Oriented Programming
Important Concepts from Clojure
Lecture 10 Concepts of Programming Languages
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

1 Structure and Interpretation of Computer Programs Modularity, Objects, and State (part 1) Abelson & Sussman 2 UC San Diego CSE 294 May 11, 2011 Barry Demchak

Agenda Functional vs Imperative languages Functional vs Imperative languages OOP in Functional languages (e.g., Clojure) Data Encapsulation case studies Simple mutability (persistence) A simple object Mutability support Failings of the substitution model The environment model Method dispatch Language comparisons 2

Functional vs Imperative (1/2) functional programming Programming without any use of assignments … is … known as functional programming. p230 Looping via recursion Higher order programming Repeatable function execution  memoization Fewer special forms imperative programming Programming that makes extensive use of assignment is … imperative programming. p234 Explicit control structures and special forms Introduces significant data dependency 3

Functional vs Imperative (2/2) Java/C++/Pascal int factAnswer = 1; for (int counter = 2; counter <= n; counter++) factAnswer = factAnswer * counter; Maps directly to registers and compare/jump Lisp/Clojure (defn factorial [n] (if (<= n 1) 1 (* n (factorial (dec n))))) Maps to recurrence relation 4

Agenda Functional vs Imperative languages OOP in Functional languages (e.g., Clojure) OOP in Functional languages (e.g., Clojure) Data Encapsulation case studies Simple mutability (persistence) A simple object Mutability support Failings of the substitution model The environment model Method dispatch Language comparisons 5

Object Oriented Programming Major properties Abstraction Inheritance Polymorphism Data encapsulation Major uses Manage complexity Multiple instances of stateful entities Orchestration of stateful entities Key point: Stateful entities change over time Key point: Stateful entities change over time 6

Functional Programming vs OOP Contradiction Repeatable execution requires immutability Substitution model (defn factorial [n] (if (<= n 1) 1 (* n (factorial (dec n))))) (factorial 3) (* 3 (factorial 2)) (* 3 (* 2 (factorial 1))) (* 3 (* 2 (* 1 1))) OOP depends on mutability 7

Agenda Functional vs Imperative languages OOP in Functional languages (e.g., Clojure) Data Encapsulation case studies Data Encapsulation case studies Simple mutability (persistence) A simple object Mutability support Failings of the substitution model The environment model Method dispatch Language comparisons 8

Bringing OOP to Functional Abstraction (as seen by Valentin) Inheritance Polymorphism Data Encapsulation …implies mutable state Suppose a min() function (defn min [x y] (if (<= x y) x y)) where (min 5 6) -> 5 (min 5 6) -> 5 … Define last-min() to return last min() calculated 9

Simple Persistence Global persistence (def last-min (atom 0)) (defn min [x y] (reset! last-min (if (<= x y) x y))) 10 No encapsulation Single instance only (reset! ) (swap! )

(defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) A Min Object 11

A Min Object 12 (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj ))  Constructor

A Min Object 13 (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj )) ((min1 :min) 5 6)  Call to min()

A Min Object 14 (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj )) ((min1 :min) 5 6) ((min1 :get))  Fetch last min

Agenda Functional vs Imperative languages OOP in Functional languages (e.g., Clojure) Data Encapsulation case studies Simple mutability (persistence) A simple object Mutability support Mutability support Failings of the substitution model The environment model Method dispatch Language comparisons 15

(defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj )) ((min1 :get)) The Problem: Binding last-min 16

The Solution: Environments 17 What happens when you define min():

The Solution: Environments 18 What happens when you define min():

Simple Execution 19 What happens when you execute min():

Simple Execution 20 What happens when you execute min(): Variable references resolved by traversing environment chain

(defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj )) ((min1 :get)) How Does min-obj Get Defined? 21

Object Definition 22 What happens when you define min-obj():

Object Instantiation 23 How do you instantiate min-obj():

Object Instantiation 24 How do you instantiate min-obj():

Method Execution 25 How do you execute min():

Functional Language OOP Data encapsulation as a result of Constructor parameter accessible only within function Binding of internal functions to constructor parameter allows state retention Other points Dismantling of environments amenable to access count tracking Dispatcher can operate on any criteria, and is pre-wired for SOA-style message passing 26

Future Investigation How to subclass How inheritance works Existing (or desirable) object frameworks Why isn’t OOP very common in Clojure? 27

Agenda Functional vs Imperative languages OOP in Functional languages (e.g., Clojure) Data Encapsulation case studies Simple mutability (persistence) A simple object Mutability support Failings of the substitution model The environment model Method dispatch Language comparisons Language comparisons 28

Selected Comparisons Deep Static Scope Data Encapsulation Clojure/LispArbitrary depthYes PascalArbitrary depthYes, but no instances Java/C++NoYes 29

Data Encapsulation Comparison (defn min-obj [init-min] (let [last-min (atom init-min)] (defn do-min [x y] (reset! last-min (if (<= x y) x y))) (defn get-last [] (deref last-min)) {:get #(get-last) :min #(do-min %1 %2)})) (define min1 (min-obj )) ((min1 :min) 5 6) ((min1 :get)) 30 class min_obj { private int last_min; public min_obj(int init_min) { last_min = init_min;} public int min(int x, int y) { last_min = (x <= y ? x : y); return last_min; } public int get () { return last_min;} } min_obj a = new min_obj( ); a.min(5, 6); a.get(); Clojure Java

Static Scoping in Pascal program test; var a : integer; procedure outer(); var b : integer; procedure inner(); var c : integer; begin c := a + b; end; begin b := a; inner(); end; begin a := 0; outer(); end. 31 Evaluation Stack a prev-frame-ptr b c no traversing Compiler knows stack location of all variables in scope – no traversing

Agenda Functional vs Imperative languages OOP in Functional languages (e.g., Clojure) Data Encapsulation case studies Simple mutability (persistence) A simple object Mutability support Failings of the substitution model The environment model Method dispatch Language comparisons 32

33 Questions