Rose tree

In computing, a rose tree is a term for the value of a tree data structure with a variable and unbounded number of branches per node. The term is mostly used in the functional programming community, e.g., in the context of the Bird–Meertens formalism. Apart from the multi-branching property, the most essential characteristic of rose trees is the coincidence of bisimilarity with identity: two distinct rose trees are never bisimilar.

Naming
The name "rose tree" was coined by Lambert Meertens to evoke the similarly named, and similarly structured, common rhododendron. "We shall call such trees rose trees, a literal translation of rhododendron (Greek ῥόδον = rose, δένδρον = tree), because of resemblance to the habitus of this shrub, except that the latter does not grow upside-down on the Northern hemisphere."

Recursive definition
Well-founded rose trees can be defined by a recursive construction of entities of the following types:  

A base entity is an element of a predefined ground set $V$ of values (the "tip"-values ).



A branching entity (alternatively, a forking entity or a forest entity) is either of the following sub-types:   A set of entities.  A sequence of entities.  A partial map from a predefined set $Σ$ of names to entities.  Any of (a)(b)(c) can be empty. Note that (b) can be seen as a special case of (c) – a sequence is just a map from an initial segment of the set $$\mathbb{N}$$ of natural numbers.

 A pairing entity is an ordered pair $(F, x)$ such that $F$ is a branching entity and $x$ is an element of a predefined set $L$ of "label" values. Since a pairing entity can only contain a branching entity as its component, there is an induced division into sub-types (3a), (3b) or (3c) corresponding to sub-types of branching entities. 

Typically, only some combinations of entity types are used for the construction. The original paper only considers 1+2b ("sequence-forking" rose trees) and 1+2a ("set-forking" rose trees). In later literature, the 1+2b variant is usually introduced by the following definition:

data Tree a = Leaf a | Node [Tree a]

 A rose tree [...] is either a leaf containing a value, or a node that can have an arbitrary list of subtrees .

The most common definition used in functional programming (particularly in Haskell) combines 3+2b: data Rose α = Node α [Rose α]

 An element of Rose α consists of a labelled node together with a list of subtrees . That is, a rose tree is a pairing entity (type 3) whose branching entity is a sequence (thus of type 2b) of rose trees.

Sometimes even the combination 1+3b is considered. The following table provides a summary of the most established combinations of entities.


 * {|class="wikitable"

!Terminology !Entities used
 * Well-founded set
 * (2a)
 * Well-founded nested list value
 * (2b)(1)
 * Well-founded nested dictionary value
 * (2c)(1)
 * Well-founded nested data value
 * (2b)(2c)(1)
 * An $L$-name as known from forcing
 * (2a)(3)
 * Well-founded rose tree in the most common sense
 * (3)(2b)
 * }
 * An $𝒳 = (X, ...)$-name as known from forcing
 * (2a)(3)
 * Well-founded rose tree in the most common sense
 * (3)(2b)
 * }
 * }

Notes:

General definition
General rose trees can be defined via bisimilarity of accessible pointed multidigraphs with appropriate labelling of nodes and arrows. These structures are generalization of the notion of accessible pointed graph (abbreviated as apg) from non-well-founded set theory. We will use the apq acronym for the below described multidigraph structures. This is meant as an abbreviation of "accessible pointed quiver" where quiver is an established synonym for "multidigraph".

In a correspondence to the types of entities used in the recursive definition, each node of an apq is assigned a type (1), (2a), (2b), (2c) or (3). The apqs are subject to conditions that mimic the properties of recursively constructed entities. 

 <ol> <li style="list-style-type:'(a) '"> A node of type (1) is an element of the predefined set $V$ of ground values. <li style="list-style-type:'(b) '"> A node of type (1) does not appear as the source of an arrow. </ol>

<li> <ol> <li style="list-style-type:'(a) '"> A node of type (3) appears as the source of exactly one arrow. <li style="list-style-type:'(b) '"> The target of the arrow mentioned in (a) is a node of type (2). </ol>

<li> Two distinct arrows with the same source node of type (2a) have distinct targets.

<li> A node is labelled iff it is of type (3). The label belongs to the predefined set $L$.

