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. Het programma
4 dat we gaan vertalen berekent de faculteit van $x$ en stopt dat uiteindelijk in
6 \begin{lstlisting
}[title=Faculteit in
\textit{While
}]
16 In
\textit{Piet'
} ziet dit er als volgt uit...
17 \begin{lstlisting
}[title=Faculteit in
\textit{Piet'
}]
23 // $
\neg$(x=
1) $
\equiv$ $
\neg$(x-
1)>
0)
39 push
1 // zet $
0$ op de stack
43 gre // $
\neg(x-
1)>
0$ oftewel x=
1
45 pointer // als x=
0 dan draait de DP niet en gaat het programma naar pad B
46 // als x$
\neq$ dan draait de DP en gaat het programma naar pad A
49 skip // een oneindig aantal witte blokken, y is nu x!
50 // evt een outchar om $y$ naar standardout te printen
88 // nu gaat het programma weer via een wit pad naar markering A
91 Wat in
\textit{Piet
} er uit ziet als in Figuur~
\ref{fig:img3
}
93 \caption{Faculteit vanuit
\textit{While
} naar
\textit{Piet
}}
96 \fbox{\includegraphics[natheight=
320px, natwidth=
256px, height=
320px,
97 width=
256px
]{img3.png
}}
101 De afleidingsrij voor
1! ziet er als volgt uit:\\
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;push \:
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;push \:
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;push \:
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;push \:
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;roll,
[\:
],
[\:
],
[1,
2,
1,
1]\rangle \Rightarrow\\
108 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3;dup,
[\:
],
[\:
],
[1,
1]\rangle \Rightarrow\\
109 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
110 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1,
[\:
],
[\:
],
[3,
1,
1,
1]\rangle \Rightarrow\\
111 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll,
[\:
],
[\:
],
[1,
3,
1,
1,
1]\rangle \Rightarrow\\
112 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
113 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;,
[\:
],
[\:
],
[1,
1,
1,
1]\rangle \Rightarrow\\
114 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
115 \langle pointer; not;greater;subtract;push\:
1;push \:
1,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
116 \langle pointer; not;greater;subtract;push\:
1,
[\:
],
[\:
],
[1,
1,
1,
1]\rangle \Rightarrow\\
117 \langle pointer; not;greater;subtract,
[\:
],
[\:
],
[1,
1,
1,
1,
1]\rangle \Rightarrow\\
118 \langle pointer; not;greater,
[\:
],
[\:
],
[0,
1,
1,
1]\rangle \Rightarrow\\
119 \langle pointer; not,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
120 \langle pointer
[\:
],
[\:
],
[0,
1,
1]\rangle \Rightarrow\\
121 \langle [\:
],
[\:
],
[1,
1]\rangle\\
124 Eerst worden x en y op de stack gestopt zodat de stack
[y,x
] =
[1,
1].
125 Deze stack is gelijk aan de stack in de termineerende configuratie:
1 =
1! dus y = x!.
126 Als bewijs dat de wiskundige operaties op de stack goed zijn uitgevoerd staat in
127 de appendix onder het kopje voorbeelden een stukje code. In het programma
128 eenfaculteit worden alle wiskundige operaties die bij de commando's horen
129 achter elkaar uitgevoerd op de stack, waarbij het resultaat
[1,
1] is.\\
130 Bij de voorbeelden staan ook nog de programma's tweefaculteit en
131 vijffaculteit die respectievelijk
2! en
5! uitrekenen.