diff --git a/PropositionalLogic.ipynb b/PropositionalLogic.ipynb index 4d4ebb6..5a3cf51 100644 --- a/PropositionalLogic.ipynb +++ b/PropositionalLogic.ipynb @@ -40,9 +40,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "import re\n", @@ -64,15 +62,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "And now the actual rules. If you have a sentence that is not translated correctly by this program, you can augment these rules to handle your sentence." + "Let's see what a rule looks like:" ] }, { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(('{P} ⇒ {Q}',\n", + " ['if (?P
.+?) then (?P .+?), (?P.+?)$', 'if (?P
.+?)$']),)"
+ ]
+ },
+ "execution_count": 2,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "Rule('{P} ⇒ {Q}', 'if {P} then {Q}', 'if {P}, {Q}'),"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "And now the actual rules. If you have a sentence that is not translated correctly by this program, you can augment these rules to handle your sentence."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
"outputs": [],
"source": [
"rules = [\n",
@@ -82,8 +106,9 @@
" Rule('~{P} ⋀ ~{Q}', 'neither {P} nor {Q}'),\n",
" Rule('~{A}{P} ⋀ ~{A}{Q}', '{A} neither {P} nor {Q}'), # The Kaiser neither ...\n",
" Rule('~{Q} ⇒ {P}', '{P} unless {Q}'),\n",
- " Rule('{P} ⇒ {Q}', '{Q} provided that {P}', '{Q} whenever {P}', '{P} implies {Q}',\n",
- " '{P} therefore {Q}', '{Q}, if {P}', '{Q} if {P}', '{P} only if {Q}'),\n",
+ " Rule('{P} ⇒ {Q}', '{Q} provided that {P}', '{Q} whenever {P}', \n",
+ " '{P} implies {Q}', '{P} therefore {Q}', \n",
+ " '{Q}, if {P}', '{Q} if {P}', '{P} only if {Q}'),\n",
" Rule('{P} ⋀ {Q}', '{P} and {Q}', '{P} but {Q}'),\n",
" Rule('{P} ⋁ {Q}', '{P} or else {Q}', '{P} or {Q}'),\n",
" ]\n",
@@ -102,15 +127,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Now the mechanism to process these rules. Note that `defs` is a dict of definitions of propositional symbols: `{P: 'english'}`. The three `match_*` functions return two values: the translation of a sentence, and a dict of defintions."
+ "Now the mechanism to process these rules. Note that the parameter `defs` is a dict of definitions of propositional symbols: `{P: 'english'}`. The three `match_*` functions return two values: the translation of a sentence, and a dict of defintions."
]
},
{
"cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 4,
+ "metadata": {},
"outputs": [],
"source": [
"def match_rules(sentence, rules, defs):\n",
@@ -167,63 +190,61 @@
},
{
"cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 5,
+ "metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"Polkadots and Moonbeams \n",
"Logic: (P ⋀ Q)\n",
"P: Polkadots\n",
"Q: Moonbeams\n",
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"If you liked it then you shoulda put a ring on it \n",
"Logic: (P ⇒ Q)\n",
"P: you liked it\n",
"Q: you shoulda put a ring on it\n",
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"If you build it, he will come \n",
"Logic: (P ⇒ Q)\n",
"P: you build it\n",
"Q: he will come\n",
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"It don't mean a thing, if it ain't got that swing \n",
"Logic: (~P ⇒ ~Q)\n",
"P: it is got that swing\n",
"Q: It do mean a thing\n",
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"If loving you is wrong, I don't want to be right \n",
"Logic: (P ⇒ ~Q)\n",
"P: loving you is wrong\n",
"Q: I do want to be right\n",
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"Should I stay or should I go \n",
"Logic: (P ⋁ Q)\n",
"P: Should I stay\n",
"Q: should I go\n",
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"I shouldn't go and I shouldn't not go \n",
"Logic: (~P ⋀ ~~P)\n",
"P: I should go\n",
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"If I fell in love with you, would you promise to be true and help me understand \n",
"Logic: (P ⇒ (Q ⋀ R))\n",
"P: I fell in love with you\n",
"Q: would you promise to be true\n",
"R: help me understand\n",
- "__________________________________________________________________________________________________________ \n",
- "I could while away the hours conferrin' with the flowers, consulting with the rain and my head I'd be a\n",
- "scratchin' while my thoughts are busy hatchin' if I only had a brain \n",
+ "__________________________________________________________________________________________ \n",
+ "I could while away the hours conferrin' with the flowers, consulting with the rain and my\n",
+ "head I'd be a scratchin' while my thoughts are busy hatchin' if I only had a brain \n",
"Logic: (P ⇒ (Q ⋀ R))\n",
"P: I only had a brain\n",
"Q: I could while away the hours conferrin' with the flowers, consulting with the rain\n",
"R: my head I'd be a scratchin' while my thoughts are busy hatchin'\n",
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"There's a federal tax, and a state tax, and a city tax, and a street tax, and a sewer tax \n",
"Logic: (P ⋀ (Q ⋀ (R ⋀ (S ⋀ T))))\n",
"P: There's a federal tax\n",
@@ -231,50 +252,53 @@
"R: a city tax\n",
"S: a street tax\n",
"T: a sewer tax\n",
- "__________________________________________________________________________________________________________ \n",
- "A ham sandwich is better than nothing and nothing is better than eternal happiness therefore a ham\n",
- "sandwich is better than eternal happiness \n",
+ "__________________________________________________________________________________________ \n",
+ "A ham sandwich is better than nothing and nothing is better than eternal happiness\n",
+ "therefore a ham sandwich is better than eternal happiness \n",
"Logic: ((P ⋀ Q) ⇒ R)\n",
"P: A ham sandwich is better than nothing\n",
"Q: nothing is better than eternal happiness\n",
"R: a ham sandwich is better than eternal happiness\n",
- "__________________________________________________________________________________________________________ \n",
- "If I were a carpenter and you were a lady, would you marry me anyway? and would you have my baby \n",
+ "__________________________________________________________________________________________ \n",
+ "If I were a carpenter and you were a lady, would you marry me anyway? and would you have\n",
+ "my baby \n",
"Logic: ((P ⋀ Q) ⇒ (R ⋀ S))\n",
"P: I were a carpenter\n",
"Q: you were a lady\n",
"R: would you marry me anyway?\n",
"S: would you have my baby\n",
- "__________________________________________________________________________________________________________ \n",
+ "__________________________________________________________________________________________ \n",
"Either Danny didn't come to the party or Virgil didn't come to the party \n",
"Logic: (~P ⋁ ~Q)\n",
"P: Danny did come to the party\n",
"Q: Virgil did come to the party\n",
- "__________________________________________________________________________________________________________ \n",
- "Either Wotan will triumph and Valhalla will be saved or else he won't and Alberic will have the final word \n",
+ "__________________________________________________________________________________________ \n",
+ "Either Wotan will triumph and Valhalla will be saved or else he won't and Alberic will\n",
+ "have the final word \n",
"Logic: ((P ⋀ Q) ⋁ (~R ⋀ S))\n",
"P: Wotan will triumph\n",
"Q: Valhalla will be saved\n",
"R: he will\n",
"S: Alberic will have the final word\n",
- "__________________________________________________________________________________________________________ \n",
- "Sieglinde will survive, and either her son will gain the Ring and Wotan's plan will be fulfilled or else\n",
- "Valhalla will be destroyed \n",
+ "__________________________________________________________________________________________ \n",
+ "Sieglinde will survive, and either her son will gain the Ring and Wotan's plan will be\n",
+ "fulfilled or else Valhalla will be destroyed \n",
"Logic: (P ⋀ ((Q ⋀ R) ⋁ S))\n",
"P: Sieglinde will survive\n",
"Q: her son will gain the Ring\n",
"R: Wotan's plan will be fulfilled\n",
"S: Valhalla will be destroyed\n",
- "__________________________________________________________________________________________________________ \n",
- "Wotan will intervene and cause Siegmund's death unless either Fricka relents or Brunnhilde has her way \n",
+ "__________________________________________________________________________________________ \n",
+ "Wotan will intervene and cause Siegmund's death unless either Fricka relents or Brunnhilde\n",
+ "has her way \n",
"Logic: (~(R ⋁ S) ⇒ (P ⋀ Q))\n",
"P: Wotan will intervene\n",
"Q: cause Siegmund's death\n",
"R: Fricka relents\n",
"S: Brunnhilde has her way\n",
- "__________________________________________________________________________________________________________ \n",
- "Figaro and Susanna will wed provided that either Antonio or Figaro pays and Bartolo is satisfied or else\n",
- "Marcellina's contract is voided and the Countess does not act rashly \n",
+ "__________________________________________________________________________________________ \n",
+ "Figaro and Susanna will wed provided that either Antonio or Figaro pays and Bartolo is\n",
+ "satisfied or else Marcellina's contract is voided and the Countess does not act rashly \n",
"Logic: ((((P ⋁ Q) ⋀ R) ⋁ (S ⋀ ~T)) ⇒ (U ⋀ V))\n",
"P: Antonio\n",
"Q: Figaro pays\n",
@@ -283,10 +307,10 @@
"T: the Countess does act rashly\n",
"U: Figaro\n",
"V: Susanna will wed\n",
- "__________________________________________________________________________________________________________ \n",
- "If the Kaiser neither prevents Bismarck from resigning nor supports the Liberals, then the military will\n",
- "be in control and either Moltke's plan will be executed or else the people will revolt and the Reich will\n",
- "not survive \n",
+ "__________________________________________________________________________________________ \n",
+ "If the Kaiser neither prevents Bismarck from resigning nor supports the Liberals, then the\n",
+ "military will be in control and either Moltke's plan will be executed or else the people\n",
+ "will revolt and the Reich will not survive \n",
"Logic: ((~PQ ⋀ ~PR) ⇒ (S ⋀ (T ⋁ (U ⋀ ~V))))\n",
"P: the Kaiser\n",
"Q: prevents Bismarck from resigning\n",
@@ -337,7 +361,7 @@
"\n",
"import textwrap\n",
"\n",
- "def logic(sentences, width=106): \n",
+ "def logic(sentences, width=90): \n",
" \"Match the rules against each sentence in text, and print each result.\"\n",
" for s in map(clean, sentences):\n",
" logic, defs = match_rules(s, rules, {})\n",
@@ -394,9 +418,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.5.1"
+ "version": "3.5.3"
}
},
"nbformat": 4,
- "nbformat_minor": 0
+ "nbformat_minor": 1
}