The Challenge
In this challenge, you specify a source language S
and a target language T
. Your task is to write the following program P
in the language S
. If a valid program Q
in the language T
is given as input to P
, it will output a valid program R
in the language T
which takes no input and outputs Q(R)
, that is, the program Q
applied to the source code of R
. In addition, you should present in your answer a nontrivial example program Q
(the more interesting, the better, although you score no points for this), the resulting program R
, and the output of R
. This is code-golf, so the shortest code for P
wins.
In other words, this is a challenge about writing a "universal quine constructor" that can create arbitrary types of generalized quines.
Clarifications
- Your source and target languages may be identical.
- The program
P
should take one string as input (from STDIN or equivalent), and output one string (to STDOUT or equivalent), as should every output programR
. - The input programs
Q
should also transform a string to another string, but their form is more flexible: they can be string-to-string functions, code snippets that modify a variable with a certain name, snippets that modify the data stack if your target language has one, etc. You can also further restrict the form of theQ
's by stating that, for example, they may not contain any comments. However, you must be able to implement any computable string-to-string function as an input programQ
, and you must explicitly state how they function and what further constraints you place on them. - The output program
R
should really be a (generalized) quine, so it must not read any input (user input, files etc.) unlessQ
does so. - Standard loopholes are disallowed.
An Example
Suppose I choose Python as my source language, and Haskell as my target language, and I further require that the input program should be a one-line definition of a String -> String
function named f
. If I give the string-reversing program
f x = reverse x
as input to my Python program P
, it will output the source code of another Haskell program R
. This program prints to STDOUT the source code of R
, but reversed. If P
is given the identity function
f x = x
as input, the output program R
is a quine.