Input, State, and Output in a Program
Distinguish information supplied to a run from the state retained during it and the observations it emits along the way.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 2 min
In this article4 chapters
Draw the boundaries
A temperature report may read five values, maintain the current maximum, and then display a number. The five supplied values are input. The retained maximum is state. The printed number is output. A trace can omit temperatures already processed if the current maximum is the only remembered information relevant to the rest of the run.
Predict two observations
Give one run temperatures 3 and 8, and another run 8 and 3. Both final maxima are 8. If the program prints whenever the maximum rises, the output sequences differ. Decide which observations belong to the public behavior before changing the implementation. In particular, returning the answer and printing each update expose different observable effects.[1][2]
Track what an online calculation needs to remember
Imagine reading one bank account adjustment at a time. The required output is the current balance after each adjustment. Start at an opening balance of ten and read \(+4\), \(-3\), and \(+2\). The program can hold one running balance. Each update produces fourteen, eleven, and thirteen. Because the state already summarizes the visited adjustments, this particular feature does not need a retained list of them. The same approach produces prefix totals where intermediate answers matter.
Change the requirement to output the largest withdrawal so far and the position where it occurred. A running balance alone cannot recover that information. Two histories of adjustments can reach the same balance and contain different withdrawals. Add the best eligible withdrawal and its position to the state. This is how output requirements guide a minimal representation.
Find dependencies that look like state from outside
Whether elapsed time belongs to input or state depends on where it is obtained. A loop receiving a time as part of each observation has it in its inputs. A routine that samples a clock internally has an effectful dependency whose observations need to be controlled in a test. See clock dependencies for keeping that boundary explicit.
To review a feature, write down which information arrives on each step, which facts are retained, and which results leave. Test two distinct histories ending in the same retained state. If the specification requires different outputs afterward, your proposed state representation may have discarded a necessary fact.