Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168733
Image: ubuntu2004
# To browse the code for modules listed here goto: # http://480.sagenb.org/src/logic
from sage.logic import booleval print ( '=== booleval ===\n' ) print ( join(dir(booleval),'\n') ) print ( '\n' )
=== booleval === __builtins__ __doc__ __file__ __name__ __package__ __vars eval_f eval_formula eval_op logicparser
booleval?

File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/booleval.py

Type: <type ‘module’>

Definition: booleval( [noargspec] )

Docstring:

Evaluate Boolean Formulas

Perform boolean evaluation of boolean formulas.

AUTHORS:

  • Chris Gorecki

EXAMPLES:

sage: import sage.logic.booleval as booleval
sage: t = ['|', ['&', 'a', 'b'], ['&', 'a', 'c']]
sage: d = {'a' : True, 'b' : False, 'c' : True}
sage: booleval.eval_formula(t, d)
True
sage: d['a'] = False
sage: booleval.eval_formula(t, d)
False

Classes and functions

from sage.logic import boolformula print ( '=== boolformula ===\n' ) print ( join(dir(boolformula),'\n') ) print ( '\n' )
=== boolformula === BooleanFormula BooleanType BufferType BuiltinFunctionType BuiltinMethodType ClassType CodeType ComplexType DictProxyType DictType DictionaryType EllipsisType FileType FloatType FrameType FunctionType GeneratorType GetSetDescriptorType InstanceType IntType LambdaType ListType LongType MemberDescriptorType MethodType ModuleType NoneType NotImplementedType ObjectType SliceType StringType StringTypes TracebackType TupleType TypeType UnboundMethodType UnicodeType XRangeType __builtins__ __doc__ __file__ __name__ __package__ booleval latex_operators logicparser logictable
boolformula?

File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/boolformula.py

Type: <type 'module'>

Definition: boolformula( [noargspec] )

Docstring:



    Module associated with booleval, logictable, and logicparser, to do 
    boolean evaluation of boolean formulas.
    Formulas consist of the operators &, |, ~, ^, ->, [removed], corresponding
    to and, or, not, xor, if...then, if and only if.  Operators can
    be applied to variables that consist of a leading letter and trailing
    underscores and alphanumerics.  Parentheses may be used to 
    explicitly show order of operation.    
        
    AUTHORS: 
        -- Chris Gorecki
    
    EXAMPLES:
        sage: import sage.logic.propcalc as propcalc
        sage: f = propcalc.formula("a&((b|c)^a->c)[removed]b")
        sage: g = propcalc.formula("boolean[removed]algebra")
        sage: (f&~g).ifthen(f)
        ((a&((b|c)^a->c)[removed]b)&(~(boolean[removed]algebra)))->(a&((b|c)^a->c)[removed]b)
        
     EXAMPLES:
        sage: import sage.logic.propcalc as propcalc
        sage: f = propcalc.formula("a&((b|c)^a->c)[removed]b")
        sage: g = propcalc.formula("boolean[removed]algebra")
        sage: (f&~g).ifthen(f)
        ((a&((b|c)^a->c)[removed]b)&(~(boolean[removed]algebra)))->(a&((b|c)^a->c)[removed]b)    
        
    We can create a truth table from a formula.
        sage: f.truthtable()        
        a      b      c      value
        False  False  False  True
        False  False  True   True
        False  True   False  False
        False  True   True   False
        True   False  False  True
        True   False  True   False
        True   True   False  True
        True   True   True   True
        sage: f.truthtable(end=3)  
        a      b      c      value
        False  False  False  True
        False  False  True   True
        False  True   False  False
        sage: f.truthtable(start=4)   
        a      b      c      value
        True   False  False  True
        True   False  True   False
        True   True   False  True
        True   True   True   True
        sage: propcalc.formula("a").truthtable()
        a      value
        False  False
        True   True
        
    Now we can evaluate the formula for a given set of inputs.
        sage: f.evaluate({'a':True, 'b':False, 'c':True})
        False
        sage: f.evaluate({'a':False, 'b':False, 'c':True})
        True    
        
    And we can convert a boolean formula to conjunctive normal form.
        sage: f.convert_cnf_table()
        sage: f       
        (a|~b|c)&(a|~b|~c)&(~a|b|~c)
        sage: f.convert_cnf_recur()
        sage: f
        (a|~b|c)&(a|~b|~c)&(~a|b|~c)

    Or determine if an expression is satisfiable, a contradiction, or a tautology.
        sage: f = propcalc.formula("a|b")
        sage: f.is_satisfiable()
        True
        sage: f = f & ~f
        sage: f.is_satisfiable()
        False
        sage: f.is_contradiction()
        True
        sage: f = f | ~f
        sage: f.is_tautology()
        True
        
    The equality operator compares semantic equivalence.
        sage: f = propcalc.formula("(a|b)&c")
        sage: g = propcalc.formula("c&(b|a)")
        sage: f == g
        True
        sage: g = propcalc.formula("a|b&c")
        sage: f == g
        False
        
    It is an error to create a formula with bad syntax.
        sage: propcalc.formula("")
        Traceback (most recent call last):
        ...
        SyntaxError: malformed statement
        sage: propcalc.formula("a&b~(c|(d)")
        Traceback (most recent call last):
        ...
        SyntaxError: malformed statement
        sage: propcalc.formula("a&&b")
        Traceback (most recent call last):
        ...
        SyntaxError: malformed statement
        sage: propcalc.formula("a&b a")
        Traceback (most recent call last):
        ...
        SyntaxError: malformed statement
        
        It is also an error to not abide by the naming conventions.
        sage: propcalc.formula("~a&9b")
        Traceback (most recent call last):
        ...
        NameError: invalid variable name 9b: identifiers must begin with a letter and contain only alphanumerics and underscores
