Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Equivalency of Code

by Corion (Pope)
on Feb 07, 2005 at 10:15 UTC ( #428611=note: print w/replies, xml ) Need Help??


in reply to Equivalency of Code

As any C macro programmer will tell you, the three are not equivalent if you want to substitute any scalar expression for $x. The easy way of telling whether two pieces of code are identical is to force them into one canonical form, and then check if the two forms are identical. In the general case though, proving the functional identity of two pieces of code is equivalent to solving the halting problem, and thus infeasible (I think).

Replies are listed 'Best First'.
Re^2: Equivalency of Code
by ambrus (Abbot) on Feb 07, 2005 at 10:26 UTC
    In the general case though, proving the functional identity of two pieces of code is equivalent to solving the halting problem, and thus infeasible (I think).

    Indeed. If same($code1, $code2) could find out whether the $code1 and $code2 functions behave identically, then

    sub f1 { "different"; } sub f2 { same(\&f1, \&f2) ? "same" : "different"; } f2();
    would be a contradiction.

      To spell it out explicitly, there is nothing that f2() can return that would be right.

      • If f2() returns "same", that means f2() and f1() should return the same things. f1() always returns "different" so this is incorrect.
      • If f2() returns "different", that means f2() and f1() should return different things. Now they are both returning the same thing, so this is also incorrect.

      The logical conclusion is that function same() does not exist.

        I disagree with your second idea. f2() and f1() returning the same thing is not the same concept as them having identical functionality.

        To Wit:

        sub f1 { rand; } sub f2 { srand; rand(1); }
        these functions are identical in function, but return different results for the same input.
      That's a pretty big "in the general case".

      One can EASILY prove that the two following pieces of code are equivalent.
      my $foo = 1; my $foo = 1;
      So while you may not be able to work out if _any_ two pieces of code are equivalent, you can work out if _some_ two pieces of code are equivalent.

      As for the above example, it isn't really fair for the comparison function to have to compare itself.

      Generally most observes are given at least the countesy of being outside the situation being examined...

      So to summarise, there are ways to prove _some_ sorts of code are equivalent, just not _any_ code. And if you bias that process towards false negatives you stay pretty safe. (Accept false readings of "not equal" in exchange for certain results of "is equal")
      Actually, you'll hit a recursion limit before you run into your logical contradiction.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        dragonchild~

        You are assuming that same calls the functions it is passed as arguments.

        The theoretical same function cannot evaluate its arguments, as it cannot know that its arguments halt. After all, sub f1{ while(1) { } } and sub f2{ for(; ;) { } } should be considered the same...

        Also, for the purposes of computability "deciding" a problem means that the program must halt with an answer of either "yes" or "no". (Well, classically accepting or rejecting, but that is just semantics).

        Boots
        ---
        Computer science is merely the post-Turing decline of formal systems theory.
        --???

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://428611]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2021-10-27 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (94 votes). Check out past polls.

    Notices?