Documentation Index
Fetch the complete documentation index at: https://mintlify.com/paulch42/lean-spec/llms.txt
Use this file to discover all available pages before exploring further.
LeanSpec.lib.Util provides the generic types and functions that underpin the tutorial examples throughout lean-spec. The definitions here are foundational building blocks — range-constrained strings and numbers, ordering predicates for lists, finite sets with the standard set algebra, and finite maps supporting key-based lookup. Items are included only when needed by the examples, and most instances requiring a proof obligation are left as sorry to keep the focus squarely on specification rather than proof or implementation detail.
Restricted Basic Types
These subtypes carve out well-behaved subsets ofString, Nat, and Int and equip them with the typeclass instances — equality, ordering — needed for use in specifications.
Str m n
A length-constrained string whose character count lies in the closed interval [m, n].
s : Str m n carries the proof m ≤ s.val.length ∧ s.val.length ≤ n as part of its type, so any attempt to construct an out-of-range string is rejected at the type level.
DecidableEq instance — equality on Str m n is decidable:
LT instance — the strict order on Str m n is inherited from the underlying String order:
< — the decision procedure delegates to the underlying string comparison:
NatMN m n
A natural number constrained to the half-open interval [m, n). The lower bound is inclusive and the upper bound is exclusive.
DecidableEq instance:
NatMN is used extensively in Geo to represent degree values — for example, bearings in [0°, 360°).
IntMN m n
A signed integer constrained to the half-open interval [m, n). The lower bound is inclusive and the upper bound is exclusive.
DecidableEq instance:
IntMN is used in Geo to represent latitude and longitude expressed in arc-seconds, where negative values are South or West respectively.
List Utilities
TheList namespace is extended with aggregation functions and order-checking predicates. Each function is polymorphic over the element type and requires only the minimal typeclass constraint.
List.add
The sum of the elements of a list. An identity value must be supplied for the empty case.
List.mul
The product of the elements of a list. An identity value must be supplied for the empty case.
List.max
The maximum of the elements of a list. A default value is supplied for the empty case.
List.min
The minimum of the elements of a list. A default value is supplied for the empty case.
List.minimumD
The minimum element of a list, returning a caller-supplied default for the empty list. Delegates to List.minimum? internally.
List.ascending
A proposition stating that the elements of a list are in non-strictly ascending (i.e. non-decreasing) order.
ascending.
List.ascendingStrict
A proposition stating that the elements of a list are in strictly ascending order (no two adjacent elements are equal).
List.descending
A proposition stating that the elements of a list are in non-strictly descending (i.e. non-increasing) order.
List.descendingStrict
A proposition stating that the elements of a list are in strictly descending order.
List.toSet
Converts a list to a Set by removing duplicate elements using eraseDup.
Finite Sets
Set A is the type of finite sets of elements drawn from A. Sets are represented internally as duplicate-free lists; all operations must be order-agnostic so that two sets with the same elements are considered equal regardless of the order in which their backing lists happen to be stored.
Type definition
Nodup predicate guarantees no element appears more than once.
Standard typeclass instances
| Instance | Notation | Meaning |
|---|---|---|
EmptyCollection | ∅ | The empty set |
Membership | a ∈ s | Element membership |
HasSubset | s₁ ⊆ s₂ | Every element of s₁ is in s₂ |
HasSSubset | s₁ ⊊ s₂ | s₁ ⊆ s₂ and some element of s₁ is absent from s₂ |
Union | s₁ ∪ s₂ | Elements in either set (deduped) |
Inter | s₁ ∩ s₂ | Elements in both sets |
SDiff | s₁ \ s₂ | Elements in s₁ not in s₂ |
Insert | insert a s | Add an element, preserving Nodup |
Singleton | {a} | A one-element set |
DecidableEq | — | Equality is decidable |
Sep | { a ∈ A | P a } | Set comprehension via filterp |
Set.card
The cardinality (number of elements) of a set.
Set.map
Apply a function to every element, deduplicating the image. The result may have fewer elements than the input if the function is not injective.
Set.filter
Retain elements satisfying a boolean predicate.
Set.filterp
Retain elements satisfying a propositional predicate. This is the backing function for the Sep instance, enabling { a ∈ A | P a } notation.
Set.foldr
Fold a binary function over the elements of a set. Because sets are unordered, the supplied function should be commutative and associative for the result to be well-defined.
Set.select
Extract the unique element from a singleton set. Requires a proof that card s = 1.
Set.minimumD
The minimum element of a set, with a caller-supplied default for the empty set. Delegates to List.minimumD on the backing list.
Set.add
The sum of all elements in the set. An identity value must be provided for the empty case.
Set.add'
The sum of the images of elements under a function f. An identity value is provided for the empty case.
Finite Maps
α ⟹ β is the type of finite maps from keys of type α to values of type β. It uses the infix operator ⟹ (precedence 34, just below ×) and is defined within the Std namespace.
Type definition
AMap.Entry pairs a key with a value:
Map is a Set of entries in which all keys are distinct — enforced by requiring the cardinality of the entry set equals the cardinality of the set of projected keys:
Standard typeclass instances
| Instance | Notation | Meaning |
|---|---|---|
DecidableEq | — | Equality is decidable |
EmptyCollection | ∅ | The empty map |
Union | m₁ ∪ m₂ | Merge; m₁ takes precedence on duplicate keys |
Inter | m₁ ∩ m₂ | Keys present in both; values drawn from m₁ |
Membership | entry ∈ m | Full entry membership (key and value must match) |
Map.contains
Test whether a map contains a given key (regardless of value).
Map.erase
Remove the entry with the given key from a map.
Map.insert
Add a key-value pair to a map.
Map.findEntry?
Find the full entry (key + value) for a given key, returning none if the key is absent.
Map.findEntry
Find the full entry for a given key, given a proof h : m.contains a that the key is present.
Map.find?
Look up the value associated with a key, returning none if the key is absent.
Map.find
Look up the value associated with a key, given a proof that the key is present. Implemented via findEntry.
Map.isEmpty
Test whether a map has no entries.
Map.filter
Retain only those entries whose key satisfies a boolean predicate.
Map.domain
The domain of a finite map — the set of all keys. Guaranteed duplicate-free by the Set representation.
Map.range
The range (image) of a finite map — the set of all values.
Option Utilities
Two convenience notations are provided in theStd namespace to reduce verbosity when handling Option values in specifications.
▹ … ‖ … — full match notation
Captures the common pattern of branching on none vs some:
t ▹ n ‖ s returns n when t is none, and applies s to the unwrapped value when t is some x.
▹ … — optional predicate notation
A special case that defaults to True when the option is empty:
t ▹ p holds if t is none (no constraint imposed) or if p x holds for the wrapped value x. This notation is widely used in the tutorial to express optional constraints: “if the field is present, it must satisfy P.”