Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.

Slides:



Advertisements
Similar presentations
Propositional Equivalences
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
PROOF BY CONTRADICTION
CSE 311: Foundations of Computing Fall 2013 Lecture 3: Logic and Boolean algebra.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
22C:19 Discrete Structures Logic and Proof Spring 2014 Sukumar Ghosh.
Chair of Software Engineering ATOT - Lecture 10, 5 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 10: Project Presentation Ilinca.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
Static and Dynamic Contract Verifiers For Java Hongming Liu.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Discrete Mathematics Math 6A Instructor: M. Welling.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Describing Syntax and Semantics
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Predicates and Quantifiers
Component-Based Software Engineering Components and Interfaces Paul Krause.
Discrete Mathematics Goals of a Discrete Mathematics Learn how to think mathematically 1. Mathematical Reasoning Foundation for discussions of methods.
XOR and XNOR Logic Gates. XOR Function Output Y is TRUE if input A OR input B are TRUE Exclusively, else it is FALSE. Logic Symbol  Description  Truth.
MATH 224 – Discrete Mathematics
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Design by Contract in Java Concept and Comparison.
Contracts for Concurrency - Contracts & Inheritance Aryabrata Basu University of Georgia.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
MATH 224 – Discrete Mathematics
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 5: Invariants and Logic.
Predicates and Quantified Statements
Chapter 2 Logic 2.1 Statements 2.2 The Negation of a Statement 2.3 The Disjunction and Conjunction of Statements 2.4 The Implication 2.5 More on Implications.
Chapter 1: The Foundations: Logic and Proofs
Boolean Values The true story ;=P. Expressions.
1 Devon M. Simmonds, Computer Science Department Design by Contract Devon M. Simmonds Computer Science Department University of North Carolina, Wilmington.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
PREDICATES AND QUANTIFIERS COSC-1321 Discrete Structures 1.
Thinking Mathematically Logic 3.4 Truth Tables for the Conditional and Biconditional.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
Propositional Logic. Assignment Write any five rules each from two games which you like by using propositional logic notations.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Lecture 03 Logic and Propositional Calculus Profs. Koike and Yukita
Discrete Mathematics Lecture # 2.
Propositional Equivalences
Einführung in die Programmierung Introduction to Programming Prof. Dr
CSE 311 Foundations of Computing I
Program correctness Axiomatic semantics
Presentation transcript:

Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008

Chair of Software Engineering 2 Today’s learning goals  A bit of logic  Understanding contracts (preconditions, postconditions, and class invariants)

Chair of Software Engineering Logic  Tautology  True for all truth assignments  a or (not a)  not (a and (not a))  (a and b) or ((not a) or (not b))  Contradiction  False for all truth assignments  a and (not a) 3

Chair of Software Engineering Logic  Satisfiable  True for at least one truth assignment  Equivalent  u and v are equivalent if they are satisfied under exactly the same truth assignments, or if u = v is a tautology 4

Chair of Software Engineering 5 Truth tables Does the following equivalence hold? (P implies Q) = (not P implies not Q) Hands-On

Chair of Software Engineering 6 Tautology / contradiction / satisfiable?  P or Q satisfiable  P and Q satisfiable  P or (not P) tautology  not P satisfiable  Integer (i) implies (i = 0) tautology Hands-On

Chair of Software Engineering 7 Useful stuff De Morgan laws  not (P or Q) = (not P) and (not Q)  not (P and Q) = (not P) or (not Q) Implications  P implies Q= (not P) or Q  P implies Q = (not Q) implies (not P) Equality on Boolean expressions  (P = Q) = (P implies Q) and (Q implies P)

Chair of Software Engineering 8 Existential and universal quantification  There exists a human whose name is Bill Gates  h: Human | h.name = “Bill Gates”  All persons have a name  p: Person | p.name /= Void  Some people are students  p: Person | p.is_student  The age of any person is at least 0  p: Person | p.age >= 0  Nobody likes Rivella not (  p: Person | p.likes (Rivella))

Chair of Software Engineering From the lecture… Semi-strict operators ( and then, or else )  a and then b  has same value as ( a and b ) if a and b are defined, and has value False whenever a has value False.  a or else b  has same value as ( a or b ) if a and b are defined, and has value True whenever a has value True. 9

Chair of Software Engineering 10 Assertions balance_non_negative: balance >= 0 Assertion Assertion tag (not required, but recommended) Condition (required)

Chair of Software Engineering clap (n: INTEGER) -- Clap n times and update count. require not_too_tired: count <= 10 n_positive: n > 0 Property that a feature imposes on every client A feature with no require clause is always applicable, as if the precondition reads require always_OK: True 11 Precondition

Chair of Software Engineering Property that a feature guarantees on termination A feature with no ensure clause always satisfies its postcondition, as if the postcondition reads ensure always_OK: True clap (n: INTEGER) -- Clap n times and update count. require not_too_tired: count <= 10 n_positive: n > 0 ensure count_updated: count = old count + n 12 Postcondition

Chair of Software Engineering 13 Obligations and guarantees  Precondition  Obligation on client: I must not call unless …  Guarantee for supplier: I know that … when I start executing  Postcondition  Obligation on supplier: I must establish … when I terminate  Guarantee for client: I can assume … after the call

Chair of Software Engineering 14 Invariant Property that is true of the current object at any observable point A class with no invariant clause has a trivial invariant always_OK: True class ACROBAT … invariant count_non_negative: count >= 0 end

Chair of Software Engineering 15 Pre- and postcondition example Add pre- and postconditions to: square_root (i: INTEGER): REAL -- Square root of `i’ -- with precision `eps’ (given somewhere else). do... end Hands-On

Chair of Software Engineering 16 One possible solution square_root (i: INTEGER): REAL is -- Square root of `i’ -- with precision `eps’ (given somewhere else) require i_positive: i >= 0 ensure correct_result: i – eps <= Result * Result and i + eps >= Result * Result end

Chair of Software Engineering 17 Hands-on exercise  Add invariants to classes ACROBAT_WITH_BUDDY and CURMUDGEON.  Add preconditions and postconditions to feature make_with_buddy in ACROBAT_WITH_BUDDY. Hands-On