Suppose we have a (potentially infinite) graph $G$. Let $xRy$ denote the statement "there exists a directed edge from $x$ to $y$" for some two nodes $x, y$ of $G$. I'm interested in a formalization of the sentence
There exists a finite path from $a$ to $b$.
I have convinced myself this is impossible in first-order logic. How could we do this in some higher-order logic? I'm also curious as to how difficult it would be to prove that this statement isn't expressible in first-order logic.
I don't know much about this topic, so sorry if my question is vague. I couldn't find much on the internet about it. I'd be interested in any reading recommendations if this is too complicated to explain in a single thread. Thank you.
This is indeed impossible in first-order logic. The key is the compactness theorem: if $\varphi$ were a first-order formula characterizing finite distance, the set of sentences (in the language of graph theory + two constant symbols $a,b$) $$\varphi(a,b)\cup\{\delta_n: n\in\mathbb{N}\}$$ (where $\delta_n$ says "there is no length-$\le n$ path from $a$ to $b$" - this is clearly first-order-expressible) would be finitely satisfiable but not satisfiable. This, and similar questions, are treated in multiple places on this site; search "compactness + logic" and you should find a bunch.
Second-order logic, meanwhile, is not compact (incidentally it's vastly easier to show that SOL is non-compact than it is to show that FOL is compact) so the above idea doesn't work. In fact, second-order logic can do the job. There are various ways to do this. My personal favorite approach is to shift from "There is a finite path from $a$ to $b$" to the equivalent "The connected component of $a$ contains $b$," which itself can be rephrased as "Every 'distance-$1$-closed' set containing $a$ also contains $b$." And this isn't hard to second-order-ify:
Note the similarity with induction - this sort of idea is extremely prevalent in applications of second-order logic.