Simplification of Boolean Expressions CS 680: Formal Methods
Simplifying Expression Trees Constant folding p 1 q p p 1
Exercise Implement and test (bool-simp expr) (bool-simp expr) returns a simplified boolean expression using the following simplifications evaluate all constant subexpressions (not (not expr)) -> expr (and t expr) -> expr (and expr t) -> expr (and nil expr) -> nil (and expr nil) -> nil (or t expr) -> t (or expr t) -> t (or nil expr) -> expr (or expr nil) -> expr
Exercise Simplification (2) is done through the helper routine not-simp. Simplifications (3)-(6) are done through the helper routine and-simp. Simplifications (7)-(10) are done through the helper routine or-simp. bool-simp traverses the boolean expression and recursively simplifies all operands to not, or and and, and calls the appropriate helper routine to perform operator specific simplifications and constant evaluation.
Exercise Prove the following lemmas (bool-eval '(not expr) env) = (bool-eval (not- simp expr) env) (bool-eval '(and expr1 expr2) env) = (bool-eval (and-simp expr1 expr2) env) (bool-eval '(or expr1 expr2) env) = (bool-eval (or-simp expr1 expr2) env) (bool-eval expr env) = (bool-eval (bool-simp expr) env)
Exercise Prove using induction on expr that (bool-eval expr env) = (bool-eval (bool-simp expr) env) Prove by induction that (bool-simp expr) Has no double negations Is either a constant or an expression with no constants Write an is-simplified function to test whether the output of (bool-simp expr) satisfies this property
bool-eval (defun bool-eval (expr env) (cond ( (is-constant expr) expr ) ( (is-variable expr) (lookup expr env) ) ( (is-not expr) (not (bool-eval (op1 expr) env)) ) ( (is-or expr) (or (bool-eval (op1 expr) env) (bool-eval (op2 expr) env)) ) ( (is-and expr) (and (bool-eval (op1 expr) env) )
bool-simp (defun bool-simp (expr) (cond ( (is-constant expr) expr) ( (is-variable expr) expr) ( (is-not expr) (not-simp (bool-simp (op1 expr))) ) ( (is-and expr) (and-simp (bool-simp (op1 expr)) (bool-simp (op2 expr))) ) ( (is-or expr) (or-simp (bool-simp (op1 expr)) (bool-simp (op2 expr))) )
not-simp (defun not-simp (expr) (cond ( (equal expr t) nil) ( (equal expr nil) t) ( (is-not expr) (op1 expr)) ; (not (not expr)) -> expr ( (list 'not expr) ) )
and-simp (defun and-simp (expr1 expr2) (cond ( (equal expr1 t) expr2) ( (equal expr2 t) expr1) ( (equal expr1 nil) nil) ( (equal expr2 nil) nil) ( (list 'and expr1 expr2) ) )
or-simp (defun or-simp (expr1 expr2) (cond ( (equal expr1 t) t) ( (equal expr2 t) t) ( (equal expr1 nil) expr2) ( (equal expr2 nil) expr1) ( (list 'or expr1 expr2) ) )
Theorem 1 (bool-eval (list ‘not expr) env) = (bool-eval (not-simp expr) env) Case 1: Assume expr = t (bool-eval (not-simp t) env) = (bool-eval nil env) {def of not-simp} = nil {def of bool-eval} (bool-eval (list ‘not t) env) = (not (bool-eval t env)) {def of bool-eval} = (not t) = nil Case 2: Assume the expr = nil (bool-eval (list ‘not nil) env) = (bool-eval (not-simp nil) env) {similar proof} Case 3: Assume that expr is not a constant (bool-eval (not-simp expr) env) = (bool-eval (list ‘not expr) env) {def of not-simp}
Theorem 2 (bool-eval (list ‘and expr1 expr2) env) = (bool-eval (and-simp expr1 expr2) env) Case 1: Assume expr1 = t (bool-eval (and-simp t expr2) env) = (bool-eval expr2 env) {def of and-simp} = (and t (bool-eval expr2 env)) {property of and} = (bool-eval (list ‘and t expr2) env) {def of bool-eval} Case 2: Assume expr1 = nil (bool-eval (and-simp nil expr2) env) = (bool-eval nil env) {def of and-simp} = (and nil (bool-eval expr2) env) {property of and} = (bool-eval (list ‘and nil expr2) env) {def of bool-eval}
Theorem 2 (bool-eval (list ‘and expr1 expr2) env) = (bool-eval (and-simp expr1 expr2) env) Case 3: Assume expr2 = t, Case 4: expr2 = nil {analogous to Cases 1,2} Case 5: Neither expr1 nor expr2 is a constant (bool-eval (and-simp expr1 expr2) env) = (bool-eval (list ‘and expr1 expr2) env) {def of and-simp} Property of and expr2 (and t expr2) (and nil expr2) nil t t nil nil nil nil nil nil
Theorem 3 (bool-eval (list ‘or expr1 expr2) env) = (bool-eval (or-simp expr1 expr2) env) Case 1: Assume expr1 = t (bool-eval (or-simp t expr2) env) = (bool-eval t env) {def of or-simp} = (or t (bool-eval expr2 env)) {property of or} = (bool-eval (list ‘or t expr2) env) {def of bool-eval} Case 2: Assume expr1 = nil (bool-eval (or-simp nil expr2) env) = (bool-eval expr2 env) {def of or-simp} = (or nil (bool-eval expr2) env) {property of or} = (bool-eval (list ‘or nil expr2) env) {def of bool-eval}
Theorem 3 (bool-eval (list ‘or expr1 expr2) env) = (bool-eval (or-simp expr1 expr2) env) Case 3: Assume expr2 = t, Case 4: expr2 = nil {analogous to Cases 1,2} Case 5: Neither expr1 nor expr2 is a constant (bool-eval (or-simp expr1 expr2) env) = (bool-eval (list ‘or expr1 expr2) env) {def of and-simp} Property of or expr2 (or t expr2) (or nil expr2) t t t t t nil t nil t
Theorem 4 (bool-eval expr env) = (bool-eval (bool-simp expr) env) Proof is by induction on expr. Base cases. Case 1) expr = constant (bool-eval (bool-simp expr) env) = (bool-eval expr env) {def of bool-eval} Case 2) expr = variable
Theorem 4 Case 3) expr = (not expr1) Assume (bool-eval expr1 env) = (bool-eval (bool-simp expr1) env) [IH] (bool-eval (bool-simp (list ‘not expr1)) env) = (bool-eval (not-simp (bool-simp expr1)) env) {def of bool-simp} = (bool-eval (list ‘not (bool-simp expr1)) env) {Theorem 1} = (not (bool-eval (bool-simp expr1) env)) {def of bool-eval} = (not (bool-eval expr1 env)) {IH} = (bool-eval (list ‘not expr1) env) {def of bool-eval}
Theorem 4 Case 4) expr = (and expr1 expr2) Assume (bool-eval expr1 env) = (bool-eval (bool-simp expr1) env) (bool-eval expr2 env) = (bool-eval (bool-simp expr2) env) (bool-eval (bool-simp (list ‘and expr1 expr2)) env) = (bool-eval (and-simp (bool-simp expr1) (bool-simp expr2) env) {def of bool-simp} = (bool-eval (list ‘and (bool-simp expr1) (bool-simp expr2) env) {Theorem 2} = (and (bool-eval (bool-simp expr1) env) (bool-eval (bool-simp expr2) env) {def of bool-eval} = (and (bool-eval expr1 env) (bool-eval expr2 env)) {IH} = (bool-eval (list ‘and expr1 expr2) env) {def of bool-eval}
Theorem 4 Case 5) expr = (and expr1 expr2) Assume (bool-eval expr1 env) = (bool-eval (bool-simp expr1) env) (bool-eval expr2 env) = (bool-eval (bool-simp expr2) env) (bool-eval (bool-simp (list ‘or expr1 expr2)) env) = (bool-eval (or-simp (bool-simp expr1) (bool-simp expr2) env) {def of bool- simp} = (bool-eval (list ‘or (bool-simp expr1) (bool-simp expr2) env) {Theorem 3} = (or (bool-eval (bool-simp expr1) env) (bool-eval (bool-simp expr2) env) {def of bool-eval} = (or (bool-eval expr1 env) (bool-eval expr2 env)) {IH} = (bool-eval (list ‘or expr1 expr2) env) {def of bool-eval}
no-constants (defun no-constants (expr) (cond ( (is-constant expr) nil) ( (is-variable expr) t) ( (is-not expr) (no-constants (op1 expr)) ) ( (is-and expr) (and (no-constants (op1 expr)) (no-constants (op2 expr))) ) ( (is-or expr) (and (no-constants (op1 expr)) )
is-simplified (defun is-simplified (expr) (if (is-constant expr) t (no-constants expr)) )
Theorem 5 (is-simplified (bool-simp expr)) Proof is by induction on expr. Base cases. Case 1) expr = constant = (is-simplified expr) {def of bool-simp} = (if (is-constant expr) t (no-constants expr)) {def of is-simplified} = t { def of is-constant, if axiom}
Theorem 5 (is-simplified (bool-simp expr)) Proof is by induction on expr. Base cases. Case 2) expr = variable = (is-simplified expr) {def of bool-simp} = (if (is-constant expr) t (no-constants expr)) {def of is-simplified} = (no-constants expr) {if axiom} = (if (is-variable expr) t …) = t {def of no-constants, is-variable, if axiom}
Theorem 5 Case 3) expr = (not expr1) Assume (is-simplified (bool-simp expr1)) [IH] (is-simplified (bool-simp (list ‘not expr1))) = (is-simplified (not-simp (bool-simp expr1))) {def of bool-simp} Since (is-simplified (bool-simp expr1)) there are two possibilities {def of is-simplified} Case 3a) (is-constant (bool-simp expr1)) = t (is-constant (not-simp (bool-simp expr1)) = t {def of not-simp} (is-simplified (not-simp (bool-simp expr1))) = t {def of is-simplified}
Theorem 5 Case 3b) (is-constant (bool-simp expr1)) = nil (no-constants (bool- simp expr1)) = t (is-simplified (not-simp (bool-simp expr1)) = (is-simplified (list ‘not (bool-simp expr1))) {def of not-simp and assumption that (is-constant (bool-simp expr1)) = nil } = (no-constants (list ‘not (bool-simp expr1))) {def of is-simplified} = (no-constants (bool-simp expr1)) {def of no-constants} = t {IH}
Theorem 5 Case 4) expr = (and expr1 expr2) Assume (is-simplified (bool-simp expr1)) (is-simplified (bool-simp expr2)) (is-simplified (bool-simp (list ‘and expr1 expr2))) = (is-simplified (and-simp (bool-simp expr1) (bool-simp expr2))) {def of bool-simp} Since (is-simplified (bool-simp expr1)) (is-simplified (bool-simp expr2)) There are three cases to consider 4a) (is-constant (bool-simp expr1) 4b) (is-constant (bool-simp expr2) 4c) (no-constants (bool-simp expr1)) (no-constants (bool-simp expr2)
Theorem 5 4a) (is-constant (bool-simp expr1) If (bool-simp expr1) = nil (is-simplified (and-simp (bool-simp expr1) (bool-simp expr2))) = (is-simplified nil) = t {def of and-simp, def of is-simplified} If (bool-simp expr1) = t = (is-simplified (bool-simp expr2)) = t {IH}
Theorem 5 4b) (is-constant (bool-simp expr2) This case is identical to case (4a) with expr1 and expr2 swapped. 4c) (no-constants (bool-simp expr1)) (not-constants (bool-simp expr2) (is-simplified (and-simp (bool-simp expr1) (bool-simp expr2))) = (is-simplified (list ‘and (bool-simp expr1) (bool-simp expr2))) {def of and- simp} = (no-constants (list ‘and (bool-simp expr1) (bool-simp expr2))) {def of is- simplified} =(and (no-constants (bool-simp expr1) (no-constants (bool-simp expr2)))) {def of no-constants} =(and t t) = t {IH and evaluation of and}
no-double-negatives (defun no-double-negatives (expr) (cond ( (is-constant expr) t) ( (is-variable expr) t) ( (is-not expr) (if (is-not (op1 expr)) nil (no-double-negatives (op1 expr))) ) ( (is-and expr) (and (no-double-negatives (op1 expr)) (no-double-negatives (op2 expr))) ) ( (is-or expr) (and (no-double-negatives (op1 expr)) )