Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.

Slides:



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

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
L41 Lecture 2: Predicates and Quantifiers.. L42 Agenda Predicates and Quantifiers –Existential Quantifier  –Universal Quantifier 
Logic Chapter 2. Proposition "Proposition" can be defined as a declarative statement having a specific truth-value, true or false. Examples: 2 is a odd.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
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 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
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 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 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 6: Object Creation.
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.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
CSE115/ENGR160 Discrete Mathematics 01/20/11 Ming-Hsuan Yang UC Merced 1.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Logic Truth Tables, Propositions, Implications. Statements Logic is the tool for reasoning about the truth or falsity of statements. –Propositional logic.
Predicates and Quantifiers
Predicates & Quantifiers Goal: Introduce predicate logic, including existential & universal quantification Introduce translation between English sentences.
Lecture 8 Introduction to Logic CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Discrete Mathematics Goals of a Discrete Mathematics Learn how to think mathematically 1. Mathematical Reasoning Foundation for discussions of methods.
Course Outline Book: Discrete Mathematics by K. P. Bogart Topics:
MATH 224 – Discrete Mathematics
(CSC 102) Lecture 3 Discrete Structures. Previous Lecture Summary Logical Equivalences. De Morgan’s laws. Tautologies and Contradictions. Laws of Logic.
Fall 2002CMSC Discrete Structures1 Let’s get started with... Logic !
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chapter 1, Part II: Predicate Logic With Question/Answer Animations.
MATH 224 – Discrete Mathematics
Formal Methods in Software Engineering 1
CSE 311 Foundations of Computing I Lecture 7 Logical Inference Autumn 2012 CSE
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.
Lecture 9 Conditional Statements CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Propositional Logic ITCS 2175 (Rosen Section 1.1, 1.2)
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.
CSNB143 – Discrete Structure Topic 4 – Logic. Learning Outcomes Students should be able to define statement. Students should be able to identify connectives.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Predicate Logic One step stronger than propositional logic Copyright © Curt Hill.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Copyright © Peter Cappello 2011 Predicates & Quantifiers.
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.
1 Chapter 2.1 Chapter 2.2 Chapter 2.3 Chapter 2.4 All images are copyrighted to their respective copyright holders and reproduced here for academic purposes.
PREDICATES AND QUANTIFIERS COSC-1321 Discrete Structures 1.
Spring 2003CMSC Discrete Structures1 Let’s get started with... Logic !
Mathematics for Comter I Lecture 3: Logic (2) Propositional Equivalences Predicates and Quantifiers.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Section 1.4. Propositional Functions Propositional functions become propositions (and have truth values) when their variables are each replaced by a value.
Discrete Mathematical Structures: Theory and Applications 1 Logic: Learning Objectives  Learn about statements (propositions)  Learn how to use logical.
Propositional Logic. Assignment Write any five rules each from two games which you like by using propositional logic notations.
You are an Acrobat When you are asked to Clap, you will be given a number. Clap your hands that many times. When you are asked to Twirl, you will be given.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Chapter 1 Logic and proofs
Logic Hubert Chan [O1 Abstract Concepts] [O2 Proof Techniques]
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Predicates & Quantifiers
Logic Hubert Chan [O1 Abstract Concepts] [O2 Proof Techniques]
Einführung in die Programmierung Introduction to Programming Prof. Dr
CSE 311 Foundations of Computing I
Einführung in die Programmierung Introduction to Programming Prof. Dr
Discrete Mathematics CMP-200 Propositional Equivalences, Predicates & Quantifiers, Negating Quantified Statements Abdul Hameed
Predicates and Quantifiers
Presentation transcript:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4

2 Today  A bit of logic  Understanding contracts (preconditions, postconditions, and class invariants)  Entities and objects  Object creation

3 Propositional Logic  Constants: True, False  Atomic formulae (propositional variables): P, Q,...  Logical connectives: not, and, or, implies, =  Formulae: φ, χ,... are of the form  True  False  P  not φ  φ and χ  φ or χ  φ implies χ  φ = χ 3

