Skip to content

Commit 15b19cc

Browse files
hyperpolymathclaude
andcommitted
feat(vcl/tropical): ProofOptimalAssignment clause + TropicalDeterminant.jl
P2 of the tropical bridge: connects Tropical_Determinants.thy to the VCL query layer and the Julia experiment implementation. New files: impl/tropical/TropicalDeterminant.jl — Julia mirror of Tropical_Determinants.thy; tropm_det (brute-force n≤8), optimal_assignment, is_within_bound Updated: impl/vcl/Query.jl — ProofOptimalAssignment clause (octad_id, n, bound) impl/vcl/Prover.jl — prove(ProofOptimalAssignment, ...) dispatching to TropicalDeterminant.optimal_assignment; cost matrix decoded from Semantic blob test/test_vcl.jl — 6 new ProofOptimalAssignment tests (1×1, 2×2, asymmetric, not-found, no-semantic, n>8); tropical module includes added examples/krladapter_integration.jl — TropicalDeterminant include added STATE.a2ml (both) — P2 marked complete; completion 85→90% / 50→55% Formal backing: Tropical_Determinants.thy::optimal_assignment_bound tropm_det n A ≤ B ↔ ∃ π. π permutes {..<n} ∧ perm_weightm n A π ≤ B Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ab29f77 commit 15b19cc

7 files changed

Lines changed: 405 additions & 23 deletions

File tree

.machine_readable/6a2/STATE.a2ml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: PMPL-1.0-or-later
22
# STATE.a2ml — Project state checkpoint
33
# Converted from STATE.scm on 2026-03-15
4-
# Updated: 2026-04-11 — bellman_ford sorry CLOSED; all Isabelle sorries gone
4+
# Updated: 2026-04-11 — bellman_ford CLOSED; Tropical_Determinants.thy + ProofOptimalAssignment DONE
55

66
[metadata]
77
project = "nextgen-databases"
@@ -11,24 +11,23 @@ status = "active"
1111

1212
[project-context]
1313
name = "nextgen-databases"
14-
completion-percentage = 50
15-
phase = "Phase 5 — Tropical Consonance + KRL Integration"
14+
completion-percentage = 55
15+
phase = "Phase 5 — Tropical Consonance + KRL Integration (P2 DONE)"
1616

1717
[current-position]
1818
milestone = "verisim-modular-experiment tropical bridge v0.2 + bellman_ford proved"
1919
last-session = "2026-04-11"
20-
last-action = "VQL→VCL rename (303 files); P1 hardening: VCLTypeChecker ArgumentError fixed, null-byte sanitisation added to VCLBridge tokenizer"
20+
last-action = "P2: Tropical_Determinants.thy (sorry-free), TropicalDeterminant.jl, ProofOptimalAssignment VCL clause + 6 tests"
2121

2222
[tropical-bridge]
2323
status = "v0.2 — RI + RII live, RIII stub (KnotTheory.jl r3_simplify is no-op)"
2424
isabelle-sorry-tropical-matrices = "NONE — all closed (bellman_ford CLOSED 2026-04-11)"
25-
isabelle-sorry-other = "cycle_shortcutting CLOSED; floyd_warshall CLOSED; tropm_mat_pow_eq_sum_walks CLOSED"
26-
julia-mirror = "TropicalMatrix.jl + TangleGraph.jl (verisim-modular-experiment/impl/tropical/)"
27-
vcl-clauses = "ProofIntegrity, ProofConsistency, ProofFreshness, ProofConsonance — all live"
25+
isabelle-sorry-other = "cycle_shortcutting CLOSED; floyd_warshall CLOSED; tropm_mat_pow_eq_sum_walks CLOSED; Tropical_Determinants CLOSED 2026-04-11"
26+
julia-mirror = "TropicalMatrix.jl + TangleGraph.jl + TropicalDeterminant.jl (verisim-modular-experiment/impl/tropical/)"
27+
vcl-clauses = "ProofIntegrity, ProofConsistency, ProofFreshness, ProofConsonance, ProofOptimalAssignment — all live"
2828

2929
[pending-tropical]
30-
1 = "Add Tropical_Determinants.thy + ProofOptimalAssignment VCL clause"
31-
2 = "Wire _riii_neighbors when KnotTheory.jl gains live r3_simplify"
30+
1 = "Wire _riii_neighbors when KnotTheory.jl gains live r3_simplify"
3231

3332
[test-coverage]
3433
unit-tests = "~40 (Elixir consensus + federation adapters)"
@@ -52,10 +51,9 @@ gleam-smoke = "Written; requires compiled lith_nif.so to run connection/lifecycl
5251
- "modality_benchmarks.rs uses old API — does not compile (pre-existing)"
5352

5453
[route-to-mvp]
55-
next = "Add Tropical_Determinants.thy + ProofOptimalAssignment VCL clause"
56-
then = "Update integration_test.rs to current API"
54+
next = "Update integration_test.rs to current API"
5755
then = "Update modality_benchmarks.rs to current API"
5856

5957
[critical-next-actions]
60-
1 = "Tropical_Determinants.thy new file — permanent = optimal assignment"
61-
2 = "Update integration_test.rs to current API"
58+
1 = "Update integration_test.rs to current API"
59+
2 = "Update modality_benchmarks.rs to current API"

verisim-modular-experiment/.machine_readable/6a2/STATE.a2ml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ session = "tropical bridge v0.2 — bellman_ford sorry CLOSED"
1313
name = "Verisim Modular Experiment"
1414
purpose = """Research experiment + tropical bridge: ProofConsonance VCL clause,
1515
tropical min-plus matrix power (Bellman-Ford), RI+RII Reidemeister moves via KnotTheory.jl."""
16-
completion-percentage = 85
16+
completion-percentage = 90
1717

1818
[position]
1919
phase = "phase-5-tropical-consonance" # design | implementation | testing | maintenance | archived
@@ -25,15 +25,17 @@ phase-1-core-identification = "complete"
2525
phase-2-federation-contract = "complete"
2626
phase-3-reference-impl = "complete"
2727
phase-4-krladapter-dogfood = "complete"
28-
phase-5-tropical-consonance = "in-progress (RI+RII live; bellman_ford CLOSED; Tropical_Determinants pending)"
28+
phase-5-tropical-consonance = "complete (RI+RII live; bellman_ford CLOSED; Tropical_Determinants.thy + ProofOptimalAssignment DONE 2026-04-11)"
2929

3030
[tropical-status]
31-
TropicalMatrix-jl = "live — min-plus semiring, bellman_ford_matrix"
32-
TangleGraph-jl-v02 = "live — RI (exact) + RII (KnotTheory.jl r2_simplify)"
33-
TangleGraph-jl-rii = "wired — _rii_neighbors calls Main.KnotTheory.r2_simplify"
34-
TangleGraph-jl-riii = "stub — r3_simplify is no-op in KnotTheory.jl v1.0.1"
35-
Prover-jl = "live — prove(ProofConsonance, ...)"
36-
TROPICAL-BRIDGE = "docs/TROPICAL-BRIDGE.adoc updated to v0.2"
31+
TropicalMatrix-jl = "live — min-plus semiring, bellman_ford_matrix"
32+
TangleGraph-jl-v02 = "live — RI (exact) + RII (KnotTheory.jl r2_simplify)"
33+
TangleGraph-jl-rii = "wired — _rii_neighbors calls Main.KnotTheory.r2_simplify"
34+
TangleGraph-jl-riii = "stub — r3_simplify is no-op in KnotTheory.jl v1.0.1"
35+
TropicalDeterminant-jl = "live — tropm_det, optimal_assignment, is_within_bound (brute-force n≤8)"
36+
Prover-jl = "live — prove(ProofConsonance, ...); prove(ProofOptimalAssignment, ...)"
37+
TROPICAL-BRIDGE = "docs/TROPICAL-BRIDGE.adoc updated to v0.2"
38+
Tropical-Determinants-thy = "DONE — perm_weightm, tropm_det, tropm_det_le_perm, tropm_det_1x1, tropm_det_2x2, optimal_assignment, optimal_assignment_bound (0 sorries)"
3739

