Function for determining the Arity of an $n$-tuple If we define an ordered pair in the standard way (the Kuratowski definition):
$$(x_1,x_2)\equiv\{\{x_1\},\{x_1,x_2\}\}$$
We can recursively define $n$-tuples as an ordered pair of an $(n-1)$-tuple and the $n$th element. For example a $3$-tuple is:
\begin{align}
(x_1,x_2,x_3)&=((x_1,x_2),x_3)\\
&=\{\{(x_1,x_2)\},\{(x_1,x_2),x_3\}\}\\
&=\{\{\{\{x_1\},\{x_1,x_2\}\}\},\{\{\{x_1\},\{x_1,x_2\}\},x_3\}\}\\
\end{align}
This would imply that any $n$-tuple can also be interpreted as a $k$-tuple where $2\le k\le n$.
(That was just to clear up my definitions) My actual question is what would a function that maps an arbitrary $n$-tuple to its maximum $n$ look like.
I've seen functions that allow us to extract the $n$th element of an $n$-tuple, i.e. $\pi^n_m(P)$ is the $m$th element of an $n$-tuple $P$. But this assumes that $P$ is an $n$-tuple, so how can we determine $n$ beforehand for a given tuple $P$.
Moreover can we define a function that can identify whether a given arbitrary set is an $n$-tuple in the first place?
Also when I say function I mean a set-theoretic formula, not a subset of a relation or however you might define that type of function.
 A: First of all, yes. You can identify a $4$-tuple as a pair of two pairs, of a $166$-tuple as a $123$-tuple by putting the last $42$-tuple as one object. You could even notice that every real number is actually a set of rational numbers, or worse, it's a set of Cauchy sequences of rational numbers. Or worse, it's a set of natural numbers. Or worse, it's a set of functions from the natural numbers to itself modulo some equivalence relation.
Secondly, this is why it's better not to think about tuples as "iterated pairs" but as functions. We first define ordered pairs, and using this we define functions. Now an $I$-tuple is a function whose domain is $I$.
So an $n$-tuple is a function from $n$ into some set. A $k$-tuple is a function from $k$ into a set. We can check what is the domain of the function, and so we know what is the type of tuple we consider. And then a $k$-tuple is an $n$-tuple if and only if $n=k$.
(Yes, the natural numbers are also sets.)

The thing to take away from this is that objects can be different things. You can sit on a table, effectively making it a chair, and you can write on a bar turning it into a desk. Set theoretic foundations of mathematics work under the assumption that "everything is a set", and "encoding doesn't really matter" (which is formalized in a suitable way). Therefore it is certainly not type safe.
