[erlang-questions] The unfathomable beauty of quines

Michael Truog mjtruog@REDACTED
Fri Apr 5 08:34:35 CEST 2019


On 4/4/19 7:53 AM, Joe Armstrong wrote:
> I've been playing with the idea of a quine.
>
> Here are three that are fun
>
> Smalltalk
> TiddlyWiki
> Meta II
>
> A quine is a self-reproducing program -  once the first version works it is used
> to make the second version and so on ad infinitum. It's especially fun when it's
> made as a single file.
>
> WARNING - do not read the rest if you want to get any work done for
> the next month.
>
> Dan Ingalls (the guy who made Smalltalk) told me about Dewey Val Schorre's paper
>
> http://www.ibm-1401.info/Meta-II-schorre.pdf
>
> With strict instructions *not* to read it.
>
> Well, of course, I immediately read it and implemented it in Erlang -
> and poof - 3-6 weeks vanished in a blur (and don't get me started on
> O'meta etc.)
>
> Unfortunately, Erlang is not a quine - it actually was (or has been)
> (or was for a short time)
> but now it's very definitely not - all these BIFs and NIFS and dirty
> this-and-that's
> have destroyed any possibility for quine like behaviour.
>
> Is there a subset of Erlang that can be made into a quine with a new
> VM to support it?
>
> Cheers
>
> /Joe
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>

Hi Joe,

I know you want to do more than a simple regex to mutate the quine, but 
the simplest approach to a quine in Erlang should be in an escript.  An 
example is below (I put it at 
https://gist.github.com/okeuday/0fc551e2a5b1f346d64646d2cd2b66c7 , just 
save it as "quine0"):

#!/usr/bin/env escript
%%!
%-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
% ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et:

-mode(compile).

main(["-i" | _]) ->
     MagicNumber = 0,
     GenerationName = filename:basename(escript:script_name()),
     "quine" ++ GenerationIndexStr = GenerationName,
     io:format("Hi, my name is \"~s\" (generation ~s)~n"
               "my magic number is ~w!~n",
[GenerationName,GenerationIndexStr,MagicNumber]),
     exit_code(0);
main([]) ->
     Path = filename:dirname(?FILE),
     "quine" ++ GenerationIndexStr = filename:basename(?FILE),
     NewGenerationIndex = erlang:list_to_integer(GenerationIndexStr) + 1,
     NewGenerationName = "quine" ++ 
erlang:integer_to_list(NewGenerationIndex),
     {ok, GenerationCurrent} = file:read_file(?FILE),
     S = erlang:integer_to_list(binary:decode_unsigned(
         crypto:strong_rand_bytes(2))),
     GenerationNew = re:replace(GenerationCurrent,
                                "MagicNumber = [0-9]+,",
                                "MagicNumber = " ++ S ++ ",",
                                [global, {return, binary}]),
     FilePath = filename:join(Path,NewGenerationName),
     ok = file:write_file(FilePath,GenerationNew),
     ok = file:change_mode(FilePath,8#00775),
     io:format("~s is born!~n", [NewGenerationName]),
     exit_code(0).

exit_code(ExitCode) ->
     erlang:halt(ExitCode, [{flush, true}]).



More information about the erlang-questions mailing list