-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProofs.idr
More file actions
77 lines (65 loc) · 2.87 KB
/
Copy pathProofs.idr
File metadata and controls
77 lines (65 loc) · 2.87 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
-- SPDX-License-Identifier: MPL-2.0
-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
--
||| Machine-checked theorems for the Affinescriptiser ABI.
|||
||| These theorems pin down the correctness of the concrete struct layouts and
||| the FFI result-code encoding. They are checked by the Idris2 type checker:
||| if a layout's field offsets stopped being correctly aligned, or the result
||| encoding changed, this module would fail to compile.
module Affinescriptiser.ABI.Proofs
import Affinescriptiser.ABI.Types
import Affinescriptiser.ABI.Layout
import Data.Vect
%default total
--------------------------------------------------------------------------------
-- Layout compliance
--------------------------------------------------------------------------------
||| The tracked-file-descriptor layout is C-ABI compliant: every field's offset
||| is an exact multiple of that field's alignment.
|||
||| Field offsets: kind=0, linearity=4, ownership=8, padding=12, fd=16, each
||| with alignment 4. The DivideBy witnesses give offset = k * 4 for each:
||| 0=0*4, 4=1*4, 8=2*4, 12=3*4, 16=4*4. These multiplications reduce during
||| typechecking, so the equalities hold by Refl.
export
trackedFDCompliant : CABICompliant Layout.trackedFDLayout
trackedFDCompliant =
CABIOk Layout.trackedFDLayout
(ConsField _ _ (DivideBy 0 Refl)
(ConsField _ _ (DivideBy 1 Refl)
(ConsField _ _ (DivideBy 2 Refl)
(ConsField _ _ (DivideBy 3 Refl)
(ConsField _ _ (DivideBy 4 Refl)
NoFields)))))
--------------------------------------------------------------------------------
-- Result-code encoding
--------------------------------------------------------------------------------
||| The success result encodes to the C integer 0, as required by callers that
||| test the FFI return value against zero.
export
okIsZero : resultToInt Ok = 0
okIsZero = Refl
||| The affine-violation result encodes to 5, matching the dispatch in
||| Foreign.analyse which maps 5 to AffineViolation.
export
affineViolationIsFive : resultToInt AffineViolation = 5
affineViolationIsFive = Refl
||| The resource-leak result encodes to 6, matching Foreign.analyse's mapping
||| of 6 to ResourceLeak.
export
resourceLeakIsSix : resultToInt ResourceLeak = 6
resourceLeakIsSix = Refl
--------------------------------------------------------------------------------
-- Default linearity
--------------------------------------------------------------------------------
||| Mutex locks default to Linear (exactly-once): failing to unlock is a bug, so
||| the affine "at most once" relaxation is not permitted for them.
export
mutexIsLinear : defaultLinearity MutexLock = Linear
mutexIsLinear = Refl
||| File descriptors default to Affine (at most once) — the safe default for
||| acquire/release resources.
export
fdIsAffine : defaultLinearity FileDescriptor = Affine
fdIsAffine = Refl