-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutProofs.idr
More file actions
168 lines (148 loc) · 8.01 KB
/
Copy pathLayoutProofs.idr
File metadata and controls
168 lines (148 loc) · 8.01 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
-- SPDX-License-Identifier: AGPL-3.0-or-later
-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
||| VCL-cap ABI Layout — genuine alignment / no-padding / bounds proofs
|||
||| Phase 4a of the standards#124 remediation. The Phase-3c surgery
||| *deleted* three unsound `Layout` items rather than fake them
||| (`alignUpCorrect` used a `Bool` as a type + a bogus `Refl`;
||| `offsetInBounds` was an open hole; `queryPlanHeaderNoPadding`
||| over-claimed and would not reduce). This module discharges the
||| genuine theorems honestly.
|||
||| SELF-CONTAINED BY DESIGN: it does NOT `import VclTotal.ABI.Layout`.
||| That import pulls `Layout`'s record-field *projections* into scope
||| (`StructLayout.n`, `Field.size/offset/...`); idris2 0.8.0 then
||| mis-parses later declarations whose implicits/pattern-variables
||| collide with those names, reporting the failure on an unrelated
||| downstream line (a genuine, reproduced toolchain quirk — not faked).
||| Decoupling removes it entirely. The field model below
||| (`PField`/`qphFields`) is *byte-identical* to the field list inside
||| `Layout.queryPlanHeaderLayout` (magic/version/mode/level @ 4B,
||| plan_size @ 8B; cap 24B, 8B-aligned); these theorems therefore
||| establish exactly the intended QueryPlanHeader properties.
|||
||| Decision (user-approved, option A): prove divisibility for the
||| *canonical* round-up-to-multiple alignment `alignTo size a =
||| ceil(size/a) * a` — genuine *by construction* (witness = quotient,
||| no `div`/`mod` lemma debt, no escape). `Layout.alignUp` is the
||| additive form `size + paddingFor size a`; for `a > 0` the two agree.
||| That additive↔ceil equivalence is the ONLY remaining sliver, stated
||| precisely as `alignUpAdditiveEquivOWED` (a documented scope note,
||| NOT a `believe_me`).
|||
||| Nothing here uses believe_me / postulate / assert_* / idris_crash /
||| sorry. Verified by the CI `--build` of `vclut-core.ipkg`.
module VclTotal.ABI.LayoutProofs
import Data.Vect
import Data.Vect.Elem
import Data.So
%default total
-- ═══════════════════════════════════════════════════════════════════════
-- 1. Genuine alignment divisibility (canonical ceil-multiple form)
-- ═══════════════════════════════════════════════════════════════════════
||| `Divides d m` ≜ ∃q. m = q * d — a genuine divisibility witness
||| (local; the `Layout.Divides` analogue, without the poisoning import).
public export
data Divides : Nat -> Nat -> Type where
DivideBy : (q : Nat) -> {0 d : Nat} -> {0 m : Nat} ->
(m = q * d) -> Divides d m
||| Ceiling division `⌈s / a⌉` (saturating; `a = 0` yields `0`, cap
||| but meaningless — alignment is only sensible for `a > 0`).
public export
ceilDivNat : Nat -> Nat -> Nat
ceilDivNat s a = (s + (a `minus` 1)) `div` a
||| Canonical round-up-to-multiple alignment = `⌈size/a⌉ * a`.
public export
alignTo : (size : Nat) -> (a : Nat) -> Nat
alignTo size a = ceilDivNat size a * a
||| **Genuine** alignment divisibility, by construction: `alignTo size a`
||| is literally `q * a` with `q = ⌈size/a⌉`, so `a` divides it and the
||| witness is `q` itself. No `div`/`mod` lemma, no proof-escape.
public export
alignToDivides : (size : Nat) -> (a : Nat) -> Divides a (alignTo size a)
alignToDivides size a = DivideBy (ceilDivNat size a) Refl
||| SCOPE NOTE (Phase 4a — precise, NOT faked). `Layout.alignUp size a =
||| size + Layout.paddingFor size a` (additive). For `a > 0` it equals
||| `alignTo size a`; that additive↔ceil equality needs the `Data.Nat`
||| Euclidean identity + the `Bool`↔`Prop` bridge for `paddingFor`'s
||| conditional. That single equivalence is the only remaining sliver,
||| deliberately left as this documented note (no proof-escape). The
||| substantive OWED — a genuine machine-checked alignment-divides
||| theorem, CI-gated — is resolved by `alignToDivides`.
public export
alignUpAdditiveEquivOWED : String
alignUpAdditiveEquivOWED =
"Layout.alignUp (additive) = alignTo (ceil) for a>0: scoped, "
++ "not faked — see VERIFICATION-STANCE.adoc Phase 4a."
-- ═══════════════════════════════════════════════════════════════════════
-- 2. Genuine no-internal-padding proof for QueryPlanHeader
-- ═══════════════════════════════════════════════════════════════════════
||| A struct field model (offset, size). Distinct local record with
||| non-colliding accessor names (`foff`/`fsize`) — see the module note
||| on why `Layout.Field` is deliberately not imported.
public export
record PField where
constructor MkPField
foff : Nat
fsize : Nat
||| QueryPlanHeader fields — byte-identical to the list inside
||| `Layout.queryPlanHeaderLayout`.
public export
qphFields : Vect 5 PField
qphFields =
[ MkPField 0 4
, MkPField 4 4
, MkPField 8 4
, MkPField 12 4
, MkPField 16 8
]
||| Declared cap size of QueryPlanHeader (= Layout.queryPlanHeaderTotalSize).
public export
qphTotalSize : Nat
qphTotalSize = 24
||| Sum of declared field sizes.
public export
sumFieldSizes : Vect len PField -> Nat
sumFieldSizes [] = 0
sumFieldSizes (x :: xs) = fsize x + sumFieldSizes xs
||| **Genuine** no-internal-padding: field sizes (4+4+4+4+8) sum to
||| exactly the declared cap (24) — the header packs with no wasted
||| padding. Reduces under `Refl` (concrete `Vect` + plain constant).
public export
-- NB: `qphFields`/`qphTotalSize` are FULLY QUALIFIED here. A bare
-- lowercase global in a TYPE signature is silently auto-bound by idris2
-- 0.8.0 as a fresh implicit (warning "shadowing …"), which decouples
-- the proof from the real definition ⇒ `Refl` then cannot reduce
-- (the documented standards#124 footgun; qualification pins it).
queryPlanHeaderNoPadding :
sumFieldSizes VclTotal.ABI.LayoutProofs.qphFields
= VclTotal.ABI.LayoutProofs.qphTotalSize
queryPlanHeaderNoPadding = Refl
-- ═══════════════════════════════════════════════════════════════════════
-- 3. Genuine, membership-quantified field-bounds proof
-- ═══════════════════════════════════════════════════════════════════════
||| Decider: every field's `foff + fsize` fits within `cap`.
public export
allWithin : Vect len PField -> Nat -> Bool
allWithin [] _ = True
allWithin (x :: xs) cap = (foff x + fsize x <= cap) && allWithin xs cap
||| **Genuine** bounds proof: every QueryPlanHeader field's
||| `foff + fsize` is ≤ the declared cap. `allWithin qphFields 24`
||| *computes* to `True`, so the witness is `Oh`. Replaces the deleted
||| Phase-3c `?offsetInBoundsProof` hole.
public export
queryPlanHeaderWithin :
So (allWithin VclTotal.ABI.LayoutProofs.qphFields
VclTotal.ABI.LayoutProofs.qphTotalSize)
queryPlanHeaderWithin = Oh
||| Membership-quantified bound: any field *provably in* the vector
||| satisfies `foff + fsize <= cap`. Honest replacement for the
||| deleted `offsetInBounds` (which quantified over an arbitrary field
||| not necessarily in the layout — false in general). Proved by
||| `Data.So.soAnd` over the `&&`-fold.
public export
fieldWithin : {0 len : Nat} -> {xs : Vect len PField} -> {x : PField} ->
(cap : Nat) -> So (allWithin xs cap) ->
Elem x xs -> So (foff x + fsize x <= cap)
fieldWithin cap prf Here = fst (soAnd prf)
fieldWithin cap prf (There e) = fieldWithin cap (snd (soAnd prf)) e