Coverage for aether_quant/formal.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""Differential cases computed by the Lean 4 models in formal/ (`aq prove --regen-cases`): run a Python function on a 

2case's `input` and compare with its `expected` to check it still matches the verified model.""" 

3 

4from __future__ import annotations 

5 

6import hashlib 

7import json 

8from fractions import Fraction 

9from importlib import resources 

10from pathlib import Path 

11from typing import TYPE_CHECKING, Any 

12 

13if TYPE_CHECKING: 

14 from collections.abc import Mapping 

15 

16__all__ = ["cases_dir", "decode_rational", "load_cases", "spec_sha256", "targets", "to_float"] 

17 

18 

19def cases_dir() -> Path: 

20 """The folder of differential cases shipped with the package.""" 

21 return Path(str(resources.files("aether_quant") / "_formal_cases")) 

22 

23 

24def targets() -> list[str]: 

25 """The names of the targets with shipped cases.""" 

26 return sorted(path.stem for path in cases_dir().glob("*.json")) 

27 

28 

29def load_cases(target: str) -> dict[str, Any]: 

30 """One target's cases file (`input`, `expected` and the spec digest).""" 

31 return json.loads((cases_dir() / f"{target}.json").read_text(encoding="utf-8")) 

32 

33 

34def decode_rational(value: Mapping[str, int] | None) -> Fraction | None: 

35 """`{"num": n, "den": d}` -> Fraction; JSON null -> None.""" 

36 if value is None: 

37 return None 

38 return Fraction(int(value["num"]), int(value["den"])) 

39 

40 

41def to_float(value: Mapping[str, int] | None) -> float | None: 

42 """A case's `{"num", "den"}` rational as a float; None for null.""" 

43 fraction = decode_rational(value) 

44 return None if fraction is None else float(fraction) 

45 

46 

47def spec_sha256(spec_root: Path, spec_files: list[str]) -> str: 

48 """sha256 over the spec files' bytes in listed order (line endings normalised to LF).""" 

49 digest = hashlib.sha256() 

50 for name in spec_files: 

51 digest.update((spec_root / name).read_bytes().replace(b"\r\n", b"\n")) 

52 return digest.hexdigest()