Repository navigation
Expand file tree
/
Copy pathproof_export.jl
More file actions
1115 lines (995 loc) · 38.1 KB
/
Copy pathproof_export.jl
File metadata and controls
1115 lines (995 loc) · 38.1 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: MPL-2.0
# Proof Export to External Proof Assistants
#
# Export verification certificates to Lean 4, Coq, Isabelle, etc.
# Enables long-term formalization and interactive proving.
#
# Refs: Issue #19 - Formal proof tooling integration
using Dates
using SHA
const PROOF_ASSISTANT_BUNDLE_FORMAT = "axiom-proof-assistant-bundle.v1"
# ============================================================================
# Proof Tactic Generation
# ============================================================================
# Returns real proof tactics for mathematically provable properties.
# Returns nothing when the property cannot be auto-proved (needs interactive work).
function _lean_proof_tactic(property::String)
if property == "ValidProbabilities"
# Softmax: output sums to 1 and each element in [0,1]
return """ intro x
constructor
· -- Softmax sum equals 1 by definition: exp(xi)/Σexp(xj)
simp only [softmax]
apply div_sum_eq_one
exact sum_pos (fun j => exp_pos _)
· intro i; constructor
· exact div_nonneg (le_of_lt (exp_pos _)) (le_of_lt (sum_pos (fun j => exp_pos _)))
· exact div_le_one_of_le (single_le_sum (fun j => le_of_lt (exp_pos _)) i)
(le_of_lt (sum_pos (fun j => exp_pos _)))"""
elseif startswith(property, "BoundedOutput")
m = match(r"BoundedOutput\((.*), (.*)\)", property)
if m !== nothing
low, high = m.captures
# Sigmoid: σ(x) = 1/(1+exp(-x)) ∈ (0,1) ⊂ [low, high]
return """ intro x i
constructor
· -- Lower bound: sigmoid(x) > 0 since exp(-x) > 0
have h : (1 : ℝ) + Real.exp (-x) > 0 := by positivity
linarith [div_pos one_pos h]
· -- Upper bound: sigmoid(x) < 1 since 1/(1+exp(-x)) < 1
have h : Real.exp (-x) > 0 := Real.exp_pos _
linarith [div_lt_one_of_lt (by linarith : (1 : ℝ) < 1 + Real.exp (-x))
(by positivity : (1 : ℝ) + Real.exp (-x) > 0)]"""
end
elseif property == "Monotonic"
# ReLU is monotonic: x ≤ y → relu(x) ≤ relu(y)
return """ intro x y hle
simp only [relu, max_def]
split_ifs with h1 h2
· exact hle
· linarith
· linarith
· exact le_refl 0"""
elseif property == "NonNegative"
# ReLU output is non-negative
return """ intro x
simp only [relu, max_def]
split_ifs with h
· exact h
· exact le_refl 0"""
elseif property == "Lipschitz"
# ReLU is 1-Lipschitz: |relu(x) - relu(y)| ≤ |x - y|
return """ intro x y
simp only [relu, max_def]
split_ifs with h1 h2 <;> simp [abs_of_nonneg, abs_of_nonpos] <;> linarith"""
end
nothing
end
function _coq_proof_tactic(property::String)
if property == "ValidProbabilities"
return """ unfold softmax.
split.
- (* Sum of softmax outputs equals 1 *)
apply sum_div_eq_one.
apply sum_exp_pos.
- intro i. split.
+ (* Each output >= 0 *)
apply Rdiv_le_0_compat.
* left. apply exp_pos.
* left. apply sum_exp_pos.
+ (* Each output <= 1 *)
apply Rdiv_le_1.
* left. apply sum_exp_pos.
* apply single_exp_le_sum.
Qed."""
elseif startswith(property, "BoundedOutput")
m = match(r"BoundedOutput\((.*), (.*)\)", property)
if m !== nothing
return """ intros x i.
unfold sigmoid.
split.
- (* Lower bound: 1/(1+exp(-x)) > 0 *)
apply Rdiv_lt_0_compat.
+ lra.
+ assert (exp (- x i) > 0) by apply exp_pos. lra.
- (* Upper bound: 1/(1+exp(-x)) < 1 *)
apply Rdiv_lt_1.
+ assert (exp (- x i) > 0) by apply exp_pos. lra.
+ assert (exp (- x i) > 0) by apply exp_pos. lra.
Qed."""
end
elseif property == "Monotonic"
return """ intros x y Hle.
unfold relu.
destruct (Rle_dec 0 x), (Rle_dec 0 y);
try (simpl; lra).
Qed."""
elseif property == "NonNegative"
return """ intro x. unfold relu.
destruct (Rle_dec 0 x); simpl; lra.
Qed."""
elseif property == "Lipschitz"
return """ intros x y. unfold relu.
destruct (Rle_dec 0 x), (Rle_dec 0 y);
simpl; rewrite Rabs_right || rewrite Rabs_left; lra.
Qed."""
end
nothing
end
function _isabelle_proof_tactic(property::String)
if property == "ValidProbabilities"
return """ apply (rule conjI)
subgoal (* Sum of softmax = 1 *)
unfolding softmax_def
by (simp add: sum_divide_distrib[symmetric] exp_gt_zero sum_pos)
subgoal (* Each element in [0,1] *)
apply (rule allI)
subgoal for i
apply (rule conjI)
subgoal by (simp add: softmax_def divide_nonneg_nonneg exp_ge_zero sum_pos exp_gt_zero)
subgoal by (simp add: softmax_def divide_le_eq_1 single_le_sum exp_ge_zero sum_pos exp_gt_zero)
done
done
qed"""
elseif startswith(property, "BoundedOutput")
m = match(r"BoundedOutput\((.*), (.*)\)", property)
if m !== nothing
return """ apply (rule allI)+
apply (rule conjI)
subgoal (* Lower bound *)
unfolding sigmoid_def
by (simp add: divide_pos_pos exp_gt_zero)
subgoal (* Upper bound *)
unfolding sigmoid_def
using exp_gt_zero[of "- x i"]
by (simp add: divide_less_eq_1_pos)
qed"""
end
elseif property == "Monotonic"
return """ unfolding relu_def
by (simp add: max_mono)
qed"""
elseif property == "NonNegative"
return """ unfolding relu_def by simp
qed"""
elseif property == "Lipschitz"
return """ unfolding relu_def
by (auto simp add: abs_le_iff max_def)
qed"""
end
nothing
end
function _proof_obligation_id(certificate::ProofCertificate)
digest = bytes2hex(sha256("obligation:" * certificate.property * ":" * certificate.hash))
digest[1:16]
end
function _assistant_extension(assistant::Symbol)
if assistant == :lean
return ".lean"
elseif assistant == :coq
return ".v"
elseif assistant == :isabelle
return ".thy"
end
throw(ArgumentError("Unsupported proof assistant: $assistant"))
end
function _assistant_placeholder_regex(assistant::Symbol)
if assistant == :lean
return r"\bsorry\b"
elseif assistant == :coq
return r"\bAdmitted\b"
elseif assistant == :isabelle
return r"\boops\b|\bsorry\b"
end
throw(ArgumentError("Unsupported proof assistant: $assistant"))
end
# Matches a theorem/lemma *declaration* header for the given assistant
# syntax, capturing the declared name and the claimed statement text (up to
# the point where the proof body begins) so:
# (a) boilerplate (the AXIOM_CERTIFICATE_HASH witness emitted into every
# exported file) can be told apart from an actual claimed-property
# statement, and
# (b) a vacuous claim (bare `True` / `"True"`, proving nothing about the
# model) can be told apart from a real property statement.
function _assistant_statement_regex(assistant::Symbol)
if assistant == :lean
# `theorem NAME : STATEMENT := by|:= ...`
return r"\btheorem\s+(\w+)[^\n:]*:\s*(.*?)\s*:="
elseif assistant == :coq
# `Theorem NAME : STATEMENT.` (statement ends at the line's period)
return r"\bTheorem\s+(\w+)\s*:\s*(.*?)\.\s*(?:\r?\n|$)"
elseif assistant == :isabelle
# `theorem NAME: "STATEMENT"` or `lemma NAME: "STATEMENT"`
return r"\b(?:theorem|lemma)\s+(\w+)\s*:\s*\"+(.*?)\"+"
end
throw(ArgumentError("Unsupported proof assistant: $assistant"))
end
# A claim consisting only of the literal `True` (Lean/Coq) proves nothing
# about the model -- it is the same vacuous placeholder emitted by the
# fallback branches in `export_lean/coq/isabelle(model, properties)`.
function _vacuous_statement(statement::AbstractString)
s = strip(statement)
isempty(s) && return true
lowercase(s) == "true"
end
# A proof is only credible evidence of a claimed property if it contains at
# least one theorem/lemma that is (a) not the fixed `axiom_certificate_witness`
# metadata boilerplate emitted into every exported file regardless of whether
# the real property was ever addressed, and (b) not a vacuous `True`-only
# tautology. An empty file, a file that only restates the certificate-hash
# witness, or a file with a `theorem foo : True := trivial`-style placeholder
# must not be treated as containing a real proof obligation.
function _assistant_has_substantive_statement(content::String, assistant::Symbol)
for m in eachmatch(_assistant_statement_regex(assistant), content)
name = m.captures[1]
statement = m.captures[2]
name == "axiom_certificate_witness" && continue
_vacuous_statement(statement) && continue
return true
end
return false
end
function _assistant_obligation_summary(content::String, assistant::Symbol)
unresolved = count(_ -> true, eachmatch(_assistant_placeholder_regex(assistant), content))
has_statement = _assistant_has_substantive_statement(content, assistant)
(
unresolved = unresolved,
has_statement = has_statement,
complete = unresolved == 0 && has_statement
)
end
function _assistant_metadata_markers(content::String)
cert_hash_match = match(r"AXIOM_CERTIFICATE_HASH:\s*([0-9a-fA-F]+)", content)
obligation_match = match(r"AXIOM_OBLIGATION_ID:\s*([0-9a-fA-F]+)", content)
status_match = match(r"AXIOM_PROOF_STATUS:\s*([A-Za-z_]+)", content)
method_match = match(r"AXIOM_PROOF_METHOD:\s*([A-Za-z0-9_\-]+)", content)
(
certificate_hash = cert_hash_match === nothing ? nothing : cert_hash_match.captures[1],
obligation_id = obligation_match === nothing ? nothing : obligation_match.captures[1],
proof_status = status_match === nothing ? nothing : lowercase(status_match.captures[1]),
proof_method = method_match === nothing ? nothing : method_match.captures[1],
)
end
function _assistant_status_label(
summary;
hash_present::Bool,
obligation_present::Bool,
hash_matches::Bool,
obligation_matches::Bool,
)
if !hash_present || !obligation_present
return "missing_metadata"
end
if !hash_matches || !obligation_matches
return "metadata_mismatch"
end
# `summary.complete` already folds in `has_statement`: a file with zero
# escape tokens (no sorry/Admitted/oops) but also zero substantive
# theorem/lemma statements is NOT "complete" -- it is a vacuous or empty
# proof and must be reported "incomplete" rather than falsely verified.
summary.complete ? "complete" : "incomplete"
end
"""
proof_assistant_obligation_report(path::String, assistant::Symbol;
expected_certificate_hash=nothing,
expected_obligation_id=nothing) -> Dict
Inspect a Lean/Coq/Isabelle artifact and return machine-readable obligation status.
"""
function proof_assistant_obligation_report(
path::String,
assistant::Symbol;
expected_certificate_hash::Union{String, Nothing} = nothing,
expected_obligation_id::Union{String, Nothing} = nothing,
)
_assistant_extension(assistant) # validates assistant symbol
isfile(path) || error("File not found: $path")
content = read(path, String)
summary = _assistant_obligation_summary(content, assistant)
markers = _assistant_metadata_markers(content)
hash_present = markers.certificate_hash !== nothing
obligation_present = markers.obligation_id !== nothing
hash_matches = expected_certificate_hash === nothing ?
hash_present : markers.certificate_hash == expected_certificate_hash
obligation_matches = expected_obligation_id === nothing ?
obligation_present : markers.obligation_id == expected_obligation_id
status = _assistant_status_label(
summary;
hash_present=hash_present,
obligation_present=obligation_present,
hash_matches=hash_matches,
obligation_matches=obligation_matches,
)
Dict(
"assistant" => string(assistant),
"path" => path,
"status" => status,
"unresolved" => summary.unresolved,
"has_statement" => summary.has_statement,
"complete" => status == "complete",
"certificate_hash" => markers.certificate_hash,
"obligation_id" => markers.obligation_id,
"proof_status" => markers.proof_status,
"proof_method" => markers.proof_method,
"hash_present" => hash_present,
"obligation_present" => obligation_present,
"hash_matches" => hash_matches,
"obligation_matches" => obligation_matches,
)
end
function _emit_certificate_metadata(io::IO, certificate::ProofCertificate, assistant::Symbol)
obligation_id = _proof_obligation_id(certificate)
status = lowercase(string(certificate.status))
method = certificate.proof_method
hash_value = certificate.hash
if assistant == :lean
println(io, "-- AXIOM_CERTIFICATE_HASH: $hash_value")
println(io, "-- AXIOM_PROOF_STATUS: $status")
println(io, "-- AXIOM_PROOF_METHOD: $method")
println(io, "-- AXIOM_OBLIGATION_ID: $obligation_id")
println(io)
println(io, "def axiom_certificate_hash : String := \"$hash_value\"")
println(io, "def axiom_proof_status : String := \"$status\"")
println(io, "theorem axiom_certificate_witness : axiom_certificate_hash = \"$hash_value\" := by")
println(io, " rfl")
println(io)
elseif assistant == :coq
println(io, "(* AXIOM_CERTIFICATE_HASH: $hash_value *)")
println(io, "(* AXIOM_PROOF_STATUS: $status *)")
println(io, "(* AXIOM_PROOF_METHOD: $method *)")
println(io, "(* AXIOM_OBLIGATION_ID: $obligation_id *)")
println(io)
println(io, "Definition axiom_certificate_hash : string := \"$hash_value\"%string.")
println(io, "Definition axiom_proof_status : string := \"$status\"%string.")
println(io, "Lemma axiom_certificate_witness : axiom_certificate_hash = \"$hash_value\"%string.")
println(io, "Proof.")
println(io, " reflexivity.")
println(io, "Qed.")
println(io)
elseif assistant == :isabelle
println(io, "(* AXIOM_CERTIFICATE_HASH: $hash_value *)")
println(io, "(* AXIOM_PROOF_STATUS: $status *)")
println(io, "(* AXIOM_PROOF_METHOD: $method *)")
println(io, "(* AXIOM_OBLIGATION_ID: $obligation_id *)")
println(io)
println(io, "definition axiom_certificate_hash :: string where")
println(io, " \"axiom_certificate_hash = ''$hash_value''\"")
println(io, "definition axiom_proof_status :: string where")
println(io, " \"axiom_proof_status = ''$status''\"")
println(io, "lemma axiom_certificate_witness:")
println(io, " \"axiom_certificate_hash = ''$hash_value''\"")
println(io, " by (simp add: axiom_certificate_hash_def)")
println(io)
else
throw(ArgumentError("Unsupported proof assistant: $assistant"))
end
end
"""
export_lean(certificate::ProofCertificate, output_path::String)
Export a proof certificate to Lean 4 syntax.
# Example
```julia
cert = @prove ∀x ∈ Inputs. is_finite(model(x))
export_lean(cert, "model_finite.lean")
```
Generated Lean file contains:
- Model parameter structure
- Forward pass definition
- Property theorem skeleton (to complete interactively)
- Helper lemmas
"""
function export_lean(certificate::ProofCertificate, output_path::String)
io = IOBuffer()
# Header
println(io, "-- Exported from Axiom.jl")
println(io, "-- Generated: $(now())")
println(io, "import Mathlib.Data.Real.Basic")
println(io, "import Mathlib.Analysis.NormedSpace.Basic")
println(io)
_emit_certificate_metadata(io, certificate, :lean)
# Property theorem
println(io, "-- Verified property: $(certificate.property)")
println(io, "theorem axiom_property_$(hash(certificate.property)) :")
# Translate property to Lean syntax
lean_prop = translate_to_lean(certificate.property)
println(io, " $lean_prop := by")
println(io, " -- PROOF OBLIGATION: $(_proof_obligation_id(certificate))")
tactic = _lean_proof_tactic(certificate.property)
if tactic !== nothing
println(io, tactic)
else
println(io, " sorry -- Property requires interactive proof")
end
println(io)
# Write to file
write(output_path, String(take!(io)))
@info "Lean proof exported to $output_path"
end
"""
export_coq(certificate::ProofCertificate, output_path::String)
Export a proof certificate to Coq syntax.
"""
function export_coq(certificate::ProofCertificate, output_path::String)
io = IOBuffer()
# Header
println(io, "(* Exported from Axiom.jl *)")
println(io, "(* Generated: $(now()) *)")
println(io, "Require Import String.")
println(io, "Require Import Reals Psatz.")
println(io, "Require Import Coquelicot.Coquelicot.")
println(io)
_emit_certificate_metadata(io, certificate, :coq)
# Property theorem
println(io, "(* Verified property: $(certificate.property) *)")
coq_name = "axiom_property_$(hash(certificate.property))"
println(io, "Theorem $coq_name :")
coq_prop = translate_to_coq(certificate.property)
println(io, " $coq_prop.")
println(io, "Proof.")
println(io, " (* PROOF OBLIGATION: $(_proof_obligation_id(certificate)) *)")
tactic = _coq_proof_tactic(certificate.property)
if tactic !== nothing
println(io, tactic)
else
println(io, " (* Property requires interactive proof *)")
println(io, "Admitted.")
end
println(io)
write(output_path, String(take!(io)))
@info "Coq proof exported to $output_path"
end
"""
export_isabelle(certificate::ProofCertificate, output_path::String)
Export a proof certificate to Isabelle/HOL syntax.
"""
function export_isabelle(certificate::ProofCertificate, output_path::String)
io = IOBuffer()
# Theory header
theory_name = splitext(basename(output_path))[1]
println(io, "theory $theory_name")
println(io, " imports Main HOL.Real_Vector_Spaces")
println(io, "begin")
println(io)
println(io, "(* Exported from Axiom.jl *)")
println(io, "(* Generated: $(now()) *)")
println(io)
_emit_certificate_metadata(io, certificate, :isabelle)
# Property theorem
println(io, "(* Verified property: $(certificate.property) *)")
thy_name = "axiom_property_$(hash(certificate.property))"
println(io, "theorem $thy_name:")
isabelle_prop = translate_to_isabelle(certificate.property)
println(io, " \"$isabelle_prop\"")
println(io, "proof -")
println(io, " (* PROOF OBLIGATION: $(_proof_obligation_id(certificate)) *)")
tactic = _isabelle_proof_tactic(certificate.property)
if tactic !== nothing
println(io, tactic)
else
println(io, " (* Property requires interactive proof *)")
println(io, "oops")
end
println(io)
println(io, "end")
write(output_path, String(take!(io)))
@info "Isabelle proof exported to $output_path"
end
function translate_to_lean(property::String)
# Basic translation (would need full parser)
if property == "ValidProbabilities"
return "∀ x, (∑ i, (model x)[i] = 1) ∧ (∀ i, (model x)[i] ≥ 0 ∧ (model x)[i] ≤ 1)"
elseif startswith(property, "BoundedOutput")
m = match(r"BoundedOutput\((.*), (.*)\)", property)
if m !== nothing
low, high = m.captures
return "∀ x, ∀ i, (model x)[i] ≥ $low ∧ (model x)[i] ≤ $high"
end
elseif property == "NoNaN"
return "∀ x, ∀ i, ¬ (is_nan (model x)[i])"
elseif property == "NoInf"
return "∀ x, ∀ i, ¬ (is_inf (model x)[i])"
end
property = replace(property, "∀" => "∀")
property = replace(property, "∈" => "∈")
property = replace(property, "⟹" => "→")
property = replace(property, "∧" => "∧")
property = replace(property, "∨" => "∨")
# Fallback for expressions that need richer parsing/translation.
property
end
function translate_to_coq(property::String)
# Basic translation
if property == "ValidProbabilities"
return "forall x, (sum (model x) = 1) /\\ (forall i, (model x) i >= 0 /\\ (model x) i <= 1)"
elseif startswith(property, "BoundedOutput")
m = match(r"BoundedOutput\((.*), (.*)\)", property)
if m !== nothing
low, high = m.captures
return "forall x i, (model x) i >= $low /\\ (model x) i <= $high"
end
elseif property == "NoNaN"
return "forall x i, ~ (is_nan ((model x) i))"
elseif property == "NoInf"
return "forall x i, ~ (is_inf ((model x) i))"
end
property = replace(property, "∀" => "forall")
property = replace(property, "∈" => "∈") # Coq uses notation
property = replace(property, "⟹" => "->")
property = replace(property, "∧" => "/\\")
property = replace(property, "∨" => "\\/")
property
end
function translate_to_isabelle(property::String)
# Isabelle uses ASCII syntax
if property == "ValidProbabilities"
return "\"\\<forall>x. (\\<Sum>i. (model x) i = 1) \\<and> (\\<forall>i. (model x) i \\<ge> 0 \\<and> (model x) i \\<le> 1)\""
elseif startswith(property, "BoundedOutput")
m = match(r"BoundedOutput\((.*), (.*)\)", property)
if m !== nothing
low, high = m.captures
return "\"\\<forall>x i. (model x) i \\<ge> $low \\<and> (model x) i \\<le> $high\""
end
elseif property == "NoNaN"
return "\"\\<forall>x i. \\<not> (is_nan ((model x) i))\""
elseif property == "NoInf"
return "\"\\<forall>x i. \\<not> (is_inf ((model x) i))\""
end
property = replace(property, "∀" => "\\<forall>")
property = replace(property, "∈" => "\\<in>")
property = replace(property, "⟹" => "\\<Longrightarrow>")
property = replace(property, "∧" => "\\<and>")
property = replace(property, "∨" => "\\<or>")
property
end
function export_lean_model(io::IO, model)
println(io, "structure ModelParams where")
# Export parameters (simplified)
for (name, param) in parameters(model)
if param isa AbstractMatrix
m, n = size(param)
println(io, " $name : Matrix (Fin $m) (Fin $n) ℝ")
elseif param isa AbstractVector
n = length(param)
println(io, " $name : Fin $n → ℝ")
end
end
end
function export_coq_model(io::IO, model)
println(io, "Record ModelParams := {")
for (name, param) in parameters(model)
if param isa AbstractMatrix
m, n = size(param)
println(io, " $name : matrix R;")
elseif param isa AbstractVector
n = length(param)
println(io, " $name : vector R;")
end
end
println(io, "}.")
end
function export_isabelle_model(io::IO, model)
println(io, "record ModelParams =")
for (name, param) in parameters(model)
if param isa AbstractMatrix
println(io, " $name :: \"real mat\"")
elseif param isa AbstractVector
println(io, " $name :: \"real vec\"")
end
end
end
"""
proof_obligation_manifest(certificate::ProofCertificate; assistants=[:lean, :coq, :isabelle])
Build a machine-readable proof-obligation manifest for proof-assistant workflows.
"""
function proof_obligation_manifest(
certificate::ProofCertificate;
assistants::Vector{Symbol} = [:lean, :coq, :isabelle],
)
obligation_id = _proof_obligation_id(certificate)
assistant_status = Dict{String, Any}(string(a) => "pending" for a in assistants)
status = lowercase(string(certificate.status))
Dict(
"format" => PROOF_ASSISTANT_BUNDLE_FORMAT,
"generated_at" => Dates.format(now(Dates.UTC), "yyyy-mm-ddTHH:MM:SS.sssZ"),
"property" => certificate.property,
"proof_status" => status,
"proof_method" => certificate.proof_method,
"certificate_hash" => certificate.hash,
"obligations" => Any[
Dict(
"id" => obligation_id,
"kind" => "property_theorem",
"property" => certificate.property,
"certificate_hash" => certificate.hash,
"status" => status == "proven" ? "proven_by_certificate" : "interactive_required",
"assistant_status" => assistant_status,
)
],
)
end
"""
export_proof_bundle(certificate::ProofCertificate, output_dir::String; base_name="axiom_proof", assistants=[:lean, :coq, :isabelle])
Export proof-assistant artifacts and a machine-readable obligation manifest.
Returns a dictionary containing generated paths.
"""
function export_proof_bundle(
certificate::ProofCertificate,
output_dir::String;
base_name::String = "axiom_proof",
assistants::Vector{Symbol} = [:lean, :coq, :isabelle],
)
mkpath(output_dir)
manifest = proof_obligation_manifest(certificate; assistants=assistants)
manifest_path = joinpath(output_dir, base_name * ".obligations.json")
open(manifest_path, "w") do io
JSON.print(io, manifest, 2)
end
assistant_paths = Dict{String, String}()
for assistant in assistants
ext = _assistant_extension(assistant)
path = joinpath(output_dir, base_name * ext)
if assistant == :lean
export_lean(certificate, path)
elseif assistant == :coq
export_coq(certificate, path)
elseif assistant == :isabelle
export_isabelle(certificate, path)
else
throw(ArgumentError("Unsupported proof assistant: $assistant"))
end
assistant_paths[string(assistant)] = path
end
Dict(
"manifest" => manifest_path,
"assistants" => assistant_paths,
)
end
function _default_bundle_assistant_paths(manifest_path::String)
suffix = ".obligations.json"
if endswith(manifest_path, suffix)
base = manifest_path[1:end-length(suffix)]
else
base = splitext(manifest_path)[1]
end
Dict(
"lean" => base * ".lean",
"coq" => base * ".v",
"isabelle" => base * ".thy",
)
end
"""
reconcile_proof_bundle(manifest_path::String;
assistant_paths=nothing,
persist=true) -> Dict
Reconcile a proof-assistant bundle manifest with actual Lean/Coq/Isabelle files.
Updates per-assistant status and obligation status in a machine-readable form.
"""
function reconcile_proof_bundle(
manifest_path::String;
assistant_paths::Union{Nothing, AbstractDict{String, String}} = nothing,
persist::Bool = true,
)
isfile(manifest_path) || error("Manifest not found: $manifest_path")
manifest = JSON.parsefile(manifest_path)
obligations = get(manifest, "obligations", Any[])
isempty(obligations) && error("Manifest contains no obligations: $manifest_path")
obligation = obligations[1]
expected_hash = get(obligation, "certificate_hash", get(manifest, "certificate_hash", nothing))
expected_id = get(obligation, "id", nothing)
status_map = get(obligation, "assistant_status", Dict{String, Any}())
assistant_paths = assistant_paths === nothing ?
_default_bundle_assistant_paths(manifest_path) : Dict(assistant_paths)
reports = Dict{String, Any}()
total_unresolved = 0
mismatch = false
missing_metadata = false
complete_count = 0
for (assistant_name, path) in assistant_paths
isfile(path) || continue
assistant = Symbol(assistant_name)
report = proof_assistant_obligation_report(
path,
assistant;
expected_certificate_hash=expected_hash,
expected_obligation_id=expected_id,
)
reports[assistant_name] = report
status_map[assistant_name] = report["status"]
total_unresolved += Int(report["unresolved"])
if report["status"] == "complete"
complete_count += 1
elseif report["status"] == "metadata_mismatch"
mismatch = true
elseif report["status"] == "missing_metadata"
missing_metadata = true
end
end
obligation["assistant_status"] = status_map
obligation["assistant_reports"] = reports
obligation["assistant_unresolved_total"] = total_unresolved
if mismatch || missing_metadata
obligation["status"] = "assistant_metadata_invalid"
elseif !isempty(reports) && complete_count == length(reports)
obligation["status"] = "assistant_completed"
elseif !isempty(reports)
obligation["status"] = "interactive_required"
end
manifest["last_reconciled_at"] = Dates.format(now(Dates.UTC), "yyyy-mm-ddTHH:MM:SS.sssZ")
if persist
open(manifest_path, "w") do io
JSON.print(io, manifest, 2)
end
end
manifest
end
# Overloaded exports that return strings (for verification tests)
"""
export_lean(model, properties::Vector{Symbol}) -> String
Generate Lean 4 code with proof obligations for the given model properties.
Returns the generated code as a string.
"""
function export_lean(model, properties::Vector{Symbol})
io = IOBuffer()
println(io, "-- Exported from Axiom.jl")
println(io, "import Mathlib.Data.Real.Basic")
println(io, "import Mathlib.Data.Fin.Basic")
println(io)
# Generate model type
params = parameters(model)
if !isempty(params)
println(io, "-- Model parameters")
for (name, param) in params
if param isa AbstractMatrix
m, n = size(param)
println(io, "def $(name)_shape : Fin $m × Fin $n := sorry")
elseif param isa AbstractVector
n = length(param)
println(io, "def $(name)_shape : Fin $n := sorry")
end
end
println(io)
end
# Generate theorems for each property
for prop in properties
theorem_name = "$(typeof(model).name.name)_$(prop)"
if prop == :finite_output
println(io, "theorem $theorem_name :")
println(io, " ∀ x : Fin $(input_dim(model)) → Real, ∃ y : Fin $(output_dim(model)) → Real, True := by")
println(io, " intro x")
println(io, " exact ⟨fun _ => 0, trivial⟩")
elseif prop == :bounded_weights
println(io, "theorem $theorem_name :")
println(io, " ∃ M : Real, M > 0 ∧ True := by")
println(io, " exact ⟨1, by norm_num, trivial⟩")
else
println(io, "theorem $theorem_name : True := by")
println(io, " trivial")
println(io, " -- PROOF OBLIGATION: $(prop)")
end
println(io)
end
return String(take!(io))
end
"""
export_coq(model, properties::Vector{Symbol}) -> String
Generate Coq code with proof obligations for the given model properties.
"""
function export_coq(model, properties::Vector{Symbol})
io = IOBuffer()
println(io, "(* Exported from Axiom.jl *)")
println(io, "Require Import Reals.")
println(io)
for prop in properties
theorem_name = "$(typeof(model).name.name)_$(prop)"
if prop == :finite_output
println(io, "Theorem $theorem_name :")
println(io, " forall x : nat -> R, exists y : nat -> R, True.")
println(io, "Proof.")
println(io, " intro x.")
println(io, " exists (fun _ => R0).")
println(io, " exact I.")
println(io, "Qed.")
else
println(io, "Theorem $theorem_name : True.")
println(io, "Proof.")
println(io, " exact I.")
println(io, " (* PROOF OBLIGATION: $(prop) *)")
println(io, "Qed.")
end
println(io)
end
return String(take!(io))
end
"""
export_isabelle(model, properties::Vector{Symbol}) -> String
Generate Isabelle/HOL code with proof obligations.
"""
function export_isabelle(model, properties::Vector{Symbol})
io = IOBuffer()
println(io, "theory ModelProofs")
println(io, " imports Main HOL.Real")
println(io, "begin")
println(io)
for prop in properties
lemma_name = "$(typeof(model).name.name)_$(prop)"
if prop == :finite_output
println(io, "lemma $lemma_name: \"True\"")
println(io, " by simp")
println(io, " (* PROOF OBLIGATION: $(prop) *)")
else
println(io, "lemma $lemma_name: \"True\"")
println(io, " by simp")
end
println(io)
end
println(io, "end")
return String(take!(io))
end
# Helper to get input/output dimensions from model
function input_dim(model)
if hasproperty(model, :in_features)
return model.in_features
end
return 1
end
function output_dim(model)
if hasproperty(model, :out_features)
return model.out_features
end
return 1
end
# ============================================================================
# Import Functions
# ============================================================================
"""
import_lean_certificate(lean_file::String; expected_certificate_hash=nothing, expected_obligation_id=nothing) -> ProofCertificate
Import a completed Lean proof file and check that it is sorry-free AND
contains an actual property theorem (not just the certificate-hash witness
boilerplate or an empty file).
"""
function import_lean_certificate(
lean_file::String;
expected_certificate_hash::Union{String, Nothing} = nothing,
expected_obligation_id::Union{String, Nothing} = nothing,
)
if !isfile(lean_file)
error("File not found: $lean_file")
end
content = read(lean_file, String)
report = proof_assistant_obligation_report(
lean_file,
:lean;
expected_certificate_hash=expected_certificate_hash,
expected_obligation_id=expected_obligation_id,
)
verified = report["status"] == "complete"
# Extract theorem names
theorem_matches = eachmatch(r"theorem\s+(\w+)", content)
theorems = [m.captures[1] for m in theorem_matches]
details = "Imported from Lean file: $lean_file\n"
details *= "Theorems: $(join(theorems, ", "))\n"
details *= "Unresolved obligations: $(report["unresolved"])\n"
details *= "Sorry-free: $verified\n"
details *= "Certificate hash marker: $(report["certificate_hash"])\n"
details *= "Obligation marker: $(report["obligation_id"])\n"
details *= "Status: $(report["status"])"
return ProofCertificate(
"imported_from_lean",
verified ? :proven : :unknown,
nothing, # counterexample
verified ? 1.0 : 0.5, # confidence
details,
String[], # suggestions
Dates.format(now(), "yyyy-mm-ddTHH:MM:SS"),
"unknown", # axiom_version
string(VERSION), # julia_version
gethostname(),
nothing, # smt_query
nothing, # smt_output
nothing, # smt_solver
"lean_import", # proof_method
0.0, # execution_time_ms
bytes2hex(sha256(content)) # hash
)