LemmaScript icon indicating copy to clipboard operation
LemmaScript copied to clipboard

Dafny: an integer literal returned as a real, or passed to a real parameter, is emitted as int

Open robertpenner opened this issue 3 weeks ago • 0 comments

This was generated by AI during triage.

Problem

In a function whose result is //@ type \result real, an integer literal used as the result is emitted as a bare Dafny int. The same happens to an integer literal passed to a real parameter. Comparisons, division and ensures clauses already convert the same literals with (0 as real), so the generated model is inconsistent within one function.

export function cutoff(t: number): number {
  //@ verify
  //@ type t real
  //@ type \result real
  //@ ensures t <= 0 ==> \result == 0
  if (t <= 0) return 0;
  return t;
}

export function direction(t: number): number {
  //@ verify
  //@ type t real
  //@ type \result real
  //@ ensures t < 0 ==> \result == -1
  return t < 0 ? -1 : 1;
}

export function half(t: number): number {
  //@ verify
  //@ type t real
  //@ type \result real
  return t / 2;
}

export function halfOfOne(): number {
  //@ verify
  //@ type \result real
  return half(1);
}

lsc gen --backend=dafny emits:

function cutoff(t: real): real
{
  if (t <= (0 as real)) then
    0
  else
    t
}

function direction(t: real): real
{
  if (t < (0 as real)) then
    -1
  else
    1
}

function half(t: real): real
{
  (t / (2 as real))
}

function halfOfOne(): real
{
  half(1)
}

dafny verify:

realLit.dfy(5,2): Error: the two branches of an if-then-else expression must have the same type (got int and real)
realLit.dfy(18,2): Error: the two branches of an if-then-else expression must have the same type (got int and int)
realLit.dfy(18,2): Error: the two branches of an if-then-else expression must have the same type (got int and int)
realLit.dfy(36,7): Error: incorrect argument type for function parameter 't' (expected real, found int)
4 resolution/type errors detected in realLit.dfy

The condition t <= (0 as real), the divisor (2 as real) and the postcondition (cutoff(t) == (0 as real)) are all converted. The returned 0, the branches -1 and 1, and the argument 1 are not.

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

Proposal

Type an integer literal from the position it fills. Resolve already passes a target type to coerceToTargetTy (tools/src/resolve.ts, around line 181) for returns, arguments and initializers. When that target is real (or real | undefined), it can give a numeric literal, a negated numeric literal, or each branch of a conditional the type real. Transform then emits such a literal as (n as real), as it already does in comparisons.

A literal whose target is int or nat, or that has no target, keeps today's lowering.

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

  • cutoff, direction, half and halfOfOne above verify.
  • Integer-typed functions produce byte-identical output.
  • A fixture covers a literal in a real return, in both branches of a real conditional, under unary minus, and as a real argument.

robertpenner avatar Nov 12 '26 06:11 robertpenner