Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Intentional Logic in C#

Rate me:
Please Sign up or sign in to vote.
4.84/5 (42 votes)
7 Oct 200310 min read 104.5K   910   45  
Allow your programs to reason with incomplete data.
using System;
using System.Diagnostics;

namespace IntentionalLogic
{
	[Serializable]
	public struct IntentionalBool
	{
		[Flags] 
		private enum State : byte 
		{ False, Unknown, NotUnknown, True };

		private State state;

		private IntentionalBool(State state)
		{
			Debug.Assert(state <= State.True);
			this.state = state;
		}

		public static IntentionalBool True
		{
			get { return new IntentionalBool(State.True); }
		}

		public static IntentionalBool False
		{
			get { return new IntentionalBool(State.False);}
		}

		public static IntentionalBool Unknown
		{
			get { return new IntentionalBool(State.Unknown); }
		}

		public static IntentionalBool NotUnknown
		{
			get { return new IntentionalBool(State.NotUnknown); }
		}

		public bool IsKnown
		{
			get { return state == State.False | state == State.True; }
		}
		
		public static bool operator == (IntentionalBool p, IntentionalBool q)
		{
			return p.state == q.state;
		}

		public static bool operator != (IntentionalBool p, IntentionalBool q)
		{
			return p.state != q.state;
		}

		public override bool Equals(object obj)
		{
			return obj is IntentionalBool && ((IntentionalBool) obj).state == state;
		}

		public override int GetHashCode()
		{
			return (int) state;
		}

		public override string ToString()
		{
			return state.ToString();
		}

		/// <exception cref="ArgumentNullException">value is null</exception>
		/// <exception cref="ArgumentException">value is not one of 
		/// "True", "Unknown", "NotUnknown", "False"</exception>
		public static IntentionalBool Parse(string value)
		{
			return new IntentionalBool((State) Enum.Parse(typeof(State), value));
		}

		public static IntentionalBool operator & (IntentionalBool p, IntentionalBool q)
		{
			return new IntentionalBool(p.state & q.state);
		}

		public static IntentionalBool operator | (IntentionalBool p, IntentionalBool q)
		{
			return new IntentionalBool(p.state | q.state);
		}

		public static IntentionalBool operator ^ (IntentionalBool p, IntentionalBool q)
		{
			return new IntentionalBool(p.state ^ q.state);
		}

		public static IntentionalBool operator ! (IntentionalBool p)
		{
			return new IntentionalBool(p.state ^ State.True);
		}

		public static bool operator true (IntentionalBool p)
		{
			return p.state == State.True;
		}

		public static bool operator false (IntentionalBool p)
		{
			return p.state == State.False;
		}

		public static implicit operator IntentionalBool (bool b)
		{
			return b ? True : False;
		}

		public static explicit operator bool (IntentionalBool p)
		{
			return p.state == State.True;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions