我已经实现了一个查询,它告诉数组是否已排序。我想制作一个好的后置条件,可以有效地检查数组是否使用across
或其他方式排序。
我试着这样做:
is_sorted (a: ARRAY [INTEGER]): BOOLEAN
-- Given an array, tell if it's sorted in ascending order
require
array_set: a /= Void
local
i, j, temp: INTEGER
do
Result := True
from
i := a.lower
j := i + 1
invariant
(i >= a.lower) and (i <= a.upper)
until
i = (a.upper) or (Result = False)
loop
if a[i] > a[j] then
Result := False
end
i := i + 1
j := j + 1
variant
a.count - i + 1
end -- End of loop
ensure
-- Check if ARRAY is sorted here
end
我试着写这样的东西:
ensure
across a as el all (el.item <= el.forth.item) end
但这显然行不通,因为el.forth
它不是查询,而是命令。
我怎样才能使这项across
工作或我应该做其他事情?