PossiblyInfiniteTree #
This file defines a PossiblyInfiniteTree which is an InfiniteTreeSkeleton over an Option of the desired type.
The offered functions are similar to the ones of InfiniteTreeSkeleton.
The tree can still be infinite in both dimensions, i.e.
it may have infinite depth and each node may have infinitely many (direct) children.
An InfiniteTreeSkeleton over Option has no_orphans if an element being none implies its InfiniteTreeSkeleton.childNodes also being none. That is, intuitively, every non-none node needs to have a non-none parent. This is a property that we want for possibly infinite trees but we need to be able to express it on the underlying infinite tree first.
Equations
- t.no_orphans = ∀ (subtree : InfiniteTreeSkeleton (Option α)), subtree <:+ t → subtree.root = none → ∀ (n : Option α), n ∈ subtree.childNodes → n = none
Instances For
A closed version of the no_orphans property. That is, if an element is none, then not only its immediate childNodes but all nodes in all subtrees are none.
A PossiblyInfiniteTree is an InfiniteTreeSkeleton over Option that has no_orphans and also for each subtree, its InfiniteTreeSkeleton.childNodes have InfiniteList.no_holes. The last condition is necessary because we want the trees to be somewhat "compact", meaning that we want to forbid that e.g. the second child for a node is defined but the first child is none. Note that this would not be captured by the InfiniteTreeSkeleto.no_orphans property yet.
- infinite_tree : InfiniteTreeSkeleton (Option α)
- no_orphans : self.infinite_tree.no_orphans
- no_holes_in_children (subtree : InfiniteTreeSkeleton (Option α)) : subtree <:+ self.infinite_tree → subtree.childNodes.no_holes
Instances For
Basics #
The essential functions on infinite trees, mainly get, drop, and root.
The childTrees function is defined separately here since it is more involved than for the InfiniteTreeSkeleton case.
Obtains the element of the tree at the given address.
Equations
- t.get? ns = t.infinite_tree.get ns
Instances For
Obtains the subtree at the given address (by dropping everything else).
Equations
Instances For
Get the element at the root of the tree (i.e. at the empty address).
Equations
- t.root = t.infinite_tree.root
Instances For
Equations
- PossiblyInfiniteTree.instMembership = { mem := fun (t : PossiblyInfiniteTree α) (a : α) => some a ∈ t.infinite_tree }
Two PossiblyInfiniteTrees are the same if they agree on all addresses.
Dropping the empty address changes nothing.
The root is equal to getting the empty address.
The root is in the tree.
The root of the dropped tree at address ns is exactly the element at address ns.
Empty Infinite Trees #
The empty PossiblyInfiniteTree is simply the PossiblyInfiniteTree that is none on all addresses.
Child Trees #
Defining the childTrees function requires a bit of machinery.
We only want to return the child trees that are not already empty.
Then all returned trees have a non-none root, which we aim to capture directly in the return type.
The PossiblyInfiniteTreeWithRoot is a PossiblyInfiniteTree where the root is not none.
Equations
Instances For
PossiblyInfiniteTreeWithRoot #
For the PossiblyInfiniteTreeWithRoot we mainly provide some functions to convert PossiblyInfiniteTree to and from Option PossiblyInfiniteTreeWithRoot. Clearly, if a PossiblyInfiniteTree has a non-none root, we can convert it directly into a PossiblyInfiniteTreeWithRoot, otherwise, we simply convert it to none. Also in the other direction, none can just be converted to PossiblyInfiniteTree.empty and any PossiblyInfiniteTreeWithRoot is already a PossiblyInfiniteTree.
Equations
Instances For
Equations
Instances For
The actual childTrees definition #
With PossiblyInfiniteTreeWithRoot in place, we can now define the actual childTrees function.
The childTrees of a PossiblyInfiniteTree are the PossiblyInfiniteList of all child trees that are not empty, i.e. it only consists of PossiblyInfiniteTreeWithRoot.
Equations
- One or more equations did not get rendered due to their size.
Instances For
PossiblyInfiniteTree.childTrees can be expressed through InfiniteTreeSkeleton.childTrees.
Getting a child tree is the same as dropping the corresponding singleton address.
Getting at an address in a child tree can be combined into a single get call.
The childTrees of the empty tree are exactly PossiblyInfiniteList.empty.
Node Constructor #
Similar to the InfiniteTreeSkeleton, we can also define a node constructor on the PossiblyInfiniteTree.
Construct a PossiblyInfiniteTree from a PossiblyInfiniteList of PossiblyInfiniteTreeWithRoot and a new root element.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Getting the element at address [] on node is the new root.
Getting any address != [] on node yields the respective element from the previous PossiblyInfiniteTreeWithRoot.
Dropping from node with an address of the form n::ns is the same as getting the n child from the child trees used in the construction and then dropping ns there.
The childTrees of node are the childTrees used in the construction.
Any PossiblyInfiniteTree where the root is not none can be written using the node constructor.
ChildNodes #
It can be convenient to obtain a list of the immediate child nodes of a given tree. This is equivalent to mapping each child tree to its root.
The childNodes are InfiniteTreeSkeleton.childNodes.
Equations
- t.childNodes = { infinite_list := t.infinite_tree.childNodes, no_holes := ⋯ }
Instances For
Getting the nth childNodes is the root of the nth childTrees.
The childNodes are the roots of the childTrees.
Each child node is a tree member.
The childNodes of the empty tree are PossiblyInfiniteList.empty.
Suffixes #
Here, we define a suffix relation on PossiblyInfiniteTree inspired by List.IsSuffix.
For t1 and t2, t1 <:+ t2 denotes that t1 is a subtree of t2.
The suffix relation is reflexive and transitive but not necesarrily antisymmetric!
Note also that InfiniteList.suffix_or_suffix_of_suffix has no equivalent statement here, i.e.
just because two trees are subtrees of the same parent tree, we cannot say anything about their relation to one another.
They might be totally "disconnected".
A suffix relation on infinite trees. This is inspired by List.IsSuffix. Read t1 <:+ t2 as: t1 is a subtree of t2.
Equations
- (t1 <:+ t2) = (t1.infinite_tree <:+ t2.infinite_tree)
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
The suffix relation is reflexive.
The suffix relation is transitive.
A member of a subtree is also a member of the current tree.
Dropping elements yields a subtree.
Each child tree is a subtree.
Every suffix of the empty tree is empty.
We can express the InfiniteTreeSkeleton.no_orphans condition directly on PossiblyInfiniteTree.
Recursor for Members #
We define a recursion (induction) principle for members (Elements) of an PossiblyInfiniteTree called mem_rec.
This can be used with the induction tactic to prove a property for each Element of an PossiblyInfiniteTree.
Note that for using this coveniently, the goal needs to expressed (rewritten) using an Element.
A recursor for proving properties about tree members (Elements) via induction.
Branches #
Branches are essentially PossiblyInfiniteLists in a PossiblyInfiniteTree
and can be characterizes by an infinite "address", i.e. InfiniteList Nat.
This function defines the PossiblyInfiniteList of tree elements that corresponds to a given infinite address.
Equations
- t.branchForAddress ns = { infinite_list := t.infinite_tree.branchForAddress ns, no_holes := ⋯ }
Instances For
An infinite address is maximal in a PossiblyInfiniteTree if whenever the the tree element is none at the nth step of the address, then all of its siblings are also none (and it is enough to demand that the first sibling is none).
Equations
- t.branchAddressIsMaximal ns = ∀ (n : Nat), (t.branchForAddress ns).get? n.succ = none → (t.drop (ns.take n)).childNodes.head = none
Instances For
The branches in the PossiblyInfiniteTree are exactly the PossiblyInfiniteLists for which an infinite address exists that is also maximal.
Equations
- t.branches b = ∃ (ns : InfiniteList Nat), b = t.branchForAddress ns ∧ t.branchAddressIsMaximal ns
Instances For
Getting from the branch corresponding to an infinite address corresponds to getting from the tree at the corresponding finite part of the address.
The set of branches can equivalently be expressed as the set of all PossiblyInfiniteLists where the head equals the root of the tree and the tail occurs in the branches of some childTree. If there are no childTrees, then the tail needs to be empty.
Branch Generation #
We can use PossiblyInfiniteList.generate to construct branches in a PossiblyInfiniteTree.
First of all, this requires that the mapper function produces a PossiblyInfiniteTreeWithRoot.
By that PossiblyInfiniteList.generate gives us an PossiblyInfiniteList of PossiblyInfiniteTreeWithRoot.
Intuitively, using all the roots of these trees gives us a branch.
But this is only true if the generate trees are always child trees of each other and the generation indeed creates a maximal branch according to branchAddressIsMaximal.
This condition is used in the generate_branch_mem_branches theorem to proof that a PossiblyInfiniteList
from the generate_branch function is indeed in branches.
Given a generator and a mapper that maps generated elements to PossiblyInfiniteTreeWithRoot, construct an PossiblyInfiniteList with the goal of constructing a branch in a tree.
Equations
- One or more equations did not get rendered due to their size.
Instances For
If the generated trees are childTrees of each other and the generated branch is maximal according to branchAddressIsMaximal, then the generated PossiblyInfiniteList is indeed a branch.
The PossiblyInfiniteList.head of generate_branch is the root of the first tree.
Getting the nth element from a generate_branch result is the root of the nth generated tree.
The PossiblyInfiniteList.tail of generate_branch is the branch generated when applying the generator function once on the starting element before the actual generation.
Leaves #
The leaves of a PossiblyInfiniteTree is the set of elements that occur in a node that has no childNodes.
Constructing a Tree from a Branch #
A PossiblyInfiniteList directly corresponds to the PossiblyInfiniteTree
where the list is the "first" branch (with the address that only consists of zeros) and all other nodes are none.
Equations
- One or more equations did not get rendered due to their size.