# Macro Library logic

Version 1.0: April 18, 2022
Created by Avery Flowers
A collection of functions designed to aid in the creation of basic logic questions.
Note: All variables used in these functions must be single alphabetical characters. Greek words or symbols are not supported.
Note: The valid logical operations are 'and', 'or', 'xor', 'neg', 'implies' and 'iff'. Other synonymous words (such as 'not', 'ifthen', 'oplus', 'wedge', 'vee') can be used as well.

- logicallyequivalent
- logcallyimplies
- logicevaluate
- logicevaluateall
- logicrand
- logicmakepretty
- logicsteps
- truthtable
- truthtableanswers
- logiclegend

### logicallyequivalent(exp1,exp2,vars)
Input: | exp1: A logical expression. exp2: A logical expression. vars: An array/list of variables.
Output: | 1 (TRUE) if exp1 is logically equivalent to exp2. 0 (FALSE) otherwise.
### logcallyimplies(exp1,exp2,vars)
Input: | exp1: A logical expression. exp2: A logical expression. vars: An array/list of variables.
Output: | 1 (TRUE) if exp1 logically implies exp2. 0 (FALSE) otherwise.
### logicevaluate(exp,vars,truths)
Input: | exp: A logical expression. vars: An array (or list) of variables contained within the expression. truths: An array (or list) the same size as vars, containing binary or boolean values to be assigned to each entry in vars.
Output: | A binary digit corresponding to the truth value of exp if each variable in vars is replaced with the corresponding binary digit (truth value) in truths
Example: | logicevaluate('a implies b','a,b','1,0') would output 0 (FALSE), since a->b is false when a=1 and b=0.
### logicevaluateall(exp,vars)
Input: | exp: A logical expression. vars: An array (or list) of variables contained within the expression.
Output: | A bitstring of length 2^|vars|. The bit in position i (indexed at 0) is the result of the logicevaluate function with the truth array equalling the binary representation of i.
Example: | logicevaluateall('a and b','a,b') would output array(0,0,0,1) The entry at index 2 (binary 10) corresponds to evaluating 'a and b' with a=1 and b=0. Remember that arrays are 0-indexed.
### logicrand(vars,ops,bnum,unum)
Input: | vars: An array (or list) of variables. ops: An array (or list) of logical binary operations (and,or,xor,implies,iff) bnum: An array (or list) of integers between 1 and 4 unum: An array (or list) of integers between 0 and 2*(the minimum entry in bnum)+1
Output: | A random logical expression containing variables from the vars array, operations from the ops array. The number of binary operations will be a random element of bnum. The number of negations will be a random element of unum. Tries to avoid 'silly' expressions such as (p and q) or (q and p) for example.
Example: | logicrand("p,q,r","and,or,xor",2,1) will return a random expression using p, q, r with 2 binary operations (and, or, xor only) and 1 negation symbol. For instance, it could return the string 'r and not (p xor q)'.
### logicmakepretty(exp)
Input: | exp: A logical expression.
Output: | Converts the output of the logical functions here to a format to be 'prettified' (removing unnecessary parentheses, double negations, etc.) for display in the Question Text
### logicsteps(exp)
Input: | exp: A logical expression.
Output: | An array of logical subexpressions of exp, each entry applying one operation to the previous entry until the last entry is exp. This shows the 'steps' required to evaluate or construct exp.
Example: | logicsteps('(p and q) or not p') would return array('p and q', 'not p', '(p and q) or not p') since these are the steps necessary to evaluate '(p and q) or not p'.
### truthtable(exp, [showResult=FALSE], [showSteps=TRUE], [offset=0])
Input: | exp: A logical expression. showResult: If FALSE (default), each entry in the constructed truth table (beyond the atomic variables) will contain an answerbox [AB#]. If TRUE, the constructed truth table will be completed with T's and F's. If 2, the truth table will be filled in, and [AB#] will be placed in the header for the expression(s). If 3, the truth table will be filled in, and a single [AB] will be placed in the header for hte expression. showSteps: If TRUE (default), the constructed truth table will have a column for each subexpression of exp. If FALSE only the atomic variables and exp will be included in the table. offset: Only applies when showResult is FALSE. Gives the index of the first answerbox to be contained in the truth table. For instance, if offset=3, then the first entry in the truth table will be '[AB3]'.
Output: | A string containing HTML code for a truth table for exp, using T's and F's for true and false.
Example: | truthtable('(p and q) or not p',TRUE,TRUE) would return a string for the table shown below (but in math display mode):
p | q | (p and q) | neg p | (p and q) or neg p
--- | --- | --- | --- | ---
T | T | T | F | T
T | F | F | F | F
F | T | F | T | T
F | F | F | T | T
Note: | This does not change the $anstypes or $answer values. It should be used with truthtableanswers function below.
### truthtableanswers(exp, [showSteps=TRUE])
Input: | exp: A logical expression. showSteps: If TRUE (default), the constructed truth table will have a column for each subexpression of exp. If FALSE only the atomic variables and exp will be included in the table.
Output: | An array of binary truth values containing the answer to each answerbox in the truthtable function.
Note: | This is useful for autogenerating answers to the answerboxes found in truthtable. It does not affect $anstypes or $answer. It is recommended you place something like the following in the Common Control box: $exp = 'p and q or not r' $table = truthtable($exp, FALSE,TRUE) $tableanswers = truthtableanswers($exp,TRUE) $numboxes = count($tableanswers)-1 for($i=0..$numboxes){ $anstypes[$i] = "choices" $displayformat[$i] = "select" $questions[$i] = array('F','T') // Order is important: F=0 and T=1 $noshuffle[$i] = "all" $answer[$i] = $tableanswers[$i] }
### logiclegend()
Output: | An simple string of HTML syntax that generates a table showing keyboard shortcuts for entering logic expressions.