Evaluation of Boolean Formulas - Symbolic Logic

Skip to content
Back to top
View this page
Edit this page

# Evaluation of Boolean Formulas

AUTHORS:

- Chris Gorecki (2006): initial version

- Paul Scurek (2013-08-05): updated docstring formatting

EXAMPLES:

We can assign values to the variables and evaluate a formula:

Sage

    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

Python

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

We can change our assignment of values by modifying the dictionary:

Sage

    sage: d['a'] = False
    sage: booleval.eval_formula(t, d)
    False

[Python]

    >>> from sage.all import *
    >>> d['a'] = False
    >>> booleval.eval_formula(t, d)
    False

sage.logic.booleval.eval_f(tree)

  Evaluate the tree.

  INPUT:

  - `tree` – list of three elements corresponding to a branch of a parse tree

  OUTPUT: the result of the evaluation as a boolean value

  EXAMPLES:

  This example illustrates how to evaluate a parse tree:

  Sage

      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

  Python

      >>> from sage.all import *
      >>> import sage.logic.booleval as booleval
      >>> booleval.eval_f(['&', True, False])
      False

      >>> booleval.eval_f(['^', True, True])
      False

      >>> booleval.eval_f(['|', False, True])
      True

sage.logic.booleval.eval_formula(tree, vdict)

  Evaluate the tree and return a boolean value.

  INPUT:

  - `tree` – list of three elements corresponding to a branch of a parse tree

  - `vdict` – dictionary containing variable keys and boolean values

  OUTPUT: the result of the evaluation as a boolean value

  EXAMPLES:

  This example illustrates evaluating a boolean formula:

  Sage

      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

  Python

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

  Sage

      sage: d['a'] = False
      sage: booleval.eval_formula(t, d)
      False

  [Python]

      >>> from sage.all import *
      >>> d['a'] = False
      >>> booleval.eval_formula(t, d)
      False

sage.logic.booleval.eval_op(op, lv, rv)

  Evaluate `lv` and `rv` according to the operator `op`.

  INPUT:

  - `op` – string or character representing a boolean operator

  - `lv` – boolean or variable

  - `rv` – boolean or variable

  OUTPUT: the evaluation of `lv op rv` as a boolean value

  EXAMPLES:

  We can evaluate an operator given the values on either side:

  Sage

      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

  Python

      >>> from sage.all import *
      >>> import sage.logic.booleval as booleval
      >>> booleval.eval_op('&', True, False)
      False

      >>> booleval.eval_op('^', True, True)
      False

      >>> booleval.eval_op('|', False, True)
      True