Skip to content

php: verify PHP functions against Lean specifications - #15

Merged
conao3 merged 2 commits into
masterfrom
php-verify
Nov 22, 2026
Merged

conao3 merged 2 commits into
masterfrom
php-verify

Conversation

@conao3

@conao3 conao3 commented Nov 22, 2026

Copy link
Copy Markdown
Owner

This PR lets a PHP project prove properties of its own PHP functions in Lean. A typed, pure PHP function is translated into a Lean definition, and a spec written in a #[Lean(...)] block is proved about that definition.

What it provides

  • #[Conao3\Leanfran\Model(refines: ..., requires: ..., ensures: [...])] connects a static method or a namespace function to Lean functions of a block. The PHP side has only this attribute. Specs and proofs stay in Lean.
  • The translator reads the function with nikic/php-parser and writes a Lean do block. It covers int, bool, string, float, nullable types, typed lists and tuples, backed enums and readonly value classes, with assignments, if, early return, foreach, for, while, match, and a fixed set of operators and built-in functions.
  • LeanPhpLib.Php holds the meaning of PHP operations. Integer +, - and * fail with overflow when the result leaves 64 bits, where PHP would return a float. An out-of-range list index, a match with no arm, and division by zero also fail.
  • Obligations are generated as <function>.obligation.<label>. The user writes <function>.proof.<label> in lean/Proofs/.
  • leanfran check [--require proved] reports each obligation as proved, sorry, axioms, failed, missing, mismatch or unextracted. leanfran-php obligations check compares the theorem type with the obligation and collects the axioms.
  • leanfran.lock stores the result with hashes of the function body, the spec block, the translator and the operation model. leanfran status reads it without Lean and reports a changed function as stale.
  • Syntax outside the supported set stops with the file, the line and a reason. This includes integer /, ==, references, property writes, dynamic calls, and === between objects, lists or tuples.
  • nikic/php-parser is a suggested package. A project that uses #[Model] without it gets the install command.

What a proof means

A proved refines says that the PHP function returns the value of the spec when no modelled operation fails, so it covers the inputs where integers do not overflow. Ranges must be given in requires. PHPDoc ranges are not used as hypotheses, because PHP does not check them at run time. String arguments are assumed to be valid UTF-8. The translator and the operation model are trusted, and the tests below compare them with real PHP.

How it was checked

make check BACKENDS=PHP passes with PHP 8.4.27 and PHP 8.5.12. The new tests take about 33 seconds.

  • The clamp fixture is proved for refines and in_range. Replacing the proof with sorry gives sorry, and reversing one comparison in the PHP body gives failed.
  • add($a, $b) cannot be proved equal to a + b without requires, and a Lean example shows Php.add 9223372036854775807 1 = .error .overflow. With a range in requires it is proved.
  • One check run on a fixture shows all seven states.
  • Translated definitions built by Lean's C backend print the same 44 lines as the PHP functions.
  • Each operation of the model matches the real PHP operation on boundary values and 2,048 random pairs. Changing the model of addition makes the test fail.
  • 26 unsupported constructs are each reported with file, line and reason.
  • status runs with an empty PATH and marks only the two obligations of a changed function as stale.

Assisted-by: Claude Code:claude-fable-5-2
Assisted-by: Codex:gpt-6.2-sol

@conao3
conao3 merged commit b5004c3 into master Nov 22, 2026
5 checks passed
@conao3
conao3 deleted the php-verify branch November 22, 2026 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant