{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "
Here are a few tips on how to get started using Sage to work logic. We look at possible answers to #2 in Section 3.3 of Applied Discrete Structures.
\n", "\n", " Here are the symbols used in Sage.\n", "
\n", "and | .... | & |
or | .... | | |
not | .... | ~ |
Something that is equivalent to x?
\n", "\n", " It's a cop-out but you could answer x here. ... and the same for the other two parts.\n", "
" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "p q r value\n", "False False False True \n", "False False True True \n", "False True False True \n", "False True True True \n", "True False False True \n", "True False True True \n", "True True False True \n", "True True True True \n" ] }, "execution_count": 21, "metadata": { }, "output_type": "execute_result" } ], "source": [ "x.iff(x).truthtable()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "\n",
" Here is one that is a bit more interesting
.\n",
"
Something that implies x?
\n",
" Here you need a proposition that has the property that when it is true,
\n", " There are lots of pos\n", " sible answers here, as there was in the previous part. One is to notice that whenever p is true in the truth table, x is also true.\n", "
" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "p=prop(\"p\")" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 66, "metadata": { }, "output_type": "execute_result" } ], "source": [ "x.implies(p)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "\n", "Here is an algorithmic solution to exercise 10 at the end of Section 3.7, on Mathematical Induction. The problem was to prove that all postage amounts greater than or equal to 8 can be made using 3 and 5 cent stamps. Notice that the solution for any value of\n", " Thanks to Richard Voss for the following code that produces a truth table that is more compatible with the text.\n", "
" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def TruthTable(formula):\n", " txt = str(propcalc.formula(formula).truthtable()).replace(\"value\",formula)\n", " return txt.replace(\"False\",\"0\").replace(\"True \",\"1\").replace(\" \",\" \")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p q ((p | ~q)& ~p) ->~q\n", "0 0 1 \n", "0 1 1 \n", "1 0 1 \n", "1 1 1 \n", "\n" ] } ], "source": [ "print TruthTable('((p | ~q)& ~p) ->~q')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "\n", " Consider the argument \\[a \\lor b, c\\land d,a\\rightarrow \\neg c \\Rightarrow b\\]\n", "
" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "p1=prop(\"a|b\")\n", "p2=prop(\"c&d\")\n", "p3=prop(\"a->~c\")\n", "concl=prop(\"b\")" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": { }, "output_type": "execute_result" } ], "source": [ "propcalc.valid_consequence(concl,p1,p2,p3)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 71, "metadata": { }, "output_type": "execute_result" } ], "source": [ "propcalc.valid_consequence(prop(\"~b\"),p1,p2,p3)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "c,r1,r2=propcalc.get_formulas(\"b\", \"c->b\", \"c\")" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 74, "metadata": { }, "output_type": "execute_result" } ], "source": [ "propcalc.valid_consequence(c,r1,r2)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 75, "metadata": { }, "output_type": "execute_result" } ], "source": [ "propcalc.valid_consequence(p2,p1,c)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "q1,q2,q3,q4=propcalc.get_formulas(\"c->b\", \"c->a\", \"a->~b\",\"c\")\n", "cx=prop(\"a&b\")" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 77, "metadata": { }, "output_type": "execute_result" } ], "source": [ "propcalc.valid_consequence(cx,q1,q2,q3,q4)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 78, "metadata": { }, "output_type": "execute_result" } ], "source": [ "propcalc.valid_consequence(~cx,q1,q2,q3,q4)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 79, "metadata": { }, "output_type": "execute_result" } ], "source": [ "propcalc.consistent(q1,q2,q3,q4)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ ] } ], "metadata": { "kernelspec": { "display_name": "SageMath (stable)", "language": "sagemath", "metadata": { "cocalc": { "description": "Open-source mathematical software system", "priority": 10, "url": "https://www.sagemath.org/" } }, "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 0 }