Appendix A. Strings, parsing, and boundary design
The input boundary converts external bytes and text into the typed values the verified core accepts. It handles encoding, validation, and error reporting, and makes units explicit.
Characters and UTF-8
A Char represents a Unicode scalar value. A String stores UTF-8 text.
Byte offsets and character counts measure different things. Neither directly counts user-perceived graphemes, such as a letter followed by combining marks.
Code that assumes each character occupies 1 byte can fail on ordinary non-ASCII names.
For a simple character-based traversal, convert to a list of characters and recurse over it. For performance-sensitive slicing, use the string-position APIs of the selected Lean version and preserve their validity requirements. A character can occupy several bytes, so advance using the string-position API.
def asciiDigits (s : String) : Bool :=
s.toList.all (fun c => '0' ≤ c && c ≤ '9')
#eval asciiDigits "4096"
#eval asciiDigits "40x6"
#eval "café".toList.length
The predicate checks for ASCII digits, so Unicode numerals and locale-specific formats need different checks. It returns true for the empty string because every element of an empty list satisfies any predicate. If the grammar requires at least 1 digit, check for an empty string separately.
Parse into a typed error
inductive InputError where
| empty
| notNatural (input : String)
| aboveLimit (value limit : Nat)
deriving Repr
def parseLimited (input : String) (limit : Nat) : Except InputError Nat := do
if input.isEmpty then throw .empty
let value ← match input.toNat? with
| some n => pure n
| none => throw (.notNatural input)
if value ≤ limit then
pure value
else
throw (.aboveLimit value limit)
#eval parseLimited "17" 100
#eval parseLimited "101" 100
Parsing establishes that the text represents a natural number. The limit check enforces a separate rule. Distinct errors let the caller tell the user whether to fix the format or change the value.
If downstream code needs the bound, preserve its proof in a subtype or checked structure.
trimAscii.toString removes surrounding ASCII whitespace. Applying it before parsing accepts inputs with surrounding whitespace that the parser alone would reject. Document and test that choice, and give the helper a name that describes what it changes.
Choose the trust boundary
A proof about the parsed natural number begins after decoding and validation. To verify the entire boundary, specify the parser’s accepted grammar and prove that successful parsing corresponds to that grammar and numerical interpretation. For many small tools, tested parsing plus a proved domain core is an appropriate first division of effort, provided the resulting claim is stated precisely.
The operating system interprets file paths. Document assumptions about filesystem permissions and race freedom separately from properties proved about path strings.
Practice
Extend the CLI to report a specific error for a negative amount. Keep the general parse error for other invalid natural-number inputs.
Decide whether a leading plus sign and surrounding whitespace are accepted. Write a table of accepted and rejected examples before changing the parser, then run each example against the compiled executable.