CS Computable Functions. Reading: Chapter 2

Similar documents
Modularity. Modular program development. Language support for modularity. Step-wise refinement Interface, specification, and implementation

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

Seminar in Programming Languages

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

Object-Oriented Programming Concepts

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Lecture 7: Data Abstractions

Inductive Data Types

Lists. Michael P. Fourman. February 2, 2010

The type checker will complain that the two branches have different types, one is string and the other is int

Modules. Cardelli, 1996

CS558 Programming Languages

Subtyping for behaviour?

CS558 Programming Languages

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

CS558 Programming Languages

Functional Programming in Haskell Part I : Basics

Programming Language Concepts, CS2104 Lecture 7

Software Architecture. Abstract Data Types

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Some Advanced ML Features

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Polymorphism and Type Inference

Subtyping for behaviour?

CS 242. Fundamentals. Reading: See last slide

The Object Oriented Paradigm

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Standard ML. Data types. ML Datatypes.1

Chapter 6 Abstract Datatypes

COMPUTER SCIENCE TRIPOS

Mini-ML. CS 502 Lecture 2 8/28/08

Type Systems, Type Inference, and Polymorphism

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

Lamé s Theorem. Strings. Recursively Defined Sets and Structures. Recursively Defined Sets and Structures

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Short Notes of CS201

Sample Exam; Solutions

CS201 - Introduction to Programming Glossary By

Draw a diagram of an empty circular queue and describe it to the reader.

Scope. Chapter Ten Modern Programming Languages 1

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Higher-Order Functions

Types, Type Inference and Unification

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Fall 2018 Lecture N

Testing. Prof. Clarkson Fall Today s music: Wrecking Ball by Miley Cyrus

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

COM2001 Advanced Programming Techniques. Mike Stannett Department of Computer Science The University of Sheffield

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Week 7&8: Types and Type Systems

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Fall Recursion and induction. Stephen Brookes. Lecture 4

COS 320. Compiling Techniques

Lecture 19: Functions, Types and Data Structures in Haskell

Recursively Defined Functions

02157 Functional Programming Tagged values and Higher-order list functions

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

G Programming Languages - Fall 2012

- couldn t be instantiated dynamically! - no type or other method of organizing, despite similarity to

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

Today s lecture. CS 314 fall 01 C++ 1, page 1

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

G Programming Languages - Fall 2012

HW 1 CMSC 452. Morally DUE Feb 7 NOTE- THIS HW IS THREE PAGES LONG!!! SOLUTIONS THROUGOUT THIS HW YOU CAN ASSUME:

CSCI-GA Scripting Languages

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Reasoning about programs. Chapter 9 of Thompson

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

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

Software Design and Analysis for Engineers

Polymorphism and Type Inference

Generic polymorphism on steroids

RSL Reference Manual

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name)

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

Classes, subclasses, subtyping

Object Orientation. Chapter Sixteen Modern Programming Languages, 2nd ed. 1

Data Abstraction. Hwansoo Han

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

Fall 2018 Lecture 7. Stephen Brookes

COMPUTER SCIENCE TRIPOS

10. Abstract Data Types

Lecture 19: Signatures, Structures, and Type Abstraction

CSCE 314 Programming Languages. Type System

To illustrate what is intended the following are three write ups by students. Diagonalization

CSE 100 Advanced Data Structures

The Imperative Paradigm

Subtyping for behaviour?

Chapter 11 :: Functional Languages

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

More Lambda Calculus and Intro to Type Systems

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

COMP 382: Reasoning about algorithms

Functional Programming

Transcription:

CS 242 2012 Computable Functions Reading: Chapter 2

Foundations: Partial,Total Functions Value of an expression may be undefined Undefined operation, e.g., division by zero 3/0 has no value implementation may halt with error condition Nontermination f(x) = if x=0 then 1 else f(x-2) this is a partial function: not defined on all arguments cannot be detected at compile-time; this is halting problem These two cases are Mathematically equivalent Operationally different

Partial and Total Functions f(x) g(x) x Total function: f(x) has a value for every x Partial function: g(x) does not have a value for every x

Functions and Graphs f(x) g(x) x Graph of f = { x,y y = f(x) } Graph of g = { x,y y = g(x) } Mathematics: a function is a set of ordered pairs (graph of function)

Partial and Total Functions Total function f:a B is a subset f A B with For every x A, there is some y B with x,y f (total) If x,y f and x,z f then y=z (single-valued) Partial function f:a B is a subset f A B with If x,y f and x,z f then y=z (single-valued) Programs define partial functions for two reasons partial operations (like division) nontermination f(x) = if x=0 then 1 else f(x-2)

