Page

Web

Wiki

# Quine In Stratego

Stratego -- Strategies for Program Transformation
After GPCE/OOPSLA in Vancouver Tijs van der Storm challenged me to write a Stratego program that prints its own source. So I set to work, with the following result. I'll make it educational and reconstruct the design process.

### The basic idea

The core of a self printing program is that it should contain its own source and duplicate that. The following program implements this idea, but with 'prefix' and 'suffix' instead of the

```----------------------------------
module quine
imports lib
strategies
main =
!["prefix", "suffix"]
; \ [x, y] -> [x, x, y, y] \
; concat-strings
; <print>(stdout, [<id>])
; <exit> 0
-----------------------------------
```

Compiling and running this program produces:

```prefixprefixsuffixsuffix
```

### Quoting the source

Next we replace 'prefix' and 'suffix' by the prefix and suffix of the program with respect to the list. Note that we don't bother with the layout of the quoted fragment. Note that the backslashes of the anonymous rewrite rule need to be escaped in the string.

```------------------------------------------------------------------------------------------
module quine
imports lib
strategies
main =
!["module quine imports lib strategies main = ![",
"]; \\ [x, y] -> [x, x, y, y] \\; concat-strings; <print>(stdout, [<id>]); <exit> 0"]
; \ [x, y] -> [x, x, y, y] \
; concat-strings
; <print>(stdout, [<id>])
; <exit> 0
------------------------------------------------------------------------------------------
```

The output of this program is

```------------------------------------------------------------------
module quine imports lib strategies main = ![module quine
imports lib strategies main = ![]; \ [x, y] -> [x, x, y, y] \;
concat-strings; <print>(stdout, [<id>]); <exit> 0]; \ [x, y] ->
[x, x, y, y] \; concat-strings; <print>(stdout, [<id>]); <exit> 0
------------------------------------------------------------------
```

which is starting to look good, but not quite there, since it doesn't compile.

### Getting the quotes right

What we have to do now is introduce quotes in the printed program, such that the pieces of code in the list are actually parsed as strings.

```------------------------------------------------------------------------------------------
module quine
imports lib
strategies
main =
!["module quine imports lib strategies main = ![\"",
"\"]; \\ [x, y] -> [x, x, y, y] \\; concat-strings; <print>(stdout, [<id>]); <exit> 0"]
; \ [x, y] -> [x, x, "\",\"", y, y] \
; concat-strings
; <print>(stdout, [<id>])
; <exit> 0
------------------------------------------------------------------------------------------
```

The output of this program is

```------------------------------------------------------------------
module quine imports lib strategies main = !["module quine
imports lib strategies main = !["",""]; \ [x, y] -> [x, x, y, y] \;
concat-strings; <print>(stdout, [<id>]); <exit> 0"]; \ [x, y] ->
[x, x, y, y] \; concat-strings; <print>(stdout, [<id>]); <exit> 0
------------------------------------------------------------------
```

which still does not compile because of the doublequotes embedded in the string.

### Getting the quotes more right

We need to escape the embedded strings. This can be achieved easily by using the `escape` strategy from the library, which escapes doublequotes and backslashes.

```------------------------------------------------------------------------------------------
module quine
imports lib
strategies
main =
!["module quine imports lib strategies main = ![\"",
"\"]; \\ [x, y] -> [x, <escape>x, \"\\\",\\\"\", <escape>y, y] \\; concat-strings; <print>(stdout, [<id>]); <exit> 0"]
; \ [x, y] -> [x, <escape>x, "\",\"", <escape>y, y] \
; concat-strings
; <print>(stdout, [<id>])
; <exit> 0
------------------------------------------------------------------------------------------
```

This produces (without the newlines)

```-----------------------------------------------------------------------------
module quine imports lib strategies main = !["module quine imports
lib strategies main = ![\"","\"]; \\ [x, y] -> [x, <escape>x,
\"\\\",\\\"\", <escape>y, y] \\; concat-strings; <print>(stdout
, [<id>]); <exit> 0"]; \ [x, y] -> [x, <escape>x, "\",\"", <escape>y, y] \;
concat-strings; <print>(stdout, [<id>]); <exit> 0
-----------------------------------------------------------------------------
```

### Bootstrapping

Now the last program is not exactly the same as its predecessor, but if we compile and run it, it produces its own source literally.
{"ft_lang_label":"__label__en","ft_lang_prob":0.562592,"math_prob":0.82169765,"size":4481,"snap":"2020-24-2020-29","text_gpt3_token_len":1065,"char_repetition_ratio":0.33817288,"word_repetition_ratio":0.4317406,"special_character_ratio":0.49163133,"punctuation_ratio":0.24172662,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99041474,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-05-31T04:37:45Z\",\"WARC-Record-ID\":\"<urn:uuid:dc677fc6-2d76-4811-9c48-e16a28c9430f>\",\"Content-Length\":\"11484\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a49d0b3f-f067-40b2-90e6-88e721ee80b8>\",\"WARC-Concurrent-To\":\"<urn:uuid:fcc6378c-f12a-4120-b842-d507182e2680>\",\"WARC-IP-Address\":\"131.180.119.75\",\"WARC-Target-URI\":\"http://www.strategoxt.org/Stratego/QuineInStratego\",\"WARC-Payload-Digest\":\"sha1:E6LFW4WDGNDFGOBDTXAWIOIKH4DX2XP4\",\"WARC-Block-Digest\":\"sha1:YJKNJHGMWHV3BA52LG2I6QB7C435GY66\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347410745.37_warc_CC-MAIN-20200531023023-20200531053023-00112.warc.gz\"}"}