Today’s Agenda  Correctness Issues. Why Correctness?  Programming is engineering Program is a product Program quality to be determined during production.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Muller ‘ s mothod Hun Hee Lee. Muller ’ s method Muller ’ s method for solving an equation of one variable f(x)=0. Muller ’ s method is an iterative method.
Reasoning About Code; Hoare Logic, continued
Announcements We are done with homeworks Second coding exam this week, in recitation –Times will be posted later today –If in doubt, show up for your regular.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
© Janice Regan Problem-Solving Process 1. State the Problem (Problem Specification) 2. Analyze the problem: outline solution requirements and design.
Software Engineering and Design Principles Chapter 1.
Chapter 1 pp 1-14 Properties of Algorithms Pseudocode.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Chapter 1 pp 1-14 Properties of Algorithms Pseudocode.
Validating High-Level Synthesis Sudipta Kundu, Sorin Lerner, Rajesh Gupta Department of Computer Science and Engineering, University of California, San.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Fall 2008 Insertion Sort – review of loop invariants.
Chapter 1 Principles of Programming and Software Engineering.
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Proving Program Correctness The Axiomatic Approach.
Proving Program Correctness The Axiomatic Approach.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Reading and Writing Mathematical Proofs
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
Software Reliability. Risks of faulty software  Example: –Therak 25, –AT&T network failure –Airport traffic control  Costs of software errors can be.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
CPSC 873 John D. McGregor Session 9 Testing Vocabulary.
Matlab Programming for Engineers
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
Problem Session Working in pairs of two, solve the following problem...
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Solving Quadratic Equations. Factor: x² - 4x - 21 x² -21 a*c = -21 b = -4 x + = -21 = x 3x3x x 3 (GCF) x-7 (x – 7)(x + 3)
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Discrete Maths: Invariant/2 1 Discrete Maths Objectives – –to show the use of induction for proving properties of code involving loops use induction.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
C++ for Engineers and Scientists, Second Edition 1 Problem Solution and Software Development Software development procedure: method for solving problems.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
Principles of Programming & Software Engineering
SWEN421 – Lecture 3 Building High Integrity Software with SPARK Ada
Principles of Programming and Software Engineering
Formal Methods in Software Engineering 1
The Quadratic Formula.
CS 220: Discrete Structures and their Applications
Programming Languages 2nd edition Tucker and Noonan
Quadratic Equations.
Loop Construct.
Output Variables {true} S {i = j} i := j; or j := i;
EECE.2160 ECE Application Programming
Program correctness Axiomatic semantics
CSE 1020:Software Development
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Today’s Agenda  Correctness Issues

Why Correctness?  Programming is engineering Program is a product Program quality to be determined during production process Program Correctness is one essential aspect of quality

 Design Correctness Solution (design) meets requirements Verified offline (often on paper) Proof arguments  Implementation Correctness Implementation (program code) matches design Verified online (often by execution) Tests and Test Cases

 Output of Design step: Program Design High level solution to problem Consists of modules and module interconnections Modules are solutions to sub-problems Interconnections capture ways to combine sub-solutions

Module Correctness  Often reduces to algorithm correctness: Algorithm should terminate Algorithm should produce required result when it terminates.  Both arguments are fairly easy for “straight- line” programs – i.e., no loops.

Module Correctness  e.g., Define function to compute square (x) as return x * x  It is easy to verify the requirements: It terminates: a single step It computes the square of a given number: x 2 =x*x

Module Correctness  Similar argument can be applied for more than one step as well.  Problem: Given a, b, and c, solve quadratic equation – a*x 2 + b*x + c = 0 Solution X1 = (-b + √b 2 – 4ac ) / 2a X2 = (-b - √b2 – 4ac ) / 2a

Module Correctness  Solution: Define function quad(a, b, c, sign) disc = b*b – 4*a*c; if (sign) return (-b + sqrt(disc)) / (2 *a); else return (-b – sqrt(disc)) / (2*a);

Module Correctness  Termination: if sqrt terminates, quad() function terminates.  Valid results: if sqrt is correct then quad() returns correct value.  How to verify this? Contracts

Inter-Module Correctness  Whoever writes sqrt function, specifies input- output contract: /* Pre-condition: m > 0 Post-condition: return n such that |n * n – m| / m <.01 */ float sqrt(float m)  argument must be +ve definite

Inter-Module Correctness  Observation: Precondition m > 0 This is required for sqrt to be correct (or may be even to terminate). So, quad module must guarantee before invocation of sqrt: disc > 0

Inter-Module Correctness  The previous contract may propagate up: /* Pre-condition: b*b > 4*a*c Post-condition: return x such that | a*x*x + b*x + c | <= epsilon */ float quad(float a, float b, float c, int sign)

Note:  Function interfacing errors minimized due to the Pre -conditions and Post conditions.

Why do we need correctness ?  Testing will be easy -Helpful to the third party users  When you write a function you should write: /* Pre-condition : ……. */ /* Post-condition: …….*/

Module Correctness for sqrt(x) R = x/2 Err = abs(R * R – x) Err <.01? Yes return R R= (R + x/R)/2 Err = abs(R*R – x) No

Illustration: sqrt(9) IterationR next Err next =R 2 –x 0=x/2=9/2= <0.01? N 1(4.5+9/4.5)/2= <0.01N [ = ] 2 (3.25+9/3.25)/2 = <0.01N [ ] 3 ( / )/2 =3.00 R next ~ sqrt(9) <0.01 Y Exits from while

Functions for quadratic eqn int main() { float a,b,c,x1,x2; scanf("%f %f %f",&a,&b,&c); x1 = quad(a,b,c,1); x2 = quad(a,b,c,-1); printf("x1 = %f x2 = %f\n",x1,x2); return(0); }

float quad(float a,float b,float c,int sign) { float disc,res ; disc= b*b - 4*a*c; if(disc > 0) { if(sign) res = (-b + sqrt(disc))/(2*a); else res = (-b - sqrt(disc))/(2*a); return (res); } return(-1.0); }

float sqrt(float x) { float r,err; r = x/2.0; err = abs(r*r - x); while((err > 0.01)||(err == 0.01)) { r = (r + x/r)/2; err = abs(r*r - x); } printf("sqrt of disc is %f\n",r); return (r); } }

What is a loop invariant?  Property that is maintained “invariant” by iterations in a loop.  How is it used? Verify before the loop Verify each iteration preserves it. Property on termination of loop must result in “what is expected”

Loop Invariants: Method to prove correctness of loops  Loop has the following appearance: [pre-condition for loop] while (Guard) [Statements in body of loop. Branching statements that lead outside the loop.] end while [post-condition for loop]

Loop Invariance (Example 1 ) Program to do sum of the elements in an array. Sum() { int s = 0; i = 0; // pre condn : s=0 && i=0 While(i < n) { // s is the sum of first i array elements // s = a[0] + … + a[i-1] s = s + a[i]; i = i + 1; } //post condn s = a[0] + ….+ a[i-1] Return s; //post cond : s = a[0] + ………+ a[n-1]

Loop Invariance (Example 2 )  Factorial (int n) fact (int n)// pre condn n>=0 { int i =1, fact = 1; // pre cond while(i<=n) { // invariance here is (fact = (i-1)! && i >0) fact = fact * i; i = i+1; } // post cond: fact = (i-1)! && i > 0 return fact; // post cond: fact = (n+1- 1)! }