Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Data.ByteString

Lean: Hale.ByteString.Data.ByteString | Haskell: Data.ByteString

Overview

Strict byte strings with O(1) slicing. The core type is a slice into a ByteArray:

structure ByteString where
  data : ByteArray
  off : Nat
  len : Nat
  valid : off + len ≤ data.size

Key API Mapping

LeanHaskellNotes
ByteString.emptyemptyO(1)
ByteString.singletonsingleton
ByteString.packpackList UInt8 → ByteString
ByteString.unpackunpackByteString → List UInt8
ByteString.taketakeO(1) slice
ByteString.dropdropO(1) slice
ByteString.splitAtsplitAtO(1) slice
ByteString.head hheadTotal with proof h : len > 0
ByteString.tail htailO(1) slice with proof
ByteString.last hlastTotal with proof
ByteString.init hinitO(1) slice with proof
ByteString.index i hindexBounds-checked with proof h : i < len
ByteString.foldlfoldl'Strict left fold
ByteString.foldrfoldrRight fold
ByteString.mapmapO(n)
ByteString.reversereverseO(n)
ByteString.filterfilterO(n)
ByteString.isPrefixOfisPrefixOf
ByteString.isSuffixOfisSuffixOf
ByteString.isInfixOfisInfixOf
ByteString.readFilereadFileWraps IO.FS.readBinFile
ByteString.writeFilewriteFileWraps IO.FS.writeBinFile

Instances

  • BEq ByteString – byte-by-byte comparison
  • Ord ByteString – lexicographic ordering
  • Append ByteString – concatenation (O(m+n))
  • ToString ByteString[w1, w2, ...] format
  • Hashable ByteString
  • Inhabited ByteString – empty

Proofs

  • take_valid / drop_valid – slice operations preserve bounds
  • null_iff_length_zeronull <-> length = 0

Performance

OperationComplexityNotes
take, drop, splitAtO(1)Zero-copy slice
head, last, indexO(1)Direct array access
tail, initO(1)Slice adjustment
cons, snoc, appendO(n)Copies data
map, reverse, filterO(n)
copyO(n)Materialises fresh array