7
This challenge is inspired by this other challenge, which you may want to read first for some context. However, a competitive answer is likely to look very different, so after reading some posts on Meta I decided it was best posted as a separate question.
The goal here is to write a polyglot program; that is, a program that works in multiple languages. However, typically polyglot challenges are answered via some standard techniques (listed below). I think it's much more interesting if the standard techniques are avoided, and thus this challenge has a number of restrictions that your program must obey; each restriction is designed to close a standard loophole (although if you can somehow exploit the loophole while remaining within the restriction, go ahead). Here are the restrictions, and the reasons behind them:
Deleting any non-whitespace character from your program must cause it to fail to function in all your languages. Your program may not read its own source.
One of the most common tricks for creating polyglots is to hide code intended for one language inside syntax that's a comment block in each other language. In order to define "comments" (and other non-coding syntax, which similarly needs to be disallowed) in a language-independent way, we define a character to be part of a comment if it could be deleted without affecting the operation of the program. (This definition was based on that in nneonneo's challenge, linked above; not counting the removal of whitespace isn't quite loophole-free as some languages have particularly significant whitespace, but in general it's hard to find a lot of languages which use it for computation, and I don't want the focus of the challenge to be on minor details about which languages require whitespace where.)
To clarify "failing to function", the intention is that the program works like this: deleting a character from the program causes it to break at least one of the other restrictions (e.g. it errors out, or prints the output that does nor comply with the specification).
It's fairly clear that a program could work around the spirit of this restriction via examining its own source code (e.g. via opening the file that stores it on disk, or using a command similar to Befunge's
g
); as such, doing that is banned too. To be precise, you may not use language features that would act differently if a comment were added to the source code.The program may not error out, either at run time or (if applicable) at compile time.
A common trick with polyglots is to not worry about executing parts of the program intended for other languages that aren't valid in one of the languages; you just write them anyway, then let the program crash. Banning errors closes the loophole. Warning messages are acceptable, unless the language is one that downgrades situations that would be errors in most typical languages (e.g. invalid syntax) as warnings.
The program must not use any language features that return the name or version of the language in use.
For example, no using
$]
in a submission that's intended to work in Perl. A common trick when answering polyglot questions is to take two languages which are very similar (such as Python 2 and Python 3) and claim that a program is a polyglot in both of them; however, if the languages are that similar, it makes the programs much easier to write and is uninteresting in a way. This restriction requires any solution to the problem that uses similar languages to focus on the differences between them, rather than the similarities.When executed, the program must: print the name of the language it is executing in; print the name (e.g. "A000290") of a sequence from OEIS; then print the sequence itself. The sequence must be different for each language.
One final trick for writing polyglots is to write a very simple program that uses only code constructs which have the same meaning in a wide range of languages, such as
print(1)
(which is a complete program that outputs1
in sufficiently many languages that it or something like it would probably win without this loophole being closed). As such, there's a requirement here for the program to do a nontrivial amount of work. The program must calculate the sequence elements, not just produce hardcoded output (i.e. do not exploit this standard loophole). If the sequence you choose is an infinite one, this means that the program should run forever producing more and more terms.There's a large enough variety of sequences in OEIS that you should be able to find something that's relatively easy to implement in each of the languages you use, so long as your construction can handle some nontrivial code in addition to just printing constant strings.
This requirement also closes another standard loophole for implementing polyglots: observing that a file is missing a marker such as
<?php
and thus echoes itself, and claiming that that's a valid program. (Deleting a character would definitely change the result then!) Forcing the program to calculate something prevents that.Note: this isn't really a challenge about implementing bignums, so it's OK if the output becomes incorrect after a while or even if the program errors out due to things like word size concerns, so long as you don't try to abuse this by picking a language with very small integers. Likewise, there aren't any hard requirements on the output format. You can output in unary for all I care.
Incidentally, the requirement to print the sequence number is to prevent a solution accidentally becoming invalid because deleting a character changes the sequence that's printed without changing anything else (and therefore is there to make the first restriction easier to comply with).
The winner will be the answer that works in the most languages, making this a code-challenge. If there's a tie, the tiebreak is to favour the submission that was posted earlier. Unlike many challenges on this site, there's no benefit to writing a short program, so feel free to make it as readable as the restrictions allow (which, admittedly, is probably still not very readable). Good luck!
What's the tiebreaker if multiple submissions have the same number of languages? – DLosc – 2016-11-20T04:15:03.713
I was intending to just leave it as a tie, because I feel that optimizing for a tiebreak would detract from optimizing for the main goal. ([tag:code-golf] challenges can often be tied with an equal byte count.) Is that not allowed? – None – 2016-11-20T04:49:32.020
7Could the people who put this on hold please clarify what it is that they find unclear about the problem? I wrote three screenfuls of explanation because I wanted to be as clear as possible! – None – 2016-11-20T04:50:25.663
Clarifications about why this was put on hold from chat: no tiebreak, a few of the criteria weren't quite objective, issues with whitespace-specific languages. I've edited the post to try to resolve them. – None – 2016-11-20T05:11:56.523
The first rule may be better as "deleting any character from the source code will cause it to fail in at least one language". I'm tired, though, so that variation may be much worse and I'm not seeing it. – Mego – 2016-11-20T05:57:46.557
That allows you to write something that's a comment in all but one language, and not a comment in the remaining language, i.e. the normal technique for creating polyglots. – None – 2016-11-20T06:38:50.410
One loophole I can think of is to use strings instead of comments and taking their checksum (or even length) to prevent the code from running if a character is deleted. – Martin Ender – 2016-11-20T08:44:04.250
I don't think that can really be considered a loophole, given that if you're checksumming the contents of the string, it is by definition actually relevant to how the program operates. Besides, I'm not sure there's a good place to draw the dividing line; and you'll need to use this sort of method to hide at least the names of the other languages. (I think something like what you suggested will probably be used by the winning submission; it's something that I've considered for my own answer.) – None – 2016-11-20T08:56:26.220
3
For future reference, we have a challenge sandbox where you can get feedback for challenge ideas and sort out the gritty details of the challenge spec with the help of the community before it goes live. That can be especially useful for tricky specs like this one. (I think you've done a solid job though, especially since it's your first challenge. :))
– Martin Ender – 2016-11-20T10:13:21.523