Writing a compiler in elm?

I’m not familiar with the algorithm, but you could semi-automatically heap recycling. The Clean programming language can apparently do something similar (though slightly different, as it converts shared types to unique types).

On the subject of making a compiler in Elm, I’m currently working through the book Implementing functional languages: a tutorial, which I can certainly recommend. It has code samples in the Miranda language, which looks nearly identical to Elm, and doesn’t use any advanced features you might find in e.g. Haskell. So yes, you should be able to write a compiler in Elm, so long as you don’t care how fast it is.

P.S.: keep in mind that the book treats non-strict languages specifically (this is often called “lazy”, but that is technically incorrect)