Std.Nat
View source →Peano natural numbers for type-level computation.
Nat is intentionally small: it exists so indexed stdlib types can use a
trusted, total unary natural-number index.
Types
-
type Nat = Z | S
Functions
-
# fn of_int(i: Int) -> Nat extern
Cast an
Intto aNat, clamping negatives toZ(==max(i, 0)). Even thoughIntis now inductive, it compiles to a native BEAM machine integer, so this cast is kept as an asserted FFI boundary against that native representation — exactly where Idris marksintegerToNattotal. It is the ONLY trusted step behind integer ranges; the enumeration itself (range_from) is genuine structural recursion. -
# fn plus(m: Nat, n: Nat) -> Nat
-
# fn range_from(start: Int, count: Nat) -> List(Int)
Build
[start, start + 1, …, start + count - 1], driven by a structuralNatfuel. The recursion is oncount, so it is certified terminating. -
# fn range_upto(from: Int, to: Int) -> List(Int)
Exclusive integer range
a..b=[a, …, b - 1]; empty whena >= b(of_intclamps the negative gap toZ). The surfacea..bdesugars here. -
# fn range_upto_incl(from: Int, to: Int) -> List(Int)
Inclusive integer range
a..=b=[a, …, b]. The surfacea..=bdesugars here. -
# fn to_integer(value: Nat) -> Int
Project a natural number to a machine integer (structural, total; the constructive inverse of the trusted
of_intclamp — this direction needs no assertion becauseNatis well-founded).