LemmaScript
LemmaScript copied to clipboard
Support string lastIndexOf in the Dafny backend
This was generated by AI during triage.
Problem
s.lastIndexOf(sub) is rejected by the Dafny backend:
ERROR: Dafny emission failed for 'lastDot' in last.ts: Unsupported Dafny method call: .lastIndexOf() on string
The table in SPEC.md already lowers s.indexOf(sub) and s.indexOf(sub, from) to StringIndexOf and StringIndexOfFrom, with the position clamped to [0, |s|]. lastIndexOf is the matching search from the end.
Checked on upstream main at 36eb31d (LemmaScript 0.6.8).
Proposal
Lower s.lastIndexOf(sub) and s.lastIndexOf(sub, position) in Dafny to StringLastIndexOf and StringLastIndexOfFrom, mirroring the indexOf pair:
- the position clamps to
[0, |s|]as in JavaScript; - an empty needle matches at the clamped position;
- the postconditions state that the result is
-1or a match, and no match starts after it.
Lean would keep rejecting lastIndexOf, as it does indexOf(sub, from) today.
Acceptance criteria
- Both forms lower in Dafny and are documented in SPEC.md and SUBSET.md.
- A test compiles the emitted functions and compares their results with JavaScript's own
lastIndexOfover a set of inputs that includes empty strings, empty needles, and negative and out-of-range positions.