[step:Use finite computation histories for the primitive recursion clause]Assume $g:\mathbb{N}^k\to\mathbb{N}$ is functionally represented in PA by $G(x_1,\dots,x_k,y)$, and assume $h:\mathbb{N}^{k+2}\to\mathbb{N}$ is functionally represented in PA by $H(x_1,\dots,x_k,n,z,w)$. Let $f:\mathbb{N}^{k+1}\to\mathbb{N}$ be defined by primitive recursion:
\begin{align*}
f(a_1,\dots,a_k,0) &= g(a_1,\dots,a_k),\\
f(a_1,\dots,a_k,n+1) &= h(a_1,\dots,a_k,n,f(a_1,\dots,a_k,n)).
\end{align*}
We use the standard PA finite-sequence coding package, obtained by primitive recursive coding of finite lists and by representability of the primitive recursive sequence operations already established in the closure clauses above. Concretely, fix $\mathcal{L}_{\mathrm{PA}}$-formulas
\begin{align*}
\operatorname{Seq}(c,\ell)
\end{align*}
and
\begin{align*}
\operatorname{Entry}(c,i,v)
\end{align*}
where $\operatorname{Seq}(c,\ell)$ states that $c$ codes a finite sequence of length $\ell$, and $\operatorname{Entry}(c,i,v)$ states that the $i$-th entry of the sequence coded by $c$ is $v$. These formulas are chosen so that PA proves the following uniform sentences, with $c,\ell,i,v,w,d$ arbitrary PA variables rather than numerals. First, entries below the length exist uniquely:
\begin{align*}
\mathrm{PA}\vdash \forall c\,\forall \ell\,\forall i\,\bigl(\operatorname{Seq}(c,\ell) \wedge i<\ell \to \exists!v\,\operatorname{Entry}(c,i,v)\bigr).
\end{align*}
Second, coded sequences can be extended by appending one prescribed final entry:
\begin{align*}
\mathrm{PA}\vdash \forall c\,\forall \ell\,\forall w\,\bigl(\operatorname{Seq}(c,\ell) \to \exists d\,(\operatorname{Seq}(d,S\ell) \wedge \operatorname{Entry}(d,\ell,w) \wedge \forall i\,\forall v\,(i<\ell \wedge \operatorname{Entry}(c,i,v) \to \operatorname{Entry}(d,i,v)))\bigr).
\end{align*}
Third, coded sequences admit initial segments:
\begin{align*}
\mathrm{PA}\vdash \forall c\,\forall \ell\,\bigl(\operatorname{Seq}(c,S\ell) \to \exists d\,(\operatorname{Seq}(d,\ell) \wedge \forall i\,\forall v\,(i<\ell \wedge \operatorname{Entry}(c,i,v) \to \operatorname{Entry}(d,i,v)))\bigr).
\end{align*}
For standard finite lists, the same coding package also gives the corresponding numeral instances: every particular finite list of natural numbers has a numeral code whose length and entries are PA-provable. These are the only sequence-coding facts used below.
Define $\operatorname{Hist}_f(x_1,\dots,x_k,n,c)$ to be the formula saying that $c$ codes a computation history of length $n+1$:
\begin{align*}
\operatorname{Hist}_f(x_1,\dots,x_k,n,c) \;:\!\iff\;
&\operatorname{Seq}(c,S n)
\\
&\wedge \exists v_0\,
\left(
\operatorname{Entry}(c,0,v_0)
\wedge
G(x_1,\dots,x_k,v_0)
\right)
\\
&\wedge \forall i\,\forall v\,\forall w\,
\left(
i<n \wedge \operatorname{Entry}(c,i,v) \wedge \operatorname{Entry}(c,S i,w)
\to
H(x_1,\dots,x_k,i,v,w)
\right).
\end{align*}
Now define
\begin{align*}
F_f(x_1,\dots,x_k,n,y) \;:\!\iff\;
\exists c\,
\left(
\operatorname{Hist}_f(x_1,\dots,x_k,n,c)
\wedge
\operatorname{Entry}(c,n,y)
\right).
\end{align*}
We prove in PA, by induction on $n$, that for every $x_1,\dots,x_k$ there exists a unique $y$ satisfying $F_f(x_1,\dots,x_k,n,y)$.
For $n=0$, a history of length $1$ consists of exactly one entry $v_0$, and the defining condition requires $G(x_1,\dots,x_k,v_0)$. Since PA proves $\exists!v_0\,G(x_1,\dots,x_k,v_0)$, the final entry is unique, and PA proves
\begin{align*}
\forall x_1\cdots\forall x_k\exists!y\,F_f(x_1,\dots,x_k,0,y).
\end{align*}
For the induction step, assume PA proves
\begin{align*}
\forall x_1\cdots\forall x_k\exists!v\,F_f(x_1,\dots,x_k,n,v).
\end{align*}
The following argument is carried out in PA with $x_1,\dots,x_k$ arbitrary. By existential elimination applied to the induction hypothesis, choose a variable $v$ together with a proof of $F_f(x_1,\dots,x_k,n,v)$. Expanding $F_f$, choose a code $c$ such that
\begin{align*}
\operatorname{Hist}_f(x_1,\dots,x_k,n,c) \wedge \operatorname{Entry}(c,n,v).
\end{align*}
Since $H$ functionally represents $h$, PA proves
\begin{align*}
\exists!w\,H(x_1,\dots,x_k,n,v,w).
\end{align*}
By existential elimination again, choose a variable $w$ satisfying $H(x_1,\dots,x_k,n,v,w)$. From $\operatorname{Hist}_f(x_1,\dots,x_k,n,c)$ PA has $\operatorname{Seq}(c,S n)$. Applying the sequence-extension property with $\ell=S n$, PA obtains a code $d$ such that $\operatorname{Seq}(d,S(S n))$, $\operatorname{Entry}(d,S n,w)$, and every entry of $c$ below $S n$ is copied to the corresponding entry of $d$.
PA now verifies $\operatorname{Hist}_f(x_1,\dots,x_k,S n,d)$. The base entry is preserved from $c$ because $0<S n$, so the $G$-clause remains true. If $i<n$, then both entries at $i$ and $S i$ are copied from $c$, and the transition clause follows from $\operatorname{Hist}_f(x_1,\dots,x_k,n,c)$. If $i=n$, entry functionality for $d$ gives that the entry at $n$ is the copied value $v$ and the entry at $S n$ is $w$, so the transition clause is exactly $H(x_1,\dots,x_k,n,v,w)$. Therefore PA proves
\begin{align*}
\operatorname{Hist}_f(x_1,\dots,x_k,S n,d) \wedge \operatorname{Entry}(d,S n,w),
\end{align*}
and hence PA proves existence of a $y$ satisfying $F_f(x_1,\dots,x_k,S n,y)$.
For uniqueness, suppose $F_f(x_1,\dots,x_k,S n,y)$ and $F_f(x_1,\dots,x_k,S n,y')$ hold, witnessed by codes $c$ and $c'$. By the initial-segment clause of the coding lemma, PA obtains codes for the first $n+1$ entries of $c$ and $c'$. Because these initial segments preserve all entries below $S n$, the base clause and all transition clauses with index $i<n$ are preserved, so the two initial segments satisfy $\operatorname{Hist}_f(x_1,\dots,x_k,n,\cdot)$. By the induction hypothesis, their stage-$n$ entries are equal. Calling this common entry $v$, both final entries $y$ and $y'$ satisfy
\begin{align*}
H(x_1,\dots,x_k,n,v,y)
\end{align*}
and
\begin{align*}
H(x_1,\dots,x_k,n,v,y').
\end{align*}
The uniqueness clause for $H$ gives $y=y'$. Therefore PA proves
\begin{align*}
\forall x_1\cdots\forall x_k\exists!y\,F_f(x_1,\dots,x_k,S n,y).
\end{align*}
By PA-induction on $n$, PA proves
\begin{align*}
\forall x_1\cdots\forall x_k\forall n\exists!y\,F_f(x_1,\dots,x_k,n,y).
\end{align*}[/step]