$
bin/run examples/fizzbuzz.js --recursion-depth 1
Edit: Thanks to the commenters on Hacker News for pointing out that I was mixing up the Many-Worlds Interpretation (Level III) with a Max Tegmark Level II Multiverse.
What if you could explore your code in another universe? (Cue music: Also sprach Zarathustra, Op. 30 – Strauss) This is the question that popped into my mind after watching Sean Carroll’s talk on the many-worlds interpretation of quantum mechanics.
Hopefully, by the end of this blog post you’ll understand:
The many-worlds interpretation of quantum mechanics imagines our universe as one node in an infinitely branching tree of universes where every possible quantum outcome exists in its own universe. And each time a universe branches, it creates a child universe where the outcomes are slightly different from the parent universe.
Imagine that you are in a job interview, and you are asked to write FizzBuzz (which is probably a hint that you are in a simulation [1] ). You decide to use a for-loop so you write the following code.
1for (let i = 1; i <= 100; i++) {
2 if (i % 15 === 0) {
3 console.log('FizzBuzz');
4 } else if (i % 3 === 0) {
5 console.log('Fizz');
6 } else if (i % 5 === 0) {
7 console.log('Buzz');
8 } else {
9 console.log(i);
10 }
11}
According to the many-worlds interpretation of quantum mechanics, your universe branched into many universes the moment you decided to use a for-loop. In this universe you wrote a for-loop, but in another universe you wrote a while-loop.
I’ve written a JavaScript command-line tool that allows you to explore your code in alternate universes. It’s essentially a Babel plugin that randomly applies transformations to your code that could be attributed to human decision making therefore simulating code you might have written in another universe.
Here’s a list of all the transformations that might be applied to your code in order to simulate outcomes in other universes.
You can find the command-line tool here: github.com/hachibu/many-worlds-javascript-interpreter.
If we run the FizzBuzz example through the command-line tool, it will print out a binary tree of universes containing code you might have written in other universes during your simulated interview.
$
bin/run examples/fizzbuzz.js --recursion-depth 1
Universe {
depth: 0,
data: {
// Original parent universe
name: 'World 0',
code: 'for (let i = 1; i <= 100; i++) {\n' +
' if (i % 15 === 0) {\n' +
" console.log('FizzBuzz');\n" +
' } else if (i % 3 === 0) {\n' +
" console.log('Fizz');\n" +
' } else if (i % 5 === 0) {\n' +
" console.log('Buzz');\n" +
' } else {\n' +
' console.log(i);\n' +
' }\n' +
'}'
},
children: [
Universe {
depth: 1,
data: {
// Alternate child universe
name: 'World 0 => World 0₀',
code: 'for (let i = 2; i <= 99; i++) {\n' +
' if (0 === 14 % i) {\n' +
" console.log('FizzBuzz');\n" +
' } else if (2 % i === 1) {\n' +
" console.log('Fizz');\n" +
' } else if (0 == 6 / i) {\n' +
" console.log('Buzz');\n" +
' } else {\n' +
' console.log(i);\n' +
' }\n' +
'}'
}
},
Universe {
depth: 1,
data: {
// Another alternate child universe
name: 'World 0 => World 0₁',
code: 'let i = 2;\n' +
'\n' +
'while (101 >= i) {\n' +
' if (15 % i === 0) {\n' +
" console.log('FizzBuzz');\n" +
' } else if (i % 4 === 1) {\n' +
" console.log('Fizz');\n" +
' } else if (i % 4 <= 0) {\n' +
" console.log('Buzz');\n" +
' } else {\n' +
' console.log(i);\n' +
' }\n' +
'\n' +
' i++\n' +
'}'
}
}
]
}