Hacker News new | past | comments | ask | show | jobs | submit login
Radiation-hardened Quine (2014) (github.com/mame)
360 points by sirnicolaz on June 2, 2023 | hide | past | favorite | 107 comments



This comes from the same dev known for the 128-language quine relay, where each of the 128 languages prints a program in the next one, looping all the way to the original one. In alphabetical order and as ASCII art, no less.

https://github.com/mame/quine-relay


This project has always fascinated me, so I tried making a tiny uroboros quine with Python -> Javascript -> Python -> etc. I started with a quine for each language, and tried inserting them into each other, but the output always grew exponentially.

    py_quine_template = "s = {}; print(s.format(repr(s)))"
    py_quine = py_quine_template.format(repr(py_quine_template))
    
    js_quine_template = "s = {}; console.log(s.replace('{}', JSON.stringify(s)))"
    js_quine = js_quine_template.format(repr(js_quine_template))

    py_js_quine = py_quine_template.format(repr(js_quine_template))
    ???
So I checked how it's done in the Github repo[1], but the language snippets only seemed to be quoting and printing without any self-references like you'd expect in a quine...

Then it hit me. You only need the quine machinery in one language. If I can make a Python script that has a string with its own source code, then the Javascript code will simply be `console.log(py_source)`, no manipulation necessary.

So here's my Python/Javascript uroboros quine:

    s = "s = {}\npy_source = s.format(repr(s))\nprint('console.log('+repr(py_source)+')')"
    py_source = s.format(repr(s))
    print('console.log('+repr(py_source)+')')
If there were more languages, then it'd be `console.log("System.out.println({python_source})")`, etc. The problem then becomes escaping inner quotes, and escaping the escape characters themselves. I managed to sidestep the issue by using two types of quotes, and relying on Python's `repr` also giving valid JS strings, but if I had to add just one more language I'd need a lot more scaffolding.

I still think the Quine Relay is a tour de force, but now for different reasons. It's not 128 quines in different languages, but an incredibly robust system for escaping strings in 128 wildly different languages.

[1] https://github.com/mame/quine-relay/blob/master/src/code-gen...


> but an incredibly robust system for quoting and escaping strings in 128 wildly different languages

I'm getting a tension headache just thinking about it.


i just want to know what possesses a man to do such a wonderful thing, and to even know if such a thing is possible before pursuing it

i would also watch a 128 hour livestream of him coding it because how do you even start


> and to even know if such a thing is possible before pursuing it

I'm not the creator of that magnificent piece of programmatical art. But I can take a stab at this. As long as the languages are Turing complete it is possible.

Any Turing machine can simulate any other Turing machine so once you step up to that level the challenge is keeping your head through the maze of nuances between many of those languages. I think the logic behind it goes something like this, if a language is complete (eg in the Gödel sense) then you can have it produce things that are nonsense in its own language.

Anyways I would expect the bulk of the work to be making those incremental step of going from each language to the next. In terms of data shape, I am thinking a linked list. So the final step is to call them all in a nested fashion, bootstrapping each up onto the top of that stack as you go.


The quines are not simulating each other, they are just printing a string after some formatting (which usually involves embedding a copy of the string into itself). There's no Turing completeness needed (most of the quines don't even have branches), much less Gödel encodings.


> they are just printing a string after some formatting

There's (at least) one language in quine-relay that's not that simple: Piet. It's a visual 2D language written as an image file.

https://www.dangermouse.net/esoteric/piet/samples.html


Oh huh you're absolutely right. Start at the bottom, work your way up


> i just want to know what possesses a man to do such a wonderful thing

My impression is, that they are an artist who just happens to use a medium not normally (directly) used for art: Code.


He's got a book! ...in Japanese. I hope someone can help him translate it into English so more people can learn about this kind of thing and he can be rewarded for his adventures.


Discussion for 'quine-relay': https://news.ycombinator.com/item?id=33105706 (350 points | 7 months ago | 56 comments)


mame (yusuke endoh) also has a pretty awesome youtube channel which is basically just showing their hacks

https://www.youtube.com/@YusukeEndoh


mame is also very well known for their IOCCC submissions. My favorite is https://www.ioccc.org/2012/endoh1/hint.html.