4 Propositional Logic Truth assignment and truth table  Assigning a truth value to each propositional variable Tautology  True for all truth assignments  P or (not P)  not (P and (not P))  (P and Q) or ((not P) or (not Q)) Contradiction  False for all truth assignments  P and (not P) 4 PQP implies Q TFF TTT FTT FFT

5 Propositional Logic Satisfiable  True for at least one truth assignment Equivalent  φ and χ are equivalent if they are satisfied under exactly the same truth assignments, or if φ = χ is a tautology 5

6 6 Tautology / contradiction / satisfiable? P or Q satisfiable P and Q satisfiable P or (not P) tautology P and (not P) contradiction Q implies (P and (not P)) satisfiable Hands-On

7 7 Equivalence Does the following equivalence hold? Prove. (P implies Q) = (not P implies not Q) Does the following equivalence hold? Prove. (P implies Q) = (not Q implies not P) Hands-On PQP implies Qnot P implies not Qnot Q implies not P TTTTT TFFTF FTTFT FFTTT F T

8 8 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)

9 Predicate Logic  Domain of discourse: D  Variables: x: D  Functions: f: D n -> D  Predicates: P: D n -> {True, False}  Logical connectives: not, and, or, implies, =  Quantifiers:   Formulae: φ, χ,... are of the form  P (x,...)  not φ | φ and χ | φ or χ | φ implies χ | φ = χ   x φ   x φ 9

10 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  p: Person | not p.likes (Rivella) not (  p: Person | p.likes (Rivella))

11 Tautology / contradiction / satisfiable? Let the domain of discourse be INTEGER x = 0 tautology x > 0 implies x > 1 satisfiable  x | x > 0 implies x > 1 contradiction  x | x*y = y satisfiable  y |  x | x*y = y tautology Hands-On

12 Semi-strict operations 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. text /= Void and then text.contains (“Joe”)  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. list = Void or else list.is_empty 12

13 Strict or semi-strict?  a = 0 or b = 0  a /= 0 and then b // a /= 0  a /= Void and b /= Void  a 2  (a = b and b /= Void) and then not a.name.is_equal (“”) 13 Hands-On

14 Assertions balance_non_negative: balance >= 0 Assertion clause Assertion tag (not required, but recommended) Condition (required)

15 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 15 Precondition

16 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 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 16 Postcondition

17 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 17 Class Invariant

18 Pre- and postcondition example Add pre- and postconditions to: smallest_power (n, bound: NATURAL): NATURAL -- Smallest x such that `n'^x is greater or equal `bound'. require ??? do... ensure ??? end Hands-On

19 One possible solution smallest_power (n, bound: NATURAL): NATURAL -- Smallest x such that `n'^x is greater or equal `bound'. require n_large_enough: n > 1 bound_large_enough: bound > 1 do... ensure greater_equal_bound: n ^ Result >= bound smallest: n ^ (Result - 1) < bound end

20 Hands-on exercise Add invariants to classes ACROBAT_WITH_BUDDY and CURMUDGEON. Add preconditions and postconditions to feature make in ACROBAT_WITH_BUDDY. Hands-On

21 Class ACROBAT_WITH_BUDDY class ACROBAT_WITH_BUDDY inherit ACROBAT redefine twirl, clap, count end create make feature make (p: ACROBAT) do -- Remember `p’ being -- the buddy. end clap (n: INTEGER) do -- Clap `n’ times and -- forward to buddy. end twirl (n: INTEGER) do -- Twirl `n’ times and -- forward to buddy. end count: INTEGER do -- Ask buddy and return his -- answer. end buddy: ACROBAT end

22 Class CURMUDGEON class CURMUDGEON inherit ACROBAT redefine clap, twirl end feature clap (n: INTEGER) do -- Say “I refuse”. end twirl (n: INTEGER) do -- Say “I refuse”. end

23 In the class text: an entity joe: STUDENT In memory, during execution: an object Entity vs. object (COURSE) MEMORY (ASSISTANT) (STUDENT) (MARK) (PROFESSOR) Fields Generating class

24 class INTRODUCTION_TO_PROGRAMMING inherit COURSE feature execute -- Teach `joe’ programming. do -- ??? joe.solve_all_assignments end joe: STUDENT -- A first year computer science student end INTRODUCTION_TO_PROGRAMMING

