Widen pseudo for numerical work: sized types, FLOAT32, exact built-ins, USE IEEE

Pseudo could say what a procedure computes only up to INTEGER and FLOAT,
so it could not state what a Mojo or Python program with Int32, UInt8 or
Float32 values will compute, and it had no words for rounding itself.

- lang/parse: INT8..INT64, UINT8..UINT64, FLOAT32, FLOAT64 as types
  (INTEGER or FLOAT with a width, so every "whole number" rule accepts
  them); conversions written as Mojo writes them, INT32(X), FLOAT32(X);
  1_000_000, 0xFF and 0b1010 literals; a type name as an argument
  (MAX_VALUE(INT8)); the USE line (USE IEEE now, library modules next).
- check: widths and precisions never mix silently (mixed-width,
  mixed-precision); a literal takes the type of the sized value beside
  it, as in Mojo, and one that does not fit is an error before any run
  (literal-range); typing for every new built-in.
- interp: a sized result, store, parameter, RETURN or conversion that
  does not fit stops the run (kind overflow), WRAP(X, T) wraps on
  purpose; FLOAT32 results are rounded to single precision; USE IEEE
  lets NaN and infinities flow.
- numeric.js: exact helpers: FMA rounded once, EXACT(X) with every digit
  a FLOAT holds, ULP and NEXT_AFTER for both precisions, bit operations
  in two's complement.
- Built-ins: ASIN ACOS ATAN SINH COSH TANH HYPOT POW CBRT EXPM1 LOG1P E
  TRUNC SIGN COPYSIGN CLAMP FMA IS_NAN IS_INF IS_FINITE INF NAN ULP
  NEXT_AFTER IS_CLOSE EXACT MAX_VALUE MIN_VALUE EPSILON GCD LCM BIT_AND
  BIT_OR BIT_XOR BIT_NOT SHIFT_LEFT SHIFT_RIGHT WRAP. They stay usable
  as names (a variable E, a FUNCTION GCD): the program's own wins.
- vcgen: an overflow obligation on every sized result, then the range
  as a fact (also for sized parameters and loop-cut variables); FLOAT32
  arithmetic as its own uninterpreted functions, constants rounded
  exactly, TOFLOAT32(v) otherwise; argument obligations for SQRT, the
  logarithms, ASIN/ACOS, LOG1P, CLAMP and the shifts; nothing proved
  under USE IEEE.
- testing: the fuzzer tries the edges of a sized type and generates
  FLOAT32 values.
- pseudo-to-mojo: Int8..Float32, conversions, the new built-ins through
  std.math (each name in the harvested index), a note where Mojo wraps.
- Tests: numeric.test.mjs; corpus 49-52; prover fixtures int8-overflow,
  float32-store, ieee-nan; the reference documents every new form.

