Theorems about List
operations. #
For each List
operation, we would like theorems describing the following, when relevant:
- if it is a "convenience" function, a
@[simp]
lemma reducing it to more basic operations
(e.g. List.partition_eq_filter_filter
), and otherwise:
- any special cases of equational lemmas that require additional hypotheses
- lemmas for special cases of the arguments (e.g.
List.map_id
)
- the length of the result
(f L).length
- the
i
-th element, described via (f L)[i]
and/or (f L)[i]?
(these should typically be @[simp]
)
- consequences for
f L
of the fact x ∈ L
or x ∉ L
- conditions characterising
x ∈ f L
(often but not always @[simp]
)
- injectivity statements, or congruence statements of the form
p L M → f L = f M
.
- conditions characterising the result, i.e. of the form
f L = M ↔ p M
for some predicate p
,
along with special cases of M
(e.g. List.append_eq_nil : L ++ M = [] ↔ L = [] ∧ M = []
)
- negative characterisations are also useful, e.g.
List.cons_ne_nil
- interactions with all previously described
List
operations where possible
(some of these should be @[simp]
, particularly if the result can be described by a single operation)
- characterising
(∀ (i) (_ : i ∈ f L), P i)
, for some predicate P
Of course for any individual operation, not all of these will be relevant or helpful, so some judgement is required.
General principles for simp
normal forms for List
operations:
- Conversion operations (e.g.
toArray
, or length
) should be moved inwards aggressively,
to make the conversion effective.
- Similarly, operations which work on elements should be moved inwards in preference to
"structural" operations on the list, e.g. we prefer to simplify
List.map f (L ++ M) ~> (List.map f L) ++ (List.map f M)
,
List.map f L.reverse ~> (List.map f L).reverse
, and
List.map f (L.take n) ~> (List.map f L).take n
.
- Arithmetic operations are "light", so e.g. we prefer to simplify
drop i (drop j L)
to drop (i + j) L
,
rather than the other way round.
- Function compositions are "light", so we prefer to simplify
(L.map f).map g
to L.map (g ∘ f)
.
- We try to avoid non-linear left hand sides (i.e. with subexpressions appearing multiple times),
but this is only a weak preference.
- Generally, we prefer that the right hand side does not introduce duplication,
however generally duplication of higher order arguments (functions, predicates, etc) is allowed,
as we expect to be able to compute these once they reach ground terms.
See also
Init.Data.List.Attach
for definitions and lemmas about List.attach
and List.pmap
.
Init.Data.List.Count
for lemmas about List.countP
and List.count
.
Init.Data.List.Erase
for lemmas about List.eraseP
and List.erase
.
Init.Data.List.Find
for lemmas about List.find?
, List.findSome?
, List.findIdx
,
List.findIdx?
, and List.indexOf
Init.Data.List.MinMax
for lemmas about List.min?
and List.max?
.
Init.Data.List.Pairwise
for lemmas about List.Pairwise
and List.Nodup
.
Init.Data.List.Sublist
for lemmas about List.Subset
, List.Sublist
, List.IsPrefix
,
List.IsSuffix
, and List.IsInfix
.
Init.Data.List.TakeDrop
for additional lemmas about List.take
and List.drop
.
Init.Data.List.Zip
for lemmas about List.zip
, List.zipWith
, List.zipWithAll
,
and List.unzip
.
Further results, which first require developing further automation around Nat
, appear in
Also
get
and get?
. #
We simplify l.get i
to l[i.1]'i.2
and l.get? i
to l[i]?
.
getD #
We simplify away getD
, replacing getD l n a
with (l[n]?).getD a
.
Because of this, there is only minimal API for getD
.
get! #
We simplify l.get! i
to l[i]!
.
getElem! #
We simplify l[i]!
to (l[i]?).getD default
.
If one has l[i]
in an expression and h : l = l'
,
rw [h]
will give a "motive it not type correct" error, as it cannot rewrite the
implicit i < l.length
to i < l'.length
directly. The theorem getElem_of_eq
can be used to make
such a rewrite, with rw [getElem_of_eq h]
.
Variant of any_beq
with ==
reversed.
Variant of all_bne
with !=
reversed.
@[reducible, inline, deprecated List.foldr_cons_eq_append (since := "2024-08-22")]
Equations
Instances For
@[reducible, inline, deprecated List.foldr_cons_nil (since := "2024-09-04")]
Equations
Instances For
Prove a proposition about the result of List.foldl
,
by proving it for the initial data,
and the implication that the operation applied to any element of the list preserves the property.
The motive can take values in Sort _
, so this may be used to construct data,
as well as to prove propositions.
Equations
Instances For
Prove a proposition about the result of List.foldr
,
by proving it for the initial data,
and the implication that the operation applied to any element of the list preserves the property.
The motive can take values in Sort _
, so this may be used to construct data,
as well as to prove propositions.
Equations
Instances For
We can prove that two folds over the same list are related (by some arbitrary relation)
if we know that the initial elements are related and the folding function, for each element of the list,
preserves the relation.
We can prove that two folds over the same list are related (by some arbitrary relation)
if we know that the initial elements are related and the folding function, for each element of the list,
preserves the relation.
@[deprecated List.getLast_eq_getElem (since := "2024-07-15")]
@[deprecated List.getElem_cons_length (since := "2024-06-12")]
@[deprecated List.getLast?_eq_getElem? (since := "2024-07-07")]
@[simp]
simp
unfolds headD
in terms of head?
and Option.getD
.
@[simp]
simp
unfolds tailD
in terms of tail?
and Option.getD
.
map_id'
differs from map_id
by representing the identity function as a lambda, rather than id
.
Variant of map_id
, with a side condition that the function is pointwise the identity.
@[deprecated List.getElem?_map (since := "2024-06-12")]
@[deprecated List.getElem_map (since := "2024-06-12")]
@[reducible, inline, deprecated List.forall_mem_map (since := "2024-07-25")]
Equations
Instances For
@[reducible, inline, deprecated List.map_eq_nil_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.map_eq_cons_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.map_eq_cons' (since := "2024-09-05")]
Equations
Instances For
@[deprecated "Use the reverse direction of `map_set`." (since := "2024-09-20")]
@[reducible, inline, deprecated List.filter_eq_nil_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.forall_mem_filter (since := "2024-07-25")]
Equations
Instances For
@[reducible, inline, deprecated List.filter_map (since := "2024-06-15")]
Equations
Instances For
@[reducible, inline, deprecated List.filter_eq_cons_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.filter_congr (since := "2024-06-20")]
Equations
Instances For
@[reducible, inline, deprecated List.forall_mem_filterMap (since := "2024-07-25")]
Equations
Instances For
@[reducible, inline, deprecated List.filterMap_eq_nil_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.filterMap_eq_cons_iff (since := "2024-09-05")]
Equations
Instances For
@[deprecated List.getElem?_append_right (since := "2024-06-12")]
Variant of getElem_append_left
useful for rewriting from the small list to the big list.
Variant of getElem_append_right
useful for rewriting from the small list to the big list.
@[deprecated "Deprecated without replacement." (since := "2024-06-12")]
@[deprecated List.getElem_append_right (since := "2024-06-12")]
@[deprecated "Deprecated without replacement." (since := "2024-06-12")]
@[deprecated List.getElem_of_append (since := "2024-06-12")]
See also eq_append_cons_of_mem
, which proves a stronger version
in which the initial list must not contain the element.
Variant of append_inj
instead requiring equality of the lengths of the second lists.
Variant of append_inj_right
instead requiring equality of the lengths of the second lists.
Variant of append_inj_left
instead requiring equality of the lengths of the second lists.
@[deprecated List.getElem_append (since := "2024-06-12")]
@[deprecated List.getElem_append_left (since := "2024-06-12")]
@[deprecated List.getElem_append_right (since := "2024-06-12")]
@[deprecated List.getElem?_append_left (since := "2024-06-12")]
@[reducible, inline, deprecated List.tail_append_of_ne_nil (since := "2024-07-24")]
Equations
Instances For
@[reducible, inline, deprecated List.nil_eq_append_iff (since := "2024-07-24")]
Equations
Instances For
@[deprecated List.append_ne_nil_of_left_ne_nil (since := "2024-07-24")]
@[deprecated List.append_ne_nil_of_right_ne_nil (since := "2024-07-24")]
@[reducible, inline, deprecated List.append_eq_cons_iff (since := "2024-07-24")]
Equations
Instances For
@[reducible, inline, deprecated List.cons_eq_append_iff (since := "2024-07-24")]
Equations
Instances For
@[reducible, inline, deprecated List.append_inj (since := "2024-07-24")]
Equations
Instances For
@[reducible, inline, deprecated List.append_inj' (since := "2024-07-24")]
Equations
Instances For
@[reducible, inline, deprecated List.mem_append_left (since := "2024-11-20")]
Equations
Instances For
@[reducible, inline, deprecated List.mem_append_right (since := "2024-11-20")]
Equations
Instances For
@[reducible, inline, deprecated List.filterMap_eq_append_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.append_eq_filterMap (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.append_eq_filter_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.map_eq_append_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.append_eq_map_iff (since := "2024-09-05")]
Equations
Instances For
concat #
Note that concat_eq_append
is a @[simp]
lemma, so concat
should usually not appear in goals.
As such there's no need for a thorough set of lemmas describing concat
.
@[reducible, inline, deprecated List.concat_inj (since := "2024-09-05")]
Equations
Instances For
Two lists of sublists are equal iff their flattens coincide, as well as the lengths of the
sublists.
@[reducible, inline, deprecated List.flatMap_eq_nil_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_append (since := "2024-07-24")]
Equations
Instances For
Variant of replicate_succ
that concatenates a
to the end of the list.
@[simp, deprecated List.mem_replicate (since := "2024-09-05")]
@[simp, deprecated List.mem_replicate (since := "2024-09-05")]
@[reducible, inline, deprecated List.replicate_eq_nil_iff (since := "2024-09-05")]
Equations
Instances For
@[deprecated List.getElem_replicate (since := "2024-06-12")]
@[reducible, inline, deprecated List.eq_replicate_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.append_eq_replicate_iff (since := "2024-09-05")]
Equations
Instances For
Every list is either empty, a non-empty replicate
, or begins with a non-empty replicate
followed by a different element.
@[irreducible]
An induction principle for lists based on contiguous runs of identical elements.
Variant of getElem?_reverse
with a hypothesis giving the linear relation between the indices.
@[deprecated List.getElem?_reverse' (since := "2024-06-12")]
@[deprecated List.getElem?_reverse (since := "2024-06-12")]
@[reducible, inline, deprecated List.reverse_eq_cons_iff (since := "2024-09-05")]
Equations
Instances For
@[deprecated List.map_reverse (since := "2024-06-20")]
@[reducible, inline, deprecated List.reverse_eq_append_iff (since := "2024-09-05")]
Equations
Instances For
Reversing a flatten is the same as reversing the order of parts and reversing all parts.
Flattening a reverse is the same as reversing all parts and reversing the flattened result.
Further results about getLast
and getLast?
#
partition #
Because we immediately simplify partition
into two filter
s for verification purposes,
we do not separately develop much theory about it.
@[deprecated List.getElem_dropLast (since := "2024-06-12")]
splitAt #
We don't provide any API for splitAt
, beyond the @[simp]
lemma
splitAt n l = (l.take n, l.drop n)
,
which is proved in Init.Data.List.TakeDrop
.
@[reducible, inline, deprecated List.any_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.all_flatten (since := "2024-10-14")]
Equations
Instances For
Legacy lemmas about get
, get?
, and get!
. #
Hopefully these should not be needed, in favour of lemmas about xs[i]
, xs[i]?
, and xs[i]!
,
to which these simplify.
We may consider deprecating or downstreaming these lemmas.
If one has l.get i
in an expression (with i : Fin l.length
) and h : l = l'
,
rw [h]
will give a "motive is not type correct" error, as it cannot rewrite the
i : Fin l.length
to Fin l'.length
directly. The theorem get_of_eq
can be used to make
such a rewrite, with rw [get_of_eq h]
.
@[deprecated List.getD_eq_getElem?_getD (since := "2024-06-12")]
@[deprecated List.getElem_singleton (since := "2024-06-12")]
@[deprecated List.getElem?_concat_length (since := "2024-06-12")]
@[deprecated List.getElem_set_self (since := "2024-06-12")]
@[deprecated List.getElem_set_ne (since := "2024-06-12")]
@[deprecated List.getElem_set (since := "2024-06-12")]
@[reducible, inline, deprecated List.cons_inj_right (since := "2024-06-15")]
Equations
Instances For
@[reducible, inline, deprecated List.ne_nil_of_length_eq_add_one (since := "2024-06-16")]
Equations
Instances For
@[deprecated "Deprecated without replacement." (since := "2024-07-09")]
@[deprecated List.filter_flatten (since := "2024-08-26")]
@[reducible, inline, deprecated List.getElem_eq_getElem?_get (since := "2024-09-04")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_eq_nil_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_ne_nil_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_eq_cons_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_eq_cons_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_eq_append_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.mem_of_getElem? (since := "2024-09-06")]
Equations
Instances For
@[reducible, inline, deprecated List.mem_of_get? (since := "2024-09-06")]
Equations
Instances For
@[reducible, inline, deprecated List.getElem_set_self (since := "2024-09-04")]
Equations
Instances For
@[reducible, inline, deprecated List.getElem?_set_self (since := "2024-09-04")]
Equations
Instances For
@[reducible, inline, deprecated List.set_eq_nil_iff (since := "2024-09-05")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_nil (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_cons (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.length_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_singleton (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.mem_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_eq_nil_iff (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_ne_nil_iff (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.exists_of_mem_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.mem_flatten_of_mem (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.forall_mem_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_eq_flatMap (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.head?_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.foldl_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.foldr_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.map_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.filterMap_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.filter_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_filter_not_isEmpty (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_filter_ne_nil (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_append (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_concat (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_eq_append_iff (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.eq_iff_flatten_eq (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_replicate_nil (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_replicate_singleton (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_replicate_replicate (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.reverse_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_reverse (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.getLast?_flatten (since := "2024-10-14")]
Equations
Instances For
@[reducible, inline, deprecated List.flatten_eq_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_def (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_id (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.mem_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.exists_of_mem_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.mem_flatMap_of_mem (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_eq_nil_iff (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.forall_mem_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_singleton (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_singleton' (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.head?_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_append (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_assoc (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.map_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_map (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.map_eq_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.filterMap_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.filter_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_eq_foldl (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_replicate (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.reverse_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.flatMap_reverse (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.getLast?_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.any_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.all_flatMap (since := "2024-10-16")]
Equations
Instances For
@[reducible, inline, deprecated List.get?_eq_none (since := "2024-11-29")]
Equations
Instances For
@[reducible, inline, deprecated List.getElem?_eq_some_iff (since := "2024-11-29")]
Equations
Instances For
@[reducible, inline, deprecated List.get?_eq_some_iff (since := "2024-11-29")]
Equations
Instances For
@[deprecated LawfulGetElem.getElem?_def (since := "2024-11-29")]
@[reducible, inline, deprecated List.getElem?_eq_none (since := "2024-11-29")]
Equations
Instances For
@[deprecated isSome_getElem? (since := "2024-12-09")]
@[deprecated isNone_getElem? (since := "2024-12-09")]