Fuse curried lambdas into multi-argument lambda productions - #376
Open
kavigupta wants to merge 1 commit into
Open
Fuse curried lambdas into multi-argument lambda productions#376kavigupta wants to merge 1 commit into
kavigupta wants to merge 1 commit into
Conversation
DreamCoder curries higher-order arguments as nested single-argument lambdas, but the list DSL types `mapi`/`reducei` with uncurried multi-argument arrows (`(i, i) -> R`, `(i, R, i) -> R`), so a nested `(lambda (lambda ...))` never unified against them and the abstraction was skipped. Rewrite the stitch-body converter (`_convert_stitch_node`) to be type-directed: at each argument slot it reads the arrow arity the consuming primitive expects and fuses that many nested single-argument lambdas into the DSL's matching multi-argument lambda production (`lam_1`, `lam_2`). Fusing a group reverses the binder order (a native multi-arg lambda numbers `$k` by argument position, whereas curried nesting numbers innermost-first), so enclosed de Bruijn variables are remapped via absolute binder levels -- correct even for references from deeper nested lambdas. Co-Authored-By: Claude Opus 4.8 (1M context) <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.
PR 2 of 4. Stacks on #375. Review/merge that one first.
What
DreamCoder curries higher-order arguments as nested single-argument lambdas, but the list DSL types
mapi/reduceiwith uncurried multi-argument arrows ((i, i) -> R,(i, R, i) -> R). A nested(lambda (lambda ...))never unified against those, so the abstraction was skipped.The stitch-body converter (
_convert_stitch_node) is now type-directed: at each argument slot it reads the arrow arity the consuming primitive expects and fuses that many nested single-argument lambdas into the DSL's matching multi-argument lambda production (lam_1,lam_2).Fusing a group reverses binder order (a native multi-arg lambda numbers
$kby argument position; curried nesting numbers innermost-first), so enclosed de Bruijn variables are remapped via absolute binder levels — correct even for references from deeper nested lambdas.Reproducing test
tests/s_exp/dreamcoder_abstractions_test.py::TestCurriedLambdaFusion#(lambda (mapi_1 (lambda (lambda 0)) $0))→[i] -> [i](was skipped)reducei_13-arg callback →[i] -> imapi_3 (index $1 $0)types, swapped(index $0 $1)is rejected[[i]] -> [[i]]🤖 Generated with Claude Code