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

sage.logic.booleval.eval_f(tree)

This function can be applied to a parse tree to evaluate it.

INPUT:

  • tree – a list of three elements corresponding to a branch of a parse tree.

OUTPUT:

  • Returns a boolean evaluation of the tree.

EXAMPLES:

sage: import sage.logic.booleval as booleval
sage: booleval.eval_f(['&', True, False])
False
sage: booleval.eval_f(['^', True, True])
False
sage: booleval.eval_f(['|', False, True])
True
sage.logic.booleval.eval_formula(tree, vdict)

Evaluates the tree using the boolean values contained in dictionary and returns a single boolean value.

INPUT:

  • tree – a list of three elements corresponding to a branch of a parse tree.
  • vdict – a dictionary containing variable keys and boolean values.

OUTPUT:

  • Returns the boolean evaluation of a boolean formula.

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
sage.logic.booleval.eval_op(op, lv, rv)

This function evaluates lv and rv according to the operator op.

INPUT:

  • op – a string or character representing a boolean operator.
  • lv – a boolean or variable.
  • rv – a boolean or variable.

OUTPUT:

  • Returns the evaluation of lv op rv.

EXAMPLES:

sage: import sage.logic.booleval as booleval
sage: booleval.eval_op('&', True, False)
False
sage: booleval.eval_op('^', True, True)
False
sage: booleval.eval_op('|', False, True)
True

Table Of Contents

Previous topic

Propositional Calculus

This Page