Fun with homoiconicity and equality semantics

So today I stumbled accross an interesting article on HN which goes into great detail about the limitation of equality in most languages. The author mentions:

I don’t know of any programming language which even attempts to do anything beyond reference equality for functions. But it’s easy to come up with examples where it would be useful! (An optimizer which removes duplicate code, e.g.) You’re on your own if you need this…

As it just so happened I’d written about this the week before!

Clojure is homoiconic (code is data) and it has great equality semantics which makes it relatively trivial to write these kind of tools.

This example could be expanded with some heuristics to use semantic equality rather than syntactic equality. Even to automatically refactor the code. Or as a macro to automatically optimize code at compile time. :exploding_head:

That being said I’m not sure how useful this is as code duplication hasn’t been common in my experience with Clojure. Still pretty fun so I thought I’d share. :slight_smile:

1 Like

Another example it’s easy to come up with is in ClojureScript “Reagent” programs. Your function is supposed to return a whole page’s Hiccup HTML, and the framework should briskly spot the differences relative to last time, and re-draw only what needs re-drawing. So your function returns

[:html [:body [:p "Hello" [:a ...

and all is well, quite stable. Then you add just a little bit

[:html [:body [:p "Hello" [:a {:on-click #(...

and from that time onward, the A element is “different” every time your function runs. (To restore order, you lift the function out and refer to it by a stable symbol.)

1 Like