-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprover_integration.zig
More file actions
276 lines (246 loc) · 10 KB
/
Copy pathprover_integration.zig
File metadata and controls
276 lines (246 loc) · 10 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//! Valence Shell - Prover Integration
//!
//! Demonstrates integration with zig-prover-ffi for runtime verification
//! of formal proofs. This bridges the Coq/Lean/Agda/Isabelle/Mizar/Z3
//! proof files in proofs/ with the Zig implementation.
//!
//! Use cases:
//! 1. CI verification of proof files
//! 2. Runtime verification before critical operations
//! 3. Proof certificate validation
const std = @import("std");
const prover = @import("prover");
/// Proof verification result for valence-shell operations
pub const VerificationResult = struct {
status: prover.ProofStatus,
prover_used: prover.ProverKind,
duration_ms: u64,
message: []const u8,
pub fn isVerified(self: VerificationResult) bool {
return self.status == .verified;
}
};
/// Verifies a proof file using the appropriate prover
pub fn verifyProofFile(
allocator: std.mem.Allocator,
file_path: []const u8,
endpoint: []const u8,
) !VerificationResult {
// Detect prover from file extension
const ext = std.fs.path.extension(file_path);
const prover_kind = prover.ProverKind.fromExtension(ext) orelse {
return VerificationResult{
.status = .unknown,
.prover_used = .z3, // placeholder
.duration_ms = 0,
.message = "Unknown file extension - cannot determine prover",
};
};
// Read file content
const file = try std.fs.cwd().openFile(file_path, .{});
defer file.close();
const content = try file.readToEndAlloc(allocator, 10 * 1024 * 1024); // 10MB max
defer allocator.free(content);
// Initialize prover client
var client = prover.ProverClient.init(allocator, endpoint);
// Verify
const result = client.verifyProof(prover_kind, content, file_path) catch |err| {
return VerificationResult{
.status = .err,
.prover_used = prover_kind,
.duration_ms = 0,
.message = switch (err) {
prover.Error.ProverNotFound => "Prover not found - is it installed?",
prover.Error.Timeout => "Verification timed out",
prover.Error.SubprocessFailed => "Prover subprocess failed",
else => "Verification failed",
},
};
};
return VerificationResult{
.status = result.status,
.prover_used = prover_kind,
.duration_ms = result.duration_ms,
.message = result.message,
};
}
/// Verify all proofs in a directory
pub fn verifyProofDirectory(
allocator: std.mem.Allocator,
dir_path: []const u8,
endpoint: []const u8,
) !struct { passed: u32, failed: u32, skipped: u32 } {
var passed: u32 = 0;
var failed: u32 = 0;
var skipped: u32 = 0;
var dir = try std.fs.cwd().openDir(dir_path, .{ .iterate = true });
defer dir.close();
var iter = dir.iterate();
while (try iter.next()) |entry| {
if (entry.kind != .file) continue;
// Check if it's a proof file
const ext = std.fs.path.extension(entry.name);
if (prover.ProverKind.fromExtension(ext) == null) {
skipped += 1;
continue;
}
// Build full path
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const full_path = try std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ dir_path, entry.name });
const result = verifyProofFile(allocator, full_path, endpoint) catch {
failed += 1;
continue;
};
if (result.isVerified()) {
passed += 1;
} else {
failed += 1;
}
}
return .{ .passed = passed, .failed = failed, .skipped = skipped };
}
/// Valence-specific: Check if filesystem operation has valid proof backing
pub const ProofBackingCheck = struct {
/// Operation types that require proof backing
pub const Operation = enum {
mkdir,
rmdir,
create_file,
delete_file,
pub fn proofFile(self: Operation, prover_system: []const u8) []const u8 {
return switch (self) {
.mkdir, .rmdir => switch (prover_system[0]) {
'c' => "proofs/coq/filesystem_model.v",
'l' => "proofs/lean4/FilesystemModel.lean",
'a' => "proofs/agda/FilesystemModel.agda",
'i' => "proofs/isabelle/FilesystemModel.thy",
'm' => "proofs/mizar/filesystem_model.miz",
'z' => "proofs/z3/filesystem_operations.smt2",
else => "proofs/coq/filesystem_model.v",
},
.create_file, .delete_file => switch (prover_system[0]) {
'c' => "proofs/coq/file_operations.v",
'l' => "proofs/lean4/FileOperations.lean",
'a' => "proofs/agda/FileOperations.agda",
'i' => "proofs/isabelle/FileOperations.thy",
'm' => "proofs/mizar/file_operations.miz",
'z' => "proofs/z3/filesystem_operations.smt2",
else => "proofs/coq/file_operations.v",
},
};
}
};
/// Verify that the proof backing for an operation is valid
pub fn checkProofBacking(
allocator: std.mem.Allocator,
operation: Operation,
prover_system: []const u8,
endpoint: []const u8,
) !bool {
const proof_file = operation.proofFile(prover_system);
const result = try verifyProofFile(allocator, proof_file, endpoint);
return result.isVerified();
}
};
// =============================================================================
// Main - CLI tool for proof verification
// =============================================================================
pub fn main() !void {
const allocator = std.heap.page_allocator;
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len < 2) {
std.debug.print(
\\Valence Shell - Prover Integration
\\
\\Usage:
\\ prover_test <proof-file> Verify a single proof file
\\ prover_test --dir <directory> Verify all proofs in directory
\\ prover_test --check <operation> Check proof backing for operation
\\
\\Supported provers (12 total):
\\ Tier 1: Agda (.agda), Coq (.v), Lean (.lean), Isabelle (.thy), Z3 (.smt2), CVC5
\\ Tier 2: Metamath (.mm), HOL Light (.ml), Mizar (.miz)
\\ Tier 3: PVS (.pvs), ACL2 (.lisp), HOL4 (.sml)
\\
\\Examples:
\\ prover_test proofs/coq/filesystem_model.v
\\ prover_test --dir proofs/lean4
\\ prover_test --check mkdir
\\
, .{});
return;
}
const endpoint = "http://localhost:8080/graphql"; // ECHIDNA Core endpoint
if (std.mem.eql(u8, args[1], "--dir")) {
if (args.len < 3) {
std.debug.print("Error: --dir requires a directory path\n", .{});
return;
}
const result = try verifyProofDirectory(allocator, args[2], endpoint);
std.debug.print(
\\Directory verification complete:
\\ Passed: {d}
\\ Failed: {d}
\\ Skipped: {d}
\\
, .{ result.passed, result.failed, result.skipped });
} else if (std.mem.eql(u8, args[1], "--check")) {
if (args.len < 3) {
std.debug.print("Error: --check requires an operation name\n", .{});
return;
}
const op = std.meta.stringToEnum(ProofBackingCheck.Operation, args[2]) orelse {
std.debug.print("Error: Unknown operation '{s}'\n", .{args[2]});
return;
};
const has_backing = try ProofBackingCheck.checkProofBacking(allocator, op, "coq", endpoint);
std.debug.print("Operation '{s}' proof backing: {s}\n", .{
args[2],
if (has_backing) "VERIFIED" else "NOT VERIFIED",
});
} else {
// Single file verification
const result = try verifyProofFile(allocator, args[1], endpoint);
std.debug.print(
\\Verification result:
\\ File: {s}
\\ Prover: {s}
\\ Status: {s}
\\ Duration: {d}ms
\\ Message: {s}
\\
, .{
args[1],
result.prover_used.displayName(),
@tagName(result.status),
result.duration_ms,
result.message,
});
}
}
// =============================================================================
// Tests
// =============================================================================
test "prover kind detection" {
try std.testing.expectEqual(prover.ProverKind.coq, prover.ProverKind.fromExtension(".v").?);
try std.testing.expectEqual(prover.ProverKind.lean, prover.ProverKind.fromExtension(".lean").?);
try std.testing.expectEqual(prover.ProverKind.agda, prover.ProverKind.fromExtension(".agda").?);
try std.testing.expectEqual(prover.ProverKind.isabelle, prover.ProverKind.fromExtension(".thy").?);
try std.testing.expectEqual(prover.ProverKind.z3, prover.ProverKind.fromExtension(".smt2").?);
try std.testing.expectEqual(prover.ProverKind.mizar, prover.ProverKind.fromExtension(".miz").?);
}
test "prover tiers" {
try std.testing.expectEqual(@as(u8, 1), prover.ProverKind.coq.tier());
try std.testing.expectEqual(@as(u8, 1), prover.ProverKind.lean.tier());
try std.testing.expectEqual(@as(u8, 2), prover.ProverKind.mizar.tier());
try std.testing.expectEqual(@as(u8, 3), prover.ProverKind.pvs.tier());
}
test "proof backing file paths" {
const mkdir_coq = ProofBackingCheck.Operation.mkdir.proofFile("coq");
try std.testing.expectEqualStrings("proofs/coq/filesystem_model.v", mkdir_coq);
const create_lean = ProofBackingCheck.Operation.create_file.proofFile("lean4");
try std.testing.expectEqualStrings("proofs/lean4/FileOperations.lean", create_lean);
}