CS195Y Lecture 2

1/27/17


Announcement: Oracle goes out today (1/27/17) and is due Thursday (2/2/17)

One minute recap of first lecture:
A logic class in the philosophy department will be primarily proof and formalism
This logic class will be predominately application-based. There will be no pencil-paper proofs.
We will be writing code to show what problems we can solve using logic.

Testing Oracles

Strengths of Test Cases

Weaknesses of Test Cases

Sort
Record = {Name, School}
Test cases for sorting records of this type:

[] -> []
[{"", ""}] -> [{"", ""}]
[{"Tim", "Brown"}, {"Tim", "Not Brown"}]

Are we sorting by Name or School or both?
If we sort just by Name, there are potentially several correct outputs.
[{"Tim", "Brown"}, {"Tim", "Not Brown"}]
If we want order to be preserved, then the only possible output is [{"Tim", "Brown"}, {"Tim", "Not Brown"}]
But, if we don't care whether order is presevered, there are two possible outputs, so it's harder to
write the test case (you can't just have the input and output)
Other problems with multiple correct results:

Issue: Test cases are too rigid. We need to be more flexible in order to be sure our tests are enough to say the
works.

What is the type of Sort?
Sort: Listof<Rec> -> Listof<Rec>
What is the type of a test suite for sort?
Suite: (Listof<Rec> -> Listof<Rec>) -> (Boolean)

What are we going to test our sort against?

What properties should be true about our sorting algorithm?
A. all x: Input | x in Output
B. all X: Output| x in Input
C. "sortedness" all x,y : Output | x > y implies pos(x) > pos(y)

Oracle: (Listof<Rec> -> Listof<Rec>) -> (Boolean)
Our goal is to construct a function that we can pass our sort function to, and it will tell us
whether or not the sort function is correct.
It's challenging to be both sound and complete.
Sound: You never say true if the answer is not true.
Complete: You never say false if the answer is true.

Programs are hard to verify. Our goal in this course is to fight that problem.

An oracle is made of two components: an input generator and a validity checker.
valid?: List<Rec>, List<Rec> -> List<Rec> -> Boolean

generate-input: () -> List<Rec>
Break down into a couple problems:

New paradigm for testing: Instead of coming up with inputs and outputs, write code to generate inputs and
test whether the outputs satisfy certain properties.

Integer Factorization
Given an integer, express that integer as the product of primes

4 -> 2^2
7 -> 7^1
1 -> 1
12 -> 2^2 * 3^1

generate-input: () -> Integer

valid?: Integer, Integer -> List<Integer> -> Boolean

Sometimes the properties are themselves difficult to check. For example, how do we determine if the output numbers
are all prime?
Even if we can't test all of the properties, partial oracles can still be helpful.

Generation is not intellectually difficult. Checking validity can be difficult.

Sudoku
Input: A 3x3 board, each cell of which is also divided into a 3x3 grid. Some spaces will be filled in with a number
1-9, but most will be empty
Output: The same board with every space filled in.
valid?:

Next week, we will prove implications between these properties.