Demo: the CLRS red-black tree with Python selectors - #153
Open
sidprasad wants to merge 1 commit into
Open
Conversation
spytial-clrs's trees.ipynb declares its red-black constraints in sgq, over the relationalized instance -- skipping the NIL sentinel reads `left & (RBNode -> (RBNode - NoneType.~key))`. This is the same chapter 13 structure with the selectors written as Python functions over the objects themselves, so the NIL test is `n is NIL` and the colour test is `n.color == RED`. The notebook is honest about the boundary: `disjoint-sets.ipynb`'s `^(~parent)` is transitive closure, which has no comprehension form, and sgq stays the better spelling for anything about the shape of the graph rather than the value of a field. Executed; the committed output is the CLRS figure 13.4 tree, verified a valid red-black tree (BST order, no red-red, uniform black-height 3). Co-Authored-By: Claude Opus 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.
Stacked on #152. Base is
python-selectors— review that one first; this diff is one notebook.What this is
spytial-clrs'strees.ipynbdeclares its red-black constraints in sgq, which runs over the relationalized instance. Skipping the NIL sentinel reads:This is the same CLRS chapter 13 structure with the selectors written as Python functions over the objects themselves:
trees.ipynb)left & (RBNode -> (RBNode - NoneType.~key))[(n, n.left) for n in values if real(n) and real(n.left)]{ x : RBNode | @:(x.color) = "red" }[n for n in values if real(n) and n.color == RED]{ x : RBNode | (x.key in NoneType) } + RBTree + int + NoneType + {s : str | @:s = "red" or @:s = "black"}[v for v in values if v is NIL or isinstance(v, (RBTree, int)) or v is None or v in (RED, BLACK)]Worth noting on the first row: NIL is a single object, so Python says
n is NIL. sgq has no way to say "this object", sotrees.ipynbidentifies it by a property that happens to hold only of it — its key isNone. That works, and it stops working the moment a real node is given aNonekey.It is honest about the boundary
The notebook ends by pointing at
disjoint-sets.ipynb's^(~parent)— transitive closure of the inverted parent relation. There is no comprehension for that; the Python version is a hand-written fixpoint. The rule it offers: Python selectors for conditions about a value (n.color == RED,n is NIL), sgq for conditions about the shape of the graph (^parent,~next,iden).It found a bug
Porting this surfaced a real defect in #152, now fixed there: a
strwas emitted as a quoted literal, but a quoted string is a value in sgq and not a member ofuniv("black" & univis empty). SohideAtomon the colour strings silently drew them anyway — visible as two strayred/blackboxes in the diagram. Strings are now emitted as{s : str | @:s = "..."}, which is exactly how the sgq notebooks spell it, and now I know why.Verification
The notebook is executed and its output committed. The tree is CLRS figure 13.4, checked as a valid red-black tree — BST order, no red child of a red node, uniform black-height 3 — and the rendered diagram was inspected in a browser: six nodes, five edges matching the tree's shape, red borders on exactly the two red nodes, no scaffolding atoms drawn.
🤖 Generated with Claude Code