Write the shortest self-identifying program (a quine variant)

Write a program that will generate a "true" output iff the input matches the source code of the program, and which generates a "false" output iff the input does not match the source code of the program.

This problem can be described as being related to quines, as the program must be able to somehow compute its own source code in the process.

This is code golf: standard rules apply. Your program must not access any special files, such as the file of its own source code.

Edit: If you so choose, true/false can be replaced with True/False or 1/0.

Example

If the source code of your program is bhiofvewoibh46948732));:/)4, then here is what your program must do:

Input (Stdin)

bhiofvewoibh46948732));:/)4


Output (Stdout)

true


Input

(Anything other than your source code)


Output

false

• Is the true/false output a strong requirement, or are variations (True/False, 1/0) acceptable as well? – Cristian Lupascu Apr 17 '13 at 14:54
• Is it a problem if the program outputs a little more than true/false (if it keeps being unambiguous and ends with true/false) ? – Denys Séguret Apr 19 '13 at 11:31
• – Ilmari Karonen Jan 11 '14 at 5:28
• So you mean a Narcissist program? – PyRulez Jul 15 '17 at 5:57
• Very much related – Deadcode Jan 20 at 7:37

Keg, 3628 26 bytes

:&\ⁿ^\ⁿ&⅀=:&\ⁿ^\ⁿ&⅀=


Try it online!

Same concept as before, but shorter.

:&\ⁿ⅍^\ⁿ⅍++&+¿=:&\ⁿ⅍^\ⁿ⅍++&+¿=


Try it online!

Who knew generating a program that knows itself would be so unreadable!

Explained

The very first step here is to generate the string that will be checked against. At this point, we don't particularly know what the checking part will look like, but we do know that there will be a string that looks like so:

\ⁿ⅍^\ⁿ⅍++


Which would consequently be followed by:

 \ⁿ⅍^\ⁿ⅍++


This creates a string which contains itself quoted... Something needed for when input is taken, as the quotes are going to be present.

From here, string equality is quite easy, coming in at a trivial 2 bytes:

¿=


Incorporating this into the string gives:

\ⁿ⅍^\ⁿ⅍++¿=\ⁿ⅍^\ⁿ⅍++¿=


At this stage, one might think the quine is complete... It has the string part and it has the equality part... What else could be needed? Well. Let me tell you we ain't done yet.

Y'see, we have a quoted string on the stack, but we don't have it's unquoted equivalent present, meaning we need to account for that. The register works well here:

:&\ⁿ⅍^\ⁿ⅍++&+¿=:&\ⁿ⅍^\ⁿ⅍++&+¿=


And there you have it: a self identifying program written completely on a phone keyboard.

• It still outputs 1 if the input is followed by a newline and then anything else – Jo King Jan 19 at 23:32
• Well of course it would, because Keg only reads one line at a time. I'll have to clarify input methods with Keg, because otherwise multi line input isn't possible. – Lyxal Jan 19 at 23:38

PowerShell, 28 bytes

Very similar to this JavaScript answer, uses function provider.

filter f{"$Function:f"-eq$_}


Example

PS > '"$Function:f"-eq$_' | f
True

PS > 'xxx' | f
False


V, 19 bytes

ñOÑ~"qpxØ¥^¨©î±¥$ Try it online! Prints 1 or 0. Here is a hexdump: 00000000: f14f d11b 7e22 7170 78d8 a55e a881 a9ee .O..~"qpx..^.... 00000010: b1a5 24 ..$


interface M{static void main(String[]a){String s="interface M{static void main(String[]a){String s=%c%s%1$c;System.out.print(s.format(s,34,s).equals(new java.util.Scanner(System.in).nextLine()));}}";System.out.print(s.format(s,34,s).equals(new java.util.Scanner(System.in).nextLine()));}}  Try it online. Java 8, 210 bytes (Full Program with Console Arguments) interface M{static void main(String[]a){String s="interface M{static void main(String[]a){String s=%c%s%1$c;System.out.print(s.format(s,34,s).equals(a[0]));}}";System.out.print(s.format(s,34,s).equals(a[0]));}}


Try it online.

Java 8, 108 bytes (Function)

i->{String s="i->{String s=%c%s%1$c;return s.format(s,34,s).equals(i);}";return s.format(s,34,s).equals(i);}  Try it online. Explanation: -part: • The String s contains the unformatted source code. • %s is used to input this String into itself with the s.format(...). • %c, %1$c and the 34 are used to format the double-quotes.
• s.format(s,34,s) puts it all together

Challenge-part:

• .equals(...) checks if this formatted source code equals the input.
• java.util.Scanner(System.in).nextLine() is used as this input for STDIN
• a[0] is used as this input for Console arguments
• i is used as this input for the lambda function

JavaScript ES6, 14 bytes

Like the other javascript answer but includes it's entire source code and not just the function

a=q=>'a='+a==q


Example usage:

a("q=>'a='+a==q") // returns false
a("a=q=>'a='+a==q") // returns true


Pascal (FPC), 167 157 bytes

const b=#39';var s:string;begin read(s);write(s=a+b+#39#59#97#61#39+a+b)end.';a='const b=#39';var s:string;begin read(s);write(s=a+b+#39#59#97#61#39+a+b)end.


Try it online!

While substring can be easily extracted, like in my regular quine, unfortunately, they cannot be concatenated because FPC thinks it is an array in these circumstances. In a and b are characters before and after constant definitions. #39#59#97#61#39 is a replacement for ';a=' as this is not represented in the constants. #39 represents ' and is at the start of b, glued together to the rest of b, to shorten the comparison expression as much as possible.

Zsh, 33 bytes

f () {
[ "type -f f" = \$1 ]
}



Try it online!

The extra spaces, tab, and newlines are required. type -f f does not print the original source, but the function formatted in a particular way, with indentation and a trailing newline.

TeX, 55 bytes

The file has to be saved as a.tex and should be run using pdftex a.tex. The script doesn't terminate after returning 1 or 0, if that should be necessary append it by \end (+4 bytes). If it should work for arbitrary file names replace \openin0a with \openin0\jobname (+7 bytes).

\openin0=a\read0to\0\read1to~\message{\ifx\0~1\else0\fi}


Python 3.8 (pre-release), 42 bytes

exec(s:="print('exec(s:=%r)'%s==input())")


Try it online!