Equations
- outOfBounds = panicWithPosWithDecl "Init.GetElem" "outOfBounds" 15 2 "index out of bounds"
Instances For
The classes GetElem and GetElem? implement lookup notation,
specifically xs[i], xs[i]?, xs[i]!, and xs[i]'p.
Both classes are indexed by types coll, idx, and elem which are
the collection, the index, and the element types.
A single collection may support lookups with multiple index
types. The relation valid determines when the index is guaranteed to be
valid; lookups of valid indices are guaranteed not to fail.
For example, an instance for arrays looks like
GetElem (Array α) Nat α (fun xs i => i < xs.size). In other words, given an
array xs and a natural number i, xs[i] will return an α when valid xs i
holds, which is true when i is less than the size of the array. Array
additionally supports indexing with USize instead of Nat.
In either case, because the bounds are checked at compile time,
no runtime check is required.
Given xs[i] with xs : coll and i : idx, Lean looks for an instance of
GetElem coll idx elem valid and uses this to infer the type of the return
value elem and side condition valid required to ensure xs[i] yields
a valid value of type elem. The tactic get_elem_tactic is
invoked to prove validity automatically. The xs[i]'p notation uses the
proof p to satisfy the validity condition.
If the proof p is long, it is often easier to place the
proof in the context using have, because get_elem_tactic tries
assumption.
The proof side-condition valid xs i is automatically dispatched by the
get_elem_tactic tactic; this tactic can be extended by adding more clauses to
get_elem_tactic_trivial using macro_rules.
xs[i]? and xs[i]! do not impose a proof obligation; the former returns
an Option elem, with none signalling that the value isn't present, and
the latter returns elem but panics if the value isn't there, returning
default : elem based on the Inhabited elem instance.
These are provided by the GetElem? class, for which there is a default instance
generated from a GetElem class as long as valid xs i is always decidable.
Important instances include:
arr[i] : αwherearr : Array αandi : Natori : USize: does array indexing with no runtime bounds check and a proof side goali < arr.size.l[i] : αwherel : List αandi : Nat: index into a list, with proof side goali < l.length.
- getElem (xs : coll) (i : idx) (h : valid xs i) : elem
The syntax
arr[i]gets thei'th element of the collectionarr. If there are proof side conditions to the application, they will be automatically inferred by theget_elem_tactictactic.Conventions for notations in identifiers:
Instances
The syntax arr[i] gets the i'th element of the collection arr. If there
are proof side conditions to the application, they will be automatically
inferred by the get_elem_tactic tactic.
Conventions for notations in identifiers:
- The recommended spelling of
xs[i]in identifiers isgetElem.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The syntax arr[i] gets the i'th element of the collection arr. If there
are proof side conditions to the application, they will be automatically
inferred by the get_elem_tactic tactic.
Conventions for notations in identifiers:
- The recommended spelling of
xs[i]'hin identifiers isgetElem.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Helper function for implementation of GetElem?.getElem?.
Instances For
The classes GetElem and GetElem? implement lookup notation,
specifically xs[i], xs[i]?, xs[i]!, and xs[i]'p.
Both classes are indexed by types coll, idx, and elem which are
the collection, the index, and the element types.
A single collection may support lookups with multiple index
types. The relation valid determines when the index is guaranteed to be
valid; lookups of valid indices are guaranteed not to fail.
For example, an instance for arrays looks like
GetElem (Array α) Nat α (fun xs i => i < xs.size). In other words, given an
array xs and a natural number i, xs[i] will return an α when valid xs i
holds, which is true when i is less than the size of the array. Array
additionally supports indexing with USize instead of Nat.
In either case, because the bounds are checked at compile time,
no runtime check is required.
Given xs[i] with xs : coll and i : idx, Lean looks for an instance of
GetElem coll idx elem valid and uses this to infer the type of the return
value elem and side condition valid required to ensure xs[i] yields
a valid value of type elem. The tactic get_elem_tactic is
invoked to prove validity automatically. The xs[i]'p notation uses the
proof p to satisfy the validity condition.
If the proof p is long, it is often easier to place the
proof in the context using have, because get_elem_tactic tries
assumption.
The proof side-condition valid xs i is automatically dispatched by the
get_elem_tactic tactic; this tactic can be extended by adding more clauses to
get_elem_tactic_trivial using macro_rules.
xs[i]? and xs[i]! do not impose a proof obligation; the former returns
an Option elem, with none signalling that the value isn't present, and
the latter returns elem but panics if the value isn't there, returning
default : elem based on the Inhabited elem instance.
These are provided by the GetElem? class, for which there is a default instance
generated from a GetElem class as long as valid xs i is always decidable.
Important instances include:
arr[i] : αwherearr : Array αandi : Natori : USize: does array indexing with no runtime bounds check and a proof side goali < arr.size.l[i] : αwherel : List αandi : Nat: index into a list, with proof side goali < l.length.
- getElem? : coll → idx → Option elem
The syntax
arr[i]?gets thei'th element of the collectionarr, if it is present (and wraps it insome), and otherwise returnsnone.Conventions for notations in identifiers:
- The recommended spelling of
xs[i]?in identifiers isgetElem?.
- The recommended spelling of
Instances
The syntax arr[i]? gets the i'th element of the collection arr or
returns none if i is out of bounds.
Conventions for notations in identifiers:
- The recommended spelling of
xs[i]?in identifiers isgetElem?.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The syntax arr[i]! gets the i'th element of the collection arr and
panics i is out of bounds.
Conventions for notations in identifiers:
- The recommended spelling of
xs[i]!in identifiers isgetElem!.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- instGetElem?OfGetElemOfDecidable = { toGetElem := inst✝¹, getElem? := fun (xs : coll) (i : idx) => decidableGetElem? xs i }
Lawful GetElem? instances (which extend GetElem) are those for which the potentially-failing
GetElem?.getElem? and GetElem?.getElem! operators succeed when the validity predicate is
satisfied, and fail when it is not.
- getElem?_def (c : cont) (i : idx) [Decidable (dom c i)] : c[i]? = if h : dom c i then some c[i] else none
GetElem?.getElem?succeeds when the validity predicate is satisfied and fails otherwise. - getElem!_def [Inhabited elem] (c : cont) (i : idx) : c[i]! = match c[i]? with | some e => e | none => default
GetElem?.getElem!succeeds and fails whenGetElem.getElem?succeeds and fails.
Instances
Equations
Instances For
Equations
Instances For
getElem? #
This instance overrides the default implementation of a[i]? via decidableGetElem?,
giving better definitional equalities.
Equations
- One or more equations did not get rendered due to their size.