Presentation is loading. Please wait.

Presentation is loading. Please wait.

Boolean Expression Evaluation CS 270: Math Foundations of CS Jeremy Johnson.

Similar presentations


Presentation on theme: "Boolean Expression Evaluation CS 270: Math Foundations of CS Jeremy Johnson."— Presentation transcript:

1 Boolean Expression Evaluation CS 270: Math Foundations of CS Jeremy Johnson

2 Objective  To use recursive data structure to represent Boolean expressions. To use recursion to evaluate and process expression trees, and to use induction to reason about expression trees.

3 3 Outline  Boolean expressions  Expression trees  Predicate to check for expression trees  Recursive Evaluation  ITE (if-then-else) representation  Conversion and Equivalence  Inductive proof  Simplification

4 4 Boolean Expressions  BExpr :=  Constant: T|F [t | nil]  Variable [symbol]  Negation:  BExpr [(not BExpr)]  And: BExpr  BExpr [(and BExpr BExpr)  Or: BExpr  BExpr [(or BExpr BExpr)]

5 5 Predicate for Boolean Expressions (defunc booleanexprp (expr) :input-contract t :output-contract (booleanp (booleanexprp expr)) (cond ( (is-constant expr) t ) ( (is-variable expr) t ) ( (is-not expr) (booleanexprp (op1 expr)) ) ( (is-or expr) (and (booleanexprp (op1 expr)) (booleanexprp (op2 expr))) ) ( (is-and expr) (and (booleanexprp (op1 expr)) (booleanexprp (op2 expr))) ) ( t nil) ) )

6 Expression Trees Boolean expressions can be represented by a binary tree Internal nodes are operators Leaf nodes are operands Consider p  (1   q): (and p (or t (not q))  p  1  q

7 Evaluation (defun bool-eval (expr env) (cond ( (is-constant expr) expr ) ( (is-variable expr) (lookup expr env) ) ( (is-not expr) (not (bool-eval (op 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-eval (op2 expr) env)) ) ))

8 Evaluation with Contracts (defunc bool-eval (expr env) :input-contract (and (booleanexprp expr) (environmentp env) (all-variables-defined expr env)) :output-contract (booleanp (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-eval (op2 expr) env)) ) ) )

9 If-then-else  The ternary boolean function ite(p,q,r) can be used to represent , , and    p  ite(p,0,1)  p  q  ite(p,1,q)  p  q  ite(p,q,0) p q r ite(p,q,r) 0 0 0 0 1 1 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 1 1

10 Conversion to ite Expression  Any Boolean expression can be converted to an equivalent expression using ite  (bool-eval expr env)  (ite-eval (bool2ite expr) env)  p  1  q ite p 1 q 0 1 01

11 bool2ite (defun bool2ite (expr) (cond ( (is-constant expr) expr ) ( (is-variable expr) expr ) ( (is-not expr) (list 'ite (bool2ite (op1 expr)) nil t) ) ( (is-or expr) (list 'ite (bool2ite (op1 expr)) t (bool2ite (op2 expr))) ) ( (is-and expr) (list 'ite (bool2ite (op1 expr)) (bool2ite (op2 expr)) nil) ) )

12 Ite-eval (defun ite-eval (expr env) (cond ( (is-constant expr) expr ) ( (is-variable expr) (lookup expr env) ) ( (is-ite expr) (if (ite-eval (op1 expr) env) (ite-eval (op2 expr) env) (ite-eval (op3 expr) env)) ) )

13 Equivalence of Conversion  Want to prove that (bool-eval expr env) = (ite-eval (bool2ite expr) env)  Lemma ite 1.  p  ite(p,0,1) 2.p  q  ite(p,1,q) 3.p  q  ite(p,q,0) p q ite(p,0,1)  p ite(p,1,q) p  q ite(p,q,0) p  q 0 0 1 1 0 0 0 0 0 1 1 1 1 1 0 0 1 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1

14 Equivalence of Conversion  (bool-eval expr env) = (ite-eval (bool2ite expr) env)  Proof by induction on expr using Lemma ite  [Base case] constant or variable. In this case (bool2ite expr) = expr and bool-eval and ite- eval return the same thing

15 Equivalence of Conversion  [Not] Assume (bool-eval expr1 env) = (ite-eval (bool2ite expr1))  (ite-eval (bool2ite ‘(not expr1)) env)  (ite-eval ‘(ite (bool2ite expr1) nil t) env) [by def of bool2ite]  (not (ite-eval (bool2ite expr1) env)) [by Lemma ite part 1]  (not (bool-eval expr1 env)) [by IH]  (bool-eval ‘(not expr1) env) [by def of bool-eval]

16 Equivalence of Conversion  [Or] Assume (bool-eval expr1 env) = (ite-eval (bool2ite expr1)) and (bool-eval expr2 env) = (ite- eval (bool2ite expr2))  (ite-eval (bool2ite ‘(or expr1 expr2)) env)  (ite-eval ‘(ite (bool2ite expr1) t (bool2ite expr2)) env) [by def of bool2ite]  (or (ite-eval (bool2ite expr1) env) (ite-eval (bool2ite expr2) env)) [by Lemma ite part 2]  (or (bool-eval expr1 env) (bool-eval expr2 env)) [by IH]  (bool-eval ‘(or expr1 expr2) env) [by def of bool-eval]

17 Equivalence of Conversion  [And] Assume (bool-eval expr1 env) = (ite-eval (bool2ite expr1)) and (bool-eval expr2 env) = (ite- eval (bool2ite expr2))  (ite-eval (bool2ite ‘(and expr1 expr2)) env)  (ite-eval ‘(ite (bool2ite expr1) (bool2ite expr2) nil) env) [by def of bool2ite]  (and (ite-eval (bool2ite expr1) env) (ite-eval (bool2ite expr2) env)) [by Lemma ite part 3]  (and (bool-eval expr1 env) (bool-eval expr2 env)) [by IH]  (bool-eval ‘(and expr1 expr2) env) [by def of bool- eval]

18 Exercise  Implement a recursive function to convert ite expressions to boolean expressions  (ite2bool iexpr)  Use and define the following helper functions  (is-ite expr)  Check for ‘(ite … )  (is-itenot iexpr)  Check for ‘(ite iexpr nil t)  (is-iteor iexpr)  Check for ‘(ite iexpr t iexpr)  (is-iteand iexpr)  Check for ‘(ite iexpr iexpr nil)

19 Solution (defun is-itenot (iexpr) (and (equal (op2 iexpr) nil) (equal (op3 iexpr) t))) (defun is-iteor (iexpr) (equal (op2 iexpr) t)) (defun is-iteand (iexpr) (equal (op3 iexpr) nil))

20 Solution (defun ite2bool (iexpr) (cond ( (is-constant iexpr) iexpr ) ( (is-variable iexpr) iexpr ) ( (is-ite iexpr) (cond ( (is-itenot iexpr) (list 'not (ite2bool (op1 iexpr))) ) ( (is-iteor iexpr) (list 'or (ite2bool (op1 iexpr)) (ite2bool (op3 iexpr))) ) ( (is-iteand iexpr) (list 'and (ite2bool (op1 iexpr)) (ite2bool (op2 iexpr))) ) ))))

21 Solution Remark  Note that there is one overlap in  Not (ite p nil t)  Or (ite p t q)  And (ite p q nil)  (ite p t nil) = (and p t) = (or p nil) = p  This implies (ite2bool (bool2ite ‘(and p t)) = (or p t) not equal to the initial expression  However, (ite2bool (bool2ite expr))  expr, i.e. (booleval expr) = (ite2bool (bool2ite expr))

22 Correctness of ite2bool  Use induction to prove  (equiv (ite2bool (bool2ite expr)) expr)  Base case: expr is a constant or variable  (not expr)  (or expr1 expr2)  (and expr1 expr2)

23 Solution  Show (equiv (ite2bool (bool2ite expr)) expr)  Base case: if expr is a constant or variable then (ite2bool (bool2ite expr)) = (ite2bool expr) = expr [by def]  [Not] Assume (equiv (ite2bool (bool2ite expr)) expr)  (ite2bool (bool2ite (not expr)))  (ite2bool (list ‘ite (bool2ite expr) nil t))) [by def b2ite]  (not (ite2bool (bool2ite expr))) [by def ite2bool and Lemma ite ]  (not expr) [by IH]

24 Solution  [Or] Assume (equiv (ite2bool (bool2ite expr1)) expr1) and (equiv (ite2bool (bool2ite expr2) expr2)  (ite2bool (bool2ite (or expr1 expr2)))  (ite2bool (list ‘ite (bool2ite expr1) t (bool2ite expr2))) [by def of bool2ite]  (or (ite2bool (bool2ite expr1)) (ite2bool (bool2ite expr2))) [by def of ite2bool and Lemma ite]  (or expr1 expr2) [by IH]

25 Solution  [And] Assume (equiv (ite2bool (bool2ite expr1)) expr1) and (equiv (ite2bool (bool2ite expr2) expr2)  (ite2bool (bool2ite (and expr1 expr2)))  (ite2bool (list ‘ite (bool2ite expr1) (bool2ite expr2) nil)) [by def of bool2ite]  (and (ite2bool (bool2ite expr1)) (ite2bool (bool2ite expr2))) [by def of ite2bool and Lemma ite]  (and expr1 expr2) [by IH]

26 Boolean Algebra Laws

27 Simplifying Expression Trees  Constant folding  p  1  q  p 1 p

28 Exercise  Implement and test (bool-simp expr)  (bool-simp expr) returns a simplified boolean expression using the following simplifications 1.evaluate all constant subexpressions 2.(not (not expr)) -> expr 3.(and t expr) -> expr 4.(and expr t) -> expr 5.(and nil expr) -> nil 6.(and expr nil) -> nil 7.(or t expr) -> t 8.(or expr t) -> t 9.(or nil expr) -> expr 10. (or expr nil) -> expr

29 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 routineto perform operator specific simplifiations and constant evaluation.

30 Exercise  Prove the following lemmas 1. (bool-eval '(not expr) env) = (bool-eval (not- simp expr) env) 2.(bool-eval '(and expr1 expr2) env) = (bool-eval (and-simp expr1 expr2) env) 3.(bool-eval '(or expr1 expr2) env) = (bool-eval (or-simp expr1 expr2) env) 4.(bool-eval expr env) = (bool-eval (bool-simp expr) env)

31 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

32 32 Tautology Checker  A program can be written to check to see if a Boolean expression is a tautology.  Simply generate all possible truth assignments for the variables occurring in the expression and evaluate the expression with its variables set to each of these assignments. If the evaluated expressions are always true, then the given Boolean expression is a tautology.  A similar program can be written to check if any two Boolean expressions E1 and E2 are equivalent, i.e. if E1  E2. Such a program has been provided.

33 Satisfiability  A formula is satisfiable if there is an assignment to the variables that make the formula true  A formula is unsatisfiable if all assignments to variables eval to false  A formula is falsifiable if there is an assignment to the variables that make the formula false  A formula is valid if all assignments to variables eval to true (a valid formula is a theorem or tautology)

34 Satisfiability  Checking to see if a formula f is satisfiable can be done by searching a truth table for a true entry  Exponential in the number of variables  Does not appear to be a polynomial time algorithm (satisfiability is NP-complete)  There are efficient satisfiability checkers that work well on many practical problems  Checking whether f is satisfiable can be done by checking if  f is a tautology  An assignment that evaluates to false provides a counter example to validity

35 Propositional Logic in ACL2  In beginner mode and above ACL2S B !>QUERY (thm (implies (and (booleanp p) (booleanp q)) (iff (implies p q) (or (not p) q)))) > Q.E.D. Summary Form: ( THM...) Rules: NIL Time: 0.00 seconds (prove: 0.00, print: 0.00, proof tree: 0.00, other: 0.00) Proof succeeded.

36 Propositional Logic in ACL2 ACL2 >QUERY (thm (implies (and (booleanp p) (booleanp q)) (iff (xor p q) (or p q)))) … **Summary of testing** We tested 500 examples across 1 subgoals, of which 1 (1 unique) satisfied the hypotheses, and found 1 counterexamples and 0 witnesses. We falsified the conjecture. Here are counterexamples: [found in : "Goal''"] (IMPLIES (AND (BOOLEANP P) (BOOLEANP Q) P) (NOT Q)) -- (P T) and (Q T)


Download ppt "Boolean Expression Evaluation CS 270: Math Foundations of CS Jeremy Johnson."

Similar presentations


Ads by Google