Summary
Add optional verification that stack depth (and, where known, int vs float bit-interpretation) is consistent at control-flow merges and word boundaries.
Motivation
The stack is intentionally untyped (!forth.stack as i64 cells; floats are bit patterns). The programmer ensures type safety. That is flexible but makes mistakes silent: using + instead of F+, or unbalanced IF branches that leave different depths, often produce wrong results rather than compile errors.
Control-flow structure is already validated (#21). Stack effects are not.
Proposed levels
Level 1 — Depth checking (high value, do first)
- Annotate or infer stack depth delta per word/op
- Require matching depths on
IF/ELSE arms, loop back-edges, and word exit points
- Error:
IF branches leave different stack depths (then=2, else=1)
Level 2 — Optional sort tracking
- Track a coarse tag per slot:
int / float / addr / unknown
- Warn or error on
F+ consuming int, + consuming float, etc.
- Escape hatch: explicit
bitcast words or UNSAFE regions
Level 3 — Effect comments as checked contracts (optional)
: dot ( addr addr n -- f ) ... ;
Parse stack comments and verify against inferred effects (Forth tradition).
Acceptance criteria
Non-goals
- Full dependent types or MLIR type redesign of
!forth.stack in v1
- Runtime type tags on the GPU stack
Related
Summary
Add optional verification that stack depth (and, where known, int vs float bit-interpretation) is consistent at control-flow merges and word boundaries.
Motivation
The stack is intentionally untyped (
!forth.stackas i64 cells; floats are bit patterns). The programmer ensures type safety. That is flexible but makes mistakes silent: using+instead ofF+, or unbalancedIFbranches that leave different depths, often produce wrong results rather than compile errors.Control-flow structure is already validated (#21). Stack effects are not.
Proposed levels
Level 1 — Depth checking (high value, do first)
IF/ELSEarms, loop back-edges, and word exit pointsIF branches leave different stack depths (then=2, else=1)Level 2 — Optional sort tracking
int/float/addr/unknownF+consumingint,+consumingfloat, etc.bitcastwords orUNSAFEregionsLevel 3 — Effect comments as checked contracts (optional)
: dot ( addr addr n -- f ) ... ;Parse stack comments and verify against inferred effects (Forth tradition).
Acceptance criteria
Non-goals
!forth.stackin v1Related