<li> <ol> <li style="list-style-type:'(a) '"> An arrow is labelled by an index from $$\mathbb{N}$$ if its source node is of type (2b). <li style="list-style-type:'(b) '"> An arrow is labelled by a name from a predefined set $Σ$ if its source node is of type (2c). <li style="list-style-type:'(c) '"> Otherwise an arrow is unlabelled. </ol>

<li> Labels of arrows with the same source node are distinct.

<li> Labels of arrows with the same source node of type (2b) form an initial segment of $$\mathbb{N}$$.

</ol>

A bisimilarity between apqs $𝒴 = (Y, ...)$ and $R ⊆ X × Y$ is a relation $𝒳$ between nodes such that the roots of $𝒴$ and $(x,y)$ are $R$-related and for every pair $𝒳$ of $R$-related nodes, the following are satisfied: <ol> <li> The nodes $x$ and $y$ have the same type. <li> If $x$ and $y$ are of type (1) then they are identical. <li> If $x$ and $y$ are of type (3) then they have the same label. <li> For every arrow $a$ of $𝒴$ whose source node is $x$ there exists an arrow $b$ of $𝒳$ whose source is $y$ and

<ol> <li style="list-style-type:'(a) '"> the target nodes of $a$ and b are $R$-related, <li style="list-style-type:'(b) '"> the labels of $a$ and b, if defined, are identical. </ol>

A symmetric condition is satisfied with $𝒴$ and $𝒳$ interchanged. </ol>

Two apqs $𝒴$ and $𝒞$ are said to be bisimilar if there exists a bisimilarity relation $R$ for them. This establishes an equivalence relation on the class of all apqs.

A rose tree is then some fixed representation of the class $𝒳$ of apqs that are bisimilar to some given apq $𝒳$. If the root node of $𝒞 = {𝒳}$ is of type (1) then $𝒞$, thus $𝒞$ can be represented by this root node. Otherwise, $𝒞$ is a proper class – in this case the representation can be provided by Scott's trick to be the set of those elements of $ℛ$ that have the lowest rank.

As a result of the above set-theoretic construction, the class $ℛ$ of all rose trees is defined, depending on the sets $V$ (ground values), $Σ$ (arrow names) and $L$ (node labels) as the definitory constituents. Subsequently, the structure of apqs can be carried over to a labelled multidigraph structure over $ℛ$. That is, elements of $𝒜$ can themselves be considered as "nodes" with induced type assignment, node labelling and arrows. The class $(ℛ × ℛ) ∪ (ℛ × ($\mathbb{N}$ ∪ Σ) × ℛ)$ of arrows is a subclass of $ℛ$, that is, arrows are either source-target couples or source-label-target triples according to the type of the source.

For every element $r$ of $𝒳 = (X, A, r, ...)$ there is an induced apq $𝒳$ such that $r$ is the root node of $𝒳$ and the respective sets $X$ and $A$ of nodes and arrows of $ℛ$ are formed by those elements of $𝒜$ and $𝒳$ that are accessible via a path of arrows starting at $r$. The induced apq $a = [a1, ..., an]$ is bisimilar to apqs used for the construction of $r$.

Pathname maps
Rose trees that do not contain set-branching nodes (type 2a) can be represented by pathname maps. A pathname is just a finite sequence of arrow labels. For an arrow path $σ(a) = [σ(a1), ..., σ(an)]$ (a finite sequence of consecutive arrows), the pathname of $p$ is the corresponding sequence $a$ of arrow labels. Here it is assumed that each arrow is labelled ($σ$ denotes the labelling function). In general, each arrow path needs to be first reduced by removing all its arrows sourced at pairing nodes (type 3).

A pathname $p$ is resolvable iff there exists a root-originating arrow path $a$ whose pathname is $p$. Such $t(p)$ is uniquely given up to a possible unlabelled last arrow (sourced at a pairing node). The target node of a non-empty resolvable path is the target node of the last arrow of the correspondent root-originating arrow path that does not end with an unlabelled arrow. The target of the empty path is the root node.

Given a rose tree $r$ that does not contain set-branching nodes, the pathname map of $r$ is a map $t$ that assigns each resolvable pathname $p$ its value $($\mathbb{N}$ ∪ Σ)^{⁎} ⊇ dom(t)$ according to the following general scheme:

$(V ∪ {⊥} ∪ L) × T$ $t ——— →$ $$\mathbb{N}$ ∪ Σ$

Recall that $⊥$ is the set of arrow labels ($$\mathbb{N}$$ is the set of natural numbers and $Σ$ is the set of arrow names) $L$ is the set of node labels, and $V$ is the set of ground values. The additional symbols $T = {'1', '2b', '2c', '3b', '3c'}$ and $T$ respectively mean an indicator of a resolvable pathname and the set of type tags, $t(p) =$. The $t$ map is defined by the following prescription ($x$ denotes the target of $p$):


 * {| cellpadding=0 style="border:0;"


 * - style="vertical-align:top;"
 * if $x$ is of type (1),
 * - style="vertical-align:top;"
 * $(x, '1')$ or $(⊥, '2b')$
 * if $x$ is of respective type (2b) or (2c),
 * - style="vertical-align:top;"
 * $(⊥, '2c')$ or $(ℓ, '3b')$
 * if $x$ is of respective type (3b) or (3c) and $(ℓ, '3c')$ is the label of $x$.
 * }
 * $ℓ ∈ L$ or $$\mathbb{N}$^{⁎} ⊇ dom(t)$
 * if $t$ is of respective type (3b) or (3c) and $V ∪ {⊥}$ is the label of $t ——— →$.
 * }
 * }

It can be shown that different rose trees have different pathname maps. For "homogeneous" rose trees there is no need for type tagging, and their pathname map $t ——— →$ can be defined as summarized below:


 * {|class="wikitable"

!Terminology !Scheme
 * Nested list value
 * Nested dictionary value
 * Rose tree by Haskell, tree over $L$
 * $t ——— →$-valued tree, tree labelled in $L$
 * }
 * Rose tree by Haskell, tree over $L$
 * $t ——— →$-valued tree, tree labelled in $p$
 * }
 * $L$-valued tree, tree labelled in $p$
 * }
 * $Σ$-valued tree, tree labelled in $t$
 * }
 * }

In each case, there is a simple axiomatization in terms of pathnames:


 * 1) $Σ^{⁎} ⊇ dom(t)$ is a non-empty prefix-closed subset of $V ∪ {⊥}$ or $$\mathbb{N}$^{⁎} ⊇ dom(t)$. In case of $L$, $Σ^{⁎} ⊇ dom(t)$ also needs to be "left-sibling-closed" to form a tree domain, see Encoding by sequences.
 * 2) In case of a nested list or a nested dictionary value, if $M$ is a pathname that is non-maximal in $L$, then $dom(t)$.

In particular, a rose tree in the most common "Haskell" sense is just a map from a non-empty prefix-closed and left-sibling-closed set of finite sequences of natural numbers to a set $n$. Such a definition is mostly used outside the branch of functional programming, see Tree (automata theory). Typically, documents that use this definition do not mention the term "rose tree" at all.

Notes:

Examples
The diagrams below show two examples of rose trees together with the correspondent Haskell code. In both cases, the Data.Tree module is used as it is provided by the Haskell containers package. The module introduces rose trees as pairing entities by the following definition:

Both examples are contrived so as to demonstrate the concept of "sharing of substructures" which is a distinguished feature of rose trees. In both cases, the labelling function is injective (so that the labels,  ,   or   uniquely identify a subtree / node) which does not need to be satisfied in general. The natural numbers (0,1,2 or 3) along the arrows indicate the zero-based position in which a tree appears in the  sequence of a particular "super-tree". As a consequence of possible repetitions in, there can be multiple arrows between nodes. In each of the examples, the rose tree in question is labelled by  and equals  the value of the   variable in the code. In both diagrams, the tree is pointed to by a source-less arrow.

<div style="float:left; width: 690px; margin: 1em 1em 1em 0; padding: 0 0.2em .1em .5em; border:1px solid rgba(90,90,90,.2); text-align:center"> Well-founded rose tree
 * Well-founded rose tree
 * 300px

<div style="float:left; width: 390px; margin: 1em 1em 1em 0; padding: 0 0.2em .1em .5em; border:1px solid rgba(90,90,90,.2); text-align:center"> Non-well-founded rose tree
 * Non-well-founded rose tree
 * 280px

