fix(binary_tree): read trees passed back in as flat arrays - #977
Merged
Merged
Conversation
make_tree(5, make_tree(4, None, None), None) failed with "make_tree expects binary tree for left (expected binary tree, got ARRAY)". A tree is the list [entry, left, right]. make_tree builds it as a PAIR chain, but when a program passes it back in, py-slang and js-slang both flatten a proper list into one 3-element ARRAY. is_tree and the accessors walked every tree with pair_head/pair_tail, which on an ARRAY read elements 0 and 1 as head and tail, so the flattened tree was rejected. Trees are now read by readTreeNode, which reads an ARRAY by index and a PAIR chain by walking it, and requires exactly three elements. The old ARRAY test built nested 2-element arrays, which neither evaluator produces; it is replaced by tests using the real flat shape. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After #976, building a tree from another tree fails:
The same happens in Source:
make_tree(5, make_tree(4, null, null), null).A tree is the list
[entry, left, right], andmake_treebuilds it as a PAIR chain. When a program passes that tree back into the module, py-slang (pythonToModule) and js-slang (sourceToModule) both flatten a proper list into a single 3-elementARRAY.is_treeand the accessors walked every tree withpair_head/pair_tail. On anARRAYthose read elements 0 and 1, so the "tail" of[4, None, None]wasNoneand the tree was rejected.This bug predates #976. It was hidden because
make_treeused to fail earlier, on its entry argument.Fix
A new helper,
readTreeNode, reads a tree node into its three elements. It reads anARRAYby index and walks a PAIR chain, and requires exactly three elements either way.is_tree,entry,left_branchandright_branchall use it.Tests
The old "round-tripped as
ARRAY" test built nested 2-element arrays, which neither evaluator produces, so it passed while the real case failed. It is replaced by tests using the flat shape:is_treeaccepts flat trees, including a flat tree nested inside anotheris_treerejects arrays that don't have 3 elements, and flat trees with non-tree branchesmake_treeaccepts a flat tree as a branch (the reported case)entry/left_branch/right_branchread a flat tree by positionThe three positive tests fail on master and pass with this change. binary_tree has 23/23 passing, and
tscand lint are clean.Related
plotly's
add_fields_to_data(behindnew_plot) also walks a list with pair operations, and reads a flatARRAYas a single pair. It likely has the same bug. Not addressed here.🤖 Generated with Claude Code