Data Abstraction and Modularity Slides today from John Mitchell.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
1 Modularity Shani Bard Based on a lecture by John Mitchell Reading: Chapter 9.
Modularity and Object-Oriented Programming John Mitchell CS 242 Reading: Chapter 10 and parts of Chapter 9.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Lecture 9 Concepts of Programming Languages
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Abstract Data Types and Encapsulation Concepts
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
C++ fundamentals.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
1 CSC241: Object Oriented Programming Lecture No 07.
Slide 1 Vitaly Shmatikov CS 345 Modularity and Object-Oriented Programming.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Data Structures Using C++ 2E
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Ceg860 (Prasad)L6MR1 Modularity Extendibility Reusability.
CS 2104 Prog. Lang. Concepts Subprograms
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Prepared By Ms.R.K.Dharme Head Computer Department.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Data Structure Introduction.
Types John Mitchell CS 242 Reading: Chapter 6. Announcements uHomework 1 due today Turn in at the end of class, OR Turn in by 5PM to the homework drop.
SOFTWARE DESIGN. INTRODUCTION There are 3 distinct types of activities in design 1.External design 2.Architectural design 3.Detailed design Architectural.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Data Abstaraction Chapter 10.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Modularity and Object-Oriented Programming John Mitchell CS 242 Reading: Chapter 10 and parts of Chapter 9.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Data Abstraction and Modularity
Abstract Data Types and Encapsulation Concepts
CS 326 Programming Languages, Concepts and Implementation
Abstract Data Types and Encapsulation Concepts
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Data Abstraction and Modularity Slides today from John Mitchell

Topics uModular program development Step-wise refinement Interface, specification, and implementation uLanguage support for modularity Procedural abstraction Abstract data types –Representation independence –Datatype induction Packages and modules Generic abstractions –Functions and modules with type parameters

Stepwise Refinement uWirth, 1971 “… program... gradually developed in a sequence of refinement steps” In each step, instructions … are decomposed into more detailed instructions. uHistorical reading on web (CS242 Reading page) N. Wirth, Program development by stepwise refinement, Communications of the ACM, 1971 D. Parnas, On the criteria to be used in decomposing systems into modules, Comm ACM, 1972 Both ACM Classics of the Month

Dijkstra’s Example (1969) begin print first 1000 primes end begin variable table p fill table p with first 1000 primes print table p end begin int array p[1:1000] make for k from 1 to 1000 p[k] equal to k-th prime print p[k] for k from 1 to 1000 end

Program Structure Main Program Sub-program

Data Refinement uWirth, 1971 again: As tasks are refined, so the data may have to be refined, decomposed, or structured, and it is natural to refine program and data specifications in parallel

Example uFor level 2, represent account balance by integer variable uFor level 3, need to maintain list of past transactions Bank Transactions DepositWithdrawPrint Statement Print transaction history

Modular program design uTop-down design Begin with main tasks, successively refine uBottom-up design Implement basic concepts, then combine uPrototyping Build coarse approximation of entire system Successively add functionality

Modularity: Basic Concepts uComponent Meaningful program unit –Function, data structure, module, … uInterface Types and operations defined within a component that are visible outside the component uSpecification Intended behavior of component, expressed as property observable through interface uImplementation Data structures and functions inside component

Example: Function Component uComponent Function to compute square root uInterface float sqroot (float x) uSpecification If x>1, then sqrt(x)*sqrt(x)  x. uImplementation float sqroot (float x){ float y = x/2; float step=x/4; int i; for (i=0; i<20; i++){if ((y*y)<x) y=y+step; else y=y-step; step = step/2;} return y; }

Example: Data Type uComponent Priority queue: data structure that returns elements in order of decreasing priority uInterface Type pq Operations empty : pq insert : elt * pq  pq deletemax : pq  elt * pq uSpecification Insert add to set of stored elements Deletemax returns max elt and pq of remaining elts

Heap sort using library data structure uPriority queue: structure with three operations empty : pq insert : elt * pq  pq deletemax : pq  elt * pq uAlgorithm using priority queue (heap sort) begin empty pq s insert each element from array into s remove elements in decreasing order and place in array end This gives us an O(n log n) sorting algorithm (see HW)

Language support for info hiding uProcedural abstraction Hide functionality in procedure or function uData abstraction Hide decision about representation of data structure and implementation of operations Example: priority queue can be binary search tree or partially-sorted array In procedural languages, refine a procedure or data type by rewriting it. Incremental reuse later with objects.

Abstract Data Types uProminent language development of 1970’s uMain ideas: Separate interface from implementation –Example: Sets have empty, insert, union, is_member?, … Sets implemented as … linked list … Use type checking to enforce separation –Client program only has access to operations in interface –Implementation encapsulated inside ADT construct

