# Self-replicating code/quines: a fun exercise

**URL:** <https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061>\
**Category:** Links\
**Created:** [November 9, 2023, 10:03pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061 "2023-11-09T22:03:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubenmoor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/rubenmoor/32/2075_2.png) [@rubenmoor](https://discourse.haskell.org/u/rubenmoor)\
**Post date:** [November 9, 2023, 10:03pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/1 "2023-11-09T22:03:05Z")

</div>

> <https://github.com/rubenmoor/haskell-quine/blob/main/README.md>

tl:dr

A quine is a program that produces its source code as output.

I got drawn into quines and couldn’t stop for a while. I put my insights together in a (hopefully) entertaining essay.

Show me your favourite quine!

I encourage anyone to just try and implement a quine by themselves. I don’t say it’s easy. It’s not impossible either. It doesn’t necessarily require a lot of programming experience … rather a hang for riddles.

---

<div class="post-metadata">

**Author:** ![tristanC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tristanc/32/827_2.png) [@tristanC](https://discourse.haskell.org/u/tristanC)\
**Post date:** [November 9, 2023, 10:26pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/2 "2023-11-09T22:26:41Z")

</div>

Here is the one I wrote for the tiny-game jam: [https://github.com/haskell-game/tiny-games-hs/tree/main/prelude/quine](https://github.com/haskell-game/tiny-games-hs/tree/main/prelude/quine)

---

<div class="post-metadata">

**Author:** ![rubenmoor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/rubenmoor/32/2075_2.png) [@rubenmoor](https://discourse.haskell.org/u/rubenmoor)\
**Post date:** [November 9, 2023, 10:33pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/3 "2023-11-09T22:33:43Z")

</div>

```haskell
<insert output of (fmt <$> readFile quine.hs)><insert output of (fmt <$> readFile quine.hs)>"
-- ^ 10 ------------------------------------------------------------------- 80> --
{- prelude-10-80/quine (tristanC)
SPDX: CC0-1.0
-}

```

this is the output that I get. What am I doing wrong?

And what about that `readFile`?

---

<div class="post-metadata">

**Author:** ![tristanC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tristanc/32/827_2.png) [@tristanC](https://discourse.haskell.org/u/tristanC)\
**Post date:** [November 9, 2023, 10:35pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/4 "2023-11-09T22:35:34Z")

</div>

I guess that happens when running the readme version, can you try with the provided `quine.hs` file?

---

<div class="post-metadata">

**Author:** ![rubenmoor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/rubenmoor/32/2075_2.png) [@rubenmoor](https://discourse.haskell.org/u/rubenmoor)\
**Post date:** [November 9, 2023, 10:39pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/5 "2023-11-09T22:39:04Z")

</div>

yeah that one works! But is it accessing the file system?

EDIT: ah, so I guess the code in the readme is meant to be used to create the final, minified code.

---

<div class="post-metadata">

**Author:** ![tristanC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tristanc/32/827_2.png) [@tristanC](https://discourse.haskell.org/u/tristanC)\
**Post date:** [November 9, 2023, 10:43pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/6 "2023-11-09T22:43:32Z")

</div>

Well no, that wouldn’t be a quine otherwise 🙂

---

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [November 9, 2023, 10:59pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/7 "2023-11-09T22:59:01Z")

</div>

For an extra challenge use `import Prelude (putStrLn)` and avoid escape sequences in string literals.

One of my old university professors has [an extensive page about a quine challenge](https://www.win.tue.nl/~wstomv/edu/javascript/challenge.html) (in JavaScript, but should not be that hard to adapt). He also has a bunch of hints, and a [generalized challenge](https://www.win.tue.nl/~wstomv/edu/javascript/fp-challenge.html) (with more hints):

> Write a program _G_ […]
> 
> - that takes as **input** any valid […] definition of a function _f_ that takes a string as argument, and
> - that produces as **output** a valid […] program _P\_f_
> - such that the program _P\_f_ **behaves like** the function _f_ applied to the program _P\_f_ itself, that is, like _f_(_P\_f_).

So for the input: `"f x = putStrLn x"`, the output should be a regular quine.

I never did get completely through it. Maybe I should try again.

---

<div class="post-metadata">

**Author:** ![rubenmoor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/rubenmoor/32/2075_2.png) [@rubenmoor](https://discourse.haskell.org/u/rubenmoor)\
**Post date:** [November 9, 2023, 11:05pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/8 "2023-11-09T23:05:50Z")

</div>

> [@jaror](#):
>
> For an extra challenge use `import Prelude (putStrLn)` and avoid escape sequences in string literals.

What’s the idea behind this? No functions except `putStrLn`?

And avoid escape sequences: this can be done rather trivially by using `chr(34)` for double quotes. But is this what you mean?

---

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [November 9, 2023, 11:09pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/9 "2023-11-09T23:09:56Z")

</div>

Yes, you’d only be allowed to use `putStrLn` and the base language, so no `chr` either.

But you can of course define your own functions.

I think the idea behind it is that it forces you to implement your own escaping mechanism.

---

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [November 10, 2023, 9:37am UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/10 "2023-11-10T09:37:17Z")

</div>

Here’s a quine that I would consider not using any “cheating” functions from Prelude and it also does not use the built-in string escaping mechanism:

> **SPOILER**
>
> ```nohighlight
> escape ('%':'%':xs) = '%' : escape xs
> escape ('%':'|':xs) = '|' : escape xs
> escape ('%':'#':xs) = '#' : escape xs
> escape ('%':'$':xs) = '$' : escape xs
> escape ('#':xs) = '"' : self ++ '"' : escape xs
> escape ('|':xs) = '\n' : escape xs
> escape ('%':xs) = '"' : escape xs
> escape ('$':xs) = '\\' : escape xs
> escape (x:xs) = x : escape xs
> escape [] = []
> 
> self = "escape ('%%':'%%':xs) = '%%' : escape xs|escape ('%%':'%|':xs) = '%|' : escape xs|escape ('%%':'%#':xs) = '%#' : escape xs|escape ('%%':'%$':xs) = '%$' : escape xs|escape ('%#':xs) = '%' : self ++ '%' : escape xs|escape ('%|':xs) = '$n' : escape xs|escape ('%%':xs) = '%' : escape xs|escape ('%$':xs) = '$$' : escape xs|escape (x:xs) = x : escape xs|escape [] = []||self = #||main = putStrLn (escape self)"
> 
> main = putStrLn (escape self)
> 
> ```

But it still stores the whole program in a long unidiomatic string. The next step would be to factor that out into a pretty printing ADT similar to the `Doc` type from pretty printing libraries like this one: [https://hackage.haskell.org/package/prettyprinter-1.7.1/docs/src/Prettyprinter.Internal.html#Doc](https://hackage.haskell.org/package/prettyprinter-1.7.1/docs/src/Prettyprinter.Internal.html#Doc).

---

<div class="post-metadata">

**Author:** ![rubenmoor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/rubenmoor/32/2075_2.png) [@rubenmoor](https://discourse.haskell.org/u/rubenmoor)\
**Post date:** [November 10, 2023, 12:52pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/11 "2023-11-10T12:52:58Z")

</div>

I will try myself first. The longer I think about Quines, the more straightforward they become … the new challenge is welcome.

---

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [November 11, 2023, 10:37pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/12 "2023-11-11T22:37:30Z")

</div>

> [@jaror](#):
>
> The next step would be to factor that out into a pretty printing ADT

Here’s the basic idea of what I mean by this, although this is not yet a quine (it does produce a quine but the result has one very long line again):

> **SPOILER**
>
> ```plaintext
> data Doc = Text String | QU | BS | Self | Doc :-: Doc | Doc :|: Doc
> 
> eval :: Doc -> String
> eval (Text xs) = xs
> eval QU = ['"']
> eval BS = ['\\']
> eval Self = eval (quote self)
> eval (xs :-: ys) = eval xs ++ eval ys
> eval (xs :|: ys) = eval xs ++ '\n' : eval ys
> 
> quote :: Doc -> Doc
> quote (Text xs) = Text "(Text " :-: QU :-: Text xs :-: QU :-: Text ")"
> quote QU = Text "QU"
> quote BS = Text "BS"
> quote Self = Text "Self"
> quote (xs :-: ys) = Text "(" :-: quote xs :-: Text " :-: " :-: quote ys :-: Text ")"
> quote (xs :|: ys) = Text "(" :-: quote xs :-: Text " :|: " :-: quote ys :-: Text ")"
> 
> self = 
> Text "data Doc = Text String | QU | BS | Self | Doc :-: Doc | Doc :|: Doc"
> :|: Text ""
> :|: Text "eval :: Doc -> String"
> :|: Text "eval (Text xs) = xs"
> :|: Text "eval QU = ['" :-: QU :-: Text "']"
> :|: Text "eval BS = ['" :-: BS :-: BS :-: Text "']"
> :|: Text "eval Self = eval (quote self)"
> :|: Text "eval (xs :-: ys) = eval xs ++ eval ys"
> :|: Text "eval (xs :|: ys) = eval xs ++ '" :-: BS :-: Text "n' : eval ys"
> :|: Text ""
> :|: Text "quote :: Doc -> Doc"
> :|: Text "quote (Text xs) = Text " :-: QU :-: Text "(Text " :-: QU :-: Text " :-: QU :-: Text xs :-: QU :-: Text " :-: QU :-: Text ")" :-: QU
> :|: Text "quote QU = Text " :-: QU :-: Text "QU" :-: QU
> :|: Text "quote BS = Text " :-: QU :-: Text "BS" :-: QU
> :|: Text "quote Self = Text " :-: QU :-: Text "Self" :-: QU
> :|: Text "quote (xs :-: ys) = Text " :-: QU :-: Text "(" :-: QU :-: Text " :-: quote xs :-: Text " :-: QU :-: Text " :-: " :-: QU :-: Text " :-: quote ys :-: Text " :-: QU :-: Text ")" :-: QU
> :|: Text "quote (xs :|: ys) = Text " :-: QU :-: Text "(" :-: QU :-: Text " :-: quote xs :-: Text " :-: QU :-: Text " :|: " :-: QU :-: Text " :-: quote ys :-: Text " :-: QU :-: Text ")" :-: QU
> :|: Text ""
> :|: Text "self = " :-: Self
> :|: Text ""
> :|: Text "main = putStrLn (eval self)"
> 
> main = putStrLn (eval self)
> 
> ```

I need a better pretty printer.

---

<div class="post-metadata">

**Author:** ![sonatsuer](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/sonatsuer/32/3839_2.png) [@sonatsuer](https://discourse.haskell.org/u/sonatsuer)\
**Post date:** [November 18, 2023, 1:25pm UTC](https://discourse.haskell.org/t/self-replicating-code-quines-a-fun-exercise/8061/13 "2023-11-18T13:25:39Z")

</div>

Little late to the party but here is a quine of order two I wrote many years ago in Haskell.

> **[GitHub - sonatsuer/Quine-of-Order-2](https://github.com/sonatsuer/Quine-of-Order-2)**
>
> Contribute to sonatsuer/Quine-of-Order-2 development by creating an account on GitHub.
