Additional List lemmas for Lean 4 that complement Mathlib.
Mathlib provides comprehensive lemmas for Multiset and Finset sum operations,
but the List module has some gaps particularly around negation and subtraction.
This library fills those gaps.
Add to your lakefile.lean:
require «list-utils» from git
"https://github.com/sdiehl/list-utils.git" @ "v1.3.0"Then run:
lake update
lake buildList.sum_map_neg- Sum of negated elements (ℚ version)List.sum_map_neg'- Sum of negated elements (generalAddCommGroup)
List.sum_map_sub- Sum distributes over subtraction (ℚ version)List.sum_map_sub'- Sum distributes over subtraction (generalAddCommGroup)
List.sum_mul_left- Left scalar multiplication into sumList.sum_mul_right- Right scalar multiplication into sum
List.sum_map_const- Sum of constant functionList.sum_map_append- Sum over appended lists
List.exists_pos_of_sum_pos- If sum of non-negative values is positive, some element is positiveList.exists_neg_of_sum_neg- If sum of non-positive values is negative, some element is negativeList.exists_ne_zero_of_sum_ne_zero- If sum is nonzero with same-sign elements, some element is nonzero
Operations for lists where elements have a key accessor, supporting the common "upsert" pattern.
List.upsertBy- Update element matching key, or append default if not foundList.updateBy- Update element matching key (no-op if not found)List.findBy?- Find element by key accessorList.containsBy- Check if element with key existsList.removeBy- Remove all elements where key matches
List.countBy- Count elements satisfying a predicateList.maxByOpt- Find element with maximum value by comparator
List.keysOf- Extract first elements from list of pairsList.valuesOf- Extract second elements from list of pairs
Example:
structure User where
id : Nat
name : String
def users : List User := [{ id := 1, name := "Alice" }]
-- Update user 1's name, or add new user if not found
let updated := users.upsertBy (·.id) 1
(fun u => { u with name := "Alicia" })
{ id := 1, name := "Default" }Apache 2.0