std:Region
lib/Region.omg
lib/Region.omg241 lines
1{- Half-open regions over exact naturals. Allocation policy is ordinary Omegyu
2 code; inspection uses ordinary List folds. The proofs below require neither
3 NatFacts nor a hash-injectivity assumption. A target still has to prove that
4 every physical access uses a location inside its assigned region.
5-}
6let A = std:Arith
7let E = std:Equal
8let L = std:List
9let Region = { start : Natural, size : Natural }
10let end = \(r : Region) -> r.start + r.size
11let Within = \(r : Region) (n : Natural) -> { lower : A.Le r.start n, upper : A.Lt n (end r) }
12let Location = \(r : Region) -> exists (n : Natural) * Within r n
13let Offset = \(size : Natural) -> exists (n : Natural) * A.Lt n size
14let Inside =
15 \(inner outer : Region) ->
16 { lower : A.Le outer.start inner.start, upper : A.Le (end inner) (end outer) }
17let Fits = \(capacity : Natural) (r : Region) -> A.Le (end r) capacity
18let Before = \(a b : Region) -> A.Le (end a) b.start
19let Disjoint = \(a b : Region) -> forall (n : Natural) -> Within a n -> Within b n -> <>
20
21let locate
22 : forall (r : Region) -> Offset r.size -> Location r
23 = \r offset ->
24 (r.start + offset.1,
25 { lower = A.lePlusRight r.start offset.1
26 , upper = A.lePlus r.start r.start (offset.1 + 1) r.size (A.leRefl r.start) offset.2
27 })
28let insideRefl
29 : forall (r : Region) -> Inside r r
30 = \r -> { lower = A.leRefl r.start, upper = A.leRefl (end r) }
31let insideTrans
32 : forall (a b c : Region) -> Inside a b -> Inside b c -> Inside a c
33 = \a b c ab bc ->
34 { lower = A.leTrans c.start b.start a.start bc.lower ab.lower
35 , upper = A.leTrans (end a) (end b) (end c) ab.upper bc.upper
36 }
37let widen
38 : forall (inner outer : Region) -> Inside inner outer -> Location inner -> Location outer
39 = \inner outer containment location ->
40 (location.1,
41 { lower = A.leTrans outer.start inner.start location.1 containment.lower location.2.lower
42 , upper = A.ltLeTrans location.1 (end inner) (end outer) location.2.upper containment.upper
43 })
44let bounded
45 : forall (capacity : Natural) (r : Region)
46 -> Fits capacity r
47 -> forall (location : Location r)
48 -> A.Lt location.1 capacity
49 = \capacity r fits location -> A.ltLeTrans location.1 (end r) capacity location.2.upper fits
50let separate
51 : forall (a b : Region) -> Before a b -> Disjoint a b
52 = \a b before n left right ->
53 A.ltIrrefl
54 n
55 (A.ltLeTrans n (end a) n left.upper (A.leTrans (end a) b.start n before right.lower))
56let disjointSym
57 : forall (a b : Region) -> Disjoint a b -> Disjoint b a
58 = \a b disjoint n right left -> disjoint n left right
59let distinct
60 : forall (a b : Region)
61 -> Disjoint a b
62 -> forall (x : Location a) (y : Location b)
63 -> x.1 === y.1
64 -> <>
65 = \a b disjoint x y equal -> disjoint x.1 x.2 (E.subst (Within b) (E.sym equal) y.2)
66
67-- Splitting a reservation assigns adjacent ranges. A child may itself contain
68-- a composed layout: transitive containment preserves its ownership boundary.
69let split =
70 \(start left right : Natural) ->
71 { left = { start, size = left }, right = { start = start + left, size = right } }
72let splitDisjoint
73 : forall (start left right : Natural)
74 -> Disjoint (split start left right).left (split start left right).right
75 = \start left right ->
76 separate
77 (split start left right).left
78 (split start left right).right
79 (A.leRefl (start + left))
80let splitContained
81 : forall (start left right : Natural)
82 -> Inside (split start left right).left { start, size = left + right }
83 * Inside (split start left right).right { start, size = left + right }
84 = \start left right ->
85 ({ lower = A.leRefl start
86 , upper =
87 A.leTransEq
88 (start + left)
89 (start + left + right)
90 (start + (left + right))
91 (A.lePlusRight (start + left) right)
92 (A.plusAssoc start left right)
93 },
94 { lower = A.lePlusRight start left
95 , upper =
96 A.leOfEq (start + left + right) (start + (left + right)) (A.plusAssoc start left right)
97 })
98-- Reassociation preserves the address of every offset in the third region.
99-- This is a pointwise address theorem, without function extensionality.
100let reassociate
101 : forall (start a b offset : Natural) -> start + a + b + offset === start + (a + b) + offset
102 = \start a b offset -> E.cong (\n -> n + offset) (A.plusAssoc start a b)
103
104let All
105 : (Region -> Type) -> List Region -> Type
106 = \P -> List/elim (\(_ : List Region) -> Type) {} (\r rest proof -> P r * proof)
107let allMap
108 : forall (P Q : Region -> Type)
109 -> (forall (r : Region) -> P r -> Q r)
110 -> forall (rs : List Region)
111 -> All P rs
112 -> All Q rs
113 = \P Q f ->
114 List/elim
115 (\rs -> All P rs -> All Q rs)
116 (\_ -> {=})
117 (\r rest recurse proof -> (f r proof.1, recurse proof.2))
118let Ordered
119 : List Region -> Type
120 = List/elim (\(_ : List Region) -> Type) {} (\r rest proof -> All (Before r) rest * proof)
121let Separated
122 : List Region -> Type
123 = List/elim (\(_ : List Region) -> Type) {} (\r rest proof -> All (Disjoint r) rest * proof)
124let orderedSeparated
125 : forall (rs : List Region) -> Ordered rs -> Separated rs
126 = List/elim
127 (\rs -> Ordered rs -> Separated rs)
128 (\_ -> {=})
129 (\r rest recurse proof ->
130 (allMap (Before r) (Disjoint r) (separate r) rest proof.1, recurse proof.2))
131
132let total = L.fold (\(n rest : Natural) -> n + rest) 0
133let span = \(start : Natural) (sizes : List Natural) -> { start, size = total sizes }
134let pack
135 : Natural -> List Natural -> List Region
136 = \start sizes ->
137 List/elim
138 (\(_ : List Natural) -> Natural -> List Region)
139 (\_ -> ([] : List Region))
140 (\size rest recurse at -> [ { start = at, size } ] # recurse (at + size))
141 sizes
142 start
143let packAfter
144 : forall (sizes : List Natural) (start lower : Natural)
145 -> A.Le lower start
146 -> All (\r -> A.Le lower r.start) (pack start sizes)
147 = List/elim
148 (\sizes ->
149 forall (start lower : Natural)
150 -> A.Le lower start
151 -> All (\r -> A.Le lower r.start) (pack start sizes))
152 (\start lower proof -> {=})
153 (\size rest recurse start lower proof ->
154 (proof,
155 recurse
156 (start + size)
157 lower
158 (A.leTrans lower start (start + size) proof (A.lePlusRight start size))))
159let packOrdered
160 : forall (sizes : List Natural) (start : Natural) -> Ordered (pack start sizes)
161 = List/elim
162 (\sizes -> forall (start : Natural) -> Ordered (pack start sizes))
163 (\_ -> {=})
164 (\size rest recurse start ->
165 (packAfter rest (start + size) (start + size) (A.leRefl (start + size)),
166 recurse (start + size)))
167let packContained
168 : forall (sizes : List Natural) (start : Natural)
169 -> All (\r -> Inside r (span start sizes)) (pack start sizes)
170 = List/elim
171 (\sizes ->
172 forall (start : Natural) -> All (\r -> Inside r (span start sizes)) (pack start sizes))
173 (\_ -> {=})
174 (\size rest recurse start ->
175 let sub = splitContained start size (total rest)
176 in (sub.1,
177 allMap
178 (\r -> Inside r (span (start + size) rest))
179 (\r -> Inside r (span start ([ size ] # rest)))
180 (\r proof ->
181 insideTrans
182 r
183 (span (start + size) rest)
184 (span start ([ size ] # rest))
185 proof
186 sub.2)
187 (pack (start + size) rest)
188 (recurse (start + size))))
189let Plan =
190 \(capacity : Natural) ->
191 exists (rs : List Region) * { separated : Separated rs, fits : All (Fits capacity) rs }
192let derive
193 : forall (capacity start : Natural) (sizes : List Natural)
194 -> Fits capacity (span start sizes)
195 -> Plan capacity
196 = \capacity start sizes fits ->
197 (pack start sizes,
198 { separated = orderedSeparated (pack start sizes) (packOrdered sizes start)
199 , fits =
200 allMap
201 (\r -> Inside r (span start sizes))
202 (Fits capacity)
203 (\r proof -> A.leTrans (end r) (end (span start sizes)) capacity proof.upper fits)
204 (pack start sizes)
205 (packContained sizes start)
206 })
207in { Region
208 , end
209 , Within
210 , Location
211 , Offset
212 , Inside
213 , Fits
214 , Before
215 , Disjoint
216 , locate
217 , insideRefl
218 , insideTrans
219 , widen
220 , bounded
221 , separate
222 , disjointSym
223 , distinct
224 , split
225 , splitDisjoint
226 , splitContained
227 , reassociate
228 , All
229 , allMap
230 , Ordered
231 , Separated
232 , orderedSeparated
233 , total
234 , span
235 , pack
236 , packAfter
237 , packOrdered
238 , packContained
239 , Plan
240 , derive
241 }