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

NonEmpty

Lean: Hale.Base.NonEmpty | Haskell: Data.List.NonEmpty

Overview

Non-empty list with a guaranteed minimum of one element. The length function returns a subtype {n : Nat // n >= 1}, encoding the non-emptiness invariant at the type level.

API Mapping

LeanHaskellKind
NonEmptyNonEmptyType
headheadAccessor
lastlastAccessor
lengthlengthFunction
toListtoListFunction
singletonsingletonConstructor
consconsConstructor
appendappendFunction
mapmapFunction
reversereverseFunction
foldrfoldrFunction
foldr1foldr1Function
foldl1foldl1Function
fromList?nonEmptyFunction
fromListfromListFunction

Instances

  • Append (NonEmpty a)
  • Functor NonEmpty
  • Pure NonEmpty
  • Bind NonEmpty
  • Monad NonEmpty
  • ToString (NonEmpty a) (requires ToString a)
  • BEq (NonEmpty a) (derived)
  • Ord (NonEmpty a) (derived)
  • Repr (NonEmpty a) (derived)
  • Hashable (NonEmpty a) (derived)

Proofs & Guarantees

  • toList_ne_niltoList ne != [] (the list is never empty)
  • reverse_length — reversing preserves length
  • toList_lengthtoList length agrees with NonEmpty.length
  • map_length — mapping preserves length

Example

-- Construct a non-empty list
let ne := NonEmpty.mk 1 [2, 3]
ne.head    -- => 1
ne.last    -- => 3
ne.length  -- => ⟨3, by omega⟩