The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V33. Data structures and computation

Mathlib.Data.List.Defs

59 declarations. Mathlib docs · Source

theorem List.Forall.match_1.congr_eq_2 E1 #

∀ {α : Type u_1} (motive : List α → Sort u_2) (x : List α) (h_1 : Unit → motive []) (h_2 : (x : α) → motive [x])
  (h_3 : (x : α) → (l : List α) → motive (x :: l)) (x_1 : α),
  x = [x_1] →
    (match x with
      | [] => h_1 ()
      | [x] => h_2 x
      | x :: l => h_3 x l) ≍
      h_2 x_1

theorem List.map₂Left'.match_1.congr_eq_3 E1 #

∀ {α : Type u_1} {β : Type u_2} (motive : List α → List β → Sort u_3) (x : List α) (x_1 : List β)
  (h_1 : (bs : List β) → motive [] bs) (h_2 : (a : α) → (as : List α) → motive (a :: as) [])
  (h_3 : (a : α) → (as : List α) → (b : β) → (bs : List β) → motive (a :: as) (b :: bs)) (a : α) (as : List α) (b : β)
  (bs : List β),
  x = a :: as →
    x_1 = b :: bs →
      (match x, x_1 with
        | [], bs => h_1 bs
        | a :: as, [] => h_2 a as
        | a :: as, b :: bs => h_3 a as b bs) ≍
        h_3 a as b bs

theorem List.headI.match_1.congr_eq_2 E1 #

∀ {α : Type u_1} (motive : List α → Sort u_2) (x : List α) (h_1 : Unit → motive [])
  (h_2 : (a : α) → (tail : List α) → motive (a :: tail)) (a : α) (tail : List α),
  x = a :: tail →
    (match x with
      | [] => h_1 ()
      | a :: tail => h_2 a tail) ≍
      h_2 a tail

theorem List.map₂Left'.match_1.congr_eq_2 E1 #

∀ {α : Type u_1} {β : Type u_2} (motive : List α → List β → Sort u_3) (x : List α) (x_1 : List β)
  (h_1 : (bs : List β) → motive [] bs) (h_2 : (a : α) → (as : List α) → motive (a :: as) [])
  (h_3 : (a : α) → (as : List α) → (b : β) → (bs : List β) → motive (a :: as) (b :: bs)) (a : α) (as : List α),
  x = a :: as →
    x_1 = [] →
      (match x, x_1 with
        | [], bs => h_1 bs
        | a :: as, [] => h_2 a as
        | a :: as, b :: bs => h_3 a as b bs) ≍
        h_2 a as

theorem List.iterate.match_1.congr_eq_2 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ) (n : ℕ),
  x = n.succ →
    (match x with
      | 0 => h_1 ()
      | n.succ => h_2 n) ≍
      h_2 n

theorem List.iterate.match_1.congr_eq_1 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ),
  x = 0 →
    (match x with
      | 0 => h_1 ()
      | n.succ => h_2 n) ≍
      h_1 ()

theorem List.Forall.match_1.congr_eq_3 E1 #

∀ {α : Type u_1} (motive : List α → Sort u_2) (x : List α) (h_1 : Unit → motive []) (h_2 : (x : α) → motive [x])
  (h_3 : (x : α) → (l : List α) → motive (x :: l)) (x_1 : α) (l : List α),
  x = x_1 :: l →
    (l = [] → False) →
      (match x with
        | [] => h_1 ()
        | [x] => h_2 x
        | x :: l => h_3 x l) ≍
        h_3 x_1 l

theorem List.map₂Left'.match_1.congr_eq_1 E1 #

∀ {α : Type u_1} {β : Type u_2} (motive : List α → List β → Sort u_3) (x : List α) (x_1 : List β)
  (h_1 : (bs : List β) → motive [] bs) (h_2 : (a : α) → (as : List α) → motive (a :: as) [])
  (h_3 : (a : α) → (as : List α) → (b : β) → (bs : List β) → motive (a :: as) (b :: bs)) (bs : List β),
  x = [] →
    x_1 = bs →
      (match x, x_1 with
        | [], bs => h_1 bs
        | a :: as, [] => h_2 a as
        | a :: as, b :: bs => h_3 a as b bs) ≍
        h_1 bs

theorem List.Forall.match_1.congr_eq_1 E1 #

∀ {α : Type u_1} (motive : List α → Sort u_2) (x : List α) (h_1 : Unit → motive []) (h_2 : (x : α) → motive [x])
  (h_3 : (x : α) → (l : List α) → motive (x :: l)),
  x = [] →
    (match x with
      | [] => h_1 ()
      | [x] => h_2 x
      | x :: l => h_3 x l) ≍
      h_1 ()

theorem List.headI.match_1.congr_eq_1 E1 #

∀ {α : Type u_1} (motive : List α → Sort u_2) (x : List α) (h_1 : Unit → motive [])
  (h_2 : (a : α) → (tail : List α) → motive (a :: tail)),
  x = [] →
    (match x with
      | [] => h_1 ()
      | a :: tail => h_2 a tail) ≍
      h_1 ()

instance List.instSDiffOfDecidableEq_mathlib E0 #

{α : Type u_1} → [DecidableEq α] → SDiff (List α)

def List.getI E1 #

{α : Type u_1} → [Inhabited α] → List α → ℕ → α

"Inhabited" get function: returns default instead of none in the case that the index is out of bounds.

def List.headI E1 #

{α : Type u_1} → [Inhabited α] → List α → α

The head of a list, or the default element of the type is the list is nil.

theorem List.headI_nil E0 #

∀ {α : Type u_1} [inst : Inhabited α], [].headI = default

theorem List.headI_cons E0 #

∀ {α : Type u_1} [inst : Inhabited α] {h : α} {t : List α}, (h :: t).headI = h

def List.getLastI E1 #

{α : Type u_1} → [Inhabited α] → List α → α

The last element of a list, with the default if list empty

def List.takeI E1 #

{α : Type u_1} → [Inhabited α] → ℕ → List α → List α

"Inhabited" take function: Take n elements from a list l. If l has less than n elements, append n - length l elements default.

def List.findM E1 #

{α : Type u} → {m : Type u → Type v} → [Alternative m] → (α → m PUnit.{u + 1}) → List α → m α

findM tac l returns the first element of l on which tac succeeds, and fails otherwise.

def List.findM?' E1 #

{m : Type u → Type v} → [Monad m] → {α : Type u} → (α → m (ULift.{u, 0} Bool)) → List α → m (Option α)

findM? p l returns the first element a of l for which p a returns true. findM? short-circuits, so p is not necessarily run on every a in l. This is a monadic version of List.find.

def List.orM E1 #

{m : Type → Type v} → [Monad m] → List (m Bool) → m Bool

orM xs runs the actions in xs, returning true if any of them returns true. orM short-circuits, so if an action returns true, later actions are not run.

def List.andM E1 #

{m : Type → Type v} → [Monad m] → List (m Bool) → m Bool

andM xs runs the actions in xs, returning true if all of them return true. andM short-circuits, so if an action returns false, later actions are not run.

def List.foldlIdxM E1 #

{m : Type v → Type w} → [Monad m] → {α : Type u_7} → {β : Type v} → (ℕ → β → α → m β) → β → List α → m β

Monadic variant of foldlIdx.

def List.foldrIdxM E1 #

{m : Type v → Type w} → [Monad m] → {α : Type u_7} → {β : Type v} → (ℕ → α → β → m β) → β → List α → m β

Monadic variant of foldrIdx.

def List.mapIdxMAux' E1 #

{m : Type v → Type w} → [Monad m] → {α : Type u_7} → (ℕ → α → m PUnit.{v + 1}) → ℕ → List α → m PUnit.{v + 1}

Auxiliary definition for mapIdxM'.

def List.mapIdxM' E1 #

{m : Type v → Type w} → [Monad m] → {α : Type u_7} → (ℕ → α → m PUnit.{v + 1}) → List α → m PUnit.{v + 1}