Gate: 324 pass (core). New Mojo uses std.math names the interpreter has
not been verified on (listed in sync-mojo's KNOWN_GAPS); they need
pixi run mojo-verify on the network before the interpreter runs them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
alexmandClaude committed 2026-10-05 11:43:53 +00:00
1 parent c5157fcad5
commit be0db2d74f
24 files changed
+1271 -96

No files matched your search

+118 -7
View File
@@ -49,10 +49,24 @@ export const USED_STDLIB: Readonly<Record<string, string>> = {
sqrt: 'std.math', exp: 'std.math', log: 'std.math', log2: 'std.math', log10: 'std.math',
sin: 'std.math', cos: 'std.math', tan: 'std.math', atan2: 'std.math', pi: 'std.math',
floor: 'std.math', ceil: 'std.math', comb: 'std.math',
asin: 'std.math', acos: 'std.math', atan: 'std.math', sinh: 'std.math', cosh: 'std.math', tanh: 'std.math',
hypot: 'std.math', cbrt: 'std.math', expm1: 'std.math', log1p: 'std.math', trunc: 'std.math', copysign: 'std.math',
fma: 'std.math', isnan: 'std.math', isinf: 'std.math', isfinite: 'std.math', inf: 'std.math', nan: 'std.math',
ulp: 'std.math', nextafter: 'std.math', gcd: 'std.math', lcm: 'std.math',
random_float64: 'std.random', random_si64: 'std.random',
};
/** Names the translator writes that Mojo provides without an import. */
export const USED_PRELUDE: readonly string[] = ['Int', 'Float64', 'String', 'Bool', 'List', 'Dict', 'print', 'range', 'len', 'min', 'max', 'abs', 'round', 'input', 'atol', 'atof', 'chr', 'ord', 'Error'];
export const USED_PRELUDE: readonly string[] = ['Int', 'Float64', 'String', 'Bool', 'List', 'Dict', 'print', 'range', 'len', 'min', 'max', 'abs', 'round', 'input', 'atol', 'atof', 'chr', 'ord', 'Error',
'Int8', 'Int16', 'Int32', 'Int64', 'UInt8', 'UInt16', 'UInt32', 'UInt64', 'Float32', 'DType'];
/** The Mojo type for a pseudo number type: INT8 is Int8, FLOAT32 is Float32, INTEGER is Int, FLOAT and FLOAT64 are Float64. */
export function mojoNumber(t: any): string | null {
if (!t) return null;
if (t.k === 'INTEGER') return t.bits ? `${t.signed ? 'Int' : 'UInt'}${t.bits}` : 'Int';
if (t.k === 'FLOAT') return t.bits === 32 ? 'Float32' : 'Float64';
return null;
}
const isSized = (t: any): boolean => !!t && ((t.k === 'INTEGER' && !!t.bits) || (t.k === 'FLOAT' && t.bits === 32));
const up = (s: unknown): string => String(s).toUpperCase();
@@ -71,6 +85,7 @@ const RESERVED = new Set([
'and', 'or', 'not', 'in', 'is', 'if', 'elif', 'else', 'for', 'while', 'break', 'continue', 'return', 'pass', 'del', 'assert', 'class', 'lambda', 'global', 'yield', 'match', 'case', 'self',
'len', 'min', 'max', 'abs', 'round', 'print', 'range', 'input', 'sum', 'type', 'list', 'str', 'int', 'float', 'set', 'dict', 'chr', 'ord', 'pow', 'sort', 'sorted', 'copy', 'main',
'atol', 'atof', 'floor', 'ceil', 'sqrt', 'exp', 'log', 'log2', 'log10', 'sin', 'cos', 'tan', 'atan2', 'pi', 'comb', 'true', 'false', 'none',
'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float32', 'float64', 'dtype',
...Object.keys(USED_STDLIB),
]);
@@ -253,8 +268,7 @@ class Gen {
type(t: any): string {
if (!t) this.gap('a value whose type is not known');
switch (t.k) {
case 'INTEGER': return 'Int';
case 'FLOAT': return 'Float64';
case 'INTEGER': case 'FLOAT': return mojoNumber(t)!;
case 'CHAR': case 'STRING': return 'String';
case 'BOOL': return 'Bool';
case 'ARRAY': if (t.dims === 2) return `List[List[${this.type(t.of)}]]`; return `List[${this.type(t.of)}]`;
@@ -286,10 +300,17 @@ class Gen {
return `${text}.copy()`;
}
/** An INTEGER value going where a FLOAT belongs becomes a Float64, as the notation widens it. */
/**
* A value going where another number type belongs, converted as the notation converts it: an INTEGER into a
* FLOAT becomes Float64(...) (Float32(...) for a FLOAT32), an INTEGER into a sized type becomes Int32(...), a
* FLOAT32 into a FLOAT becomes Float64(...). A literal needs nothing: Mojo gives it the type it lands in.
*/
widened(target: any, e: any, text: string): string {
if (target?.k === 'FLOAT' && e?.type?.k === 'INTEGER' && e.kind !== 'Num') return `Float64(${text})`;
return text;
if (!target || !e?.type || e.kind === 'Num') return text;
const want = mojoNumber(target); const have = mojoNumber(e.type);
if (!want || !have || want === have) return text;
if (isSized(target) && target.k === 'INTEGER') this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
return `${want}(${text})`;
}
/* ---------- program ---------- */
@@ -650,6 +671,7 @@ class Gen {
case 'Str': case 'Char': return JSON.stringify(e.value);
case 'Bool': return e.value ? 'True' : 'False';
case 'Blank': return this.gap(`a blank to fill in: <${e.text}>`);
case 'TypeRef': return e.name === 'INTEGER' ? 'Int' : e.name === 'FLOAT' || e.name === 'FLOAT64' ? 'Float64' : e.name === 'FLOAT32' ? 'Float32' : e.name.startsWith('UINT') ? `UInt${e.name.slice(4)}` : `Int${e.name.slice(3)}`;
case 'Name':
if (e.sym?.kind === 'const') return this.constName(e.name);
if (e.sym?.kind === 'define') return `${this.name(e.name)}()`;
@@ -659,7 +681,12 @@ class Gen {
case 'Binary': {
const map: Record<string, string> = { AND: 'and', OR: 'or', DIV: '//', MOD: '%', '^': '**', '&': '+', IN: 'in' };
if (e.op === '/') this.note('divide', 'In the notation, dividing by zero stops the program. In Mojo a Float64 division by zero gives inf or nan and the program goes on.');
if (e.op === '/' && mojoNumber(e.type) === 'Float32') {
const side = (x: any) => (x.kind === 'Num' || mojoNumber(x.type) === 'Float32' ? this.sideExpr(x, e, x === e.left ? 'l' : 'r') : `Float32(${this.expr(x)})`);
return `${side(e.left)} / ${side(e.right)}`;
}
if (e.op === '/') return `Float64(${this.expr(e.left)}) / Float64(${this.expr(e.right)})`;
if (isSized(e.type) && e.type.k === 'INTEGER' && ['+', '-', '*', '^', 'DIV', 'MOD'].includes(e.op)) this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
if (e.op === 'IN' && e.right.kind === 'Range') return `${this.expr(e.right.lo)} <= ${P(e.left)} < ${this.expr(e.right.hi)}`;
if (e.op === '&') {
const side = (x: any) => (x.type?.k === 'CHAR' || x.type?.k === 'STRING' ? P(x) : `String(${this.expr(x)})`);
@@ -720,7 +747,17 @@ class Gen {
const BIND: Record<string, number> = { OR: 1, AND: 2, '==': 4, '!=': 4, '<': 4, '<=': 4, '>': 4, '>=': 4, IN: 4, '&': 5, '+': 6, '-': 6, '*': 7, '/': 7, DIV: 7, MOD: 7, '^': 9 };
let s = this.expr(x);
const other = side === 'l' ? parent.right : parent.left;
if (['+', '-', '*', '==', '!=', '<', '<=', '>', '>='].includes(parent.op) && x.type?.k === 'INTEGER' && other?.type?.k === 'FLOAT' && x.kind !== 'Num') s = `Float64(${s})`;
if (['+', '-', '*', '==', '!=', '<', '<=', '>', '>=', 'DIV', 'MOD', '^'].includes(parent.op) && x.kind !== 'Num' && other?.type) {
// two sides of different number types meet as the notation has them meet
const mine = mojoNumber(x.type); const theirs = mojoNumber(other.type);
const compare = ['==', '!=', '<', '<=', '>', '>='].includes(parent.op);
if (mine && theirs && mine !== theirs) {
if (x.type.k === 'INTEGER' && other.type.k === 'FLOAT') s = `${theirs}(${s})`;
else if (x.type.k === 'INTEGER' && other.type.k === 'INTEGER' && !x.type.bits) s = `${theirs}(${s})`;
else if (compare && x.type.k === 'FLOAT' && other.type.k === 'FLOAT' && mine === 'Float32') s = `Float64(${s})`;
else if (compare && x.type.k === 'INTEGER' && other.type.k === 'INTEGER') s = `Int(${s})`;
}
}
if (x.kind === 'Chain' || x.kind === 'Quant') return `(${s})`;
if (x.kind === 'Unary' && x.op === 'NOT' && BIND[parent.op] > 3) return `(${s})`;
if (x.kind !== 'Binary') return s;
@@ -729,6 +766,18 @@ class Gen {
return s;
}
/** A value as the Mojo number type T: unchanged when it already is one, or a literal; converted otherwise. */
as(T: string, node: any, text: string): string {
if (mojoNumber(node?.type) === T) return text;
return `${T}(${text})`;
}
/** IS_CLOSE's four values, as Float64 unless they are already FLOATs of one precision. */
args4(e: any): string[] {
const f32 = e.args.slice(0, 2).every((x: any) => mojoNumber(x.type) === 'Float32');
return e.args.map((x: any) => (x.kind === 'Num' || mojoNumber(x.type) === (f32 ? 'Float32' : 'Float64') ? this.expr(x) : `${f32 ? 'Float32' : 'Float64'}(${this.expr(x)})`));
}
/** A built-in that is a short loop: write it before the statement and name its result. */
loopResult(stem: string, type: string, init: string, lines: Array<[number, string]>): string {
const t = this.fresh(stem);
@@ -738,6 +787,13 @@ class Gen {
call(e: any): string {
if (e.isOld) return this.gap('OLD(...) outside a contract');
if (e.convert) {
const to = mojoNumber(e.type)!;
if (e.type.k === 'INTEGER') this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
const arg = e.args[0];
// a FLOAT literal becomes a Float64 first, so the conversion is from a runtime value, as in the notation
return `${to}(${arg.kind === 'Num' && arg.float ? `Float64(${this.expr(arg)})` : this.expr(arg)})`;
}
if (!e.builtin) {
const flags = this.varParams.get(key(e.name)) || [];
const args = e.args.map((a: any, i: number) => this.callArg(a, e.sub?.params?.[i], flags[i]));
@@ -808,6 +864,7 @@ class Gen {
}
case 'SUM_OF': {
const x = this.fresh('x');
if (isSized(e.type)) return this.loopResult('total', this.type(e.type), '0', [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]);
const zero = e.type?.k === 'FLOAT' ? '0.0' : '0';
return this.loopResult('total', '', zero, [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]);
}
@@ -818,6 +875,7 @@ class Gen {
}
case 'DOT': {
const i = this.fresh('i');
if (isSized(e.type)) return this.loopResult('dot', this.type(e.type), '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]);
return this.loopResult('dot', '', e.type?.k === 'FLOAT' ? '0.0' : '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]);
}
case 'VARIANCE_OF': case 'STDEV_OF': {
@@ -864,6 +922,59 @@ class Gen {
this.hoist([[0, `sort(${items})`]]);
return items;
}
// ---- numerical work
case 'ASIN': return math('asin');
case 'ACOS': return math('acos');
case 'ATAN': return math('atan');
case 'SINH': return math('sinh');
case 'COSH': return math('cosh');
case 'TANH': return math('tanh');
case 'CBRT': return math('cbrt');
case 'EXPM1': return math('expm1');
case 'LOG1P': return math('log1p');
case 'HYPOT': this.need('hypot'); return `hypot(Float64(${a}), Float64(${b}))`;
case 'POW': return `Float64(${a}) ** Float64(${b})`;
case 'COPYSIGN': this.need('copysign'); return `copysign(Float64(${a}), Float64(${b}))`;
case 'E': return '2.718281828459045';
case 'TRUNC': this.need('trunc'); return `trunc(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
case 'SIGN': { const T = this.type(e.type); return `(${T}(1) if ${a} > 0 else (${T}(-1) if ${a} < 0 else ${T}(0)))`; }
case 'CLAMP': return `min(max(${a}, ${b}), ${c})`;
case 'FMA': { const T = mojoNumber(e.type)!; this.need('fma'); return `fma(${this.as(T, e.args[0], a)}, ${this.as(T, e.args[1], b)}, ${this.as(T, e.args[2], c)})`; }
case 'IS_NAN': this.need('isnan'); return `isnan(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
case 'IS_INF': this.need('isinf'); return `isinf(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
case 'IS_FINITE': this.need('isfinite'); return `isfinite(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
case 'INF': this.need('inf'); return 'inf[DType.float64]()';
case 'NAN': this.need('nan'); return 'nan[DType.float64]()';
case 'ULP': { const T = mojoNumber(e.type)!; this.need('ulp'); return `ulp(${this.as(T, e.args[0], a)})`; }
case 'NEXT_AFTER': { const T = mojoNumber(e.type)!; this.need('nextafter'); return `nextafter(${this.as(T, e.args[0], a)}, ${this.as(T, e.args[1], b)})`; }
case 'IS_CLOSE': {
const d = this.args4(e);
return `(${d[0]} == ${d[1]} or abs(${d[0]} - ${d[1]}) <= max(${d[2]} * max(abs(${d[0]}), abs(${d[1]})), ${d[3]}))`;
}
case 'MAX_VALUE': case 'MIN_VALUE': case 'EPSILON': {
const T = mojoNumber(e.type)!;
const ty = e.type;
if (e.builtin === 'EPSILON') return `${T}(${ty.bits === 32 ? '1.1920928955078125e-07' : '2.220446049250313e-16'})`;
if (ty.k === 'INTEGER') {
const hi = ty.signed ? 2 ** (ty.bits - 1) - 1 : 2 ** ty.bits - 1; const lo = ty.signed ? -(2 ** (ty.bits - 1)) : 0;
return `${T}(${e.builtin === 'MAX_VALUE' ? hi : lo})`;
}
const m = ty.bits === 32 ? '3.4028234663852886e38' : '1.7976931348623157e308';
return `${T}(${e.builtin === 'MAX_VALUE' ? m : `-${m}`})`;
}
case 'GCD': this.need('gcd'); return `gcd(${a}, ${b})`;
case 'LCM': this.need('lcm'); return `lcm(${a}, ${b})`;
case 'BIT_AND': return `(${a} & ${b})`;
case 'BIT_OR': return `(${a} | ${b})`;
case 'BIT_XOR': return `(${a} ^ ${b})`;
case 'BIT_NOT': return `~${a}`;
case 'SHIFT_LEFT': case 'SHIFT_RIGHT': {
const T = this.type(e.type);
const n = isSized(e.type) ? `${T}(${b})` : b;
if (e.builtin === 'SHIFT_LEFT' && isSized(e.type)) this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
return `(${a} ${e.builtin === 'SHIFT_LEFT' ? '<<' : '>>'} ${n})`;
}
case 'WRAP': return `${this.type(e.type)}(${a})`;
default:
return this.gap(`the built-in ${e.builtin}, which Mojo's standard library has no function for`);
}