Booleans

An abstract data type is a collection of values and operations (in Pascal terminology, procedures and functions) associated with them. Each operation in an abstract data type is specified by its interface: its inputs (that is, the values it receives as parameters), its outputs (the values it returns, either as function values or through variable-parameters), its preconditions (the conditions on the inputs that must be satisfied in order for the operation to succeed), and its postconditions (the conditions that are guaranteed to hold at the successful completion of the operation).

A data type presented this way is ``abstract'' in the sense that no mention is made of its implementation -- the actual representations of the values at the machine level and the machine instructions by which the operations are actually performed. Even the algorithms used to implement the operations are abstracted away. Abstract data types are (theoretically) implementation-independent -- the interface is the same regardless of the computer, compiler, or programming environment one happens to be using.

As an example, let's consider how Booleans would be described as an abstract data type. There are two Boolean values, and we should specify a name for each one; we might as well use the standard Pascal identifiers true and false. As for operations, designers haven't reached a complete consensus about which ones are essential enough to appear on the roster, but I'd propose the following list:

negate
Input: negand, a Boolean.
Output: result, a Boolean.
Preconditions: none.
Postcondition: result is false if negand is true and true if negand is false.

conjoin
Inputs: left-conjunct and right-conjunct, both Booleans.
Output: result, a Boolean.
Preconditions: none.
Postcondition: result is true if left-conjunct and right-conjunct are both true and false if either is false.

disjoin
Inputs: left-disjunct and right-disjunct, both Booleans.
Output: result, a Boolean.
Preconditions: none.
Postcondition: result is true if either left-disjunct or right-disjunct is true and false if both are false.

Pascal programmers will recognize these operations as not, and, and or respectively.

exclusively-disjoin
Inputs: left-alternative and right-alternative, both Booleans.
Output: result, a Boolean.
Preconditions: none.
Postcondition: result is true if one and only one of the inputs is true and false if both are true or both are false.

It is perhaps less obvious that exclusively-disjoin is provided by standard Pascal, in the guise of the relational operator <>. Some implementations of Pascal provide a second name for the same operator, often xor.

equal
Inputs: left-operand and right-operand, both Booleans.
Output: result, a Boolean.
Preconditions: none.
Postcondition: result is true if the two inputs are the same (both true or both false) and false if they are different (one true and the other false).

read
Input: source, a data source (e.g., a file, the keyboard, a device).
Outputs: legend and success, both Booleans.
Preconditions: none.
Postcondition: Either some representation of a Boolean value has been extracted from source and legend is that Boolean value, or an input error of some kind has occurred and success is false.

The reason for providing success as an output, rather than simply making it a precondition of the read operation that the data source is accessible and has a representation of a Boolean ready for extraction, is that it should be possible to test whether the preconditions for an operation are satisfied without actually performing the operation. In practice, there is no way to determine whether a Boolean can be extracted from a data source without actually trying to extract it, so it would be pointless to formulate such a precondition.

Standard Pascal does not provide the read operation if the data source is a text file; HP Pascal extends the Read and ReadLn procedures to Booleans, but crashes if an input error occurs instead of setting a success variable.

write
Inputs: target, a data sink (e.g., a file, a window, a device), and scribend, a Boolean.
Outputs: none.
Preconditions: none.
Postcondition: A representation of scribend has been appended to target.

Standard Pascal also provides seven other operations on Booleans: the inequality relations <, <=, >, and >=, the function Ord (which maps false to the integer 0 and true to 1), and the functions Pred and Succ. These are easily defined in terms of the operations listed above, if they are wanted, which they seldom are.


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/courses/fundamentals/Booleans.html

created July 23, 1996
last revised September 9, 1996

John David Stone (stone@math.grin.edu)