Let-expression traversal¶
These functions walk the let-expression AST with attention to binders: they find the free names of
an expression (pure expressions)
and substitute without capturing a variable. Replacing an outer x, for
instance, must not replace an unrelated x bound by an inner lambda.
LetExprExtension lets a downstream IR expose additional binder-free nodes through the same
operations. Its adapter provides children and reconstruction; canonical traversal retains control
of scope and capture avoidance. substitute_let_expr can also use a separate replacement in direct
index positions, so a target's index origin does not require another substitution implementation.
map_let_expr also accepts a descend predicate for target-specific traversal boundaries. A
renderer can keep an indexed array opaque while rewriting its subscripts, then use the shared
reconstruction for every other constructor. This separates array-layout policy from AST coverage;
it does not replace the binder-aware substitution used for variable renaming.
let_expr_traversal
¶
Exhaustive, binder-aware operations over the shared let-expression AST.
The native compiler, QIEC elaborator, and transpilation preflight all consume the same expression nodes. Keeping their recursive walks here makes adding a new node an explicit cross-surface change: an unrecognised variant raises instead of being silently treated as a leaf.
LetExprExtension
¶
Bases: Protocol
Expose binder-free downstream expressions to canonical traversal.
An adapter lists every expression child in a stable order and rebuilds its node over children in that same order. It preserves all other fields. An extension cannot introduce a binder; binder scope and capture avoidance are defined by the shared surface AST, independently of the downstream IR.
children
¶
children(node: LetExprNode) -> tuple[LetExprNode, ...] | None
Read the direct children of a supported extension.
| PARAMETER | DESCRIPTION |
|---|---|
node
|
Expression that is not a canonical surface variant.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[LetExprNode, ...] or None
|
All expression children in source order, or None for an unsupported node. An empty tuple denotes a supported leaf. |
Source code in src/quivers/dsl/let_expr_traversal.py
rebuild
¶
rebuild(node: LetExprNode, children: tuple[LetExprNode, ...]) -> LetExprNode
Rebuild an extension without changing its non-expression metadata.
| PARAMETER | DESCRIPTION |
|---|---|
node
|
Supported extension previously read by children.
TYPE:
|
children
|
Rewritten operands in the same order and arity.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LetExprNode
|
The reconstructed extension, retaining its other fields. |
Source code in src/quivers/dsl/let_expr_traversal.py
let_expr_children
¶
let_expr_children(expr: LetExprNode, *, extension: LetExprExtension | None = None) -> tuple[LetExprNode, ...]
Return the immediate expression children of expr in source order.
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
The expression to take apart.
TYPE:
|
extension
|
Child/rebuild adapter for binder-free nodes owned by a downstream IR.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[LetExprNode, ...]
|
The direct subexpressions, empty for a leaf. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/quivers/dsl/let_expr_traversal.py
walk_let_expr
¶
walk_let_expr(expr: LetExprNode, *, extension: LetExprExtension | None = None) -> Iterator[LetExprNode]
Yield expr and all descendants in preorder.
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
The root of the walk.
TYPE:
|
extension
|
Child/rebuild adapter for binder-free downstream IR nodes.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
LetExprNode
|
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the walk reaches an unsupported let-expression variant. |
Source code in src/quivers/dsl/let_expr_traversal.py
map_let_expr
¶
map_let_expr(expr: LetExprNode, transform: Callable[[LetExprNode], LetExprNode], *, extension: LetExprExtension | None = None, descend: Callable[[LetExprNode], bool] | None = None) -> LetExprNode
Rebuild expr bottom-up, applying transform at every node.
Each node is first rebuilt over its rewritten children and then
passed to transform, so transform sees a node whose
subexpressions are already rewritten. The rebuild is structural and
does not track binders, so a rewrite of variable names belongs in
substitute_let_expr.
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
The root of the rewrite.
TYPE:
|
transform
|
The rewrite applied to each rebuilt node.
TYPE:
|
extension
|
Child/rebuild adapter for binder-free downstream IR nodes.
TYPE:
|
descend
|
Whether to rewrite a node's children before applying
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LetExprNode
|
The rewritten expression. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the rewrite reaches an unsupported let-expression variant. |
Source code in src/quivers/dsl/let_expr_traversal.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
free_let_names
¶
free_let_names(expr: LetExprNode, *, bound: frozenset[str] = frozenset(), include_callees: bool = False, extension: LetExprExtension | None = None) -> tuple[str, ...]
Return free value names in first-use order.
Lambda parameters and factor indices bind in their bodies. Call names are declarations by default; callers that model a callee as a host value can include them explicitly.
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
The expression to scan.
TYPE:
|
bound
|
Names already bound around
TYPE:
|
include_callees
|
Whether the function name of a call counts as a free name.
TYPE:
|
extension
|
Child/rebuild adapter for binder-free downstream IR nodes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, ...]
|
Each free name once, in the order of its first occurrence. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the scan reaches an unsupported let-expression variant. |
Source code in src/quivers/dsl/let_expr_traversal.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
free_let_callees
¶
free_let_callees(expr: LetExprNode, *, bound: frozenset[str] = frozenset(), extension: LetExprExtension | None = None) -> tuple[str, ...]
Return the free callee names of calls in first-use order.
A call f(...) whose f is not bound by an enclosing lambda
parameter or factor index contributes f.
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
The expression to scan.
TYPE:
|
bound
|
Names already bound around
TYPE:
|
extension
|
Child/rebuild adapter for binder-free downstream IR nodes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, ...]
|
Each free callee once, in the order of its first occurrence. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the scan reaches an unsupported let-expression variant. |
Source code in src/quivers/dsl/let_expr_traversal.py
substitute_let_expr
¶
substitute_let_expr(expr: LetExprNode, substitution: Mapping[str, LetExprNode], *, callees: Mapping[str, str] | None = None, index_substitution: Mapping[str, LetExprNode] | None = None, extension: LetExprExtension | None = None) -> LetExprNode
Substitute expressions for free variables in expr without capture.
A lambda parameter or factor binder that would capture a free name of a replacement is renamed to a fresh name first.
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
The expression to rewrite.
TYPE:
|
substitution
|
The replacement for each free variable name.
TYPE:
|
callees
|
The replacement name for each free callee of a call
TYPE:
|
index_substitution
|
Optional overrides in the direct index positions of an indexing node.
All other positions use
TYPE:
|
extension
|
Child/rebuild adapter for binder-free downstream IR nodes. Extensions add structural cases; scope and capture avoidance remain in this walk.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LetExprNode
|
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the rewrite reaches an unsupported let-expression variant. |
Source code in src/quivers/dsl/let_expr_traversal.py
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | |