feat(slc): accept end=-1 as 'to end' sugar (#15)#540
Merged
Conversation
5d4acfa to
fe525af
Compare
Add resolve_slc_end helper that special-cases end_raw == -1 with a non-negative start as len(xs), so 'slc xs 0 -1' returns the whole list instead of dropping the last element. Other shapes keep the existing Python-style relative-offset semantics (slc xs -3 -1 is still the penultimate window, slc xs 0 -2 still drops the last two). The sugar matches the Python/JS mental model agents reach for first when they want 'from index N to the end'. Drop-last continues to live on take -1 xs. Tree-walker, VM (OP_SLC), and Cranelift JIT helper all route through the same helper.
Update the cross-engine slc regression suite to match the new sugar: the slc xs 0 -1 / slc "hello" 0 -1 / quant-trader cases now assert 'to end' behaviour. Add new pins for slc xs 2 -1 (mid-start sugar), slc xs -1 -1 (negative start keeps Python style), slc xs 0 -2 (other negative ends untouched), and slc xs -3 -1 (penultimate window). Adds take -1 xs as the canonical 'drop last' shape.
Add examples/slc-to-end.ilo so the engines harness pins the new agent-facing shape (slc xs s -1 with s>=0 means 'to end'). Update examples/negative-indices.ilo: the drop-last shape moves to 'take -1 xs', the trim-edges example uses explicit (- (len s) 1), and a penultimate example is added so the file still covers the negative-start path.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Note the sugar (a>=0 + b==-1 means 'to end of list/text') in the slc row of SPEC.md, the canonical ai.txt builtins line, and the list section of the ilo-builtins-core skill. Each pointer also nudges agents toward take -1 xs for the 'drop last' shape so the sugar stays ergonomic without losing the Python-style negative-offset path.
fe525af to
e7db91c
Compare
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.
Summary
Pending entry P2 #15:
slc s n -1doesn't mean "to end". Today ilo'sslcreads negative end indices Python-style (slc xs 0 -1drops the last element), but agents trained on Python/JS keep reaching for-1to mean "to end of list/text", and the friction shows up in persona transcripts.This PR special-cases one narrow shape: when
start >= 0ANDend == -1,endis read aslen xs. Every other shape (negative start, orend <= -2) keeps the existing Python-style relative-offset semantics, soslc xs -3 -1is still the penultimate window andslc xs 0 -2still drops the last two.Manifesto framing: matches the agent's first guess, zero extra tokens at the call site, narrow enough that the existing Python-style negative-offset path stays intact for the cases that already depend on it. "Drop last" continues to live on
take -1 xs, which I now point at from the docs.Repro before/after
Before:
After:
What's in the diff (per commit)
slc: accept end=-1 as 'to end' sugar when start is non-negative- newresolve_slc_endhelper insrc/builtins.rs; tree-walker (src/interpreter/mod.rs), VMOP_SLChandler, and Cranelift JITjit_slcall route through it.tests: pin slc end=-1 sugar and adjacent Python-style shapes- cross-engine regression suite updated intests/regression_neg_index_slice.rs. The existingslc xs 0 -1/slc "hello" 0 -1/ quant-trader fencepost tests now assert the sugar; new pins forslc xs 2 -1(mid-start sugar),slc xs -1 -1(negative start keeps Python),slc xs 0 -2(end<-1 keeps Python), andtake -1 xsas the canonical drop-last shape. 34/34 pass on every available engine.examples: slc-to-end demo + refresh negative-indices for the new sugar-examples/slc-to-end.ilofor the engines harness;examples/negative-indices.iloswitches its drop-last and trim-edges demos ontotake -1/ explicit(- (len s) 1)and adds apenultimateexample that exercises the negative-start path.docs: document slc end=-1 'to end' sugar in SPEC, ai.txt, and skill-SPEC.mdslc row,ai.txtslc line,skills/ilo/ilo-builtins-core.mdlist section. Companion PR on the site repo (already merged) updatesdocs/builtins/collections.md.Behaviour change worth flagging
This is a behaviour change for one specific shape (
slc xs 0 -1was Python-style "drop last" before this PR). I checked every test that hit that shape; the only callers were three regression tests (added in PR #183) and thenegative-indices.iloexample, all of which I updated to the new contract or moved ontotake -1. The quant-trader-fencepost flagship example in the original PR moves totake -1 eq, which is the shape I'd reach for now.If we want to add a deprecation hint for
slc xs <non-neg> -1users who are on the old contract, we could surface a one-time advisory the first time they hit the shape - happy to layer that as a follow-up, but the verifier doesn't track call-site provenance for builtins right now.Test plan
cargo test --release --features cranelift- full suite green (200 result groups, 0 failures)cargo fmt --check- cleancargo clippy --release --features cranelift --all-targets -- -D warnings- cleanexamples/slc-to-end.iloruns on every engine viatests/examples_engines.rsexamples/negative-indices.iloupdated assertions run on every engineFollow-ups
slc xs 0 -1drop-last shape if persona transcripts show agents still reaching for it (would land separately once we have signal).