Tail recursive implementations for List definitions. #
Many of the proofs require theorems about Array,
so these are in a separate file to minimize imports.
The definitions replaced here are noncomputable, so compiled code using them must import this
file.
Basic List operations. #
The following operations are already tail-recursive, and do not need @[csimp] replacements:
get, foldl, beq, isEqv, reverse, elem (and hence contains), drop, dropWhile,
partition, isPrefixOf, isPrefixOf?, find?, findSome?, lookup, any (and hence or),
all (and hence and) , range, eraseDups, eraseReps, span, splitBy.
The following operations are still missing @[csimp] replacements:
concat, zipWithAll.
The following operations are not recursive to begin with
(or are defined in terms of recursive primitives):
isEmpty, isSuffixOf, isSuffixOf?, insert, enum, min?, max?, min, max and
removeAll.
The following operations were already given @[csimp] replacements in Init/Data/List/Basic.lean:
length, map, filter, replicate, leftPad, range'.
The following operations are given @[csimp] replacements below:
set, filterMap, foldr, append, bind, join,
take, takeWhile, dropLast, replace, modify, insertIdx, erase, eraseIdx, zipWith,
unzip, enumFrom, intersperse and intercalate.
The following definitions use the operations above. They are given @[csimp] replacements below
with the same definition, which is compiled using the replacements of those operations:
sum, prod, foldrRecOn, extract, rotateLeft, rotateRight, isPerm and zip.
set #
Replaces the value at (zero-based) index n in l with a. If the index is out of bounds, then
the list is returned unmodified.
This is a tail-recursive version of List.set that's used at runtime.
Examples:
["water", "coffee", "soda", "juice"].set 1 "tea" = ["water", "tea", "soda", "juice"]["water", "coffee", "soda", "juice"].set 4 "tea" = ["water", "coffee", "soda", "juice"]
Equations
- l.setTR n a = List.setTR.go✝ l a l n #[]
Instances For
filterMap #
Applies a function that returns an Option to each element of a list, collecting the non-none
values.
O(|l|). This is a tail-recursive version of List.filterMap, used at runtime.
Example:
#eval [1, 2, 5, 2, 7, 7].filterMapTR fun x =>
if x > 2 then some (2 * x) else none
[10, 14, 14]
Equations
- List.filterMapTR f l = List.filterMapTR.go f l #[]
Instances For
Auxiliary for filterMap: filterMap.go f l = acc.toList ++ filterMap f l
Equations
- List.filterMapTR.go f [] a✝ = a✝.toList
- List.filterMapTR.go f (a :: as) a✝ = match f a with | none => List.filterMapTR.go f as a✝ | some b => List.filterMapTR.go f as (a✝.push b)
Instances For
reduceOption #
Drop nones from a list, and replace each remaining some a with a.
Equations
Instances For
foldr #
Folds a function over a list from the right, accumulating a value starting with init. The
accumulated value is combined with the each element of the list in reverse order, using f.
O(|l|). This is the tail-recursive replacement for List.foldr in runtime code.
Examples:
[a, b, c].foldrTR f init = f a (f b (f c init))[1, 2, 3].foldrTR (toString · ++ ·) "" = "123"[1, 2, 3].foldrTR (s!"({·} {·})") "!" = "(1 (2 (3 !)))"
Equations
- List.foldrTR f init l = Array.foldr f init l.toArray
Instances For
sum #
Computes the sum of the elements of a list.
This is the implementation of List.sum used at runtime; it is compiled using List.foldrTR.
Equations
- List.sumTR = List.foldr (fun (x1 x2 : α) => x1 + x2) 0
Instances For
prod #
Computes the product of the elements of a list.
This is the implementation of List.prod used at runtime; it is compiled using List.foldrTR.
Equations
- List.prodTR = List.foldr (fun (x1 x2 : α) => x1 * x2) 1
Instances For
foldrRecOn #
A reasoning principle for proving propositions about the result of List.foldr by establishing an
invariant that is true for the initial data and preserved by the operation being folded.
This is the implementation of List.foldrRecOn used at runtime; it is compiled using
List.foldrTR.
Equations
- List.foldrRecOnImpl [] x✝³ x✝¹ x✝ = x✝¹
- List.foldrRecOnImpl (x_5 :: l) x✝³ x✝¹ x✝ = x✝ (List.foldr x✝³ x✝² l) (List.foldrRecOnImpl l x✝³ x✝¹ fun (b : β) (c : motive b) (a : α) (m : a ∈ l) => x✝ b c a ⋯) x_5 ⋯
Instances For
flatMap #
Applies a function that returns a list to each element of a list, and concatenates the resulting lists.
This is the tail-recursive version of List.flatMap that's used at runtime.
Examples:
[2, 3, 2].flatMapTR List.range = [0, 1, 0, 1, 2, 0, 1]["red", "blue"].flatMapTR String.toList = ['r', 'e', 'd', 'b', 'l', 'u', 'e']
Equations
- List.flatMapTR f as = List.flatMapTR.go✝ f as #[]
Instances For
flatten #
Concatenates a list of lists into a single list, preserving the order of the elements.
O(|flatten L|). This is a tail-recursive version of List.flatten, used in runtime code.
Examples:
[["a"], ["b", "c"]].flattenTR = ["a", "b", "c"][["a"], [], ["b", "c"], ["d", "e", "f"]].flattenTR = ["a", "b", "c", "d", "e", "f"]
Equations
- l.flattenTR = List.flatMapTR id l
Instances For
Sublists #
take #
Extracts the first n elements of xs, or the whole list if n is greater than xs.length.
O(min n |xs|). This is a tail-recursive version of List.take, used at runtime.
Examples:
[a, b, c, d, e].takeTR 0 = [][a, b, c, d, e].takeTR 3 = [a, b, c][a, b, c, d, e].takeTR 6 = [a, b, c, d, e]
Equations
- List.takeTR n l = List.takeTR.go✝ l l n #[]
Instances For
extract #
Returns the slice of l from indices start (inclusive) to stop (exclusive).
This is the implementation of List.extract used at runtime; it is compiled using List.takeTR.
Instances For
rotateLeft #
Rotates the elements of xs to the left, moving i % xs.length elements from the start of the list
to the end.
This is the implementation of List.rotateLeft used at runtime; it is compiled using
List.takeTR.
Equations
Instances For
rotateRight #
Rotates the elements of xs to the right, moving i % xs.length elements from the end of the list
to the start.
This is the implementation of List.rotateRight used at runtime; it is compiled using
List.takeTR.
Equations
Instances For
takeWhile #
Returns the longest initial segment of xs for which p returns true.
O(|xs|). This is a tail-recursive version of List.take, used at runtime.
Examples:
[7, 6, 4, 8].takeWhileTR (· > 5) = [7, 6][7, 6, 6, 5].takeWhileTR (· > 5) = [7, 6, 6][7, 6, 6, 8].takeWhileTR (· > 5) = [7, 6, 6, 8]
Equations
- List.takeWhileTR p l = List.takeWhileTR.go✝ p l l #[]
Instances For
dropLast #
Removes the last element of the list, if one exists.
This is a tail-recursive version of List.dropLast, used at runtime.
Examples:
[].dropLastTR = []["tea"].dropLastTR = []["tea", "coffee", "juice"].dropLastTR = ["tea", "coffee"]
Equations
- l.dropLastTR = l.toArray.pop.toList
Instances For
Finding elements #
Tail recursive implementation of findRev?. This is only used at runtime.
Equations
- List.findRev?TR p l = List.find? p l.reverse
Instances For
Tail recursive implementation of finSomedRev?. This is only used at runtime.
Equations
- List.findSomeRev?TR f l = List.findSome? f l.reverse
Instances For
Manipulating elements #
replace #
Replaces the first element of the list l that is equal to a with b. If no element is equal to
a, then the list is returned unchanged.
O(|l|). This is a tail-recursive version of List.replace that's used in runtime code.
Examples:
[1, 4, 2, 3, 3, 7].replaceTR 3 6 = [1, 4, 2, 6, 3, 7][1, 4, 2, 3, 3, 7].replaceTR 5 6 = [1, 4, 2, 3, 3, 7]
Equations
- l.replaceTR b c = List.replaceTR.go✝ l b c l #[]
Instances For
modify #
Replaces the element at the given index, if it exists, with the result of applying f to it.
This is a tail-recursive version of List.modify.
Examples:
[1, 2, 3].modifyTR 0 (· * 10) = [10, 2, 3][1, 2, 3].modifyTR 2 (· * 10) = [1, 2, 30][1, 2, 3].modifyTR 3 (· * 10) = [1, 2, 3]
Equations
- l.modifyTR i f = List.modifyTR.go✝ f l i #[]
Instances For
insertIdx #
Inserts an element into a list at the specified index. If the index is greater than the length of the list, then the list is returned unmodified.
In other words, the new element is inserted into the list l after the first i elements of l.
This is a tail-recursive version of List.insertIdx, used at runtime.
Examples:
["tues", "thur", "sat"].insertIdxTR 1 "wed" = ["tues", "wed", "thur", "sat"]["tues", "thur", "sat"].insertIdxTR 2 "wed" = ["tues", "thur", "wed", "sat"]["tues", "thur", "sat"].insertIdxTR 3 "wed" = ["tues", "thur", "sat", "wed"]["tues", "thur", "sat"].insertIdxTR 4 "wed" = ["tues", "thur", "sat"]
Equations
- l.insertIdxTR n a = List.insertIdxTR.go✝ a n l #[]
Instances For
erase #
Removes the first occurrence of a from l. If a does not occur in l, the list is returned
unmodified.
O(|l|).
This is a tail-recursive version of List.erase, used in runtime code.
Examples:
Equations
- l.eraseTR a = List.eraseTR.go✝ l a l #[]
Instances For
Removes the first element of a list for which p returns true. If no element satisfies p, then
the list is returned unchanged.
This is a tail-recursive version of eraseP, used at runtime.
Examples:
[2, 1, 2, 1, 3, 4].erasePTR (· < 2) = [2, 2, 1, 3, 4][2, 1, 2, 1, 3, 4].erasePTR (· > 2) = [2, 1, 2, 1, 4][2, 1, 2, 1, 3, 4].erasePTR (· > 8) = [2, 1, 2, 1, 3, 4]
Equations
- List.erasePTR p l = List.erasePTR.go✝ p l l #[]
Instances For
eraseIdx #
Removes the element at the specified index. If the index is out of bounds, the list is returned unmodified.
O(i).
This is a tail-recursive version of List.eraseIdx, used at runtime.
Examples:
[0, 1, 2, 3, 4].eraseIdxTR 0 = [1, 2, 3, 4][0, 1, 2, 3, 4].eraseIdxTR 1 = [0, 2, 3, 4][0, 1, 2, 3, 4].eraseIdxTR 5 = [0, 1, 2, 3, 4]
Equations
- l.eraseIdxTR n = List.eraseIdxTR.go✝ l l n #[]
Instances For
Permutations #
isPerm #
Returns true if l₁ and l₂ are permutations of each other. O(|l₁| * |l₂|).
This is the implementation of List.isPerm used at runtime; it is compiled using List.eraseTR.
Equations
Instances For
Zippers #
zipWith #
Applies a function to the corresponding elements of two lists, stopping at the end of the shorter list.
O(min |xs| |ys|). This is a tail-recursive version of List.zipWith that's used at runtime.
Examples:
[1, 2].zipWithTR (· + ·) [5, 6] = [6, 8][1, 2, 3].zipWithTR (· + ·) [5, 6, 10] = [6, 8, 13][].zipWithTR (· + ·) [5, 6] = [][x₁, x₂, x₃].zipWithTR f [y₁, y₂, y₃, y₄] = [f x₁ y₁, f x₂ y₂, f x₃ y₃]
Equations
- List.zipWithTR f as bs = List.zipWithTR.go✝ f as bs #[]
Instances For
zip #
Combines two lists into a list of pairs in which the first and second components are the corresponding elements of each list. The resulting list is the length of the shorter of the input lists.
This is the implementation of List.zip used at runtime; it is compiled using List.zipWithTR.
Equations
Instances For
unzip #
Separates a list of pairs into two lists that contain the respective first and second components.
O(|l|). This is a tail-recursive version of List.unzip that's used at runtime.
Examples:
[("Monday", 1), ("Tuesday", 2)].unzipTR = (["Monday", "Tuesday"], [1, 2])[(x₁, y₁), (x₂, y₂), (x₃, y₃)].unzipTR = ([x₁, x₂, x₃], [y₁, y₂, y₃])([] : List (Nat × String)).unzipTR = (([], []) : List Nat × List String)
Equations
Instances For
Ranges and enumeration #
zipIdx #
Pairs each element of a list with its index, optionally starting from an index other than 0.
O(|l|). This is a tail-recursive version of List.zipIdx that's used at runtime.
Examples:
Equations
- One or more equations did not get rendered due to their size.
Instances For
enumFrom #
Other list operations #
intersperse #
Alternates the elements of l with sep.
O(|l|).
This is a tail-recursive version of List.intersperse, used at runtime.
Examples:
List.intersperseTR "then" [] = []List.intersperseTR "then" ["walk"] = ["walk"]List.intersperseTR "then" ["walk", "run"] = ["walk", "then", "run"]List.intersperseTR "then" ["walk", "run", "rest"] = ["walk", "then", "run", "then", "rest"]
Equations
Instances For
intercalate #
Alternates the lists in xs with the separator sep.
This is a tail-recursive version of List.intercalate used at runtime.
Examples:
List.intercalateTR sep [] = []List.intercalateTR sep [a] = aList.intercalateTR sep [a, b] = a ++ sep ++ bList.intercalateTR sep [a, b, c] = a ++ sep ++ b ++ sep ++ c
Equations
- sep.intercalateTR [] = []
- sep.intercalateTR [x_1] = x_1
- sep.intercalateTR (x_1 :: xs) = List.intercalateTR.go✝ sep.toArray x_1 xs #[]