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 \caption{Faculteit vanuit
\textit{While
} naar
\textit{Piet
}(geannoteerd)
}
104 \fbox{\includegraphics[natheight=
320px, natwidth=
256px, height=
160px,
105 width=
128px
]{img5.png
}}
107 De code blokken komen als volgt overeen met stukken
\textit{While
} code.\\
109 A &
\lstinline{x:=
5; y:=
1}\\
110 B &
\lstinline{$
\neg$(x=
1)
}\\
111 C &
\lstinline{y:=y*x
}\\
112 D &
\lstinline{x:=x-
1}\\
113 E & het stukje achter de while loop, hierin printen we de uitkomst zodat we er ook nog wat aan hebben, \\
114 & dit gebeurt in het originele
\textit{While
}
115 programma niet. Het pyramidevormige stuk zorgt voor terminatie.\\
116 F &
\lstinline{while$
\neg$(x=
1) do(y:=y*x; x:=x-
1)
}
120 De afleidingsrij voor
1! ziet er als volgt uit:\\
122 \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\\
123 \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\\
124 \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\\
125 \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\\
126 \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\\
127 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3;dup,
[\:
],
[\:
],
[1,
1]\rangle \Rightarrow\\
128 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1;push \:
3,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
129 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll;push \:
1,
[\:
],
[\:
],
[3,
1,
1,
1]\rangle \Rightarrow\\
130 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1;roll,
[\:
],
[\:
],
[1,
3,
1,
1,
1]\rangle \Rightarrow\\
131 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;push \:
1,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
132 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not;subtract;,
[\:
],
[\:
],
[1,
1,
1,
1]\rangle \Rightarrow\\
133 \langle pointer; not;greater;subtract;push\:
1;push \:
1;not,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
134 \langle pointer; not;greater;subtract;push\:
1;push \:
1,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
135 \langle pointer; not;greater;subtract;push\:
1,
[\:
],
[\:
],
[1,
1,
1,
1]\rangle \Rightarrow\\
136 \langle pointer; not;greater;subtract,
[\:
],
[\:
],
[1,
1,
1,
1,
1]\rangle \Rightarrow\\
137 \langle pointer; not;greater,
[\:
],
[\:
],
[0,
1,
1,
1]\rangle \Rightarrow\\
138 \langle pointer; not,
[\:
],
[\:
],
[1,
1,
1]\rangle \Rightarrow\\
139 \langle pointer
[\:
],
[\:
],
[0,
1,
1]\rangle \Rightarrow\\
140 \langle [\:
],
[\:
],
[1,
1]\rangle\\
143 Eerst worden x en y op de stack gestopt zodat de stack
[y,x
] =
[1,
1].
144 Deze stack is gelijk aan de stack in de termineerende configuratie:
1 =
1! dus y = x!.
145 Als bewijs dat de wiskundige operaties op de stack goed zijn uitgevoerd staat in
146 de appendix onder het kopje voorbeelden een stukje code. In het programma
147 eenfaculteit worden alle wiskundige operaties die bij de commando's horen
148 achter elkaar uitgevoerd op de stack, waarbij het resultaat
[1,
1] is.\\
149 Bij de voorbeelden staan ook nog de programma's tweefaculteit en
150 vijffaculteit die respectievelijk
2! en
5! uitrekenen.