statement
stringlengths
1
8.65k
proof
stringlengths
0
19.6k
type
stringclasses
12 values
symbolic_name
stringlengths
1
110
library
stringclasses
165 values
filename
stringclasses
822 values
imports
listlengths
0
19
deps
listlengths
0
64
docstring
stringlengths
0
3.64k
source_url
stringclasses
1 value
commit
stringclasses
1 value
List.flatMap {α : Type u} {β : Type v} (b : α → List β) (as : List α) : List β
flatten (map b as)
def
List.flatMap
Init
src/Init/Prelude.lean
[]
[ "List" ]
Applies a function that returns a list to each element of a list, and concatenates the resulting lists. Examples: * `[2, 3, 2].flatMap List.range = [0, 1, 0, 1, 2, 0, 1]` * `["red", "blue"].flatMap String.toList = ['r', 'e', 'd', 'b', 'l', 'u', 'e']`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array (α : Type u) where /-- Converts a `List α` into an `Array α`. The function `List.toArray` is preferred. At runtime, this constructor is overridden by `List.toArrayImpl` and is `O(n)` in the length of the list. -/ mk :: /-- Converts an `Array α` into a `List α` that contains the same elements i...
structure
Array
Init
src/Init/Prelude.lean
[]
[ "List" ]
`Array α` is the type of [dynamic arrays](https://en.wikipedia.org/wiki/Dynamic_array) with elements from `α`. This type has special support in the runtime. Arrays perform best when unshared. As long as there is never more than one reference to an array, all updates will be performed _destructively_. This results in p...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
List.toArray (xs : List α) : Array α
.mk xs
abbrev
List.toArray
Init
src/Init/Prelude.lean
[]
[ "Array", "List" ]
Converts a `List α` into an `Array α`. `O(|xs|)`. At runtime, this operation is implemented by `List.toArrayImpl` and takes time linear in the length of the list. `List.toArray` should be used instead of `Array.mk`. Examples: * `[1, 2, 3].toArray = #[1, 2, 3]` * `["monday", "wednesday", friday"].toArray = #["monda...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkEmpty {α : Type u} (c : @& Nat) : Array α
where toList := List.nil
def
Array.mkEmpty
Init
src/Init/Prelude.lean
[]
[ "Array", "Nat" ]
Constructs a new empty array with initial capacity `c`. This will be deprecated in favor of `Array.emptyWithCapacity` in the future.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.emptyWithCapacity {α : Type u} (c : @& Nat) : Array α
where toList := List.nil
def
Array.emptyWithCapacity
Init
src/Init/Prelude.lean
[]
[ "Array", "Nat" ]
Constructs a new empty array with initial capacity `c`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.empty {α : Type u} : Array α
emptyWithCapacity 0
def
Array.empty
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Constructs a new empty array with initial capacity `0`. Use `Array.emptyWithCapacity` to create an array with a greater initial capacity.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.size {α : Type u} (a : @& Array α) : Nat
a.toList.length
def
Array.size
Init
src/Init/Prelude.lean
[]
[ "Array", "Nat" ]
Gets the number of elements stored in an array. This is a cached value, so it is `O(1)` to access. The space allocated for an array, referred to as its _capacity_, is at least as large as its size, but may be larger. The capacity of an array is an internal detail that's not observable by Lean code.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.getInternalBorrowed {α : Type u} (a : @& Array α) (i : @& Nat) (h : LT.lt i a.size) : α
a.toList.get ⟨i, h⟩
opaque
Array.getInternalBorrowed
Init
src/Init/Prelude.lean
[]
[ "Array", "Nat" ]
Version of `Array.getInternal` that does not increment the reference count of its result. This is only intended for direct use by the compiler.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.getInternal {α : Type u} (a : @& Array α) (i : @& Nat) (h : LT.lt i a.size) : α
a.toList.get ⟨i, h⟩
def
Array.getInternal
Init
src/Init/Prelude.lean
[]
[ "Array", "Nat" ]
Use the indexing notation `a[i]` instead. Access an element from an array without needing a runtime bounds checks, using a `Nat` index and a proof that it is in bounds. This function does not use `get_elem_tactic` to automatically find the proof that the index is in bounds. This is because the tactic itself needs to ...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.getD (a : Array α) (i : Nat) (v₀ : α) : α
dite (LT.lt i a.size) (fun h => a.getInternal i h) (fun _ => v₀)
abbrev
Array.getD
Init
src/Init/Prelude.lean
[]
[ "Array", "Nat", "dite" ]
Returns the element at the provided index, counting from `0`. Returns the fallback value `v₀` if the index is out of bounds. To return an `Option` depending on whether the index is in bounds, use `a[i]?`. To panic if the index is out of bounds, use `a[i]!`. Examples: * `#["spring", "summer", "fall", "winter"].getD 2...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.get!InternalBorrowed {α : Type u} [@&Inhabited α] (a : @& Array α) (i : @& Nat) : α
opaque
Array.get!InternalBorrowed
Init
src/Init/Prelude.lean
[]
[ "Array", "Inhabited", "Nat" ]
Version of `Array.get!Internal` that does not increment the reference count of its result. This is only intended for direct use by the compiler.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.get!Internal {α : Type u} [@&Inhabited α] (a : @& Array α) (i : @& Nat) : α
Array.getD a i default
def
Array.get!Internal
Init
src/Init/Prelude.lean
[]
[ "Array", "Array.getD", "Inhabited", "Nat" ]
Use the indexing notation `a[i]!` instead. Access an element from an array, or panic if the index is out of bounds.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.push {α : Type u} (a : Array α) (v : α) : Array α
where toList := List.concat a.toList v
def
Array.push
Init
src/Init/Prelude.lean
[]
[ "Array", "List.concat" ]
Adds an element to the end of an array. The resulting array's size is one greater than the input array. If there are no other references to the array, then it is modified in-place. This takes amortized `O(1)` time because `Array α` is represented by a dynamic array. Examples: * `#[].push "apple" = #["apple"]` * `#["a...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkArray0 {α : Type u} : Array α
emptyWithCapacity 0
def
Array.mkArray0
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Create array `#[]`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkArray1 {α : Type u} (a₁ : α) : Array α
(emptyWithCapacity 1).push a₁
def
Array.mkArray1
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Create array `#[a₁]`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkArray2 {α : Type u} (a₁ a₂ : α) : Array α
((emptyWithCapacity 2).push a₁).push a₂
def
Array.mkArray2
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Create array `#[a₁, a₂]`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkArray3 {α : Type u} (a₁ a₂ a₃ : α) : Array α
(((emptyWithCapacity 3).push a₁).push a₂).push a₃
def
Array.mkArray3
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Create array `#[a₁, a₂, a₃]`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkArray4 {α : Type u} (a₁ a₂ a₃ a₄ : α) : Array α
((((emptyWithCapacity 4).push a₁).push a₂).push a₃).push a₄
def
Array.mkArray4
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Create array `#[a₁, a₂, a₃, a₄]`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkArray5 {α : Type u} (a₁ a₂ a₃ a₄ a₅ : α) : Array α
(((((emptyWithCapacity 5).push a₁).push a₂).push a₃).push a₄).push a₅
def
Array.mkArray5
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Create array `#[a₁, a₂, a₃, a₄, a₅]`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkArray6 {α : Type u} (a₁ a₂ a₃ a₄ a₅ a₆ : α) : Array α
((((((emptyWithCapacity 6).push a₁).push a₂).push a₃).push a₄).push a₅).push a₆
def
Array.mkArray6
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Create array `#[a₁, a₂, a₃, a₄, a₅, a₆]`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkArray7 {α : Type u} (a₁ a₂ a₃ a₄ a₅ a₆ a₇ : α) : Array α
(((((((emptyWithCapacity 7).push a₁).push a₂).push a₃).push a₄).push a₅).push a₆).push a₇
def
Array.mkArray7
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Create array `#[a₁, a₂, a₃, a₄, a₅, a₆, a₇]`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.mkArray8 {α : Type u} (a₁ a₂ a₃ a₄ a₅ a₆ a₇ a₈ : α) : Array α
((((((((emptyWithCapacity 8).push a₁).push a₂).push a₃).push a₄).push a₅).push a₆).push a₇).push a₈
def
Array.mkArray8
Init
src/Init/Prelude.lean
[]
[ "Array" ]
Create array `#[a₁, a₂, a₃, a₄, a₅, a₆, a₇, a₈]`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.appendCore {α : Type u} (as : Array α) (bs : Array α) : Array α
let rec loop (i : Nat) (j : Nat) (as : Array α) : Array α := dite (LT.lt j bs.size) (fun hlt => match i with | 0 => as | Nat.succ i' => loop i' (hAdd j 1) (as.push (bs.getInternal j hlt))) (fun _ => as) loop bs.size 0 as
def
Array.appendCore
Init
src/Init/Prelude.lean
[]
[ "Array", "Nat", "dite" ]
Slower `Array.append` used in quotations.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Array.extract (as : Array α) (start : Nat := 0) (stop : Nat := as.size) : Array α
let rec loop (i : Nat) (j : Nat) (bs : Array α) : Array α := dite (LT.lt j as.size) (fun hlt => match i with | 0 => bs | Nat.succ i' => loop i' (hAdd j 1) (bs.push (as.getInternal j hlt))) (fun _ => bs) let sz' := Nat.sub (min stop as.size) start loop sz' start (emp...
def
Array.extract
Init
src/Init/Prelude.lean
[]
[ "Array", "Nat", "Nat.sub", "dite" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
ByteArray where /-- Packs an array of bytes into a `ByteArray`. Converting between `Array` and `ByteArray` takes linear time. -/ mk :: /-- The data contained in the byte array. Converting between `Array` and `ByteArray` takes linear time. -/ data : Array UInt8
structure
ByteArray
Init
src/Init/Prelude.lean
[]
[ "Array", "UInt8" ]
`ByteArray` is like `Array UInt8`, but with an efficient run-time representation as a packed byte buffer.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
ByteArray.emptyWithCapacity (c : @& Nat) : ByteArray
{ data := Array.empty }
def
ByteArray.emptyWithCapacity
Init
src/Init/Prelude.lean
[]
[ "Array.empty", "ByteArray", "Nat" ]
Constructs a new empty byte array with initial capacity `c`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
ByteArray.empty : ByteArray
emptyWithCapacity 0
def
ByteArray.empty
Init
src/Init/Prelude.lean
[]
[ "ByteArray" ]
Constructs a new empty byte array with initial capacity `0`. Use `ByteArray.emptyWithCapacity` to create an array with a greater initial capacity.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
ByteArray.push : ByteArray → UInt8 → ByteArray
| ⟨bs⟩, b => ⟨bs.push b⟩
def
ByteArray.push
Init
src/Init/Prelude.lean
[]
[ "ByteArray", "UInt8" ]
Adds an element to the end of an array. The resulting array's size is one greater than the input array. If there are no other references to the array, then it is modified in-place. This takes amortized `O(1)` time because `ByteArray` is represented by a dynamic array.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
List.toByteArray (bs : List UInt8) : ByteArray
let rec loop | nil, r => r | cons b bs, r => loop bs (r.push b) loop bs ByteArray.empty
def
List.toByteArray
Init
src/Init/Prelude.lean
[]
[ "ByteArray", "ByteArray.empty", "List", "UInt8" ]
Converts a list of bytes into a `ByteArray`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
ByteArray.size : (@& ByteArray) → Nat
| ⟨bs⟩ => bs.size
def
ByteArray.size
Init
src/Init/Prelude.lean
[]
[ "ByteArray", "Nat" ]
Returns the number of bytes in the byte array. This is the number of bytes actually in the array, as distinct from its capacity, which is the amount of memory presently allocated for the array.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
String.utf8EncodeChar (c : Char) : List UInt8
let v := c.val.toNat ite (LE.le v 0x7f) (List.cons (UInt8.ofNat v) List.nil) (ite (LE.le v 0x7ff) (List.cons (UInt8.ofNat (HAdd.hAdd (HMod.hMod (HDiv.hDiv v 64) 0x20) 0xc0)) (List.cons (UInt8.ofNat (HAdd.hAdd (HMod.hMod v 0x40) 0x80)) List.nil)) (ite (LE.le v 0x...
def
String.utf8EncodeChar
Init
src/Init/Prelude.lean
[]
[ "Char", "List", "UInt8", "UInt8.ofNat", "ite" ]
Returns the sequence of bytes in a character's UTF-8 encoding.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
List.utf8Encode (l : List Char) : ByteArray
l.flatMap String.utf8EncodeChar |>.toByteArray
def
List.utf8Encode
Init
src/Init/Prelude.lean
[]
[ "ByteArray", "Char", "List", "String.utf8EncodeChar" ]
Encode a list of characters (Unicode scalar value) in UTF-8. This is an inefficient model implementation. Use `List.asString` instead.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
ByteArray.IsValidUTF8 (b : ByteArray) : Prop /-- Show that a byte array is valid UTF-8 by exhibiting it as `List.utf8Encode m` for some list `m` of characters. -/ | intro (m : List Char) (hm : Eq b (List.utf8Encode m))
inductive
ByteArray.IsValidUTF8
Init
src/Init/Prelude.lean
[]
[ "ByteArray", "Char", "Eq", "List", "List.utf8Encode" ]
A byte array is valid UTF-8 if it is of the form `List.Internal.utf8Encode m` for some `m`. Note that in order for this definition to be well-behaved it is necessary to know that this `m` is unique. To show this, one defines UTF-8 decoding and shows that encoding and decoding are mutually inverse.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
String where ofByteArray :: /-- The bytes of the UTF-8 encoding of the string. Since strings have a special representation in the runtime, this function actually takes linear time and space at runtime. For efficient access to the string's bytes, use `String.utf8ByteSize` and `String.getUTF8Byte`. -/ toByteArray...
structure
String
Init
src/Init/Prelude.lean
[]
[ "ByteArray", "ByteArray.IsValidUTF8" ]
A string is a sequence of Unicode scalar values. At runtime, strings are represented by [dynamic arrays](https://en.wikipedia.org/wiki/Dynamic_array) of bytes using the UTF-8 encoding. Both the size in bytes (`String.utf8ByteSize`) and in characters (`String.length`) are cached and take constant time. Many operations ...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
String.ofList (data : List Char) : String
⟨List.utf8Encode data, .intro data rfl⟩
def
String.ofList
Init
src/Init/Prelude.lean
[]
[ "Char", "List", "String" ]
Creates a string that contains the characters in a list, in order. Examples: * `String.ofList ['L', '∃', '∀', 'N'] = "L∃∀N"` * `String.ofList [] = ""` * `String.ofList ['a', 'a', 'a'] = "aaa"`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
String.decEq (s₁ s₂ : @& String) : Decidable (Eq s₁ s₂)
match s₁, s₂ with | ⟨⟨⟨s₁⟩⟩, _⟩, ⟨⟨⟨s₂⟩⟩, _⟩ => dite (Eq s₁ s₂) (fun h => match s₁, s₂, h with | _, _, Eq.refl _ => isTrue rfl) (fun h => isFalse (fun h' => h (congrArg (fun s => Array.toList (ByteArray.data (String.toByteArray s))) h')))
def
String.decEq
Init
src/Init/Prelude.lean
[]
[ "Decidable", "Eq", "String", "congrArg", "dite", "rfl" ]
Decides whether two strings are equal. Normally used via the `DecidableEq String` instance and the `=` operator. At runtime, this function is overridden with an efficient native implementation.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
String.Pos.Raw where /-- Get the underlying byte index of a `String.Pos.Raw` -/ byteIdx : Nat
0
structure
String.Pos.Raw
Init
src/Init/Prelude.lean
[]
[ "Nat" ]
A byte position in a `String`, according to its UTF-8 encoding. Character positions (counting the Unicode code points rather than bytes) are represented by plain `Nat`s. Indexing a `String` by a `String.Pos.Raw` takes constant time, while character positions need to be translated internally to byte positions, which ta...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Substring.Raw where /-- The underlying string. -/ str : String /-- The byte position of the start of the string slice. -/ startPos : String.Pos.Raw /-- The byte position of the end of the string slice. -/ stopPos : String.Pos.Raw
structure
Substring.Raw
Init
src/Init/Prelude.lean
[]
[ "String", "String.Pos.Raw" ]
A region or slice of some underlying string. A substring contains a string together with the start and end byte positions of a region of interest. Actually extracting a substring requires copying and memory allocation, while many substrings of the same underlying string may exist with very little overhead, and they ar...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Substring.Raw.bsize : Substring.Raw → Nat
| ⟨_, b, e⟩ => e.byteIdx.sub b.byteIdx
def
Substring.Raw.bsize
Init
src/Init/Prelude.lean
[]
[ "Nat", "Substring.Raw" ]
The number of bytes used by the string's UTF-8 encoding.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
String.utf8ByteSize (s : @& String) : Nat
s.toByteArray.size
def
String.utf8ByteSize
Init
src/Init/Prelude.lean
[]
[ "Nat", "String" ]
The number of bytes used by the string's UTF-8 encoding. At runtime, this function takes constant time because the byte length of strings is cached.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
String.rawEndPos (s : String) : String.Pos.Raw
where byteIdx := utf8ByteSize s
def
String.rawEndPos
Init
src/Init/Prelude.lean
[]
[ "String", "String.Pos.Raw" ]
A UTF-8 byte position that points at the end of a string, just after the last character. * `"abc".rawEndPos = ⟨3⟩` * `"L∃∀N".rawEndPos = ⟨8⟩`
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
String.toRawSubstring (s : String) : Substring.Raw
where str := s startPos := {} stopPos := s.rawEndPos
def
String.toRawSubstring
Init
src/Init/Prelude.lean
[]
[ "String", "Substring.Raw" ]
Converts a `String` into a `Substring` that denotes the entire string.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
String.toRawSubstring' (s : String) : Substring.Raw
s.toRawSubstring
def
String.toRawSubstring'
Init
src/Init/Prelude.lean
[]
[ "String", "Substring.Raw" ]
Converts a `String` into a `Substring` that denotes the entire string. This is a version of `String.toRawSubstring` that doesn't have an `@[inline]` annotation.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
unsafeCast {α : Sort u} {β : Sort v} (a : α) : β
PLift.down (ULift.down.{max u v} (cast lcProof (ULift.up.{max u v} (PLift.up a))))
def
unsafeCast
Init
src/Init/Prelude.lean
[]
[ "cast", "lcProof" ]
This function will cast a value of type `α` to type `β`, and is a no-op in the compiler. This function is **extremely dangerous** because there is no guarantee that types `α` and `β` have the same data representation, and this can lead to memory unsafety. It is also logically unsound, since you could just cast `True` t...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
panicCore {α : Sort u} [@&Inhabited α] (msg : String) : α
default
def
panicCore
Init
src/Init/Prelude.lean
[]
[ "Inhabited", "String" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
panic {α : Sort u} [Inhabited α] (msg : String) : α
panicCore msg
def
panic
Init
src/Init/Prelude.lean
[]
[ "Inhabited", "String", "panicCore" ]
`(panic "msg" : α)` has a built-in implementation which prints `msg` to the error buffer. It *does not* terminate execution, and because it is a safe function, it still has to return an element of `α`, so it takes `[Inhabited α]` and returns `default`. It is primarily intended for debugging in pure contexts, and assert...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Bind (m : Type u → Type v) where /-- Sequences two computations, allowing the second to depend on the value computed by the first. If `x : m α` and `f : α → m β`, then `x >>= f : m β` represents the result of executing `x` to get a value of type `α` and then passing it to `f`. -/ bind : {α β : Type u} → m ...
class
Bind
Init
src/Init/Prelude.lean
[]
[]
The `>>=` operator is overloaded via instances of `bind`. `Bind` is typically used via `Monad`, which extends it.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Pure (f : Type u → Type v) where /-- Given `a : α`, then `pure a : f α` represents an action that does nothing and returns `a`. Examples: * `(pure "hello" : Option String) = some "hello"` * `(pure "hello" : Except (Array String) String) = Except.ok "hello"` * `(pure "hello" : StateM Nat String).run 105 = (...
class
Pure
Init
src/Init/Prelude.lean
[]
[]
The `pure` function is overloaded via `Pure` instances. `Pure` is typically accessed via `Monad` or `Applicative` instances.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Functor (f : Type u → Type v) : Type (max (u+1) v) where /-- Applies a function inside a functor. This is used to overload the `<$>` operator. When mapping a constant function, use `Functor.mapConst` instead, because it may be more efficient. -/ map : {α β : Type u} → (α → β) → f α → f β /-- Mapping a ...
Function.comp map (Function.const _)
class
Functor
Init
src/Init/Prelude.lean
[]
[ "Function.comp", "Function.const" ]
A functor in the sense used in functional programming, which means a function `f : Type u → Type v` has a way of mapping a function over its contents. This `map` operator is written `<$>`, and overloaded via `Functor` instances. This `map` function should respect identity and function composition. In other words, for ...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Seq (f : Type u → Type v) : Type (max (u+1) v) where /-- The implementation of the `<*>` operator. In a monad, `mf <*> mx` is the same as `do let f ← mf; x ← mx; pure (f x)`: it evaluates the function first, then the argument, and applies one to the other. To avoid surprising evaluation semantics, `mx` is t...
class
Seq
Init
src/Init/Prelude.lean
[]
[ "Unit" ]
The `<*>` operator is overloaded using the function `Seq.seq`. While `<$>` from the class `Functor` allows an ordinary function to be mapped over its contents, `<*>` allows a function that's “inside” the functor to be applied. When thinking about `f` as possible side effects, this captures evaluation order: `seq` arra...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
SeqLeft (f : Type u → Type v) : Type (max (u+1) v) where /-- Sequences the effects of two terms, discarding the value of the second. This function is usually invoked via the `<*` operator. Given `x : f α` and `y : f β`, `x <* y` runs `x`, then runs `y`, and finally returns the result of `x`. The evaluatio...
class
SeqLeft
Init
src/Init/Prelude.lean
[]
[ "Unit" ]
The `<*` operator is overloaded using `seqLeft`. When thinking about `f` as potential side effects, `<*` evaluates first the left and then the right argument for their side effects, discarding the value of the right argument and returning the value of the left argument. For most applications, `Applicative` or `Monad`...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
SeqRight (f : Type u → Type v) : Type (max (u+1) v) where /-- Sequences the effects of two terms, discarding the value of the first. This function is usually invoked via the `*>` operator. Given `x : f α` and `y : f β`, `x *> y` runs `x`, then runs `y`, and finally returns the result of `y`. The evaluatio...
class
SeqRight
Init
src/Init/Prelude.lean
[]
[ "Unit" ]
The `*>` operator is overloaded using `seqRight`. When thinking about `f` as potential side effects, `*>` evaluates first the left and then the right argument for their side effects, discarding the value of the left argument and returning the value of the right argument. For most applications, `Applicative` or `Monad...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Applicative (f : Type u → Type v) extends Functor f, Pure f, Seq f, SeqLeft f, SeqRight f where map
fun x y => Seq.seq (pure x) fun _ => y seqLeft := fun a b => Seq.seq (Functor.map (Function.const _) a) b seqRight := fun a b => Seq.seq (Functor.map (Function.const _ id) a) b
class
Applicative
Init
src/Init/Prelude.lean
[]
[ "Function.const", "Functor", "Pure", "Seq", "SeqLeft", "SeqRight", "id" ]
An [applicative functor](lean-manual://section/monads-and-do) is more powerful than a `Functor`, but less powerful than a `Monad`. Applicative functors capture sequencing of effects with the `<*>` operator, overloaded as `seq`, but not data-dependent effects. The results of earlier computations cannot be used to contr...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Monad (m : Type u → Type v) : Type (max (u+1) v) extends Applicative m, Bind m where map f x
bind x (Function.comp pure f) seq f x := bind f fun y => Functor.map y (x ()) seqLeft x y := bind x fun a => bind (y ()) (fun _ => pure a) seqRight x y := bind x fun _ => y ()
class
Monad
Init
src/Init/Prelude.lean
[]
[ "Applicative", "Bind", "Function.comp" ]
[Monads](https://en.wikipedia.org/wiki/Monad_(functional_programming)) are an abstraction of sequential control flow and side effects used in functional programming. Monads allow both sequencing of effects and data-dependent effects: the values that result from an early step may influence the effects carried out in a l...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadLift (m : semiOutParam (Type u → Type v)) (n : Type u → Type w) where /-- Translates an action from monad `m` into monad `n`. -/ monadLift : {α : Type u} → m α → n α
class
MonadLift
Init
src/Init/Prelude.lean
[]
[ "semiOutParam" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadLiftT (m : Type u → Type v) (n : Type u → Type w) where /-- Translates an action from monad `m` into monad `n`. -/ monadLift : {α : Type u} → m α → n α
class
MonadLiftT
Init
src/Init/Prelude.lean
[]
[]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
liftM
@monadLift
abbrev
liftM
Init
src/Init/Prelude.lean
[]
[]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadEval (m : semiOutParam (Type u → Type v)) (n : Type u → Type w) where /-- Evaluates a value from monad `m` into monad `n`. -/ monadEval : {α : Type u} → m α → n α
class
MonadEval
Init
src/Init/Prelude.lean
[]
[ "semiOutParam" ]
Typeclass used for adapting monads. This is similar to `MonadLift`, but instances are allowed to make use of default state for the purpose of synthesizing such an instance, if necessary. Every `MonadLift` instance gives a `MonadEval` instance. The purpose of this class is for the `#eval` command, which looks for a `Mo...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadEvalT (m : Type u → Type v) (n : Type u → Type w) where /-- Evaluates a value from monad `m` into monad `n`. -/ monadEval : {α : Type u} → m α → n α
class
MonadEvalT
Init
src/Init/Prelude.lean
[]
[]
The transitive closure of `MonadEval`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadFunctor (m : semiOutParam (Type u → Type v)) (n : Type u → Type w) where /-- Lifts a fully-polymorphic transformation of `m` into `n`. -/ monadMap {α : Type u} : ({β : Type u} → m β → m β) → n α → n α
class
MonadFunctor
Init
src/Init/Prelude.lean
[]
[ "semiOutParam" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadFunctorT (m : Type u → Type v) (n : Type u → Type w) where /-- Lifts a fully-polymorphic transformation of `m` into `n`. -/ monadMap {α : Type u} : ({β : Type u} → m β → m β) → n α → n α
class
MonadFunctorT
Init
src/Init/Prelude.lean
[]
[]
A way to interpret a fully-polymorphic function in `m` into `n`. Such a function can be thought of as one that may change the effects in `m`, but can't do so based on specific values that are provided. This is the reflexive, transitive closure of `MonadFunctor`. It automatically chains together `MonadFunctor` instance...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
monadFunctorRefl (m) : MonadFunctorT m m
where monadMap f := f
instance
monadFunctorRefl
Init
src/Init/Prelude.lean
[]
[ "MonadFunctorT" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Except (ε : Type u) (α : Type v) where /-- A failure value of type `ε` -/ | error : ε → Except ε α /-- A success value of type `α` -/ | ok : α → Except ε α
inductive
Except
Init
src/Init/Prelude.lean
[]
[]
`Except ε α` is a type which represents either an error of type `ε` or a successful result with a value of type `α`. `Except ε : Type u → Type v` is a `Monad` that represents computations that may throw exceptions: the `pure` operation is `Except.ok` and the `bind` operation returns the first encountered `Except.error...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadExceptOf (ε : semiOutParam (Type u)) (m : Type v → Type w) where /-- Throws an exception of type `ε` to the nearest enclosing `catch`. -/ throw {α : Type v} : ε → m α /-- Catches errors thrown in `body`, passing them to `handler`. Errors in `handler` are not caught. -/ tryCatch {α : Type v} (body :...
class
MonadExceptOf
Init
src/Init/Prelude.lean
[]
[ "semiOutParam" ]
Exception monads provide the ability to throw errors and handle errors. In this class, `ε` is a `semiOutParam`, which means that it can influence the choice of instance. `MonadExcept ε` provides the same operations, but requires that `ε` be inferable from `m`. `tryCatchThe`, which takes an explicit exception type, is...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
throwThe (ε : Type u) {m : Type v → Type w} [MonadExceptOf ε m] {α : Type v} (e : ε) : m α
MonadExceptOf.throw e
abbrev
throwThe
Init
src/Init/Prelude.lean
[]
[ "MonadExceptOf" ]
Throws an exception, with the exception type specified explicitly. This is useful when a monad supports throwing more than one type of exception. Use `throw` for a version that expects the exception type to be inferred from `m`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
tryCatchThe (ε : Type u) {m : Type v → Type w} [MonadExceptOf ε m] {α : Type v} (x : m α) (handle : ε → m α) : m α
MonadExceptOf.tryCatch x handle
abbrev
tryCatchThe
Init
src/Init/Prelude.lean
[]
[ "MonadExceptOf" ]
Catches errors, recovering using `handle`. The exception type is specified explicitly. This is useful when a monad supports throwing or handling more than one type of exception. Use `tryCatch`, for a version that expects the exception type to be inferred from `m`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadExcept (ε : outParam (Type u)) (m : Type v → Type w) where /-- Throws an exception of type `ε` to the nearest enclosing handler. -/ throw {α : Type v} : ε → m α /-- Catches errors thrown in `body`, passing them to `handler`. Errors in `handler` are not caught. -/ tryCatch {α : Type v} : (body : m α...
class
MonadExcept
Init
src/Init/Prelude.lean
[]
[ "outParam" ]
Exception monads provide the ability to throw errors and handle errors. In this class, `ε` is an `outParam`, which means that it is inferred from `m`. `MonadExceptOf ε` provides the same operations, but allows `ε` to influence instance synthesis. `MonadExcept.tryCatch` is used to desugar `try ... catch ...` steps ins...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadExcept.ofExcept [Monad m] [MonadExcept ε m] : Except ε α → m α
| .ok a => pure a | .error e => throw e
def
MonadExcept.ofExcept
Init
src/Init/Prelude.lean
[]
[ "Except", "Monad", "MonadExcept" ]
Re-interprets an `Except ε` action in an exception monad `m`, succeeding if it succeeds and throwing an exception if it throws an exception.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
orElse [MonadExcept ε m] {α : Type v} (t₁ : m α) (t₂ : Unit → m α) : m α
tryCatch t₁ fun _ => t₂ ()
def
MonadExcept.orElse
Init
src/Init/Prelude.lean
[]
[ "MonadExcept", "Unit" ]
Unconditional error recovery that ignores which exception was thrown. Usually used via the `<|>` operator. If both computations throw exceptions, then the result is the second exception.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
ReaderT (ρ : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v)
(a : @&ρ) → m α
def
ReaderT
Init
src/Init/Prelude.lean
[]
[]
Adds the ability to access a read-only value of type `ρ` to a monad. The value can be locally overridden by `withReader`, but it cannot be mutated. Actions in the resulting monad are functions that take the local value as a parameter, returning ordinary actions in `m`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
ReaderT.mk {ρ : Type u} {m : Type u → Type v} {α : Type u} (x : ρ → m α) : ReaderT ρ m α
x
def
ReaderT.mk
Init
src/Init/Prelude.lean
[]
[ "ReaderT" ]
Interpret `ρ → m α` as an element of `ReaderT ρ m α`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
ReaderT.run {ρ : Type u} {m : Type u → Type v} {α : Type u} (x : ReaderT ρ m α) (r : ρ) : m α
x r
def
ReaderT.run
Init
src/Init/Prelude.lean
[]
[ "ReaderT" ]
Executes an action from a monad with a read-only value in the underlying monad `m`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
read [Monad m] : ReaderT ρ m ρ
pure
def
ReaderT.read
Init
src/Init/Prelude.lean
[]
[ "Monad", "ReaderT" ]
Retrieves the reader monad's local value. Typically accessed via `read`, or via `readThe` when more than one local value is available.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
pure [Monad m] {α} (a : α) : ReaderT ρ m α
fun _ => pure a
def
ReaderT.pure
Init
src/Init/Prelude.lean
[]
[ "Monad", "ReaderT" ]
Returns the provided value `a`, ignoring the reader monad's local value. Typically used via `Pure.pure`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
bind [Monad m] {α β} (x : ReaderT ρ m α) (f : α → ReaderT ρ m β) : ReaderT ρ m β
fun r => bind (x r) fun a => f a r
def
ReaderT.bind
Init
src/Init/Prelude.lean
[]
[ "Monad", "ReaderT" ]
Sequences two reader monad computations. Both are provided with the local value, and the second is passed the value of the first. Typically used via the `>>=` operator.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
adapt {ρ' α : Type u} (f : ρ' → ρ) : ReaderT ρ m α → ReaderT ρ' m α
fun x r => x (f r)
def
ReaderT.adapt
Init
src/Init/Prelude.lean
[]
[ "ReaderT" ]
Modifies a reader monad's local value with `f`. The resulting computation applies `f` to the incoming local value and passes the result to the inner computation.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadReaderOf (ρ : semiOutParam (Type u)) (m : Type u → Type v) where /-- Retrieves the local value. -/ read : m ρ
class
MonadReaderOf
Init
src/Init/Prelude.lean
[]
[ "semiOutParam" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadReader (ρ : outParam (Type u)) (m : Type u → Type v) where /-- Retrieves the local value. Use `readThe` to explicitly specify a type when more than one value is available. -/ read : m ρ
class
MonadReader
Init
src/Init/Prelude.lean
[]
[ "outParam" ]
Reader monads provide the ability to implicitly thread a value through a computation. The value can be read, but not written. A `MonadWithReader ρ` instance additionally allows the value to be locally overridden for a sub-computation. In this class, `ρ` is an `outParam`, which means that it is inferred from `m`. `Mona...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
readThe (ρ : Type u) {m : Type u → Type v} [MonadReaderOf ρ m] : m ρ
MonadReaderOf.read
def
readThe
Init
src/Init/Prelude.lean
[]
[ "MonadReaderOf" ]
Retrieves the local value whose type is `ρ`. This is useful when a monad supports reading more than one type of value. Use `read` for a version that expects the type `ρ` to be inferred from `m`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadWithReaderOf (ρ : semiOutParam (Type u)) (m : Type u → Type v) where /-- Locally modifies the reader monad's value while running an action. During the inner action `x`, reading the value returns `f` applied to the original value. After control returns from `x`, the reader monad's value is restored. -/ ...
class
MonadWithReaderOf
Init
src/Init/Prelude.lean
[]
[ "semiOutParam" ]
A reader monad that additionally allows the value to be locally overridden. In this class, `ρ` is a `semiOutParam`, which means that it can influence the choice of instance. `MonadWithReader ρ` provides the same operations, but requires that `ρ` be inferable from `m`.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
withTheReader (ρ : Type u) {m : Type u → Type v} [MonadWithReaderOf ρ m] {α : Type u} (f : ρ → ρ) (x : m α) : m α
MonadWithReaderOf.withReader f x
def
withTheReader
Init
src/Init/Prelude.lean
[]
[ "MonadWithReaderOf" ]
Locally modifies the reader monad's value while running an action, with the reader monad's local value type specified explicitly. This is useful when a monad supports reading more than one type of value. During the inner action `x`, reading the value returns `f` applied to the original value. After control returns fro...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadWithReader (ρ : outParam (Type u)) (m : Type u → Type v) where /-- Locally modifies the reader monad's value while running an action. During the inner action `x`, reading the value returns `f` applied to the original value. After control returns from `x`, the reader monad's value is restored. -/ withR...
class
MonadWithReader
Init
src/Init/Prelude.lean
[]
[ "outParam" ]
A reader monad that additionally allows the value to be locally overridden. In this class, `ρ` is an `outParam`, which means that it is inferred from `m`. `MonadWithReaderOf ρ` provides the same operations, but allows `ρ` to influence instance synthesis.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadStateOf (σ : semiOutParam (Type u)) (m : Type u → Type v) where /-- Retrieves the current value of the monad's mutable state. -/ get : m σ /-- Replaces the current value of the mutable state with a new one. -/ set : σ → m PUnit /-- Applies a function to the current state that both computes a ne...
class
MonadStateOf
Init
src/Init/Prelude.lean
[]
[ "PUnit", "Prod", "semiOutParam" ]
State monads provide a value of a given type (the _state_) that can be retrieved or replaced. Instances may implement these operations by passing state values around, by using a mutable reference cell (e.g. `ST.Ref σ`), or in other ways. In this class, `σ` is a `semiOutParam`, which means that it can influence the cho...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
getThe (σ : Type u) {m : Type u → Type v} [MonadStateOf σ m] : m σ
MonadStateOf.get
abbrev
getThe
Init
src/Init/Prelude.lean
[]
[ "MonadStateOf" ]
Gets the current state that has the explicitly-provided type `σ`. When the current monad has multiple state types available, this function selects one of them.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
modifyThe (σ : Type u) {m : Type u → Type v} [MonadStateOf σ m] (f : σ → σ) : m PUnit
MonadStateOf.modifyGet fun s => (PUnit.unit, f s)
abbrev
modifyThe
Init
src/Init/Prelude.lean
[]
[ "MonadStateOf", "PUnit" ]
Mutates the current state that has the explicitly-provided type `σ`, replacing its value with the result of applying `f` to it. When the current monad has multiple state types available, this function selects one of them. It is equivalent to `do set (f (← get))`. However, using `modify` may lead to higher performance ...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
modifyGetThe {α : Type u} (σ : Type u) {m : Type u → Type v} [MonadStateOf σ m] (f : σ → Prod α σ) : m α
MonadStateOf.modifyGet f
abbrev
modifyGetThe
Init
src/Init/Prelude.lean
[]
[ "MonadStateOf", "Prod" ]
Applies a function to the current state that has the explicitly-provided type `σ`. The function both computes a new state and a value. The new state replaces the current state, and the value is returned. It is equivalent to `do let (a, s) := f (← getThe σ); set s; pure a`. However, using `modifyGetThe` may lead to hig...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
MonadState (σ : outParam (Type u)) (m : Type u → Type v) where /-- Retrieves the current value of the monad's mutable state. -/ get : m σ /-- Replaces the current value of the mutable state with a new one. -/ set : σ → m PUnit /-- Applies a function to the current state that both computes a new stat...
class
MonadState
Init
src/Init/Prelude.lean
[]
[ "PUnit", "Prod", "outParam" ]
State monads provide a value of a given type (the _state_) that can be retrieved or replaced. Instances may implement these operations by passing state values around, by using a mutable reference cell (e.g. `ST.Ref σ`), or in other ways. In this class, `σ` is an `outParam`, which means that it is inferred from `m`. `M...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
modify {σ : Type u} {m : Type u → Type v} [MonadState σ m] (f : σ → σ) : m PUnit
modifyGet fun s => (PUnit.unit, f s)
def
modify
Init
src/Init/Prelude.lean
[]
[ "MonadState", "PUnit" ]
Mutates the current state, replacing its value with the result of applying `f` to it. Use `modifyThe` to explicitly select a state type to modify. It is equivalent to `do set (f (← get))`. However, using `modify` may lead to higher performance because it doesn't add a new reference to the state value. Additional refe...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
getModify {σ : Type u} {m : Type u → Type v} [MonadState σ m] (f : σ → σ) : m σ
modifyGet fun s => (s, f s)
def
getModify
Init
src/Init/Prelude.lean
[]
[ "MonadState" ]
Replaces the state with the result of applying `f` to it. Returns the old value of the state. It is equivalent to `get <* modify f` but may be more efficient.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Result (ε σ α : Type u) where /-- A success value of type `α` and a new state `σ`. -/ | ok : α → σ → Result ε σ α /-- An exception of type `ε` and a new state `σ`. -/ | error : ε → σ → Result ε σ α
inductive
EStateM.Result
Init
src/Init/Prelude.lean
[]
[]
The value returned from a combined state and exception monad in which exceptions do not automatically roll back the state. `Result ε σ α` is equivalent to `Except ε α × σ`, but using a single combined inductive type yields a more efficient data representation.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
EStateM (ε σ α : Type u)
σ → Result ε σ α
def
EStateM
Init
src/Init/Prelude.lean
[]
[]
A combined state and exception monad in which exceptions do not automatically roll back the state. Instances of `EStateM.Backtrackable` provide a way to roll back some part of the state if needed. `EStateM ε σ` is equivalent to `ExceptT ε (StateM σ)`, but it is more efficient.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
pure (a : α) : EStateM ε σ α
fun s => Result.ok a s
def
EStateM.pure
Init
src/Init/Prelude.lean
[]
[ "EStateM" ]
Returns a value without modifying the state or throwing an exception.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
set (s : σ) : EStateM ε σ PUnit
fun _ => Result.ok ⟨⟩ s
def
EStateM.set
Init
src/Init/Prelude.lean
[]
[ "EStateM", "PUnit" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
get : EStateM ε σ σ
fun s => Result.ok s s
def
EStateM.get
Init
src/Init/Prelude.lean
[]
[ "EStateM" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
modifyGet (f : σ → Prod α σ) : EStateM ε σ α
fun s => match f s with | (a, s) => Result.ok a s
def
EStateM.modifyGet
Init
src/Init/Prelude.lean
[]
[ "EStateM", "Prod" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
throw (e : ε) : EStateM ε σ α
fun s => Result.error e s
def
EStateM.throw
Init
src/Init/Prelude.lean
[]
[ "EStateM" ]
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
Backtrackable (δ : outParam (Type u)) (σ : Type u) where /-- Extracts the information in the state that should be rolled back if an exception is handled. -/ save : σ → δ /-- Updates the current state with the saved information that should be rolled back. This updated state becomes the current state whe...
class
EStateM.Backtrackable
Init
src/Init/Prelude.lean
[]
[ "outParam" ]
Exception handlers in `EStateM` save some part of the state, determined by `δ`, and restore it if an exception is caught. By default, `δ` is `Unit`, and no information is saved.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
tryCatch {δ} [Backtrackable δ σ] {α} (x : EStateM ε σ α) (handle : ε → EStateM ε σ α) : EStateM ε σ α
fun s => let d := Backtrackable.save s match x s with | Result.error e s => handle e (Backtrackable.restore s d) | ok => ok
def
EStateM.tryCatch
Init
src/Init/Prelude.lean
[]
[ "EStateM" ]
Handles exceptions thrown in the combined error and state monad. The `Backtrackable δ σ` instance is used to save a snapshot of part of the state prior to running `x`. If an exception is caught, the state is updated with the saved snapshot, rolling back part of the state. If no instance of `Backtrackable` is provided,...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
orElse {δ} [Backtrackable δ σ] (x₁ : EStateM ε σ α) (x₂ : Unit → EStateM ε σ α) : EStateM ε σ α
fun s => let d := Backtrackable.save s; match x₁ s with | Result.error _ s => x₂ () (Backtrackable.restore s d) | ok => ok
def
EStateM.orElse
Init
src/Init/Prelude.lean
[]
[ "EStateM", "Unit" ]
Failure handling that does not depend on specific exception values. The `Backtrackable δ σ` instance is used to save a snapshot of part of the state prior to running `x₁`. If an exception is caught, the state is updated with the saved snapshot, rolling back part of the state. If no instance of `Backtrackable` is provi...
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6
adaptExcept {ε' : Type u} (f : ε → ε') (x : EStateM ε σ α) : EStateM ε' σ α
fun s => match x s with | Result.error e s => Result.error (f e) s | Result.ok a s => Result.ok a s
def
EStateM.adaptExcept
Init
src/Init/Prelude.lean
[]
[ "EStateM" ]
Transforms exceptions with a function, doing nothing on successful results.
https://github.com/leanprover/lean4
d265d1ca745e7741a7e7f7366c22ce9c9dda57b6