Let $g(a), h(a,n,b), p(a)$ be primitive recursive functions. Consider the function $\phi(a,n)$ defined recursively by $$ \phi(a,0) = g(a) \\ \phi(a,sn) = h(a,n,\phi(p(a), n)) $$ Question: Is $\phi$ primitive recursive?
Usually, the construction of primitive recursive functions only permits constructions using the recursive scheme $\phi(a,sn) = h(a,n,\phi(a,n))$, so I'm wondering if this modified scheme (which includes $p$) still produces primitive recursive functions. It seems like it should work, since we're still decreasing the index every time, but I can't quite work out a proof.
Context: this problem came up in some research I'm doing involving natural numbers objects. As such, I'm hoping to translate any solution into a categorical context.
Attempt: I've noticed that I can define the function $\hat{\phi}(m,a,n)$ recursively by $$ \hat{\phi}(m,a,0) = g(p^m(a)) \\ \hat{\phi}(m,a,sn) = h(p^{m-sn}(a), n, \hat{\phi}(m,a,n)) $$ (where subtraction can't go lower than $0$), and then setting $\phi(a,n) = \hat{\phi}(n,a,n)$ should work. However, I can't prove (categorically) that this construction does indeed satisfy $\phi(a,sn) = h(a,n,\phi(p(a),n))$. It seems to require showing that $\hat{\phi}(sn,a,n) = \hat{\phi}(n,p(a),n)$, which is difficult to do inductively, and the more general statement $\hat{\phi}(sm,a,n) = \hat{\phi}(m,p(a),n)$ only holds for $m \geq n$, which makes induction on $n$ tricky.