2 Als we de analyse op een compleet programma toepassen komt er een
\textit{Piet
}
3 programma uit dat behoorlijk fors is maar netjes zijn werk doet. Dit programma
4 berekent de faculteit van $x$ en stopt dat uiteindelijk in $y$.
5 \begin{lstlisting
}[title=Faculteit in
\textit{While
}]
15 In
\textit{Piet
} ziet dit er als volgt uit...
16 \begin{lstlisting
}[title=Faculteit in
\textit{Piet'
}]
22 // $
\neg$(x=
1) $
\equiv$
\neg(x-
1)>
0)
38 push
1 // zet $
0$ op de stack
42 gre // $
\neg(x-
1)>
0$ oftewel x=
1
44 pointer // als x=
0 dan draait de DP niet en gaat het programma naar pad B
45 // als x$
\neq$ dan draait de DP en gaat het programma naar pad A
48 skip // een oneindig aantal witte blokken, y is nu x!
49 // evt een outchar om $y$ naar standardout te printen
87 // nu gaat het programma weer via een wit pad naar markering A
90 Wat in piet er uit zit als in
\ref{fig:img3
}
92 \caption{Factulteit vanuit
\textit{While
} naar
\textit{Piet
}}
95 \fbox{\includegraphics[natheight=
320px, natwidth=
256px, height=
320px,
96 width=
256px
]{img3.png
}}
100 De afleidingsrij voor
1! ziet er als volgt uit:\\
102 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3;dup;roll;push \:
1;push \:
2;push \:
1;push \:
1,
[\:
],
[\:
],
[\:
]\rangle \Rightarrow\\
103 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3;dup;roll;push \:
1;push \:
2;push \:
1,
[\:
],
[\:
],
[1]\rangle \Rightarrow\\
104 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3;dup;roll;push \:
1;push \:
2,
[\:
],
[\:
],
[1,
1]\rangle \Rightarrow\\
105 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3;dup;roll;push \:
1,
[\:
],
[\:
],
[2,
1,
1]\rangle \Rightarrow\\
106 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3;dup;roll,
[\:
],
[\:
],
[1,
2,
1,
1]\rangle \Rightarrow\\
107 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3;dup,
[\:
],
[\:
],
[1,
1]\rangle \Rightarrow\\
108 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
109 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1,
[\:
],
[\:
],
[3,
1,
1,
1]\rangle \Rightarrow\\
110 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll,
[\:
],
[\:
],
[1,
3,
1,
1,
1]\rangle \Rightarrow\\
111 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
112 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;,
[\:
],
[\:
],
[1,
1,
1,
1]\rangle \Rightarrow\\
113 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
114 \langle pointer; not;greater;subtract;push\:
1;push \:
1,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
115 \langle pointer; not;greater;subtract;push\:
1,
[\:
],
[\:
],
[1,
1,
1,
1]\rangle \Rightarrow\\
116 \langle pointer; not;greater;subtract,
[\:
],
[\:
],
[1,
1,
1,
1,
1]\rangle \Rightarrow\\
117 \langle pointer; not;greater,
[\:
],
[\:
],
[0,
1,
1,
1]\rangle \Rightarrow\\
118 \langle pointer; not,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
119 \langle pointer
[\:
],
[\:
],
[0,
1,
1]\rangle \Rightarrow\\
120 \langle [\:
],
[\:
],
[1,
1]\rangle\\
123 Eerst worden x en y op de stack gepusht zodat de stack
[y,x
] =
[1,
1].
124 Deze stack is gelijk aan de stack in de termineerende configuratie:
1 =
1! dus y = x!.
125 Als bewijs dat de wiskundige operaties op de stack goed zijn uitgevoerd staat in
126 de appendix onder het kopje voorbeelden een stukje code. In het programma
127 eenfaculteit worden alle wiskundige operaties die bij de commando's horen
128 achter elkaar uitgevoerd op de stack, waarbij het resultaat
[1,
1] is.