A Quine in Fortran 90 & CO

Glen DC
4 min readDec 6, 2015

--

The other day I was reading a paper named “Reflections on Trusting Trust” by Ken Thompson, a hacker whom I deeply respect. In it, he describes at some point what a quine is and how you should write one if you never went through the challenge yourself:

[…] the problem is to write a source program that, when compiled and executed, will produce as output an exact copy of its source. If you have never done this, I urge you to try it on your own. The discovery of how to do it is a revelation that far surpasses any benefit obtained by being told how to do it.

In that same paper he also talks about how writing it in Fortran increases the level of “joy”:

[…] was to write the shortest self-reproducing program. Since this is an exercise divorced from reality, the usual vehicle was FORTRAN. Actually, FORTRAN was the language of choice for the same reason that three-legged races are popular.

I would recommend you to read the short paper yourself, as it is interesting far beyond this challenge’s info. None the less, the challenge was given and I resisted the urge to take it heads on! … for a day.

You can find my solutions embedded in this article in case you just want to see the result. If however, you didn’t know what a quine was until know, or you never made one yourself, you should stop reading now. This way you can experience the revelations (and insanity) yourself.

Fortran 90

Learning different computer languages and the ideologies that drive them could be described a hobby of mine. As Fortran was on my hitlist since long, it was a great opportunity to learn it, in order to write my first quine.

A first good resource I found was “Fortran Tutorial”. The explanations are short and well written, but their format is certainly not unique. As it turns out there are many similar resources to learn Fortran. This me200c tutorial by Stanford was another great resource and learned me more about topics such as modules.

Compiling Fortran code can be done via GNU Fortran. It is packaged with gcc, although it can be downloaded separately. On OSX you can simply download gcc via Homebrew and you’re set to go.

My first attempt

Getting familiar enough with Fortran 90 in order to write some simple toy programs shouldn’t take to long if you’re not new to programming. It doesn’t mean however that you’re ready to twist your mind enough to actually write your first quine in it.

During my first attempt I quickly realized that it’s not as easy as I thought to achieve closure. If you’re not careful, you’ll be writing self-replicating code indefinitely.

Clearly my first attempt was going nowhere, as you can see for yourself above. Python is a language I like to use to quickly solve problems, for fun and profit. Therefore I decided to switch direction and try to port my Fortran code to python to make it work. This was quite a good idea as it turned out and I swiftly crafted the following solution that successfully replicates itself when run:

This python script was the first time I crafted and witnessed a magical Quine myself.

You can test this solution locally by running the following shell code:

Coming up with this lengthy solution was fun, but the original challenge to solve it in Fortran remained open. I was however a little closer and I just crafted a Quine that’s as useless as a switch that turns itself off. Here is what I learned from the python solution:

  • A quote character can’t be escaped (easily). Instead you should rely on a function that generates that character based on its ASCII Code. If you do try to escape it you will notice that your script corrupts itself, in many situations;
  • Open gaps in your source code array allows you to escape the infinite circle of self-replicating code;

Back to Fortran 90

I knew I could do better than this, especially in a language as flexible as python. And as it turns out, it has been done already. However, the goal of hacking my Fortran solution in python was purely to figure out where it went wrong in my original attempt.

Thanks to my python solution I regained the confidence to try it in Fortran. By applying what I’ve learned I generated the following solution:

My first Fortran 90 quine. Perhaps not the smartest one, but at least I could sleep again.

Time for a relay

I can’t remember the last time that I went to bed in the morning, because I was simply lost in the world of code. But it felt amazing, especially as it balanced out the time spend on the random party the night before.

Call me crazy, but this made me actually appreciate quines even more. In the morning that followed, I came up with a new challenge for myself. What If I write a Fortran program that writes a python program that writes the original Fortran program?

Such programs are called ouroboros programs or quine-relays I learned later. I can appreciate a concept named after a mythical snake eating itself. After some brunch I managed to came up with the following solution, to my own surprise:

I apologize to my neighbors who heard me screaming, jumping and laughing hysterical. No one got killed, it was just another nerd who had to celebrate his return to sanity.

You can test this solution locally by running the following shell code:

Note that you need curl, Python and GNU Fortran to run this script and see the result.

Back to sanity

Going through the challenge of writing a quine certainly filled my weekend more than I had imagined. I even discovered the concept of a multiquine, which would a great follow up challenge. Priorities however are important and I decided that for now I already spend enough time on this topic.

If you think about it, writing a quine is a perfect challenge for a programming job interview. It reflects satirically on the absurd process of certain job interviews out there in the wild.

With this I’ll conclude this article. I might write another article about this topic in the near future if I decide to lose more of my sanity in such endeavors. Feel free to leave your corrections and thoughts as a comment below. If this inspired you to write your own quine I would love to see it, so please do share this as well.

--

--

Glen DC

Freelance Programmer on the road, currently in South America. Writing about programming, projects, life, my travels and stories about my encounters.