CSE 331 Winter 2018 Homework 1

Similar documents
CSE 331 Winter 2018 Reasoning About Code I

What happens to the value of the expression x + y every time we execute this loop? while x>0 do ( y := y+z ; x := x:= x z )

Formal Reasoning CSE 331. Lecture 2 Formal Reasoning. Announcements. Formalization and Reasoning. Software Design and Implementation

Hoare Logic: Reasoning About Imperative Programs

CSE 331 Software Design & Implementation

Checkpoint Questions Due Monday, October 1 at 2:15 PM Remaining Questions Due Friday, October 5 at 2:15 PM

Proof Calculus for Partial Correctness

Axiomatic Semantics. Lecture 9 CS 565 2/12/08

Axiomatic Semantics: Verification Conditions. Review of Soundness and Completeness of Axiomatic Semantics. Announcements

Lecture 17: Floyd-Hoare Logic for Partial Correctness

Hoare Logic I. Introduction to Deductive Program Verification. Simple Imperative Programming Language. Hoare Logic. Meaning of Hoare Triples

Hoare Calculus and Predicate Transformers

Program Analysis Part I : Sequential Programs

Hoare Logic: Part II

Solutions to exercises for the Hoare logic (based on material written by Mark Staples)

Self-assessment due: Monday 3/18/2019 at 11:59pm (submit via Gradescope)

Northwest High School s Algebra 1

CS558 Programming Languages

Chapter 7: Exponents

Foundations of Computation

Hoare Logic and Model Checking

Representing signed numbers in Two s Complement notation

Northwest High School s Algebra 1

Lecture Notes: Axiomatic Semantics and Hoare-style Verification

Floyd-Hoare Style Program Verification

COP4020 Programming Languages. Introduction to Axiomatic Semantics Prof. Robert van Engelen

Axiomatic Semantics: Verification Conditions. Review of Soundness of Axiomatic Semantics. Questions? Announcements

Assignment 3 Logic and Reasoning KEY

Some Review Problems for Exam 1: Solutions

Reasoning About Imperative Programs. COS 441 Slides 10b

Math 2C03 - Class # 4. Mon. July 6th, 2015

Sample Project: Simulation of Turing Machines by Machines with only Two Tape Symbols

Last Time. Inference Rules

The Assignment Axiom (Hoare)

Axiomatic Semantics. Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11 CSE

A Short Introduction to Hoare Logic

Loop Convergence. CS 536: Science of Programming, Fall 2018

Northwest High School s Algebra 1. Summer Review Packet

Physics 2101, First Exam, Spring 2007

Student s Printed Name:

Northwest High School s Geometry

HONORS GEOMETRY SUMMER REVIEW PACKET (2012)

Hoare Logic (I): Axiomatic Semantics and Program Correctness

Assignment 5 Bounding Complexities KEY

Programming Languages and Compilers (CS 421)

Algebra I Chapter 6: Solving and Graphing Linear Inequalities

Program verification. 18 October 2017

PSU MATH RELAYS LOGIC & SET THEORY 2017

AP Physics 1 LCHS Summer Work

NTC Thermistors. Standardized R/T characteristics. Date: January 2018

Probabilistic Guarded Commands Mechanized in HOL

CSC 7101: Programming Language Structures 1. Axiomatic Semantics. Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11.

Algorithms Exam TIN093 /DIT602

CLASS NOTES: 2 1 thru 2 3 and 1 1 Solving Inequalities and Graphing

MATH 1040 Test 2 Spring 2016 Version A QP 16, 17, 20, 25, Calc 1.5, 1.6, , App D. Student s Printed Name:

Fall 2017 November 10, Written Homework 5

Math 110 (Fall 2018) Midterm II (Monday October 29, 12:10-1:00)

CIS 375 Intro to Discrete Mathematics Exam 3 (Section M004: Blue) 6 December Points Possible

Algebra Exam. Solutions and Grading Guide

SOME SMALL EXAMPLES OF FORMAL VERIFICATION

Introduction to Axiomatic Semantics

Egyptian Fractions: Part I

Hoare Logic: Reasoning About Imperative Programs

Real Analysis Notes Suzanne Seager 2015

CSCI3390-Assignment 2 Solutions

Intermediate Math Circles February 14, 2018 Contest Prep: Number Theory

To hand in: (a) Prove that a group G is abelian (= commutative) if and only if (xy) 2 = x 2 y 2 for all x, y G.

Formal Specification and Verification. Specifications

CS 336. We use the principle of inclusion and exclusion. Let B. = {five digit decimal numbers ending with a 5}, and

Deductive Verification

Designing Information Devices and Systems I Fall 2018 Homework 5

INTRODUCTION TO SIGMA NOTATION

Notes for Chapter 1 of. Scientific Computing with Case Studies

t 0. Show the necessary work and make sure that your conclusion is clear. (10 points) MTH 254 Test 1 No Calculator Portion Given: October 14, 2015

CIS 375 Intro to Discrete Mathematics Exam 3 (Section M001: Green) 6 December Points Possible

More About Turing Machines. Programming Tricks Restrictions Extensions Closure Properties

that shows the semi-decidability of the problem (i.e. a semi-decision procedure for EXISTS-HALTING) and argue that it is correct.

Soundness and Completeness of Axiomatic Semantics

Proving Programs Correct

Arithmetic and Error. How does error arise? How does error arise? Notes for Part 1 of CMSC 460

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Classical Program Logics: Hoare Logic, Weakest Liberal Preconditions

MATH 137 : Calculus 1 for Honours Mathematics. Online Assignment #2. Introduction to Sequences

Egyptian Fractions: Part I

Weakest Precondition Calculus

Algebra II (Common Core) Summer Assignment Due: September 11, 2017 (First full day of classes) Ms. Vella

Assignment 1 Physics/ECE 176

Electric Fields. Goals. Introduction

Math 10850, fall 2017, University of Notre Dame

1 Binary Classification

In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and

P.1 Prerequisite skills Basic Algebra Skills

A. Incorrect! This inequality is a disjunction and has a solution set shaded outside the boundary points.

MTH 132 Solutions to Exam 2 Apr. 13th 2015

Prepared by Sa diyya Hendrickson. Package Summary

AP Physics 1 Summer Assignment

Lesson 5b Solving Quadratic Equations

Pre-Final Round 2018

1 Backward and Forward Error

Physics Exam I

Transcription:

Directions: - Due Wednesday, January 10 by 11 pm. - Turn in your work online using gradescope. You should turn in a single pdf file. You can have more than one answer per page, but please try to avoid page breaks in the middle of a question if you can by inserting page breaks between questions as needed. - Your file should be no larger than 3MB or so. Scanned copies of hand-written documents are fine if they are legible when printed. - Feel free to rewrite the problems and solutions on a separate sheet you do not have to turn in these specific pages with the blanks filled in. - You may use any standard symbols for and and or (& and, V and ^, etc.) - If no precondition is needed for a code sequence, write {true} to denote the trivial precondition. - You should assume that all numbers are integers, that integer overflow will never occur, and that division is truncating integer division (like in Java). 1. Forward reasoning with assignment statements. Write an assertion in each blank space indicating what is known about the program state, given the precondition and the previously executed statements. Be as specific as possible. The first assertion in part (a) is supplied as an example. a. { x > 0 } x = 10; { x = 10 } y = 2 * x; z = y + 4; x = z / 2; y = 0; b. { x > 0 } y = x; y = y + 2; c. { x > 10 } x = -x; x = x / 2; x = x + 1; 1

d. { y > 2x } y = y * 2; x = x + 1; 2. Backward reasoning with assignment statements. Find the weakest precondition for each sequence using backward reasoning, and write the appropriate assertion in each blank space. a. x = x + 5; y = 2 * x; { y > 10 } b. y = x + 6; z = x + y; { z <= 0 } c. y = w 10; x = 2 * x; { x > y } d. t = 2 * s; r = w + 4; s = 2*s + w; { r > s & s > t } 2

3. Backward reasoning with if/ statements. Find the weakest precondition for the following conditional statement using backward reasoning, inserting the appropriate assertion in each blank. Be sure to verify that the intermediate postconditions for the two cases imply the total postcondition, i.e. show that (Q1 Q2) => Q. if (x >= 0) z = x; z = x + 1; {z!= 0 } 4. Weakest conditions. Circle the weakest condition in each set. a. {x = 20 } { x > 10 } { x >= 10 } b. { t = 2 } { t!= 0 } { t > 0 } c. { x > 0 & y > 0 } { x > 0 y > 0 } d. { x+y > w } { x+y > w } 5. Hoare triples. State whether each Hoare triple is valid. If it is invalid, explain why and show how you would modify the precondition or postcondition to make it valid. a. { x < 0 } y = 2*x; { y <= 0 } b. { x >= y } z = x y; { z > 0 } 3

c. {true} if (x >= 10) y = x % 7; y = x-1; { y < 9} d. { x < 0 } if (x < 100) x = -1; x = 1; { x < 0 } 6. Verifying correctness. For each block of code, fill in the intermediate assertions, then use them to state whether the precondition is sufficient to guarantee the postcondition. If the precondition is insufficient, explain why and indicate where the assertions don t match up. (Hint: for assignment statements, use backward reasoning to find the weakest precondition that guarantees the postcondition, then see if the given precondition is weaker than the weakest precondition. For conditional statements (if), you may find a combination of forward and backward reasoning most useful. Follow the rules given in class for what assertion to insert at each point.) a. { x > 0 } y = x 1; z = 2 * y; z = z + 1; { z > 1 } 4

b. { 2x >= w } y = w 2; x = 2*x; z = x 2; { z >= y } c. { y > 0 } if (x == y) x= -1; x = y - 1; { x < y } 7. Write and prove code. Write a block of code that calculates the smallest even number greater than or equal to x and stores it in y. In other words, y will be assigned either x or x+1. Assume x and y have already been initialized, and annotate your code with assertions before and after each statement to prove that it is correct. At the end of the block, it should be true that y is even and that y equals x or y equals x +1. 5