I have encountered several times, while doing mathematics, the following situation: We have a finite "sequence" $\left(a_{n}\right)_{n\in\left\{ 1,\ldots,p\right\} }$ of some objects that has the (meta-mathematical) property that the value of $p$ isn't known beforehand - and we get it only if we " run''/''compile'' the "sequences" $\left(a_{n}\right)_{n\in\left\{ 1,\ldots,p\right\} }$ (hence the use of the " ", since this "sequence" isn't well defined if its domain isn't know at the time of its definition). So this is actually an algorithm, since in algorithm there also a "runtime" also doesn't have to be specified at the beginning.
How can we get around this difficulty, i.e. how can we rigorously define $\left(a_{n}\right)_{n\in\left\{ 1,\ldots,p\right\} }$ ?
I always thought that we translate the "sequences" $\left(a_{n}\right)_{n\in\left\{ 1,\ldots,p\right\} }$ to another mathematical object for which one doesn't need to specify how "long'' it can run. For example I could mimick one step of $\left(a_{n}\right)_{n\in\left\{ 1,\ldots,p\right\} }$ by a function $f$, such that $a_{2}=f(a_{1}),\ a_{3}=\left(f\circ f\right)\left(a_{1}\right)$ and so on and let $f$ attain some specific value $\alpha$, if the iteration wasn't succesful for. Then I could just define my $p$ as the smallest number such that $f$ equals $\alpha$ - and now I could rigorously define $\left(a_{n}\right)_{n\in\left\{ 1,\ldots,p\right\} }$.
Is the above idea correct ? Do you know of other such methods ?
There are of course algorithms such that only knowing the input there is a closed expression (involving only certain function - not arbitrary ones like my $f$ above) that tells me, how long my algorithm will run, but I don't think that that is the case for all algorithms. Is that true ? Can someone contribute something interesting ? (I know, I'm going slightly off-topic here).
Is there an area in mathematics that deals with questions like 1.-2. ? Maybe proof theory? What about questions like 3. ?
Note: If you should ask me for examples of such "sequences" $\left(a_{n}\right)_{n\in\left\{ 1,\ldots,p\right\} }$ : The euclidean algorithm, the greedy algorithm for a minimal spanning tree of graph...actually take any maths book that contains the word "algorithm", since a (well-defined) algorithm is nothing but a finite sequence, for which we generally don't know yet, how long it will run (I haven't checked, if for these two algorithms there is a simple closed expression in terms of "classic'' functions, that determine beforehand how long they run...if there is, pretend you don't know it and have to take the route described in 1. to get your number of iterations until the algorithm terminates )