Program with no intermediary states

26 Views Asked by At

Every program P which built of function sequence (order counts): $F_1,..,F_n$, where $F_i$ returns $R_i$ and $F_{i+1}$ takes $R_i$ as an argument, can be shown as $F_1(F_2(F_3(...(F_n))))$, i.e. we do not need to store intermediary program states.

Can every program be transformed to such without intermediary states?

1

There are 1 best solutions below

0
On BEST ANSWER

Yes, as long as the semantics of the underlying programming language are effective and we allow higher-order functions.

Assume for the sake of the argument that we are given big-step semantics for the underlying programming language, using the notation $s_1 \to s_2$, where $s_1$ and $s_2$ are program states. Suppose furthermore that the underlying programming language is "roughly imperative": We have several base commands (e.g., assignment) that are joined into a sequence. Then the execution of a sequence $c_1; c_2; \ldots c_n$ of commands corresponds to executing the big-step reductions for $c_1, c_2, \ldots, c_n$ in sequence. Since we assumed that the semantics are effective, in the sense that there are recursive functions implementing the reduction rules, this boils down to applying the corresponding functions implementing the semantics in sequence.

If we add control-flow constructors like if or while, things get a bit more interesting. Consider for example the following simple if command: $\text{if } e \text{ then } c$, where $e$ is an expression and $c$ is a command. The semantics could be given by the two big-step reduction rules $$ \frac{e(s_1)=1\quad s_1 \to_c s_2}{s_1 \to_{\text{if } e \text{ then } c} s_2} \qquad \frac{e(s_1)=0}{s_1 \to_{\text{if } e \text{ then } c} s_1} $$ This gives rise to an evaluation function of the form $E_\text{if}(E_e,E_c,s)$ which takes two functions as arguments, namely the evaluation functions for $e$ and $c$, and the execution state $s$. $E_\text{if}$ is clearly recursive. Since the evaluation functions for $E_e$ and $E_c$ can be derived from the program being considered, we can treat them as parameters and therefore get an implementation function $E_{\text{if } e \text{ then } c}$ that maps execution states to execution states, as above.