A variant of mapIdxM specialised to applicative actions which return Unit.

def List.Forall E1 #

{α : Type u_1} → (α → Prop) → List α → Prop

l.Forall p is equivalent to ∀ a ∈ l, p a, but unfolds directly to a conjunction, i.e. List.Forall p [0, 1, 2] = p 0 ∧ p 1 ∧ p 2.

def List.permutationsAux2 E1 #

{α : Type u_1} → {β : Type u_2} → α → List α → List β → List α → (List α → β) → List α × List β

An auxiliary function for defining permutations. permutationsAux2 t ts r ys f is equal to (ys ++ ts, (insert_left ys t ts).map f ++ r), where insert_left ys t ts (not explicitly defined) is the list of lists of the form insert_nth n t (ys ++ ts) for 0 ≤ n < length ys.

    permutations_aux2 10 [4, 5, 6] [] [1, 2, 3] id =
      ([1, 2, 3, 4, 5, 6],
       [[10, 1, 2, 3, 4, 5, 6],
        [1, 10, 2, 3, 4, 5, 6],
        [1, 2, 10, 3, 4, 5, 6]])

def List.permutationsAux.rec E1 #

{α : Type u_1} →
  {C : List α → List α → Sort v} →
    ((is : List α) → C [] is) →
      ((t : α) → (ts is : List α) → C ts (t :: is) → C is [] → C (t :: ts) is) → (l₁ l₂ : List α) → C l₁ l₂

A recursor for pairs of lists. To have C l₁ l₂ for all l₁, l₂, it suffices to have it for l₂ = [] and to be able to pour the elements of l₁ into l₂.

def List.permutationsAux E0 #

{α : Type u_1} → List α → List α → List (List α)

An auxiliary function for defining permutations. permutationsAux ts is is the set of all permutations of is ++ ts that do not fix ts.

def List.permutations E1 #

{α : Type u_1} → List α → List (List α)

List of all permutations of l.

     permutations [1, 2, 3] =
       [[1, 2, 3], [2, 1, 3], [3, 2, 1],
        [2, 3, 1], [3, 1, 2], [1, 3, 2]]

def List.permutations'Aux E0 #

{α : Type u_1} → α → List α → List (List α)

permutations'Aux t ts inserts t into every position in ts, including the last. This function is intended for use in specifications, so it is simpler than permutationsAux2, which plays roughly the same role in permutations.

Note that (permutationsAux2 t [] [] ts id).2 is similar to this function, but skips the last position:

    permutations'Aux 10 [1, 2, 3] =
      [[10, 1, 2, 3], [1, 10, 2, 3], [1, 2, 10, 3], [1, 2, 3, 10]]
    (permutationsAux2 10 [] [] [1, 2, 3] id).2 =
      [[10, 1, 2, 3], [1, 10, 2, 3], [1, 2, 10, 3]]

def List.permutations' E1 #

{α : Type u_1} → List α → List (List α)

List of all permutations of l. This version of permutations is less efficient but has simpler definitional equations. The permutations are in a different order, but are equal up to permutation, as shown by List.permutations_perm_permutations'.

     permutations [1, 2, 3] =
       [[1, 2, 3], [2, 1, 3], [2, 3, 1],
        [1, 3, 2], [3, 1, 2], [3, 2, 1]]

def List.extractp E1 #

{α : Type u_1} → (p : α → Prop) → [DecidablePred p] → List α → Option α × List α

extractp p l returns a pair of an element a of l satisfying the predicate p, and l, with a removed. If there is no such element a it returns (none, l).

instance List.instSProd E0 #

{α : Type u_1} → {β : Type u_2} → SProd (List α) (List β) (List (α × β))

Notation for calculating the product of a List

def List.dedup E1 #

{α : Type u_1} → [DecidableEq α] → List α → List α

dedup l removes duplicates from l (taking only the last occurrence). Defined as pwFilter (≠).

See also eraseDups which keeps the first occurrence instead:

dedup [2, 0, 1, 0, 2, 0] = [1, 2, 0]
eraseDups [2, 0, 1, 0, 2, 0] = [2, 0, 1]

