简体   繁体   English

证明两种算法是相同的

[英]Prove two algorithms are identical

I am supposed to show that two algorithms execute identical statements in identical order. 我应该证明两种算法以相同的顺序执行相同的语句。 One is a tail recursive version of the other. 一个是另一个的尾部递归版本。 They are written in Eiffel. 他们是用埃菲尔写的。

tail_rec(x:instance_type):result_type is
    local
        y:instance_type;
    do
        if b(x) then
            Result :=c(x)
        else
            y:=d(x);
            Result:=tail_rec(y)
    end
  end

Then the non-tail recursive version. 然后是非尾递归版本。

non_rec(x:instance_type):result_type is
    do
        from until b(x) loop
            x:= d(x)
        end;
        Result:= c(x)
    end  

where b(x), c(x), and d(x) are any functions of type BOOLEAN, result_type, and instance_type respectively. 其中b(x),c(x)和d(x)分别是BOOLEAN,result_type和instance_type类型的任何函数。

How are these two algorithms similar and how do they execute identical statements in identical order? 这两种算法如何相似,它们如何以相同的顺序执行相同的语句?

By substituting all flow-of-control constructs with goto s, (essentially turning the code from Eiffel into pseudocode,) and allowing if statements to execute only goto s, it can be shown that both functions end up consisting of the exact same set of instructions. 通过用goto替换所有控制流结构(基本上将代码从Eiffel转换为伪代码),并允许if语句仅执行goto ,可以表明两个函数最终都由完全相同的一组说明。

Let me begin by copying the original tail_rec here for convenience: 为了方便起见,让我开始复制原始的tail_rec

tail_rec(x:instance_type):result_type is
    local
        y:instance_type;
    do
        if b(x) then
            Result := c(x)
        else
            y := d(x);
            Result := tail_rec(y)
        end
    end

First, get rid of Eiffel's silly Result := construct and replace it with return for convenience. 首先,摆脱Eiffel愚蠢的Result :=构造,为方便起见将其替换为return (Otherwise, we are going to have to add more goto s, and frankly, the fewer the better.) (否则,我们将必须添加更多的goto ,坦率地说,数量越少越好。)

tail_rec(x:instance_type):result_type is
    local
        y:instance_type;
    do
        if b(x) then
            return c(x)
        end
        y := d(x);
        return tail_rec(y)
    end

Substitute the if - then - end with if - then goto : 代替if - then - endif - then goto

tail_rec(x:instance_type):result_type is
    local
        y:instance_type;
    do
        if not b(x) then goto label1
        return c(x)
    label1:
        y := d(x);
        return tail_rec(y)
    end

Replace tail recursion with another goto : 将尾递归替换为另一个goto

tail_rec(x:instance_type):result_type is
    do
    label0:
        if not b(x) then goto label1
        return c(x)
    label1:
        x := d(x);
        goto label0
    end

Replace if not b(x) with if b(x) : if not b(x) if b(x)替换为if b(x)

tail_rec(x:instance_type):result_type is
    do
    label0:
        if b(x) then goto label1
        x := d(x);
        goto label0
    label1:
        return c(x)
    end

Similar transformations to tail_rec should turn it into the exact same thing. tail_rec类似转换应将其变成完全相同的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM