我目前正在通过"Accelerated C++"工作,并在第3章中遇到了这个问题:
// invariant:
// we have read count grades so far, and
// sum is the sum of the first count grades
while (cin >> x) {
++count;
sum += x;
}
Run Code Online (Sandbox Code Playgroud)
作者通过解释不变量需要特别注意它来遵循这一点,因为当读入输入时x,我们将读取count + 1等级,因此不变量将是不真实的.同样,当我们增加计数器时,sum将不再是最后计数等级的总和(如果您没有猜到,这是计算学生分数的传统程序).
我不明白为什么这很重要.当然对于任何其他循环,类似的陈述是真的吗?例如,这是本书的第一个while循环(输出稍后填写):
// invariant: we have written r rows so far
while (r != rows) {
// write a row of output
std::cout << std::endl;
++r;
}
Run Code Online (Sandbox Code Playgroud)
一旦我们编写了适当的输出行,那么在我们增加之前,不变量肯定是假的r,就像在另一个例子中一样吗?
是什么让这两个条件不同?
编辑:谢谢你的所有回复. 我想我已经得到了它,但是在我选择一个"接受的答案"之前我还要再花一点时间才能确定. 到目前为止,所有回复基本上都是一致的,所以看起来不太公平,但我觉得值得做.
原始段落,如下所述:
"理解这个循环的不变量需要特别小心,因为while中的条件有副作用.这些副作用会影响不变量的真实性:成功执行cin >> …
我一直在考虑创建一个允许程序员在接口上指定不变量(前置条件和后置条件)的Java框架.目的是使代码更加健壮,并减少需要为同一接口的不同实现编写的单元测试的数量.
我设想创建一些使用程序员也会编写的不变量来注释方法的方法.例如
interface Sort {
int [] sort(int [] nums);
}
Run Code Online (Sandbox Code Playgroud)
将使用注释进行修饰,以确保任何实现都返回已排序的列表.此注释将链接到可以在编译时针对任何实现运行的单元测试.
这是一个疯狂的想法还是对更广泛的编程社区有用?
我正在寻找一种可以在C程序中静态发现不变量的工具.我检查了Daikon,但它只是动态发现不变量.
是否有可用于我正在寻找的工具?谢谢!
我想定义一个类型,以便所有构造都通过可以保留不变量的模块成员,但允许对模式匹配进行解构.
我只是学习OCaml但是以下几乎适用于一个带有不变量的int对,左边应该严格小于右边
module Range : sig
type t = private { left:int; right:int }
exception InvalidRange of (int*int)
val make : int -> int -> t
end = struct
type t = { left:int; right:int }
exception InvalidRange of (int*int)
let make left right = if left < right
then { left; right }
else raise (InvalidRange (left, right))
end
Run Code Online (Sandbox Code Playgroud)
这是有效的
# let p = Range.make 1 2;;
val p : Range.t = {Range.left = 1; Range.right = 2}
# …Run Code Online (Sandbox Code Playgroud) 我正在学习一门中级编程课程,该课程强调使用不变量.我之前从未使用它们,它们似乎占用了更多的时间来创造.软件工程行业是否强调使用不变量?
我想POINT在埃菲尔做一个不可变的课.下面的代码定义了一个吗?和字段的{NONE}可访问性是否足够?我可以写一些类不变的类,或者我怎样才能实现不变性?xyx = x'
class POINT
create
make
feature {NONE}
x: DOUBLE
y: DOUBLE
feature
make (x_: DOUBLE; y_: DOUBLE)
do
x := x_
y := y_
ensure
set: x = x_ and y = y_
end
feature --accessors
get_x: DOUBLE
do
Result := x
ensure
Result = x
end
end
Run Code Online (Sandbox Code Playgroud) 我想知道评论的确切位置以及我应该使用什么关键字,因为我似乎真的无法在网上找到示例,例如我应该这样做吗?
/**
* @invariant invariant example
*/
public class Example {
}
Run Code Online (Sandbox Code Playgroud) 你将如何通过对循环不变量的状态进行推理来证明合并排序的正确性?我唯一能想象的是,在合并步骤中,子数组(不变量)在组合时保持它们的状态,即它们再次排序在每个合并步骤。但我不知道我是否正确进行。我对循环不变量和东西没有太多了解。有人可以启发我吗?。解释每个阶段会发生什么
a) 初始化 b) 维护 c) 终止
多谢!
我知道聚合应该很小并且它们应该保护不变量。我也知道在聚合中保存大型集合会影响性能。
我有一个用例,需要保护它的不变量,但也会导致大量收集。
Aggregate 是Vendor,它可以有多个活动的Promotion (s)。每个Promotion都有PromotionType、StartDate和EndDate。不变量是:
public Vendor : Aggregate {
public Guid Id;
public List<Promotion> Promotions;
// some other Vendor props here
public void AddPromotion(Promotion promo) {
// protect invariants (business rules) here:
// rule_1: if 2 promotions are already active during any time between promo.Start and promo.End then throw ex
// rule_2: if during any time between promo.Start and promo.End there …Run Code Online (Sandbox Code Playgroud) 鉴于这种习俗TypedDict TMyDict:
class TMyDict(TypedDict, total=False):
prop_a: int
prop_b: int
Run Code Online (Sandbox Code Playgroud)
还行吧:
def get_my_dict_works() -> TMyDict:
return {
'prop_a': 0,
'prop_b': 1
}
Run Code Online (Sandbox Code Playgroud)
但这不会:
def get_my_dict_fail() -> TMyDict:
d= {
'prop_a': 0,
'prop_b': 1
}
return d
Run Code Online (Sandbox Code Playgroud)
错误信息是:
Expression of type "dict[str, Unknown]" cannot be assigned to return type "TMyDict"
"dict[str, Unknown]" is incompatible with "TMyDict"
Run Code Online (Sandbox Code Playgroud)
如果我在分配 var 时添加类型注释,它就会起作用:
Expression of type "dict[str, Unknown]" cannot be assigned to return type "TMyDict"
"dict[str, Unknown]" is incompatible with "TMyDict"
Run Code Online (Sandbox Code Playgroud)
为什么?
invariants ×10
java ×2
aggregate ×1
algorithm ×1
annotations ×1
c ×1
c++ ×1
eiffel ×1
immutability ×1
interface ×1
javadoc ×1
loops ×1
mergesort ×1
ocaml ×1
point ×1
python ×1
sorting ×1
typeddict ×1
typing ×1
unit-testing ×1