Specifying an ADT Give function specification. Identify partial functions A function which cannot be applied on all of its possible parameters Categorize.

Slides:



Advertisements
Similar presentations
THE WELL ORDERING PROPERTY Definition: Let B be a set of integers. An integer m is called a least element of B if m is an element of B, and for every x.
Advertisements

Introduction to Proofs
To prove by induction that 2 n ≥ n 2 for n ≥ 4, n  N Next (c) Project Maths Development Team 2011.
We want to prove the above statement by mathematical Induction for all natural numbers (n=1,2,3,…) Next SlideSlide 1.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Scientific Notation.
© 2003, Yishai Feldman Proving Sufficient Completeness Example: Set ADT.
Reasoning About Code; Hoare Logic, continued
© M. Winter COSC 4P41 – Functional Programming Testing vs Proving Testing –uses a set of “typical” examples, –symbolic testing, –may find errors,
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 3: Abstract Data Types.
Comparing Integers Lesson
Topic 15 Implementing and Using Stacks
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
Design by Contract. Specifications Correctness formula (Hoare triple) {P} A {Q} – A is some operation (for example, a routine body) – P and Q are predicates.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Lecture 2: Fundamental Concepts
Chair of Software Engineering OOSC - Lecture 4 1 Object-Oriented Software Construction Bertrand Meyer.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Topic 15 Implementing and Using Stacks
Chapter 10 Sequences, Induction, and Probability Copyright © 2014, 2010, 2007 Pearson Education, Inc Mathematical Induction.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSE115/ENGR160 Discrete Mathematics 03/29/11 Ming-Hsuan Yang UC Merced 1.
analysis, plug ‘n’ chug, & induction
Induction and recursion
Lesson 6-3 Example Example 1 Find the difference of –2 and –4. Use algebra tiles. 1.Write the subtraction expression. –2 – (–4)
Expressions, Data Conversion, and Input
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Lesson 8-1 Negative & Zero. Your Goal: Simplify expressions containing integer exponents.
Reading and Writing Mathematical Proofs
CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 1 Lecture 18 CS 1813 – Discrete Mathematics Loops Without Invariants Are Like.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
Planning, page 1 CSI 4106, Winter 2005 Planning Points Elements of a planning problem Planning as resolution Conditional plans Actions as preconditions.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
9.4 Mathematical Induction
Lesson 6-3 Example Example 2 Find the difference of –2 and –4. Use counters. 1.Write the subtraction expression. –2 – (–4)
Concept Learning and the General-to-Specific Ordering 이 종우 자연언어처리연구실.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
To prove by induction that 3 is a factor of 4 n - 1, n  N Next (c) Project Maths Development Team 2011.
Scientific Notation How to write in scientific notation.
1+2+3+…+n = n(n+1)/2 We want to prove the above statement by mathematical Induction for all natural numbers (n=1,2,3,…) Next SlideSlide 1.
Understanding ADTs CSE 331 University of Washington.
Planning I: Total Order Planners Sections
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Java-02 Basic Concepts Review concepts and examine how java handles them.
MTH 231 Section 5.2 Addition and Subtraction of Integers.
5-5 Indirect Proof. Indirect Reasoning: all possibilities are considered and then all but one are proved false. The remaining possibility must be true.
Add and Subtract Polynomial Expressions. Question 1.
Comparing Numbers Adding Positive & Negative Numbers Subtracting Positive & Negative Numbers Algebraic Expressions Miscellaneous 100.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Mathematical Induction. The Principle of Mathematical Induction Let S n be a statement involving the positive integer n. If 1.S 1 is true, and 2.the truth.
Recursion Powerful Tool
Goal: use counters to add integers
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
6.5 Indirect proof inequalities in one triangle
Axiomatic semantics Points to discuss: The assignment statement
With Assignment Operator
COMPUTER 2430 Object Oriented Programming and Data Structures I
Lists Chapter 8.
Review Mathematics Skills
This Lecture Substitution model
Assignment Operators Topics Increment and Decrement Operators
11.4 Mathematical Induction
Presentation transcript:

Specifying an ADT Give function specification. Identify partial functions A function which cannot be applied on all of its possible parameters Categorize functions into: creator: creates a new ADT query: returns a value other than the ADT command: receives a value of the new ADT and returns a value of the new ADT Give preconditions for partial functions Give axioms to describe ADT behavior.

Preconditions Every partial function should have a precondition. If a function cannot return a value or should not be applied in a specific situation, it must have a precondition. Item-at(s:stack[G], i:integer) - cannot be applied unless 0 < i <= count() remove(s: stack[G]) – cannot be applied unless !empty(s)

Axioms Write axioms to describe the behavior of the ADT. The axioms are used for reasoning and for proving sufficiently completeness, therefore they should simplify expressions. You should have an axiom for every query applied on every command. If necessary add axioms of commands applied on commands. The expression on the right hand side of the axiom should be simpler than the one on the left hand side. In axioms, a simpler expression is shorter than a more complicated one: item-at(put(s, x), i) = item-at(s, i-1)

Forgetful Stack make: INTEGER FS[G] capacity: FS[G]  INTEGER item: FS[G] G put: FS[G]  G FS[G] remove: FS[G] FS[G] count: FS[G]  INTEGER item-at: FS[G]  INTEGER G AXIOMS A1.capacity(make(n)) = n A2.capacity(put(s, x)) = capacity(s) A3.capacity(remove(s)) = capacity(s) A4.count(make(n)) = 0 A5.count(put(s,x))=min(count(s)+1,capacity(s)) A6.count(remove(s)) = count(s) –1 A7.item(s) = item-at(s, 1) A8.item-at(put(s, x), 1) = x A9.i>1  item-at(put(s, x), i) = item-at(s, i-1) A10.item-at(remove(s), i) = item-at(s, i+1)      Preconditions make(n) require n  0 item(s) require count(s) > 0 put(s, x) require capacity(s) > 0 remove(s) require count(s) > 0 item-at(s, i) require 1  i  count(s)

Sufficiently complete Show correctness (explained later) Show that every query q i applied on every command cmd j reduces the expression. Use q i (make(n)) as base case for the induction. Assume that applying q i on an expression of size n returns a value. Show, using axioms, for every cmd j how to reduce q i (cmd j (s)) to q i (s) - the induction step.

Forgetful stack example q 1 = capacity cmd 1 = make(n) - Base case q 1 (cmd 1 ) = capacity(make(n)) = n (By Axiom 1) cmd 2 = put(s, x) q 1 (cmd 2 ) = capacity(put(s, x)) = capacity(s) (By Axiom 2) cmd 3 = remove(s) q 1 (cmd 3 ) = capacity(remove(s)) = capacity(s) (By Axiom 3)

Correctness Given expression e, prove using preconditions and axioms that every subexpression of e is correct. Proof by induction on the weight of expression e. Define a weight function on the functions of the ADT. Each ADT has its own weight function.

Correctness The creator is of zero weight - Base case Give a weight to every command. In the stack example: put increments the weight by 1 remove decrements the weight by 1 Assume the correctness of an expression e. Prove by induction by applying a command on an assumed correct expression s. The weight of s >= 0 (by the assumption)

Correctness Show that the weight of e 1 = cmd i (s) is non negative for every command cmd i. If cmd i has no precondition, like put(s, x) then e 1 = put(s, x) is correct, s assumed correct (non negative weight)  e 1 has non negative (positive) weight. Else, the outermost (last) command in e can be one of cmd j : cmd i  cmd j. e 1 = cmd i (s)  s = cmd j (s’)  e 1 = cmd i (cmd j (s ’ )) e 1 = remove(s)  s = put(s’, x)  e 1 = remove(put(s’, x)) Show using preconditions and axioms how the weight of e 1 remains non negative.