Prof. Fateman CS164 Lecture 251 Languages as a Tool for Software Engineering Piercing the Mysteries of Object-Oriented Programming Lecture 25.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
Advertisements

Agenda Definitions Evolution of Programming Languages and Personal Computers The C Language.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
The Assembly Language Level
OOP - Object Oriented Programming Object Oriented Programming is an approach to programming that was developed to make large programs easier to manage.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Generics "It is easy to study the rules of overloading and of templates without noticing that together they are one of the keys to elegant and efficient.
Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Programming Language Paradigms: summary. Object-oriented programming Objects are the fundamental building blocks of a program. Interaction is structured.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Using Classes Lisp also supports the Object-Oriented paradigm. The process of defining classes in Lisp is similar to how you you define structures. The.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Chapter 1: An Overview of Computers and Programming Languages
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Common Lisp Object System (CLOS)
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Chapter 2: Algorithm Discovery and Design
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
CSCI-383 Object-Oriented Programming & Design Lecture 15.
1 CS101 Introduction to Computing Lecture 19 Programming Languages.
Language Evaluation Criteria
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CS 403 – Programming Languages Class 25 November 28, 2000.
CS101 Introduction to Computing Lecture Programming Languages.
1 CLOS: Common Lisp Object System Basic principles of CLOS Classes, Instances and Slots Inheritance Generic functions Methods Multi-methods Comparison.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 1 Object Orientation: Objects and Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
CE Operating Systems Lecture 14 Memory management.
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Object-Oriented Programming Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Introduction to OOP CPS235: Introduction.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSCE 240 – Intro to Software Engineering Lecture 3.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
1 Case Study: Meta Classes  Class representation in memory  Class variables and class methods  Meta Classes  3 Level System  4 Level System  5 Level.
Object Oriented Programming Some Interesting Genes.
Prof. Fateman CS164 Lecture 35
Visit for more Learning Resources
CS101 Introduction to Computing Lecture 19 Programming Languages
CLOS CSC 358/
Week 6 Object-Oriented Programming (2): Polymorphism
Peter Seibel Practical Common Lisp Peter Seibel
Basic Concepts of Algorithm
Common Lisp II.
C++ Object Oriented 1.
Presentation transcript:

Prof. Fateman CS164 Lecture 251 Languages as a Tool for Software Engineering Piercing the Mysteries of Object-Oriented Programming Lecture 25

Prof. Fateman CS164 Lecture 252 Lecture Outline What is the relationship of PL and productivity? –Search for the silver bullet –Fads: Systematic, nth Generation, Object Oriented, … –Side issues: batch vs. time-sharing vs. graphic interaction OO –Message passing vs Generic functions –The CLOS model –Implementation

Prof. Fateman CS164 Lecture 253 Programmer Productivity is Low If a programmer produces on average only 5-10 lines of correct code per day, and it is irrelevant whether the code is in assembler or C or …. Then the programmer using assembler is less productive. What can we do? What is the silver bullet?

Prof. Fateman CS164 Lecture 254 Various proposals for higher productivity Structured Requirements etc Formal methods Team dynamics Hardware and software support (above the language level) Testing Rapid turnaround

Prof. Fateman CS164 Lecture 255 Higher Level Languages and Productivity Use more concise programming languages? Verify correctness Automatic test generation Fire the below-average programmers? Hire more programmers? (Re-)educate the programmers in mathematics and logic? Genetic engineering: clone the best programmers? Smart pills? BETTER SUPPORT FOR THE SOFTWARE “LIFE CYCLE” maintenance portability Change the PL “paradigm” to model the application so that the translation from real-world to virtual program world is easier.

Prof. Fateman CS164 Lecture 256 Object Oriented Programming tries to model the world Is this really a language issue? Yes if you are using a language that interferes; No if you can just add these concepts to the language. Lisp supports many forms of language, and at least 4 OO additions have been proposed and implemented (Flavors, Loops, NewFlavors, CLOS). Lisp allows one to use the “meta object protocol” to define variations on its object system.

Prof. Fateman CS164 Lecture 257 Review of Object Oriented Programming Ideas Base some or all of your language about data objects: running this down to the “ground”…. An instance or object is a block of memory that can hold some data in slots, and is associated with some class of similar objects. There is a way of finding out the class (es) of any object. Associated with the class is a collection of methods for manipulating objects, referring to their “slots” and other methods. Sometimes classes have data too. Typically there is a class hierarchy, so that an object is a member of a class, but also a member of its “super” class. Methods can also be attached to the super-classes. Objects – Classes – Hierarchy-Inheritance-Methods

