Repository navigation
php: verify PHP functions against Lean specifications - #15
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.doblock. It coversint,bool,string,float, nullable types, typed lists and tuples, backed enums and readonly value classes, with assignments,if, earlyreturn,foreach,for,while,match, and a fixed set of operators and built-in functions.LeanPhpLib.Phpholds the meaning of PHP operations. Integer+,-and*fail withoverflowwhen the result leaves 64 bits, where PHP would return afloat. An out-of-range list index, amatchwith no arm, and division by zero also fail.<function>.obligation.<label>. The user writes<function>.proof.<label>inlean/Proofs/.leanfran check [--require proved]reports each obligation as proved, sorry, axioms, failed, missing, mismatch or unextracted.leanfran-php obligations checkcompares the theorem type with the obligation and collects the axioms.leanfran.lockstores the result with hashes of the function body, the spec block, the translator and the operation model.leanfran statusreads it without Lean and reports a changed function as stale./,==, references, property writes, dynamic calls, and===between objects, lists or tuples.#[Model]without it gets the install command.What a proof means
A proved
refinessays 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 inrequires. 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=PHPpasses with PHP 8.4.27 and PHP 8.5.12. The new tests take about 33 seconds.clampfixture is proved forrefinesandin_range. Replacing the proof withsorrygives sorry, and reversing one comparison in the PHP body gives failed.add($a, $b)cannot be proved equal toa + bwithoutrequires, and a LeanexampleshowsPhp.add 9223372036854775807 1 = .error .overflow. With a range inrequiresit is proved.checkrun on a fixture shows all seven states.statusruns 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