In some programming languages, there's functions whose output can change on the basis of changing internal state; for example, the second time you compute $f(5)$ you might get a different answer. This doesn't really happen in mathematics. Nonetheless, I'd like to better understand the extent to which mathematics relies on the "statelessness" of mathematical functions.

To this end, define:

Definition 0.Given a set $X$, view the Kleene closure $X^*$ as an $(\mathbb{N},\leq)$ presheaf as follows:

- $X^*(n) = \{x \in X^* \mid \mathrm{length}(x) = n\} \cong X^n$
- If $\varphi:a \leq b$, then $X^*(\varphi) : X^*(b) \rightarrow X^*(a)$ is the restriction map that takes the first $a$-many letters of the word and deletes the rest.

Definition 1.Given sets $X$ and $Y$, astateful function$X \rightarrow Y$ is a natural transform $X^* \rightarrow Y^*$, where $X^*$ is the set of all words in $X$. Write $\mathbf{Set}^*$ for the category whose objects are sets and whose morphisms are stateful functions.

My questions are whether or not this has been studied, and in particular, whether or not there is a sensible answer to the admittedly rather vague question: "What is the internal logic of $\mathbf{Set}^*$?"