SelectionQuote#
- class penzai.core.selectors.SelectionQuote[source]#
Bases:
StructMarks a particular subtree as relating to an inner selection.
SelectionQuoteis primarily used to handle the situation where aSelectioncontains anotherSelection. In this situation, the innerSelectionholes must be kept distinct from the outerSelectionholes, so that only the outerSelection’s holes are modified by operations on the selection. When this occurs, the innerSelection’sSelectionHoleinstances are wrapped in aSelectionQuote, so that they aren’t processed until the outerSelectionis resolved.Note that quoting is only applied to the
remaindertree, since this is the tree where we expect to find aSelectionHole. If there areSelectionHoleinstances 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
treescopeto visualize aSelection. To support even higher levels of nesting, or trees where the user has inserted their ownSelectionHoleorSelectionQuotefor some reason, we also quoteSelectionQuote; it’s not clear that there are many uses for this butpenzaisupports it regardless.Users should not need to create a
SelectionQuotedirectly, and should instead use theselect(...)function and other selector traversals. However, you may seeSelectionQuoteif inspecting the contents of aSelectionobject.Note
Aside for programming languages geeks:
SelectionQuotecan be seen as a Peano-arithmetic “successor” function for De Bruijn levels, a particular convention for encoding variable binding in the lambda calculus, withSelectionHolerepresenting 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 https://randall-holmes.github.io/Watson/babydocs/node26.html for a bit of related discussion.
This is related to the interpretation of
Selectionas a lens, for which theremaindertree conceptually represents the partially-applied setter function.Methods
__init__(quoted)Attributes
quotedInherited Methods
(expand to view inherited methods)
attributes_dict()Constructs a dictionary with all of the fields in the class.
from_attributes(**field_values)Directly instantiates a struct given all of its fields.
key_for_field(field_name)Generates a JAX PyTree key for a given field name.
select()Wraps this struct in a selection, enabling functional-style mutations.
tree_flatten()Flattens this tree node.
tree_flatten_with_keys()Flattens this tree node with keys.
tree_unflatten(aux_data, children)Unflattens this tree node.
treescope_color()Computes a CSS color to display for this object in treescope.