simple functional queue from Okasaki#558
Conversation
b7c4a9c to
a3c0424
Compare
| | pop => | ||
| simp only [Amortized.Op.applyOp, applyOp, potential] | ||
| cases h_front : q.front <;> (rw [Raw.pop, h_front] at ⊢; grind [Raw.rebalance]) | ||
|
|
There was a problem hiding this comment.
Amortized cost is nice, but not immediately usable. To provide a good API for users, can you add a theorem about the total cost of a sequence of m operations on the queue of n elements? This should be at most 2m+n.
There was a problem hiding this comment.
done, although it's probably not the most elegant way :)
| following Okasaki, chapter 5 -/ | ||
| def amortizedCost {α o : Type*} [Op α o] [Potential α] (x : α) (op : o) : ℕ := | ||
| (Op.applyOp x op).time + Potential.potential (Op.applyOp x op).ret - Potential.potential x | ||
|
|
There was a problem hiding this comment.
Some theorems/APIs about amortized cost imply that the total cost would be nice to have.
b22b379 to
5fb8647
Compare
| [h_op : Op α o] [h_pot : Potential α] | ||
| (k : ℕ) (h_bounded : ∀ (x : α) (op : o), amortizedCost x op ≤ k) | ||
| (x : α) (ops : List o) | ||
| : amortizedCostL x ops ≤ k * ops.length |
There was a problem hiding this comment.
Thanks for the edit. I would state the theorem this way:
(applyOps x ops).time <= k * ops.length + Potential.potential x
This formulation explicitly relates the total cost (LHS) to the amortized cost. Users only care about the LHS, and the initial potential can be bounded depending on the application.
There was a problem hiding this comment.
I kept the more accurate bound but otherwise amortizedCostL is gone.
|
|
||
| /-- Physicist method: a potential (lower bound on savings) defined on a | ||
| data structure -/ | ||
| class Potential φ α extends CommRing φ, LinearOrder φ, IsStrictOrderedRing φ where |
There was a problem hiding this comment.
This should almost surely be
| class Potential φ α extends CommRing φ, LinearOrder φ, IsStrictOrderedRing φ where | |
| class Potential (φ α) [CommRing φ] [LinearOrder φ] [IsStrictOrderedRing φ] where |
Hi, this is my first attempt at contributing Lean code.
I dug out the old Okasaki, and started off with the simple queue. This should
contain a correctness proof, a complexity proof, and a mapping to a ghost list
of elements.
TimeMdoesn't seem to be enough for amortized complexity soI added a module for that, and I also removed the
ZeroAddtypeclass constraintbecause
\Ndoesn't implement it(!).AI disclosure: all code is mine, but I had some assistance from GLM 5.1 to
tidy up proof terms and get unstuck.