Computability Definition Function f is computable if some program P computes it: For any input x, the computation P(x) halts with output f(x) Terminology Partial recursive functions = partial functions (int to int) that are computable Church-Turing Hypothesis The programming language doesn t matter all reasonable programming languages define the same class of computable functions

Halting function Decide whether program halts on input Given program P and input x to P, Halt (P,x) = yes if P(x) halts no otherwise Clarifications Assume program P requires one string input x Write P(x) for output of P when run in input x Program P is string input to Halt Represent two inputs P, x as string P$x (for example) Theorem: There is no program for Halt

Unsolvability of the halting problem Suppose P solves variant of halting problem On input Q, assume yes if Q(Q) halts P(Q) = no otherwise Build program D D(Q) = run forever halt if Q(Q) halts if Q(Q) runs forever Does this make sense? What can D(D) do? If D(D) halts, then D(D) runs forever. If D(D) runs forever, then D(D) halts. CONTRADICTION: program P must not exist.

Examples Is there an algorithm to decide whether this program has a run-time type error? if f(x) then y=1+ Bob else y=2+ Alice Is there an algorithm to decide whether this program reads variable z? if f(x) then y=z+ Bob else y=z+ Alice

Main points about computability Some functions are computable, some are not Halting problem Other problems that are equivalent Programming language implementation Can report error if program result is undefined due to division by zero, other error condition Cannot warn user if program will not terminate Many useful program properties are not computable

CS 242 2012 Data Abstraction and Modularity Reading: Sections 9.1, 9.2 (except 9.2.5), and 9.3.1

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

Modularity: Basic Concepts Component Meaningful program unit Function, data structure, module, Interface Types and operations defined within a component that are visible outside the component Specification Intended behavior of component, expressed as property observable through interface Implementation Data structures and functions inside component

Example: Function Component Component Function to compute square root Interface float sqroot (float x) Specification If x>0, then sqrt(x)*sqrt(x) x. Implementation 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 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 add to set of stored elements Deletemax returns max elt and pq of remaining elts

Philosophy Build reusable program components Construct systems by divide-and-conquer Limit interactions between components Each component is assumed to satisfy spec If another component satisfies the same specification, you can replace the first by the second Internal improvements only improve the overall system, not break it

Example program using component Priority queue: structure with three operations empty insert : pq : elt * pq pq deletemax : pq elt * pq Sorting algorithm using priority queue 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 (HW?)

Component Dependencies source: Lattix.com

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

Stepwise Refinement Wirth, 1971 program... gradually developed in a sequence of refinement steps In each step, instructions are decomposed into more detailed instructions. Historical 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 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 begin end variable table p fill table p with first 1000 primes print table p

Program Structure Main Program Sub-program Sub-program Sub-program Sub-program Sub-program

Data Refinement Wirth, 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 Bank Transactions Deposit Withdraw Print Statement For level 2, represent account balance by integer variable For level 3, need to maintain list of past transactions Print transaction history

Language support for modularity Interface definition Interface may consist of types, functions, subtype relationships, other language concepts exposed to other modules Isolation Restrict dependence to factors visible through explicitly defined interface

Examples Procedural abstraction Hide functionality in procedure or function Data abstraction Hide decision about representation of data structure and implementation of operations Example: priority queue can be binary search tree or partially-sorted array

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

ML Abstype Declare new type with values and operations abstype t = <tag> of <type> with val <pattern> = <body>... fun f(<pattern>) = <body>... end Representation t = <tag> of <type> similar to ML datatype decl

Abstype for Complex Numbers Input 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 Declaration 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 Types (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

Origin of Abstract Data Types Structured 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 Research 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 Example: int Can declare variables of this type x: int Specific set of built-in operations +, -, *, No other operations can be applied to integer values Similar 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

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,...

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 provide Separate interface and implementation One interface can have multiple implementations

Haskell modules Hide and selectively export declarations Export list Declar- ations module Tree ( Tree(Leaf,Branch), fringe ) where data Tree a = Leaf a Branch (Tree a) (Tree a) fringe :: Tree a -> [a] fringe (Leaf x) = [x] fringe (Branch left right) = fringe left ++ fringe right Basic description: http://www.haskell.org/tutorial/modules.html More information: http://www.haskell.org/onlinereport/modules.html

Generic Abstractions Parameterize modules by types, other modules Create general implementations Can be instantiated in many ways Language examples: Ada generic packages, C++ templates, ML functors, ML geometry modules in supplementary readings C++ Standard Template Library (STL) provides extensive examples

Summary Modularity Interface, specification, and implementation Modular program development Step-wise refinement ; Prototyping ; Language support for modularity Procedural abstraction Abstract data types Representation independence Datatype induction Packages and modules Generic abstractions Functions and modules with type parameters Modularity is supported by object-oriented languages, but did not originate with OOP