LemmaScript icon indicating copy to clipboard operation
LemmaScript copied to clipboard

Dafny: Math.abs, Math.min and Math.max on real operands call the integer helpers

Open robertpenner opened this issue 3 weeks ago • 0 comments

This was generated by AI during triage.

Problem

With //@ type x real, Math.abs(x), Math.min(a, b) and Math.max(a, b) still lower to the integer preamble helpers. lsc gen exits 0, and Dafny then rejects the model.

export function magnitude(t: number): number {
  //@ verify
  //@ type t real
  //@ type \result real
  //@ ensures \result >= 0
  return Math.abs(t);
}

export function smaller(a: number, b: number): number {
  //@ verify
  //@ type a real
  //@ type b real
  //@ type \result real
  //@ ensures \result <= a && \result <= b
  return Math.min(a, b);
}

export function clampUnit(t: number): number {
  //@ verify
  //@ type t real
  //@ type \result real
  //@ ensures 0 <= \result && \result <= 1
  return Math.min(1, Math.max(0, t));
}

lsc gen --backend=dafny emits:

function MathAbs(x: int): nat { if x >= 0 then x else -x }

function MathMin(a: int, b: int): int { if a <= b then a else b }

function MathMax(a: int, b: int): int { if a >= b then a else b }

function magnitude(t: real): real
{
  MathAbs(t)
}

function smaller(a: real, b: real): real
{
  MathMin(a, b)
}

function clampUnit(t: real): real
{
  MathMin(1, MathMax(0, t))
}

dafny verify:

mathReal.dfy(11,10): Error: incorrect argument type for function parameter 'x' (expected int, found real)
mathReal.dfy(9,9): Error: Function body type mismatch (expected real, got nat)
mathReal.dfy(21,10): Error: incorrect argument type at index 0 for function parameter 'a' (expected int, found real)
mathReal.dfy(21,13): Error: incorrect argument type at index 1 for function parameter 'b' (expected int, found real)
mathReal.dfy(19,9): Error: Function body type mismatch (expected real, got int)
mathReal.dfy(32,24): Error: incorrect argument type at index 1 for function parameter 'b' (expected int, found real)
mathReal.dfy(30,9): Error: Function body type mismatch (expected real, got int)
7 resolution/type errors detected in mathReal.dfy

Checked on main at 6b18f23 (0.6.8) with Dafny 4.11.0.

Resolve already computes the right type. tools/src/resolve.ts (around line 657) gives Math.abs its argument's type and gives Math.min/Math.max the type real when any argument is real. tools/src/transform.ts (around line 861) then emits MathAbs, MathMin or MathMax without consulting that type.

Math.floor and Math.ceil already handle reals (CeilReal for a real argument), so abs, min and max are the remaining helpers in SPEC.md's Math table that only work on integers.

Proposal

When resolve has typed the call real, lower to real-valued preamble helpers and convert any integer operands with as real:

function MathAbsReal(x: real): real { if x >= 0.0 then x else -x }
function MathMinReal(a: real, b: real): real { if a <= b then a else b }
function MathMaxReal(a: real, b: real): real { if a >= b then a else b }

clampUnit above would then emit MathMinReal((1 as real), MathMaxReal((0 as real), t)). Integer calls keep today's helpers, so existing output does not change. Lean is unaffected because it already rejects real arithmetic.

These helpers model mathematical reals, so they have no -0 or NaN cases. That matches the rest of the //@ type x real mode and is independent of the JavaScript-number work described in DESIGN_NUMBERS.md.

We have run this change as a local patch against 0.6.6 in a downstream project, and I can port it to tools/src as a PR with fixtures.

Acceptance criteria

  • magnitude, smaller and clampUnit above verify.
  • Integer Math.abs/min/max output is byte-identical to today's.
  • SPEC.md's Math table and SPEC_DAFNY.md's preamble list mention the real helpers.

robertpenner avatar Nov 12 '26 06:11 robertpenner