3840
[blockers-and-issues]
3941
issues = [
@@ -42,7 +44,6 @@ issues = [
4244

4345
[critical-next-actions]
4446
actions = [
45-
"Add Tropical_Determinants.thy + ProofOptimalAssignment VCL clause",
4647
"Wire _riii_neighbors when KnotTheory.jl gains live r3_simplify",
4748
"Promote impl/ to shippable verisim-core package",
4849
"Zig FFI port of VerisimCore per hyperpolymath standard",

verisim-modular-experiment/examples/krladapter_integration.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ include(joinpath(@__DIR__, "..", "impl", "FederationManager.jl"))
2323
# Tropical modules must be loaded before VCLProver (Prover dispatches to them)
2424
include(joinpath(@__DIR__, "..", "impl", "tropical", "TropicalMatrix.jl"))
2525
include(joinpath(@__DIR__, "..", "impl", "tropical", "TangleGraph.jl"))
26+
include(joinpath(@__DIR__, "..", "impl", "tropical", "TropicalDeterminant.jl"))
2627
include(joinpath(@__DIR__, "..", "impl", "vcl", "Query.jl"))
2728
include(joinpath(@__DIR__, "..", "impl", "vcl", "Prover.jl"))
2829

@@ -32,6 +33,7 @@ using .Metrics
3233
using .FederationManager
3334
using .TropicalMatrix
3435
using .TangleGraph
36+
using .TropicalDeterminant
3537
using .VCLQuery
3638
using .VCLProver
3739
# KnotTheory must be loaded as a package so TangleGraph._rii_neighbors
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# SPDX-License-Identifier: PMPL-1.0-or-later
2+
#
3+
# TropicalDeterminant.jl — Julia mirror of Tropical_Determinants.thy
4+
#
5+
# Computes the tropical (min-plus) determinant of an n×n cost matrix:
6+
#
7+
# tropm_det(A) = min over all permutations π of (∑ᵢ A[i, π(i)])
8+
#
9+
# where the inner ∑ is ordinary nat-addition (tropical multiplication)
10+
# and the outer min is tropical addition. This equals the minimum-cost
11+
# perfect matching in the n×n assignment problem.
12+
#
13+
# Formal backing: Tropical_Determinants.thy (Isabelle 2025-1)
14+
# theorem optimal_assignment
15+
# theorem optimal_assignment_bound
16+
#
17+
# Performance: brute-force over all n! permutations — suitable for n ≤ 8.
18+
# For larger n, use the Hungarian algorithm (not yet implemented).
19+
#
20+
# Mirror fidelity:
21+
# perm_weightm(n, A, π) ↔ definition perm_weightm in .thy
22+
# tropm_det(n, A) ↔ definition tropm_det in .thy
23+
# optimal_assignment(n,A) ↔ theorem optimal_assignment in .thy
24+
25+
module TropicalDeterminant
26+
27+
using ..TropicalMatrix: Tropical, TROP_ZERO, TROP_ONE, trop_add, trop_mul
28+
29+
export perm_weightm, tropm_det, optimal_assignment, OptimalAssignmentResult
30+
31+
# ---------------------------------------------------------------------------
32+
# perm_weightm: weight of a single permutation π on cost matrix A
33+
# ---------------------------------------------------------------------------
34+
35+
"""
36+
perm_weightm(n, A, π) -> Tropical
37+
38+
The tropical product (= nat sum) of A[i, π(i)] for i ∈ {0, …, n-1}.
39+
40+
Mirrors: `definition perm_weightm` in Tropical_Determinants.thy
41+
"""
42+
function perm_weightm(n::Int, A::Matrix{Tropical}, π::Vector{Int})::Tropical
43+
weight = TROP_ONE # identity for *, i.e., Fin'(0)
44+
for i in 1:n
45+
weight = trop_mul(weight, A[i, π[i]])
46+
end
47+
return weight
48+
end
49+
50+
# ---------------------------------------------------------------------------
51+
# tropm_det: tropical determinant = minimum-cost perfect matching
52+
# ---------------------------------------------------------------------------
53+
54+
"""
55+
tropm_det(n, A) -> Tropical
56+
57+
Tropical determinant: min over all permutations π of {1,…,n} of perm_weightm(n, A, π).
58+
59+
Brute-force enumeration of all n! permutations. Suitable for n ≤ 8.
60+
61+
Mirrors: `definition tropm_det` in Tropical_Determinants.thy
62+
"""
63+
function tropm_det(n::Int, A::Matrix{Tropical})::Tropical
64+
n == 0 && return TROP_ONE # empty product = 1 (zero rows)
65+
det = TROP_ZERO # identity for +, i.e., PosInf
66+
# Generate all permutations of {1,…,n} (1-indexed to match Julia arrays)
67+
for π in _permutations(n)
68+
w = perm_weightm(n, A, π)
69+
det = trop_add(det, w) # min
70+
end
71+
return det
72+
end
73+
74+
# ---------------------------------------------------------------------------
75+
# optimal_assignment: find a minimising permutation
76+
# ---------------------------------------------------------------------------
77+
78+
"""
79+
OptimalAssignmentResult
80+
81+
Result of `optimal_assignment/2`:
82+
- `permutation`: 1-indexed permutation vector achieving the minimum cost
83+
- `cost`: minimum assignment cost (= tropm_det)
84+
- `is_finite`: false if the matrix has no finite-cost perfect matching
85+
86+
Mirrors: `theorem optimal_assignment` in Tropical_Determinants.thy
87+
"""
88+
struct OptimalAssignmentResult
89+
permutation::Vector{Int}
90+
cost::Tropical
91+
is_finite::Bool
92+
end
93+
94+
"""
95+
optimal_assignment(n, A) -> OptimalAssignmentResult
96+
97+
Find a minimum-cost perfect matching.
98+
99+
Formal guarantee (Tropical_Determinants.thy, theorem `optimal_assignment`):
100+
∃ π. π permutes {..<n} ∧ tropm_det n A = perm_weightm n A π
101+
∧ ∀ π'. π' permutes {..<n} → tropm_det n A ≤ perm_weightm n A π'
102+
"""
103+
function optimal_assignment(n::Int, A::Matrix{Tropical})::OptimalAssignmentResult
104+
if n == 0
105+
return OptimalAssignmentResult(Int[], TROP_ONE, true)
106+
end
107+
best_π = collect(1:n)
108+
best_cost = TROP_ZERO # PosInf — will be replaced on first iteration
109+
for π in _permutations(n)
110+
w = perm_weightm(n, A, π)
111+
# trop_add is min; if w < best_cost, update
112+
new_min = trop_add(best_cost, w)
113+
if new_min != best_cost || best_cost == TROP_ZERO
114+
best_cost = w
115+
best_π = copy(π)
116+
end
117+
end
118+
is_finite = isfinite(best_cost.val)
119+
return OptimalAssignmentResult(best_π, best_cost, is_finite)
120+
end
121+
122+
"""
123+
is_within_bound(n, A, bound) -> Bool
124+
125+
Check whether the optimal assignment cost ≤ bound.
126+
127+
Mirrors: corollary `optimal_assignment_bound` in Tropical_Determinants.thy:
128+
(∃ π. π permutes {..<n} ∧ perm_weightm n A π ≤ B) ↔ tropm_det n A ≤ B
129+
"""
130+
function is_within_bound(n::Int, A::Matrix{Tropical}, bound::Tropical)::Bool
131+
cost = tropm_det(n, A)
132+
# In min-plus: a ≤ b iff trop_add(a, b) = a (min(a,b) = a)
133+
return trop_add(cost, bound) == cost || cost == bound
134+
end
135+
136+
# ---------------------------------------------------------------------------
137+
# Private: permutation generator (Heap's algorithm, 1-indexed)
138+
# ---------------------------------------------------------------------------
139+
140+
function _permutations(n::Int)
141+
perms = Vector{Vector{Int}}()
142+
_heap_perms!(collect(1:n), n, perms)
143+
return perms
144+
end
145+
146+
function _heap_perms!(a::Vector{Int}, k::Int, out::Vector{Vector{Int}})
147+
if k == 1
148+
push!(out, copy(a))
149+
return
150+
end
151+
_heap_perms!(a, k - 1, out)
152+
for i in 1:(k - 1)
153+
if k % 2 == 0
154+
a[i], a[k] = a[k], a[i]
155+
else
156+
a[1], a[k] = a[k], a[1]
157+
end
158+
_heap_perms!(a, k - 1, out)
159+
end
160+
end
161+
162+
end # module TropicalDeterminant

verisim-modular-experiment/impl/vcl/Prover.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,81 @@ function prove(clause::Main.VCLQuery.ProofConsonance, store, manager)
194194
end
195195
end
196196

197+
198+
# -----------------------------------------------------------------------
199+
# PROOF OPTIMAL_ASSIGNMENT — tropical determinant assignment check
200+
# -----------------------------------------------------------------------
201+
202+
function prove(clause::Main.VCLQuery.ProofOptimalAssignment, store, manager)
203+
# Fetch the octad holding the cost matrix
204+
octad = Main.VerisimCore.get_core(store, clause.octad_id)
205+
octad === nothing && return Main.VCLQuery.VerdictFail(
206+
"octad not found in Core store")
207+
208+
# Require Semantic shape (cost matrix is stored as proof_bytes)
209+
octad.semantic === nothing && return Main.VCLQuery.VerdictFail(
210+
"octad has no Semantic shape — cannot extract cost matrix")
211+
212+
n = clause.n
213+
n < 1 && return Main.VCLQuery.VerdictFail(
214+
"matrix dimension must be ≥ 1 (got $(n))")
215+
n > 8 && return Main.VCLQuery.VerdictFail(
216+
"matrix dimension $(n) > 8: brute-force determinant not supported " *
217+
"(Hungarian algorithm not yet wired)")
218+
219+
# Decode n×n Tropical cost matrix from the semantic blob
220+
cost_matrix = _decode_cost_matrix(octad.semantic.proof_bytes, n)
221+
cost_matrix === nothing && return Main.VCLQuery.VerdictFail(
222+
"could not decode $(n)×$(n) Tropical cost matrix from Semantic blob")
223+
224+
# Compute optimal assignment via tropical determinant
225+
#
226+
# Formal guarantee: optimal_assignment_bound theorem (Tropical_Determinants.thy)
227+
# tropm_det n A ≤ B ↔ ∃ π. π permutes {..<n} ∧ perm_weightm n A π ≤ B
228+
TDet = Main.TropicalDeterminant
229+
result = TDet.optimal_assignment(n, cost_matrix)
230+
231+
if !result.is_finite
232+
return Main.VCLQuery.VerdictFail(
233+
"no finite-cost perfect matching exists (det = PosInf)")
234+
end
235+
236+
# Check the bound: trop_add(cost, bound) = cost means cost ≤ bound in min-plus
237+
TM = Main.TropicalMatrix
238+
bound = clause.bound
239+
within = TM.trop_add(result.cost, bound) == result.cost ||
240+
result.cost == bound
241+
242+
if within
243+
perm_str = join(result.permutation, "")
244+
return Main.VCLQuery.VerdictPass(
245+
"optimal assignment cost $(result.cost.val) ≤ bound " *
246+
"[permutation: $(perm_str); " *
247+
"backed by Tropical_Determinants.thy::optimal_assignment_bound]")
248+
else
249+
return Main.VCLQuery.VerdictFail(
250+
"optimal assignment cost $(result.cost.val) exceeds bound " *
251+
"($(bound)) [tropical determinant]")
252+
end
253+
end
254+
255+
# Decode a flat sequence of nat values from proof_bytes into an n×n
256+
# Tropical matrix. Expected encoding: n² little-endian uint32 values;
257+
# 0xFFFFFFFF encodes PosInf (∞).
258+
function _decode_cost_matrix(proof_bytes::AbstractVector{UInt8}, n::Int)
259+
TM = Main.TropicalMatrix
260+
expected = n * n * 4 # n² × 4 bytes
261+
length(proof_bytes) < expected && return nothing
262+
A = Matrix{TM.Tropical}(undef, n, n)
263+
for row in 1:n, col in 1:n
264+
offset = ((row - 1) * n + (col - 1)) * 4 + 1
265+
raw = UInt32(proof_bytes[offset]) |
266+
(UInt32(proof_bytes[offset+1]) << 8) |
267+
(UInt32(proof_bytes[offset+2]) << 16) |
268+
(UInt32(proof_bytes[offset+3]) << 24)
269+
A[row, col] = raw == 0xFFFFFFFF ? TM.TROP_ZERO : TM.Tropical(Float64(raw))
270+
end
271+
return A
272+
end
273+
197274
end # module

verisim-modular-experiment/impl/vcl/Query.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
module VCLQuery
1616

1717
export ProofClause, ProofIntegrity, ProofConsistency, ProofFreshness,
18-
ProofConsonance,
18+
ProofConsonance, ProofOptimalAssignment,
1919
ProofVerdict, VerdictPass, VerdictFail
2020

2121
# -----------------------------------------------------------------------
@@ -86,6 +86,32 @@ struct ProofConsonance <: ProofClause
8686
end
8787
ProofConsonance(a, b) = ProofConsonance(a, b, 2)
8888

89+
"""
90+
PROOF OPTIMAL_ASSIGNMENT FOR <id> WITHIN <bound>
91+
92+
Asks: does there exist a perfect matching in the cost matrix stored in the
93+
octad's Semantic blob with total cost ≤ bound (in the min-plus tropical
94+
semiring)?
95+
96+
Uses the tropical determinant (min over all permutations of assignment
97+
cost products) to decide the optimal assignment problem. The bound is a
98+
`Tropical` value from TropicalMatrix.jl.
99+
100+
Formal backing:
101+
`optimal_assignment_bound` theorem — Tropical_Determinants.thy
102+
See docs/TROPICAL-BRIDGE.adoc for the Isabelle ↔ Julia correspondence.
103+
104+
Fields:
105+
octad_id — OctadId of the octad holding the n×n cost matrix (Semantic blob)
106+
n — matrix dimension (number of agents = number of tasks)
107+
bound — claimed optimal cost ≤ bound (Tropical value)
108+
"""
109+
struct ProofOptimalAssignment <: ProofClause
110+
octad_id::Any # OctadId (duck-typed)
111+
n::Int # matrix dimension
112+
bound::Any # Tropical value from TropicalMatrix.jl
113+
end
114+
89115
# -----------------------------------------------------------------------
90116
# Results
91117
# -----------------------------------------------------------------------

0 commit comments

Comments
 (0)