File size: 2,922 Bytes
961d104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327a442
 
 
 
 
 
 
 
 
 
 
 
 
 
961d104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Executable semantics for solution reports and reviews of previous reports."""

import re

_RECORD = re.compile(r"[+-]?[0-9]{1,14}(?:\s*,\s*[+-]?[0-9]{1,14})*")


def parse(text, width):
    """Accept an entire bounded integer record, with optional matching square brackets."""
    text = text.strip()
    if text.startswith("[") and text.endswith("]"):
        text = text[1:-1].strip()
    if len(text) > 1024 or not _RECORD.fullmatch(text):
        return None
    values = tuple(map(int, text.split(",")))
    return values if len(values) == width else None


def render(record):
    return ",".join(map(str, record))


def named_values(text, width):
    """Secondary analysis of one explicit sN = integer assignment per line, in listed order."""
    lines=text.strip().splitlines()
    if len(lines)!=width:
        return None
    result=[]
    for index,line in enumerate(lines,1):
        match=re.fullmatch(rf"\s*s{index}\s*=\s*([+-]?[0-9]{{1,14}})\s*",line)
        if not match:
            return None
        result.append(int(match[1]))
    return tuple(result)


def solver_truth(problem, trace):
    """Provide an exact first-fault witness, including independently correct final answers."""
    predicted = parse(trace, len(problem.values))
    if predicted is None:
        return (0, -1, 0)
    for step, (value, truth) in enumerate(zip(predicted, problem.values, strict=True), 1):
        if value != truth:
            return (0, step, truth)
    return (1, 0, problem.values[-1])


def audit(previous, ideal, solution):
    """A positive meta-verdict certifies the predecessor, irrespective of solver acceptance."""
    observed = parse(previous, len(ideal))
    if observed is None:
        return (0, -1, 0, *solution)
    for field, (seen, wanted) in enumerate(zip(observed, ideal, strict=True), 1):
        if seen != wanted:
            return (0, field, wanted, *solution)
    return (1, 0, ideal[-1], *solution)


def expected(problem, trace, history):
    solution = solver_truth(problem, trace)
    ideal = solution
    for report in history:
        ideal = audit(report, ideal, solution)
    return ideal


def terminal(text, depth):
    """Learned rewards consult the solution bit in the last report, never the oracle."""
    record = parse(text, 3 if depth == 1 else 6)
    return int(record is not None and record[-3] == 1 and (depth == 1 or record[0] in (0, 1)))


def approval(text):
    """A top-down judge trains the immediately subordinate judge on its soundness bit."""
    record = parse(text, 6)
    return int(record is not None and record[0] == 1 and record[-3] in (0, 1))


def sound(text, truth):
    return parse(text, len(truth)) == truth


def mask_evidence(text):
    """The two masked twins are identical, including their visible solution and audit bits."""
    record = parse(text, 6)
    return "malformed" if record is None else f"{record[0]},?, ?,{record[3]},?,?"