diff --git a/.gitignore b/.gitignore index 73f5db0..f539f3d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,9 @@ Thumbs.db /target/ /_build/ /build/ +**/build/ +*.ttc +*.ttm /dist/ /out/ diff --git a/src/interface/abi/Foreign.idr b/src/interface/abi/Betlangiser/ABI/Foreign.idr similarity index 99% rename from src/interface/abi/Foreign.idr rename to src/interface/abi/Betlangiser/ABI/Foreign.idr index e84a552..7ad59a4 100644 --- a/src/interface/abi/Foreign.idr +++ b/src/interface/abi/Betlangiser/ABI/Foreign.idr @@ -18,6 +18,7 @@ module Betlangiser.ABI.Foreign import Betlangiser.ABI.Types import Betlangiser.ABI.Layout +import Data.So %default total diff --git a/src/interface/abi/Layout.idr b/src/interface/abi/Betlangiser/ABI/Layout.idr similarity index 69% rename from src/interface/abi/Layout.idr rename to src/interface/abi/Betlangiser/ABI/Layout.idr index 68fceaa..cc28945 100644 --- a/src/interface/abi/Layout.idr +++ b/src/interface/abi/Betlangiser/ABI/Layout.idr @@ -16,6 +16,8 @@ module Betlangiser.ABI.Layout import Betlangiser.ABI.Types import Data.Vect import Data.So +import Data.Nat +import Decidable.Equality %default total @@ -29,24 +31,39 @@ paddingFor : (offset : Nat) -> (alignment : Nat) -> Nat paddingFor offset alignment = if offset `mod` alignment == 0 then 0 - else alignment - (offset `mod` alignment) + else minus alignment (offset `mod` alignment) ||| Proof that alignment divides aligned size public export data Divides : Nat -> Nat -> Type where DivideBy : (k : Nat) -> {n : Nat} -> {m : Nat} -> (m = k * n) -> Divides n m +||| Sound decision procedure for divisibility. +||| For n = S k, the quotient q = div m (S k) is tested by checking +||| m = q * (S k) via decidable equality on Nat. Division does not reduce +||| during typechecking, so we obtain the witness by an explicit equality test. +public export +decDivides : (n : Nat) -> (m : Nat) -> Maybe (Divides n m) +decDivides Z _ = Nothing +decDivides (S k) m = + let q = div m (S k) in + case decEq m (q * (S k)) of + Yes prf => Just (DivideBy q prf) + No _ => Nothing + ||| Round up to next alignment boundary public export alignUp : (size : Nat) -> (alignment : Nat) -> Nat alignUp size alignment = size + paddingFor size alignment -||| Proof that alignUp produces aligned result +||| Decide whether alignUp produced an aligned result. +||| Soundly returns a divisibility witness when the rounded-up size is +||| genuinely a multiple of the alignment (it always is for align > 0, but +||| we obtain the witness via the decision procedure rather than asserting it). public export -alignUpCorrect : (size : Nat) -> (align : Nat) -> (align > 0) -> Divides align (alignUp size align) -alignUpCorrect size align prf = - DivideBy ((size + paddingFor size align) `div` align) Refl +alignUpCorrect : (size : Nat) -> (align : Nat) -> Maybe (Divides align (alignUp size align)) +alignUpCorrect size align = decDivides align (alignUp size align) -------------------------------------------------------------------------------- -- Struct Field Layout @@ -70,7 +87,8 @@ nextFieldOffset f = alignUp (f.offset + f.size) f.alignment public export record StructLayout where constructor MkStructLayout - fields : Vect n Field + {0 fieldCount : Nat} + fields : Vect fieldCount Field totalSize : Nat alignment : Nat {auto 0 sizeCorrect : So (totalSize >= sum (map (\f => f.size) fields))} @@ -102,7 +120,10 @@ verifyLayout : (fields : Vect n Field) -> (align : Nat) -> Either String StructL verifyLayout fields align = let size = calcStructSize fields align in case decSo (size >= sum (map (\f => f.size) fields)) of - Yes prf => Right (MkStructLayout fields size align) + Yes prf => + case decDivides align size of + Just dvd => Right (MkStructLayout fields size align {sizeCorrect = prf} {aligned = dvd}) + Nothing => Left "Total size not aligned" No _ => Left "Invalid struct size" -------------------------------------------------------------------------------- @@ -135,6 +156,8 @@ distributionLayout = ] 40 -- Total size: 40 bytes 8 -- Alignment: 8 bytes + {sizeCorrect = Oh} + {aligned = DivideBy 5 Refl} -------------------------------------------------------------------------------- -- Sample Buffer Layout @@ -166,6 +189,8 @@ sampleBufferLayout = ] 56 -- Total size: 56 bytes 8 -- Alignment: 8 bytes + {sizeCorrect = Oh} + {aligned = DivideBy 7 Refl} -------------------------------------------------------------------------------- -- Confidence Interval Layout @@ -189,6 +214,8 @@ confidenceIntervalLayout = ] 24 -- Total size: 24 bytes 8 -- Alignment: 8 bytes + {sizeCorrect = Oh} + {aligned = DivideBy 3 Refl} -------------------------------------------------------------------------------- -- Ternary Bool Array Layout @@ -211,6 +238,8 @@ ternaryArrayLayout = ] 16 -- Total size: 16 bytes 8 -- Alignment: 8 bytes + {sizeCorrect = Oh} + {aligned = DivideBy 2 Refl} -------------------------------------------------------------------------------- -- Platform-Specific Layouts @@ -241,26 +270,65 @@ data CABICompliant : StructLayout -> Type where FieldsAligned layout.fields -> CABICompliant layout -||| Check if layout follows C ABI +||| Sound decision procedure: build a FieldsAligned witness for a Vect of +||| fields by checking, for each field, that its alignment divides its offset. +||| Returns Nothing if any field is misaligned. +public export +decFieldsAligned : (fields : Vect n Field) -> Maybe (FieldsAligned fields) +decFieldsAligned [] = Just NoFields +decFieldsAligned (f :: fs) = + case decDivides f.alignment f.offset of + Just dvd => + case decFieldsAligned fs of + Just rest => Just (ConsField f fs dvd rest) + Nothing => Nothing + Nothing => Nothing + +||| Check if layout follows C ABI by deciding field alignment soundly. public export checkCABI : (layout : StructLayout) -> Either String (CABICompliant layout) checkCABI layout = - Right (CABIOk layout ?fieldsAlignedProof) - -||| Proof that distribution layout is valid + case decFieldsAligned layout.fields of + Just prf => Right (CABIOk layout prf) + Nothing => Left "Struct fields are not correctly aligned" + +||| Proof that distribution layout is valid. +||| Each field offset is a literal multiple of its alignment, so the +||| divisibility witnesses are built directly (multiplication reduces during +||| typechecking; division does not). export -distributionLayoutValid : CABICompliant distributionLayout -distributionLayoutValid = CABIOk distributionLayout ?distributionFieldsAligned +distributionLayoutValid : CABICompliant Layout.distributionLayout +distributionLayoutValid = + CABIOk distributionLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 1 Refl) + (ConsField _ _ (DivideBy 1 Refl) + (ConsField _ _ (DivideBy 2 Refl) + (ConsField _ _ (DivideBy 3 Refl) + (ConsField _ _ (DivideBy 8 Refl) + (ConsField _ _ (DivideBy 9 Refl) NoFields))))))) ||| Proof that sample buffer layout is valid export -sampleBufferLayoutValid : CABICompliant sampleBufferLayout -sampleBufferLayoutValid = CABIOk sampleBufferLayout ?sampleBufferFieldsAligned +sampleBufferLayoutValid : CABICompliant Layout.sampleBufferLayout +sampleBufferLayoutValid = + CABIOk sampleBufferLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 1 Refl) + (ConsField _ _ (DivideBy 2 Refl) + (ConsField _ _ (DivideBy 3 Refl) + (ConsField _ _ (DivideBy 4 Refl) + (ConsField _ _ (DivideBy 5 Refl) + (ConsField _ _ (DivideBy 6 Refl) NoFields))))))) ||| Proof that confidence interval layout is valid export -confidenceIntervalLayoutValid : CABICompliant confidenceIntervalLayout -confidenceIntervalLayoutValid = CABIOk confidenceIntervalLayout ?confidenceIntervalFieldsAligned +confidenceIntervalLayoutValid : CABICompliant Layout.confidenceIntervalLayout +confidenceIntervalLayoutValid = + CABIOk confidenceIntervalLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 1 Refl) + (ConsField _ _ (DivideBy 2 Refl) NoFields))) -------------------------------------------------------------------------------- -- Offset Calculation @@ -274,7 +342,13 @@ fieldOffset layout name = Just idx => Just (finToNat idx ** index idx layout.fields) Nothing => Nothing -||| Proof that field offset is within struct bounds +||| Decide whether a field lies within the struct bounds. +||| This is not universally true for arbitrary fields, so it is decided at +||| runtime via `choose` and only yields the witness when it actually holds. public export -offsetInBounds : (layout : StructLayout) -> (f : Field) -> So (f.offset + f.size <= layout.totalSize) -offsetInBounds layout f = ?offsetInBoundsProof +offsetInBounds : (layout : StructLayout) -> (f : Field) -> + Maybe (So (f.offset + f.size <= layout.totalSize)) +offsetInBounds layout f = + case choose (f.offset + f.size <= layout.totalSize) of + Left ok => Just ok + Right _ => Nothing diff --git a/src/interface/abi/Betlangiser/ABI/Proofs.idr b/src/interface/abi/Betlangiser/ABI/Proofs.idr new file mode 100644 index 0000000..e01d95c --- /dev/null +++ b/src/interface/abi/Betlangiser/ABI/Proofs.idr @@ -0,0 +1,110 @@ +-- SPDX-License-Identifier: MPL-2.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) +-- +||| Machine-Checked ABI Theorems for Betlangiser +||| +||| This module collects genuine, machine-checked theorems about the +||| betlangiser ABI: that each concrete C-struct layout is C-ABI compliant +||| (every field offset is a multiple of its alignment), and that the +||| result-code encoding pins the success code to zero. +||| +||| Every proof below reduces by computation alone — no holes, no postulates, +||| no `believe_me`. The divisibility witnesses are built DIRECTLY (each field +||| offset is a literal `k * alignment`, and multiplication reduces during +||| typechecking) rather than via the runtime decision procedure. + +module Betlangiser.ABI.Proofs + +import Betlangiser.ABI.Types +import Betlangiser.ABI.Layout +import Data.Vect + +%default total + +-------------------------------------------------------------------------------- +-- Layout Compliance Theorems +-------------------------------------------------------------------------------- + +||| The Distribution C-struct layout is C-ABI compliant: every field offset +||| is an exact multiple of that field's alignment. +||| Offsets/alignments: tag 0/4, _pad0 4/4, param1 8/8, param2 16/8, +||| custom_ptr 24/8, custom_len 32/4, _pad1 36/4. +export +distributionCompliant : CABICompliant Layout.distributionLayout +distributionCompliant = + CABIOk Layout.distributionLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 1 Refl) + (ConsField _ _ (DivideBy 1 Refl) + (ConsField _ _ (DivideBy 2 Refl) + (ConsField _ _ (DivideBy 3 Refl) + (ConsField _ _ (DivideBy 8 Refl) + (ConsField _ _ (DivideBy 9 Refl) NoFields))))))) + +||| The sample-buffer C-struct layout is C-ABI compliant. +||| All seven fields are 8 bytes at offsets 0,8,16,24,32,40,48 with align 8. +export +sampleBufferCompliant : CABICompliant Layout.sampleBufferLayout +sampleBufferCompliant = + CABIOk Layout.sampleBufferLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 1 Refl) + (ConsField _ _ (DivideBy 2 Refl) + (ConsField _ _ (DivideBy 3 Refl) + (ConsField _ _ (DivideBy 4 Refl) + (ConsField _ _ (DivideBy 5 Refl) + (ConsField _ _ (DivideBy 6 Refl) NoFields))))))) + +||| The confidence-interval C-struct layout is C-ABI compliant. +||| Three 8-byte doubles at offsets 0,8,16 with align 8. +export +confidenceIntervalCompliant : CABICompliant Layout.confidenceIntervalLayout +confidenceIntervalCompliant = + CABIOk Layout.confidenceIntervalLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 1 Refl) + (ConsField _ _ (DivideBy 2 Refl) NoFields))) + +||| The ternary-array C-struct layout is C-ABI compliant. +||| Two 8-byte fields at offsets 0,8 with align 8. +export +ternaryArrayCompliant : CABICompliant Layout.ternaryArrayLayout +ternaryArrayCompliant = + CABIOk Layout.ternaryArrayLayout + (ConsField _ _ (DivideBy 0 Refl) + (ConsField _ _ (DivideBy 1 Refl) NoFields)) + +-------------------------------------------------------------------------------- +-- Result-Code Encoding Theorems +-------------------------------------------------------------------------------- + +||| The success result code encodes to the integer 0, as required by the +||| C-ABI convention (zero means success). +export +okIsZero : resultToInt Ok = 0 +okIsZero = Refl + +||| The result-code encoding is injective on the two codes that matter most +||| at the boundary: success (0) is distinct from the generic error (1). +||| Pinned here so a future re-ordering of the `Result` enum cannot silently +||| collide success with an error. +export +okNotError : Not (resultToInt Ok = resultToInt Error) +okNotError = \case Refl impossible + +-------------------------------------------------------------------------------- +-- Ternary Logic Theorems +-------------------------------------------------------------------------------- + +||| Kleene NOT is self-inverse on the indeterminate value: re-exported as a +||| concrete, fully-applied theorem (no universally-quantified variable) so it +||| witnesses a closed fact about the ABI's Unknown encoding. +export +notUnknownIsUnknown : ternaryNot TUnknown = TUnknown +notUnknownIsUnknown = Refl + +||| Kleene AND is annihilated by False regardless of the other operand being +||| the indeterminate value — the strong-falsity law at the ABI boundary. +export +falseAndUnknownIsFalse : ternaryAnd TFalse TUnknown = TFalse +falseAndUnknownIsFalse = Refl diff --git a/src/interface/abi/Types.idr b/src/interface/abi/Betlangiser/ABI/Types.idr similarity index 77% rename from src/interface/abi/Types.idr rename to src/interface/abi/Betlangiser/ABI/Types.idr index d5977aa..20fb755 100644 --- a/src/interface/abi/Types.idr +++ b/src/interface/abi/Betlangiser/ABI/Types.idr @@ -18,6 +18,7 @@ module Betlangiser.ABI.Types import Data.Bits import Data.So import Data.Vect +import Decidable.Equality %default total @@ -33,10 +34,7 @@ data Platform = Linux | Windows | MacOS | BSD | WASM ||| This will be set during compilation based on target public export thisPlatform : Platform -thisPlatform = - %runElab do - -- Platform detection logic - pure Linux -- Default, override with compiler flags +thisPlatform = Linux -- Default, override with compiler flags -------------------------------------------------------------------------------- -- Result Codes @@ -82,7 +80,48 @@ DecEq Result where decEq NullPointer NullPointer = Yes Refl decEq InvalidDistribution InvalidDistribution = Yes Refl decEq SamplingFailed SamplingFailed = Yes Refl - decEq _ _ = No absurd + decEq Ok Error = No (\case Refl impossible) + decEq Ok InvalidParam = No (\case Refl impossible) + decEq Ok OutOfMemory = No (\case Refl impossible) + decEq Ok NullPointer = No (\case Refl impossible) + decEq Ok InvalidDistribution = No (\case Refl impossible) + decEq Ok SamplingFailed = No (\case Refl impossible) + decEq Error Ok = No (\case Refl impossible) + decEq Error InvalidParam = No (\case Refl impossible) + decEq Error OutOfMemory = No (\case Refl impossible) + decEq Error NullPointer = No (\case Refl impossible) + decEq Error InvalidDistribution = No (\case Refl impossible) + decEq Error SamplingFailed = No (\case Refl impossible) + decEq InvalidParam Ok = No (\case Refl impossible) + decEq InvalidParam Error = No (\case Refl impossible) + decEq InvalidParam OutOfMemory = No (\case Refl impossible) + decEq InvalidParam NullPointer = No (\case Refl impossible) + decEq InvalidParam InvalidDistribution = No (\case Refl impossible) + decEq InvalidParam SamplingFailed = No (\case Refl impossible) + decEq OutOfMemory Ok = No (\case Refl impossible) + decEq OutOfMemory Error = No (\case Refl impossible) + decEq OutOfMemory InvalidParam = No (\case Refl impossible) + decEq OutOfMemory NullPointer = No (\case Refl impossible) + decEq OutOfMemory InvalidDistribution = No (\case Refl impossible) + decEq OutOfMemory SamplingFailed = No (\case Refl impossible) + decEq NullPointer Ok = No (\case Refl impossible) + decEq NullPointer Error = No (\case Refl impossible) + decEq NullPointer InvalidParam = No (\case Refl impossible) + decEq NullPointer OutOfMemory = No (\case Refl impossible) + decEq NullPointer InvalidDistribution = No (\case Refl impossible) + decEq NullPointer SamplingFailed = No (\case Refl impossible) + decEq InvalidDistribution Ok = No (\case Refl impossible) + decEq InvalidDistribution Error = No (\case Refl impossible) + decEq InvalidDistribution InvalidParam = No (\case Refl impossible) + decEq InvalidDistribution OutOfMemory = No (\case Refl impossible) + decEq InvalidDistribution NullPointer = No (\case Refl impossible) + decEq InvalidDistribution SamplingFailed = No (\case Refl impossible) + decEq SamplingFailed Ok = No (\case Refl impossible) + decEq SamplingFailed Error = No (\case Refl impossible) + decEq SamplingFailed InvalidParam = No (\case Refl impossible) + decEq SamplingFailed OutOfMemory = No (\case Refl impossible) + decEq SamplingFailed NullPointer = No (\case Refl impossible) + decEq SamplingFailed InvalidDistribution = No (\case Refl impossible) -------------------------------------------------------------------------------- -- Probability Value (proven [0,1] bounds) @@ -304,8 +343,10 @@ data Handle : Type where ||| Returns Nothing if pointer is null public export createHandle : Bits64 -> Maybe Handle -createHandle 0 = Nothing -createHandle ptr = Just (MkHandle ptr) +createHandle ptr = + case choose (ptr /= 0) of + Left ok => Just (MkHandle ptr {nonNull = ok}) + Right _ => Nothing ||| Extract pointer value from handle public export @@ -320,8 +361,10 @@ data DistributionHandle : Type where ||| Safely create a distribution handle public export createDistHandle : Bits64 -> Maybe DistributionHandle -createDistHandle 0 = Nothing -createDistHandle ptr = Just (MkDistHandle ptr) +createDistHandle ptr = + case choose (ptr /= 0) of + Left ok => Just (MkDistHandle ptr {nonNull = ok}) + Right _ => Nothing ||| Extract pointer from distribution handle public export @@ -359,10 +402,16 @@ ptrSize MacOS = 64 ptrSize BSD = 64 ptrSize WASM = 32 -||| Pointer type for platform +||| Pointer-sized integer type for the platform. +||| 64-bit on all native platforms; 32-bit on WASM. The pointee type is +||| phantom (the C-ABI carries pointers as opaque integers). public export CPtr : Platform -> Type -> Type -CPtr p _ = Bits (ptrSize p) +CPtr Linux _ = Bits64 +CPtr Windows _ = Bits64 +CPtr MacOS _ = Bits64 +CPtr BSD _ = Bits64 +CPtr WASM _ = Bits32 -------------------------------------------------------------------------------- -- Memory Layout Proofs @@ -378,21 +427,20 @@ public export data HasAlignment : Type -> Nat -> Type where AlignProof : {0 t : Type} -> {n : Nat} -> HasAlignment t n -||| Size of C types (platform-specific) +||| Size of C types (platform-specific). +||| Note: `CInt p` and `CSize p` reduce to `Bits32`/`Bits64`, so they are +||| covered by the corresponding concrete clauses below. public export cSizeOf : (p : Platform) -> (t : Type) -> Nat -cSizeOf p (CInt _) = 4 -cSizeOf p (CSize _) = if ptrSize p == 64 then 8 else 4 cSizeOf p Bits32 = 4 cSizeOf p Bits64 = 8 cSizeOf p Double = 8 cSizeOf p _ = ptrSize p `div` 8 -||| Alignment of C types (platform-specific) +||| Alignment of C types (platform-specific). +||| As above, `CInt p`/`CSize p` reduce to concrete `Bits*` types. public export cAlignOf : (p : Platform) -> (t : Type) -> Nat -cAlignOf p (CInt _) = 4 -cAlignOf p (CSize _) = if ptrSize p == 64 then 8 else 4 cAlignOf p Bits32 = 4 cAlignOf p Bits64 = 8 cAlignOf p Double = 8 @@ -406,19 +454,21 @@ cAlignOf p _ = ptrSize p `div` 8 ||| P(E) >= 0 for all events E. public export data NonNegative : Distribution -> Type where - NormalNonNeg : NonNegative (Normal m s) - UniformNonNeg : NonNegative (Uniform l h) - BetaNonNeg : NonNegative (Beta a b) + NormalNonNeg : {0 valid : So (s > 0.0)} -> NonNegative (Normal m s {valid}) + UniformNonNeg : {0 valid : So (l < h)} -> NonNegative (Uniform l h {valid}) + BetaNonNeg : {0 validA : So (a > 0.0)} -> {0 validB : So (b > 0.0)} -> + NonNegative (Beta a b {validA} {validB}) BernoulliNonNeg : NonNegative (Bernoulli p) - CustomNonNeg : NonNegative (Custom n xs) + CustomNonNeg : NonNegative (Custom nm xs) ||| Witness that a distribution satisfies Kolmogorov's second axiom: ||| P(Omega) = 1. public export data Normalised : Distribution -> Type where - NormalNorm : Normalised (Normal m s) - UniformNorm : Normalised (Uniform l h) - BetaNorm : Normalised (Beta a b) + NormalNorm : {0 valid : So (s > 0.0)} -> Normalised (Normal m s {valid}) + UniformNorm : {0 valid : So (l < h)} -> Normalised (Uniform l h {valid}) + BetaNorm : {0 validA : So (a > 0.0)} -> {0 validB : So (b > 0.0)} -> + Normalised (Beta a b {validA} {validB}) BernoulliNorm : Normalised (Bernoulli p) -- Custom distributions must be validated at runtime diff --git a/src/interface/abi/betlangiser-abi.ipkg b/src/interface/abi/betlangiser-abi.ipkg new file mode 100644 index 0000000..df6e494 --- /dev/null +++ b/src/interface/abi/betlangiser-abi.ipkg @@ -0,0 +1,9 @@ +-- SPDX-License-Identifier: MPL-2.0 +package betlangiser-abi + +sourcedir = "." + +modules = Betlangiser.ABI.Types + , Betlangiser.ABI.Layout + , Betlangiser.ABI.Foreign + , Betlangiser.ABI.Proofs