CS 351 Design of Large Programs Programming Abstractions

Similar documents
Programming II (CS300)

Programming II (CS300)

CS 251 Intermediate Programming Methods and More

Programming II (CS300)

CS 251 Intermediate Programming Methods and Classes

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

IUSE Knowledge Test. 1. Demographic Questions. IUSE Knowledge Test. 2. Computational Thinking Knowledge Test

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Weeks 6&7: Procedures and Parameter Passing

Control Abstraction. Hwansoo Han

CS 351 Design of Large Programs Threads and Concurrency

Pace University. Fundamental Concepts of CS121 1

Com S 541. Programming Languages I

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

Faster Sorting Methods

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

CS 12 Fall 2003 Solutions for mid-term exam #2

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Functional Logic Programming Language Curry

The return Statement

LECTURE 18. Control Flow

A case study of delegates and generics in C#

CS 351 Design of Large Programs Singleton Pattern

Computer Components. Software{ User Programs. Operating System. Hardware

Chapter 15. Functional Programming Languages

Motivation was to facilitate development of systems software, especially OS development.

CSC Java Programming, Fall Java Data Types and Control Constructs

Sorting. Task Description. Selection Sort. Should we worry about speed?

Software Engineering using Formal Methods

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CPS 506 Comparative Programming Languages. Programming Language Paradigm

On the correctness of template metaprograms

Writing an Interpreter Thoughts on Assignment 6

Clojure Concurrency Constructs. CSCI 5828: Foundations of Software Engineering Lecture 12 10/02/2014

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Recursion. Chapter 5

Functional Programming. Pure Functional Programming

Programmiersprachen (Programming Languages)

Unit-3 Software Design (Lecture Notes)

Motivation was to facilitate development of systems software, especially OS development.

CS558 Programming Languages

Programming Language Pragmatics

Classic Data Structures Introduction UNIT I

M1-R4: Programing and Problem Solving using C (JAN 2019)

CSE 230 Computer Science II (Data Structure) Introduction

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

G Programming Languages - Fall 2012

5/23/2015. Core Java Syllabus. VikRam ShaRma

CS 242. Fundamentals. Reading: See last slide

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

Operating Systems (234123) Spring (Homework 3 Wet) Homework 3 Wet

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

CS 251 Intermediate Programming Java Basics

Lecture Notes on Program Equivalence

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CS 351 Design of Large Programs Object-Oriented Design Principles

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Operational Semantics. One-Slide Summary. Lecture Outline

CS 139 Practice Midterm Questions #2

Data Types. Operators, Assignment, Output and Return Statements

Statement level control structures

COMP Parallel Computing. CC-NUMA (2) Memory Consistency

Type Checking and Type Equality

Chapter 4 Defining Classes I

CONCURRENT/DISTRIBUTED PROGRAMMING ILLUSTRATED USING THE DINING PHILOSOPHERS PROBLEM *

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CS 132 Exam #1 - Study Suggestions

Concepts of Programming Languages

Data Structures and Algorithms. Chapter 1

CS510 Advanced Topics in Concurrency. Jonathan Walpole

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

G52CON: Concepts of Concurrency

Formal Semantics of Programming Languages

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Lecture 16: Recapitulations. Lecture 16: Recapitulations p. 1

CSE 230 Intermediate Programming in C and C++ Recursion

Example Scheme Function: equal

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CS103L SPRING 2017 UNIT 8: RECURSION

Functional Programming. Big Picture. Design of Programming Languages

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

for (int outercounter = nums.length - 1; outercounter > 0 && swappedthatturn; outercounter --

Formal Semantics of Programming Languages

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Formal Specification and Verification

Chapter 6 Control Flow. June 9, 2015

CS 106 Introduction to Computer Science I

Handout 9: Imperative Programs and State

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

CSC 1351: Quiz 6: Sort and Search

QUICKSORT TABLE OF CONTENTS

G52CON: Concepts of Concurrency

Resource management. Real-Time Systems. Resource management. Resource management

Transcription:

CS 351 Design of Large Programs Programming Abstractions Brooke Chenoweth University of New Mexico Spring 2019

Searching for the Right Abstraction The language we speak relates to the way we think. The way we view programming affects the kinds of systems we construct. Thus, the level of abstraction impacts: Programming productivity Reasoning about programs Program analysis Formal verification

Control Abstractions Flow of control defines the order in which instructions are executed. Sequential flow of control is built into most machines (program counter) Conditional jumps allow the interpreter to skip and/or repeat code segments if and goto statements provide a more uniform treatment by separating the condition from the flow of control transfer. Further reductions in complexity are achieved by the shift to structured programming Exceptions provide a structured mechanism for handling error conditions

Procedural Abstractions Procedural abstractions laid the foundation for modular design. Macro substitution offered a mechanism for naming sections of code and inserting them as needed Subroutines (non-recursive), introduced as a memory saving device, structured the flow of control Blocks provide an in-line structuring construct central to all modern programming languages Procedures (recursive) encapsulate processing, thus eliminating the need to look at the way they are coded (if a specification exists!) Functions (pure) are procedures without side effects

Data Abstraction Data is central to the representation of state. Built-in data types provide essential but primitive forms of data representation and operations on them Programmer-defined types facilitate tailoring data selection to the specifics of the application Strongly-typed languages improve dependability by doing strict compile-time checking Abstract data type (ADT) is a formal characterization of a set of data structures sharing a common set of operations Generic types (e.g., generics in Java) allow for parameterized definitions

Concurrency Concurrency impacts the choice of components and connectors. Concurrency is often relegated to operating systems services rather than language definition (e.g., C++) Coroutines introduced the notion of passing the flow of control among subroutines Concurrent process (e.g., task in Ada, thread in Java) provides for logically parallel execution Inter-process communication assumes a variety of forms Shared variables Message passing Remote procedure call Synchronization (mutual exclusion, barriers, etc.)

Functions Revisited As in mathematics, a function defines a transformation from its inputs to the outputs. It has no side effects (no memory and no changes in the program state) It is deterministic (same inputs generate the same outputs every time)

Function Example: factorial factorial(n) where n is a natural number returns 1 if n = 0 n (n 1) 1 if n > 0 Functions are often defined recursively factorial(n) returns 1 if n = 0 n factorial(n 1) if n > 0 What happens if n is an integer? factorial(n) returns error if n < 0 1 if n = 0 n factorial(n 1) if n > 0

Factorial in Java public static int factorial ( int n) throws ArithmeticException { if( n < 0) throw new ArithmeticException (); else if( n == 0 n == 1) return 1; return n * factorial (n -1); public static int factorial ( int n) { if(n < 0) { System. err. println (" Error : Factorial is undefined " + " for negative integers."); return 0; else if( n == 0 n == 1) return 1; return n * factorial (n -1);

Axiomatic Specification A mathematical relation between the input and output values. Assertions represent a convenient abstract mechanism for function specification An assertion is a logical fact that is true about the state of the program at some point in its execution Some programming languages provide assertions as built-in constructs A pre-assertion defines the relevant properties of the input values A post-assertion defines the relevant properties of the output value

Sort(X) returns Y pre: post: Axiomatic Spec: Sort X is an array of integers indexed from 0 to N Y is an array of integers indexed from 0 to N Y is sorted in ascending order any integer k occurs the same number of times in both X and Y

Operational Specification (Pseudocode) An operational specification is an abstract program that: Establishes the desired relation between inputs and outputs Places no restrictions on how the function is ultimately coded Any code that accomplishes the same transformation is acceptable Some coding solutions may be more efficient than others

Sort(X) returns Y given: Pseudocode: Sort X is an array of integers indexed from 0 to N Y is an array of integers indexed from 0 to N 1. copy X into Y 2. while (there exists i and j such that i < jand Y [i] > Y [j]) swap Y[i] and Y[j]

Sort in Java: bubblesort public static int [] bubblesort ( int [] ary ) { // assert ary. length > 0; int length = ary. length -1; boolean swap = true ; while ( swap ) { swap = false ; for ( int i =0; i< length ; i ++) { if(ary [i +1] < ary [i]) { int tmp = ary [i]; ary [i] = ary [j]; ary [j] = tmp ; swap = true ; return ary ;

Sort in Java: quicksort public static void quicksort ( int [] ary, int low, int high ) { if( ary == null ary. length == 0) return ; if(low >= high ) return ; int mid = low + ( high low ) / 2; int pivot = ary [ mid ]; int i = low, j = high ; while (i <= j) { while ( ary [i] < pivot ) i ++; while ( ary [j] > pivot ) j - -; if(i <= j) { int tmp = ary [i]; ary [i] = ary [j]; ary [j] = tmp ; i ++; j - -; if(low < j) quicksort (ary, low, j); if( high > i) quicksort (ary, i, high );

Procedures Revisited Procedures, in contrast to functions, may have side effects due to: Local variables Access to resources Access to devices The result of invoking a procedure may lead to Returning data whose values depend on the internal state of the procedure Changes in the internal state of the procedure The specification methods are similar except for: The treatment of the internal state!

Abstract State Specification The internal state (e.g., data structures) of a procedure may be highly complex Proper abstraction of the internal state simplifies greatly the specification Users of the procedure need not be exposed to the internal data representation Internal representation may change over time Specification is not affected Code may be drastically affected

Documentation Implications (1/2) Pre and Post assertions are the best way to document procedures and methods. Assertions are very helpful when placed at critical junctions in the code. Pseudocode is not as helpful as assertions in documenting code is very good at capturing processing logic, e.g., explicit task scheduling must be highly abstract with a typical ratio of 1:10 (text vs. code)

Documentation Implications (2/2) Focusing on an abstract state is challenging simplifies documentation protects the documentation against implementation changes is primarily associated with object and class documentation