Definition
Decidability and semidecidability are properties of a yes-or-no question, such as whether a program can reach an error, that say what an algorithm §14.1 can do with the question. A question is decidable when one algorithm stops on every input with the correct answer. It is semidecidable when an algorithm stops with yes on every input whose answer is yes, but may run forever on an input whose answer is no. A question that is not decidable is undecidable. For a semidecidable question, a yes always arrives; a no may not.
The idea
A checker searches the executions of a program for a write outside an array §14.5. After one minute it has printed nothing. Two very different situations produce that silence: the program has no such write, or it has one at the end of an execution longer than anything the checker has examined. A violation, once found, is a finite object: a sequence of steps that anyone can replay to confirm the bad write. Safety is a claim about every execution, and a program that accepts unbounded inputs or runs for unbounded time has infinitely many executions.
Searching an endless beach for a lost coin is a fair picture. If the coin is on the beach, a search that covers the beach strip by strip reaches it after a finite time, and finding it settles the question. If there is no coin, the search never ends, and at no moment has it shown that there is none. On a beach of known, finite size, a complete search does establish absence. The picture stops in one respect: absence can sometimes be proved without searching, by an argument that nothing could have brought a coin there. Proofs of that kind exist for many programs, but undecidability means that no single algorithm settles the question for every program.
Testing is a search of this kind. Each test runs one execution. A failing test is a found coin: a concrete execution that shows the violation. A passing test suite has examined finitely many executions and says nothing about the others.
How it works
Each term has a precise meaning.
A decision problem is a yes-or-no question asked about every member of a set of inputs, called instances. Each instance is a finite string, such as the text of a program together with its input. The instances whose answer is yes form a set, and solving the problem means recognizing that set.
An algorithm here means a program for a machine with unlimited memory. The standard mathematical model is the Turing machine: a finite table of rules that reads and writes symbols on an unbounded tape. Every common programming language, given unlimited memory, can solve exactly the same problems, so the choice of model changes none of the results below.
A witness for a yes-instance is a finite string that shows the answer is yes and that a separate algorithm can check by a computation that always stops.
An algorithm for a semidecidable problem can work as a search for witnesses.
List all candidate witnesses in a fixed order: all strings of length 1, then all of length 2, and so on, so that every finite string appears at some finite position.
Check each candidate in turn. Each check stops, because checking a witness is a computation that always stops.
Stop with yes at the first candidate that passes.
If the answer is yes, a witness exists, it sits at some finite position in the list, and the search reaches it. If the answer is no, no candidate ever passes and the search runs forever. A problem is semidecidable exactly when its yes-instances have witnesses of this kind.
A safety property §1.11 says that something bad never happens, for example that no write lands outside its buffer. Every violation has a finite witness, called a bad prefix: the beginning of an execution, up to and including the step that does the bad thing. Checking a claimed bad prefix means replaying its steps from the start and confirming that each step follows from the one before and that the last step is bad, a computation that stops. So the question “can this program violate this safety property?” is semidecidable whenever each step of the program can be computed and the possible inputs can be listed.
Some runs never stop, so the search must not wait for any one run to finish. It interleaves the runs instead. At stage , it runs each of the first inputs from the start for up to steps. A violation at step of the run on input number is found at stage , the first stage that both includes that input and runs it long enough. This interleaving is called dovetailing.
Two facts connect the terms. First, a problem is decidable exactly when both the problem and its complement, the same question with yes and no exchanged, are semidecidable: run the two searches in alternation, one step each, and one of them stops. Second, some semidecidable problems are not decidable. The standard one is the halting problem §19.2.2: whether a given program stops on a given input. Running the program confirms every yes, and Alan Turing’s 1936 result implies that no algorithm confirms every no. The halting problem entry gives the proof.
Safety inherits this limit. From any program and input , build a program that first simulates on and, if that simulation stops, performs a bad write. The built program is safe exactly when never stops on . An algorithm that confirmed every safe program would therefore confirm every run that never stops, which no algorithm can do. For programs in a general programming language, a violation is semidecidable and safety is not: a search confirms every unsafe program, and no algorithm confirms every safe one.
Worked example
A program reads a whole number . Suppose that exactly one of its executions is unsafe: on input 3, the fifth step writes outside an array. Some other inputs make the program run forever, and the rest stop safely. Stage of the dovetailing search runs inputs 1 to for up to steps each, which costs at most steps.
Stages 1 to 4 cost at most steps. They never run input 3 beyond its fourth step, so they find nothing.
Stage 5 runs input 1 for up to 5 steps, input 2 for up to 5 steps and then input 3, whose fifth step is the bad write. Up to that point stage 5 costs at most steps.
The search stops with the answer unsafe after at most steps, at stage . The five steps of input 3 are a witness that anyone can replay.
A test suite that ran inputs 1 to 4 for up to 4 steps each did the same work as stages 1 to 4, and it passed. Its result means “no violation within 4 steps on inputs 1 to 4”, not “safe”.
Now suppose instead that the program has no unsafe execution. Stage still costs up to steps, and the search never stops. After stage 100 it has spent up to steps, and it knows nothing about input 101 or about step 101 of any input.
Contrast a finite case. If the input is one byte, with 256 possible values, and every run is known to stop within 20 steps, then running every input to completion takes at most steps and decides safety, because every execution has been seen.
Stated precisely
Fix a finite alphabet , such as the 256 byte values, and let be the set of all finite strings over it. A decision problem is a set , the yes-instances.
is decidable when some algorithm stops on every and outputs yes exactly when .
is semidecidable, also called recursively enumerable, when some algorithm stops with yes on exactly when . For , outputs no or never stops.
Equivalently, is semidecidable when there is a decidable relation , a set of pairs of strings whose membership an algorithm decides, such that exactly when holds for some . The string is the witness.
is decidable exactly when both and its complement are semidecidable.
For safety, let be the set of pairs , each a program and an input written as one string, such that the execution of on reaches a bad state. Take as the witness the number of steps to the bad state. Checking the witness runs on for steps, which stops, so is semidecidable. The argument assumes three things: each step of can be computed from the current state; the property is a safety property, so each violation shows in a finite prefix; and the replayed steps follow the real semantics of the system, meaning the exact definition of what each instruction does, with a test oracle §10.5 that judges a state bad correctly. For a language that can simulate every Turing machine, the complement of , the set of safe pairs, is not semidecidable, by the construction above.
Limits and common mistakes
Semidecidable says nothing about speed. A violation may need more steps, or a larger input, than any feasible search reaches.
A search that has not stopped proves nothing. A checker should report four distinct outcomes: a violation found, with its witness; safety proved, by a complete search of a finite model or by a proof; unknown, because a time or memory budget ran out; and unsupported, because the checker cannot model some feature of the program. Reporting unknown as safe is the central mistake.
The finite-witness argument covers safety properties only. A liveness property §1.12 says that something good eventually happens, for example that every request gets a response. Its violation is an infinite execution in which the response never comes, and no finite test shows it: a run that has not responded yet might respond at the next step. A timeout §9.34 shows only that the response took longer than the timeout.
Decidable does not mean practical. A finite problem is decidable by exhaustive search, but the search may need more steps than any computer can perform; this is the state explosion §3.26 problem.
Undecidable does not mean hopeless. A sound analysis that always stops, answering safe only when it has a proof and unknown otherwise, is possible; it cannot answer safe for every safe program. Restricting the programs, for example to loops with fixed bounds, can make the question decidable.
A test establishes a violation only for the system it actually ran. A violation found in a model or a test harness that allows something the real system cannot do is a false alarm §1.22 for the real system, and a wrong test oracle can flag correct behavior.
Questions answered
Why can testing establish a violation but fail to establish safety?
Testing can establish a violation but not safety because a violation shows in one finite execution, while safety is a claim about every execution, and a program with unbounded inputs or unbounded running time has infinitely many executions. For a safety property, a property saying that something bad never happens (such as “no write lands outside its buffer”), every violation ends at a finite step, so a failing test is a concrete witness: a sequence of steps that anyone can replay to see the bad event. A passing test suite has examined finitely many executions, and a violation may lie in one it did not run. Tests of every execution up to 4 steps, for example, miss a first bad write at step 5.
In terms of computability, “this program can violate the property” is semidecidable: a systematic search stops with yes whenever the answer is yes, but may run forever when the answer is no. “This program is safe” is, for programs in a general programming language, not even semidecidable, because confirming it is at least as hard as confirming that a program never stops, which no algorithm can do. Safety can be established only by other means: a proof that covers all executions, or an exhaustive test of a finite, faithfully modeled set of cases.
Related entries: Bounded model checking (§3.29), Conservative refusal (§3.36), Exhaustive enumeration (§10.33), Safety property (§1.11), Liveness property (§1.12), Halting problem, Rice’s theorem, and restricted verifiers (§19.2.2), Why fuzzing cannot certify universal safety (§17.18). Further reading: [158].