1
play

1 Negation ( not ) Complex expressions a not a Build more complex - PDF document

Reasoning and programming Chair of Softw are Engineering Logic is the basis of Einfhrung in die Programmierung Mathematics: proofs are only valid if they follow the Introduction to Programming rules of logic. Prof. Dr. Bertrand Meyer


  1. Reasoning and programming Chair of Softw are Engineering Logic is the basis of Einführung in die Programmierung � Mathematics: proofs are only valid if they follow the Introduction to Programming rules of logic. Prof. Dr. Bertrand Meyer � Software development: October 2006 – February 2007 � Conditions in contracts: “ x must not be zero, so that + we can calculate .” x 7 x Lecture 5: Invariants and Logic � Conditions in program actions: “If i is positive, then execute this instruction.” (to be introduced in a later lecture) I ntro. to Programming, lecture 1: Overview 4 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 ). 2 I ntro. to Programming, lecture 1: Overview 5 Reasoning Examples Programming is reasoning. Examples of boolean expressions (with rain_today and cuckoo_sang_last_night as boolean Logic is the science of reasoning. variables): We use logic in our every days life: � rain_today “Socrates is human. (a boolean variable is a boolean expression) All humans are mortal. � not rain_today Therefore Socrates must be mortal.” � ( not cuckoo_sang_last_night ) implies rain_today (Parentheses group sub-expressions.) I ntro. to Programming, lecture 1: Overview 3 I ntro. to Programming, lecture 1: Overview 6 1

  2. Negation ( not ) Complex expressions a not a Build more complex boolean expressions by using the boolean operators. True False False True Example: For any boolean expression e and any values of variables: a and ( b and ( not c )) � 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.) I ntro. to Programming, lecture 1: Overview 7 I ntro. to Programming, lecture 1: Overview 10 Disjunction ( or ) Truth assignment and truth table a b a or b Truth assignment for a set of variables: particular choice of values ( True or False ), for every variable. True True True True False True A truth assignment satisfies an expression if the value for False True True the expression is True . False False False A truth table for an expression with n variables has or operator is non-exclusive. � n+1 columns or operator is commutative. � 2 n rows Disjunction principle : � An or disjunction has value True except if both operands have value False . I ntro. to Programming, lecture 1: Overview 8 I ntro. to Programming, lecture 1: Overview 11 Conjunction ( and ) Combined truth table for basic operators a b a and b a b not a a or b a and b True True True True True False True True True False False True False True True False False True False False True True False False False False False False False False 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 . I ntro. to Programming, lecture 1: Overview 9 I ntro. to Programming, lecture 1: Overview 12 2

  3. Tautologies De Morgan’s laws Tautology: a boolean expression that has value True for De Morgan’s Laws: Tautologies every possible truth assignment. � ( not ( a or b )) = (( not a ) and ( not b )) � ( not ( a and b )) = (( not a ) or ( not b )) Examples: More tautologies: � a or ( not a ) � ( a and ( b or c )) = (( a and b ) or ( a and c )) � not ( a and ( not a )) � ( a or ( b and c )) = (( a or b ) and ( a or c )) � ( a and b ) or (( not a ) or ( not b )) I ntro. to Programming, lecture 1: Overview 13 I ntro. to Programming, lecture 1: Overview 16 Contradictions Binding Order of binding (starting with tightest binding): not , Contradiction: a boolean expression that has value False for and , or , implies (to be introduced), = . every possible truth assignment. and and or are associative: Examples: � a and ( b and c ) = ( a and b ) and c � a and ( not a ) � a or ( b or c ) = ( a or b ) or c Style rules: When writing a boolean expression, drop the parentheses: Satisfiable: for at least one truth assignment the expression yields True . • Around the expressions of each side of “=“ if whole expression is an equivalence. � Any tautology is satisfiable. • Around successive elementary terms if they are � No contradiction is satisfiable. separated by the same associative operators. I ntro. to Programming, lecture 1: Overview 14 I ntro. to Programming, lecture 1: Overview 17 Equivalence ( = ) Implication ( im plies ) a b a = b a b a implies b True True True True True True True False False True False False False True True False True False False False True False False True a implies b , for any a and b , is the value of ( not a ) or b = operator is commutative ( a = b has same value as b = a ). In a implies b : a is antecedent, b consequent = operator is reflexive ( a = a is a tautology for any a ). Implication principle: Substitution: � An implication has value True except if its antecedent � For any expressions u , v and e , if u = v is a tautology has value True and its consequent has value False and e’ is the expression obtained from e by replacing � In particular, always True if antecedent is False every occurrence of u by v , then e = e’ is a tautology. I ntro. to Programming, lecture 1: Overview 15 I ntro. to Programming, lecture 1: Overview 18 3

  4. Implication in ordinary language Reversing implications (2) implies in ordinary language often means causation, as in Correct: “if … then …” a implies b = ( not b ) implies ( not a ) � “ If the weather stays like this, skiing will be great Example: this week-end .” � “All the people who live near the lake are rich. She is not rich, so she can’t be living in Küsnacht” � “ If you put this stuff in your hand luggage, they won’t let you through .” live_near_lake implies rich = ( not rich ) implies ( not live_near_lake ) I ntro. to Programming, lecture 1: Overview 19 I ntro. to Programming, lecture 1: Overview 22 Misunderstanding implications 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. I ntro. to Programming, lecture 1: Overview 20 I ntro. to Programming, lecture 1: Overview 23 Reversing implications (1) Semistrict boolean operators (1) It is not generally true that Example boolean-valued expression ( x is an integer): a implies b = ( not a ) implies ( not b ) + 7 > x 1 Example (wrong!): x � “All the people in Zurich who live near the lake are rich. I do not live near the lake, so I am not rich.” False for x <= -7 Undefined for x = 0 live_near_lake implies rich [1] ( not live_near_lake ) implies ( not rich ) [2] I ntro. to Programming, lecture 1: Overview 21 I ntro. to Programming, lecture 1: Overview 24 4

  5. Semistrict boolean operators (2) Ordinary vs. non-strict boolean operators BUT: Use � Ordinary boolean operators ( and and or ) if you can guarantee that both operands are defined. � Division by zero: x must not be 0. � and then , if a condition only makes sense when another is true. + 7 > ( x /= 0) and ( ) x 0 � or else , if a condition only makes sense when another x is false. Example: � “If you are not single, then your spouse must sign the contract.” is_single or else spouse_must_sign I ntro. to Programming, lecture 1: Overview 25 I ntro. to Programming, lecture 1: Overview 28 Semistrict boolean operators (3) Non-strict implication BUT: Example: � and is commutative (program would crash). � “If you are not single, then your spouse must sign the contract.” ( not is_single ) implies spouse_must_sign We need a non-commutative version of and (and or ): Non-strict boolean operators. Definition of implies : in our case, always non-strict! � a implies b = ( not a ) or else b I ntro. to Programming, lecture 1: Overview 26 I ntro. to Programming, lecture 1: Overview 29 Eiffel keywords and mathematical symbols Non-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 False whenever a has value False . Eiffel keyword Common mathematical symbol not ~ or ¬ a or else b : has same value as a or b if a and b are defined, or ∨ and has True whenever a has value True . and ∧ + 7 > ( x /= 0) and then ( ) x 0 = ⇔ x implies ⇒ Non-strict operators allow us to define an order of expression evaluation (left to right). Important for programming when undefined objects may cause program crashes. I ntro. to Programming, lecture 1: Overview 27 I ntro. to Programming, lecture 1: Overview 30 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend