Author: Ken Levasseur
Views : 69

# An Introduction to Logic using Sage

Here are a few tips on how to get started using Sage to work logic.

import sage.logic.propcalc as propcalc
prop=propcalc.formula
f = prop("(a&b)|(a&c)")
f

(a&b)|(a&c)
f.truthtable()

a b c value False False False False False False True False False True False False False True True False True False False False True False True True True True False True True True True True


f.ifthen(prop("a")).truthtable()

a b c 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
f.implies(prop("a"))

True
propcalc.formula("a&(b|c)").equivalent(f)

True


propcalc.formula("(x&y)->z").truthtable()

x y z 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 False True True True True
f.polish_notation()

'&a|bc'
f.convert_cnf()
f

(a|b|c)&(a|b|~c)&(a|~b|c)&(a|~b|~c)&(~a|b|c)
f.tree()

['|', ['&', 'a', 'b'], ['&', 'a', 'c']]
f.is_contradiction()

False


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

[17, 1]