"""规则覆盖证明基线:调用真实编译器,保留完整、遗漏、重叠及宽条件输入。""" import argparse import itertools import json from pathlib import Path from scale import execute, fingerprint def source(columns, shape, syntax): names = [f"flag{i}" for i in range(columns)] signature = ", ".join(f"{name}: Bool" for name in names) rows = list(itertools.product(["false", "true"], repeat=columns)) if shape != "wide" else [tuple("-" for _ in names)] if shape == "missing": rows.pop() elif shape == "overlap": rows.append(rows[0]) definitions = [] if syntax == "tables": definitions = [f"rule choose({signature}) (answer: Int)", "| " + " | ".join(names + ["answer"]) + " |"] definitions.extend("| " + " | ".join(list(row) + [str(i)]) + " |" for i, row in enumerate(rows)) else: for i, row in enumerate(rows): parameters = ", ".join(f"{name}: {'Bool' if value == '-' else value}" for name, value in zip(names, row)) definitions.extend([f"choose({parameters}) (answer: Int) {{", f" answer = {i}", "}"]) arguments = ", ".join("false" for _ in names) definitions.extend(["main() (answer: Int) {", f" answer = choose({arguments})", "}"]) return '\n'.join(definitions) + '\n', len(rows) def numeric_source(size, shape): if shape == "budget": rows = [f"| {value} | 1 |" for value in range(size)] + ["| other | 2 |"] return "rule choose(x: Int) (answer: Int)\n| x | answer |\n" + "\n".join(rows) + "\n", len(rows), 1 columns = 2 if shape == "interval-grid" else 1 names = [f"value{i}" for i in range(columns)] signature = ", ".join(f"{name}: Int >= 0 and < {size * 10}" for name in names) rows = [f"rule choose({signature}) (answer: Int)", "| " + " | ".join(names + ["answer"]) + " |"] for index, cells in enumerate(itertools.product(range(size), repeat=columns)): conditions = [f">= {value * 10} and < {(value + 1) * 10}" for value in cells] rows.append("| " + " | ".join(conditions + [str(index)]) + " |") return "\n".join(rows) + "\n", size ** columns, columns def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--compiler", required=True, type=Path) parser.add_argument("--output", required=True, type=Path) parser.add_argument("--syntax", required=True, choices=["clauses", "tables"]) parser.add_argument("--numeric", action="store_true", help="measure interval partitions and the proof-budget diagnostic instead of Boolean cases") args = parser.parse_args() if args.numeric and args.syntax != "tables": parser.error("numeric table cases require --syntax tables") compiler = args.compiler.resolve(strict=True) root = args.output.resolve() root.mkdir(parents=True, exist_ok=False) report = dict(compiler=str(compiler), compiler_sha256=fingerprint(compiler), driver_sha256=fingerprint(Path(__file__)), syntax=args.syntax, repeats=3, timeout_seconds=30, samples=[]) cases = [(size, "complete") for size in [4, 8, 10]] cases += [(4, "missing"), (4, "overlap")] cases += [(size, "wide") for size in [8, 16, 20]] if args.numeric: cases = [(size, "intervals") for size in [16, 64, 256]] cases += [(size, "interval-grid") for size in [4, 16, 32]] cases += [(1200, "budget")] for size, shape in cases: for repeat in range(3): directory = root / f"{shape}-{size}-{repeat}" directory.mkdir() path = directory / "main.dever" columns = size if shape in ("intervals", "interval-grid", "budget"): text, rows, columns = numeric_source(size, shape) else: text, rows = source(columns, shape, args.syntax) path.write_text(text) formatted = execute([str(compiler), "fmt", str(directory)], directory, "fmt", 30) if formatted["exit_code"] != 0: sample = dict(columns=columns, rows=rows, shape=shape, repeat=repeat, correct=False, stage="fmt", **formatted) else: result = execute([str(compiler), "check", str(path)], directory, "check", 30) diagnostic = Path(result["stderr"]).read_text() if shape == "missing": correct = result["exit_code"] != 0 and "C009" in diagnostic and "cover" in diagnostic elif shape == "overlap": correct = result["exit_code"] != 0 and "C009" in diagnostic and "overlap" in diagnostic elif shape == "budget": correct = result["exit_code"] != 0 and "C015" in diagnostic and "has not been proved" in diagnostic else: correct = result["exit_code"] == 0 sample = dict(columns=columns, rows=rows, shape=shape, repeat=repeat, source_sha256=fingerprint(path), correct=correct, expected="reject" if shape in ["missing", "overlap", "budget"] else "accept", **result) sample["axis_size"] = size report["samples"].append(sample) (root / "results.json").write_text(json.dumps(report, indent=2) + '\n') print(json.dumps(sample), flush=True) if not sample["correct"]: break raise SystemExit(0 if all(s["correct"] for s in report["samples"]) else 1) if __name__ == "__main__": main()