def List.destutter' E3 #

{α : Type u_1} → (R : α → α → Prop) → [DecidableRel R] → α → List α → List α

Greedily create a sublist of a :: l such that, for every two adjacent elements a, b, R a b holds. Mostly used with ≠; for example, destutter' (≠) 1 [2, 2, 1, 1] = [1, 2, 1], destutter' (≠) 1, [2, 3, 3] = [1, 2, 3], destutter' (<) 1 [2, 5, 2, 3, 4, 9] = [1, 2, 5, 9].

def List.destutter E1 #

{α : Type u_1} → (R : α → α → Prop) → [DecidableRel R] → List α → List α

Greedily create a sublist of l such that, for every two adjacent elements a, b ∈ l, R a b holds. Mostly used with ≠; for example, destutter (≠) [1, 2, 2, 1, 1] = [1, 2, 1], destutter (≠) [1, 2, 3, 3] = [1, 2, 3], destutter (<) [1, 2, 5, 2, 3, 4, 9] = [1, 2, 5, 9].

def List.chooseX E1 #

{α : Type u_1} → (p : α → Prop) → [DecidablePred p] → (l : List α) → (∃ a ∈ l, p a) → { a // a ∈ l ∧ p a }

Given a decidable predicate p and a proof of existence of a ∈ l such that p a, choose the first element with this property. This version returns both a and proofs of a ∈ l and p a.

def List.choose E1 #

{α : Type u_1} → (p : α → Prop) → [DecidablePred p] → (l : List α) → (∃ a ∈ l, p a) → α

Given a decidable predicate p and a proof of existence of a ∈ l such that p a, choose the first element with this property. This version returns a : α, and properties are given by choose_mem and choose_property.

def List.mapDiagM' E1 #

{m : Type → Type u_7} → [Monad m] → {α : Type u_8} → (α → α → m Unit) → List α → m Unit

mapDiagM' f l calls f on all elements in the upper triangular part of l × l. That is, for each e ∈ l, it will run f e e and then f e e' for each e' that appears after e in l.

Example: suppose l = [1, 2, 3]. mapDiagM' f l will evaluate, in this order, f 1 1, f 1 2, f 1 3, f 2 2, f 2 3, f 3 3.

def List.map₂Left' E1 #

{α : Type u_1} → {β : Type u_2} → {γ : Type u_3} → (α → Option β → γ) → List α → List β → List γ × List β

Left-biased version of List.map₂. map₂Left' f as bs applies f to each pair of elements aᵢ ∈ as and bᵢ ∈ bs. If bs is shorter than as, f is applied to none for the remaining aᵢ. Returns the results of the f applications and the remaining bs.

map₂Left' prod.mk [1, 2] ['a'] = ([(1, some 'a'), (2, none)], [])

map₂Left' prod.mk [1] ['a', 'b'] = ([(1, some 'a')], ['b'])

def List.map₂Right' E1 #

{α : Type u_1} → {β : Type u_2} → {γ : Type u_3} → (Option α → β → γ) → List α → List β → List γ × List α

Right-biased version of List.map₂. map₂Right' f as bs applies f to each pair of elements aᵢ ∈ as and bᵢ ∈ bs. If as is shorter than bs, f is applied to none for the remaining bᵢ. Returns the results of the f applications and the remaining as.

map₂Right' prod.mk [1] ['a', 'b'] = ([(some 1, 'a'), (none, 'b')], [])

map₂Right' prod.mk [1, 2] ['a'] = ([(some 1, 'a')], [2])

def List.map₂Left E1 #

{α : Type u_1} → {β : Type u_2} → {γ : Type u_3} → (α → Option β → γ) → List α → List β → List γ

Left-biased version of List.map₂. map₂Left f as bs applies f to each pair aᵢ ∈ as and bᵢ ∈ bs. If bs is shorter than as, f is applied to none for the remaining aᵢ.

map₂Left Prod.mk [1, 2] ['a'] = [(1, some 'a'), (2, none)]

map₂Left Prod.mk [1] ['a', 'b'] = [(1, some 'a')]

map₂Left f as bs = (map₂Left' f as bs).fst

def List.map₂Right E1 #

{α : Type u_1} → {β : Type u_2} → {γ : Type u_3} → (Option α → β → γ) → List α → List β → List γ

Right-biased version of List.map₂. map₂Right f as bs applies f to each pair aᵢ ∈ as and bᵢ ∈ bs. If as is shorter than bs, f is applied to none for the remaining bᵢ.

map₂Right Prod.mk [1, 2] ['a'] = [(some 1, 'a')]

map₂Right Prod.mk [1] ['a', 'b'] = [(some 1, 'a'), (none, 'b')]

map₂Right f as bs = (map₂Right' f as bs).fst

def List.mapAsyncChunked E1 #

{α : Type u_7} → {β : Type u_8} → (α → β) → List α → optParam ℕ 1024 → List β

Asynchronous version of List.map.

def List.zipWith3 E1 #

{α : Type u_1} → {β : Type u_2} → {γ : Type u_3} → {δ : Type u_4} → (α → β → γ → δ) → List α → List β → List γ → List δ

Ternary version of List.zipWith.

def List.zipWith4 E1 #

{α : Type u_1} →
  {β : Type u_2} →
    {γ : Type u_3} → {δ : Type u_4} → {ε : Type u_5} → (α → β → γ → δ → ε) → List α → List β → List γ → List δ → List ε

Quaternary version of list.zipWith.

def List.zipWith5 E1 #

{α : Type u_1} →
  {β : Type u_2} →
    {γ : Type u_3} →
      {δ : Type u_4} →
        {ε : Type u_5} → {ζ : Type u_6} → (α → β → γ → δ → ε → ζ) → List α → List β → List γ → List δ → List ε → List ζ

Quinary version of list.zipWith.

def List.replaceIf E1 #

{α : Type u_1} → List α → List Bool → List α → List α

Given a starting list old, a list of Booleans and a replacement list new, read the items in old in succession and either replace them with the next element of new or not, according as to whether the corresponding Boolean is true or false.

def List.iterate E1 #

{α : Type u_1} → (α → α) → α → ℕ → List α

iterate f a n is [a, f a, ..., f^[n - 1] a].

def List.iterateTR E1 #

{α : Type u_1} → (α → α) → α → ℕ → List α

Tail-recursive version of List.iterate.

def List.iterateTR.loop E1 #

{α : Type u_1} → (α → α) → α → ℕ → List α → List α

iterateTR.loop f a n l := iterate f a n ++ reverse l.

theorem List.iterateTR_loop_eq E0 #

∀ {α : Type u_1} (f : α → α) (a : α) (n : ℕ) (l : List α), List.iterateTR.loop f a n l = l.reverse ++ List.iterate f a n

theorem List.iterate_eq_iterateTR E1 #

@List.iterate = @List.iterateTR

def List.mapAccumr E1 #

{α : Type u_1} → {β : Type u_2} → {γ : Type u_3} → (α → γ → γ × β) → List α → γ → γ × List β

Runs a function over a list returning the intermediate results and a final result.

theorem List.length_mapAccumr E0 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → γ → γ × β) (x : List α) (s : γ),
  (List.mapAccumr f x s).2.length = x.length

Length of the list obtained by mapAccumr.

def List.mapAccumr₂ E1 #

{α : Type u_1} →
  {β : Type u_2} → {γ : Type u_3} → {δ : Type u_4} → (α → β → γ → γ × δ) → List α → List β → γ → γ × List δ

Runs a function over two lists returning the intermediate results and a final result.

theorem List.length_mapAccumr₂ E0 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (f : α → β → γ → γ × δ) (x : List α) (y : List β) (c : γ),
  (List.mapAccumr₂ f x y c).2.length = min x.length y.length

Length of a list obtained using mapAccumr₂.

abbrev List.consecutivePairs E1 #

{α : Type u_1} → List α → List (α × α)

consecutivePairs [a, b, c, d] is [(a, b), (b, c), (c, d)].