Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Coq中nat到Q的转换_Coq - Fatal编程技术网

Coq中nat到Q的转换

Coq中nat到Q的转换,coq,Coq,如何在Coq中将nat转换为Q(Rational) 我希望能够写出这样的东西: Require Import Coq.QArith.QArith. Open Scope Q_scope. Definition a := 2/3. 当我尝试这样做时,Coq告诉我: Error: The term "2" has type "nat" while it is expected to have type "Q". 你可以这样写: Definition a := Z.of_nat 2 # Pos.

如何在Coq中将nat转换为Q(Rational)

我希望能够写出这样的东西:

Require Import Coq.QArith.QArith.
Open Scope Q_scope.

Definition a := 2/3.
当我尝试这样做时,Coq告诉我:

Error: The term "2" has type "nat" while it is expected to have type "Q".

你可以这样写:

Definition a := Z.of_nat 2 # Pos.of_nat 3.
#
运算符只是
Q
类型的
Qmake
构造函数的符号。该构造函数将
Z
positive
元素作为参数,因此需要强制转换才能将
nat
s放入其中

如果使用文字数字语法,还可以直接使用
Z
positive

Definition a := 2 # 3.
不同之处在于,此定义不会提及
nat
的转换;数字的类型已经正确,因为Coq直接将数字符号解释为
Z
正数

我个人不太喜欢标准的Coq有理数库,因为它使用等价而不是莱布尼兹等式;也就是说,
Q
1#1
2#2
的元素等同于有理数,但根据Coq的等式,它们并不相等:

Goal (1 # 1 <> 2 # 2).
congruence.
Qed.
目标(1#12#2)。
相似
Qed。
有一个叫做setoidrewrite的功能,可以让你假装他们是平等的。它的工作原理是只允许您在证明与
Q
上的等价概念兼容的函数上重写。然而,仍然有一些情况下,它比莱布尼茨等式更难使用

您还可以尝试软件包的
rat
库(请参阅文档)。它有一个适用于莱布尼茨等式的理性的定义,它比Coq的更全面