Simplification of Boolean Expressions

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Functional Programming Lecture 13 - induction on lists.
Introduction to Proofs
Types of Logic Circuits
Disjunctive Normal Form CS 680: Formal Methods Jeremy Johnson.
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
Section 7.4: Closures of Relations Let R be a relation on a set A. We have talked about 6 properties that a relation on a set may or may not possess: reflexive,
Termination Analysis Math Foundations of Computer Science.
Proving Facts About Programs With a Proof Assistant John Wallerius An Example From: Isabelle/HOL, A Proof Assistant for Higher Order Logic, By T. Nipkow,
Brief Introduction to Logic. Outline Historical View Propositional Logic : Syntax Propositional Logic : Semantics Satisfiability Natural Deduction : Proofs.
1 Section 10.1 Boolean Functions. 2 Computers & Boolean Algebra Circuits in computers have inputs whose values are either 0 or 1 Mathematician George.
1 A Single Final State for Finite Accepters. 2 Observation Any Finite Accepter (NFA or DFA) can be converted to an equivalent NFA with a single final.
1.3 – AXIOMS FOR THE REAL NUMBERS. Goals  SWBAT apply basic properties of real numbers  SWBAT simplify algebraic expressions.
Propositional Calculus Math Foundations of Computer Science.
Propositional Calculus CS 680: Formal Methods in Verification Computer Systems Jeremy Johnson.
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
Induction Schemes Math Foundations of Computer Science.
Module 4.  Boolean Algebra is used to simplify the design of digital logic circuits.  The design simplification are based on: Postulates of Boolean.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Copyright © Curt Hill Mathematical Logic An Introduction.
Type Safety Kangwon National University 임현승 Programming Languages.
Second-Order Functions and Theorems in ACL2 Alessandro Coglio Workshop 2015 Kestrel Institute.
CS 611: Lecture 6 Rule Induction September 8, 1999 Cornell University Computer Science Department Andrew Myers.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
Disjunctive Normal Form CS 270: Math Foundation of CS Jeremy Johnson.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
ECE 3110: Introduction to Digital Systems Chapter #4 Review.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
Advanced Formal Methods Lecture 4: Isabelle – Types and Terms Mads Dam KTH/CSC Course 2D1453, Some material from Paulson.
Functional Programming: Lisp MacLennan Chapter 10.
Simplifying Algebraic Expressions. 1. Evaluate each expression using the given values of the variables (similar to p.72 #37-49)
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Boolean Expression Evaluation CS 270: Math Foundations of CS Jeremy Johnson.
CSE Winter 2008 Introduction to Program Verification February 5 calculating with simplify.
Simplify Radical Expressions Using Properties of Radicals.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
1 Interactive Computer Theorem Proving CS294-9 September 7, 2006 Adam Chlipala UC Berkeley Lecture 3: Data structures and Induction.
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Chapter 11 (Part 1): Boolean Algebra
14:332:231 DIGITAL LOGIC DESIGN Boolean Algebra
ECE 2110: Introduction to Digital Systems
Simplify Radical Expressions Using Properties of Radicals
Propositional Calculus: Boolean Functions and Expressions
ECE 3110: Introduction to Digital Systems
Bellwork (this is done on loose leaf paper)
2-4 The Distributive Property
CHAPTER 3 SETS AND BOOLEAN ALGEBRA
Disjunctive Normal Form
Propositional Calculus: Boolean Functions and Expressions
Proving Properties of Recursive Functions and Data Structures
Proving Properties of Recursive List Functions
Propositional Calculus: Boolean Algebra and Simplification
Elementary Metamathematics
Boolean Algebra.
Algebraic Limits and Continuity
The Metacircular Evaluator
Properties of Functions
Disjunctive Normal Form
Lecture 26: The Metacircular Evaluator Eval Apply
More on Functions and Their Graphs
1.3 – AXIOMS FOR THE REAL NUMBERS
Boolean Algebra How gates get picked.
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Design of Digital Circuits Lab 1 Supplement: Drawing Basic Circuits
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

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