> Prepare a script that deletes one character randomly:

Hmm, but can it also deal with general bit flips? Because that's usually what radiation does not delete characters. The only case it would delete a character with a bit flip on the data would be causing the DEL char 0x7F i think, which 7 ASCII chars have a 1 in 8 chance of mutating into (because we have 8 actual bits but only 7 are used for ASCII): ~}{wo_?

There are 96 ASCII characters that might be used in source code naively assuming equal character probability that's 7/96 * 1/8 = 0.9% chance of a bit flip deleting a character by mutating into 0x7F. There's also a chance of a character mutating into a non-printable character (0-31), only the 2nd and 3rd column of ASCII can mutate into this by ending up with both high bits off, so there are 64/96 chars with a 1/8 chance that's an 8.3% chance of mutating into a non-printable control char etc (i'm ignoring LF because it displaces DEL). If the 8th bit flips into a one on any char then we get extended ASCII I think, that's 96/96 * 1/8 = 12.5% chance. That leaves 78.3% chance of mutating into another regular printable ASCII character. Apologies for any bad math.

There are a lot of possible affects on the interpreter when it sees any of those characters, the control chars could be particularly troublesome.

Either way this is still a very cool quine :P


What you're describing just isn't what this game called "radiation hardening" is. It would be a different, also interesting game.

But "radiation hardening" challenges are common in code golf, and always (from challenges I've seen) framed in terms of character mutation.

Also I suspect some unconscious bias based on your username :P


So by "character mutation" you mean the deletion of characters? Because to my ears that term still sounds like bit flips, i.e. changing characters.



> This tag is for challenges which require answers to still work when a random edit to the source code is made.

What if my random edit is to add a character?


Possible -- depends on the rules of the specific challenge. Most of the time it's edits, deletes, or some combination. Sometimes the score depends on how many edits your code can withstand.


Ah, I wasn't aware it was the name of a common code golf challenge, I do code golfing myself but apparently not enough :)

I guess it makes sense people would frame the game that way, arbitrary bit flips seems way too hard, I'm not even sure it's possible for a program to cope on it's own.


To be fair, "radiation" suggests only bit flips.


Physics guy here. Isn't the solution to simply duplicate the code and compare it before execution? Do it 3 times and use the majority. This could be the preamble to any quine. (I realize though that, like most physics people, I enjoy waltzing into a specialized domain that I know nothing about and reduce it to first principles. But hey, it's who I am.)


Someone said similar elsewhere, but the problem is the "meta code" that does the comparison needs to be hardened as well.


Yes, triple redundancy is nice, but then the problem becomes how to make the code that checks the code also tipple redundant and checked without creating another layer to deal with.

Most software that does this simply tries to make the test code as small and reliable as possible. But it's an interesting challenge to make the test code somehow self checking with the same properties.


Well, physically, you really need three independent machines. Which means three copies of the file, three processes, three verifications, and so on, and a way for those processes to communicate with each other and "vote". Then you run into the problem of definition - is a cluster a quine? Note that most of the "primordial bit flip" problems go away if you make N >> 3 and design each machine to "fail fast".


> Isn't the solution to simply duplicate the code and compare it before execution? Do it 3 times and use the majority

Yes, voting is a common way to deal with radiation or critical software. There are of course also complications about the voting software itself needing to also be redundant. Some can be done with hardware, or you can just radiation harden parts of the system (reducing overall cost).

This is definitely one of those problems where at a quick glance it seems like there's a few simple solutions that can resolve 90% of issues but when you dig into the weeds you find out that 20% dictates most of the outcomes and there's at most a 10% overlap with your quick/obvious solutions.


It adds weight but why not put all the computing hardware in one spot and build a lead or water shield around it?


Hardware radiation hardening techniques have existed for a long time, and are applied commonly in space, satellites, nuclear power, military.

Roughly speaking it breaks down into a couple categories including the manufacturing of electronics and chips themselves, including everything from substrate differences to using entirely different types of RAM and block storage [0], to the higher level strategies employed by many generations of space probes where a variety of total system redundancy is employed, e.g the Voyager probes used discrete component level redundancy, and most modern probes use almost full system redundancy where they run 3 complete computers in lock step, the idea being that triple redundancy is the minimum needed to detect which computer is wrong.

Another potential strategy not so commonly applied for this purpose is software resilience, such as the techniques applied by the Minix3 microkernel, the idea being to minimise the critical kernel code to be so small that it's unlikely to have bugs or be affected by hardware level affects, then everything else underneath can be kept alive by the kernel, even block device drivers, by employing triple redundancy and a "resurrection server". This was intended to protect against bugs in critical software like device drivers and hardware/firmware bugs rather than radiation, but it could be similarly effective especially as a layered strategy.

[0] https://en.wikipedia.org/wiki/Radiation_hardening#Radiation-...



Try doing that in space


Doing what?


See also the International Obfuscated C Code Contest. [0]

This program [1], for example. It just accepts some input on stdin and returns the same input, but mirrored along the diagonal. So the first row of the input becomes the first column of the output, second row becomes second column, etc. But the program is functionally invariant when given itself as input. In other words, you can flip the source code of the program along its diagonal and the result is a program which has the same functionality - it flips stdin.

Or this one [2], which parodies java in C. It's functional C code that looks like Java, including the

  class LameJavaApp
  {
    /** The infamous Long-Winded Signature From Hell. */
    public static void main(String[] args)
    ...
Or this one [3] that calculates pi by estimating its own surface area.

Or this one [4]. It's a lovers quarrel, written simultaneously in C and English. It's incredible, seriously, read it.

[0]: https://www.ioccc.org/

[1]: https://github.com/ioccc-src/winner/blob/master/1994/schnitz...

[2]: https://github.com/ioccc-src/winner/blob/master/2005/chia/ch...

[3]: https://github.com/ioccc-src/winner/blob/master/1988/westley...

[4]: https://github.com/ioccc-src/winner/blob/master/1990/westley...


It would seem that the author of this code is actually the World No. 1 IOCCC champ!


Number 2 really did make chuckle!


I struggled a bit too long to build it: don't forget the `-w` flag (disable all warnings):

    cc -w chia.c -o chia


Someone managed to create a level 3 radiation-hardened quine in Perl (you can remove any 3 characters and it still works):

https://codegolf.stackexchange.com/a/100785/98955


At the most basic, you just have N+1 copies of the program, and run whichever one is longest, since that must be the one with no missing characters.

The program itself then prints N+1 copies of itself...

You just need a little care about the start of the first program, since that is the 'entry point' - it needs to fall through to the 2nd program if the first is damaged.


The code that checks which one is longest needs to be resilient to missing characters


Plus, a bit flip doesn’t necessarily remove a character..


The "radiation hardening" aspect is metaphorical, this is referring specifically to removing characters, not bit-flips from gamma rays.


Here is something quite similar I've just made, but in the image domain :

https://github.com/unrealwill/uncroppable

It's a tool POC to steganographically encode an image into itself to make it crop resistant.


Is DNA a radiation hardened quine? I mean, it’s basically code that makes more of itself, even in the presence of mutations.


There's a great video recently by Kurzgesagt video recently[0] on just how hard the immune system works to identify and kill any cell that is not cooperating anymore.

In a nutshell, the DNA fails at protecting from changes all the time, and the immune system is constantly checking for that and killing off those cells.

[0]https://youtu.be/zFhYJRqz_xk


It pretty much is, but it's not rad hard in the sense that you can change or delete any one letter. Well, maybe in animals and plants, since we have at least two copies of most genes.


DNA doesn't make a copy of itself, the cell makes a copy of the DNA. DNA doesn't really do anything, it requires RNA and proteins to actually do anything.


well, quine program doesn't do anything by itself too, you need computer and electricity for it to work


There's a definitional problem here. Would a book with instructions for how to order additional copies be a quine? What's the difference? I don't know exactly.


I guess the right question to ask about DNA is, does it contain all the instructions required to produce a copy of the DNA itself? DNA translation involves a bunch of RNA, sure. But DNA copying is mostly done by DNA polymerases, which are typical peptide based proteins encoded in the DNA itself, right?

So in a real sense, the DNA contains the blueprint for the machinery to copy the DNA itself, but not the instructions for building that machine from the blueprint, the same way a software quine doesn't necessarily include the source code for a compiler.

Definitely a definitional problem, but that's my shot at resolving it a bit.

Then again, DNA polymerase is not perfect, and DNA is not immune to single nucleotide changes having a real effect, so it's only a quine in the "spherical cow" way of looking at it anyway. But it's certainly the most "quinish" thing in nature that I can think of.


I just don't think it's accurate to say DNA has instructions in the same way a computer does. It's more like a recipe for a chemical reaction. If we expand the definition of a quine away from being self reproducing code and to self reproducing chemical reaction, it seems like things like crystalization or fire could be considered a quine.


I actually like that.

But I may have been a bit unclear about my mapping of computer and biology terminology to this question.

I tend to think of a computer programs as being mathematical machines, and proteins as being molecular/chemical machines. Motor proteins "walking" along microtubules is the most vivid example of this.

I'm sort of torn between both worlds here, honestly. I like both perspectives. Viruses especially have a very quinish quality to them in that they're stripped down to just enough stuff to get a host cell to make more viruses.


I like the virus as quine analogy much better than generic DNA as a quine, personally. Seems much more meaningful.


DNA includes instructions for how to build all the machinery needed to make a copy of itself, including the surrounding cell, and RNA, and proteins, and the organism that is capable of gathering food/fuel to obtain the all the raw materials to build everything.


No it doesn't, it holds a recipe for creating proteins under specific chemical and biological conditions. If you put human DNA in a chicken egg, it wouldn't hatch a human.


And if you write a quine computer program on a piece of paper, it won't create a copy of itself further down the page.

Or if you put a fully-functional cell in the vacuum of space, or the heart of a star, it won't replicate either.

All replicators need a suitable environment to replicate in. The fact that inhospitable/non-viable environments exist does not negate the fact that they are actually replicators.


If you build a cell from only eukaryote DNA instructions, it won't have any mitochondria and be unable to copy itself.


Mitochondrial DNA is still DNA.

Symbiotic replicators that rely on the presence of each other to reproduce (like cycles of quines?) are also still replicators.


I think also the Golgi apparatus has no DNA. You can't build one if you don't have one. But apparently we're still not sure where it comes from.


The program doesn't make a copy of itself, the runtime makes a copy of the program. Program doesn't really do anything, it requires runtime and cpu to actually do anything.


In some cases, this is in fact a useful way to look at it.

The DDC model[0] proposes that one of the drivers of evolutionary change is polyploidy, where after an organism has multiple copies of the genome, genes are free to drift into new functions.

An organism with four copies of a gene instead of the usual two is more rad-hardened than the basal non-ploid organism.

[0]: https://www.researchgate.net/figure/The-duplication-degenera...


Maybe it is the cell that is the quine?


It's not robust to any point mutation, only some, but I would still say this is a fair point


Wait, what is the definition of radiation hardening? Radiation would flip one random bit at a time, not delete a whole character in one go.


I think it’s just aesthetically reminiscent of radiation hardening. Pretty sure protecting agains arbitrary bit flips is impossible. If the MSB of the first character gets flipped, the code is illegal no matter what you do.


Maybe you could have bit-flip resistant assembly? I hardly know assembly so I don't know if there are opcodes 1-bit apart that could work together.


FYI: A quine is a computer program which takes no input and produces a copy of its own source code as its only output.


Challenge: a self-healing version of this where the program missing a character outputs the original program (with the character re-inserted).


That's what this program is doing - the mutated code with one character missing will output the original program.


You're right! I misread the diff command after mutation. Amazing!


I think it would be more impressive if it produced itself (with character removed) after a character is removed


The author of this quine (Yusuke Endoh) has a Github repository which encodes a program in the Github contributions graph. I have no idea what the program does but here is a link to the start of it:

https://github.com/mame?tab=overview&from=1970-12-01&to=1970...



How... does it work? What happens if you delete one of the characters in "eval"? What's the space in "e xit" about?


Not sure about “e xit” off the top of my head, this isn't one of my “natural” languages, but I assume breaking eval is protected against by the multiple invocations and the fact that assignment is an operator that returns its result as well as setting the target variable. If one of the evals is broken (or one of the =s between is removed) it just becomes a variable that is assigned to and the result is passed along. Three evals is enough to give the “any single character deleted” survivability, for “any two characters” you would need five (and many of the other tricks would need to be altered/expanded also).


Radiation would flip a bit though, which more likely changes a character


There would also be a difference between deletion and replacing with a space character. Ie shorten the program file by one char (however many bytes depending on encoding). Can anyone explain what deletion means in this useage?


Deletion means deletion, not replacing with a space.


This is what “radiation-hardening” means in the context of programming challenges, it doesn't refer to actual radiation.


This is absolutely incredible. Definitely one of those 'I will never understand how" black magic levels of impressive.


I'm quite confident I understand what is going on with such things, so it isn't black magic to me, but I doubt I'd be able to implement these tricks to do something like this myself in any practical amount of time.


IIRC I saw an interpreter somewhere where each input program is quine, the interpreter name rhymed with `hat` as I remember it.


Any known issues when radiation deleted (instead of altering) any characters? Deleting something is introducing significantly more entropy than altering because it alters the length of the array of text, but altering the array requires recalculating of the rest of the array which is impossible outcome of radiation.


Mostly impressed that it's not two redundant copies of a source code, but rather one actual code fragment.


There are multiple versions in the repo. The first one is essentially just two copies, the second was an improved design from someone else and the last was an ASCII-art version (of the radiation symbol) that was based off the second.


Related:

Radiation-hardened quine (2014) - https://news.ycombinator.com/item?id=18840797 - Jan 2019 (22 comments)

Radiation-hardened Quine is now “ascii-arted” - https://news.ycombinator.com/item?id=8318879 - Sept 2014 (1 comment)

Radiation-hardened quine - https://news.ycombinator.com/item?id=7276976 - Feb 2014 (71 comments)


Radiation-hardened quine (4 years ago) - 22 comments - https://news.ycombinator.com/item?id=18840797

Radiation-hardened quine (9 years ago) - 71 comments - https://news.ycombinator.com/item?id=7276976


holy .... even more impressive, it was created 9 years ago!


It is.

ChatGPT will write a ‘hello world’ that survives character deletions. I’m pretty impressed it can do this.


If this isn't definitive proof that we live in a simulation, I don't know what is.

Seriously impressive work!


What are the advantages to radiation hardening software? In the cases I've run into the thinking has been "if you can't trust the hardware then you're already lost".


Defense in depth. Like the Space Shuttle that had four rad hardened processors. And a voting system to reject anomalous outputs. With two you don't know which one is right. With three you're OK. Four allows for one to permanently fail and still leave three.

This is analogous to having at least three NTP servers. Or filesystem level checksums on top of ECC RAM and LDPC in the SSD. Or TCP checksum on IPv4 header checksum on Ethernet/LTE/Wi-Fi checksum.


Protecting against cosmic bitflips for example, relevant if you write software that ends up on airplanes/space crafts. Also if you just want resilient software that work even though hardware is starting to fail. See https://en.wikipedia.org/wiki/Soft_error


Yes but what is the FMECA case where software is a better domain to address this than hardware?


I think the point is to implement in software for redundancy, not as an alternative to doing it in hardware. You'll do both essentially.


In this case, it's just for fun.


Now can we go further and add self-healing?

Include a checksum somewhere. Modify the output or the checksum until they agree.

This is starting to sound like how the immune system works actually...


It is self healing. The output matches the original


How is radiation supposed to delete character instead of flipping one bit? How is interpreter protected?


No matter how many times you kill him, Quine rises from his tomb, his hand showing through the dirt to point at a rabbit.


I follow mame, always up to something fascinating!


does it work if you flip any random bit though?

no.


(2014)


Eh its not a quine anymore.


How so?


I quine prints it's own source code. This essentially prints an older version of itself.


But it does print it's source code if you don't modify it. It's like saying that it's not a quine because you formatted it.


The unmodified version is obviously still a quine. The modified version is not. And yes, formatting matters!


Agreed, but no one ever asserted that it is a quine no matter what. It is a quine that just so happens to still run after decay




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: