Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Don't ask to ask, just ask
 
PerlMonks  

Re: Equivalency of Code

by Anonymous Monk
on Feb 07, 2005 at 11:46 UTC ( #428637=note: print w/ replies, xml ) Need Help??


in reply to Equivalency of Code

Does there exist a way to identify functionally similar blocks of code
In general, you can't, as that will mean solving the halting problem.

That doesn't mean that you will never be able to say "these two pieces of code will do the same thing" - sometimes you can. And sometimes, you will be able to say "these two pieces of code will not do the same thing". It just means that you can't write a program that for any pair of pieces of code can actually decide whether they will do the same thing or not.

As for you example, those pieces of code do not do the same thing. For instance, if $x equals "foo", then $x++ will result in $x becoming "fop", while $x = $x + 1 results in $x becoming 1.

One heuristics that you might try is to compile both pieces of code into bytecode, and compare the bytecodes. That will in a small amount of simple cases tell you that two pieces of code are equivalent.


Comment on Re: Equivalency of Code
Download Code
Re^2: Equivalency of Code
by Anonymous Monk on Feb 07, 2005 at 19:48 UTC
    In general, you can't, as that will mean solving the halting problem.

    You can't solve this problem in general, but not for the reasons you state.

    The halting problem doesn't apply to finite state machines. Any computer that exists today is a finite state machine: it can only store a finite number of states, not an infinite number. Even quantum computers only probabilistically distinguish a finite number of probabilities, though they do enable massive parallelism.

    You can, in theory, determine whether any two finite state machines do the same thing.

    However, you still can't always do this in practice, because you may not have enough states in your machine: memory is costly, and a given computation might require more states than, say, the galaxy has particles. "Finite" can still mean "mind-bogglingly huge", at times.

    Sorry, but one of the things that annoys me is when people reason about modern computers as if they were Turing machines. It's sometimes very useful to think of them that way, but they're simply not the same.

    Unless you're an immortal with an infinite lifespan, with infinite resources to construct an infinitely large computer, you just can't build a Turing machine. And really, if you're an immortal, don't you have something better to do with your infinite lifespan than to site around waiting literally forever for your Turing machine to halt? I know I'd be out smiting people, or creating life, or something fun instead. :-)

    --
    Ytrew

      Ytrew~

      It turns out that after a couple trillion years you get bored of the smiting and creating. But oddly enough, exploring the limits of math remains interesting...

      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
Node Status?
node history
Node Type: note [id://428637]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-04-20 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls