<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jason Rowe &#187; Math</title>
	<atom:link href="http://jasonrowe.com/tag/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://jasonrowe.com</link>
	<description>enjoying the web</description>
	<lastBuildDate>Sun, 13 May 2012 14:05:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Intro to Propositional Logic Using C#</title>
		<link>http://jasonrowe.com/2009/08/28/intro-to-propositional-logic-using-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-propositional-logic-using-c</link>
		<comments>http://jasonrowe.com/2009/08/28/intro-to-propositional-logic-using-c/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 03:18:20 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Propositional Logic]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/?p=628</guid>
		<description><![CDATA[I&#8217;ve been reading about propositional (or sentential) logic in the book &#8220;The Annotated Turing&#8221;. I put together a quick console app to write out truth tables. It helped me get the hang of the mathematical notation &#8220;v&#8221;, &#8220;&#038;&#8221;, and &#8220;->&#8221;. &#8230; <a href="http://jasonrowe.com/2009/08/28/intro-to-propositional-logic-using-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[I&#8217;ve been reading about propositional (or sentential) logic in the book &#8220;The Annotated Turing&#8221;.  I put together a quick console app to write out truth tables. It helped me get the hang of the mathematical notation &#8220;v&#8221;, &#8220;&#038;&#8221;, and &#8220;->&#8221;. In C# those would be ||, &#038;&#038;, and !x || y respectively. I hope someone else finds it useful.

<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Collections.Generic;

namespace PropositionalLogic
{
    public class Program
    {
        public class truthvalue
        {
            public bool X { get; set; }
            public bool Y { get; set; }
        }

        public static bool XorY(bool x, bool y)
        {
            return (x || y);
        }

        public static bool XandY(bool x, bool y)
        {
            return (x &amp;&amp; y);
        }

        public static bool Exclusive(bool x, bool y)
        {
            return (x &amp;&amp; y) &amp;&amp; !(x &amp;&amp; y);// always false
        }

        public static bool Tautoloty(bool x, bool y)
        {
            return x || y || (!x &amp;&amp; !y);// always true
        }

        public static bool Contradition(bool x, bool y)
        {
            return x || (y &amp;&amp; !y);
        }

        public static bool MaterialImplication(bool x, bool y)
        {
            return !x || y; 
            //this can also be expressed as the following conjunction !(x &amp;&amp; !y);
        }

        public static bool BiConditional(bool x, bool y)
        {
            return MaterialImplication(x, y) &amp;&amp; MaterialImplication(y, x); //&quot;if and only if&quot;
        }

        static void Main(string[] args)
        {
            var truthTable = new List&lt;truthvalue&gt;
                                 {
                                     new truthvalue() {X = false, Y = false},
                                     new truthvalue() {X = false, Y = true},
                                     new truthvalue() {X = true, Y = false},
                                     new truthvalue() {X = true, Y = true}
                                 };

            var sentences = new Dictionary&lt;string , Func&lt;bool, bool, bool&gt;&gt;
                                {
                                    {&quot;X v Y&quot;, XorY},
                                    {&quot;X &amp; Y&quot;, XandY},
                                    {&quot;(x v y)&amp; -(x &amp; y)&quot;, Exclusive},
                                    {&quot;x v y v (-x &amp; -y)&quot;, Tautoloty},
                                    {&quot;x -&gt; y&quot;, MaterialImplication},
                                    {&quot;x ~ y&quot;, BiConditional}
                                };

            CalculateTruthTable(truthTable, sentences);

            Console.ReadLine();
        }

        private static void CalculateTruthTable(IEnumerable&lt;truthvalue&gt; truthTable, 
Dictionary&lt;string , Func&lt;bool, bool, bool&gt;&gt; sentences)
        {
            foreach (var sentence in sentences)
            {
                Console.WriteLine(&quot;&quot;);
                Console.WriteLine(&quot;|X      |Y      |{0}  |&quot;, sentence.Key);

                foreach (var value in truthTable)
                {
                    var x = value.X;
                    var y = value.Y;

                    var result = sentence.Value(x, y);

                    Console.WriteLine(&quot;|{0}  |{1}  |{2}  |&quot;, PrettyPrint(x), PrettyPrint(y), 
                                       PrettyPrint(result));
                }
            }
        }

        private static string PrettyPrint(bool input)
        {
            return input ? input + &quot; &quot; : input.ToString();
        }
    }
}
</pre>]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2009/08/28/intro-to-propositional-logic-using-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

