140 likes | 277 Views
Using Definite Knowledge: Axiomatizing the Natural Numbers. Notes for Ch.3 of Poole et al. CSCE 580 Marco Valtorta. Constants, function, intended interpretation. The domain: the natural numbers and a special symbol S Two constants: one and zero One function one --- denotes the number 1
E N D
Using Definite Knowledge:Axiomatizing the Natural Numbers Notes for Ch.3 of Poole et al. CSCE 580 Marco Valtorta
Constants, function, intended interpretation • The domain: the natural numbers and a special symbol S • Two constants: one and zero • One function • one --- denotes the number 1 • zero---denotes S; it does not denote a number! • b(N,zero) • denotes 2 x N’, where N denotes the number N’ • b(N,one) • denotes 2 x N’ + 1
Mnemonic • A number represented in b notation can be read as a binary number by replacing one with 1 and zero with 0. Example: • b(b(b(one, zero),one), one) 1 0 1 1 (decimal 11) • b(b(b(one, one), one), one) 1 1 1 1 (decimal 15) • B(b(b(b(one,one),zero),one),zero) 1 1 0 1 0 (decimal 26)
number • numb(N) is true if N denotes (“has the form of”) a number. • For short, we may say, “if N is a number.” • Use numb, because number/1 is predefined in CILOG • numb(one). • numb(b(N,zero)) numb(N). • numb(b(N,one)) numb(N).
successor • succ(N,M) is true if M denotes the successor of (the “number after”) N • succ(one, b(one, zero)). • succ(b(N, zero), b(N, one)) numb(N) & numb(M). • succ(b(N,one), b(M,zero)) <- succ(N,M). • Adding 1 to 1 results in 0 with a carry of 1. The carry may propagate. • Check it!
Plus • plus(X,Y,Z) is true if the number denoted by Z is the sum of the numbers denoted by X and Y (i.e., Z’ = X’ + Y’, or simply Z = X + Y). • plus(one, Y, Z) <- succ(Y, Z). plus(X, Y, Z) <- succ(XMinus1, X) & succ(Y, YPlus1) & plus(XMinus1, YPlus1, Z). • X + Y = Z iff (X-1) + (Y+1) = Z
Times • X * Y = Z iff (X-1) * Y + Y = Z • times(one, Y, Y). • times(X,Y,Z) <- succ(XMinus1, X) & times(XMinus1,Y,Z1) & plus(Z1,Y,Z).
is • Is does expression evaluation • We use is1 because “is” is predefined in CILOG. • is1(E,E) <- numb(E). • is1(V, X+Y) <- is1(V1,X) & is1(V2,Y) & plus(V1,V2,V). • is1(V, X*Y) <- is1(V1,X) & is1(V2,Y) & times(V1,V2,V). • is1(V, X-Y) <- is1(V1,X) & is1(V2,Y) & plus(V2,V,V1).
Less than • lt(one, b(N,zero)) <- numb(N). • lt(one, b(N,one)) <- numb(N). • lt(N,M) <- succ(NMinus1,N) & succ(MMinus1,M) & lt(NMinus1, MMinus1).
Not Equal • neq(N,M) <- lt(N,M). • neq(N,M) <- lt(M,N).
Complexity of plus • Each time the second clause of plus is applied, the first argument is decremented by one: • plus takes linear time in the value of the first argument. • This is exponential time in the size of the binary representation of a number! • A more efficient plus (linear in the size of the representation) can be obtained by simulating addition by hand (with carry). We call it bplus.
Efficient plus • bplus(one, X, S) succ(X,S). • bplus(b(N,D),one,S) succ(b(N,D),S). • bplus(b(R,zero),b(S,zero),b(RS,zero)) bplus(R,S,RS). • bplus(b(R,zero),b(S,one),b(RS,one)) bplus(R,S,RS). • bplus(b(R,one),b(S,zero),b(RS,one)) bplus(R,S,RS). • bplus(b(R,one),b(S,zero),b(SRS,zero)) bplus(R,S,RS) & succ(RS,SRS).
Complexity of times • Each time the second clause of times is applied, the first argument is decremented by one and plus is called: • Plus takes quadratic time in the value of the first argument. • This is doubly exponential in the size of the binary representation! • A more efficient (quadratic in the size of the representation) solution is obtained by simulating hand multiplication. We call this btimes.
Efficient times • btimes(one,X,X). • btimes(b(R,zero), S, b(RS,zero) btimes(R,S,RS). • btimes(b(R,one), S, RSS) btimes(R,S,RS) & bplus(S,b(RS,zero), RSS). • When the third clause is applied, one number is “stripped” at the end of the multiplicand. This appears to be linear-time, but it is not, because the addend in bplus grows in size.