为什么 Idris 中的 Nat 数据类型以 0 而不是 1 开头?

Why Nat data type in Idris starts with 0 and not 1?

Total Idris newbie question, sorry.

I've been taught in school there are natural numbers (N) and natural numbers with zero (N0).
In Idris, there is data type Nat which corresponds to N0 by definition.

What would change if there will be Nat defined as follows:

data Nat = One | S Nat
data Nat0 = Zero | Nat

I guess it's easier now, but is it mainly compiler implementation issue or formal one?

There must be cases where 0 doesn't make sense and I guess these are more complicated to define correctly now.
Or not?

我见过它用两种方式定义,但我们更喜欢在 Idris 中从零开始的一个原因是它对于描述其他结构的大小很有用,例如列表或树。列表中可以有零个东西,树的高度可以为零,附加两个零长度列表会导致一个零长度列表。

使用您的定义会很好,只是在使用 Nat 讨论其他结构的属性时不必要地繁琐。

在零没有意义的情况下,您可以定义您的数据类型,使其不可能将零作为索引。这是一个(人为的和未经测试的)示例,其中零没有意义,树按元素数量索引,元素存储在叶子中:

data Tree : Nat -> Type -> Type where
     Leaf : ty -> Tree 1 ty
     Node : Tree n ty -> Tree m ty -> Tree (n + m) ty

鉴于这些构造函数,您将永远无法创建 Tree 0 Int,但您将能够创建 Tree 1 IntTree 2 IntTree n Int 对于任何 n > 0 罚款...

test1 : Tree 1 Int
test1 = Leaf 94

test2 : Tree 2 Int
test2 = Node test1 test1

test3 : Tree 3 Int
test3 = Node test1 test2

test0 : Tree 0 Int
test0 = ?no_chance