Skip to content

Repository files navigation

A Program Looking in the Mirror

A small, executable course about quines, self-reference, and the moment code begins to talk about itself.

Two identical program pages facing one another across a narrow mirror

A quine is a program whose only output is its own source code. It does not open its file. It does not receive a copy of itself as input. Somehow, the program already contains everything it needs.

That sounds like a trick. It is a trick—but it is also a clean doorway into quotation, fixed points, compilers, and a deep idea that appears all over computer science: making a thing refer to its own description.

This course takes about an hour. You need Python 3, a text editor, and a little curiosity. No packages, build system, or prior theory are required.

The commands below use python3, as on macOS and Linux. In Windows PowerShell, use the Python launcher py instead.

Begin

If you are reading on GitHub, you can use Code → Download ZIP. Or clone the repository:

git clone https://github.com/oddurs/a-program-looking-in-the-mirror.git
cd a-program-looking-in-the-mirror

Then meet the specimen before anyone explains it:

python3 lab/first_quine.py

Now open COURSE.md. There is also a printable, single-page edition that works offline, and a hosted reading edition.

What is here?

COURSE.md                 the complete guided journey
lab/first_quine.py        the first surprising specimen
lab/unfinished_quine.py   a workbench for building your own
lab/broken_quines/        four small failures to diagnose
gallery/                  the same idea in JavaScript and C
tools/verify.py           an exact, byte-for-byte quine checker
answers/                  hints and explanations for the exercises

To ask the machine whether the Python specimen is really a quine (and stop it after five seconds if an experiment loops forever):

python3 tools/verify.py lab/first_quine.py

If you also have Node.js and a C compiler, verify the whole gallery:

make check

make is only a convenience. On Windows, the equivalent checks are:

py -m unittest discover -s tests -v
py tools/verify.py lab/first_quine.py gallery/javascript.js gallery/c.c

A note on teaching

The course starts with the phenomenon, then earns the explanation one piece at a time. Every odd character is made to account for itself. Short experiments come before terminology; the theory arrives only after the mechanism feels ordinary.

If you teach from this repository, resist the urge to reveal the finished quine too quickly. The useful part is not seeing one. It is discovering why one must be possible.

License

The writing and code are available under the MIT License. Share the course, adapt it, and make something strange.

About

A small, executable course about quines, self-reference, and code that prints itself.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages