leanfran compiles Lean 4 IR into PHP classes with a Composer runtime. See the backend README for the quick start. Paths in Layout and Conventions are relative to langs/PHP/.
LeanPhp/Cli.lean defines the commands of leanfran --lang php with the shared Leanfran.Cli, Leanfran.Module, and Leanfran.Check (../../Leanfran/). LeanPhp/EmitPhp.lean collects the IR reachable from the roots, emits PHP, and assembles initializers and entry points.LeanPhp/Marshal.lean collects type descriptors and compiles invariant decisions. LeanPhp/Generate.lean emits public types, facade methods, and their descriptor-backed conversions. The implementation class sits in the generated Internal/ directory.LeanPhp/Embed.lean defines the PHP Backend of the compiler's Leanfran.Embed (../../Leanfran/Embed/). Leanfran.Embed runs leanfran --lang php sync, check, and status: it writes the Lake workspace, runs lake, generates the PHP, installs it, and writes leanfran.manifest.json and leanfran.lock. bin/leanfran has the two commands scan and translate, which leanfran --lang php starts from the project's vendor/conao3/leanfran with php. They read a JSON object with the project directory as root on the standard input. src/Embed/Scan.php scans attributes, reflects PHP models, hashes block dependencies, and prints the blocks, models, functions, and earlier generated files as JSON. The job of a block has a block_key, the hash of the block's content. The generated trait passes it to src/Embed/Freshness.php, which compares it with the attribute of the loaded class on the first call. src/Embed/Stale.php throws or runs sync for a block that is out of date, and src/Embed/autoload.php handles a missing trait. tests/Embed/ and tests/fixtures/embed/ exercise the embedding workflow.src/Model.php declares #[Model]. src/Verify/ discovers modeled functions with nikic/php-parser, resolves signature types, and translates bodies and obligations to the Lean modules that translate prints. Leanfran.Check judges the proofs of the obligations. src/lean/LeanPhpLib/Php.lean defines the modeled PHP operations.src/Verify.php runs property tests against the generated Lean specifications. It shares signature resolution with the translator. src/Verify/Property.php runs a function and its specifications on one input, and src/Verify/Inputs.php samples boundary and seeded random values and lists smaller ones. LeanPanic in src/Runtime/io.php makes a Lean panic in a specification raise an exception. src/Verify/ValueTypes.php hashes the classes that a modeled function names. tests/fixtures/verify/ holds the proof, differential, and property test fixtures.src/Schema/, src/Marshal.php, and src/Json.php implement the public descriptor, value conversion, validation, and JSON representations. src/Valinor.php adapts validation exceptions for an optional mapper.src/Runtime/ contains Lean's C ABI functions as namespaced PHP functions. The autoload.files list in composer.json loads them. phpstan-types.neon defines the runtime value type shared by container, scalar, and closure code.LeanPhp/RuntimeExterns.lean is the generated inventory of runtime symbols. The compiler checks the symbols a job needs before emitting PHP.LeanPhpTest/Vectors.lean and Io.lean contain differential fixtures. The corresponding Lake executables run the native C oracles. LeanPhpTest/Control*.lean covers IR constructors, call depth, and complexity. tests/Differential/ holds PHP drivers, tests/Runtime/ holds PHPUnit tests, and test-out/ holds generated test artifacts.LeanPhpTest/Public*.lean, LeanPhpTest/Json.lean, and tests/Public/ cover public wrappers, JSON, JSON Schema, and Valinor. tests/Public/schema/typescript.json records schemas generated by the TypeScript backend from the same Lean modules.make/ register checks through CHECK_TARGETS. ../../maint/PHP/ holds the oracle, fuzz, and extern scripts and the PHPStan path configuration. samples/ includes an internal entry point, a generated CLI, a public pricing example, embedded applications, and verified PHP functions.lean-toolchain selects Lean for the compiler and the C oracle. Use the Lean sources and lean.h to check the C symbol, argument order, erased parameters, and ownership when implementing an extern. Increment a retained value read out of a container, and leave borrowed arguments owned by the caller.LeanPhpTest/Vectors.lean or Io.lean. Register a new IO case in ../../maint/PHP/test-io.sh with its expected exit status. The differential check compares stdout, stderr, and status with the C oracle. The IO check also compares the resulting files.autoload.files, run make gen-externs in the PHP shell, and commit the generated LeanPhp/RuntimeExterns.lean with the runtime change. make externs-check regenerates the list for comparison.generatorVersion in LeanPhp/Cli.lean and regenerate the checked-in classes under samples/. Raise Lean::GENERATOR_VERSION in src/Lean.php to the same version. The block hashes in leanfran.manifest.json include this version, and scan and translate exit 255 when the compiler's LEANFRAN_VERSION, LEANFRAN_GENERATOR_VERSION, or LEANFRAN_LEAN_VERSION differs from Lean::VERSION, Lean::GENERATOR_VERSION, or Lean::LEAN_VERSION. maint/PHP/bump-version.sh writes the release version to src/Lean.php, and maint/set-core-version.sh writes it to Leanfran/Version.lean. make test compares test-out/samples/LeanSumTo.php with the checked-in sum-to class. make pricing-fixture regenerates the pricing classes in test-out/samples/pricing/ for comparison with samples/pricing/src/_lean/. Review the generated PHP files before copying them into the samples.declare(strict_types=1); in handwritten PHP. Mago formats the runtime, tests, and maintenance scripts. PHPStan checks src/, generated public types and facades, their tests, and the pricing sample at level max. It reads the generated Internal/ classes for symbol and signature discovery. make test-static-docs expects five type errors from a separate consumer fixture with invalid arguments.src/Embed/Models.php and the PHPDoc subset in src/Embed/PhpDocType.php. The compiler emits model descriptors for the user class, with Lean deciders for imported constraints. make test-embed compares the embedded fixture with the C backend. make test-embed-samples runs status in the checked-in embedded samples, then runs them in copies and compares their checked-in files and leanfran.manifest.json with a fresh sync. make test-verify-samples does the same for the verified samples with check --require proved and leanfran.lock. make test-embed-static analyzes the fixture and samples at level max, with their generated internal classes available for symbol discovery.tests/fixtures/verify/ and the operation tests in tests/Verify/. Keep PHPDoc constraints out of verification assumptions. If translation dependencies change, update the inputs hashed by src/Verify/Lock.php. Changing a hashed translator source makes the committed leanfran.lock of the verified samples stale; run check in them and commit the lock.tests/fixtures/verify/pbt/. make test-verify-pbt uses the same generated specs as make check-verify. It runs PHPUnit after the shared verify-fixture prerequisite.#[Model] functions that replace their specifications: src/Verify/Check.php returns the php:[private ]<function> extern of a function whose parameters are PHP values, obligations check compares its types with those of the specification, LeanPhp/Generate.lean (collectReplaced) emits the spec_<method> copies and the methods of the trait, LeanPhp/EmitPhp.lean emits the bridges, and src/Embed/Bridge.php converts the values. The deciders of invariants are compiled without the replacements. --internal-only and --output-file apply none. maint/PHP/test-replaced.sh (make check-verify) runs tests/fixtures/replaced/.make test-schema uses the committed TypeScript output. Run make schema-oracle-check in the PHP Nix shell to regenerate it with the TypeScript Nix shell and compare all three targets. Review a changed oracle before copying test-out/schema/typescript.json to tests/Public/schema/typescript.json.suggest and their test dependencies in require-dev. The pricing sample installs just conao3/leanfran and verifies the public API in that environment.From the repository root, run both PHP versions with the root Makefile.
nix shell nixpkgs#gnumake -c make check BACKENDS=PHP
nix shell nixpkgs#gnumake -c make format-check BACKENDS=PHP
nix build .#leanfran
maint/cli-expect/php.txt holds the expected first line and exit code of the commands of leanfran --lang php (maint/test-cli.sh) and of bin/leanfran (maint/test-host.sh, which make check-cli runs).
The check runs differential, numeric, depth, complexity, IR coverage, and fuzz tests, plus PHPUnit, PHPStan, the extern inventory comparison, proof state and freshness checks, property tests, and the samples. The verified samples require all their obligations to be proved and compare their runtime output and checked-in generated PHP. The named shells .#php and .#php85 provide PHP 8.4 and 8.5 with matching Composer, PHPStan, and PHPUnit. Select the shell when running a backend target directly.
To use the compiler from PATH, install it with nix profile install 'github:conao3/lean-leanfran#leanfran'.
nix develop .#php -c make -C langs/PHP gen-externs
nix develop .#php85 -c make -C langs/PHP test
The checks compare stdout, stderr, and exit status against Lean's C backend and also compare filesystem changes for 11 IO fixtures. The default fuzz run compares 2,026 inputs with the C backend, including 2,000 seeded random rows.
The numeric test covers 427 extern declarations with 402 distinct C symbols. Its 423 call cases use products of boundary values and 2,000 seeded random inputs per case. The PHP 8.4 and 8.5 checks on x86-64 Linux each matched 1,707,520 numeric results byte for byte, 65 within 1 ULP, and 1,456 expected panic outputs byte for byte. The 1 ULP allowance applies to Float32 transcendental functions and binary64 exp2 and log2.
The PHP Publish job snapshots the tagged checkout into the root of the read-only mirror conao3/leanfran-php, updates its main branch and applies the same vX.Y.Z tag. Packagist reads the mirror's Composer manifest and tags for conao3/leanfran. The published package is available with composer require conao3/leanfran. The packagist environment supplies PHP_MIRROR_DEPLOY_KEY for the mirror push and PACKAGIST_USERNAME and PACKAGIST_API_TOKEN for the registry update. To exercise the snapshot, tag retries, and Composer installation with a local bare mirror, run nix develop .#php -c make -C langs/PHP test-publish.
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |