(*
LogicAssistant.
Copyright (C) 2017 Inspirel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*)
(* Content-type: application/vnd.wolfram.mathematica *)
(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)
(* CreatedBy='Mathematica 10.4' *)
(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[ 158, 7]
NotebookDataLength[ 42564, 1079]
NotebookOptionsPosition[ 41213, 1030]
NotebookOutlinePosition[ 42230, 1067]
CellTagsIndexPosition[ 42101, 1060]
WindowFrame->Normal*)
(* Beginning of Notebook Content *)
Notebook[{
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{"<<", "ProdiamsLogic`"}], "\[IndentingNewLine]", "\n",
RowBox[{"(*", " ", "functions", " ", "*)"}], "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{
"evaluates", " ", "the", " ", "expression", " ", "with", " ", "symbols",
" ", "replaced", " ", "with", " ", "actual", " ", "values"}], " ",
"*)"}]}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"evaluate", "[",
RowBox[{"expression_", ",", "symbols_", ",", "values_"}], "]"}], ":=",
RowBox[{"expression", "/.",
RowBox[{"Thread", "[",
RowBox[{"symbols", "->", "values"}], "]"}]}]}], "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{
"adds", " ", "the", " ", "results", " ", "column", " ", "to", " ", "the",
" ", "table", " ", "of", " ", "argument", " ", "tuples"}], " ",
"*)"}]}], "\n",
RowBox[{
RowBox[{
RowBox[{"appendAllResults", "[",
RowBox[{"arguments_", ",", "results_"}], "]"}], ":=",
RowBox[{"Map", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"Function", "[",
RowBox[{"argResPair", ",",
RowBox[{"Apply", "[",
RowBox[{"Join", ",", "argResPair"}], "]"}]}], "]"}], ",",
"\[IndentingNewLine]",
RowBox[{"Transpose", "[",
RowBox[{"{",
RowBox[{"arguments", ",",
RowBox[{"Map", "[",
RowBox[{"List", ",", "results"}], "]"}]}], "}"}], "]"}]}],
"\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{
RowBox[{
"generates", " ", "a", " ", "new", " ", "term", " ", "like", " ", "A"}], "&&",
"B", "&&",
RowBox[{"!",
RowBox[{
"C", " ", "from", " ", "symbols", " ", "and", " ", "their", " ",
"values"}]}]}], " ", "*)"}]}], "\n",
RowBox[{
RowBox[{
RowBox[{"makeTerm", "[",
RowBox[{"symbols_", ",", "args_"}], "]"}], ":=",
RowBox[{"And", "@@",
RowBox[{"MapThread", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"Function", "[",
RowBox[{
RowBox[{"{",
RowBox[{"sym", ",", "val"}], "}"}], ",", "\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{"val", ",", "sym", ",",
RowBox[{"!", "sym"}]}], "]"}]}], "\[IndentingNewLine]", "]"}], ",",
"\[IndentingNewLine]",
RowBox[{"{",
RowBox[{"symbols", ",", "args"}], "}"}]}], "\[IndentingNewLine]",
"]"}]}]}], "\n", "\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{
RowBox[{
"generates", " ", "a", " ", "truth", " ", "table", " ", "for", " ", "the",
" ", "given", " ", "expression"}], ",", "\[IndentingNewLine]",
RowBox[{
"fullValues", " ", "allows", " ", "to", " ", "use", " ", "short", " ",
"forms", " ",
RowBox[{"T", "/", "F"}], " ", "instead", " ", "of", " ",
RowBox[{"True", "/", "False"}], " ", "and", "\[IndentingNewLine]",
"resultFunction", " ", "allows", " ", "to", " ", "provide", " ",
"custom", " ", "function", " ", "for", " ", "computing", " ", "results",
" ", "in", " ", "the", " ", "result", " ", "column"}]}], " ",
"*)"}]}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"truthTable", "[",
RowBox[{"expression_", ",",
RowBox[{"OptionsPattern", "[",
RowBox[{"{",
RowBox[{
RowBox[{"fullValues", "\[Rule]", "True"}], ",",
RowBox[{"resultFunction", "\[Rule]", "evaluate"}]}], "}"}], "]"}]}],
"]"}], ":=",
RowBox[{"Module", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{", "\[IndentingNewLine]",
RowBox[{
RowBox[{"symbols", "=",
RowBox[{"BooleanVariables", "[", "expression", "]"}]}], ",",
"\[IndentingNewLine]", "numOfSymbols", ",", "\[IndentingNewLine]",
"parameterValueTable", ",", "\[IndentingNewLine]", "results", ",",
"\[IndentingNewLine]", "finalTable", ",", "\[IndentingNewLine]",
RowBox[{"fullVals", "=",
RowBox[{"OptionValue", "[", "fullValues", "]"}]}], ",",
"\[IndentingNewLine]",
RowBox[{"resFun", "=",
RowBox[{"OptionValue", "[", "resultFunction", "]"}]}]}],
"\[IndentingNewLine]", "}"}], ",", "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"numOfSymbols", "=",
RowBox[{"Length", "[", "symbols", "]"}]}], ";", "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{"values", " ", "for", " ", "parameter", " ",
RowBox[{"symbols", ":"}]}], " ", "*)"}], "\[IndentingNewLine]",
RowBox[{"parameterValueTable", "=",
RowBox[{"Tuples", "[",
RowBox[{
RowBox[{"{",
RowBox[{"False", ",", "True"}], "}"}], ",", "numOfSymbols"}],
"]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"results", "=",
RowBox[{"Map", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"Function", "[",
RowBox[{"args", ",", "\[IndentingNewLine]",
RowBox[{"resFun", "[",
RowBox[{"expression", ",", "symbols", ",", "args"}], "]"}]}],
"\[IndentingNewLine]", "]"}], ",", "\[IndentingNewLine]",
"parameterValueTable"}], "\[IndentingNewLine]", "]"}]}], ";",
"\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"finalTable", "=",
RowBox[{"Join", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{", "\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{"header", " ", "row"}], " ", "*)"}], "\[IndentingNewLine]",
RowBox[{"Join", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"Map", "[",
RowBox[{"TraditionalForm", ",", "symbols"}], "]"}], ",",
"\[IndentingNewLine]",
RowBox[{"{", "\"\\"", "}"}]}], "\[IndentingNewLine]",
"]"}], "\[IndentingNewLine]", "}"}], ",", "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{"truth", " ", "values"}], " ", "*)"}],
"\[IndentingNewLine]",
RowBox[{"appendAllResults", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"Map", "[",
RowBox[{"ToString", ",", "parameterValueTable", ",",
RowBox[{"{", "2", "}"}]}], "]"}], ",", " ",
RowBox[{"(*", " ",
RowBox[{"arguments", " ", "as", " ", "strings"}], " ", "*)"}],
"\[IndentingNewLine]", "results"}], "\[IndentingNewLine]", "]"}]}],
"\[IndentingNewLine]", "]"}]}], ";", "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
"fullVals", ",", "\[IndentingNewLine]", "finalTable", ",",
"\[IndentingNewLine]",
RowBox[{"finalTable", "/.",
RowBox[{"{",
RowBox[{
RowBox[{"\"\\"", "\[Rule]", "\"\\""}], ",",
RowBox[{"\"\\"", "\[Rule]", "\"\\""}]}], "}"}]}]}],
"\[IndentingNewLine]", "]"}]}]}], "\[IndentingNewLine]", "]"}]}],
"\[IndentingNewLine]", "\n",
RowBox[{"(*", " ",
RowBox[{
"generates", " ", "the", " ", "truth", " ", "table", " ", "and", " ",
"wraps", " ", "it", " ", "in", " ", "the", " ", "grid"}], " ",
"*)"}]}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"truthGrid", "[",
RowBox[{"expression_", ",",
RowBox[{"OptionsPattern", "[",
RowBox[{"{",
RowBox[{
RowBox[{"fullValues", "\[Rule]", "False"}], ",",
RowBox[{"resultFunction", "\[Rule]", "evaluate"}]}], "}"}], "]"}]}],
"]"}], ":=",
RowBox[{"With", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{", "\[IndentingNewLine]",
RowBox[{"tTable", "=",
RowBox[{"truthTable", "[",
RowBox[{"expression", ",", "\[IndentingNewLine]",
RowBox[{"fullValues", "->",
RowBox[{"OptionValue", "[", "fullValues", "]"}]}], ",",
"\[IndentingNewLine]",
RowBox[{"resultFunction", "\[Rule]",
RowBox[{"OptionValue", "[", "resultFunction", "]"}]}]}],
"\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "}"}], ",",
"\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"Grid", "[",
RowBox[{"tTable", ",",
RowBox[{"Frame", "\[Rule]", "All"}]}], "]"}]}], "\[IndentingNewLine]",
"]"}]}], "\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{"logic", " ", "assistant", " ", "GUI"}], " ", "*)"}],
"\[IndentingNewLine]"}], "\n",
RowBox[{
RowBox[{"expression", "=",
RowBox[{"ToExpression", "[", "\"\\"", "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"outputFormat", "=", "CForm"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"drawDiagram", "=", "True"}], ";"}],
"\[IndentingNewLine]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"formsExplained", "=",
RowBox[{"<|", "\[IndentingNewLine]",
RowBox[{
RowBox[{
"\"\\"", "\[Rule]",
"\"\\""}], ",",
"\[IndentingNewLine]",
RowBox[{
"\"\\"", "\[Rule]",
"\"\\""}], ",",
"\[IndentingNewLine]",
RowBox[{"\"\\"", "\[Rule]", "\"\\""}],
",", "\[IndentingNewLine]",
RowBox[{"\"\\"", "\[Rule]", "\"\\""}],
",", "\[IndentingNewLine]",
RowBox[{"\"\\"", "\[Rule]", "\"\\""}],
",", "\[IndentingNewLine]",
RowBox[{"\"\\"", "\[Rule]", "\"\\""}],
",", "\[IndentingNewLine]",
RowBox[{"\"\\"", "\[Rule]", "\"\\""}], ",",
"\[IndentingNewLine]",
RowBox[{"\"\\"", "\[Rule]", "\"\\""}]}],
"\[IndentingNewLine]", "|>"}]}], ";"}],
"\[IndentingNewLine]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"explainForm", "[", "form_", "]"}], ":=",
RowBox[{"If", "[",
RowBox[{
RowBox[{"KeyExistsQ", "[",
RowBox[{"formsExplained", ",", "form"}], "]"}], ",",
"\[IndentingNewLine]",
RowBox[{"Tooltip", "[",
RowBox[{"form", ",",
RowBox[{"formsExplained", "[", "form", "]"}]}], "]"}], ",",
"\[IndentingNewLine]", "form"}], "\[IndentingNewLine]", "]"}]}],
"\[IndentingNewLine]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"formatButton", "[",
RowBox[{"expr_", ",", "format_"}], "]"}], ":=",
RowBox[{"Button", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"Tooltip", "[",
RowBox[{
RowBox[{"Format", "[",
RowBox[{"expr", ",", "format"}], "]"}], ",",
"\"\\""}], "]"}], ",", "\[IndentingNewLine]",
RowBox[{"expression", "=", "expr"}], ",", "\[IndentingNewLine]",
RowBox[{"Appearance", "\[Rule]", "\"\\""}]}],
"\[IndentingNewLine]", "]"}]}],
"\[IndentingNewLine]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"editTermButton", "[",
RowBox[{"expr_", ",", "symbols_", ",", "values_"}], "]"}], ":=",
RowBox[{"With", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{", "\[IndentingNewLine]",
RowBox[{
RowBox[{"res", "=",
RowBox[{"evaluate", "[",
RowBox[{"expr", ",", "symbols", ",", "values"}], "]"}]}], ",",
"\[IndentingNewLine]",
RowBox[{"term", "=",
RowBox[{"makeTerm", "[",
RowBox[{"symbols", ",", "values"}], "]"}]}]}], "\[IndentingNewLine]",
"}"}], ",", "\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"Button", "[", "\[IndentingNewLine]",
RowBox[{"(*", " ", "label", " ", "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"Tooltip", "[",
RowBox[{
RowBox[{"ToString", "[", "res", "]"}], ",",
RowBox[{"If", "[",
RowBox[{
"res", ",", "\"\\"", ",",
"\"\\""}], "]"}]}], "]"}], ",",
"\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"(*", " ", "action", " ", "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"If", "[",
RowBox[{"res", ",", "\[IndentingNewLine]",
RowBox[{"expression", "=",
RowBox[{"And", "[",
RowBox[{"expression", ",",
RowBox[{"Not", "[", "term", "]"}]}], "]"}]}], ",",
RowBox[{"(*", " ",
RowBox[{"remove", " ", "term", " ", "from", " ", "expression"}],
" ", "*)"}], "\[IndentingNewLine]",
RowBox[{"expression", "=",
RowBox[{"Or", "[",
RowBox[{"expression", ",", "term"}], "]"}]}]}], " ",
RowBox[{"(*", " ",
RowBox[{"add", " ", "term", " ", "to", " ", "expression"}], " ",
"*)"}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{"expression", "=",
RowBox[{"BooleanMinimize", "[", "expression", "]"}]}], "*)"}],
"\[IndentingNewLine]",
RowBox[{"(*", " ",
RowBox[{
RowBox[{"Note", ":", " ",
RowBox[{
"doConvert", " ", "here", " ", "would", " ", "recompute", " ",
"the", " ", "new", " ", "expression"}]}], ",",
"\[IndentingNewLine]",
RowBox[{
"but", " ", "with", " ", "the", " ", "risk", " ", "of", " ",
"losing", " ", "some", " ", "symbols", " ", "in", " ", "the", " ",
"resulting", " ", "truth", " ", "table"}], ",",
"\[IndentingNewLine]",
RowBox[{
"as", " ", "some", " ", "symbols", " ", "can", " ", "be", " ",
"eliminated", " ", "by", " ", "BooleanMinimize"}], ",",
"\[IndentingNewLine]",
RowBox[{
"it", " ", "is", " ", "better", " ", "to", " ", "allow", " ", "the",
" ", "user", " ", "do", " ", "it", " ", "explicitly", " ", "with",
" ", "the", " ", "Convert", " ", "button"}]}], " ", "*)"}],
"\[IndentingNewLine]",
RowBox[{"doConvert", "[", "]"}]}], ",", "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"(*", " ", "options", " ", "*)"}], "\[IndentingNewLine]",
RowBox[{"Appearance", "\[Rule]", "\"\\""}]}],
"\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}]}],
"\[IndentingNewLine]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"doConvert", "[", "]"}], ":=",
RowBox[{"Module", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{", "\[IndentingNewLine]",
RowBox[{
"diagram", ",", "\[IndentingNewLine]", "convertedFormsGrid", ",",
"\[IndentingNewLine]", "editableTruthsGrid", ",",
"\[IndentingNewLine]", "diagramCells", ",", "\[IndentingNewLine]",
"altFormCells", ",", "\[IndentingNewLine]", "truthTableCells"}],
"\[IndentingNewLine]", "}"}], ",", "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"If", "[",
RowBox[{"drawDiagram", ",", "\[IndentingNewLine]",
RowBox[{"diagram", "=",
RowBox[{"logicDiagram", "[", "expression", "]"}]}], ",",
"\[IndentingNewLine]",
RowBox[{"diagram", "=", "\"\<\>\""}]}], "\[IndentingNewLine]", "]"}],
";", "\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"convertedFormsGrid", "=",
RowBox[{"Grid", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"Table", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{",
RowBox[{
RowBox[{"explainForm", "[", "form", "]"}], ",",
RowBox[{"formatButton", "[",
RowBox[{
RowBox[{"BooleanConvert", "[",
RowBox[{"expression", ",", "form"}], "]"}], ",",
"outputFormat"}], "]"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{"form", ",",
RowBox[{"{",
RowBox[{
"\"\\"", ",", "\"\\"", ",", "\"\\"", ",",
"\"\\"", ",", "\"\\"", ",", "\"\\"", ",",
"\"\\"", ",", "\"\\"", ",", "\"\\"", ",",
RowBox[{"(*",
RowBox[{"\"\\"", ","}], "*)"}], "\"\\""}],
"}"}]}], "}"}]}], "\[IndentingNewLine]", "]"}], ",",
"\[IndentingNewLine]",
RowBox[{"Frame", "\[Rule]", "All"}]}], "\[IndentingNewLine]",
"]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"editableTruthsGrid", "=",
RowBox[{"truthGrid", "[",
RowBox[{"expression", ",", "\[IndentingNewLine]",
RowBox[{"fullValues", "\[Rule]", "True"}], ",",
"\[IndentingNewLine]",
RowBox[{"resultFunction", "\[Rule]",
RowBox[{"Function", "[",
RowBox[{
RowBox[{"{",
RowBox[{"e", ",", "s", ",", "v"}], "}"}], ",",
RowBox[{"editTermButton", "[",
RowBox[{"e", ",", "s", ",", "v"}], "]"}]}], "]"}]}]}],
"\[IndentingNewLine]", "]"}]}], ";", "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"diagramCells", "=",
RowBox[{"Cells", "[",
RowBox[{
RowBox[{"ButtonNotebook", "[", "]"}], ",",
RowBox[{"CellTags", "\[Rule]", "\"\\""}]}], "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{"diagramCells", "==",
RowBox[{"{", "}"}]}], ",", "\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{"drawDiagram", ",",
RowBox[{
"Print", "[", "\"\\"", "]"}]}],
"]"}], ",", "\[IndentingNewLine]",
RowBox[{"Map", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"Function", "[",
RowBox[{"resultCell", ",", "\[IndentingNewLine]",
RowBox[{"NotebookWrite", "[",
RowBox[{"resultCell", ",", "\[IndentingNewLine]",
RowBox[{"Cell", "[",
RowBox[{
RowBox[{"BoxData", "[",
RowBox[{"ToBoxes", "[", "diagram", "]"}], "]"}], ",",
"\"\