Lecture 1: Overview

Similar documents
Lecture 2: SML Basics

Lecture 3: Recursion; Structural Induction

(Refer Slide Time 3:31)

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Lecture 19: Signatures, Structures, and Type Abstraction

14.1 Encoding for different models of computation

Divisibility Rules and Their Explanations

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Fall 2018 Lecture 1

(Refer Slide Time 6:48)

Intro. Scheme Basics. scm> 5 5. scm>

How to approach a computational problem

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

6.001 Notes: Section 4.1

This book is about using Visual Basic for Applications (VBA), which is a

Week - 01 Lecture - 04 Downloading and installing Python

These are notes for the third lecture; if statements and loops.

Proofwriting Checklist

Project 1 Balanced binary

Skill 1: Multiplying Polynomials

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Mr G s Java Jive. #11: Formatting Numbers

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Programming with Math and Logic

(Refer Slide Time: 00:51)

Fractions and their Equivalent Forms

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

n = 1 What problems are interesting when n is just 1?

CS 4349 Lecture August 21st, 2017

Material from Recitation 1

Recursively Enumerable Languages, Turing Machines, and Decidability

Notebook Assignments

Chapter Fourteen Bonus Lessons: Algorithms and Efficiency

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

(Refer Slide Time 5:19)

Introduction to Programming

n! = 1 * 2 * 3 * 4 * * (n-1) * n

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Lecture 5: The Halting Problem. Michael Beeson

Reasoning About Programs Panagiotis Manolios

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

(Refer Slide Time: 1:27)

Binary Relations Part One

CS 4349 Lecture October 18th, 2017

COMP combinational logic 1 Jan. 18, 2016

An Interesting Way to Combine Numbers

EECS 203 Spring 2016 Lecture 8 Page 1 of 6

Typing Control. Chapter Conditionals

Win-Back Campaign- Re-Engagement Series

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Lecture Transcript While and Do While Statements in C++

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Induction and Semantics in Dafny

This is a book about using Visual Basic for Applications (VBA), which is a

Lecture 6: Sequential Sorting

Lab 6 Vectors and functions

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

9. MATHEMATICIANS ARE FOND OF COLLECTIONS

Lecture 11 Unbounded Arrays

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

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

Animations involving numbers

Lecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets

CS103 Handout 13 Fall 2012 May 4, 2012 Problem Set 5

Properties and Definitions

The following content is provided under a Creative Commons license. Your support

COMP 161 Lecture Notes 16 Analyzing Search and Sort

CIO 24/7 Podcast: Tapping into Accenture s rich content with a new search capability

CITS5501 Software Testing and Quality Assurance Formal methods

(Refer Slide Time: 00:01:30)

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

APCS-AB: Java. Recursion in Java December 12, week14 1

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

15-451/651: Design & Analysis of Algorithms October 11, 2018 Lecture #13: Linear Programming I last changed: October 9, 2018

Reading 1 : Introduction

Where The Objects Roam

CSCI 1100L: Topics in Computing Lab Lab 1: Introduction to the Lab! Part I

Excel Basics: Working with Spreadsheets

Lectures 24 and 25: Scheduling; Introduction to Effects

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

6.001 Notes: Section 8.1


Guidelines for Writing Mathematical Proofs

CS103 Spring 2018 Mathematical Vocabulary

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Table of Laplace Transforms

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy.

Starting Boolean Algebra

Type Checking and Type Equality

It s possible to get your inbox to zero and keep it there, even if you get hundreds of s a day.

6.001 Notes: Section 17.5

p x i 1 i n x, y, z = 2 x 3 y 5 z

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Database Management System Prof. Partha Pratim Das Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Transcription:

15-150 Lecture 1: Overview Lecture by Stefan Muller May 21, 2018 Welcome to 15-150! Today s lecture was an overview that showed the highlights of everything you re learning this semester, which also meant we didn t show it in a whole lot of detail, so don t panic if you didn t understand a lot of the things I said. Starting tomorrow we ll be going through the material at a much more reasonable pace. Today s goal is just to see the main goals of the course and get a feel for what they mean and why they re useful. 1 Functional Programming In class, I tricked you into acting out a functional program to figure out how many of you had taken 15-122. This is our first course objective: You will learn to write functional programs. Imperative programming, which 34 of you studied last semester, is all about creating and altering state. Functional programming, by comparison, is all about studying transformations on data. As the name implies, these transformations are done by functions. Imperative programmers use things called functions as well, but I mean functions more in the sense of mathematical functions, like the factorial function: factorial(5) = 120 (I ll get to what that symbol = means later.) We ve used the factorial function to transform 5 into 120. This might sound like altering state, but it s not: the number 5 still exists, which is good! In a similar way, we can define a function that counts how many students in a set have taken 122: count({{s 1,1,..., s 1,n },..., {s m,1,..., s m,n }}) = Σ m i=1σ n j=1 Based on drafts by Brandon Bohrer, Carlo Angiuli and others { 0 si,j didn t take 122 1 s i,j took 122 1

In a functional program, the students themselves don t change when we count how many of you took 122. If this had been an imperative program, we might have updated some variables in the process and would have to be careful if we wanted to make sure you weren t destroyed during the counting process. In syntax that s more like what you would actually type into the computer, the function we ran on you was count : students -> int The word count is the name of a function that we re going to define. The phrase students -> int is a type, which is a contract on the behavior of this function. The type students -> int should be read count is a function that transforms a collection of students into integer. The type doesn t say in detail what the program does, it just says it transforms students into an integer. If we want to indicate how it does this, we can use a comment: (* REQUIRES: true *) (* ENSURES: returns the number of students who took 122 *) count : students -> int Depending on the situation, sometimes our specifications will be very formal and mathematical, sometimes they will be more informal. For those of you who took 15-122, it s worth noting that unlike in C 0, which actually runs code that you put in a @requires or @ensures comment, these are just regular comments, so you can include plain English in your comments when that s the best way to document your functions. Before we figure out how to implement count, we need to explain how students are represented. Well, you re organized into rows, so let s assume that the collection of all students is a sequence of rows, which we can write in SML notation using a type declaratian: type row = int sequence type students = row sequence Likewise, a row is a sequence of integers, which are either 0 (if you didn t take 122) or 1 (if you did). (In general, I don t like reducing people to numbers, but it s convenient for this example.) The notion of a sequence is something we will spend a lot of time on this semester; for now, you can think of it as an abstract, ordered collection of things, just like you students in the back row there. When I say it s an abstract collection, this means that when we program with sequences, we don t worry about the details of how they are implemented, but instead we reason from an abstract specification telling us how sequences are supposed to behave. Programming in the abstract is perhaps the single most important skill for writing large programs. First, let s assume we have a function 2

(* ENSURES: sum r is the sum of the numbers in r *) sum : row -> int How do we use these to count the whole class? We define a function fun count (s : students) : int =... This declaration introduces a function with the above type, students -> int. How did we implement this function when we acted it out? 1. First sum each row 2. Then sum the column at the end, which after all is just another sequence of integers. Here s how this looks in ML: fun count (s : students) : int = sum (map sum s) In this code, map is an operation that applies a transformation to a sequence, producing another sequence. We (1) map sum over the rows, to transform them into the sums of their rows, then (2) sum over all the rows. Computing by calculation An important idea that we will talk about this semester is computing by calculation. As an example, we re going to work through the computation you did at the start of lecture, except we re going to use a smaller example. Suppose the class looks like row 1: yes no yes row 2: no yes yes We can represent this as follows: val row1 : row = seq [1, 0, 1] val row2 : row = seq [0, 1, 1] val classroom : students = seq [row1, row2] Here the notation seq[1, 0, 1] means we re creating a sequence with three elements: the numbers 1, 0, and 1 in that order. The notation val row1 : row =... means we re defining a new value named row1, which has type row. For now we will always write the types of values for the sake of clarity, and expect you to do the same. In a week or two we will talk about when and why you can leave types out. To count the whole classroom, we apply the function count to the classroom, as in count classroom 3

This is an expression, or a program phrase. An expression, simply put, is any piece of code that can be computed. We run a program by computing by calculation, just like in high-school algebra. To evaluate the expression count classroom, we can calculate as follows: count classroom == count (seq [row1, row2]) == sum (map sum (seq [row1, row2])) == sum (seq [sum row1, sum row2]) == sum (seq [2, 2]) == 4 Whenever I calculate the value of a program, you ll see me use this symbol = (or == in ASCII). This is a notion of equivalence for programs that we call extensional equivalence. Once I start talking about proofs, you ll hear a lot more about extensional equivalence - the idea is that when we compute by calculation, it s almost surprising how much we can say about the correctness of a program just by talking about this notion of which programs are equal to each other. This is one reason why it s so much easier to do a rigorous correctness proof in a functional language than in an imperative language. At the end of my computation I have the numeral 4, which is a value the final result of a computation. This example has illustrated the first goal of the course: you will learn to write functional programs. 2 Parallelism Earlier, we took a long time trying to figure out how many of you had taken 122, because we had to go through each row in order. Maybe it would go faster if we could compute multiple rows at the same time. In fact, there isn t really any reason we can t. This is because the calculation we re doing on each row is just a mathematical transfrmation and can t affect the calculation on any other row. If we were doing this imperatively, we d have to worry about changing values that another calculation needed. This is the essence of parallelism. By parallelism, I simply mean the ability to do many things at once. Why is parallelism important? First, most computers these days are multi-core: they have many processors, which means they can do many things at once. Second, lots of processing is done by clusters: a bunch of computers working together. This is how Google processes the Internet. So the challenge is to take advantage of these computational resources: how do you write a program in such a way that there are many things that can be done at once? One of the great things about functional programming is that, to make use of parallelism, we don t really have to change the code we wrote earlier at all! We just need to use 4

a data structure that is implemented in a way that supports parallelism (as sequences are). Once you have specified a program in such a form, the mapping to concrete hardware (e.g. the two processors in your laptop or the undisclosed number of computers in a Google data center) is taken care of by the compiler. You think about the things you want done, the compiler thinks about the order in which to do them, and the result is still well-defined. This brings us to the second objective of the course, which is really just a modification of the first. 3 Complexity Analysis The next goal of the course is: You will learn to write parallel functional programs. You will learn to analyze the sequential and parallel running time of your programs. Here are the kinds of questions we usually ask: How long does it take to count the class, if only one person can add at a time? This corresponds to execution on a single-processor machine, and is called the work of the algorithm. Answer: Well, there is one addition per student, plus the addition down the side of the classroom, but that row certainly has fewer students than the class as a whole. So the number of additions is roughly proportional to the number of students in the class. This is called the work of the computation. How long does it take to count the class, if infinitely many people can add at a time? Answer: We still have to wait for (a) the longest row to do its addition and (b) the row at the side to add up at the end. Thus the answer is roughly proportional to the the length of the longest row plus the number of rows. This is called the critical path, because it is the limiting factor, even with as many processors as you need. It is determined by the data dependencies of the computation e.g. you can t add yourself to the running total until you know the running total. The length of the critical path is called the span of the algorithm. How long does it take to count if we have p processors? We ll get to this soon. Intuitively: try to divide the work W evenly over p processors, but you can never do better than the span S. 5

Asymptotic complexity analysis allows you to predict how long it will take to run your code on really big inputs, without actually running it on them. It is one of the main tools used to choose between different algorithms for the same problem (sometimes the constant factors matter more, but usually it s the behavior when the inputs get very large that matters). 4 Sum with a better span Now that we know about work and span, let s analyze the process of adding across a row. The work is the number of students in the row, because you need to add that many numbers. The span is the same: you wait for the sum of the people to the left of you before you do anything. What if a row was really big? If a row was really big, we could divide into pairs and add numbers for the pairs, then have someone from each pair add their numbers with someone from another pair, and so on... 6

To make it easier to follow the example, we ll add up the sequence seq [1,2,3,4,5,6,7,8] (this might be the column that results from counting across 8 rows). To visualize this process, we can draw the computation as a tree of additions: + + + + + + + 1 2 3 4 5 6 7 8 What is the work? There are still 7 additions that you need to do. What about the span? If we have enough processors, we can do it like this: 7

Step 1: + + + +(3) +(7) +(11) +(15) 1 2 3 4 5 6 7 8 Step 2: + +(10) +(26) +(3) +(7) +(11) +(15) 1 2 3 4 5 6 7 8 Step 3: +(36) +(10) +(26) +(3) +(7) +(11) +(15) 1 2 3 4 5 6 7 8 8

3 steps for 8 items: the span is logarithmic in the number of students in the row. This is much better than linear when things get large. This way of summing up the tree in another common pattern of computation, and is represented by a function reduce, which breaks a sequence up into a tree and then places a binary operator (in this case +) at each node in the tree. We can implement sum using reduce. Since this implementation of sum sums the numbers up in a tree shape, we ll name it sumtree, as opposed to just sum for the version we acted out at the beginning of class. fun sumtree (s : int sequence) = reduce add 0 s 5 Reasoning The fourth goal of the course is You will learn to reason mathematically about the correctness of functional programs. For example, if we ask the question is adding up the numbers one by one (we ll call this sum) the same as adding them up using a tree (which we will call sumtree)?, we can answer it with a theorem: Theorem 1. For all rows r, sum r and sumtree r compute to the same integer. Soon, you ll learn techniques for proving this theorem formally. For now, let s get some intuition: why should this be true? Returning to the example of seq[1,2,3,4,5,6,7,8], we see that sum computes 1 + (2 + (3 + (4 + (5 + (6 + (7 + (8 + 0))))))) whereas sumtree computes ((1 + 2) + (3 + 4)) + ((5 + 6) + (7 + 8)) Thus, the basic reason is that addition is associative (x + y) + z = x + (y + z), with unit 0 (x + 0 = x). More formally, we can do a proof by structural induction on the sequence r. Don t worry if you don t understand this term yet; you will by the end of the week. Why do you need to be able to reason about your code like this? For one thing, this will help you write better software. Thinking through invariants this way will make it easier to write code. For example, like we saw with sum and sumtree, it s often the case that there is a simple, obviously correct, but inefficient algorithm for a problem; and also a more complex algorithm that s more efficient. Proving them equivalent mathematically justifies your program optimization. You could do this through intuition and testing, but those aren t perfect. This way, you know your code is correct. 9

It s also possible that someday you might have to write safety-critical systems, where you really want to know that your program doesn t make a mistake. For example, airplanes these days are basically just flying computer programs. Most of us want the code running on airplanes to be correct, so it would make us feel better if we knew that someone had proven that code correct. The techniques used in provng this kind of code correct are a bit out of scope for an introductory course, but the point is that the techniques you re learning now are the foundation for proofs that can actually save lives. 6 Course Overview To summarize, you will learn to: Write parallel functional programs Analyze their sequential and parallel time complexity Reason mathematically about their correctness The thesis of the course is that functional programming is a good way to do all of this: it makes it easy to express programs (cleanly, elegantly), analyze their time complexity, and verify their correctness. And moreover that this all applies to parallel programs: you can easily specify work that can be done in parallel, and analyze a program s parallel complexity, and prove correctness about parallel executions (indeed, in this course, it will (almost) always have the same result as a sequential execution!). 6.1 Logistics See the course website (http://www.cs.cmu.edu/ 15150/policy.html) for the course policies we covered in class. Make sure you have an account on Piazza. We ll be going over the rest of the course infrastructure in lab on Wednesday. 10