Prof. Fateman CS164 Lecture 258 Two common models for thinking about it The recent first wave was to think of “message passing” … for example, to find the area of an object tell obj area If obj has a method for area, or a superclass has a method, then we “set self:= obj” and then run the method. Extra arguments are possible like tell obj move 10 X := ;; 4 has a + method, aux argument 5. X has a := method.. Etc [this is like Smalltalk)

Prof. Fateman CS164 Lecture 259 Limits of the message model This gets pretty boring, since EVERYTHING is tell RECEIVER MESSAGE EXTRA_ARGS So why not leave out the “tell” and put the “active part” first; all we need is () to put it in lisp syntax: (MESSAGE RECEIVER EXTRA_ARGS) And now you see that the OO message-passing idea says in effect that we are calling a function … which is the meaning of MESSAGE, but that its meaning is modified by its first argument (only). (+ 4 5) is ‘find the + method of 4, apply to 5.’ An obvious generalization: we could allow all the arguments to participate in modifying the meaning of MESSAGE, which brings us to the next model: generic functions.

Prof. Fateman CS164 Lecture 2510 Generic functions are another cut through the same programming text GROUPED BY METHOD (defmethod area((z rectangle)) ;; length X width.. (defmethod area ((z circle)) ;; pi X r^2 (defmethod area ((z triangle)) ….. GROUPED BY CLASS class rectangle method area …. class circle method area …

Prof. Fateman CS164 Lecture 2511 Generic functions consist of “all the methods with the same name” discriminated by argument types (defmethod area ((z rectangle)) ;; length X width.. (defmethod area ((z circle)) ;; pi X r^2 (defmethod area ((z triangle)) ….. Textually, you could write these defmethods far apart in the program, if you chose to do so.

Prof. Fateman CS164 Lecture 2512 CL Generic functions can be generic with respect to each argument’s type* (defmethod add((z rational)(w rational)) … (defmethod add((z doublefloat)(w rational)) … (defmethod add((z rational)(w doublefloat)) … (defmethod add((z interval)(w rational)) … Etc. We could program combinations of rational + interval + complex + float etc. In general we still need to define the types, their slots, their inheritance *kinds of objects. The Object system mirrors the built-in types in Lisp.

Prof. Fateman CS164 Lecture 2513 More general types can also be used for defmethod… …This is how inheritance can be used effectively (defmethod add((z number)(w number)) … CLOS will use the MOST SPECIFIC method. Thus if number is a superclass of rational, doublefloat, complex, interval etc. this last method will be used as a backup (only) when a more specific pair of arguments cannot be found.

Prof. Fateman CS164 Lecture 2514 A clumsy example for CLOS (defclass plane-figure() ()) ;no superclass, no slots (defclass rectangle(plane-figure)(height width)) (setf r1 (make-instance 'rectangle)) (setf (slot-value r1 'height) 10) (setf (slot-value r1 'width) 15) (defmethod area ((r rectangle))(* (slot-value r 'width) (slot-value r 'height))) (area r1) 150 (area ‘foo) error: no methods applicable

Prof. Fateman CS164 Lecture 2515 A neater way, declaring slots (defclass plane-figure() ()) (defclass rect (plane-figure) ((height :accessor hi) (width :accessor wi))) (setf r1 (make-instance 'rect)) (setf (hi r1) 10) (setf (wi r1) 15) (defmethod area ((r rectangle))(* (wi r) (hi r))) (area r1) 150

Prof. Fateman CS164 Lecture 2516 Even neater way, declaring initargs (defclass plane-figure() ()) (defclass rect (plane-figure) ((height :accessor hi :initarg :hi) (width :accessor wi :initarg :wi))) (setf r1 (make-instance 'rect :hi 10 :wi 15)) (defmethod area ((r rect))(* (wi r) (hi r))) (area r1) 150

Prof. Fateman CS164 Lecture 2517 Superclasses Complex precedence rules for inheritance from multiple superclasses (single inheritance is often dictated by some language optimized for “efficiency”) CLOS allows multiple inheritance, e.g. from geometric shape as well as graphical representation (defclass screen-circle (circle graphic) ….; (setf sc (make-instance ‘screen-circle …)) (radius sc) (color sc)

Prof. Fateman CS164 Lecture 2518 Method combination Complex instructions for use of methods / before, after, in-between. These are all plausible for various applications. There may be zero or more applicable methods: all the arguments in the call come within the specializations of all of its parameters. Zero= error; otherwise the most specific gets used. But auxiliary methods can be invoked e.g. “do this first” or “do this afterward” or even “around”..which can call the primary method via “call-next-method”. (Polite speaker example…)

Prof. Fateman CS164 Lecture 2519 Implementation Criteria Flexibility/ Generality is part of the CL design –Dynamic class definition: you can add methods or even slots to an object AFTER instantiation Efficiency should come out of the implementation: If you never need this dynamic facility, set fields in concrete as early as possible MOP “meta object protocol” provides tools for rolling your own version of object system. (Used by matlisp

Prof. Fateman CS164 Lecture 2520 General Techniques One way: Method/Function call is 2 steps: given the method, use a “case” on the argument type(s) to find the right version. Another way (not for CLOS), given the type of the receiver of the message, use a “case” on the method name to find the right method. Either way you have to deal with inheritance, when there is no immediate success in the search. It is possible to compile out flexibility at compile time or repeatedly “just-in-time” (CLOS recompiles classes on redefinition; other systems require recompiling everything.)

Prof. Fateman CS164 Lecture 2521 A simplified “How to do OO” (ref. ANSI Common Lisp chapter 17) YOU ARE ASSUMED TO HAVE READ THIS CHAPTER!! It uses a simple “message passing” model. Starting here. Classes are almost the same as objs. Each object obj is a hash table. If you want to find a method meth associated with that object, you compute (gethash meth obj). Thus tell obj meth becomes (funcall (gethash meth obj) obj) We can define tell (defun tell(ob m &rest args) (apply (gethash m ob) ob args)

Prof. Fateman CS164 Lecture 2522 That’s it

Prof. Fateman CS164 Lecture 2523 A more elaborate “How to do OO” Repeat from previous version: Each object obj is a hash table. If you want to find a property prop associated with that object, you compute (gethash prop obj) New part: inheritance if there is no such property, then get obj’s parent, under its :parent property, and look there, recursively if necessary. That is, you look at (gethash prop (gethash :parent obj) ) call this recursive gethash rget

Prof. Fateman CS164 Lecture 2524 Tell is almost the same. Use rget Tell obj message extra-args is implemented by (defun tell (obj message &rest args) (apply (rget message obj) obj args)) It is assumed here that (rget message obj) will return a function

Prof. Fateman CS164 Lecture 2525 We could stop here but.. Let us try for multiple inheritance. An object then does not have a single parent, but a list of parents. We must compute an order in which to visit the parents, and have rget use that. When do you compute the list of parents?

Prof. Fateman CS164 Lecture 2526 Parent precedence list for multiple inheritance Object A inherits from B and C, B inherits from D, C inherits from D. When do you look at D? Compute a precedence list. When do you compute its parents? (a)At compile time (efficient but inflexible). (b)Every time you use rget and need to know where to look? (flexible but very inefficient) (c)Every time the class hierarchy changes you recompute the :parents property for every object (inefficient if there are many objects) (d)When you use rget, you ask, has the class hierarchy changed so as to affect this object? If so, redo :parents (almost as efficient as (a)).

Prof. Fateman CS164 Lecture 2527 Make this efficient It pays to separate out classes and instances. Associate methods with classes, so instances can be smaller.

Prof. Fateman CS164 Lecture 2528 Classes become more efficient Instead of hashtables, use arrays. Compile away the “names” of methods the same way we compiled away the symbol table. For example, Method PRINT might be location 13 in the method array associated with the class of an object. Method X inherited from parent A is also given a slot in the array. (a)Do as much as possible at compile time (efficient but inflexible). (b)Instead of rget you are just doing an array ref. (c)Make it impossible to change the class hierarchy unless you recompile.

Prof. Fateman CS164 Lecture 2529 Instances become more efficient Instead of hashtables, use arrays. Compile away the “names” of instance variables the same way we compiled away the symbol table. For example, slot “radius” might be location 2 in the instance array associated with an object. Slot 0 might be a pointer to the class of this object. Look there for methods.

Prof. Fateman CS164 Lecture 2530 Object orientation has many variants If this one is too simple, make it fancier. If this is too fancy or slow, make it simpler. In some sense object orientation and first-class functions are different ways of viewing the same style of computation; languages with a flexible functional approach can implement objects easily. (CS61a OO was written in Scheme..)