Package: matlog
matlogThis module is well-documented with docstrings and type hints. Usehelp(matlog.tokens)or your editor hints to get familiar with it.Basic usage:frommatlogimportparseexpr=parse("A & B")print(expr.solve(A=1,B=0))# Falseprint(expr.solve(B=0))# Falseprint(expr.table())# prints the truth table of expressionInstallationSimply usepip install matlogor clone repo and launchsetup.pyContentsExpression stringUsing Expression objectTruth tablesEvaluating expressionPartial evaluationSimplifyEquality checkpropositionExpression stringAn expression string is a bunch of tokens (atoms, expressions, operators and literals) in a specific order.
There's a big example of expression string:a | (b -> c) == (~a ^ 1) <- ~zLet's review in detail:AtomsAny letter is considered as an atom. Atoms can be only one letter long and must be divided by operators.LiteralsA literal is1or0(True and False respectively). Literals are treated like atoms.ExpressionsAny expression can contain nested expression strings in brackets. Nested expressions are treated like atoms.Operatorsmatlogexpression syntax supports 6 binary operators and one unary (listed in order of priority, from high to low):~(not / negation)unary: turnsTruetoFalseand vice versa.&(and / conjunction):Trueif (and only if) both operands areTrue|(or / disjunction):Trueif (and only if) any operand isTrue.->(implication):Falseif (and only if) the left operand isTruebut the right isFalse==(equality / biconditional):Trueif (and only if) operands are equal to each other^(xor / exclusive disjunction):Falseif (and only if) operands are equal to each other<-(converse implication):->with reversed operands orderExpressionExpression class is the main class of the module.
There are three ways to create an Expression object:from anexpression string:matlog.parse(expr_str)copying an existing Expression:expr.copy()(same asmatlog.Expression(expr.tokens))deep-copying an existing Expression object:expr.deep_copy()constructing an Expression from tokens (module won't check if it is valid in this case):matlog.Expression([token1, token2...])Truth tablesYou can useexpr.table(keep=None)method to build a truth table for an Expression object.keepparameter can be either1or0(to filter rows with corresponding values) orNoneif you need a full tableEvaluating expressionIf you need a value for a specific set of values, you can use.solve()method like this:frommatlogimportparseexpr=parse("A & B | C")print(expr.solve(A=1,B=0,C=1))# prints 1print(expr.solve({"A":1,"B":0,"C":1}))# you can pass a dictionary tooif you need a result value (release version would provide more convenient ways, of course):expr.value(A=1,B=0,C=1)# 1 (raises an exception if there's not enough data to solve expression)Partial evaluationIf you know some (but not all) values, you can also use.solve(), providing some values:frommatlogimportparseexpr=parse("A & B | C")print(expr.solve(B=0))# prints Cprint(expr.solve({"B":0}))# prints C tooSimplifySmart (smarter than watermelon!) algorithm, simplifies expressions better than bare.solve().This method recursively simplifies complex expressions, so expect it to work slower than (again) bare.solve().frommatlogimportparseexpr=parse("(A | B | C) & ~(A | ~B | C)")print(expr.simplify())# prints ~(A | ~B | C)Equality checkIf you're wondering if expressions are equal (producing the same results with any set of values), you can useExpression.equals(self, other)method:frommatlogimportparseexpr1=parse("A & B")expr2=parse("B & A")print(expr1.equals(expr2))# True