Feature: check that `//@ extern` axioms are consistent (an unsatisfiable one makes every proof that reaches it vacuous)

Open
#310 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
48/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Start by reading how lsc check emits and verifies Dafny code for //@ extern contracts, then inspect the existing handling of string semantics and the linked issue #301 for context. The report describes a symbolic probe and edge-value probes, with Dafny's --filter-symbol to check only those probes; reproduce the surrogate example first. Done means contradictory extern axioms are reported while failed or timed-out probes remain inconclusive, with the unicode-scalar surrogate case covered.

Written by the indexing model from the issue text.

Description

Version: lsc 0.6.10 (npm), Dafny 4.11.

An //@ extern function's //@ ensures is trusted. It is emitted as an {:axiom} function. If the ensures can't hold in the model, any proof that reaches the extern is vacuous, and lsc says nothing.

One easy way to get there: under the default "string-semantics": "unicode-scalar" (--unicode-char:true), a Dafny char can't be a surrogate. An extern that describes JavaScript's UTF-16 behaviour, e.g. a lone surrogate, is then false:

//@ backend dafny

/** A lone high surrogate, as JavaScript can hold it. */
export function loneSurrogate(): string {
  //@ extern
  //@ ensures \result.length === 1 && \result.charCodeAt(0) === 0xD800
  return String.fromCharCode(0xD800)
}

export function width(s: string): number {
  //@ verify
  //@ ensures \result === 42
  return loneSurrogate().length + s.length
}
function {:axiom} loneSurrogate(): string
  ensures ((|loneSurrogate()| == 1) && ((loneSurrogate()[0] as int) == 55296))

function width(s: string): int
{
  (|loneSurrogate()| + |s|)
}

lemma width_ensures(s: string)
  ensures (width(s) == 42)
{
}
Dafny program verifier finished with 2 verified, 0 errors

In JavaScript, width("abc") is 4. With //@ option string-semantics javascript-utf16 (and //@ option dafny-library local), the same file correctly fails on width(s) == 42. The extern is fine as JavaScript, but it contradicts the unicode-scalar model.

Surrogates are only one case. Any hand-written extern axiom can be contradictory, e.g. a typo, or two ensures that can't hold together. The effect is the same: the file reports green and proves nothing.

Suggested feature:

  1. For each extern, have lsc check emit a sanity lemma that tries to prove false from its axioms. If it verifies, fail or warn: "extern loneSurrogate: its ensures are unsatisfiable under the current model". For an extern with parameters, the lemma would take arbitrary arguments satisfying the requires:

    lemma loneSurrogate_consistent() ensures false { var _ := loneSurrogate(); }
    

    I checked this by hand with dafny verify --unicode-char:true. With 55296, the lemma verifies, which flags the axiom. With 65, it fails as it should. A failure or timeout is inconclusive, so it should stay silent. Only a proof of false is reported.
    A symbolic probe catches only an axiom that is false for EVERY argument. The case we actually hit was partial: codePointText(n) (wrapping String.fromCodePoint) promised two UTF-16 units for n > 0xFFFF, so it was contradictory only for astral n, and lemma P(n: int) requires 0 <= n <= 0x10FFFF ensures false { var _ := codePointText(n); } does not verify. What found it was instantiating the axiom on fixed edge values: var _ := codePointText(65536); proves false. We now run, per axiom, the symbolic probe plus one probe per edge value for int/nat/bool/string parameters (0, ±1, 0xD800, 0xDFFF, 0x10000, 0x10FFFF and one past it; "", letters, path characters), with dafny verify --filter-symbol <probe prefix> so only the probes are checked (1-2 s a file). Dafny's own --warn-contradictory-assumptions did not help: it does not report function postconditions proved from a contradiction, and it was silent on this file.

  2. More narrowly: under unicode-scalar, warn when an extern's contract mentions a code unit in 0xD800..0xDFFF (charCodeAt(...) === 0xD8xx, \uD8xx in a literal), since no char has that value.

This complements #301, which is about proof files adding obligations. Here the obligation comes from the .ts itself.

Workaround: none in lsc. Review extern contracts by hand, and verify files whose externs talk about UTF-16 code units under "string-semantics": "javascript-utf16".

Dominant language
TypeScript
Stars
113
Forks
13
Avg merge
15h 35m
Merged PRs (30d)
56

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from midspiral/LemmaScript

All issues in midspiral/LemmaScript

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.