English 中文(简体)
正式逻辑的正确性
原标题:Proving correctness in formal logic

我想问是否有人能帮我回答这个问题。这是以前一份考试论文的答案,我可以知道这个年度考试的答案。

这个问题似乎如此简单,以至于我完全迷失了方向,到底要问什么?

考虑以下涉及整数变量的代码部分:

if (i < j) {
    m = i;
} else {
    m = j;
}

By stating an appropriate output condition and then verifying the correctness of the piece of code, prove that after execution, m is equal to the minimum of i and j.

I have got the post condition as: {m = i ∧ i < j ∨ m = j ∧ j < i}

这是正确的吗?你如何核实这一点?

最佳回答

您的邮政条件是正确的。 我个人认为以下的变体( 相当于) 比较自然 :

(i<j → m=i) ∧ (i≥j → m=j)

为证明此程序符合邮寄条件,您可做以下工作:

  1. 请注意,要确定程序 ways 符合您应该使用 true 作为您预设条件的邮政条件 。

  2. 所以,你有以下的Haare三重:

    {true}
    if (i < j) {
    
    
        m = i;
    
    } else {
    
    
        m = j;
    
    }
    {(i < j → m = i) ∧ (i ≥ j → m = j)}
    
  3. 条件必须维持在两个分支的尽头,因此(根据标准最弱的有条件先决条件规则)我们有:

    {true}
    if (i < j) {
    
    
        m = i;
        {(i < j → m = i) ∧ (i ≥ j → m = j)}     <--.
    } else {                                       |
                                                   |
                                                   |
        m = j;                                     |   copy
        {(i < j → m = i) ∧ (i ≥ j → m = j)}     <--|
    }                                              |
    {(i < j → m = i) ∧ (i ≥ j → m = j)}  ---------- 
    
  4. 根据分配产量最薄弱的先决条件,进一步推进公式

    {true}
    if (i < j) {
    
        {(i < j → i = i) ∧ (i ≥ j → i = j)}   <---.
        m = i;                                    | m replaced by i
        {(i < j → m = i) ∧ (i ≥ j → m = j)}   ---- 
    } else {
    
        {(i < j → j = i) ∧ (i ≥ j → j = j)}   <---.
        m = j;                                    | m replaced by j
        {(i < j → m = i) ∧ (i ≥ j → m = j)}   ---- 
    }
    {(i < j → m = i) ∧ (i ≥ j → m = j)}
    
  5. 在真实分支的顶端,我们知道 i< j , 在其它分支的顶端,我们知道 :

    {true}
    if (i < j) {
        {i < j}                               (1)  <--- added
        {(i < j → i = i) ∧ (i ≥ j → i = j)}   (2)
        m = i;
        {(i < j → m = i) ∧ (i ≥ j → m = j)}
    } else {
        {¬(i < j)}                            (3)  <--- added
        {(i < j → j = i) ∧ (i ≥ j → j = j)}   (4)
        m = j;
        {(i < j → m = i) ∧ (i ≥ j → m = j)}
    }
    {(i < j → m = i) ∧ (i ≥ j → m = j)}
    
  6. 对于任何连续两个说法,第一种说法就意味着第二种说法。 (这些说法通常被称为“补充义务”。 )我们有两种这类义务: < code>(1) 应该意味着 < code > (2) , < code > (3) 应该意味着 < code > (4) 。 情况显然如此。

    - "qed": -)

问题回答

暂无回答




相关问题
Prove or disprove n^2 - n + 2 ∈ O(n)

For my algorithm analysis course, I ve derived from an algorithm the function f(n) = n^2 - n + 2. Now I need to prove or disprove f(n) ∈ O(n). Obviously it s not, so I ve been trying to disprove that ...

Complexity proof

I would to prove the following example: n^k = O (c^n) for every k and c>1 It is noticeable that the polynomial function grows faster than exponential function. We try to find k0 > 0 satisfying ...

Stable Matching Problem

I am currently reading an Algorithm s book and came across the Stable Matching Problem. And a question came to mind that I m curious about, but the book doesn t answer. In every SMP is it possible to ...

prove n = Big-O(1) using induction

I know that the relation n = Big-O(1) is false. But if we use induction involving Big-O it can be proved. But the fallacy is we cannot induct Big-O. But my question is how we can disprove the relation ...

Explain the proof by Vinay Deolalikar that P != NP [closed]

Recently there has been a paper floating around by Vinay Deolalikar at HP Labs which claims to have proved that P != NP. Could someone explain how this proof works for us less mathematically ...

Have I checked every consecutive subset of this list?

I m trying to solve problem 50 on Project Euler. Don t give me the answer or solve it for me, just try to answer this specific question. The goal is to find the longest sum of consecutive primes that ...

Formally verifying the correctness of an algorithm

First of all, is this only possible on algorithms which have no side effects? Secondly, where could I learn about this process, any good books, articles, etc?

Writing a proof for an algorithm [closed]

I am trying to compare 2 algorithms. I thought I may try and write a proof for them. (My math sucks, so hence the question.) Normally in our math lesson last year we would be given a question like <...

热门标签