Class 15:
Recursive
Data
Structures
cs2102: Discrete
Mathematics | F17
uvacs2102.github.io
David Evans
University of Virginia
Slide 2
Slide 2 text
Plan for Week
Recursive Data Structures: Structural Induction
(MCS Chapter 7)
o Book data structures: strings (balanced strings),
non-negative integers, arithmetic expressions
o Class data structures: lists (today), trees (Thurs)
You should definitely read and understand what is in the book (and ask questions
about anything), but we will (mostly) do different examples in class.
Slide 3
Slide 3 text
Finishing Proof from Thursday…
Slide 4
Slide 4 text
Correctness of Slow Exponentiation
∷= ℕ × ℕ
∷= { , ⟶ , − 1
| ∀ ∈ ℕ, ∀ ∈ ℕ3}
6
∷= (1, )
What preserved invariant would be useful?
Invariant is Preserved
∷= { , ⟶ , − 1 | ∀, ∈ ℕ3}
= , ∷= = ;<=
Does this prove
slow_power(a, b) =
Slide 7
Slide 7 text
Invariant Principle
= , ∷= = ;<=
Base case:
If a preserved invariant is true for the start state, it is true for all reachable states.
Slide 8
Slide 8 text
Invariant Principle
= , ∷= = ;<=
Prove slow_power(a, b) =
Base case: (6
= 1, )
= ;<= since = 1 = 6 and − = 0.
Inductive case: is a preserved invariant
Slide 9
Slide 9 text
Partial Correctness
= , ∷= ∈ ℕ ∧ = ;<=
Prove slow_power(a, b) =
If machine reaches a terminating state
A
= (, 0)
the result is correct: = ;.
What else do we need to show?
Slide 10
Slide 10 text
Termination
= , ∷= ∈ ℕ ∧ = ;<=
Prove slow_power(a, b) =
Eventually reaches state A
= , 0
Reachability
Reaches state (, ):
∷= { , ⟶ , − 1 | ∀, ∈ ℕ3}
6
∷= (1, )
Is slow exponentiation fast enough?
In = steps, the terminating state is reached.
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
Fast Exponentiation
Read this example in the book 6.3.1
- same idea, but more complex
preserved invariant needed.
Slide 15
Slide 15 text
Defining Data Structures
Slide 16
Slide 16 text
Main Idea
We can show a property holds for all objects of a data type by:
1. Showing the property holds for all base objects.
2. Showing that all the ways of constructing new objects, preserve the
property
Slide 17
Slide 17 text
Recall from Class 7:
Slide 18
Slide 18 text
Simplest Complex Data Type
Slide 19
Slide 19 text
Making Pairs
Slide 20
Slide 20 text
Making Pairs without Lists
Slide 21
Slide 21 text
Making Pairs without Lists
Slide 22
Slide 22 text
Making a Triple
Slide 23
Slide 23 text
Making a Quadruple
Slide 24
Slide 24 text
Multuples
A Quintuple is a Pair where the second part is a Quadruple
A Sextuple is a Pair where the second part is a Quintuple
A Septuple is a pair where the second part is a Sextuple
An Octuple is group of octupi
A ? is a Pair where the second part is a …?
24
Slide 25
Slide 25 text
Lists
25
A List is a Pair where the second part is a List.
But, then we can not create any lists!
Slide 26
Slide 26 text
Lists
26
A List is either:
a Pair where the second part is a List
or, the empty list
null in LISP, None in Python, for mathematical lists
Slide 27
Slide 27 text
Defining List
Definition 1. A List is an ordered sequence of objects.
Definition 2. A List is (1) either a Pair where the second part
is a List or (2) the empty list.
Are these equivalent definitions?
Slide 28
Slide 28 text
Charge
Thursday: Structural Induction on lists
Problem Set 6 due Friday
Send any questions you want for Thursday’s
class