Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure - test for equality of function expression?

Suppose I have the following clojure functions:

(defn a [x] (* x x))

(def b (fn [x] (* x x)))

(def c (eval (read-string "(defn d [x] (* x x))")))

Is there a way to test for the equality of the function expression - some equivalent of

(eqls a b)

returns true?

like image 778
hawkeye Avatar asked Feb 22 '12 11:02

hawkeye


1 Answers

It depends on precisely what you mean by "equality of the function expression".

These functions are going to end up as bytecode, so I could for example dump the bytecode corresponding to each function to a byte[] and then compare the two bytecode arrays.

However, there are many different ways of writing semantically equivalent methods, that wouldn't have the same representation in bytecode.

In general, it's impossible to tell what a piece of code does without running it. So it's impossible to tell whether two bits of code are equivalent without running both of them, on all possible inputs.

This is at least as bad, computationally speaking, as the halting problem, and possibly worse.

The halting problem is undecidable as it is, so the general-case answer here is definitely no (and not just for Clojure but for every programming language).

like image 110
kittylyst Avatar answered Oct 08 '22 04:10

kittylyst