Search by

conao3 / leanfran

conao3

PHP runtime for Lean 4 code generated by leanfran

Package info

github.com/conao3/leanfran-php

pkg:composer/conao3/leanfran

Statistics

Installs: 6

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.7.0 2026-11-22 07:44 UTC

This package is not auto-updated.

Last update: 2026-11-22 17:51:18 UTC


README

This repository is a read-only mirror of langs/PHP/ in conao3/lean-leanfran. Please open issues and pull requests in the source repository.

PHP

leanfran compiles embedded Lean 4 blocks and standalone Lean modules to PHP. It also translates modeled PHP functions to Lean so you can prove that they satisfy a specification. The Composer runtime requires 64-bit PHP 8.4 or later with GMP and Tokenizer.

Install

From an empty working directory, set LEANFRAN_SOURCE to a local checkout of the PHP backend or the Git URL of a repository whose default branch contains it. Clone the source, build the compiler, and enter the PHP development shell.

git clone "$LEANFRAN_SOURCE" lean-leanfran
cd lean-leanfran
nix build .#php
nix develop .#php -c sh

Create an application next to the clone. The Composer path repository points to the runtime in that clone.

cd ..
mkdir pricing-php
cd pricing-php
mkdir src

Save this as composer.json.

{
    "name": "example/pricing-php",
    "require": {
        "conao3/leanfran": "dev-main"
    },
    "repositories": [
        {
            "type": "path",
            "url": "../lean-leanfran/langs/PHP",
            "options": {
                "versions": {
                    "conao3/leanfran": "dev-main"
                },
                "reference": "config"
            }
        },
        {
            "packagist.org": false
        }
    ],
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

First embedded block

Save this as src/Pricing.php. The nowdoc contains a structure with a proof field, a function, and a theorem about that function.

<?php

declare(strict_types=1);

namespace App;

use Conao3\Leanfran\Lean;

#[Lean(<<<'LEAN'
    structure Range where
      lo : Nat
      hi : Nat
      ok : lo ≤ hi

    def width (r : Range) : Nat := r.hi - r.lo

    theorem width_le (r : Range) : width r ≤ r.hi := by
      unfold width
      omega
    LEAN)]
final class Pricing
{
    use Pricing\Functions;
}

Save this as main.php.

<?php

declare(strict_types=1);

use App\Pricing;
use App\Pricing\Range;

require __DIR__ . '/vendor/autoload.php';

echo Pricing::width(new Range(lo: 2, hi: 5)), PHP_EOL;

Install the runtime, check the Lean block, generate the PHP trait and types, and run the application.

composer install --no-interaction
vendor/bin/leanfran sync --leanfran-php ../lean-leanfran/result/bin/leanfran-php
php main.php

The application prints the width.

3

Verify a PHP function

Continue from the application directory. Save this as src/Calc.php. The #[Model] attribute links the PHP implementation to an executable Lean specification, an input condition, and a property of the result.

<?php

declare(strict_types=1);

namespace App;

use Conao3\Leanfran\Lean;
use Conao3\Leanfran\Model;

#[Lean(<<<'LEAN'
    def clampSpec (x lo hi : Int) : Int := max lo (min hi x)
    def ordered (_x lo hi : Int) : Bool := lo ≤ hi
    def inRange (_x lo hi : Int) (r : Int) : Bool := lo ≤ r && r ≤ hi
    LEAN)]
final class Calc
{
    use Calc\Functions;

    #[Model(refines: 'clampSpec', requires: 'ordered', ensures: ['in_range' => 'inRange'])]
    public static function clamp(int $x, int $lo, int $hi): int
    {
        if ($x < $lo) {
            return $lo;
        }
        if ($x > $hi) {
            return $hi;
        }
        return $x;
    }
}

Copy the proofs into lean/Proofs/ next to composer.json. Enable Packagist to install the PHP parser as a development dependency. Composer still installs the leanfran runtime from the path repository above.

mkdir -p lean/Proofs
cp ../lean-leanfran/langs/PHP/samples/verified-clamp/lean/Proofs/Calc.lean lean/Proofs/
composer config --unset repositories.packagist.org
composer require --dev --no-interaction 'nikic/php-parser:^5.9'
vendor/bin/leanfran check --leanfran-php ../lean-leanfran/result/bin/leanfran-php --require proved

The proof check reports both obligations as proved.

App\Calc\clamp refines: proved
App\Calc\clamp in_range: proved

Run the property test and call the implementation.

php -r 'require "vendor/autoload.php"; Conao3\Leanfran\Verify::checkClass(App\Calc::class); echo App\Calc::clamp(12, 0, 10), PHP_EOL;'
10

Generate from a Lake project

You can also generate PHP directly from a compiled Lean module. From the application directory, build the standalone pricing sample and generate its PHP API.

(cd ../lean-leanfran/langs/PHP/samples/pricing/lean && lake build)
../lean-leanfran/result/bin/leanfran-php module generate --module-name Pricing \
  --search-path ../lean-leanfran/langs/PHP/samples/pricing/lean/.lake/build/lib/lean \
  --output-dir generated --php-namespace 'App\Lean\Pricing'

The PHP guide covers embedded blocks, the compiler CLI, public types, JSON Schema, Valinor, IO, and PHP function verification. PHP guide