CS3102 Theory of Computation www.cs.virginia.edu/~njb2b/cstheory/s2020 Warm up: What features present in Java/Python are missing from straightline programs?
Logistics • Exercise 1 due this afternoon – Didn’t submit? You have 48 hours to do so with a 25% penalty • Quiz 2 due today • Exercise 2 is out. – Some stuff due Thursday, the rest due Tuesday 2
Last Time • Boolean Circuits as a model of computing • Straightline Programs as a model of computing • Proved NAND-Straightline = NAND-Circ = AON-Circ = AON-straightline 3
Majority with Boolean Circuits # Gates = # Lines x[0] x[1] x[2] 4
NAND Straightline = AON Straightline NAND -> AON AON -> NAND x = NOT(a) x = NAND(a,b) x = OR(a,b ) Becomes Becomes Becomes x= NAND(a,a) temp = AND(a,b) t1 = NAND(a,a) x = NOT(temp) t2 = NAND(b,b) x = AND(a,b) x= NAND(t1,t2) Becomes temp= NAND(a,b) x=NAND(temp,temp) 5
Syntactic Sugar • "Full-featured" programming languages are identical to simple ones • We can add new features without changing the underlying computing model • These features can make programs easier to reason about and more readable 6
User-Defined Procedures 7
"Translating" Procedures • Adding procedures does not change computing model • We can convert a program with procedures into a program without them 8
Procedure for translating procedures • Paste code from procedure • Use arguments in place of parameters • Rename variables from the procedure to be "fresh" Before After 9
How many gates? • How many NAND gates does this use to compute MAJ? 10
Conditionals • Values of some variables might depend on a condition • Code • Translated 11
Translating Conditionals • Pre-compute each of the possible values • Use a procedure to determine which to assign 12
Lookup • Indexing into a bitstring • The 𝑀𝑝𝑝𝑙𝑣𝑞 function of order 𝑙 : 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 : 0,1 2 𝑙 +𝑙 → 0,1 Defined such that for 𝑦 ∈ 0,1 2 𝑙 , 𝑗 ∈ 0,1 𝑙 : 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 𝑦, 𝑗 = 𝑦 𝑗 13
𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 𝑙 = 3 i : x : 0 1 0 0 1 1 1 0 0 1 0 Last 𝑙 bits of input First 2 𝑙 bits of input Considered as an index Considered as a bitstring 14
Theorem There is a NAND-Cricuit that computes 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 : 0,1 2 𝑙 +𝑙 → {0,1} Moreover, the number of gates required is at most 4 ⋅ 2 𝑙 15
Proof idea • Consider index 𝑗 • If the first bit of 𝑗 is 0, then the bit we're looking for is in the first half of 𝑦 • Do lookup for 𝑙 − 1 𝑗 : 𝑦 : 0 1 0 0 1 1 1 0 0 1 0 𝑗 : 𝑦 : 0 1 0 0 1 0 16
Defining 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 For 𝑙 ≥ 2, 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙 (𝑦 0 , … , 𝑦 2 𝑙 −1 , 𝑗 0 , … , 𝑗 𝑙−1 ) is equal to: 𝐽𝐺(𝑗 0 , 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙−1 𝑦 2 𝑙−1 , … , 𝑦 2 𝑙 −1 , 𝑗 1 , … , 𝑗 𝑙−1 , 𝑀𝑃𝑃𝐿𝑉𝑄 𝑙−1 (𝑦 0 , … , 𝑦 2 𝑙−1 −1 , 𝑗 1 , … , 𝑗 𝑙−1 ) 17
Base Case Next Step 18
LOOKUP2 LOOKUP3 and 4 19
Counting Gates Show this uses at most 4 ⋅ 2 𝑙 gates (lines of code) 20
Counting Gates Show this uses at most 4 ⋅ 2 𝑙 gates (lines of code) 21
Computing Every Finite Function • Next we'll show that NAND is universal • Any finite function can be computed by some NAND-straightline program (equivalently, a NAND-circuit) 22
Idea Input Output We will have one variable to represent each 000 0 possible input. We'll do a lookup with the 001 0 actual input to select the proper output 010 1 011 0 100 1 101 1 110 0 111 0 23
Straightline Code for F Input Output 000 0 001 0 010 1 011 0 100 1 101 1 110 0 111 0 24
Getting 0 and 1 25
Computing any function • Make a variable to represent each possible input • Assign its value to match the correct output • Use LOOKUP to select the proper output for the given input 26
How many gates? • How many gates does this construction take? You can compute any finite function 𝑔: 0,1 𝑜 → 0,1 𝑛 with a NAND Circuit using no more than 𝑑 ⋅ 𝑛 ⋅ 2 𝑜 gates 2 𝑜 Note: This can be imporved to 𝑑 ⋅ 𝑛 ⋅ 𝑜 (theorem 4.16 in TCS) 27
Counting gates 1. Create variables for each input 2. Assign 0,1 to each input 3. Do the LOOKUP 28
What does this mean? • Your laptop is a 64-bit machine. Given enough transistors, it can compute any function 𝑔: 0,1 64 → 0,1 64 29
Recommend
More recommend