from sage.logic import logic print ( '=== logic===\n' ) print ( join(dir(logic),'\n') ) print ( '\n' )
=== logic=== SymbolicLogic __builtins__ __doc__ __file__ __name__ __package__ bin_list eval eval_and_op eval_bin_op eval_iff_op eval_ifthen_op eval_ltor_toks eval_mon_op eval_or_op get_bit operators reduce_bins reduce_monos string tok_list tokenize vars vars_order
logic?

File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/logic.py

Type: <type 'module'>

Definition: logic( [noargspec] )

Docstring:



Manipulation of symbolic logic expressions.

An expression is created from a string that consists of the
operators !, &, |, ->, [removed], which correspond to the logical
functions not, and, or, if then, if and only if, respectively.
Variable names must start with a letter and contain only 
alpha-numerics and the underscore character.

AUTHORS:
    -- Chris Gorecki (2007): initial version
    -- William Stein (2007-08-31): integration into Sage 2.8.4
from sage.logic import logicparser print ( '=== logicparser ===\n' ) print ( join(dir(logicparser),'\n') ) print ( '\n' )
=== logicparser === BooleanType BufferType BuiltinFunctionType BuiltinMethodType ClassType CodeType ComplexType DictProxyType DictType DictionaryType EllipsisType FileType FloatType FrameType FunctionType GeneratorType GetSetDescriptorType InstanceType IntType LambdaType ListType LongType MemberDescriptorType MethodType ModuleType NoneType NotImplementedType ObjectType SliceType StringType StringTypes TracebackType TupleType TypeType UnboundMethodType UnicodeType XRangeType __builtins__ __doc__ __file__ __name__ __op_list __package__ __symbols apply_func parse parse_ltor string tokenize tree_parse
logicparser?

File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/logicparser.py

Type: <type 'module'>

Definition: logicparser( [noargspec] )

Docstring:



Module that creates and modifies parse trees of well formed 
boolean formulas.

AUTHORS:
    -- Chris Gorecki

EXAMPLES:
    sage: import sage.logic.logicparser as logicparser
    sage: s = 'a|b&c'
    sage: t = logicparser.parse(s)
    sage: t
    (['|', 'a', ['&', 'b', 'c']], ['a', 'b', 'c'])
from sage.logic import logictable print ( '=== logictable ===\n' ) print ( join(dir(logictable),'\n') ) print ( '\n' )
=== logictable === Truthtable __builtins__ __doc__ __file__ __name__ __package__ __table __vars_order
logictable?

File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/logictable.py

Type: <type 'module'>

Definition: logictable( [noargspec] )

Docstring:



Module designed for the creation and printing of truth tables that are
associated with a logical statement.  

A logic table is essentially a 2-D array that is created by the statement class
and stored in the private global variable table, along with a list containing 
the variable names to be used, in order.  

The order in which the table is listed essentially amounts to counting in binary.
For instance, with the variables A, B, and C the truth table looks like:

A      B      C      value
False  False  False    ?  
False  False  True     ?
False  True   False    ?
False  True   True     ?
True   False  False    ?
True   False  True     ?
True   True   False    ?
True   True   True     ?

This is equivalent to counting in binary, where a table would appear thus;

2^2 2^1 2^0 value
0   0   0     0
0   0   1     1
0   1   0     2
0   1   1     3
1   0   0     4
1   0   1     5
1   1   0     6
1   1   1     7

Given that a table can be created corresponding to any range of acceptable
values for a given statement, it is easy to find the value of a statement
for arbitrary values of its variables.  

EXAMPLES:
    sage: import sage.logic.propcalc as propcalc
    sage: s = propcalc.formula("a&b|~(c|a)")
    sage: s.truthtable() 
    a      b      c      value
    False  False  False  True
    False  False  True   False
    False  True   False  True
    False  True   True   False
    True   False  False  False
    True   False  True   False
    True   True   False  True
    True   True   True   True

    sage: latex(s.truthtable(5,11))
    \\\begin{tabular}{llll}c & b & a & value \\\hline True & False & True & False \\True & True & False & True \\True & True & True & True\end{tabular}

