Proofs: S2 cell hierarchy
These are the human proofs of S2 cell hierarchy, at full length.
S2 cell ids name the cells of the hierarchy, PARENT, CHILDREN and CONTAINS follow the hierarchy, and FROM-POINT returns the leaf that contains the point full
This proof shows: correctness, termination.
Let . A cell of level on face is a pair of blocks of leaf coordinates, and , with . Its children are the four cells of level inside it. We treat the face projection and the quadratic map as given functions from points to : they are computed by closed formulas, so they stop, and we prove the claims about ids.
Orientations. An orientation is , with bit 1 for swap and bit 2 for invert. The table IJ-TO-POS maps the four bit pairs to positions 0 to 3. For each the row is a permutation of , as the four rows , , and show, and POS-TO-IJ is its inverse.
Lemma 1 (the encoding names one cell per level). Let FROM-POINT reach , and let be the positions computed at line L6, in order. For each , the first positions and are determined by the cell of level that contains , and distinct cells of level on face give distinct sequences .
Proof. By induction on . The orientation before the first step is , fixed by . Suppose the orientation before step is a function of the cell of level that contains . The bits at step (bit of and of ) say which of the four children of contains , so is a function of , and is a function of too. For distinctness: two cells of level on face with the same have, by induction, the same parent cell and so the same , and since row of the table is a permutation, the same means the same bit pair, so the same child.
So the map from cells of level to the numbers is injective, and since both sets have elements, it is a bijection.
Definition. The id of a cell of level with face and positions is . Its lowest set bit is , since the sum has no bits below ; so the level is recovered from the id as , where is the number of trailing zeros.
Lemma 2 (FROM-POINT). Line L8 returns the id of the leaf cell .
Proof. After the loop, , so , which is the definition for . The loop runs times, so it stops.
Lemma 3 (ranges). For a cell of level , the ids of the leaf cells inside are exactly the odd numbers in .
Proof. A leaf inside has the face and the first positions of , by Lemma 1, since its ancestor of level is , and any positions ; and every such sequence gives a leaf inside , by the bijection. Let . The leaf ids are , where runs over all even numbers from 0 to . So they are the odd numbers from to . Since , these are the odd numbers from to .
Theorem. (a) PARENT for returns the id of the ancestor of at level . (b) CHILDREN for returns the ids of the four children of , in the order of their positions. (c) For cells and , CONTAINS is true if and only if . All three stop after a constant number of word operations.
Proof. (a) Line L9 sets . Clearing the bits of below removes the terms with , that is , and the marker bit of , which is below when ; when , it removes the marker bit only. What is left is , and line L10 adds , which gives the id of the ancestor of level , whose positions are by Lemma 1.
(b) With the lowest set bit and , line L12 gives , the id of the child at position . By the bijection, the four values of give the four children.
(c) If , every leaf of is a leaf of ; by Lemma 3 applied to , lies in the center of the range of the leaves of , which is inside the range of the leaves of , so , with and , which is the range of Lemma 3. Conversely, suppose is in the range of . If is a leaf, is odd and, by Lemma 3, a leaf of , so . If is not a leaf, take the leaf of with the first positions of , then position 2 and then all positions 0; its id is . Since is even and is odd, , so is a leaf of . The cells of the hierarchy are nested or disjoint, and and share the leaf , so one contains the other. If , then , and the range of is a proper part of the range of that does not contain its center : by Lemma 3, the range of is the block of leaves of , which lies entirely in the lower half or the upper half of the block of , since the four children of split its block into four quarters around its center, and lies inside one child. This contradicts in the range of . So .
Each of the three functions does a fixed number of additions, subtractions, ANDs and shifts, so it stops.