contextBoolean.pl - Implements a MathObject class for Boolean expressions
Load this file:
loadMacros('contextBoolean.pl');
and then select the context:
Context('Boolean');
This constant recognizes two constants by default, T
and F
. The following are all equivalent:
$T = Compute('1');
$T = Boolean('T');
$T = Context()->T;
$T = context::Boolean->T;
By default, this context has two variables, p
and q
. More variables can be added through the usual means of modifying context:
Context->variables->add( r => 'Boolean' );
Changing the LaTeX representations of the boolean operators is handled through the operators or
, and
, xor
, and not
. Note the extra space following the LaTeX command.
Context->operators->set( not => { TeX => '\neg ' } );
Modifications to the operators should be applied to the string versions of each operator: 'or', 'xor', 'and', and 'not'; rather than to any of the following aliases or alternatives.
The 'or' operator is indicated by or
, +
, \\/
, wedge
, or unicode x{2228}
.
The 'and' operator is indicated by and
, *
, whitespace (as with implicit multiplication), /\\
, vee
, or unicode x{2227}
.
The 'xor' operator is indicated by xor
, \
\<>, oplus
, or unicodes x{22BB}
, x{2295}
.
The 'not' operator is indicated by not
, -
, !
, ~
, or unicodes x{00AC}
, x{223C}
.
A right-associative version of the 'not' operator is also available by using '
or `
following the expression to be negated.
setPrecedence
This context supports two paradigms for operation precedence: equal
(default) and oxan
.
The default setting, equal
, gives all boolean operations the same priority, meaning that parenthesis are the only manner by which an expression will evaluate operations to the right before those to the left.
$a = Compute("T or T and F"); # $a == F
The oxan
setting priortizes or
< xor
< and
< not
.
Context()->setPrecedence('oxan');
$b = Compute("T or T and F"); # $b == T
The context also handles reduceConstants
with the following reduction rules:
'x||1'
$f = Formula('p or T')->reduce; # $f == T
'x||0'
$f = Formula('p or F')->reduce; # $f == Formula('p')
'x&&1'
$f = Formula('p and T')->reduce; # $f == Formula('p')
'x&&0'
$f = Formula('p and F')->reduce; # $f == F
'!!x'
$f = Formula('not not p')->reduce; # $f == Formula('p');
Boolean Formula objects are considered equal whenever the two expressions generate the same truth table.
$f = Formula('not (p or q)');
$g = Formula('(not p) and (not q)');
# $f == $g is true