Specification and Verification in Introductory Computer Science Frank Pfenning Carnegie Mellon University MSR-CMU Center for Computational Thinking May.

Slides:



Advertisements
Similar presentations
Advanced programming tools at Microsoft
Advertisements

Writing specifications for object-oriented programs K. Rustan M. Leino Microsoft Research, Redmond, WA, USA 21 Jan 2005 Invited talk, AIOOL 2005 Paris,
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Data Structure & Abstract Data Type
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Semantics Static semantics Dynamic semantics attribute grammars
Addressing the Challenges of Current Software. Questions to Address Why? What? Where? How?
Introduction to Computer Science 2 Lecture 7: Extended binary trees
COL 106 Shweta Agrawal and Amit Kumar
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
CSE 830: Design and Theory of Algorithms
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Data Structures, Spring 2004 © L. Joskowicz 1 DAST – Final Lecture Summary and overview What we have learned. Why it is important. What next.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Marc Smith and Jim Ten Eyck
PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Spring 2015.
Mathematics throughout the CS Curriculum Support by NSF #
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Instructor: Dr. Sahar Shabanah Fall Lectures ST, 9:30 pm-11:00 pm Text book: M. T. Goodrich and R. Tamassia, “Data Structures and Algorithms in.
Language Evaluation Criteria
Teaching Teaching Discrete Mathematics and Algorithms & Data Structures Online G.MirkowskaPJIIT.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Introduction Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture 10: Class Review Dr John Levine Algorithms and Complexity March 13th 2006.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Refinements to techniques for verifying shape analysis invariants in Coq Kenneth Roe GBO Presentation 9/30/2013 The Johns Hopkins University.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
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.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Principles of Imperative Computation Lecture 1 January 15 th, 2012.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
CSCE 210 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
Introduction to Computers Computer Generations
Data Structures and Algorithms for Information Processing
Top 50 Data Structures Interview Questions
Midterm Review.
Heaps And Priority Queues
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Week 15 – Monday CS221.
Hashing Exercises.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
structures and their relationships." - Linus Torvalds
Lesson Objectives Aims
Principles of Imperative Computation
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
(1 - 2) Introduction to C Data Structures & Abstract Data Types
CSCE156: Introduction to Computer Science II
structures and their relationships." - Linus Torvalds
Presentation transcript:

Specification and Verification in Introductory Computer Science Frank Pfenning Carnegie Mellon University MSR-CMU Center for Computational Thinking May 14, c0.typesafety.net Principal Contributors: Rob Arnold, Tom Cortina, Ian Gillis, Jason Koenig, William Lovas, Karl Naden, Rob Simmons, Jakob Uecker MSR Collaborators: Rustan Leino, Nikolaj Bjørner MS Guest Speaker: Jason Yang (Windows team) MSR Inspiration: Manuvir Das, Peter Lee

Outline Background and guiding principles Role in the curriculum Learning goals C0 Language (Lecture sample) Research plans May 14, 20122MSR-CMU Center for Computational Thinking

Background Principles of Imperative Computation Part of a major core CS curriculum revision – /112 Principles of Computing/Programming – Principles of Functional Programming – Parallel & Sequential Data Structs. & Algs. – Software System Construction Still under development – Pilot in Fall 2010, every semester since then – Now taught ~630 students, majors & non-majors – Adoption at Tübingen, Germany, Spring 2012 May 14, 20123MSR-CMU Center for Computational Thinking

Core Curriculum Chart Fundamentals of Programming Principles of Imperative Computation Principles of Functional Programming Software System Construction Seq. & Par. Data Structs. & Algs Computer Systems Great Ideas of Theoretical CS Concepts of Modern Math May 14, 20124MSR-CMU Center for Computational Thinking Principles of Computing

Guiding Principles Computational thinking and programming must go hand-in-hand Algorithm and data structure design, analysis, and implementation is an intellectual activity We build abstractions from an understanding of the concrete Rigorous types, invariants, specifications, interfaces are crucial May 14, 20125MSR-CMU Center for Computational Thinking

Role in the New Curriculum Precondition: some programming experience – Self-taught or high-school programming or – Python and Java most common – Broad rather than deep; diverse Postcondition: preparation for 15-2xx courses – : Par. & Seq. Data Structs. & Algs. – : Computer Systems – : Software System Construction May 14, 20126MSR-CMU Center for Computational Thinking

Learning Goals Computational thinking Programming skills Specific data structures and algorithms Application contexts May 14, 20127MSR-CMU Center for Computational Thinking

Computational Thinking Algorithmic concepts vs. code Abstraction and interfaces Specification vs. implementation Pre- and post-conditions, loop invariants Data structure invariants Logical and operational reasoning Asymptotic complexity and practical efficiency Programs as data Exploiting randomness May 14, 20128MSR-CMU Center for Computational Thinking

Programming Skills Deliberate programming Understand static and dynamic semantics Develop, test, debug, rewrite, refine Invariants, specifications Using and designing APIs Use and implement data structures – Emphasis on mutable state (“RAM model”) Render algorithms into correct code – Emphasis on imperative programming May 14, 20129MSR-CMU Center for Computational Thinking

Some Algorithmic Concepts Asymptotic analysis – Sequential computation – Time and space – Worst-case vs. average-case – Amortized analysis – Common classes: O(n), O(n*log(n)), O(n 2 ), O(2 n ) Divide and conquer Self-adjusting data structures Randomness Sharing May 14, MSR-CMU Center for Computational Thinking

Specific Alg’s and Data Struct’s Binary search Sorting (selection sort, mergesort) Stacks and queues Hash tables Priority queues (heaps) Binary search trees (red/black, randomized) Tries Binary decision diagrams (SAT, validity) Graph traversal (depth-first, breadth-first) Minimum spanning trees (Prim’s alg., Kruskal’s alg.) Union-find May 14, MSR-CMU Center for Computational Thinking

Application Contexts See algorithms in context of use Engage students’ interest Assignments (all written+programming) – Image manipulation – Text processing (Shakespeare word freq’s) – Grammars and parsing – Maze search – Huffman codes – Puzzle solving (Lights Out) – Implementing a virtual machine (C0VM) May 14, MSR-CMU Center for Computational Thinking

Language Weeks 1-10: Use C0, a small safe subset of C, with a layer to express contracts – Garbage collection (malloc/free) – Fixed range modular integer arithmetic – Language unambiguously defined – Contracts as boolean expressions Weeks 11-14: Transition to C – Exploit positive habits, assertion macros – Pitfalls and idiosyncrasies of C May 14, MSR-CMU Center for Computational Thinking

Type Structure t = int | bool | struct s | t* | t[] Distinguish pointers and arrays Distinguish ints and booleans Structs and arrays live in memory; ints, bools, and pointers in variables Strings and chars as abstract types May 14, MSR-CMU Center for Computational Thinking

Control Structure Variables and assignment Separation of expressions and statements Conditionals, while, and for loops Functions Other considerations – Minimal operator overloading – No implicit type conversions – Initialization May 14, MSR-CMU Center for Computational Thinking