The first example presents a well-founded rose tree  obtained by an incremental construction. First  is constructed, then   then   and finally. The rose tree can be represented by the pathname map shown on the left.

The second example presents a non-well-founded rose tree  built by a breadth-first constructor. The rose tree is a Moore machine, see notes above. Its pathname map $$\mathbb{N}$^{⁎}$ is defined by $Σ^{⁎}$ be respectively equal to $$\mathbb{N}$^{⁎}$ or $dom(t)$ or $dom(t)$ according to $t(p) = ⊥$ where $p$ is the number of occurrences of $t(p) = ⊥$ in $T$.

Relation to tree data structures
The general definition provides a connection to tree data structures:


 * Rose trees are tree structures modulo bisimilarity.

<div style="float:right; width: 290px; margin: -0.5em 0 0 1em; padding: 0 0.2em .1em .3em; border:1px solid rgba(90,90,90,.2);"> Mapping tree data structures to their values
 * Values of tree data structures
 * 290px

The "tree structures" are those apqs (labelled multidigraphs from the general definition) in which each node is accessible by a unique arrow path. Every rose tree is bisimilar to such a tree structure (since every apq is bisimilar to its unfolding) and every such tree structure is bisimilar to exactly one rose tree which can therefore be regarded as the value of the tree structure.

The diagram on the right shows an example of such a structure-to-value mapping. In the upper part of the diagram, a node-labelled ordered tree $R$ is displayed, containing 23 nodes. In the lower part, a rose tree $T$ is shown that is the value of $T$. (In both $R$ and $L$, sibling arrows are implicitly ordered from left to right.) There is an induced subtree-to-subvalue mapping which is partially displayed by blue arrows.

Observe that the mapping is many-to-one: distinct tree data structures can have the same value. As a particular consequence, a rose tree in general is not a tree in terms of "subvalue" relationship between its subvalues, see.

Tree data type
The value mapping described above can be used to clarify the difference between the terms "tree data structure" and "tree data type":


 * A tree data type is a set of values of tree data structures.

Note that there are 2 degrees of discrepancy between the terms. This becomes apparent when one compares a single tree data type with a single tree data structure. A single tree data type contains (infinitely) many values each of which is represented by (infinitely) many tree data structures.

For example, given a set $dom(t) = M^{⁎}$ of labels, the set of rose trees in the Haskell sense (3b) with labels taken from $T$ is a single tree data type. All the above examples of rose trees belong to this data type.

Notes:

Terminological controversy
As it can be observed in the above text and diagrams, the term "rose tree" is controversial. There are two interrelated issues:

<ol> <li style="list-style-type:'(A) '"> Obscure meaning of "node". <li style="list-style-type:'(B) '"> Discrepancy between "tree" and "sharing of substructures". </ol>

Interestingly, the term "node" does not appear in the original paper except for a single occurrence of "nodes" in an informal paragraph on page 20. In later literature the word is used abundantly. This can already be observed in the quoted comments to the definitions:


 *  A rose tree [...] is either a leaf [...] or a node [...] .
 *  An element of Rose α consists of a labelled node [...] .

In particular, the definition of rose trees in the most common Haskell sense suggests that (within the context of discourse) "node" and "tree" are synonyms. Does it mean that every rose tree is coincident with its root node? If so, is such a property considered specific to rose trees or does it also apply to other trees? Such questions are left unanswered.

The (B) problem becomes apparent when looking at the diagrams of the above examples. Both diagrams are faithful in the sense that each node is drawn exactly once. One can immediately see that the underlying graphs are not trees. Using a quotation from Tree (graph theory)


 *  The various kinds of data structures referred to as trees in computer science have underlying graphs that are trees in graph theory [...] 

one can conclude that rose trees in general are not trees in usual meaning known from computer science.

Bayesian rose tree
There is at least one adoption of the term "rose tree" in computer science in which "sharing of substructures" is precluded. The concept of a Bayesian rose tree is based on the following definition of rose trees: $x$ is a rose tree if either $t : {0,1}^{⁎} → {'a','b','c'}$ for some data point ⇭⇭⇭ or $t(p)$ where $'a'$'s are rose trees over disjoint sets of data points.