13

Is it possible to use static or dependent types to prove a function is idempotent?

I've searched Google and various places on StackOverflow/StackExchange for the answer with no luck. The closest I've found was this conversation about Idris: https://groups.google.com/forum/#!topic/idris-lang/yp7vrspChRg

Unfortunately, that discussion is a little over my head.

bmaddy
  • 241
  • 1
  • 5
  • 3
    I'm not posting this as an answer because I'm not 100% sure, but I believe this is impossible due to Rice's Theorem. – gardenhead Sep 26 '16 at 20:37
  • 4
    This is a fascinating question, and my intuition indicates this should be possible in a restricted, non-Turing-complete language. However, Programmers focusses on questions regarding the software development life cycle (see out [help/on-topic] for details), whereas this seems to be a computer science question. The Computer Science site may be a better fit and lead to better answers. – amon Sep 26 '16 at 20:58
  • This can get tricky in the general case because, in order to prove details of what a function does, you first have to prove that it halts... – Mason Wheeler Sep 26 '16 at 20:59
  • @amon: after reviewing the help center pages, I think you're right. I don't have the rep to nominate this as "belongs on cs.stackexchange.com". If no one gets that process started in a day or two I'll flag it for a moderator. Sorry about posting to the wrong site. – bmaddy Sep 26 '16 at 22:45
  • 2
    @gardenhead Rice's theorem states that given any property that the behavior of a program could have, it's sometimes impossible to determine whether or not a program has that property. There's a big difference between "this is sometimes impossible" and "this is impossible". – Tanner Swett Sep 26 '16 at 23:16
  • @TannerSwett I don't think that's a correct explanation of Rice's theorem. I'm not sure what you mean by "sometimes impossible to determine". – gardenhead Sep 27 '16 at 00:02
  • 1
    @gardenhead: Pretty much everything interesting is impossible due to either Rice's Theorem, the Halting Problem, or some other Incompleteness, Undecidability or Uncomputability Result. However, it is possible to design languages in such a way as to make it possible to perform such analysis, or alternatively, it is possible to place the burden on the programmer to write the program in such a way as to make it possible to analyze. E.g. some dependently-typed languages are designed in such a way that the compiler tries to prove termination and rejects programs where it can't, others are … – Jörg W Mittag Sep 27 '16 at 00:52
  • 1
    … designed in such a way that the programmer must provide a machine-checkable proof of termination. In both cases, the compiler can actually prove termination for all valid programs (although in the first case, that's actually cheating because it simply defines programs for which it can't prove termination as invalid). Such a language is then no longer Turing-complete, actually, but it turns out that Turing-completeness is highly overrated. Tetris-completeness is much more interesting. – Jörg W Mittag Sep 27 '16 at 00:55
  • 2
    My last comment was pretty vague. In any case, here's what Rice's theorem has to say: there is no algorithm which correctly classifies all functions as being idempotent or not idempotent. However, there are still useful algorithms which classify some functions as being idempotent or not. – Tanner Swett Sep 27 '16 at 00:56
  • I get what you guys are going for, though it's beyond the scope of OPs question. Of course, the algorithm that always return true correctly classifies some functions as idempotent or not :D I guess the challenge is finding a sufficiently interesting class of functions that are decidable. I don't know what they are, but I'm curious. I agree that Turing-completeness is overrated; studying Godel's System T of primitive recursion was eye-opening for me. – gardenhead Sep 27 '16 at 01:20
  • 2
    The OP asked about proving that a function is idempotent, not having an algorithm classify functions as idemptotent or not. The main difference being that a proof can be written by a person. As for Turing completeness, it is indeed not a problem. – gallais Sep 27 '16 at 08:07
  • @gardenhead: I guess the type of algorithm he was talking about would never return false answers, just return "unknown" for the cases it can't handle. – RemcoGerlich Oct 28 '16 at 07:48
  • @gallais If a person can prove it, so can (in principle) a Turing machine. A turing machine can simulate a brain and (apart from the limited memory) a brain can simulate a turing machine. So your difference is irrelevant. – CodesInChaos Oct 28 '16 at 09:35
  • 1
    I like my typechecker to terminate though (and rather quickly if possible), not start enumerating all possible programs in the hope that it is going to stumble upon the right one at some point.

    Sure, a lot of things are semi-decidable but it doesn't mean that it's interesting to try and brute force them.

    – gallais Oct 28 '16 at 10:23

1 Answers1

3

For certain functions it is. Especially when you know the function ;-)

If you mean by your question "is there an algorithm to decide automatically if an arbitrary function is idempotent or not", the answer is no, due to the theorems already mentioned in the comments. However, for specific classes of functions, one can - in theory - very easily decide if the function is idempotent or not. For example, if the function is pure (means: without any side effects), and one knows it always returns a value in a finite amount of time for any given input, then idempotency can be decided simply by trying out if f(f(x))=f(x) for any possible input x to the function. Not that this will be very efficient, it could run until the end of the universe.

So if that is not the answer you were looking for, write a better question, currently it is pretty unclear what exactly you are really looking for.

Doc Brown
  • 206,877
  • Thanks for the answer. The ability to "decide automatically" was exactly what I was looking for. – bmaddy Oct 28 '16 at 12:08
  • 2
    To expand on the 'for certain functions it is' statement: Idempotency can be proven for any function that either only accepts a finite amount of inputs (by trying out all of them), or a type of input that is defined recursively (like natural numbers, or linked lists), which means that you only need to prove that the idempotency is true for the base case(es) and the recursive case(es). – Qqwy Jan 02 '17 at 21:09