30
\$\begingroup\$

A Fragile Quine

A fragile quine is a quine that satisfies the property of having each substring made by removing a single character, when evaluated, produces an error.

For example. If your program asdf is a quine, then for it to be fragile, the following programs must error:

sdf
adf
asf
asd

Your program (and all of its substrings) must be fully deterministic, and must be in the same language. A program falling into an infinite loop (that is, failing to terminate), even if not eventually producing an error is considered to "produce an error" for the purposes of this challenge.

Standard loopholes apply, including the usual quine restrictions (e.g. not able to read own source code).

For example, print("foo") is not fragile. All these substrings must error:

rint("foo")
pint("foo")
prnt("foo")
prit("foo")
prin("foo")
print"foo")
print(foo")
print("oo")
print("fo")
print("fo")
print("foo)
print("foo"

The ones that don't error are:

print("oo")
print("fo")
print("fo")

So it is not fragile.

An important note on quines

By consensus, any possible quine must satisfy this:

It must be possible to identify a section of the program which encodes a different part of the program. ("Different" meaning that the two parts appear in different positions.)

Furthermore, a quine must not access its own source, directly or indirectly.

Example

Since I consider JavaScript's function#toString to be "reading it's own source code", I am disallowing it. However, if I weren't to bar it, here is a fragile quine in JavaScript:

f=(n=b=`f=${f}`)=>(a=(n)==`f=${f}`,n=0,a)&(n!=b)?b:q

Tester

Here is a program that, given the source code of your program, generates all programs that must error.

let f = (s) =>
  [...Array(s.length).keys()].map(i =>
    s.slice(0, i) + s.slice(i + 1)).join("\n");

let update = () => {
  output.innerHTML = "";
  output.appendChild(document.createTextNode(f(input.value)));
};

input.addEventListener("change", update);

update();
#output {
  white-space: pre;
}

#input, #output {
  font-family: Consolas, monospace;
}
<input id="input" value="print('foo')">
<div id="output"></div>

\$\endgroup\$
  • \$\begingroup\$ Am I allowed HQ9+? \$\endgroup\$ – Oliver Ni Sep 6 '16 at 0:34
  • 1
    \$\begingroup\$ @OliverNi No \$\endgroup\$ – Conor O'Brien Sep 6 '16 at 0:36
  • 2
    \$\begingroup\$ This is making assumptions about language features - not all languages have "errors". \$\endgroup\$ – Mego Sep 6 '16 at 7:31
  • 2
    \$\begingroup\$ @Mego Infinite loops are also allowed instead of errors. Every Turing-complete language has infinite loops. \$\endgroup\$ – feersum Sep 6 '16 at 12:19
  • 1
    \$\begingroup\$ @Mego that is irrelevant. Why even bother bringing up the general case if only a finite subset applies per problem? \$\endgroup\$ – Conor O'Brien Sep 6 '16 at 20:07
6
\$\begingroup\$

Burlesque, 32 28 25 bytes

{3SHWD{Je!}.+{Sh}\msh}Je!

Try it here.

So most instructions in Burlesque are 2 characters. And it is much easier to write a quine in Burlesque than in Marbelous. Je! or ^^e! means _~ in CJam.

\$\endgroup\$
  • 1
    \$\begingroup\$ Wait...if this is not golfed (and is by far the shortest here), then I can't imagine what golfed is! +1 \$\endgroup\$ – Daniel Sep 6 '16 at 23:31
  • \$\begingroup\$ @Dopapp The problem is Burlesque has too many instructions, so it is very likely I missed something. \$\endgroup\$ – jimmy23013 Sep 6 '16 at 23:33
11
\$\begingroup\$

Python 3, 45 bytes

c='print(end="c=%r;exec(c"%c+c[8*4])';exec(c)

Switching to Python 3 so that the trailing newline can be easily removed.

I started out with a stupid structure that had 2 variables instead of 1, but switching to 1 variable only made it 4 bytes shorter.

-(4 + 3) bytes by Dennis.

\$\endgroup\$
  • \$\begingroup\$ didn't know about assert. Good work \$\endgroup\$ – Destructible Lemon Sep 6 '16 at 2:49
  • 2
    \$\begingroup\$ Would be great if you add an explanation. \$\endgroup\$ – Sarge Borsch Sep 6 '16 at 5:23
  • \$\begingroup\$ Combining your exec structure with my % trick, there's c='print(end=len(c)%5*"c=%r;exec(c)"%c)';exec(c) for 48 bytes. \$\endgroup\$ – Dennis Sep 6 '16 at 6:31
  • 1
    \$\begingroup\$ c='print(end="c=%r;exec(c"%c+c[8*4])';exec(c) saves 3 more bytes. \$\endgroup\$ – Dennis Sep 6 '16 at 22:34
7
\$\begingroup\$

Python, 91/92 67 bytes

This was fun!

Now I know about assert:

s='s=%r;assert len(s)==34;print(s%%s)';assert len(s)==34;print(s%s)

If a char from string is removed, assert error. I would have done this sooner had I known of this feature, assert.

\$\endgroup\$
  • 2
    \$\begingroup\$ I forgot about the trailing newline question... if you do count it, then our answers are invalid since it can be safely removed. \$\endgroup\$ – feersum Sep 6 '16 at 2:41
7
\$\begingroup\$

Python 2, 51 50 46 bytes

lambda s='lambda s=%r:s[22:]%%s%%s':s[22:]%s%s

Verify it on Ideone.

\$\endgroup\$
  • \$\begingroup\$ Function quines are indeed allowed. \$\endgroup\$ – Conor O'Brien Sep 6 '16 at 3:19
  • 1
    \$\begingroup\$ Huh? I've never heard of functions being allowed as quines...are there any examples for this? \$\endgroup\$ – feersum Sep 6 '16 at 3:21
  • \$\begingroup\$ @feersum The query javascript quine yields a few results. \$\endgroup\$ – Dennis Sep 6 '16 at 3:24
  • \$\begingroup\$ @feersum the standard Mathematica quine is also function based (using #0). \$\endgroup\$ – Martin Ender Sep 6 '16 at 5:58
  • 2
    \$\begingroup\$ I just had another look and most JS or Mathematica quines actually also call the function. So it would be more correct to call them REPL quines. That said, the example provided by Conor in the spec of this challenge is just a function, so I guess at least it's valid here. \$\endgroup\$ – Martin Ender Sep 6 '16 at 6:35
4
\$\begingroup\$

C#, 145 bytes

_=>{var@s="_=>{{var@s={1}{0}{1};for(;79!=s.Length;){{}}System.Console.Write(s,s,'{1}');}};";for(;79!=s.Length;){}System.Console.Write(s,s,'"');};

I've not written a quine in C# before, but higher scores are better in golf, right? :)

Infinite loop if a char is removed from the string or a digit from the magic const 79. Removing any other char results in a compile error.

Ungolfed:

/* Action<object> Quine = */ _ => // unused parameter
{
    // String of the function (well, mostly).
    // {0} placeholder for s, so the output contains the function and string.
    // {1} placeholder for " since it requires escaping.
    var@s = "_=>{{var@s={1}{0}{1};for(;79!=s.Length;){{}}System.Console.Write(s,s,'{1}');}};";

    // Infinite loop if a char is removed from the above string or if the 7 or 9 is removed.
    for(;79!=s.Length;){}

    // Print the quine.
    System.Console.Write(s,s,'"');
};
\$\endgroup\$
3
\$\begingroup\$

JavaScript, 90 bytes

a="a=%s;a[44]!=')'?x:console.log(a,uneval(''+a))";a[44]!=')'?x:console.log(a,uneval(''+a))

Works in the console of Firefox 48, and should work in any other environment with uneval and console.log. Breakdown of errors:

a="                                             "; [  ]!=' '?x:           (a       (''+a))   // SyntaxError
   a=%s;a[44]!=')'?x:console.log(a,uneval(''+a))  a 44    )                                  // ReferenceError from calling `x`
                                                               console.     ,uneval          // ReferenceError from calling `onsole.log`, `auneval`, etc.
                                                                       log                   // TypeError from calling `console.og`, etc.
\$\endgroup\$
  • \$\begingroup\$ Wow, didn't know about uneval. Also, nice to see you at least somewhat back! :D \$\endgroup\$ – Conor O'Brien Sep 7 '16 at 1:35
  • \$\begingroup\$ @ConorO'Brien Nice to be (at least somewhat) back! I've posted a couple more quine-y answers using this technique. \$\endgroup\$ – ETHproductions Sep 7 '16 at 1:37
2
\$\begingroup\$

Python 2, 59 bytes

x='x=%r;1/(len(x)==30);print x%%x';1/(len(x)==30);print x%x

This throws a ZeroDivisionError if a 0, a 3, or a character is removed from the string. Removing a different character results in a NameError or a SyntaxError.

\$\endgroup\$
1
\$\begingroup\$

A Pear Tree, 50 bytes

a="print('a='.repr(a).';eval(a)');#f+QF>";eval(a)

Try it online!

Not the shortest answer, but a fairly comprehensive one; any character deletion from this program causes it to fail to checksum, thus the A Pear Tree interpreter won't even attempt to run it. (For example, you get an error if you delete the trailing newline.) The ;#f+QF> is used to ensure that the program as a whole has a CRC-32 of 0 (and f+QF> is one of three possible 5-byte strings that could be placed in the comment to achieve that while staying in ASCII; using ASCII is important here because repr would fail to round-trip it correctly otherwise).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.