#!/usr/bin/env python3 """Static operations through source, AIR, packages and hosted execution; fuel bounds runs.""" import json from pathlib import Path import subprocess import tempfile REPO = Path(__file__).resolve().parents[2] AJI = REPO / "zig-out/bin/aji" checks = 0 def cli(*args, code=0): global checks run = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True) assert run.returncode == code, (args, run.returncode, run.stdout, run.stderr) checks += 1 return run.stdout example = REPO / "examples/std/static_functions.aji" cli("check", example) assert json.loads(cli("run", "--fuel", "1000000", example))["value"] == 42 cli("report", example) cli("explore", "--max-runs", "8", example) canonical = cli("lower", example) assert canonical == cli("lower", example) assert "call @identity" in canonical and "fn_ref" not in canonical with tempfile.TemporaryDirectory(prefix="aji-static-functions-") as directory: root = Path(directory) air = root / "result.air" air.write_text(canonical) cli("check", air) assert json.loads(cli("run", "--fuel", "1000000", air))["value"] == 42 result = root / "hosted.json" assert cli("run", "--profile", "hosted", "--fuel", "1000000", "--result-file", result, example) == "" assert json.loads(result.read_text())["value"] == 42 entry, algo, policy, manifest = (root / name for name in ("main.aji", "algorithm.aji", "policy.aji", "aji.toml")) manifest.write_text('[package]\nname = "static"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.algorithm = "algorithm.aji"\napp.policy = "policy.aji"\n') algo.write_text('''module app.algorithm struct Policy T effects []> { value: T } fn apply T effects []>(value: T) -> T effects [] { return F(move value); } fn read T effects []>(value: Policy) -> T effects [] { let (item) = struct.unpack(move value); return apply::(move item); } ''') policy_text = '''module app.policy fn identity(value: T) -> T effects [] { return value; } fn other(value: T) -> T effects [] { return value; } ''' policy.write_text(policy_text) imports = "import app.algorithm;\nimport app.policy;\n" body = '''fn main() -> u64 effects [] { let value = app.algorithm.Policy::> { value: 42 }; return app.algorithm.read::>(value); } ''' entry.write_text("module app.entry\n" + imports + body) assert json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest))["value"] == 42 cli("report", "--manifest", manifest) cli("explore", "--max-runs", "8", "--manifest", manifest) package_air = cli("lower", "--manifest", manifest) entry.write_text("module app.entry\nimport app.policy;\nimport app.algorithm;\n" + body) assert cli("lower", "--manifest", manifest) == package_air # Equal layouts and signatures do not erase operation identity. entry.write_text("module app.entry\n" + imports + body.replace("read::(value: T) -> bool effects [] { return true; }\n") for command in ("check", "lower", "report", "run", "explore"): error = json.loads(cli(command, "--manifest", manifest, code=1)) assert error["code"] == "E7005" and Path(error["file"]).name == "main.aji", error assert error["parameter"] == "F" and error["expected"] == "fn(u64) -> u64 effects []" assert error["found"] == "fn(u64) -> bool effects []" # Recursive function-argument resolution is bounded before body expansion. params = "T, F: fn(T) -> T effects []" declaration = f"fn apply<{params}>(value: T) -> T effects [] {{ return F(move value); }}\n" entry.write_text("module app.entry\nfn identity(value: u64) -> u64 effects [] { return value; }\n" + declaration + "fn main() -> u64 effects [] { return apply::(42u64); }\n") assert json.loads(cli("run", "--fuel", "1000000", entry))["value"] == 42 argument = "fn identity" for _ in range(70): argument = f"fn apply::" entry.write_text("module app.entry\nfn identity(value: u64) -> u64 effects [] { return value; }\n" + declaration + f"fn main() -> u64 effects [] {{ return apply::(42u64); }}\n") error = json.loads(cli("check", entry, code=1)) assert error["code"] == "E7006", error print(f"static function CLI integration passed ({checks} checks)")