如何在 coq 中定义从 naturals 到 naturals 的分段函数
how to define piecewise function from naturals to naturals in coq
我如何在 Coq 中定义一个像 f(x)=0 if x<5 否则 f(x)=1 的函数?
如果我在 OСaml 中做类似的事情,
Definition test (i:nat):nat :=
if i < 5 then 0 else 1.
它抱怨
Error: The term i < 5
has type Prop
which is not a (co-)inductive type.
您需要使用比较的 可判定 版本(i < 5
是逻辑或命题版本,您无法计算)。这是在标准库中:
Require Import Arith.
Check lt_dec.
Definition test (i:nat) : nat := if lt_dec i 5 then 0 else 1.
标准库对小于 returns 的测试不是一个布尔值,而是一个 sumbool,它包括两种情况下的证明,告诉您函数的作用(这些证明在您的示例中未使用,但会是如果你想证明一些关于 test
的东西,这很方便)。您在 lt_dec
的类型中看到的 {n < m} + {~n < m}
类型是 sumbool (n < m) (~n < m)
.
的符号
如果您不关心证明,那么您可以使用不同的函数 Nat.ltb
,即 returns 布尔值。标准库也包含此函数的方便符号:
Require Import Arith.
Definition test (i:nat) : nat := if i <? 5 then 0 else 1.
当你在证明中使用这个时,你需要应用像 Nat.ltb_lt
这样的定理来推理 i <? 5
returns.
请注意,Coq 中的 if b then .. else ...
支持 b
作为 bool 或 sumbool。事实上,它支持具有两个构造函数的 any 归纳类型,第一个构造函数使用 then
分支,第二个构造函数使用 else
分支; bool
和 sumbool
的定义小心地命令它们的构造函数使 if
语句按预期运行。
我如何在 Coq 中定义一个像 f(x)=0 if x<5 否则 f(x)=1 的函数?
如果我在 OСaml 中做类似的事情,
Definition test (i:nat):nat :=
if i < 5 then 0 else 1.
它抱怨
Error: The term
i < 5
has typeProp
which is not a (co-)inductive type.
您需要使用比较的 可判定 版本(i < 5
是逻辑或命题版本,您无法计算)。这是在标准库中:
Require Import Arith.
Check lt_dec.
Definition test (i:nat) : nat := if lt_dec i 5 then 0 else 1.
标准库对小于 returns 的测试不是一个布尔值,而是一个 sumbool,它包括两种情况下的证明,告诉您函数的作用(这些证明在您的示例中未使用,但会是如果你想证明一些关于 test
的东西,这很方便)。您在 lt_dec
的类型中看到的 {n < m} + {~n < m}
类型是 sumbool (n < m) (~n < m)
.
如果您不关心证明,那么您可以使用不同的函数 Nat.ltb
,即 returns 布尔值。标准库也包含此函数的方便符号:
Require Import Arith.
Definition test (i:nat) : nat := if i <? 5 then 0 else 1.
当你在证明中使用这个时,你需要应用像 Nat.ltb_lt
这样的定理来推理 i <? 5
returns.
请注意,Coq 中的 if b then .. else ...
支持 b
作为 bool 或 sumbool。事实上,它支持具有两个构造函数的 any 归纳类型,第一个构造函数使用 then
分支,第二个构造函数使用 else
分支; bool
和 sumbool
的定义小心地命令它们的构造函数使 if
语句按预期运行。