compareBoolean¶
Supported Response Area Types
This evaluation function is supported by the following Response Area components:
TEXT
EXPRESSION
CODE
This function uses SymPy to test two Boolean expressions for equivalence. Expressions are considered equal if they result in the same truth table.
When answering questions on Booleans, it is easy for students to come up with equivalent expressions but in a different form (e.g. if using a Karnaugh map versus by inspection). compareBoolean aims to alleviate some of the frustration that may arise by accepting any response that is equivalent to the correct answer.
Syntax¶
The current syntax expected by this function is based on the bitwise Boolean syntax used in C, Matlab and many other programming languages.
Operator | Meaning | LaTeX |
---|---|---|
A | B |
A OR B |
\(A + B\) |
A & B |
A AND B |
\(A \cdot B\) |
A ^ B |
A XOR B |
\(A \oplus B\) |
~A |
NOT A |
\(\overline{A}\) |
The order of precedence is as follows:
- NOT
- AND
- OR/XOR
Brackets can be used to group terms and specify the order of evaluation.
For example, A & B | C & D
is interpreted as (A & B) | (C & D)
.
Examples¶
The function can understand a wide variety of complex boolean expressions. Here are some examples to illustrate its capabilities. Each pair of expressions is equivalent, and would be marked as "correct" by compareBoolean.
Response | Answer | Comments |
---|---|---|
x & y |
y & x |
A trivial example, but probably the most common way student responses will differ from the answer |
(x & ~y) | (y & ~z) |
x ^ y |
Both expressions are equivalent to a logical exclusive or. |
~(~x & ~y) |
x | y |
In this example de Morgan's laws have been used to find an equivalent representation of the OR operator. |
Inputs¶
Optional parameters¶
There are two optional parameters that can be set: enforce_expression_equality
and disallowed
.
enforce_expression_equality
¶
If this Boolean parameter is true, the response and the answer must be strictly equal, i.e in the same form.
disallowed
¶
This parameter is a list of strings ("and"
, "or"
, "not"
or "xor"
). If one of these strings is present in the list, that operation
will be disallowed. For example, responding A | B
to an answer of ~(~A & ~ B)
would normally be considered correct, but if a
"disallowed": ["or"]
parameter were added, it would be considered incorrect. This could be useful for questions on De Morgan's laws, such as
expressing a function using only NAND gates.
Examples from Integration Tests¶
Trivial comparisons¶
The response and answer are exactly the same, so the response should be considered correct.
Response | Answer | Correct? |
---|---|---|
A & B |
A & B |
✓ |
Multi-character variable names are also supported
Response | Answer | Correct? |
---|---|---|
A & Test |
A & Test |
✓ |
Trivial comparisons, but not identical¶
Variables can appear in any order.
Response | Answer | Correct? |
---|---|---|
B & A |
A & B |
✓ |
The wrong operator is used, so this is incorrect as the two expressions have different truth tables.
Response | Answer | Correct? |
---|---|---|
A | B |
A & B |
✗ |
More complex comparisons¶
XOR can be implemented using NAND or NOR
Using NAND:
Response | Answer | Correct? |
---|---|---|
~(~(A & ~(A & B)) & ~(B & ~(A & B))) |
A ^ B |
✓ |
Using NOR:
Response | Answer | Correct? |
---|---|---|
~(~(~A | ~B) | ~(A | B)) |
A ^ B |
✓ |
A few examples using de Morgan's laws:
Response | Answer | Correct? |
---|---|---|
~(~A & ~(B & C)) |
A | (B & C) |
✓ |
A | ~(~B | ~C) |
A | (B & C) |
✓ |