TITLE: Continuity of distance function and its generalization
QUESTION [6 upvotes]: The starting is an easy undergraduate problem. The distance function $d: X \times X \rightarrow \mathbb{R}$ in a metric space $(X,d)$ is continuous. Please check if my proof is correct. If it is wrong please give some hint for the accurate proof.
From the triangular inequality we can say $d(x,y) \le d(x,z) + d(y,z)$ i.e. $d(x,y) - d(z,y) \le d(x,z)$
Interchanging the role of $x$ and $y$ we shall get $|d(x,y) - d(z,y)| \le d(x,z)$. 
Now for any $\epsilon > 0$ $|d(x,y) - d(z,y)| < \epsilon$ when $d(x,z) < \delta$ where $\epsilon = \delta$. 
Thus $d$ is uniformly continuous and hence continuous.
The main problem is here.

I am thinking to prove it by using the open set definition of continuous function in a general topological space. Basis open sets of $\mathbb{R}$ with usual metric is open intervals $(a,b)$. Its inverse in the metric space will be an open set. How to prove it? 
Can we generalize the concept of distance function to an arbitrary order topology? I want to say if we can replace the range $\mathbb{R}$ of the distance function $d$ by a linear continuum or an arbitrary order topology.   

Thank you very much for your help.

REPLY [2 votes]: In general,if $X,Y$ are any spaces, a function $f:X\times X\to Y$ may have the property that the functions  $f^{(1)}_a:X\to Y$ and $f^{(2)}_a:X\to Y$ are continuous for every $a\in X,$ where $f^{(1)}_a(x)=f(a,x)$ and $f^{(2)}_a(x)=f(x,a),$ and yet $f$ may still not be continuous. 
What you need to show is that when $u,v\in \mathbb R$ with $u<v$ then $d^{-1}(u,v)=\{(a,b)\in X:u <d(a,b)<v\}$ is open in the space $X\times X.$ 
An open set in $X\times X$ is a union of a family of sets of the form  $C\times D$ where $C,D$ are open balls in $X.$ So it suffices to show that if $(a,b)\in d^{-1}(u,v)$ then $C\times D\subset d^{-1}(u,v)$ for some open balls $C,D$ of $X$ with $(a,b)\in C\times D.$ That is, with $a\in C$ and $b\in D.$
For $d(a,b)\in (u,v),$ let $w=\min (d(a,b)-u,\; v-d(a,b)).$ We have $w>0$ so $a\in B_d(a,w/2)$ and $b\in B_d(b,w/2)$.
For $a'\in B_d(a,w/2)$ and $b'\in B_d(b,w/2)$ we have $$\text {(i)} \quad d(a',b')\leq d(a',a)+d(a,b)+d(b,b')<w/2+d(a,b)+w/2\leq v$$  and  we have $$\text {(ii) }\quad  d(a,a')+d(a',b')+d(b',b)\geq d(a,b)$$  which implies $$\text {(ii') }\quad  d(a',b')\geq d(a,b)-d(a,a')-d(b',b)>d(a,b)-w/2-w/2\geq  u.$$ Therefore $$(a,b)\in d^{-1}(u,v)\implies (a,b)\in B_d(a,w/2)\times B_d(b,w/2)\subset d^{-1}(u,v).$$
Remark: For an example of an $f$ of my first paragraph, let $X=Y=\mathbb R,$ let $f(0,0)=0$ and let $f(x,y)=xy/(x^2+y^2)$ when $x^2+y^2\ne 0.$ If $x\ne 0$ then $f(x,x)=1/2$ so $\lim_{x\to 0^+}f(x,x)=1/2\ne f(0,0)$. So $f$ is not continuous at $(0,0).$