add logic/verification_loop.pl
Browse files- logic/verification_loop.pl +67 -0
logic/verification_loop.pl
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
:- consult('sovereign_verification.pl').
|
| 2 |
+
|
| 3 |
+
generate_problems(Intent, MathlibCtx, OperatorSig, OutputFile) :-
|
| 4 |
+
setup_call_cleanup(
|
| 5 |
+
open(OutputFile, write, Stream),
|
| 6 |
+
json_write_dict(Stream, _{
|
| 7 |
+
id:"demo-problem-1",
|
| 8 |
+
statement:Intent,
|
| 9 |
+
context:MathlibCtx,
|
| 10 |
+
tacticHint:"aesop",
|
| 11 |
+
meta:_{
|
| 12 |
+
source:"granite-4.1-verifier",
|
| 13 |
+
timestamp:"2026-07-14T00:00:00Z",
|
| 14 |
+
operatorSig:OperatorSig,
|
| 15 |
+
maxSteps:30,
|
| 16 |
+
allowedTactics:["rw","simp_all","norm_num","linarith","nlinarith","aesop","apply","exact","intro","obtain","cases","induction"]
|
| 17 |
+
}
|
| 18 |
+
}, [width(0)]),
|
| 19 |
+
close(Stream)
|
| 20 |
+
).
|
| 21 |
+
|
| 22 |
+
call_granite(InputFile, OutputFile) :-
|
| 23 |
+
setup_call_cleanup(
|
| 24 |
+
open(InputFile, read, In),
|
| 25 |
+
read_string(In, _, Content),
|
| 26 |
+
close(In)
|
| 27 |
+
),
|
| 28 |
+
setup_call_cleanup(
|
| 29 |
+
open(OutputFile, write, Out),
|
| 30 |
+
format(Out, "{\"problemId\":\"demo-problem-1\",\"status\":\"proposed\",\"proofScript\":\"~w\",\"diagnostics\":[]}~n", [Content]),
|
| 31 |
+
close(Out)
|
| 32 |
+
).
|
| 33 |
+
|
| 34 |
+
parse_and_validate(JsonlFile, success(JsonlFile)).
|
| 35 |
+
|
| 36 |
+
%% Shell out to `lake exe verify -- --parse <file>` and capture exit code.
|
| 37 |
+
%% Requires SWI-Prolog shell/2. Lake must be on PATH or LAKE_BIN env set.
|
| 38 |
+
execute_verification(JsonlFile, Result) :-
|
| 39 |
+
( getenv('LAKE_BIN', Lake) -> true ; Lake = 'lake' ),
|
| 40 |
+
atomic_list_concat([Lake, ' exe verify -- --parse ', JsonlFile], Cmd),
|
| 41 |
+
( shell(Cmd, 0) ->
|
| 42 |
+
Result = result(0, verified)
|
| 43 |
+
;
|
| 44 |
+
Result = result(1, parse_failed)
|
| 45 |
+
).
|
| 46 |
+
|
| 47 |
+
verify_with_retries(Intent, OperatorSig, MaxRetries, FinalResult) :-
|
| 48 |
+
verify_loop(Intent, OperatorSig, 0, MaxRetries, FinalResult).
|
| 49 |
+
|
| 50 |
+
verify_loop(Intent, OperatorSig, Attempt, MaxRetries, FinalResult) :-
|
| 51 |
+
Attempt < MaxRetries,
|
| 52 |
+
generate_problems(Intent, ["import Mathlib"], OperatorSig, 'input.jsonl'),
|
| 53 |
+
call_granite('input.jsonl', 'granite_response.jsonl'),
|
| 54 |
+
parse_and_validate('granite_response.jsonl', ParseRes),
|
| 55 |
+
( ParseRes = success(_) ->
|
| 56 |
+
execute_verification('granite_response.jsonl', KernelResult),
|
| 57 |
+
( KernelResult = result(0, _) ->
|
| 58 |
+
FinalResult = success(KernelResult)
|
| 59 |
+
; NextAttempt is Attempt + 1,
|
| 60 |
+
verify_loop(Intent, OperatorSig, NextAttempt, MaxRetries, FinalResult)
|
| 61 |
+
)
|
| 62 |
+
; NextAttempt is Attempt + 1,
|
| 63 |
+
verify_loop(Intent, OperatorSig, NextAttempt, MaxRetries, FinalResult)
|
| 64 |
+
).
|
| 65 |
+
|
| 66 |
+
verify_loop(_, _, Attempt, MaxRetries, fail(max_retries_exceeded)) :-
|
| 67 |
+
Attempt >= MaxRetries.
|