-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUidMapD.lean
396 lines (299 loc) · 8.77 KB
/
UidMapD.lean
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import SafeIdx.Init
/-! # Dependent mapping from uid-s to values of some type
`UidMapD` is parameterized by the size of the mapping, unlike the other mapping type provided by
this library, `Map`, which is just a thin wrapper around `UidMapD`.
-/
namespace SafeIdx
open UidSpec (toNat ofNat)
/-- A dependent map from `Uid` to `α` of length `n`. -/
structure UidMapD
(n : Nat)
(Uid : Type)
[S : UidSpec Uid]
(α : Type u)
where private _mk ::
/-- Underlying array, never accessed directly by users. -/
private raw : Array α
/-- `n` is actually the size of `raw`. -/
private len_inv : raw.size = n
deriving Repr, DecidableEq, Hashable
section
variable
{n : Nat}
{Uid : Type}
[S : UidSpec Uid]
{α : Type u}
(dmap : UidMapD n Uid α)
(uid : Uid)
@[simp]
theorem UidMapD.invariant : dmap.raw.size = n :=
dmap.len_inv
/-- True if `id` is a legal index for `map` (decidable). -/
@[simp]
def UidMapD.isLegal : Prop :=
let _ := dmap -- force `dmap` as a parameter of this def
toNat uid < n
@[simp]
private theorem UidMapD.legalIndex_of_isLegal
{dmap : UidMapD n Uid α} {id : Uid}
:
dmap.isLegal id → toNat id < dmap.raw.size
:= by
simp only [isLegal, invariant]
exact (·)
instance : Decidable (dmap.isLegal uid) := by
simp only [UidMapD.isLegal]
exact inferInstance
/-- Constructor from a legal uid. -/
def FUid.ofDLegal
{dmap : UidMapD n Uid α} {uid : Uid}
(h: dmap.isLegal uid)
: FUid Uid n :=
⟨uid, h⟩
/-- Size/length of a map. -/
def UidMapD.len : Nat :=
let _ := dmap -- force `dmap` as a parameter of this def
n
/-- Size/length of a map. -/
def UidMapD.length : Nat :=
dmap.len
/-- Size/length of a map. -/
def UidMapD.size : Nat :=
dmap.len
/-- Produces a list of all the elements. -/
def UidMapD.toList : List α :=
dmap.raw.toList
/-- Creates an empty map with some capacity. -/
def UidMapD.mkEmpty
{α : outParam $ Type u}
(capacity : Nat := 666)
: UidMapD 0 Uid α :=
⟨Array.mkEmpty capacity, rfl⟩
/-- Pushes an element using a `gen`erator and yields its index. -/
def UidMapD.pushIdx (gen : FUid Uid (n + 1) → α) : FUid Uid (n + 1) × UidMapD (n + 1) Uid α :=
let fuid := FUid.ofNat (n + 1) n
(fuid, ⟨dmap.raw.push (gen fuid), by simp⟩)
/-- Pushes an element using a `gen`erator. -/
def UidMapD.pushIdx' (gen : FUid Uid (n + 1) → α) : UidMapD (n + 1) Uid α :=
dmap.pushIdx gen |>.2
/-- Pushes an element at the end of the map and yields the element's index. -/
def UidMapD.push (a : α) : FUid Uid (n + 1) × UidMapD (n + 1) Uid α :=
dmap.pushIdx fun _ => a
/-- Pushes an element at the end of the map. -/
def UidMapD.push' (a : α) : UidMapD (n + 1) Uid α :=
dmap.push a |>.2
/-- Populates a map of length `n` using a `gen`erator. -/
def UidMapD.generate
(n : Nat)
(gen : FUid Uid n → α)
(capacity : Nat := 666)
: UidMapD n Uid α :=
match n with
| 0 =>
mkEmpty capacity
| n' + 1 =>
let genInc :=
FUid.applyInc gen
generate n' genInc capacity
|>.pushIdx' gen
/-- Generates a map containing `n` elements all equal to `default`. -/
def UidMapD.mkD
(n : Nat)
(default : α)
(capacity : Nat := 666)
: UidMapD n Uid α :=
generate n (fun _ => default) capacity
/-- Generates a map containing `n` elements all equal to `Inhabited.default`. -/
def UidMapD.mkI
(n : Nat)
[Inhabited α]
(capacity : Nat := 666)
: UidMapD n Uid α :=
generate n (fun _ => default) capacity
instance : Inhabited (UidMapD 0 Uid α) where
default :=
UidMapD.mkEmpty
instance [Inhabited α] : Inhabited (UidMapD n Uid α) where
default :=
UidMapD.mkI n
/-- Same as `get` but takes a `FUid` uid. -/
def UidMapD.get (fuid : FUid Uid n) : α :=
let h : toNat fuid.uid < dmap.raw.size :=
by simp [fuid.isLt]
dmap.raw[toNat fuid.uid]
/-- `Uid` array-access notation for `UidMapD`s. -/
instance : GetElem
(UidMapD n Uid α) Uid α
(·.isLegal ·)
where
getElem dmap _uid legal :=
FUid.ofDLegal legal
|> dmap.get
/-- `FUid` array-access notation for `UidMapD`s. -/
instance : GetElem
(UidMapD n Uid α) (FUid Uid n) α $ 𝕂 (𝕂 True)
where
getElem dmap fuid _ :=
dmap.get fuid
/-- `FUid` array-access notation for `UidMapD`s, different-length version. -/
instance : GetElem
(UidMapD n Uid α) (FUid Uid n') α fun _ _ => n' ≤ n
where
getElem dmap fuid h :=
dmap.get fuid.lift
/-- Attempts to retrieve a value in the map. -/
def UidMapD.get? : Option α :=
toNat uid
|> dmap.raw.get?
/-- Retrieves a value at some index, crashes if none. -/
def UidMapD.get! [Inhabited α] : α :=
if h : dmap.isLegal uid
then dmap[uid]
else panic! s!"illegal uid-index `{toNat uid}`, length is `{n}`"
/-- Retrieves a value at some index, yields a default value if none. -/
def UidMapD.getD (default : α) : α :=
dmap.raw.getD (toNat uid) default
/-- Retrieves a value at some index, yields a default value from `Inhabited` if none. -/
def UidMapD.getI [Inhabited α] : α :=
if let some a := dmap.get? uid
then a else default
section set
variable
(fuid : FUid Uid n)
(a : α)
/-- Sets a value in the map. -/
def UidMapD.set : UidMapD n Uid α :=
⟨dmap.raw.set ⟨toNat fuid.uid, legalIndex_of_isLegal fuid.isLt⟩ a, by simp⟩
/-- Attempts to set a value in the map, yields `true` if successful. -/
def UidMapD.set? : Bool × UidMapD n Uid α :=
if h : dmap.isLegal uid
then (true, dmap.set ⟨uid, h⟩ a)
else (false, dmap)
/-- Attempts to set a value in the map, crashes on failure. -/
def UidMapD.set! [Inhabited α] : UidMapD n Uid α :=
if h : dmap.isLegal uid
then dmap.set ⟨uid, h⟩ a
else panic! s!"illegal index {S.toNat uid}, length is {n}"
end set
def UidMapD.mapValueM
{β : Type u}
{M : Type u → Type v} [Monad M]
(uid : FUid Uid n) (f : α → M (α × β))
: M (β × UidMapD n Uid α) := do
let (val, res) ← f $ dmap.get uid
return (res, dmap.set uid val)
section fold
/-- Fold-left with element indices. -/
def UidMapD.foldlIdx
(dmap : UidMapD n Uid α)
(f : β → FUid Uid n → α → β )
(init : β)
: β :=
foldlIdxAux init 0
where
foldlIdxAux (acc : β) (idx : Nat) :=
if h : idx < n then
let fuid : FUid Uid n :=
FUid.mk (ofNat idx) $ S.cancel_to_of ▸ h
let acc := f acc fuid dmap[fuid]
foldlIdxAux acc idx.succ
else
acc
termination_by n - idx
decreasing_by
simp_wf
apply Nat.sub_lt_sub_left
· exact h
· exact Nat.lt_succ_self idx
/-- Fold-left, see also `foldlIdx`. -/
def UidMapD.foldl
(f : β → α → β)
(init : β)
: β :=
dmap.foldlIdx (fun acc => 𝕂 $ f acc) init
/-- Fold-right with element indices. -/
def UidMapD.foldrIdx
(f : FUid Uid n → α → β → β )
(init : β)
: β :=
foldrIdxAux init n
where
foldrIdxAux
(acc : β) (idxSucc : Nat)
(h : idxSucc ≤ n := by simp_arith)
:= match idxSucc with
| 0 => acc
| idx + 1 =>
if h : idx < n then
let fuid : FUid Uid n :=
FUid.mk (ofNat idx) $ S.cancel_to_of ▸ h
let acc := f fuid dmap[fuid] acc
foldrIdxAux acc idx $ Nat.le_of_lt h
else
acc
/-- Fold-right, see also `foldrIdx`. -/
def UidMapD.foldr
(f : α → β → β)
(init : β)
: β :=
dmap.foldrIdx (𝕂 f) init
end fold
/-- Merges the bindings from two maps. -/
def UidMapD.merge
(dmap' : UidMapD n Uid β)
(f : FUid Uid n → α → β → γ)
: UidMapD n Uid γ :=
generate n fun fuid => f fuid dmap[fuid] dmap'[fuid]
/-- Zips two maps. -/
def UidMapD.zip
(dmap' : UidMapD n Uid β)
: UidMapD n Uid (α × β) :=
dmap.merge dmap' $ 𝕂 Prod.mk
section pure
/-- Builds a map of length `n` with all cells storing `a`. -/
def UidMapD.pure
{n : outParam Nat}
(a : α)
: UidMapD n Uid α :=
UidMapD.mkD n a
instance : Pure (UidMapD n Uid) where
pure := UidMapD.pure
end pure
section map
/-- Turns a map to `α`-values into a map to `β`-values. -/
def UidMapD.mapIdx
(f : FUid Uid n → α → β)
(dmap : UidMapD n Uid α)
: UidMapD n Uid β :=
generate n (fun id => f id $ dmap.get id)
/-- Plain map operation, does not give access to indices. -/
def UidMapD.map
(f : α → β)
(dmap : UidMapD n Uid α)
: UidMapD n Uid β :=
dmap.mapIdx (𝕂 f)
/-- `UidMapD` is a functor. -/
instance : Functor (UidMapD n Uid) where
map := UidMapD.map
end map
section applicative
/-- Eager version of monadic `seq`. -/
def UidMapD.seqE
(fnMap : UidMapD n Uid (α → β))
(dmap : UidMapD n Uid α)
: UidMapD n Uid β :=
generate n
(fun uid => (fnMap.get uid) (dmap.get uid))
/-- Lazy version of monadic `seq`. -/
def UidMapD.seq
(fnMap : UidMapD n Uid (α → β))
(dmap : Unit → UidMapD n Uid α)
: UidMapD n Uid β :=
let dmap := dmap ()
generate n
(fun id => (fnMap.get id) (dmap.get id))
/-- `UidMapD` is an applicative. -/
instance : Applicative (UidMapD n Uid) where
seq := UidMapD.seq
end applicative
end