Docsity

Lecture 7: Modular Program Development and Language Support for Modularity in ECS 140A - P, Assignments of Programming Languages

Toggle sidebar
bg1
1
Prof. Su ECS 140A Lecture 7 1
Programming Languages
ECS 140A
13:40-15:00 Tu/Th
55 Roessler
Prof. Su ECS 140A Lecture 7 2
Announcements
•HW2 due April 23
rd
•HW3 will be assigned this week
–Due: May 7
th
•
Midterm on May 12
th
Prof. Su ECS 140A Lecture 7 3
Outline
•Modular program development
–Step-wise refinement
–Interface, specification, and implementation
•Language support for modularity
–Procedural abstraction
–
Abstract data types
–
Packages and modules
–
Generic abstractions
•
Functions and modules with type parameters
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Lecture 7: Modular Program Development and Language Support for Modularity in ECS 140A - P and more Assignments Programming Languages in PDF only on Docsity!

Prof. Su ECS 140A Lecture 7 1

Programming Languages

ECS 140A

13:40-15:00 Tu/Th

55 Roessler

Prof. Su ECS 140A Lecture 7 2

Announcements

  •  HW2 due April 23 rd
  •  HW3 will be assigned this week
    •  Due: May 7th
  •  Midterm on May 12th

Prof. Su ECS 140A Lecture 7 3

Outline

  •  Modular program development
    •  Step-wise refinement
    •  Interface, specification, and implementation
  •  Language support for modularity
    •  Procedural abstraction
    •  Abstract data types
    •  Packages and modules
    •  Generic abstractions
      •  Functions and modules with type parameters

Prof. Su ECS 140A Lecture 7 4

Stepwise Refinement

  •  Wirth, 1971
    •  “… program ... gradually developed in a sequence of refinement steps”
    •  In each step, instructions … are decomposed into more detailed instructions.
  •  Historical readings
    •  N. Wirth, Program development by stepwise

refinement, C ACM, 1971

  •  D. Parnas, On the criteria to be used in

decomposing systems into modules, CACM, 1972

  •  Both ACM Classics of the Month

Prof. Su ECS 140A Lecture 7 5

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

Prof. Su ECS 140A Lecture 7 6

Program Structure

Main Program

Sub-program Sub-program Sub-program

Sub-program Sub-program

Prof. Su ECS 140A Lecture 7 10

Example: Function Component

  •  Component
    •  Function to compute square root
  •  Interface
    •  float sqrt (float x)
  •  Specification
    •  If x > 1, then sqrt(x)*sqrt(x) ~ x
  •  Implementation float sqrt (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; }

Prof. Su ECS 140A Lecture 7 11

Example: Data Type

  •  Component
    •  Priority queue: data structure that returns elements in order of decreasing priority
  •  Interface
    •  Type pq
    •  Operations empty : pq insert : elt * pq -> pq deletemax : pq -> elt * pq
  •  Specification
    •  insert adds to set of stored elements
    •  deletemax returns max elt and pq of remaining elts

Prof. Su ECS 140A Lecture 7 12

Heap sort using library data structure

  •  Priority queue: structure with three operations empty : pq insert : elt * pq -> pq deletemax : pq -> elt * pq
  •  Algorithm using priority queue (heap sort) begin create 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

Prof. Su ECS 140A Lecture 7 13

Abstract Data Types

  •  Prominent language development of 1970’s
  •  Main 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

Prof. Su ECS 140A Lecture 7 14

Modules

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

Prof. Su ECS 140A Lecture 7 15

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

  •  Can define ADT
    •  Private type
    •  Public operations
  •  More general
    •  Several related types and operations
  •  Some languages
    •  Separate interface and implementation
    •  One interface can have multiple implementations

Prof. Su ECS 140A Lecture 7 19

Standard Template Library for C++

  •  Many generic abstractions
    •  Polymorphic abstract types and operations
  •  Useful for many purposes
    •  Excellent example of generic programming
  •  Efficient running time (but not always space)
  •  Written in C++
    •  Uses template mechanism and overloading
    •  Does not rely on objects - no virtual functions

Architect: Alex Stepanov

Prof. Su ECS 140A Lecture 7 20

Main Entities in STL

  •  Container: Collection of typed objects
    •  Examples: array, list, associative dictionary, ...
  •  Iterator: Generalization of pointer or address
  •  Algorithm
  •  Adaptor: Convert from one form to another
    •  Example: produce iterator from updatable container
  •  Function object: form of closure (“by hand”)
  •  Allocator: encapsulation of a memory pool
    •  Example: GC memory, ref count memory, ...

Prof. Su ECS 140A Lecture 7 21

Example of STL Approach

  •  Function to merge two sorted lists
    •  merge : range(s) * range(t) * comparison(u) -> range(u) This is conceptually right, but not STL syntax
  •  Basic 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

Prof. Su ECS 140A Lecture 7 22

How merge appears in STL

  •  Ranges represented by iterators
    •  iterator is generalization of pointer
    •  supports ++ (move to next element)
  •  Comparison operator is object of class Compare
  •  Polymorphism expressed using template template < class InputIterator1, class InputIterator2, class OutputIterator, class Compare > OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)

Prof. Su ECS 140A Lecture 7 23

Comparing STL with other libraries

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

Prof. Su ECS 140A Lecture 7 24

Efficiency of STL

  •  Running time for sort N = 50000 N = 500000 C 1.4215 18.

C++ (raw arrays) 0.2895 3. C++ (vector class) 0.2735 3.

  •  Main point
    •  Generic abstractions can be convenient and efficient!
    •  But watch out for code size if using C++ templates…