1 / 16

Introduction to Programming

Introduction to Programming. Bertrand Meyer Exercise Session 6 7 October 2008. Today’s learning goals. A bit of logic Understanding contracts (preconditions, postconditions, and class invariants). Logic. Tautology True for all truth assignments a or ( not a) not (a and ( not a))

zinna
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008

  2. Today’s learning goals • A bit of logic • Understanding contracts (preconditions, postconditions, and class invariants)

  3. Logic Tautology Truefor 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

  4. 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

  5. Truth tables Does the following equivalence hold? (P implies Q) = (not P impliesnot Q) 5 Hands-On

  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 or i >= 0) tautology 6 Hands-On

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

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

  9. 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

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

  11. 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 Precondition 11 clap(n: INTEGER) -- Clap n times and update count. require not_too_tired: count <= 10 n_positive: n > 0

  12. 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 Postcondition 12 clap(n: INTEGER) -- Clap n times and update count. require not_too_tired: count <= 10 n_positive: n > 0 ensure count_updated: count = oldcount + n

  13. 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 14 class ACROBAT … invariant count_non_negative: count >= 0 end

  14. 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 15 Hands-On

  15. One possible solution square_root (i: INTEGER): REALis -- Square root of `i’ -- with precision `eps’ (given somewhere else) require i_positive:i >= 0 ensure correct_result: i – eps <= Result * Resultand i + eps >= Result * Result end 16

  16. 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. 17 Hands-On

More Related