It is not an error to use nonsensical numeric inputs.
    sage: s = propcalc.formula("a&b|~(c|a)")   
    sage: s.truthtable(5, 9)
    a      b      c      value
    True   False  True   False  
    True   True   False  True   
    True   True   True   True   

    sage: s.truthtable(9, 5)
    a      b      c      value

If one argument is provided, truthtable defaults to the end.
    sage: s.truthtable(-1)
    a      b      c      value
    False  False  False  True
    False  False  True   False
    False  True   False  True
    False  True   True   False
    True   False  False  False
    True   False  True   False
    True   True   False  True
    True   True   True   True

If the second argument is negative, truthtable defaults to the end.
    sage: s.truthtable(4, -2)
    a      b      c      value
    True   False  False  False
    True   False  True   False
    True   True   False  True
    True   True   True   True

NOTES:
    For statements that contain a variable list that when printed is longer than
    the \latex page, the columns of the table will run off the screen.
from sage.logic import propcalc print ( '=== propcalc ===\n' ) print ( join(dir(propcalc),'\n') ) print ( '\n' )
=== propcalc === __builtins__ __doc__ __file__ __name__ __package__ boolformula formula logicparser
propcalc?

File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/propcalc.py

Type: <type ‘module’>

Definition: propcalc( [noargspec] )

Docstring:

Propositional Calculus

Formulas consist of the following operators:

  • & – and
  • | – or
  • ~ – not
  • ^ – xor
  • -> – if-then
  • <-> – if and only if

Operators can be applied to variables that consist of a leading letter and trailing underscores and alphanumerics. Parentheses may be used to explicitly show order of operation.

AUTHORS:

  • Chris Gorecki – propcalc, boolformula, logictable, logicparser, booleval
  • Michael Greenberg – boolopt

EXAMPLES:

sage: import sage.logic.propcalc as propcalc
sage: f = propcalc.formula("a&((b|c)^a->c)<->b")
sage: g = propcalc.formula("boolean<->algebra")
sage: (f&~g).ifthen(f)
((a&((b|c)^a->c)<->b)&(~(boolean<->algebra)))->(a&((b|c)^a->c)<->b)

We can create a truth table from a formula:

sage: f.truthtable()
a      b      c      value
False  False  False  True
False  False  True   True
False  True   False  False
False  True   True   False
True   False  False  True
True   False  True   False
True   True   False  True
True   True   True   True
sage: f.truthtable(end=3)
a      b      c      value
False  False  False  True
False  False  True   True
False  True   False  False
sage: f.truthtable(start=4)
a      b      c      value
True   False  False  True
True   False  True   False
True   True   False  True
True   True   True   True
sage: propcalc.formula("a").truthtable()
a      value
False  False
True   True

Now we can evaluate the formula for a given set of input:

sage: f.evaluate({'a':True, 'b':False, 'c':True})
False
sage: f.evaluate({'a':False, 'b':False, 'c':True})
True

And we can convert a boolean formula to conjunctive normal form:

sage: f.convert_cnf_table()
sage: f
(a|~b|c)&(a|~b|~c)&(~a|b|~c)
sage: f.convert_cnf_recur()
sage: f
(a|~b|c)&(a|~b|~c)&(~a|b|~c)

Or determine if an expression is satisfiable, a contradiction, or a tautology:

sage: f = propcalc.formula("a|b")
sage: f.is_satisfiable()
True
sage: f = f & ~f
sage: f.is_satisfiable()
False
sage: f.is_contradiction()
True
sage: f = f | ~f
sage: f.is_tautology()
True

The equality operator compares semantic equivalence:

sage: f = propcalc.formula("(a|b)&c")
sage: g = propcalc.formula("c&(b|a)")
sage: f == g
True
sage: g = propcalc.formula("a|b&c")
sage: f == g
False

TESTS:

It is an error to create a formula with bad syntax:

sage: propcalc.formula("")
Traceback (most recent call last):
...
SyntaxError: malformed statement
sage: propcalc.formula("a&b~(c|(d)")
Traceback (most recent call last):
...
SyntaxError: malformed statement
sage: propcalc.formula("a&&b")
Traceback (most recent call last):
...
SyntaxError: malformed statement
sage: propcalc.formula("a&b a")
Traceback (most recent call last):
...
SyntaxError: malformed statement

It is also an error to not abide by the naming conventions.
sage: propcalc.formula("~a&9b")
Traceback (most recent call last):
...
NameError: invalid variable name 9b: identifiers must begin with a letter and contain only alphanumerics and underscores

Classes and functions