Arithmetic on unary numbers, in logarithmic space #
Counter programs (Turing.CounterProgram) that read a pair of unary numbers a, b into
registers with Turing.CounterProgram.Words.headerCode and evaluate an arithmetic expression
(Turing.CounterProgram.Expr) in them. The value is emitted in unary (a counting loop), in binary
(a loop over the powers of two up to the value), or compared with the value of a second
expression. Because every expression grows at most polynomially in the register values
(Turing.CounterProgram.Expr.exists_eval_le_pow), the registers stay polynomially bounded in the
input length, so any function given by an expression is computable in logarithmic space by
Turing.CounterProgram.computableInLogSpace_of_reachesOut.
The instances are unary multiplication, truncated subtraction, quotient, remainder, minimum and
maximum, the comparisons <, ≤ and =, and the conversion from unary to binary, stated for
functions computed in logarithmic space from a common input; all but the conversion are
registered with fun_prop. Counting the digits of the binary form gives the binary length
Nat.size and ⌊log₂⌋ of a unary number, and the length of a bit word in unary is a
finite-state transduction.
Main definitions #
Turing.CounterProgram.Unary.unaryProgram,.binaryProgram,.ltProgram: emitting the value of an expression in unary or binary, and comparing the values of two expressions.
Main results #
Turing.CounterProgram.Expr.exists_eval_le_pow: every expression grows at most polynomially in the register values.Turing.CounterProgram.Unary.computableInLogSpace_unary,Turing.CounterProgram.Unary.computableInLogSpace_binary: a function of two unary numbers given by an expression, with unary or binary output, in logarithmic space.Turing.CounterProgram.Unary.computableInLogSpace_lt: comparing two such functions.Turing.MultiTapeTM.ComputableInLogSpace.unaryMul,.unarySub,.unaryDiv,.unaryMod,.unaryMin,.unaryMax,.unaryLt,.unaryLe,.unaryEq,.unaryToBinary: the instances.Turing.MultiTapeTM.ComputableInLogSpace.bitsLength,.unarySize,.unaryLog2: lengths and logarithms in unary.
References #
Programs #
The program emitting the value of e in unary: after the header, label 4 stores the value
in register 3, and the counting loop at 5 emits a true for each value of register 2
below it.
Instances For
The program emitting the value of e in binary, least significant bit first: after the
header, label 4 stores the value v in register 3; register 2 runs over the powers of two
p ≤ v, and for each the program emits the bit v / p % 2.
Instances For
The program comparing the values of e₁ and e₂: after the header, it accepts if the value
of e₁ is less than that of e₂, and rejects otherwise.
Instances For
Runs #
Logarithmic space #
A function of two unary numbers given by an expression, with unary output, is computable in
logarithmic space. The expression e is evaluated with a in register 0, b in register
1, and the other registers zero.
A function of two unary numbers given by an expression, with binary output, is computable in
logarithmic space. The expression e is evaluated with a in register 0, b in register
1, and the other registers zero.
Comparing two functions of two unary numbers given by expressions is computable in logarithmic space.
Instances #
Multiplying unary numbers computed in logarithmic space from a common input.
Truncated subtraction of unary numbers computed in logarithmic space from a common input.
The quotient of unary numbers computed in logarithmic space from a common input.
The remainder of unary numbers computed in logarithmic space from a common input.
The minimum of unary numbers computed in logarithmic space from a common input.
The maximum of unary numbers computed in logarithmic space from a common input.
Comparing unary numbers with <, computed in logarithmic space from a common input.
Comparing unary numbers with ≤, computed in logarithmic space from a common input.
Testing unary numbers for equality, computed in logarithmic space from a common input.
Converting a unary number to binary, computed in logarithmic space. It changes only the
output encoding, so it is not registered with fun_prop.
The length of a bit word, in unary, by a finite-state transducer that replaces every bit by
true.
The binary length of a unary number, in unary: convert it to binary and count the digits.
⌊log₂⌋ of a unary number, in unary: one less than its binary length.