Types Coq数据类型冲突
在Coq中重新定义自然数类型并尝试使用它时,如下所示:Types Coq数据类型冲突,types,coq,Types,Coq,在Coq中重新定义自然数类型并尝试使用它时,如下所示: Inductive Nat: Type := | O: Nat | S: Nat -> Nat. Fixpoint plus (n: Nat) (m: Nat): Nat := match n with | O => m | S n' => S (plus n' m) end. Fixpoint mult (n: Nat) (m: Nat): Nat := match n with | O =&
Inductive Nat: Type :=
| O: Nat
| S: Nat -> Nat.
Fixpoint plus (n: Nat) (m: Nat): Nat :=
match n with
| O => m
| S n' => S (plus n' m)
end.
Fixpoint mult (n: Nat) (m: Nat): Nat :=
match n with
| O => O
| S n' => plus m (mult n' m)
end.
Fixpoint factorial (n: Nat) : Nat :=
match n with
| O => 1
| (S n') => mult n (factorial n')
end.
Coq发出错误
术语“1”的类型为“nat”,而预期的类型为“nat”
我理解这种行为的原因(实际的数字“1”仍然映射到Coq的内置自然数类型),但是否有办法解决它?TIA.最简单的解决方案当然是
Definition one := S O.
但是,由于您的类型与nat
完全相同,因此可以声明强制。这看起来像
Inductive Nat : Type :=
| O: Nat
| S: Nat -> Nat.
Fixpoint to_nat (n : nat) : Nat :=
match n with
| S n' => 1 + (to_nat n')
| O => 0
end.
Coercion to_nat : nat >-> Nat.
这告诉coq无论何时获得nat
并期望nat
时都要使用to_nat
。它让您可以使用+
,以及特殊的数字文字