The parse_nat built-in (added in PR #173) currently returns bare Nat. Per spec section 9, it should return Result<Nat, String> to handle invalid input gracefully.
Current behaviour
parse_nat("42") -- returns Nat (42)
parse_nat("abc") -- returns garbage (no validation)
Expected behaviour
parse_nat("42") -- returns Ok(42)
parse_nat("abc") -- returns Err("invalid digit")
Why this is non-trivial
No built-in function currently returns an ADT value. The codegen only knows ADT layouts from DataDecl AST nodes (codegen/registration.py:_register_data). Making a built-in construct heap-allocated tagged unions requires:
- New codegen infrastructure for built-in functions that return ADT values (allocate Result, store tag, box payload)
- Digit validation in the WASM parse loop (currently
byte - 48 on all non-space bytes, no check for 0-9 range)
- Error string construction for the Err path
- Test rewrites -- all 4 codegen tests need
data Result<T, E> { Ok(T), Err(E) } declarations and pattern matching
- Example update --
examples/string_ops.vera uses bare parse_nat return
Context
Deferred from PR #173 review to avoid blocking the first external contribution. The two simpler spec discrepancies (string_length return type, string_slice param types) were fixed in that PR.
The
parse_natbuilt-in (added in PR #173) currently returns bareNat. Per spec section 9, it should returnResult<Nat, String>to handle invalid input gracefully.Current behaviour
Expected behaviour
Why this is non-trivial
No built-in function currently returns an ADT value. The codegen only knows ADT layouts from
DataDeclAST nodes (codegen/registration.py:_register_data). Making a built-in construct heap-allocated tagged unions requires:byte - 48on all non-space bytes, no check for 0-9 range)data Result<T, E> { Ok(T), Err(E) }declarations and pattern matchingexamples/string_ops.verauses bareparse_natreturnContext
Deferred from PR #173 review to avoid blocking the first external contribution. The two simpler spec discrepancies (string_length return type, string_slice param types) were fixed in that PR.