Rationale for C0 Imperative implementations of simple algorithms are natural in this fragment Simplicity permits effective analysis – Proving invariants, sound reasoning Concentrate on principles first, C later Industrial use of assertions (SAL, Spec#) – Guest lecture by J. Yang from MS Windows team May 14, MSR-CMU Center for Computational Thinking

Lecture Example In actual lecture use blackboard, plus laptop for writing code Recurring theme Computational Thinking Data Structures and Algorithms Programming May 14, MSR-CMU Center for Computational Thinking

Lecture 13: Priority Queues Lecture 14: Restoring Invariants Restoring Invariants Priority Queues Heaps Trees as Arrays May 14, MSR-CMU Center for Computational Thinking

Heap Interface May 14, 2012MSR-CMU Center for Computational Thinking19 typedef struct heap* heap; bool heap_empty(heap H); /* is H empty? */ heap heap_new(int limit) /* create new heap */ limit > 0; heap_empty(\result); ; void heap_insert(heap H, int x); /* insert x into H */ int heap_min(heap H) /* find minimum */ !heap_empty(H); ; int heap_delmin(heap H) /* delete minimum */ !heap_empty(H); ;

Checking Heap Invariants May 14, 2012MSR-CMU Center for Computational Thinking20 struct heap { int limit; int next; int[] heap; }; bool is_heap(heap H) H != NULL && \length(H->heap) == H->limit; { if (!(1 next && H->next limit)) return false; for (int i = 2; i next; i++) if (!(H->heap[i/2] heap[i])) return false; return true; }

Heap Insertion May 14, 2012MSR-CMU Center for Computational Thinking21 void heap_insert(heap H, int x) is_heap(H); !heap_full(H); is_heap(H); { H->heap[H->next] = x; H->next++; sift_up(H, H->next-1); }

Preliminary Course Assessment First four course instances successful – Covered envisioned material and more – Excellent exam and assignment performance – Lecture notes – Positive student feedback Interesting programming assignments – Using our C0 compiler weeks 1-10, gcc thereafter – Linked with C/C++ libraries May 14, MSR-CMU Center for Computational Thinking

Some Course Tools C0 to C compiler (cc0), front end v3 C0 interpreter and debugger (new Su 2012) C0 tutorial C0 language reference Binaries for Windows, Linux, Mac OS X C0VM for last assignment May 14, MSR-CMU Center for Computational Thinking

Contracts Currently, contracts are checked dynamically if compiled with cc0 -d Contracts are enormously useful – Bridge the gap from algorithm to implementation – Express programmer intent precisely – Catch bugs during dynamic checking – Debugging aid (early failure, localization) Not easy to learn effective use of contracts May 14, MSR-CMU Center for Computational Thinking

Exploiting Contracts Further Ongoing research Contracts could be even more useful – Static verification (MSR: Daphne, Boogie, Z3) – Counterexample, test generation (MSR: Pex) – Autograding of code and contracts The educational setting creates unique challenges and opportunities – Explainable static analysis (null ptrs, array bounds) – Pedagogically sound design – Diversity of student programmers May 14, MSR-CMU Center for Computational Thinking

Summary Principles of Imperative Computation Freshmen-level course emphasizing the interplay between computational thinking, algorithms, and programming in simple setting C0, a small safe subset of C, with contracts Contracts pervasive, checked only dynamically Research question: can we use static analysis and theorem proving to aid in achieving student learning goals? MSR collaborators: Rustan Leino, Nikolaj Bjørner Visits: F. Pfenning, J. Yang, R. Leino, K. Naden, J. Koenig, May 14, 2012MSR-CMU Center for Computational Thinking26

Priority Queues Generalizes stacks and queues Abstract interface – Create a new priority queue – Insert an element – Remove a minimal element May 14, MSR-CMU Center for Computational Thinking

Heaps Alternative 1: unsorted array – Insert O(1), delete min O(n) Alternative 2: sorted array – Insert O(n), delete min O(1) Alternative 3: heap – Partially sorted! May 14, MSR-CMU Center for Computational Thinking

Heaps A heap represents a priority queue as a binary tree with two invariants Shape: – Tree is complete (missing nodes bottom-right) Order: – Each interior node is greater-or-equal to its parent – OR: each node is less-or-equal to all its children Guarantee a minimal element is at root! May 14, MSR-CMU Center for Computational Thinking

1 node2 nodes3 nodes4 nodes 5 nodes6 nodes7 nodes Shape Invariant May 14, MSR-CMU Center for Computational Thinking

Inserting into Heap By shape invariant, know where new element should go Now have to restore ordering invariant May 14, MSR-CMU Center for Computational Thinking

Order invariant satisfied, except between new node and its parent (“looking up”) Swapping with parent will restore locally Parent may now violate invariant Sifting Up May 14, MSR-CMU Center for Computational Thinking

Invariant Restored When reaching root (no parent!), ordering invariant restored everywhere We have a valid heap! May 14, MSR-CMU Center for Computational Thinking

Analysis Insert requires O(log(n)) swaps in worst case Logarithmic thinking: complete binary tree with n elements has 1+log(n) levels Delete min also O(log(n)), omitted here May 14, 2012MSR-CMU Center for Computational Thinking34

Exploit shape invariant Binary numbering system for nodes, root is 1 Left child is n0 = 2*n; right child is n1 = 2*n+1 Parent is at n/2; 0 is unused Heaps as Arrays May 14, MSR-CMU Center for Computational Thinking

Creating a Heap May 14, 2012MSR-CMU Center for Computational Thinking36 heap heap_new(int limit) 1 <= limit; is_heap(\result) && heap_empty(\result); { heap H = alloc(struct heap); H->limit = limit; H->next = 1; H->heap = alloc_array(int, limit); return H; } Pre/postconditions are easy to specify and reason about How to automate?

Almost a Heap May 14, 2012MSR-CMU Center for Computational Thinking37 bool is_heap_except_up(heap H, int n) H != NULL && \length(H->heap) == H->limit; { int i; if (!(1 next && H->next limit)) return false; for (i = 2; i next; i++) if (!(i == n || H->heap[i/2] heap[i])) return false; return true; } // is_heap_except_up(H, 1) == is_heap(H); Captures permitted violation precisely Observation at end for postcondition of sift_up

Sifting Up / Restoring Invariant May 14, 2012MSR-CMU Center for Computational Thinking38 void sift_up(heap H, int n) 1 limit; is_heap_except_up(H, n); is_heap(H); { int i = n; while (i > 1) is_heap_except_up(H, i); { if (H->heap[i/2] heap[i]) return; swap(H->heap, i/2, i); i = i/2; } i == 1; is_heap_except_up(H, 1); return; }