25 Initial state of a reference In an instance of INTRODUCTION_TO_PROGRAMMING, may we assume that joe is attached to an instance of STUDENT? joe (STUDENT ) reference (INTRODUCTION_ TO_PROGRAMMING) MEMORY Where does this one come from? … This object has been created (by someone else)

26 By default Initially, joe is not attached to any object: its value is a Void reference. joe Void reference (INTRODUCTION_ TO_PROGRAMMING)

27 States of an entity During execution, an entity can:  Be attached to a certain object  Have the value Void

28  To denote a void reference: use Void keyword  To create a new object in memory and attach x to it: use create keyword create x  To find out if x is void: use the expressions x = Void (true iff x is void) x /= Void (true iff x is attached) States of an entity

29 Those mean void references! The basic mechanism of computation is feature call x. f (a, …) Since references may be void, x might be attached to no object The call is erroneous in such cases! Apply feature f To object to which x is attached Possibly with arguments

30 Shouldn’t we assume that a declaration joe: STUDENT creates an instance of STUDENT and attaches it to joe? Why do we need to create objects?

31 Those wonderful void references! (PERSON) spouse Married persons: (PERSON) spouse Unmarried person:

32 Those wonderful void references! Last next reference is void to terminate the list. (STOP ) next (STOP )

33 Creation procedures  Instruction create x will initialize all the fields of the new object attached to x with default values  What if we want some specific initialization? E.g., to make object consistent with its class invariant? class STOP … station: STATION invariant station /= Void station  Use creation procedure: create stop1.set_station (Central) Incorrect!

34 STOP List one or more creation procedures May be used as a regular command and as a creation procedure Is established by set_station class STOP create set_station feature station: STATION -- Station which this stop represents next: SIMPLE_STOP -- Next stop on the same line set_station (s: STATION) -- Associate this stop with s. require station_exists: s /= Void ensure station_set: station = s link (s: SIMPLE_STOP) -- Make s the next stop on the line. ensure next_set: next = s invariant station_exists: station /= Void end

35 Object creation: summary To create an object:  If class has no create clause, use basic form: create x  If the class has a create clause listing one or more procedures, use create x.make (…) where make is one of the creation procedures, and (…) stands for arguments if any.

36 Some acrobatics class DIRECTOR create prepare_and_play feature acrobat1, acrobat2, acrobat3: ACROBAT friend1, friend2: ACROBAT_WITH_BUDDY author1: AUTHOR curmudgeon1: CURMUDGEON prepare_and_play do author1.clap (4) friend1.twirl (2) curmudgeon1.clap (7) acrobat2.clap (curmudgeon1.count) acrobat3.twirl (friend2.count) friend1.buddy.clap (friend1.count) friend2.clap (2) end Hands-On What entities are used in this class? What’s wrong with the feature prepare_and_play?

37 Some acrobatics class DIRECTOR create prepare_and_play feature acrobat1, acrobat2, acrobat3: ACROBAT friend1, friend2: ACROBAT_WITH_BUDDY author1: AUTHOR curmudgeon1: CURMUDGEON prepare_and_play do 1 create acrobat1 2 create acrobat2 3 create acrobat3 4 create friend1.make_with_buddy (acrobat1) 5 create friend2.make_with_buddy (friend1) 6 create author1 7 create curmudgeon1 end Hands-On Which entities are still Void after execution of line 4? Which of the classes mentioned here have creation procedures? Why is the creation procedure necessary?