SelectionQuote#
- 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 aSelection
contains anotherSelection
. In this situation, the innerSelection
holes must be kept distinct from the outerSelection
holes, so that only the outerSelection
’s holes are modified by operations on the selection. When this occurs, the innerSelection
’sSelectionHole
instances are wrapped in aSelectionQuote
, so that they aren’t processed until the outerSelection
is resolved.Note that quoting is only applied to the
remainder
tree, since this is the tree where we expect to find aSelectionHole
. If there areSelectionHole
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 aSelection
. To support even higher levels of nesting, or trees where the user has inserted their ownSelectionHole
orSelectionQuote
for some reason, we also quoteSelectionQuote
; it’s not clear that there are many uses for this butpenzai
supports it regardless.Users should not need to create a
SelectionQuote
directly, and should instead use theselect(...)
function and other selector traversals. However, you may seeSelectionQuote
if inspecting the contents of aSelection
object.Note
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, withSelectionHole
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 https://randall-holmes.github.io/Watson/babydocs/node26.html for a bit of related discussion.
This is related to the interpretation of
Selection
as a lens, for which theremainder
tree conceptually represents the partially-applied setter function.Methods
__init__
(quoted)Attributes
quoted
Inherited 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.