This library is in flux. APIs may change without notice. Generated from source with koruc 0.1.7 on 11/17/2026.
Heap
import std/heapKoru Standard Library: std/heap — binary min-heap over i64 keys.
heap.kz · 5 tors
Koru Standard Library: std/heap — binary min-heap over i64 keys. · 18 more lines
Koru Standard Library: std/heap — binary min-heap over i64 keys.
v1 is the i64 trunk only (the List_i64 precedent): packed keys carry
their own ordering, so `key = cost << K | payload` gives cost-ordered
extract-min for uniform-cost search (Dijkstra/A*) with zero heap-side
knowledge of what the key means. The unified `new(<element>)` /
element-synthesis machinery and the `Heap[i64]` spelling are the
follow-on rung — see concepts/frag-specialization-is-synthesis-not-
generics.md for the ruling this module is the first consumer of.
Ownership states (phantom on *Heap_i64 — identical protocol to
*List_i64):
<heap!> - new issues the obligation: this heap must be freed.
<heap> - borrowed: push/pop/len in place, obligation untouched.
<!heap> - consumed: free discharges the obligation.
Ops are direct `-i64` tors (no transform layer yet): heap.push-i64,
heap.pop-i64, heap.len-i64, heap.free-i64, heap.new-i64.
Phantom lifecycles
Derived from the phantom labels in the declarations below — state! issues an
obligation the compiler will chase, !state discharges it, a bare state holds it without moving it. Nothing here is hand-drawn.
Heap_i64 1 state heap!// Construction
//
// Concrete i64 constructor. Carries the <heap!> ownership obligation.
~pub tor new-i64 {}
| list *Heap_i64<heap!>
| err string// Mutation (borrow — obligation untouched)
//
// Insert a key. Void, chains with `|>`. OOM panics (same loud-on-failure
// split as std/list:push).
~pub tor push-i64 { xs: *Heap_i64<heap>, key: i64 }// Remove and return the minimum key. `| item` / `| empty` branches —
// callers that know the heap is non-empty may bind `item` only.
~pub tor pop-i64 { xs: *Heap_i64<heap> }
| item i64
| empty// Read (borrow)
~pub tor len-i64 { xs: *Heap_i64<heap> } -> i64// Discharge (consume)
~pub tor free-i64 { xs: *Heap_i64<!heap> }