File size: 6,405 Bytes
f7ccd08 32b4893 | 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 | { SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
Licensing: see LICENSE in the repository root. }
{ Governance Validation Engine: Executable proof obligations.
Implements 5 lemmas as per NraayTensor spec:
Lemma 1 (Shape-Rank): rank(t) = len(shape(t))
Lemma 2 (Length Product): len(t) = ∏ shape_i
Lemma 3 (Canonical Strides): B_layout=1 ⟹ σ_i = ∏_{j>i} s_j
Lemma 4 (Exclusive Ownership): B_own=1 ⟹ refcount=1
Lemma 5 (Generation Safety): all views share same generation until materialize
}
unit governance;
{$MODE FPC}
interface
uses
descriptor, buffer;
type
{ Governance violation type }
TGovernanceViolation = (
GV_NONE,
GV_SHAPE_RANK_MISMATCH, { Lemma 1 violated }
GV_LENGTH_PRODUCT_MISMATCH, { Lemma 2 violated }
GV_CANONICAL_STRIDE_VIOLATION,{ Lemma 3 violated }
GV_EXCLUSIVE_OWNERSHIP_VIOLATION, { Lemma 4 violated }
GV_GENERATION_MISMATCH { Lemma 5 violated }
);
{ Proof obligation }
TLemmaProof = record
lemma_num: uint32;
violation: TGovernanceViolation;
details: string;
end;
{ Lemma 1: Shape-Rank Consistency }
function ProveShapeRank(const desc: TTensorDescriptor): TLemmaProof;
{ Lemma 2: Length is product of shape }
function ProveLengthProduct(const desc: TTensorDescriptor): TLemmaProof;
{ Lemma 3: Canonical strides when B_layout=1 }
function ProveCanonicalStrides(const desc: TTensorDescriptor): TLemmaProof;
{ Lemma 4: B_own=1 implies refcount=1 }
function ProveExclusiveOwnership(
const desc: TTensorDescriptor;
buf: PBuffer
): TLemmaProof;
{ Lemma 5: Generation safety across views }
function ProveGenerationSafety(
const desc: TTensorDescriptor;
buf: PBuffer
): TLemmaProof;
{ Validate all lemmas }
function ValidateAllLemmas(
const desc: TTensorDescriptor;
buf: PBuffer
): boolean;
{ Get human-readable violation description }
function ViolationToString(violation: TGovernanceViolation): string;
{ Check if proof passed }
function IsProofValid(const proof: TLemmaProof): boolean;
implementation
function ProveShapeRank(const desc: TTensorDescriptor): TLemmaProof;
begin
with result do
begin
lemma_num := 1;
if desc.rank > 0 then
begin
violation := GV_NONE;
details := 'Shape rank consistent: ' + IntToStr(desc.rank);
end
else
begin
violation := GV_SHAPE_RANK_MISMATCH;
details := 'Invalid rank: ' + IntToStr(desc.rank);
end;
end;
end;
function ProveLengthProduct(const desc: TTensorDescriptor): TLemmaProof;
var
i: uint32;
prod: uint32;
begin
with result do
begin
lemma_num := 2;
prod := 1;
for i := 0 to desc.rank - 1 do
prod := prod * desc.shape_ptr^[i];
if prod = desc.len then
begin
violation := GV_NONE;
details := 'Length product valid: ' + IntToStr(prod);
end
else
begin
violation := GV_LENGTH_PRODUCT_MISMATCH;
details := 'Expected ' + IntToStr(prod) + ', got ' + IntToStr(desc.len);
end;
end;
end;
function ProveCanonicalStrides(const desc: TTensorDescriptor): TLemmaProof;
var
i, stride: uint32;
begin
with result do
begin
lemma_num := 3;
if desc.is_contiguous = 0 then
begin
violation := GV_NONE;
details := 'B_layout=0, strides not required canonical';
exit;
end;
{ Check if strides match canonical }
stride := 1;
for i := desc.rank - 1 downto 0 do
begin
if desc.strides_ptr^[i] <> stride then
begin
violation := GV_CANONICAL_STRIDE_VIOLATION;
details := 'Stride mismatch at dim ' + IntToStr(i) +
': expected ' + IntToStr(stride) +
', got ' + IntToStr(desc.strides_ptr^[i]);
exit;
end;
stride := stride * desc.shape_ptr^[i];
end;
violation := GV_NONE;
details := 'Canonical strides verified';
end;
end;
function ProveExclusiveOwnership(
const desc: TTensorDescriptor;
buf: PBuffer
): TLemmaProof;
begin
with result do
begin
lemma_num := 4;
if desc.owns_storage = 0 then
begin
violation := GV_NONE;
details := 'B_own=0, no exclusive check required';
exit;
end;
if buf = nil then
begin
violation := GV_EXCLUSIVE_OWNERSHIP_VIOLATION;
details := 'Buffer is nil but B_own=1';
exit;
end;
if buf^.refcount = 1 then
begin
violation := GV_NONE;
details := 'Exclusive ownership verified (refcount=1)';
end
else
begin
violation := GV_EXCLUSIVE_OWNERSHIP_VIOLATION;
details := 'B_own=1 but refcount=' + IntToStr(buf^.refcount);
end;
end;
end;
function ProveGenerationSafety(
const desc: TTensorDescriptor;
buf: PBuffer
): TLemmaProof;
begin
with result do
begin
lemma_num := 5;
if buf = nil then
begin
violation := GV_GENERATION_MISMATCH;
details := 'Buffer is nil, cannot verify generation';
exit;
end;
if desc.generation = buf^.generation then
begin
violation := GV_NONE;
details := 'Generation safety verified: ' + IntToStr(desc.generation);
end
else
begin
violation := GV_GENERATION_MISMATCH;
details := 'Descriptor gen ' + IntToStr(desc.generation) +
' vs buffer gen ' + IntToStr(buf^.generation);
end;
end;
end;
function ValidateAllLemmas(
const desc: TTensorDescriptor;
buf: PBuffer
): boolean;
begin
if IsProofValid(ProveShapeRank(desc)) and
IsProofValid(ProveLengthProduct(desc)) and
IsProofValid(ProveCanonicalStrides(desc)) and
IsProofValid(ProveExclusiveOwnership(desc, buf)) and
IsProofValid(ProveGenerationSafety(desc, buf)) then
result := true
else
result := false;
end;
function ViolationToString(violation: TGovernanceViolation): string;
begin
case violation of
GV_NONE:
result := 'NO_VIOLATION';
GV_SHAPE_RANK_MISMATCH:
result := 'SHAPE_RANK_MISMATCH';
GV_LENGTH_PRODUCT_MISMATCH:
result := 'LENGTH_PRODUCT_MISMATCH';
GV_CANONICAL_STRIDE_VIOLATION:
result := 'CANONICAL_STRIDE_VIOLATION';
GV_EXCLUSIVE_OWNERSHIP_VIOLATION:
result := 'EXCLUSIVE_OWNERSHIP_VIOLATION';
GV_GENERATION_MISMATCH:
result := 'GENERATION_MISMATCH';
else
result := 'UNKNOWN_VIOLATION';
end;
end;
function IsProofValid(const proof: TLemmaProof): boolean;
begin
result := (proof.violation = GV_NONE);
end;
end.
|