class penzai.core.selectors.SelectionQuote[source]#

Bases: Struct

Marks a particular subtree as relating to an inner selection.

SelectionQuote is primarily used to handle the situation where a Selection contains another Selection. In this situation, the inner Selection holes must be kept distinct from the outer Selection holes, so that only the outer Selection’s holes are modified by operations on the selection. When this occurs, the inner Selection’s SelectionHole instances are wrapped in a SelectionQuote, so that they aren’t processed until the outer Selection is resolved.

Note that quoting is only applied to the remainder tree, since this is the tree where we expect to find a SelectionHole. If there are SelectionHole instances inside the selected subtrees themselves, these are not quoted, since we never look at those subtrees when rebuilding the tree from a selection.

One situation where selections-of-selections may appear is when using penzai.treescope to visualize a Selection. To support even higher levels of nesting, or trees where the user has inserted their own SelectionHole or SelectionQuote for some reason, we also quote SelectionQuote; it’s not clear that there are many uses for this but penzai supports it regardless.

Users should not need to create a SelectionQuote directly, and should instead use the select(...) function and other selector traversals. However, you may see SelectionQuote if inspecting the contents of a Selection object.


Aside for programming languages geeks: SelectionQuote can be seen as a Peano-arithmetic “successor” function for De Bruijn levels, a particular convention for encoding variable binding in the lambda calculus, with SelectionHole representing zero. (De Bruijn levels are the conceptual opposite of De Bruijn indices, which use lower indices for the innermost variables instead of for the outermost variables.)

See for a bit of related discussion.

This is related to the interpretation of Selection as a lens, for which the remainder tree conceptually represents the partially-applied setter function.





Inherited Methods

(expand to view inherited methods)


Constructs a dictionary with all of the fields in the class.


Directly instantiates a struct given all of its fields.


Generates a JAX PyTree key for a given field name.


Wraps this struct in a selection, enabling functional-style mutations.


Flattens this tree node.


Flattens this tree node with keys.

tree_unflatten(aux_data, children)

Unflattens this tree node.


Computes a CSS color to display for this object in treescope.