Impossible to convert a program without meta logic.
![{\displaystyle fp=\lambda i.(r,o)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/06be7dbdc3ddb9e4e851d8db9cd8fabb67affb70)
![{\displaystyle \sigma _{s}(i,f,m)\ \sigma _{s}(m,x,o)=\sigma _{s}(i,f\ x,o)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/5a0da9b7dcb9e5bd206c2b4a340d4b6bde7993b2)
![{\displaystyle \lambda i.(f,m)\ (\lambda m.(x,o)\ m)=\lambda i.(f\ x,o)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/18117202e1f9fa5305f67bc8d2b1a1d7ec83b12d)
![{\displaystyle \alpha i\eta (fp)=(r(\alpha ieta(f)))(\alpha (o(\alpha ieta(f)))eta(p))}](https://wikimedia.org/api/rest_v1/media/math/render/svg/eb924814e0332dc43adffa302a8c521601f8da4e)
![{\displaystyle \alpha ifp=(r(\alpha if))(\alpha (o(\alpha if))p)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/5ca731c796e1436639c2e628b51487dce0564f81)
![{\displaystyle hf(p)g(q)=h(\lambda i.(r_{p},o_{p}))(\lambda i.(r_{q},o_{q}))}](https://wikimedia.org/api/rest_v1/media/math/render/svg/e417ff8c5cebc104d0a788df66f1d6d52ec8bd13)
![{\displaystyle hf(p)g(q)=h(\lambda i.(r_{p},o_{p}))(\lambda i.(r_{q},o_{q}))o_{p}}](https://wikimedia.org/api/rest_v1/media/math/render/svg/06c62218f6667db5bf273f6f32ad01ee2ee1bbe3)
![{\displaystyle fp=f(po(f))}](https://wikimedia.org/api/rest_v1/media/math/render/svg/bcd759a935f6e59450ec87a2fe4ae2db799822b8)
![{\displaystyle \alpha f;p=pf}](https://wikimedia.org/api/rest_v1/media/math/render/svg/e487877d621b6f5a6cfada9c91dc0f8ae071f47a)
![{\displaystyle \alpha ifp=(r(\alpha if))(\alpha (o(fi))p)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/40dc2854a117263f7e684b2ff31ee9ab5ad00753)
![{\displaystyle r(\alpha if)=i[f]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/76070cb46d4cd8d2a07830590cf7a120c73f174e)
![{\displaystyle o(\alpha if)=f}](https://wikimedia.org/api/rest_v1/media/math/render/svg/3b1e05d5b85409fcbca177a3246b21deee0c420e)
![{\displaystyle \alpha i(x:=v)=({\text{true}},i[x:=v])}](https://wikimedia.org/api/rest_v1/media/math/render/svg/48ea8e8c4e62fa178e2504ce07bc4cb13a2786c3)
![{\displaystyle i[x:=v]x=v}](https://wikimedia.org/api/rest_v1/media/math/render/svg/7d4548e66a14c8bdb75f51ec52b9d5a111d3d073)
![{\displaystyle \alpha i(x:=v)=({\text{true}},i[\eta (x):=v])}](https://wikimedia.org/api/rest_v1/media/math/render/svg/f133101d403c3486bd2f87d78638afd1e4ce3a3b)
![{\displaystyle i[x:=v]x=v}](https://wikimedia.org/api/rest_v1/media/math/render/svg/7d4548e66a14c8bdb75f51ec52b9d5a111d3d073)
Imperative programs allow the modification of state. State consists of many state variables. A state variable is the name which identifies different values at different points in the execution of a program.
For each state variable, a statement has an input value and an output value. The input value and the output value may be the same, or the statement may calculate a new output value from the input value.
To represent this mathematically, a statement has an input i, parameters p, and output r, and an output o. An imperative statement,
![{\displaystyle f(p)=r}](https://wikimedia.org/api/rest_v1/media/math/render/svg/db349cd0bd9be5289b4ed5befd92b5acf9ff39a2)
actually means,
![{\displaystyle f(i,p)=(r,o)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/94d2f6afc75b9eb72870a794fe5e4ab48ec0fafa)
The return value and the output state are calculated from the input state and the parameter. In a series of statements the outputs of proceeding statements need to be connected to the inputs of proceeding statements. An imperative statement does not explicitly represent this input.
The input state is not explicitly represented. One possibility is that the function should be represented as a lambda expression
![{\displaystyle f(p)=\lambda i.(r,o)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/be12221689425c7a33214e5f8fc96027faa8d812)
State passed by function composition
[edit]
[1]
In many languages the ; is used to separate statements. For example.
![{\displaystyle x:=6;x=x+1;x:=x*2}](https://wikimedia.org/api/rest_v1/media/math/render/svg/08fd9903e3442dda60fe61be7c3f7b651151a4bc)
An assignment statement is represented as a lambda expression as,
![{\displaystyle a:=b\equiv \lambda i.i[\eta (a):=b]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/cee6e9519ac762ed39833e5c44ac3646af59818a)
If the statement separator is regarded as function composition,
![{\displaystyle r;s\equiv r\circ s=s\ r}](https://wikimedia.org/api/rest_v1/media/math/render/svg/b143d9e8c8a7f0046b8d01431890e9386dee1b7a)
Then the series of statements becomes,
![{\displaystyle x:=6;x:=x+1;x:=x*2}](https://wikimedia.org/api/rest_v1/media/math/render/svg/4f82ca2bb708bc02a4b172a04e5e8276422e3ca7)
![{\displaystyle \equiv ((\lambda i.i[\eta (x):=6]\circ \lambda i.i[\eta (x):=i[x]+1])\circ \lambda i.i[\eta (x):=i[x]*2])}](https://wikimedia.org/api/rest_v1/media/math/render/svg/2b3e7409ceb7893ca715ab88030fe62a94bcbd56)
![{\displaystyle \equiv (\lambda i.i[\eta (x):=i[x]*2])\ (\lambda i.i[\eta (x):=i[x]+1])\ (\lambda i.i[\eta (x):=6])\ s}](https://wikimedia.org/api/rest_v1/media/math/render/svg/41119eee23d48ef1ce372cdc1852713fade005a6)
![{\displaystyle \equiv (\lambda i.i[\eta (x):=i[x]*2])\ (\lambda i.i[\eta (x):=i[x]+1])\ s[\eta (x):=6]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/46f28b527417876929bc6572ffaa11d059462400)
![{\displaystyle \equiv (\lambda i.i[\eta (x):=i[x]*2])\ s[\eta (x):=6][\eta (x):=s[\eta (x):=6][x]+1]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/5a280c7813e911c772ae5999e0fb87237e21ffb5)
![{\displaystyle \equiv (\lambda i.i[\eta (x):=i[x]*2])\ s[\eta (x):=6][\eta (x):=7]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/bd5c480004da487c61bc52af12d0bb0549ccce07)
![{\displaystyle \equiv (\lambda i.i[\eta (x):=i[x]*2])\ s[\eta (x):=7]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/892dbf525b1e89cd5381f7533ba5fce377c6bb84)
![{\displaystyle \equiv s[\eta (x):=7][\eta (x):=s[\eta (x):=7][x]*2]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/15eba0754165401874365f041f2e118eb64de866)
![{\displaystyle \equiv s[\eta (x):=7][\eta (x):=14]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/b688d2c3e29999c90254372e60f313cd54f7e869)
![{\displaystyle \equiv s[\eta (x):=14]}](https://wikimedia.org/api/rest_v1/media/math/render/svg/f96081a81c4c64baba9c46bb2824e481b383e635)
Another approach is to regard the assignment statement as a lambda expression,
![{\displaystyle a:=b;c\equiv (\lambda a.c)\ b}](https://wikimedia.org/api/rest_v1/media/math/render/svg/815999b33e085d4f4288764fb10bafdab4fcf49f)
![{\displaystyle a:=b\equiv b}](https://wikimedia.org/api/rest_v1/media/math/render/svg/b19bee282cf50fe99ca8261c72159416345cc0fe)
Then by use of bracketing,
![{\displaystyle x:=6;(x:=x+1;x:=x*2)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/38d9dab508be3e4a8ba4b7657f34d0cabe9b60a7)
![{\displaystyle \equiv (\lambda x.x:=x+1;x:=x*2)\ 6}](https://wikimedia.org/api/rest_v1/media/math/render/svg/b44f788fde95d58a824c62eb4883c433990380ae)
![{\displaystyle \equiv (\lambda x.(\lambda x.x:=x*2)\ x+1)\ 6}](https://wikimedia.org/api/rest_v1/media/math/render/svg/7663960ccd2f9276709391bcfd729d8439622914)
![{\displaystyle \equiv (\lambda x.(\lambda x.x*2)\ 6)x+1)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/c1bce3582144560c73b771ea61cfdf307f700c42)
![{\displaystyle \equiv 14}](https://wikimedia.org/api/rest_v1/media/math/render/svg/825c13d435e5ff50fc289f519eb85864c310e161)
However both these approach only work for a series of statements. For imperative functions with side effects returning results that are combined using other operators this approach does not give the right structure.
State under function application
[edit]
In the relational programming model it is not necessary to consider the output as being calculated from the input. The state holder notation includes the input as part of a triple returned by the function.
![{\displaystyle f(p)=\sigma _{s}(i,r,o)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/d14bb0050b47444d22c9eb0eb9b535372dc7302a)
In this representation the state holder does not actually behave like a mathematical object. It does not obey the substitution rule,
![{\displaystyle a=b\implies g(a)=g(b)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/c86289e9189b24597d96047f89dd243cbfe65447)
But is a good fit for an assignment statement, which is also not mathematical.
For example,
![{\displaystyle x:=5;x:=x+1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/74ad99c429a40ea05a8bc2ecb01a79f437571186)
The assignment statement here is interpreted as returning the value true if it is successful. Note that this is not the interpretation in the C language, but it allows the ; to be interpreted as conjunction,
![{\displaystyle x:=5\land x:=x+1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/ddbaf2407d6ba8b73a57b2bc80445ecf11dafed1)
Using state holders this expression is equivalent to,
![{\displaystyle \sigma _{x}(x_{1},{\text{true}},5)\land \sigma _{x}(x_{2},{\text{true}},x_{2}+1)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/97801a06c2baa0327ef6bddc912fbcaa44cf0bc0)
As the output of the first statement is the input to the next statement then,
![{\displaystyle x_{2}=5}](https://wikimedia.org/api/rest_v1/media/math/render/svg/ca69f7eb73c62c802683b0f9bd74d6ea78c24a41)
and the statement simplifies to,
![{\displaystyle \sigma _{x}(x_{1},{\text{true}},6)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/16e88e1eef9b4a04b9345f1f6847e954d94c71e7)
which is equivalent to,
![{\displaystyle x:=6}](https://wikimedia.org/api/rest_v1/media/math/render/svg/651dbe246a1ec8acf65ff6b57aa144bbc128f220)
The triple of input, result, and output is called a state holder.