Origin of Abstract Data Types uStructured programming, data refinement Write program assuming some desired operations Later implement those operations Example: –Write expression parser assuming a symbol table –Later implement symbol table data structure uResearch on extensible languages What are essential properties of built-in types? Try to provide equivalent user-defined types Example: –ML sufficient to define list type that is same as built-in lists

Comparison with built-in types uExample: int Can declare variables of this type x: int Specific set of built-in operations +, -, *, … No other operations can be applied to integer values uSimilar properties desired for abstract types Can declare variables x : abstract_type Define a set of operations (give interface) Language guarantees that only these operations can be applied to values of abstract_type

Clu Clusters complex = cluster is make_complex, real_part, imaginary_part, plus, times rep = struct [ re, im : real] make_complex = proc (x,y : real) returns (cvt) return (rep${re:x, im:y}) real_part = proc (z:cvt) returns real return (z.re) imaginary_part = proc (z:cvt) returns real return (z.im) plus = proc (z, w: cvt) returns (cvt) return (rep${ re: z.re+w.re, im: z.im+w.im }) mult = proc … end complex

ML Abstype uDeclare new type with values and operations abstype t = of with val =... fun f( ) =... end uRepresentation t = of similar to ML datatype decl

Abstype for Complex Numbers uInput abstype cmplx = C of real * real with fun cmplx(x,y: real) = C(x,y) fun x_coord(C(x,y)) = x fun y_coord(C(x,y)) = y fun add(C(x1,y1), C(x2,y2)) = C(x1+x2, y1+y2) end  Types (compiler output) type cmplx val cmplx = fn : real * real -> cmplx val x_coord = fn : cmplx -> real val y_coord = fn : cmplx -> real val add = fn : cmplx * cmplx -> cmplx

Abstype for finite sets uDeclaration abstype 'a set = SET of 'a list with val empty = SET(nil) fun insert(x, SET(elts)) =... fun union(SET(elts1), Set(elts2)) =... fun isMember(x, SET(elts)) =... end uTypes (compiler output) type 'a set val empty = - : 'a set val insert = fn : 'a * ('a set) -> ('a set) val union = fn : ('a set) * ('a set) -> ('a set) val isMember = fn : 'a * ('a set) -> bool

Encapsulation Principles uRepresentation Independence Elements of abstract type can be implemented in various ways Restricted interface -> client program cannot distinguish one good implementation from another uDatatype Induction Method for reasoning about abstract data types Relies on separation between interface and implementation

Representation Independence uIntegers Can represent 0,1,2, …, -1,-2, … any way you want As long as operations work properly +, -, *, /, print, … Example 1’s complement vs. 2’s complement uFinite Sets can represent finite set {x, y, z, … } any way you want As long as operations work properly empty, ismember?, insert, union Example linked list vs binary tree vs bit vector

Reality or Ideal? uIn Clu, ML, … rep independence is a theorem Can be proved because language restricts access to implementation: access through interface only uIn C, C++, this is an ideal “Good programming style” will support representation independence The language does not enforce it Example: print bit representation of -1 This distinguishes 1’s complement from 2’s complement

Induction (Toward Datatype Induction) uMain idea 0 is a natural number if x is a natural number, then x+1 is a natural number these are all the natural numbers uProve p(n) for all n prove p(0) prove that if p(x) then p(x+1) that’s all you need to do Skip: Will not cover datatype induction in any depth this year

Induction for integer lists uPrinciple nil is a list if y is a list and x is an int, then cons(x,y) is a list these are all of the lists uProve p(y) for all lists y prove p(nil) prove that if p(y) then p(cons(x,y)) that’s all you need to do uExample: next slide Note: we do not need to consider car, cdr Why? No new lists. (No subtraction in integer induction.)

Example of list induction uFunction to sort lists fun sort(nil) = nil | sort(x::xs) = insert(x, sort(xs)) uInsertion into sorted list fun insert(x, nil) = [x] | insert(x, y::ys) = if x<y then x::(y::ys) else y::insert(x,ys) uProve correctness of these functions Use induction on lists (easy because that’s how ML let’s us write them)

Interfaces for Datatype Induction uPartition operations into groups constructors: build elements of the data type operators: combine elements, but no “new” ones observers: produce values of other types uExample: sets with empty : set insert : elt * set -> set union : set * set -> set isMember : elt * set -> bool partition construtors: empty, insert operator: union observer: isMember

Induction on constructors uOperator: produces no new elements Example: union for finite sets Every set defined using union can be defined without union: union(empty, s) = s union(insert(x,y), s) = insert(x, union(y,s)) uProve property by induction Show for all elements produced by constructors Set example: Prove P(empty) and P(y) => P(insert(x,y)) This covers all elements of the type Example in course reader: equivalence of implementations

