|
| 1 | +-- SPDX-License-Identifier: MPL-2.0 |
| 2 | +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +-- |
| 4 | +||| Machine-checked proofs over the Chapeliser ABI. |
| 5 | +||| |
| 6 | +||| These are not runtime tests — they are propositional statements the Idris2 |
| 7 | +||| type checker must discharge at compile time. If a concrete partition were |
| 8 | +||| incomplete, a gather lost results, a slice descriptor were mis-laid-out, or |
| 9 | +||| the result-code encoding the Zig FFI depends on were wrong, this module |
| 10 | +||| would fail to typecheck and the proof build would go red. |
| 11 | +||| |
| 12 | +||| Design note on what reduces at the type level: Idris2's `Nat` `div`/`mod` |
| 13 | +||| and the `where`-block helpers inside `perItemSlices` do NOT reduce during |
| 14 | +||| conversion checking, so we never route a proof through them. Instead the |
| 15 | +||| concrete partition/gather witnesses are built from EXPLICIT slice vectors, |
| 16 | +||| where `sliceSum`, `allDisjoint`, and `gatherTotal` reduce fully. Record |
| 17 | +||| projections of top-level layout values reduce, so the layout pins are `Refl`. |
| 18 | + |
| 19 | +module Chapeliser.ABI.Proofs |
| 20 | + |
| 21 | +import Chapeliser.ABI.Types |
| 22 | +import Chapeliser.ABI.Layout |
| 23 | +import Data.So |
| 24 | +import Data.Vect |
| 25 | +import Data.Nat |
| 26 | + |
| 27 | +%default total |
| 28 | + |
| 29 | +-------------------------------------------------------------------------------- |
| 30 | +-- Result-code round-trip: the encoding the Zig FFI depends on. |
| 31 | +-------------------------------------------------------------------------------- |
| 32 | + |
| 33 | +||| Ok must encode as the C value 0 — the success sentinel every FFI wrapper |
| 34 | +||| (`init`, `shutdown`, ...) tests against. |
| 35 | +export |
| 36 | +okIsZero : resultToInt Ok = 0 |
| 37 | +okIsZero = Refl |
| 38 | + |
| 39 | +||| NullPointer must encode as the C value 4. |
| 40 | +export |
| 41 | +nullPointerIsFour : resultToInt NullPointer = 4 |
| 42 | +nullPointerIsFour = Refl |
| 43 | + |
| 44 | +||| RetryExhausted must encode as the C value 5. |
| 45 | +export |
| 46 | +retryExhaustedIsFive : resultToInt RetryExhausted = 5 |
| 47 | +retryExhaustedIsFive = Refl |
| 48 | + |
| 49 | +||| CheckpointError must encode as the C value 6 (the highest code). |
| 50 | +export |
| 51 | +checkpointErrorIsSix : resultToInt CheckpointError = 6 |
| 52 | +checkpointErrorIsSix = Refl |
| 53 | + |
| 54 | +-------------------------------------------------------------------------------- |
| 55 | +-- Slice-descriptor layout: the (start, count) pair crossing the FFI boundary. |
| 56 | +-------------------------------------------------------------------------------- |
| 57 | + |
| 58 | +||| The canonical slice descriptor is exactly 16 bytes. |
| 59 | +export |
| 60 | +sliceDescTotalSize : Layout.sliceDescLayout.totalSize = 16 |
| 61 | +sliceDescTotalSize = Refl |
| 62 | + |
| 63 | +||| `start` lives at offset 0. |
| 64 | +export |
| 65 | +sliceDescStartOffset : Layout.sliceDescLayout.startOffset = 0 |
| 66 | +sliceDescStartOffset = Refl |
| 67 | + |
| 68 | +||| `count` lives at offset 8. |
| 69 | +export |
| 70 | +sliceDescCountOffset : Layout.sliceDescLayout.countOffset = 8 |
| 71 | +sliceDescCountOffset = Refl |
| 72 | + |
| 73 | +||| The descriptor is 8-byte aligned. |
| 74 | +export |
| 75 | +sliceDescAlignment : Layout.sliceDescLayout.alignment = 8 |
| 76 | +sliceDescAlignment = Refl |
| 77 | + |
| 78 | +||| The `count` field begins exactly 8 bytes after `start` — i.e. the two |
| 79 | +||| uint64s are adjacent with no padding between them, matching the Rust |
| 80 | +||| `(u64, u64)` representation. |
| 81 | +export |
| 82 | +sliceDescCountFollowsStart : |
| 83 | + Layout.sliceDescLayout.countOffset = Layout.sliceDescLayout.startOffset + 8 |
| 84 | +sliceDescCountFollowsStart = Refl |
| 85 | + |
| 86 | +-------------------------------------------------------------------------------- |
| 87 | +-- Default checkpoint layout pins. |
| 88 | +-------------------------------------------------------------------------------- |
| 89 | + |
| 90 | +||| The default checkpoint tag buffer is 64 bytes. |
| 91 | +export |
| 92 | +checkpointDefaultTagLen : Layout.defaultCheckpointLayout.maxTagLen = 64 |
| 93 | +checkpointDefaultTagLen = Refl |
| 94 | + |
| 95 | +||| The default checkpoint data buffer is exactly 1 MiB. |
| 96 | +export |
| 97 | +checkpointDefaultDataLen : Layout.defaultCheckpointLayout.maxDataLen = 1048576 |
| 98 | +checkpointDefaultDataLen = Refl |
| 99 | + |
| 100 | +-------------------------------------------------------------------------------- |
| 101 | +-- Partition completeness + disjointness on a concrete, explicit partition. |
| 102 | +-------------------------------------------------------------------------------- |
| 103 | + |
| 104 | +||| A concrete partition of 10 items across 2 locales, given by an explicit |
| 105 | +||| slice vector (not via `perItemSlices`, whose `div`/`mod` do not reduce at |
| 106 | +||| the type level). Locale 0 gets items [0,4), locale 1 gets items [4,10). |
| 107 | +export |
| 108 | +tenAcrossTwo : Partition 10 2 |
| 109 | +tenAcrossTwo = MkPartition [MkSlice 0 4, MkSlice 4 6] |
| 110 | + |
| 111 | +||| The slices cover all 10 items with none lost or duplicated: |
| 112 | +||| 4 + 6 = 10. This is partition completeness (invariant #1). |
| 113 | +export |
| 114 | +tenAcrossTwoComplete : PartitionComplete Proofs.tenAcrossTwo |
| 115 | +tenAcrossTwoComplete = IsComplete Refl |
| 116 | + |
| 117 | +||| The two slices do not overlap: [0,4) and [4,10) are disjoint. |
| 118 | +export |
| 119 | +tenAcrossTwoDisjoint : PartitionDisjoint Proofs.tenAcrossTwo |
| 120 | +tenAcrossTwoDisjoint = IsDisjoint Oh |
| 121 | + |
| 122 | +||| Therefore the partition is valid: complete AND disjoint. This is the full |
| 123 | +||| correctness witness the Chapel codegen relies on to place items. |
| 124 | +export |
| 125 | +tenAcrossTwoValid : ValidPartition Proofs.tenAcrossTwo |
| 126 | +tenAcrossTwoValid = MkValid tenAcrossTwoComplete tenAcrossTwoDisjoint |
| 127 | + |
| 128 | +||| A three-locale example to show completeness is not a one-off: |
| 129 | +||| [0,3) + [3,6) + [6,10) = 3 + 3 + 4 = 10. |
| 130 | +export |
| 131 | +tenAcrossThree : Partition 10 3 |
| 132 | +tenAcrossThree = MkPartition [MkSlice 0 3, MkSlice 3 3, MkSlice 6 4] |
| 133 | + |
| 134 | +export |
| 135 | +tenAcrossThreeValid : ValidPartition Proofs.tenAcrossThree |
| 136 | +tenAcrossThreeValid = |
| 137 | + MkValid (IsComplete Refl) (IsDisjoint Oh) |
| 138 | + |
| 139 | +-------------------------------------------------------------------------------- |
| 140 | +-- Gather conservation (invariant #2): no results lost when collecting. |
| 141 | +-------------------------------------------------------------------------------- |
| 142 | + |
| 143 | +||| Gathering per-locale result counts [3, 3, 4] yields exactly 10 outputs — |
| 144 | +||| the sum is conserved, matching the 10-item partition above. |
| 145 | +export |
| 146 | +gatherTenConserved : GatherConservation (MkGatherInput [3, 3, 4]) 10 |
| 147 | +gatherTenConserved = Conserved Refl |
| 148 | + |
| 149 | +||| An empty gather conserves to zero (degenerate base case). |
| 150 | +export |
| 151 | +gatherEmptyConserved : GatherConservation (MkGatherInput []) 0 |
| 152 | +gatherEmptyConserved = Conserved Refl |
| 153 | + |
| 154 | +-------------------------------------------------------------------------------- |
| 155 | +-- Serialisation length-conservation (invariant #3): decoded length = original. |
| 156 | +-------------------------------------------------------------------------------- |
| 157 | + |
| 158 | +||| A 128-byte Bincode payload decodes back to exactly 128 bytes. The proof |
| 159 | +||| obligation `decodedLen = origLen` is real: `RoundTrip Bincode 128 64` has |
| 160 | +||| no inhabitant. |
| 161 | +export |
| 162 | +bincodeRoundTrips : RoundTrip Bincode 128 128 |
| 163 | +bincodeRoundTrips = RoundTripOk Bincode Refl |
| 164 | + |
| 165 | +||| The raw (identity) format conserves a 64-byte payload's length. |
| 166 | +export |
| 167 | +rawRoundTrips : RoundTrip Raw 64 64 |
| 168 | +rawRoundTrips = RoundTripOk Raw Refl |
| 169 | + |
| 170 | +-------------------------------------------------------------------------------- |
| 171 | +-- Retry isolation (invariant #4) + item-buffer isolation. |
| 172 | +-------------------------------------------------------------------------------- |
| 173 | + |
| 174 | +||| Retrying item 3 of a 10-item workload is isolated: 3 < 10, so the retry |
| 175 | +||| writes only to result slot 3 and cannot corrupt other items' results. |
| 176 | +export |
| 177 | +retryItemThreeIsolated : RetryIsolation 3 10 |
| 178 | +retryItemThreeIsolated = Isolated Oh |
| 179 | + |
| 180 | +||| Item 2's buffer (with 16-byte buffers) starts at byte offset 32 in the |
| 181 | +||| contiguous allocation: 2 * 16 = 32. |
| 182 | +export |
| 183 | +itemTwoOffset : itemOffset 2 16 = 32 |
| 184 | +itemTwoOffset = Refl |
| 185 | + |
| 186 | +||| Items 0 and 1 occupy non-overlapping 16-byte buffers. |
| 187 | +export |
| 188 | +buffersZeroOneDisjoint : So (buffersDisjoint 0 1 16 {neq = Oh}) |
| 189 | +buffersZeroOneDisjoint = Oh |
| 190 | + |
| 191 | +||| Total contiguous memory for 4 buffers of 16 bytes each is 64 bytes. |
| 192 | +export |
| 193 | +fourBuffersMemory : totalItemMemory 4 16 = 64 |
| 194 | +fourBuffersMemory = Refl |
0 commit comments