Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 5: Invariants and Logic.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 1: (Part 2): The Foundations: Logic and Proofs.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
CS128 – Discrete Mathematics for Computer Science
Axiomatic Semantics Dr. M Al-Mulhem ICS
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 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
From Chapter 4 Formal Specification using Z David Lightfoot
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.
Knoweldge Representation & Reasoning
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
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.
Describing Syntax and Semantics
Propositional Calculus Math Foundations of Computer Science.
Adapted from Discrete Math
Predicates and Quantifiers
Intro to Discrete Structures
Valid Arguments Decide if each argument is valid. If the stones are rolling, they are not gathering moss. If the stones are not gathering moss, they are.
Mathematical Structures A collection of objects with operations defined on them and the accompanying properties form a mathematical structure or system.
Systems Architecture I1 Propositional Calculus Objective: To provide students with the concepts and techniques from propositional calculus so that they.
Review I Rosen , 3.1 Know your definitions!
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Propositional Equivalences
Chap. 2 Fundamentals of Logic. Proposition Proposition (or statement): an declarative sentence that is either true or false, but not both. e.g. –Margret.
Boolean Algebra and Computer Logic Mathematical Structures for Computer Science Chapter 7.1 – 7.2 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Boolean.
Chapter 1, Part II: Predicate Logic With Question/Answer Animations.
Barnett/Ziegler/Byleen Finite Mathematics 11e1 Chapter 7 Review Important Terms, Symbols, Concepts 7.1. Logic A proposition is a statement (not a question.
Chapter 1, Part II: Predicate Logic With Question/Answer Animations.
Lecture Propositional Equivalences. Compound Propositions Compound propositions are made by combining existing propositions using logical operators.
Fundamentals of Logic 1. What is a valid argument or proof? 2. Study system of logic 3. In proving theorems or solving problems, creativity and insight.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
1 Introduction to Abstract Mathematics Expressions (Propositional formulas or forms) Instructor: Hayk Melikya
Section 1.2: Propositional Equivalences In the process of reasoning, we often replace a known statement with an equivalent statement that more closely.
Extra slides for Chapter 3: Propositional Calculus & Normal Forms Based on Prof. Lila Kari’s slides For CS2209A, 2009 By Dr. Charles Ling;
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
Chapter 1: The Foundations: Logic and Proofs
CSNB143 – Discrete Structure Topic 4 – Logic. Learning Outcomes Students should be able to define statement. Students should be able to identify connectives.
Predicates and Quantifiers Dr. Yasir Ali. 1.Predicates 2.Quantifiers a.Universal Quantifiers b.Existential Quantifiers 3.Negation of Quantifiers 4.Universal.
Chapter 2 Fundamentals of Logic 1. What is a valid argument or proof?
Symbolic Logic The Following slide were written using materials from the Book: The Following slide were written using materials from the Book: Discrete.
1 Section 6.2 Propositional Calculus Propositional calculus is the language of propositions (statements that are true or false). We represent propositions.
PREDICATES AND QUANTIFIERS COSC-1321 Discrete Structures 1.
Section 1.4. Propositional Functions Propositional functions become propositions (and have truth values) when their variables are each replaced by a value.
1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 01: Boolean Logic Sections 1.1 and 1.2 Jarek Rossignac.
Foundations of Discrete Mathematics Chapter 1 By Dr. Dalia M. Gil, Ph.D.
Propositional Logic. Assignment Write any five rules each from two games which you like by using propositional logic notations.
Propositional and predicate logic
Chapter 2 1. Chapter Summary Sets (This Slide) The Language of Sets - Sec 2.1 – Lecture 8 Set Operations and Set Identities - Sec 2.2 – Lecture 9 Functions.
Notions & Notations - 1ICOM 4075 (Fall 2010) UPRM Department of Electrical and Computer Engineering University of Puerto Rico at Mayagüez Fall 2010 ICOM.
Chapter 7. Propositional and Predicate Logic
Advanced Algorithms Analysis and Design
CSNB 143 Discrete Mathematical Structures
Lecture 1 – Formal Logic.
Knowledge Representation and Reasoning
COMP 1380 Discrete Structures I Thompson Rivers University
Propositional Calculus: Boolean Algebra and Simplification
Information Technology Department
CSE 311 Foundations of Computing I
Propositional Equivalences
Computer Security: Art and Science, 2nd Edition
Chapter 7. Propositional and Predicate Logic
Predicates and Quantifiers
Foundations of Discrete Mathematics
COMP 1380 Discrete Structures I Thompson Rivers University
1.3 Propositional Equivalences
Presentation transcript:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 5: Invariants and Logic

2 Reminder: contracts Associated with an individual feature:  Preconditions  Postconditions Associated with a class:  Class invariant

3 remove_all_segments -- Remove all stations except the south end. ensure only_one_left: count = 1 both_ends_same: south_end = north_end Contracts extend (s : STATION ) -- Add s at end of line. ensure new_station_added: i_th (count ) = s added_at_north: north_end = s one_more: count = old count + 1 Assertions

4 Contracts deposit (v : INTEGER) -- Add v to account. require positive: v > 0 do … ensure added: balance = old balance + v end Assertion

5 Class invariants The invariant expresses consistency requirements between queries of a class invariant south_is_first: south_end = i_th (1) north_is_last: north_end = i_th (count )

6 Applications of contracts 1.Getting the software right 2.Documenting it; in particular, documenting APIs 3.Testing & debugging (More to come!) Run-time effect: under compiler control (see Projects -> Settings under EiffelStudio)

7 Contracts outside of Eiffel Java: Java Modeling Language (JML), iContract etc. C#: Spec# (Microsoft Research extension) UML: Object Constraint Language Python C++: Nana etc.

8 Logic Programming is reasoning. Logic is the science of reasoning. We use logic in everyday life: “Socrates is human. All humans are mortal. Therefore Socrates must be mortal.”

9 Reasoning and programming Logic is the basis of  Mathematics: proofs are only valid if they follow the rules of logic.  Software development:  Conditions in contracts: “x must not be zero, so that we can calculate.”  Conditions in program actions: “If i is positive, then execute this instruction” (to be introduced in a later lecture)

10 Boolean expressions A condition is expressed as a boolean expression. It consists of Boolean variables (identifiers denoting boolean values) Boolean operators (not, or, and, =, implies) and represents possible Boolean values (truth values, either True or False)

11 Examples Examples of boolean expressions (with rain_today and cuckoo_sang_last_night as boolean variables):  rain_today (a boolean variable is a boolean expression)  not rain_today  (not cuckoo_sang_last_night) implies rain_today (Parentheses group sub-expressions)

12 Negation (not) For any boolean expression e and any values of variables:  Exactly one of e and not e has value True  Exactly one of e and not e has value False  One of e and not e has value True (Principle of the Excluded Middle)  Not both of e and not e have value True (Principle of Non-Contradiction) anot a TrueFalse True

13 Disjunction (or) or operator is non-exclusive or operator is commutative Disjunction principle:  An or disjunction has value True except if both operands have value False aba or b True FalseTrue FalseTrue False

14 Conjunction (and) and operator is commutative. Duality of and and or: properties of either operator yield properties of other (negating + swapping True and False) Conjunction principle:  An and conjunction has value False except if both operands have value True aba and b True False TrueFalse

15 Complex expressions Build more complex boolean expressions by using the boolean operators Example: a and (b and (not c))

16 Truth assignment and truth table Truth assignment for a set of variables: particular choice of values (True or False), for every variable A truth assignment satisfies an expression if the value for the expression is True A truth table for an expression with n variables has  n + 1 columns  2 n rows

17 Combined truth table for basic operators abnot aa or ba and b True FalseTrue FalseTrueFalse True False

18 Tautologies Tautology: a boolean expression that has value True for every possible truth assignment Examples:  a or (not a)  not (a and (not a))  (a and b) or ((not a) or (not b))

19 Contradictions Contradiction: a boolean expression that has value False for every possible truth assignment Examples:  a and (not a) Satisfiable: for at least one truth assignment the expression yields True  Any tautology is satisfiable  No contradiction is satisfiable.

20 Equivalence (=) = operator is commutative (a = b has same value as b = a) = operator is reflexive (a = a is a tautology for any a) Substitution:  For any expressions u, v and e, if u = v is a tautology and e’ is the expression obtained from e by replacing every occurrence of u by v, then e = e’ is a tautology aba = b True False TrueFalse True

21 De Morgan’s laws De Morgan’s Laws: Tautologies  (not (a or b)) = ((not a) and (not b))  (not (a and b)) = ((not a) or (not b)) More tautologies (distributivity):  (a and (b or c)) = ((a and b) or (a and c))  (a or (b and c)) = ((a or b) and (a or c))

22 Syntax convention: binding of operators Order of binding (starting with tightest binding ): not, and, or, implies (to be introduced), =. and and or are associative:  a and (b and c) = (a and b) and c  a or (b or c) = (a or b) or c Style rules: When writing a boolean expression, drop the parentheses: Around the expressions of each side of “=“if whole expression is an equivalence. Around successive elementary terms if they are separated by the same associative operators.

23 Implication (implies) a implies b, for any a and b, is the value of (not a) or b In a implies b: a is antecedent, b consequent Implication principle:  An implication has value True except if its antecedent has value True and its consequent has value False  In particular, always True if antecedent is False aba implies b True False True False True

24 Implication in ordinary language implies in ordinary language often means causation, as in “if … then …”  “If the weather stays like this, skiing will be great this week-end”  “If you put this stuff in your hand luggage, they won’t let you through.”

25 Misunderstanding implication Whenever a is False, a implies b is True, regardless of b :  “If today is Wednesday, 2+2=5.”  “If 2+2=5, today is Wednesday.” Both of the above implications are True Cases in which a is False tell us nothing about the truth of the consequent

26 It is not generally true that a implies b = (not a) implies (not b) Example (wrong!):  “All the people in Zurich who live near the lake are rich. I do not live near the lake, so I am not rich.” live_near_lake implies rich [1] (not live_near_lake ) implies (not rich )[2] Reversing implications (1)

27 Reversing implications (2) Correct: a implies b = (not b) implies (not a) Example:  “All the people who live near the lake are rich. She is not rich, so she can’t be living in Küsnacht” live_near_lake implies rich = (not rich) implies (not live_near_lake )

28 Implication

29 Semistrict boolean operators (1) Example boolean-valued expression (x is an integer): False for x <= -7 Undefined for x = > + x x

30 Semistrict boolean operators (2) BUT:  Division by zero: x must not be 0. (x /= 0) and (((x + 7) / x) > 1) False for x <= -7 False for x = 0

31 Semistrict boolean operators (3) BUT:  Program would crash during evaluation of division We need a non-commutative version of and (and or): Semistrict boolean operators

32 Semistrict operators (and then, or else) a and then b: has same value as a and b if a and b are defined, and has 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 True whenever a has value True (x /= 0) and then (((x + 7) / x) > 1) Semistrict operators allow us to define an order of expression evaluation (left to right). Important for programming when undefined objects may cause program crashes

33 Ordinary vs. Semistrict boolean operators Use  Ordinary boolean operators (and and or) if you can guarantee that both operands are defined  and then if a condition only makes sense when another is true  or else if a condition only makes sense when another is false Example:  “If you are not single, then your spouse must sign the contract” is_single or else spouse_must_sign

34 Semistrict implication Example:  “If you are not single, then your spouse must sign the contract.” (not is_single) implies spouse_must_sign Definition of implies: in our case, always semistrict!  a implies b = (not a) or else b

35 Programming language notation for boolean operators Eiffel keywordCommon mathematical symbol not~ or ¬ or  and  =  implies 

36 Propositional and predicate calculus Propositional calculus: property p holds for a single object Predicate calculus: property p holds for several objects

37 Generalizing or G : group of objects, p : property or: Does at least one of the objects in G satisfy p ? Is at least one station of Line 8 an exchange? Station_Balard.is_exchange or Station_Lourmel.is_exchange or Station_Boucicaut.is_exchange or … (all stations of Line 8) Existential quantifier: exists, or   s : Stations_8 | s.is_exchange “There exists an s in Stations_8 such that s.is_exchange is true”

38 Generalizing and and: Does every object in G satisfy p? Are all stations of Tram 8 exchanges? Station_Balard.is_exchange and Station_Lourmel.is_exchange and Station_Boucicaut.is_exchange and … (all stations of Line 8) Universal quantifier: for_all, or   s: Stations_8 | s.is_exchange “For all s in Stations8 | s.is_exchange is true”

39 Existentially quantified expression Boolean expression:  s : SOME_SET | s.some_property  True if and only if at least one member of SOME_SET satisfies property some_property Proving  True: Find one element of SOME_SET that satisfies the property  False: Prove that no element of SOME_SET satisfies the property (test all elements)

40 Universally quantified expression Boolean expression:  s: SOME_SET | s.some_property  True if and only if every member of SOME_SET satisfies property some_property Proving  True: Prove that every element of SOME_SET satisfies the property (test all elements)  False: Find one element of SOME_SET that does not satisfies the property

41 Duality Generalization of DeMorgan’s laws: not (  s : SOME_SET | P ) =  s : SOME_SET | not P not (  s : SOME_SET | P ) =  s : SOME_SET | not P

42 Empty sets  s : SOME_SET | some_property IfSOME_SET is empty: always False  s : SOME_SET | some_property IfSOME_SET is empty: always True

43 Reading assignment for next week Chapter 6 (object creation) Read corresponding slides (from Thursday) Start reading chapter 7 (control structures)

44 What we have seen  Logic as a tool for reasoning  Boolean operators: truth tables  Properties of boolean operators: don’t use truth tables!  Predicate calculus: to talk about logical properties of sets  Semistrict boolean operators