Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Class 14: Invariant Principle

David Evans
October 14, 2016
4.9k

Class 14: Invariant Principle

cs2102: Discrete Mathematics
University of Virginia, Fall 2016

See course site for notes:
https://uvacs2102.github.io

David Evans

October 14, 2016
Tweet

Transcript

  1. Class 14: Invariant Principle cs2102: Discrete Mathematics | F16 uvacs2102.github.io

    David Evans | University of Virginia Last chance to avoid storage fees on Exam 1: pick up after class today
  2. = , = → , → + = Binary Relation

    View State Machine View
  3. Reachable States A state is reachable if it appears in

    some execution. The execution of a state machine, = (, ⊆ ×, + ∈ ) is a (possibly infinite) sequence of states, (+ , 2 , … , 4 ) that: 1. + = + (it begins with the start state) 2. ∀ ∈ 0, 1, … , − 1 . < → <=2 ∈ (if and are consecutive states in the sequence, there is an edge → ∈ .
  4. Bishop Moves = , , ∈ ℕ } + =

    (0, 2) What states are reachable? = , → C, ′ , ∈ ℕ ∧ ∃ ∈ ℕ= such that C = ± ∧ C ≥ 0 ∧ C = ± ∧ C ≥ 0 }
  5. = , , ∈ ℕ } + = (0, 2)

    = , → C, ′ , ∈ ℕ ∧ ∃ ∈ ℕ= such that C = ± ∧ C ≥ 0 ∧ C = ± ∧ C ≥ 0 }
  6. Preserved Invariant is a preserved invariant of = (, ⊆

    ×, + ∈ ) if ∀ ∈ . ∧ ( → ) ∈ ⟹ ()
  7. = , ∶= > 3 Is a preserved invariant? =

    , , ∈ ℕ } + = (0, 2) = , → C, ′ , ∈ ℕ ∧ ∃ ∈ ℕ= such that C = + ∧ C ≥ 0 ∧ C = + ∧ C ≥ 0 } (up-right only bishops)
  8. Is a preserved invariant? = , , ∈ ℕ }

    = , ∶= ( = ) = , → C, ′ , ∈ ℕ ∧ ∃ ∈ ℕ= such that C = + ∧ C ≥ 0 ∧ C = + ∧ C ≥ 0 } (up-right only bishops)
  9. What is an example of a that is not a

    preserved invariant? = , , ∈ ℕ } (up-right only bishops) = , → C, ′ , ∈ ℕ ∧ ∃ ∈ ℕ= such that C = + ∧ C ≥ 0 ∧ C = + ∧ C ≥ 0 }
  10. = , , ∈ ℕ } = , → C,

    ′ , ∈ ℕ ∧ ∃ ∈ ℕ= such that C = ± ∧ C ≥ 0 ∧ C = ± ∧ C ≥ 0 } regular bishops What are preserved invariants for the Bishop machine? Any questions about definitions: state machine, execution, reachable, preserved invariant
  11. = , , ∈ ℕ } ∶= is a purple

    square = , → C, ′ , ∈ ℕ ∧ ∃ ∈ ℕ= such that C = ± ∧ C ≥ 0 ∧ C = ± ∧ C ≥ 0 } regular bishops
  12. = , , ∈ ℕ } = , → C,

    ′ , ∈ ℕ ∧ ∈ ℕ= ∧ C = ± ∧ C ≥ 0 ∧ C = ± ∧ C ≥ 0 } = (, ) ∶= + is even
  13. Invariant Principle If a preserved invariant is true for the

    start state, it is true for all reachable states.
  14. = , , ∈ ℕ } = , → C,

    ′ , ∈ ℕ ∧ ∈ ℕ= ∧ C = ± ∧ C ≥ 0 ∧ C = ± ∧ C ≥ 0 } + = (0, 2) Prove never enters a state corresponding to a white square.
  15. = , , ∈ ℕ } = , → C,

    ′ , ∈ ℕ ∧ ∈ ℕ= ∧ C = ± ∧ C ≥ 0 ∧ C = ± ∧ C ≥ 0 } + = (0, 2) Prove never enters a state where + is odd.
  16. Correctness of Slow Exponentiation ∷= ℕ × ℕ ∷= {

    , ⟶ , − 1 | ∀, ∈ ℕ=} + ∷= (1, ) What preserved invariant would be useful?
  17. Correctness of Slow Exponentiation ∷= ℕ × ℕ ∷= {

    , ⟶ , − 1 | ∀, ∈ ℕ=} + ∷= (1, ) What preserved invariant would be useful? = , ∷= ∈ ℕ ∧ ^ = _ = , ∷= ∈ ℕ ∧ = _`^
  18. Invariant is Preserved ∷= { , ⟶ , − 1

    | ∀, ∈ ℕ=} = , ∷= ∈ ℕ ∧ = _`^
  19. Invariant is Preserved ∷= { , ⟶ , − 1

    | ∀, ∈ ℕ=} = , ∷= ∈ ℕ ∧ = _`^ Does this prove slow_power(a, b) =
  20. Invariant Principle = , ∷= ∈ ℕ ∧ = _`^

    Base case: If a preserved invariant is true for the start state, it is true for all reachable states.
  21. Invariant Principle = , ∷= ∈ ℕ ∧ = _`^

    Prove slow_power(a, b) = Base case: (+ = 1, ) ∈ ℕ since = and ∈ ℕ = _`^ since = 1 = + and − = 0. Inductive case: is a preserved invariant
  22. Partial Correctness = , ∷= ∈ ℕ ∧ = _`^

    Prove slow_power(a, b) = If machine reaches terminating state e = (, 0) the result is correct: = . What else do we need to show?
  23. Invariant Principle = , ∷= ∈ ℕ ∧ = _`^

    Prove slow_power(a, b) = Eventually reaches state e = = , 0
  24. Reachability Reaches state (, ): ∷= { , ⟶ ,

    − 1 | ∀, ∈ ℕ=} + ∷= (1, )
  25. Fast Exponentiation Read this example in the book: same idea,

    but more complex preserved invariant needed.
  26. Charge • PS6 will be posted by tomorrow, due next

    Friday • Read rest of Chapter 6 • Tuesday: stable marriages!