Last active
December 16, 2015 10:29
-
-
Save diogoriba/5420703 to your computer and use it in GitHub Desktop.
Dear Mr. Thompson:
I did the math. The lamp is neither on nor off. It has become self-aware and it wants revenge.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
namespace Boolean_ish | |
{ | |
/// <summary> | |
/// This is a joke. It supposedly emulates a boolean that changes state | |
/// so often that its own state can't be determined. | |
/// | |
/// Inspiration for it was a variation of the Zeno paradox called | |
/// Thompson's lamp (http://en.wikipedia.org/wiki/Thomson's_lamp). | |
/// I found the problem interesting and decided to explore it in | |
/// order to prove that the else clause in an "if (flag) else if (!flag) else" | |
/// construct can actually be reached. | |
/// I know there are other easier ways of proving this point, and i don't | |
/// really approve of this particular construct, i just thought it was an | |
/// interesting concept to explore. | |
/// | |
/// Like women with a moustache. I'm not really fond of 'em. But they're kinda funny. | |
/// </summary> | |
public class QuantumBool | |
{ | |
public static object Lock = new object(); | |
public static readonly QuantumBool True = new QuantumBool(true); | |
public static readonly QuantumBool False = new QuantumBool(false); | |
private bool value; | |
private Thread entropyThread; | |
private static void Entropy(object x) | |
{ | |
QuantumBool val = (QuantumBool)x; | |
while (true) | |
{ | |
// uncomment the lock statements to see the last "else" clause stop getting executed | |
//lock (QuantumBool.Lock) | |
{ | |
val.value = !val.value; | |
} | |
} | |
} | |
public QuantumBool(bool val) | |
{ | |
value = val; | |
entropyThread = new Thread(new ParameterizedThreadStart(Entropy)); | |
entropyThread.Start(this); | |
} | |
public static implicit operator QuantumBool(bool val) | |
{ | |
return val ? True : False; | |
} | |
public static bool operator true(QuantumBool x) | |
{ | |
return x.value; | |
} | |
public static bool operator false(QuantumBool x) | |
{ | |
return !x.value; | |
} | |
public static QuantumBool operator !(QuantumBool x) | |
{ | |
bool reversed = !x.value; | |
QuantumBool result = new QuantumBool(reversed); | |
return result; | |
} | |
public static bool operator ==(QuantumBool x, QuantumBool y) | |
{ | |
return x.value == y.value; | |
} | |
public static bool operator !=(QuantumBool x, QuantumBool y) | |
{ | |
return x.value != y.value; | |
} | |
public override bool Equals(object obj) | |
{ | |
if (!(obj is QuantumBool)) return false; | |
return value == ((QuantumBool)obj).value; | |
} | |
public override int GetHashCode() | |
{ | |
return value.GetHashCode(); | |
} | |
public override string ToString() | |
{ | |
return value.ToString(); | |
} | |
// I could go ahead and implement || and && too, but nope. got lazy. | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
QuantumBool flag = true; | |
while (true) | |
{ | |
// uncomment the lock statements to see the last "else" clause stop getting executed | |
//lock (QuantumBool.Lock) | |
{ | |
if (flag) | |
{ | |
Console.WriteLine("true"); | |
} | |
else if (!flag) | |
{ | |
Console.WriteLine("false"); | |
} | |
else | |
{ | |
// TA-DAAAAA! | |
Console.WriteLine("tralse... frue... dafuq?"); | |
return; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment