fact
stringlengths
8
2.79k
type
stringclasses
8 values
library
stringclasses
9 values
imports
listlengths
0
14
filename
stringclasses
132 values
symbolic_name
stringlengths
1
71
docstring
stringlengths
18
10.2k
@[simp] forIn_eq [Monad m] (l : AssocList α β) (init : δ) (f : (α × β) → δ → m (ForInStep δ)) : forIn l init f = forIn l.toList init f := by simp only [forIn] induction l generalizing init <;> simp [AssocList.forIn] congr; funext a; split <;> simp [*]
theorem
Batteries.Data
[ "Batteries.Data.List.Basic" ]
Batteries/Data/AssocList.lean
forIn_eq
null
pop? : AssocList α β → Option ((α × β) × AssocList α β) | nil => none | cons a b l => some ((a, b), l)
def
Batteries.Data
[ "Batteries.Data.List.Basic" ]
Batteries/Data/AssocList.lean
pop
Split the list into head and tail, if possible.
@[simp] _root_.List.toAssocList : List (α × β) → AssocList α β | [] => nil | (a,b) :: es => cons a b (toAssocList es) @[simp] theorem _root_.List.toList_toAssocList (l : List (α × β)) : l.toAssocList.toList = l := by induction l <;> simp [*] @[simp] theorem toList_toAssocList (l : AssocList α β) : l.to...
def
Batteries.Data
[ "Batteries.Data.List.Basic" ]
Batteries/Data/AssocList.lean
_root_.List.toAssocList
Converts a list into an `AssocList`. This is the inverse function to `AssocList.toList`.
protected beq [BEq α] [BEq β] : AssocList α β → AssocList α β → Bool | .nil, .nil => true | .cons _ _ _, .nil => false | .nil, .cons _ _ _ => false | .cons a b t, .cons a' b' t' => a == a' && b == b' && AssocList.beq t t'
def
Batteries.Data
[ "Batteries.Data.List.Basic" ]
Batteries/Data/AssocList.lean
beq
Implementation of `==` on `AssocList`.
@[simp] beq_nil₂ [BEq α] [BEq β] : ((.nil : AssocList α β) == .nil) = true := rfl @[simp] theorem beq_nil_cons [BEq α] [BEq β] : ((.nil : AssocList α β) == .cons a b t) = false := rfl @[simp] theorem beq_cons_nil [BEq α] [BEq β] : ((.cons a b t : AssocList α β) == .nil) = false := rfl @[simp] theorem beq_cons₂ [BEq...
theorem
Batteries.Data
[ "Batteries.Data.List.Basic" ]
Batteries/Data/AssocList.lean
beq_nil₂
null
protected beq_eq [BEq α] [BEq β] {l m : AssocList α β} : (l == m) = (l.toList == m.toList) := by simp [(· == ·)] induction l generalizing m <;> cases m <;> simp [*, (· == ·), AssocList.beq, List.beq]
theorem
Batteries.Data
[ "Batteries.Data.List.Basic" ]
Batteries/Data/AssocList.lean
beq_eq
null
getElem_eq_data_getElem (a : ByteArray) (h : i < a.size) : a[i] = a.data[i] := rfl /-! ### uget/uset -/ @[simp] theorem uset_eq_set (a : ByteArray) {i : USize} (h : i.toNat < a.size) (v : UInt8) : a.uset i v h = a.set i.toNat v := rfl /-! ### empty -/ @[simp] theorem data_mkEmpty (cap) : (emptyWithCapacity cap)...
theorem
Batteries.Data
[]
Batteries/Data/ByteArray.lean
getElem_eq_data_getElem
null
get_push_lt (a : ByteArray) (x : UInt8) (i : Nat) (h : i < a.size) : (a.push x)[i]'(size_push .. ▸ Nat.lt_succ_of_lt h) = a[i] := Array.getElem_push_lt .. /-! ### set -/ @[simp] theorem size_set (a : ByteArray) (i : Fin a.size) (v : UInt8) : (a.set i v).size = a.size := Array.size_set .. @[simp] theorem ...
theorem
Batteries.Data
[]
Batteries/Data/ByteArray.lean
get_push_lt
null
get_set_ne (a : ByteArray) (i : Fin a.size) (v : UInt8) (hj : j < a.size) (h : i.val ≠ j) : (a.set i v)[j]'(a.size_set .. ▸ hj) = a[j] := Array.getElem_set_ne (h := h) ..
theorem
Batteries.Data
[]
Batteries/Data/ByteArray.lean
get_set_ne
null
set_set (a : ByteArray) (i : Fin a.size) (v v' : UInt8) : (a.set i v).set i v' = a.set i v' := ByteArray.ext <| Array.set_set .. /-! ### copySlice -/ @[simp] theorem data_copySlice (a i b j len exact) : (copySlice a i b j len exact).data = b.data.extract 0 j ++ a.data.extract i (i + len) ++ b.data.extract...
theorem
Batteries.Data
[]
Batteries/Data/ByteArray.lean
set_set
null
get_append_left {a b : ByteArray} (hlt : i < a.size) (h : i < (a ++ b).size := size_append .. ▸ Nat.lt_of_lt_of_le hlt (Nat.le_add_right ..)) : (a ++ b)[i] = a[i] := by simp [getElem_eq_data_getElem]; exact Array.getElem_append_left hlt
theorem
Batteries.Data
[]
Batteries/Data/ByteArray.lean
get_append_left
null
get_append_right {a b : ByteArray} (hle : a.size ≤ i) (h : i < (a ++ b).size) (h' : i - a.size < b.size := Nat.sub_lt_left_of_lt_add hle (size_append .. ▸ h)) : (a ++ b)[i] = b[i - a.size] := by simp [getElem_eq_data_getElem]; exact Array.getElem_append_right hle /-! ### extract -/
theorem
Batteries.Data
[]
Batteries/Data/ByteArray.lean
get_append_right
null
get_extract_aux {a : ByteArray} {start stop} (h : i < (a.extract start stop).size) : start + i < a.size := by apply Nat.add_lt_of_lt_sub'; apply Nat.lt_of_lt_of_le h rw [size_extract, ← Nat.sub_min_sub_right]; exact Nat.min_le_right .. @[simp] theorem get_extract {a : ByteArray} {start stop} (h : i < (a.extrac...
theorem
Batteries.Data
[]
Batteries/Data/ByteArray.lean
get_extract_aux
null
@[inline] ofFn (f : Fin n → UInt8) : ByteArray := Fin.foldl n (fun acc i => acc.push (f i)) (emptyWithCapacity n) @[simp] theorem ofFn_zero (f : Fin 0 → UInt8) : ofFn f = empty := by simp [ofFn]
def
Batteries.Data
[]
Batteries/Data/ByteArray.lean
ofFn
- `ofFn f` with `f : Fin n → UInt8` returns the byte array whose `i`th element is `f i`. -
ofFn_succ (f : Fin (n+1) → UInt8) : ofFn f = (ofFn fun i => f i.castSucc).push (f (Fin.last n)) := by simp [ofFn, Fin.foldl_succ_last, emptyWithCapacity] @[simp] theorem data_ofFn (f : Fin n → UInt8) : (ofFn f).data = .ofFn f := by induction n with | zero => simp | succ n ih => simp [ofFn_succ, Array.ofFn_...
theorem
Batteries.Data
[]
Batteries/Data/ByteArray.lean
ofFn_succ
null
@[inline] unsafe mapMUnsafe [Monad m] (a : ByteArray) (f : UInt8 → m UInt8) : m ByteArray := loop a 0 a.usize where /-- Inner loop for `mapMUnsafe`. -/ @[specialize] loop (a : ByteArray) (k s : USize) := do if k < a.usize then let x := a.uget k lcProof let y ← f x let a := a.uset k y lcPro...
def
Batteries.Data
[]
Batteries/Data/ByteArray.lean
mapMUnsafe
Unsafe optimized implementation of `mapM`. This function is unsafe because it relies on the implementation limit that the size of an array is always less than `USize.size`.
@[implemented_by mapMUnsafe] mapM [Monad m] (a : ByteArray) (f : UInt8 → m UInt8) : m ByteArray := do let mut r := a for i in [0:r.size] do r := r.set! i (← f r[i]!) return r
def
Batteries.Data
[]
Batteries/Data/ByteArray.lean
mapM
`mapM f a` applies the monadic function `f` to each element of the array.
@[inline] map (a : ByteArray) (f : UInt8 → UInt8) : ByteArray := mapM (m:=Id) a f
def
Batteries.Data
[]
Batteries/Data/ByteArray.lean
map
`map f a` applies the function `f` to each element of the array.
protected isEmpty (s : ByteSlice) := s.start == s.stop
abbrev
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
isEmpty
Test whether a byte slice is empty.
stop_eq_start_add_size (s : ByteSlice) : s.stop = s.start + s.size := by rw [ByteSlice.size, Nat.add_sub_cancel' s.start_le_stop]
theorem
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
stop_eq_start_add_size
null
pop (s : ByteSlice) : ByteSlice := s.slice 0 (s.size - 1)
abbrev
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
pop
Returns the subslice obtained by removing the last element.
popFront (s : ByteSlice) : ByteSlice := s.slice 1 s.size
abbrev
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
popFront
Returns the subslice obtained by removing the first element.
@[inline] foldlM [Monad m] (s : ByteSlice) (f : β → UInt8 → m β) (init : β) : m β := s.toByteArray.foldlM f init s.start s.stop
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
foldlM
Folds a monadic function over a `ByteSubarray` from left to right.
@[inline] foldl (s : ByteSlice) (f : β → UInt8 → β) (init : β) : β := s.foldlM (m:=Id) f init
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
foldl
Folds a function over a `ByteSubarray` from left to right.
@[specialize] protected forIn [Monad m] (s : ByteSlice) (init : β) (f : UInt8 → β → m (ForInStep β)) : m β := loop s.size (Nat.le_refl _) init where /-- Inner loop of the `forIn` implementation for `ByteSlice`. -/ loop (i : Nat) (h : i ≤ s.size) (b : β) : m β := do match i, h with | 0, _ => pure b ...
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
forIn
Implementation of `forIn` for a `ByteSlice`.
@[deprecated ByteSlice (since := "2025-10-04")] ByteSubarray where /-- `O(1)`. Get data array of a `ByteSubarray`. -/ array : ByteArray /-- `O(1)`. Get start index of a `ByteSubarray`. -/ start : Nat /-- `O(1)`. Get stop index of a `ByteSubarray`. -/ stop : Nat /-- Start index is before stop index. -/ s...
structure
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
ByteSubarray
A subarray of a `ByteArray`.
@[deprecated ByteSlice.size (since := "2025-10-04")] protected size (self : ByteSubarray) := self.stop - self.start
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
size
`O(1)`. Get the size of a `ByteSubarray`.
@[deprecated ByteSlice.isEmpty (since := "2025-10-04")] protected isEmpty (self : ByteSubarray) := self.start == self.stop @[deprecated ByteSlice.stop_eq_start_add_size (since := "2025-10-04")]
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
isEmpty
`O(1)`. Test if a `ByteSubarray` is empty.
stop_eq_start_add_size (self : ByteSubarray) : self.stop = self.start + self.size := by rw [ByteSubarray.size, Nat.add_sub_cancel' self.start_le_stop]
theorem
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
stop_eq_start_add_size
null
@[deprecated ByteSlice.toByteArray (since := "2025-10-04")] toByteArray (self : ByteSubarray) : ByteArray := self.array.extract self.start self.stop
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
toByteArray
`O(n)`. Extract a `ByteSubarray` to a `ByteArray`.
@[deprecated ByteSlice.get (since := "2025-10-04"), inline] get (self : ByteSubarray) (i : Fin self.size) : UInt8 := have : self.start + i.1 < self.array.size := by apply Nat.lt_of_lt_of_le _ self.stop_le_array_size rw [stop_eq_start_add_size] apply Nat.add_lt_add_left i.is_lt self.start self.array[self...
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
get
`O(1)`. Get the element at index `i` from the start of a `ByteSubarray`.
@[deprecated ByteSlice.pop (since := "2025-10-04"), inline] pop (self : ByteSubarray) : ByteSubarray := if h : self.start = self.stop then self else {self with stop := self.stop - 1 start_le_stop := Nat.le_pred_of_lt (Nat.lt_of_le_of_ne self.start_le_stop h) stop_le_array_size := Nat.le_trans (N...
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
pop
`O(1)`. Pop the last element of a `ByteSubarray`.
@[deprecated ByteSlice.popFront (since := "2025-10-04"), inline] popFront (self : ByteSubarray) : ByteSubarray := if h : self.start = self.stop then self else {self with start := self.start + 1 start_le_stop := Nat.succ_le_of_lt (Nat.lt_of_le_of_ne self.start_le_stop h) }
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
popFront
`O(1)`. Pop the first element of a `ByteSubarray`.
@[deprecated ByteSlice.foldlM (since := "2025-10-04"), inline] foldlM [Monad m] (self : ByteSubarray) (f : β → UInt8 → m β) (init : β) : m β := self.array.foldlM f init self.start self.stop
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
foldlM
Folds a monadic function over a `ByteSubarray` from left to right.
@[deprecated ByteSlice.foldl (since := "2025-10-04"), inline] foldl (self : ByteSubarray) (f : β → UInt8 → β) (init : β) : β := self.foldlM (m:=Id) f init
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
foldl
Folds a function over a `ByteSubarray` from left to right.
@[deprecated ByteArray.toByteSlice (since := "2025-10-04")] ByteArray.toByteSubarray (array : ByteArray) : Batteries.ByteSubarray where array := array start := 0 stop := array.size start_le_stop := Nat.zero_le _ stop_le_array_size := Nat.le_refl _ set_option linter.deprecated false in
def
Batteries.Data
[ "Std.Data.ByteSlice", "all Std.Data.ByteSlice -- for unfolding `ByteSlice.size`" ]
Batteries/Data/ByteSlice.lean
ByteArray.toByteSubarray
Implementation of `forIn` for a `ByteSubarray`. -/ @[specialize] --@[deprecated ByteSlice.forIn (since := "2025-10-04"), specialize] protected def forIn [Monad m] (self : ByteSubarray) (init : β) (f : UInt8 → β → m (ForInStep β)) : m β := loop self.size (Nat.le_refl _) init where /-- Inner loop of the `forIn` imp...
@[inline] unsafe mapMUnsafe [Monad m] (a : FloatArray) (f : Float → m Float) : m FloatArray := loop a 0 a.usize where /-- Inner loop for `mapMUnsafe`. -/ @[specialize] loop (a : FloatArray) (k s : USize) := do if k < s then let x := a.uget k lcProof let y ← f x let a := a.uset k y lcProof ...
def
Batteries.Data
[]
Batteries/Data/FloatArray.lean
mapMUnsafe
Unsafe optimized implementation of `mapM`. This function is unsafe because it relies on the implementation limit that the size of an array is always less than `USize.size`.
@[implemented_by mapMUnsafe] mapM [Monad m] (a : FloatArray) (f : Float → m Float) : m FloatArray := do let mut r := a for i in [0:r.size] do r := r.set! i (← f r[i]!) return r
def
Batteries.Data
[]
Batteries/Data/FloatArray.lean
mapM
`mapM f a` applies the monadic function `f` to each element of the array.
@[inline] map (a : FloatArray) (f : Float → Float) : FloatArray := mapM (m:=Id) a f
def
Batteries.Data
[]
Batteries/Data/FloatArray.lean
map
`map f a` applies the function `f` to each element of the array.
testBit : Int → Nat → Bool | ofNat m, n => Nat.testBit m n | negSucc m, n => !(Nat.testBit m n)
def
Batteries.Data
[ "Batteries.Data.Nat.Lemmas" ]
Batteries/Data/Int.lean
testBit
`testBit m n` returns whether the `(n+1)` least significant bit is `1` or `0`, using the two's complement convention for negative `m`.
ofBits (f : Fin n → Bool) := if 2 * Nat.ofBits f < 2 ^ n then ofNat (Nat.ofBits f) else subNatNat (Nat.ofBits f) (2 ^ n) @[simp] theorem ofBits_zero (f : Fin 0 → Bool) : ofBits f = 0 := by simp [ofBits] @[simp] theorem testBit_ofBits_lt {f : Fin n → Bool} (h : i < n) : (ofBits f).testBit i = f ⟨i, h...
def
Batteries.Data
[ "Batteries.Data.Nat.Lemmas" ]
Batteries/Data/Int.lean
ofBits
Construct an integer from a sequence of bits using little endian convention. The sign is determined using the two's complement convention: the result is negative if and only if `n > 0` and `f (n-1) = true`.
testBit_ofBits (f : Fin n → Bool) : (ofBits f).testBit i = if h : i < n then f ⟨i, h⟩ else decide (ofBits f < 0) := by split <;> simp_all
theorem
Batteries.Data
[ "Batteries.Data.Nat.Lemmas" ]
Batteries/Data/Int.lean
testBit_ofBits
null
Heap (α : Type u) where /-- An empty forest, which has depth `0`. -/ | nil : Heap α /-- A forest consists of a root `a`, a forest `child` elements greater than `a`, and another forest `sibling`. -/ | node (a : α) (child sibling : Heap α) : Heap α deriving Repr
inductive
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap
A `Heap` is the nodes of the pairing heap. Each node have two pointers: `child` going to the first child of this node, and `sibling` goes to the next sibling of this tree. So it actually encodes a forest where each node has children `node.child`, `node.child.sibling`, `node.child.sibling.sibling`, etc. Each edge in th...
Heap.size : Heap α → Nat | .nil => 0 | .node _ c s => c.size + 1 + s.size
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.size
`O(n)`. The number of elements in the heap.
Heap.singleton (a : α) : Heap α := .node a .nil .nil
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.singleton
A node containing a single element `a`.
Heap.isEmpty : Heap α → Bool | .nil => true | _ => false
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.isEmpty
`O(1)`. Is the heap empty?
@[specialize] Heap.merge (le : α → α → Bool) : Heap α → Heap α → Heap α | .nil, .nil => .nil | .nil, .node a₂ c₂ _ => .node a₂ c₂ .nil | .node a₁ c₁ _, .nil => .node a₁ c₁ .nil | .node a₁ c₁ _, .node a₂ c₂ _ => if le a₁ a₂ then .node a₁ (.node a₂ c₂ c₁) .nil else .node a₂ (.node a₁ c₁ c₂) .nil
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.merge
`O(1)`. Merge two heaps. Ignore siblings.
@[specialize] Heap.combine (le : α → α → Bool) : Heap α → Heap α | h₁@(.node _ _ h₂@(.node _ _ s)) => merge le (merge le h₁ h₂) (s.combine le) | h => h
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.combine
Auxiliary for `Heap.deleteMin`: merge the forest in pairs.
@[inline] Heap.headD (a : α) : Heap α → α | .nil => a | .node a _ _ => a
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.headD
`O(1)`. Get the smallest element in the heap, including the passed in value `a`.
@[inline] Heap.head? : Heap α → Option α | .nil => none | .node a _ _ => some a
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.head
`O(1)`. Get the smallest element in the heap, if it has an element.
@[inline] Heap.deleteMin (le : α → α → Bool) : Heap α → Option (α × Heap α) | .nil => none | .node a c _ => (a, combine le c)
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.deleteMin
Amortized `O(log n)`. Find and remove the the minimum element from the heap.
@[inline] Heap.tail? (le : α → α → Bool) (h : Heap α) : Option (Heap α) := deleteMin le h |>.map (·.snd)
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.tail
Amortized `O(log n)`. Get the tail of the pairing heap after removing the minimum element.
@[inline] Heap.tail (le : α → α → Bool) (h : Heap α) : Heap α := tail? le h |>.getD .nil
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.tail
Amortized `O(log n)`. Remove the minimum element of the heap.
Heap.NoSibling : Heap α → Prop /-- An empty heap is no more than one tree. -/ | nil : NoSibling .nil /-- Or there is exactly one tree. -/ | node (a c) : NoSibling (.node a c .nil)
inductive
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.NoSibling
A predicate says there is no more than one tree.
Heap.noSibling_merge (le) (s₁ s₂ : Heap α) : (s₁.merge le s₂).NoSibling := by unfold merge (split <;> try split) <;> constructor
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.noSibling_merge
null
Heap.noSibling_combine (le) (s : Heap α) : (s.combine le).NoSibling := by unfold combine; split · exact noSibling_merge _ _ _ · match s with | nil | node _ _ nil => constructor | node _ _ (node _ _ s) => rename_i h; exact (h _ _ _ _ _ rfl).elim
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.noSibling_combine
null
Heap.noSibling_deleteMin {s : Heap α} (eq : s.deleteMin le = some (a, s')) : s'.NoSibling := by cases s with cases eq | node a c => exact noSibling_combine _ _
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.noSibling_deleteMin
null
Heap.noSibling_tail? {s : Heap α} : s.tail? le = some s' → s'.NoSibling := by simp only [Heap.tail?]; intro eq match eq₂ : s.deleteMin le, eq with | some (a, tl), rfl => exact noSibling_deleteMin eq₂
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.noSibling_tail
null
Heap.noSibling_tail (le) (s : Heap α) : (s.tail le).NoSibling := by simp only [Heap.tail] match eq : s.tail? le with | none => cases s with cases eq | nil => constructor | some tl => exact Heap.noSibling_tail? eq
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.noSibling_tail
null
Heap.size_merge_node (le) (a₁ : α) (c₁ s₁ : Heap α) (a₂ : α) (c₂ s₂ : Heap α) : (merge le (.node a₁ c₁ s₁) (.node a₂ c₂ s₂)).size = c₁.size + c₂.size + 2 := by unfold merge; dsimp; split <;> simp +arith [size]
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.size_merge_node
null
Heap.size_merge (le) {s₁ s₂ : Heap α} (h₁ : s₁.NoSibling) (h₂ : s₂.NoSibling) : (merge le s₁ s₂).size = s₁.size + s₂.size := by match h₁, h₂ with | .nil, .nil | .nil, .node _ _ | .node _ _, .nil => simp [merge, size] | .node _ _, .node _ _ => unfold merge; dsimp; split <;> simp +arith [size]
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.size_merge
null
Heap.size_combine (le) (s : Heap α) : (s.combine le).size = s.size := by unfold combine; split · rename_i a₁ c₁ a₂ c₂ s rw [size_merge le (noSibling_merge _ _ _) (noSibling_combine _ _), size_merge_node, size_combine le s] simp +arith [size] · rfl
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.size_combine
null
Heap.size_deleteMin {s : Heap α} (h : s.NoSibling) (eq : s.deleteMin le = some (a, s')) : s.size = s'.size + 1 := by cases h with cases eq | node a c => rw [size_combine, size, size]
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.size_deleteMin
null
Heap.size_tail? {s : Heap α} (h : s.NoSibling) : s.tail? le = some s' → s.size = s'.size + 1 := by simp only [Heap.tail?]; intro eq match eq₂ : s.deleteMin le, eq with | some (a, tl), rfl => exact size_deleteMin h eq₂
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.size_tail
null
Heap.size_tail (le) {s : Heap α} (h : s.NoSibling) : (s.tail le).size = s.size - 1 := by simp only [Heap.tail] match eq : s.tail? le with | none => cases s with cases eq | nil => rfl | some tl => simp [Heap.size_tail? h eq]
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.size_tail
null
Heap.size_deleteMin_lt {s : Heap α} (eq : s.deleteMin le = some (a, s')) : s'.size < s.size := by cases s with cases eq | node a c => simp +arith [size_combine, size]
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.size_deleteMin_lt
null
Heap.size_tail?_lt {s : Heap α} : s.tail? le = some s' → s'.size < s.size := by simp only [Heap.tail?]; intro eq match eq₂ : s.deleteMin le, eq with | some (a, tl), rfl => exact size_deleteMin_lt eq₂
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.size_tail
null
@[specialize] Heap.foldM [Monad m] (le : α → α → Bool) (s : Heap α) (init : β) (f : β → α → m β) : m β := match eq : s.deleteMin le with | none => pure init | some (hd, tl) => have : tl.size < s.size := by simp +arith [Heap.size_deleteMin_lt eq] do foldM le tl (← f init hd) f termination_by s.size
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.foldM
`O(n log n)`. Monadic fold over the elements of a heap in increasing order, by repeatedly pulling the minimum element out of the heap.
@[inline] Heap.fold (le : α → α → Bool) (s : Heap α) (init : β) (f : β → α → β) : β := Id.run <| s.foldM le init f
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.fold
`O(n log n)`. Fold over the elements of a heap in increasing order, by repeatedly pulling the minimum element out of the heap.
@[inline] Heap.toArray (le : α → α → Bool) (s : Heap α) : Array α := fold le s #[] Array.push
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.toArray
`O(n log n)`. Convert the heap to an array in increasing order.
@[inline] Heap.toList (le : α → α → Bool) (s : Heap α) : List α := (s.toArray le).toList
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.toList
`O(n log n)`. Convert the heap to a list in increasing order.
@[specialize] Heap.foldTreeM [Monad m] (nil : β) (join : α → β → β → m β) : Heap α → m β | .nil => pure nil | .node a c s => do join a (← c.foldTreeM nil join) (← s.foldTreeM nil join)
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.foldTreeM
`O(n)`. Fold a monadic function over the tree structure to accumulate a value.
@[inline] Heap.foldTree (nil : β) (join : α → β → β → β) (s : Heap α) : β := Id.run <| s.foldTreeM nil join
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.foldTree
`O(n)`. Fold a function over the tree structure to accumulate a value.
Heap.toListUnordered (s : Heap α) : List α := s.foldTree id (fun a c s l => a :: c (s l)) []
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.toListUnordered
`O(n)`. Convert the heap to a list in arbitrary order.
Heap.toArrayUnordered (s : Heap α) : Array α := s.foldTree id (fun a c s r => s (c (r.push a))) #[]
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.toArrayUnordered
`O(n)`. Convert the heap to an array in arbitrary order.
Heap.NodeWF (le : α → α → Bool) (a : α) : Heap α → Prop | .nil => True | .node b c s => (∀ [TotalBLE le], le a b) ∧ c.NodeWF le b ∧ s.NodeWF le a
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.NodeWF
The well formedness predicate for a heap node. It asserts that: * If `a` is added at the top to make the forest into a tree, the resulting tree is a `le`-min-heap (if `le` is well-behaved)
Heap.WF (le : α → α → Bool) : Heap α → Prop /-- It is an empty heap. -/ | nil : WF le .nil /-- There is exactly one tree and it is a `le`-min-heap. -/ | node (h : c.NodeWF le a) : WF le (.node a c .nil)
inductive
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.WF
The well formedness predicate for a pairing heap. It asserts that: * There is no more than one tree. * It is a `le`-min-heap (if `le` is well-behaved)
Heap.WF.singleton : (Heap.singleton a).WF le := node trivial
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.WF.singleton
null
Heap.WF.merge_node (h₁ : NodeWF le a₁ c₁) (h₂ : NodeWF le a₂ c₂) : (merge le (.node a₁ c₁ s₁) (.node a₂ c₂ s₂)).WF le := by unfold merge; dsimp split <;> rename_i h · exact node ⟨fun [_] => h, h₂, h₁⟩ · exact node ⟨fun [_] => TotalBLE.total.resolve_left h, h₁, h₂⟩
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.WF.merge_node
null
Heap.WF.merge (h₁ : s₁.WF le) (h₂ : s₂.WF le) : (merge le s₁ s₂).WF le := match h₁, h₂ with | .nil, .nil => nil | .nil, .node h₂ => node h₂ | .node h₁, .nil => node h₁ | .node h₁, .node h₂ => merge_node h₁ h₂
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.WF.merge
null
Heap.WF.combine (h : s.NodeWF le a) : (combine le s).WF le := match s with | .nil => nil | .node _b _c .nil => node h.2.1 | .node _b₁ _c₁ (.node _b₂ _c₂ _s) => merge (merge_node h.2.1 h.2.2.2.1) (combine h.2.2.2.2)
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.WF.combine
null
Heap.WF.deleteMin {s : Heap α} (h : s.WF le) (eq : s.deleteMin le = some (a, s')) : s'.WF le := by cases h with cases eq | node h => exact Heap.WF.combine h
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.WF.deleteMin
null
Heap.WF.tail? (hwf : (s : Heap α).WF le) : s.tail? le = some tl → tl.WF le := by simp only [Heap.tail?]; intro eq match eq₂ : s.deleteMin le, eq with | some (a, tl), rfl => exact hwf.deleteMin eq₂
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.WF.tail
null
Heap.WF.tail (hwf : (s : Heap α).WF le) : (s.tail le).WF le := by simp only [Heap.tail] match eq : s.tail? le with | none => exact Heap.WF.nil | some tl => exact hwf.tail? eq
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.WF.tail
null
Heap.deleteMin_fst : ((s : Heap α).deleteMin le).map (·.1) = s.head? := match s with | .nil => rfl | .node _ _ _ => rfl
theorem
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
Heap.deleteMin_fst
null
PairingHeap (α : Type u) (le : α → α → Bool) := { h : Heap α // h.WF le }
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
PairingHeap
A [pairing heap](https://en.wikipedia.org/wiki/Pairing_heap) is a data structure which supports the following primary operations: * `insert : α → PairingHeap α → PairingHeap α`: add an element to the heap * `deleteMin : PairingHeap α → Option (α × PairingHeap α)`: remove the minimum element from the heap * `merge : ...
@[inline] mkPairingHeap (α : Type u) (le : α → α → Bool) : PairingHeap α le := ⟨.nil, Heap.WF.nil⟩
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
mkPairingHeap
`O(1)`. Make a new empty pairing heap.
@[inline] empty : PairingHeap α le := mkPairingHeap α le
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
empty
`O(1)`. Make a new empty pairing heap.
@[inline] isEmpty (b : PairingHeap α le) : Bool := b.1.isEmpty
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
isEmpty
`O(1)`. Is the heap empty?
@[inline] size (b : PairingHeap α le) : Nat := b.1.size
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
size
`O(n)`. The number of elements in the heap.
@[inline] singleton (a : α) : PairingHeap α le := ⟨Heap.singleton a, Heap.WF.singleton⟩
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
singleton
`O(1)`. Make a new heap containing `a`.
@[inline] merge : PairingHeap α le → PairingHeap α le → PairingHeap α le | ⟨b₁, h₁⟩, ⟨b₂, h₂⟩ => ⟨b₁.merge le b₂, h₁.merge h₂⟩
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
merge
`O(1)`. Merge the contents of two heaps.
@[inline] insert (a : α) (h : PairingHeap α le) : PairingHeap α le := merge (singleton a) h
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
insert
`O(1)`. Add element `a` to the given heap `h`.
ofList (le : α → α → Bool) (as : List α) : PairingHeap α le := as.foldl (flip insert) empty
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
ofList
`O(n log n)`. Construct a heap from a list by inserting all the elements.
ofArray (le : α → α → Bool) (as : Array α) : PairingHeap α le := as.foldl (flip insert) empty
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
ofArray
`O(n log n)`. Construct a heap from a list by inserting all the elements.
@[inline] deleteMin (b : PairingHeap α le) : Option (α × PairingHeap α le) := match eq : b.1.deleteMin le with | none => none | some (a, tl) => some (a, ⟨tl, b.2.deleteMin eq⟩)
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
deleteMin
Amortized `O(log n)`. Remove and return the minimum element from the heap.
@[inline] head? (b : PairingHeap α le) : Option α := b.1.head?
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
head
`O(1)`. Returns the smallest element in the heap, or `none` if the heap is empty.
@[inline] head! [Inhabited α] (b : PairingHeap α le) : α := b.head?.get!
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
head
`O(1)`. Returns the smallest element in the heap, or panics if the heap is empty.
@[inline] headI [Inhabited α] (b : PairingHeap α le) : α := b.head?.getD default
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
headI
`O(1)`. Returns the smallest element in the heap, or `default` if the heap is empty.
@[inline] tail? (b : PairingHeap α le) : Option (PairingHeap α le) := match eq : b.1.tail? le with | none => none | some tl => some ⟨tl, b.2.tail? eq⟩
def
Batteries.Data
[ "Batteries.Classes.Order" ]
Batteries/Data/PairingHeap.lean
tail
Amortized `O(log n)`. Removes the smallest element from the heap, or `none` if the heap is empty.