Example of set induction uAssume map function map(f,empty) = empty map(f, insert(y,s)) = union(f(y), map(f,s)) uFunction to find minimum element of list fun intersect(s,s’) = if empty(s’) then s’ else let f(x) = if member(x,s) then {x} else empty in map(f, s’) end; uProve that this work: Use induction on s’: –Correct if s’ = empty –Correct if s’ = insert(y, s’’)

What’s the point of all this induction? uData abstraction hides details uWe can reason about programs that use abstract data types in an abstract way Use basic properties of data type Ignore way that data type is implemented uThis is not a course about induction We may ask some simple questions You will not have to derive any principle of induction

Modules uGeneral construct for information hiding uTwo parts Interface: A set of names and their types Implementation: Declaration for every entry in the interface Additional declarations that are hidden uExamples: Modula modules, Ada packages, ML structures,...

Modules and Data Abstraction module Set interface type set val empty : set fun insert : elt * set -> set fun union : set * set -> set fun isMember : elt * set -> bool implementation type set = elt list val empty = nil fun insert(x, elts) =... fun union(…) = end Set uCan define ADT Private type Public operations uMore general Several related types and operations uSome languages Separate interface and implementation One interface can have multiple implementations

Generic Abstractions uParameterize modules by types, other modules uCreate general implementations Can be instantiated in many ways uLanguage examples: Ada generic packages, C++ templates, ML functors, … ML geometry modules in course reader C++ Standard Template Library (STL) provides extensive examples

C++ Templates uType parameterization mechanism template … indicates type parameter T C++ has class templates and function templates –Look at function case now uInstantiation at link time Separate copy of template generated for each type Why code duplication? –Size of local variables in activation record –Link to operations on parameter type

Example uMonomorphic swap function void swap(int& x, int& y){ int tmp = x; x = y; y = tmp; } uPolymorphic function template template void swap(T& x, T& y){ T tmp = x; x = y; y = tmp; } uCall like ordinary function float a, b; … ; swap(a,b); …

Generic sort function uFunction requires < on parameter type template void sort( int count, T * A[count] ) { for (int i=0; i<count-1; i++) for (int j=I+1; j<count-1; j++) if (A[j] < A[i]) swap(A[i],A[j]); } uHow is function < found? Link sort function to calling program Determine actual T at link time If < is defined on T, then OK else error –May require overloading resolution, etc.

Compare to ML polymorphism fun insert(less, x, nil) = [x] | insert(less, x, y::ys) = if less(x,y) then x::y::ys else y::insert(less,x,ys) fun sort(less, nil) = nil | sort(less, x::xs) = insert(less, x, sort(less,xs)) uPolymorphic sort function Pass operation as function No instantiation since all lists are represented in the same way (using cons cells like Lisp). uUniform data representation Smaller code, can be less efficient, no complicated linking

Standard Template Library for C++ uMany generic abstractions Polymorphic abstract types and operations uUseful for many purposes Excellent example of generic programming uEfficient running time (but not always space) uWritten in C++ Uses template mechanism and overloading Does not rely on objects Architect: Alex Stepanov

Main entities in STL uContainer: Collection of typed objects Examples: array, list, associative dictionary,... uIterator: Generalization of pointer or address uAlgorithm uAdapter: Convert from one form to another Example: produce iterator from updatable container uFunction object: Form of closure (“by hand”) uAllocator: encapsulation of a memory pool Example: GC memory, ref count memory,...

Example of STL approach uFunction to merge two sorted lists merge : range(s)  range(t)  comparison(u)  range(u) This is conceptually right, but not STL syntax. uBasic concepts used range(s) - ordered “list” of elements of type s, given by pointers to first and last elements comparison(u) - boolean-valued function on type u subtyping - s and t must be subtypes of u

How merge appears in STL uRanges represented by iterators iterator is generalization of pointer supports ++ (move to next element) uComparison operator is object of class Compare uPolymorphism expressed using template template < class InputIterator1, class InputIterator2, class OutputIterator, class Compare > OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator1 last2, OutputIterator result, Compare comp)

Comparing STL with other libraries uC: qsort( (void*)v, N, sizeof(v[0]), compare_int ); uC++, using raw C arrays: int v[N]; sort( v, v+N ); uC++, using a vector class: vector v(N); sort( v.begin(), v.end() );

Efficiency of STL uRunning time for sort N = 50000N = C C++ (raw arrays) C++ (vector class) uMain point Generic abstractions can be convenient and efficient ! But watch out for code size if using C++ templates…