Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simplification of Boolean Expressions

Similar presentations


Presentation on theme: "Simplification of Boolean Expressions"— Presentation transcript:

1 Simplification of Boolean Expressions
CS 680: Formal Methods

2 Simplifying Expression Trees
Constant folding p 1 q p p 1

3 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

4 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.

5 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)

6 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

7 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) )

8 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))) )

9 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) ) )

10 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) ) )

11 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) ) )

12 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}

13 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}

14 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

15 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}

16 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

17 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

18 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}

19 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}

20 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}

21 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)) )

22 is-simplified (defun is-simplified (expr) (if (is-constant expr) t (no-constants expr)) )

23 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}

24 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}

25 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}

26 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}

27 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)

28 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}

29 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}

30 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)) )


Download ppt "Simplification of Boolean Expressions"

Similar presentations


Ads by Google