Rose trees under Gorn addresses #
A Gorn address is a List ℕ path of child indices from the root, and subtreeAt navigates a
rose tree by one. The addresses inside a tree are its positions (validPaths), a prefix-closed set
of TreePaths, and the subtrees are the trees reached by descending through children
(IsSubtree). Navigation recurses on the address, not on the tree. This file also shows that
relabelling commutes with navigation, that a subtree is no larger than its tree and that a tree of
height above k descends k steps. It defines vertices, which enumerates the addresses, and
replaceAt, which replaces the subtree at an address.
Unlike BinaryTree.get (indexed by a PosNum left/right path) there is no indexOf: that is
a binary-search-tree lookup, which has no analogue for a general rose tree.
Main declarations #
RoseTree.subtreeAt,RoseTree.validPaths: the subtree at an address, and the addresses inside a tree, prefix-closed (validPaths_prefix_closed).RoseTree.IsSubtree: the subtree relation, the subtrees being the values ofsubtreeAt(isSubtree_iff_exists_subtreeAt). It is a partial order (IsSubtree.antisymm).RoseTree.subtrees: the subtrees in preorder, exactly those ofIsSubtree(mem_subtrees), which decides it.RoseTree.subtreeAt_map: relabelling commutes with navigation.RoseTree.exists_subtreeAt_height_sub: a maximal descent from a tree of height abovek.RoseTree.numNodes_le_of_subtreeAt: a subtree is no larger than its tree.RoseTree.vertices: the addresses in preorder, one per vertex (length_vertices,nodup_vertices) and exactly those inside the tree (mem_vertices);RoseTree.positionedSubtreespairs each with its subtree (mem_positionedSubtrees).RoseTree.positionedLeaves: the leaves with their addresses, exactly the leaf addresses (subtreeAt_of_mem_positionedLeaves,mem_positionedLeaves_of_subtreeAt) in the order of the frontier (map_snd_positionedLeaves), which is precedence (pairwise_precedes_positionedLeaves).RoseTree.replaceAt: replacement at an address, leaving the subtrees away from it unchanged (subtreeAt_replaceAt_of_not_prefix) and those above it holding the replacement (subtreeAt_replaceAt_of_prefix) and shrinking the tree when the new subtree is smaller (numNodes_replaceAt_lt). Its effect on the frontier is inRoseTree/Span.lean.
Navigation by address #
Positions #
The positions of t are the addresses inside it. Node identity is the position, never the
subtree, since identical subtrees occur at several positions, so orders and graphs live on
positions.
Equations
- t.validPaths = {p : Core.Order.TreePath | (t.subtreeAt p.toList).isSome = true}
Instances For
A non-root position descends to one child and is a position there.
Positions are closed under prefixes, so they inherit the rooted-tree order of TreePath.
The daughters of a position are its extensions by an index below the arity of its subtree.
Height and size along addresses #
A tree of height above k has a maximal descent of length k, along which the subtree at
the prefix of length i has height t.height - i.
Subtrees #
Equations
- RoseTree.instTransIsSubtree = { trans := ⋯ }
subtrees t lists the subtrees of t in preorder, t first, one per vertex.
Equations
- (RoseTree.node value cs).subtrees = RoseTree.node value cs :: RoseTree.subtreesList cs
Instances For
subtreesList cs lists the subtrees of the trees of cs.
Equations
- RoseTree.subtreesList [] = []
- RoseTree.subtreesList (c :: cs) = c.subtrees ++ RoseTree.subtreesList cs
Instances For
Equations
- s.instDecidableIsSubtreeOfDecidableEq t = decidable_of_iff (s ∈ t.subtrees) ⋯
Enumerating the addresses #
vertices t lists the addresses of the vertices of t in preorder, the root [] first.
Equations
- (RoseTree.node value cs).vertices = [] :: RoseTree.verticesList cs
Instances For
verticesList cs lists the addresses of the vertices of the forest cs, each starting with
the index of its tree.
Equations
- RoseTree.verticesList [] = []
- RoseTree.verticesList (c :: cs) = List.map (fun (x : List ℕ) => 0 :: x) c.vertices ++ List.map (List.modifyHead fun (x : ℕ) => x + 1) (RoseTree.verticesList cs)
Instances For
The subtrees of t with their positions, in preorder.
Equations
Instances For
The leaves with their addresses #
positionedLeaves t lists the leaves of t from left to right, each with its address.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Forgetting the addresses leaves the frontier.
Each listed address holds its leaf.
Every leaf address is listed.
The leaf addresses, in the order of the frontier, ascend in precedence.
Replacement at an address #
Replace the subtree at a Gorn address; an address outside the tree leaves it unchanged.
Equations
- x✝¹.replaceAt [] x✝ = x✝
- (RoseTree.node a cs).replaceAt (i :: p) x✝ = RoseTree.node a (cs.modify i fun (x : RoseTree α) => x.replaceAt p x✝)
Instances For
At an address above the replaced one, the subtree has the replacement inside it.