Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Worksheets related to Applied Discrete Structures

Views: 15776
Image: ubuntu2004
Kernel: SageMath (stable)

An Introduction to Logic using Sage

Applied Discrete Structures by Alan Doerr & Kenneth Levasseur is licensed under a Creative Commons Attribution - Noncommercial - No Derivative Works 3.0 United States License.

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.

Here are the symbols used in Sage.

and ....&
or ....|
not ....~
import sage.logic.propcalc as propcalc prop=propcalc.formula x = prop("(p&~q)|(r&p)") x
(p&~q)|(r&p)
x.truthtable()
p q r value False False False False False False True False False True False False False True True False True False False True True False True True True True False False True True True True

Something that is equivalent to x?

It's a cop-out but you could answer x here. ... and the same for the other two parts.

x.iff(x).truthtable()
p q r value False False False True False False True True False True False True False True True True True False False True True False True True True True False True True True True True

Here is one that is a bit more .

w=prop("(p&~q&~r)|(p&~q&r)|(p&q&r)") w
(p&~q&~r)|(p&~q&r)|(p&q&r)
x.iff(w).truthtable()
p q r value False False False True False False True True False True False True False True True True True False False True True False True True True True False True True True True True
x.equivalent(w)
True

Something that implies x?

Here you need a proposition that has the property that when it is true, is also true. Looking at the truth table, one proposition that works is "p and q and r." Let's test it.

y=prop("p&q&r")
y.ifthen(x).truthtable()
p q r value False False False True False False True True False True False True False True True True True False False True True False True True True True False True True True True True
y.implies(x)
True

Something that is implied by x?

There are lots of pos 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.

p=prop("p")
x.implies(p)
True

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 [removed]n greater than 8 relies on the solution of the previous value, [removed]n-1, in the same way as we can verify by induction that if the case of [removed]n-1 can be solved, then we can use it to solve the case of [removed]n.

def ThreeFive(n): if n==8: return [1,1] previous=ThreeFive(n-1) if previous[1]>0: return [previous[0]+2,previous[1]-1] else: return [previous[0]-3,previous[1]+2]
ThreeFive(56)

Thanks to Richard Voss for the following code that produces a truth table that is more compatible with the text.

def TruthTable(formula): txt = str(propcalc.formula(formula).truthtable()).replace("value",formula) return txt.replace("False","0").replace("True ","1").replace(" "," ")
print TruthTable('((p | ~q)& ~p) ->~q')
p q ((p | ~q)& ~p) ->~q 0 0 1 0 1 1 1 0 1 1 1 1

Testing the validity of a logical argument.

Consider the argument ab,cd,a¬cba \lor b, c\land d,a\rightarrow \neg c \Rightarrow b

p1=prop("a|b") p2=prop("c&d") p3=prop("a->~c") concl=prop("b")
propcalc.valid_consequence(concl,p1,p2,p3)
True
propcalc.valid_consequence(prop("~b"),p1,p2,p3)
False
c,r1,r2=propcalc.get_formulas("b", "c->b", "c")
propcalc.valid_consequence(c,r1,r2)
True
propcalc.valid_consequence(p2,p1,c)
False
q1,q2,q3,q4=propcalc.get_formulas("c->b", "c->a", "a->~b","c") cx=prop("a&b")
propcalc.valid_consequence(cx,q1,q2,q3,q4)
True
propcalc.valid_consequence(~cx,q1,q2,q3,q4)
True
propcalc.consistent(q1,q2,q3,q4)
False