ece 650 - reza babaee propositional logicrbabaeec/ece650/w20/assets/pdf/l9_logic.… · formal...

38
PROPOSITIONAL LOGIC ECE 650 - REZA BABAEE

Upload: others

Post on 15-Oct-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGICECE 650 - REZA BABAEE

Page 2: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

LEARNING OBJECTIVES

▸ Describe propositional logic and the normal forms

▸ Transform propositions to CNF and solve them using resolution

▸ Use the resolution in an algorithm to implement it using a SAT-solvers

Today’s class

Page 3: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

REFERENCES

▸ Chapter 1 Logic for Computer Scientists by Uwe Schöning

▸ Chapter 34 of the 3rd Edition of the CLRS book

Page 4: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

FOUNDATIONS

https://www.coulterfamilycounseling.com/building-a-strong-relationship-foundation/

Page 5: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

FORMAL LOGIC

‣ Definition (Britanica): “the abstract study of propositions, statements, or assertively used sentences and of deductive arguments.”

‣ Formal logic provides:

‣ A language to precisely express knowledge, requirements, facts

‣ A formal way to reason about consequences of given facts rigorously

‣ Components:

‣ Syntax – how does a valid sentence look in the logic

‣ Semantics – what is the meaning of a sentence in the logic

‣ Proof System – a finite set of proof rules that derives (true) sentences using syntactic manipulations

Page 6: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

TRAFFIC LIGHT LOGIC EXAMPLE‣ Syntax:

‣ 3 colours: green, red, yellow

‣ Top to the bottom order

‣ Semantics:

‣ Red: stop

‣ Yellow: if in the intersection leave immediately; o.w., stop

‣ Green: go

‣ Proof system: ‣ if ((green || in-the-intersection) && (!green || in-the-intersection)) then go!

▸ Can we simplify the above statement?

▸ if (in-the-intersection) then go!

Page 7: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

TYPES OF FORMAL LOGIC

▸ Propositional Logic

▸ Predicate Logic

▸ First-Order Logic

▸ Second-Order Logic

▸ Higher-Order Logics

▸ Infinitary Logic

▸ Many-valued Logic

▸ 3-valued Logic

▸ Fuzzy Logic

▸ Modal Logic

▸ Temporal Logic https://iwastesomuchtime.com/82224

Page 8: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

THE PREVALENCE OF LOGIC IN SE

▸ Hardware: The basics of how hardware works

▸ Programming Languages: The semantics of software (programming languages) is based on logic

▸ Algorithms: When developing algorithms and optimizations, many of them can be reduced to logic and solved effectively using automated decision procedures

▸ Requirements Engineering: In software engineering requirements and specification are sometimes defined using formal logic

▸ Software Verification: Testing and verification of software can be solved using logic-based decision procedures

Page 9: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

WHY (PROPOSITIONAL) LOGIC?

1. Many computer programs can be formulated using propositional logic

▸ A program: a sequence of instructions

▸ An instruction: manipulates the memory

▸ At each moment the memory holds a state (8 bits ! states)

▸ An instruction maps one state to another (memory manipulation)

▸ This manipulation can be demonstrated by a Boolean combinational circuit (hardware)

▸ We can encode a program that manipulates a memory with n bits as a propositional formula with n Boolean variables

▸ Your program can be implemented as a solver (SAT solver) that finds a solution for a given propositional formula

28 

Page 10: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

WHY (PROPOSITIONAL) LOGIC?

2. Studying propositional logic allows us to understand and use a SAT solver when necessary

▸ When is it necessary?

▸ Using reduction to SAT (encoding) to solve an NP problem

Page 11: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC BASICS

https://becominghuman.ai/the-logical-automation-7a6d5d8b0a5b

Page 12: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

WHAT IS PROPOSITIONAL LOGIC?

▸ Explores simple grammatical connections such as and, or, and not between simplest “atomic sentences”, a.k.a. propositions ▸ A = “Paris is the capital of France”

▸ B = “mice chase elephants”

▸ C = “ECE-650 is awesome!”

Page 13: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC SYNTAX

▸ An atomic formula has a form Ai , where i = 1, 2, 3

▸ Formulas are defined inductively as follows:

▸ All atomic formulas are formulas

▸ For every formula F, ¬F (called not F) is a formula

▸ For all formulas F and G, F ∧ G (called and) and F ∨ G (called or) are formulas

▸ Abbreviations

▸ use A, B, C, … instead of A1, A2, …

▸ use F1 → F2 instead of ¬F1 ∨ F2 (implication)

▸ use F1 ⟷ F2 instead of (F1 → F2) ∧ (F2 → F1) (iff)

Page 14: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC SYNTAX - FORMAL DEFINITION

Page 15: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC SYNTAX - EXAMPLE

▸ Subformulas:

Page 16: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC SEMANTICS

▸ Truth values: {0 (false), 1 (true)}

▸ D is any subset of the atomic formulas

▸ An assignment A is a map D → {0, 1}

▸ E ⊇ D set of formulas built from D

▸ An extended assignment A’: E → {0, 1} (defined on the next slide)

Page 17: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC SEMANTICS - FORMAL DEFINITION

▸ For an atomic formula Ai in E: A’(Ai) = A(Ai)

▸ A’(F ⋀ G) = 1 if A’(F) = 1 and A’(G) = 1 = 0 otherwise

▸ A’(F ⋁ G) = 1 if A’(F) = 1 or A’(G) = 1 = 0 otherwise

▸ A’(¬F) = 1 if A’(F) = 0 = 0 otherwise

Page 18: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC SEMANTICS - EXAMPLE

Page 19: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC SEMANTICS - TRUTH TABLE

▸ A formula F with n atomic sub-formulas has 2n suitable assignments

▸ Build a truth table enumerating all assignments

Page 20: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC SEMANTICS - TRUTH TABLE OF BASIC OPERATIONS

Page 21: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

PROPOSITIONAL LOGIC SEMANTICS - EXAMPLE

▸ Evaluate using the syntax tree and the truth tables of the basic operations

¬(A ∧ B) ∨ C

Page 22: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

SOME IMPORTANT DEFINITIONS

▸ An assignment A is suitable for a formula F if A assigns a truth value to every atomic proposition of F

▸ An assignment A is a model for F, written A⊧ F, iff

▸ A is suitable for F

▸ A(F) = 1, i.e., F holds under A

▸ A formula F is satisfiable iff F has a model, otherwise F is unsatisfiable (or contradictory)

▸ A formula F is valid (or a tautology), written ⊧ F, iff every suitable assignment for F is a model for F

Page 23: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

DETERMINING SATISFIABILITY VIA A TRUTH TABLE

▸ F is satisfiable iff there is at least one entry with 1 in the truth table of F

▸ Is satisfiable?¬(A ∧ B) ∨ C

Page 24: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

VALIDITY AND SATISFIABILITY

▸ Theorem: A formula F is valid if and only if ¬F is unsatifsiable

▸ Proof:

▸ F is valid ⬄ every suitable assignment for F is a model for F

▸ ⬄ every suitable assignment for ¬ F is not a model for ¬ F

▸ ⬄ ¬ F does not have a model

▸ ⬄ ¬ F is unsatisfiable

Page 25: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

EQUIVALENCE AND NORMAL FORMS

https://www.studyrama.com/international/etudier-a-l-etranger/equivalences-de-diplomes-comment-savoir-87056

Page 26: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

SEMANTIC EQUIVALENCE

▸ Two formulas F and G are (semantically) equivalent, written F ≡ G, iff for every assignment A that is suitable for both F and G, A(F) = A(G)

▸ For example, (F ⋀ G) is equivalent to (G ⋀ F)

▸ Formulas with different atomic propositions can be equivalent

▸ e.g., all tautologies are equivalent to True

▸ e.g., all unsatisfiable formulas are equivalent to False

Page 27: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

SUBSTITUTION THEOREM

▸ Theorem: Let F and G be equivalent formulas. Let H be a formula in which F occurs as a sub-formula. Let H’ be a formula obtained from H by replacing every occurrence of F by G. Then, H and H’ are equivalent.

▸ Proof:

▸ (Let’s talk about proof by induction first…)

Page 28: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

MATHEMATICAL INDUCTION

▸ To proof that a property P(n) holds for all natural numbers n:

1. Show that P(0) is true

2. Show that P(k+1) is true for some natural number k, using an Inductive Hypothesis that P(k) is true

‣ Example: Show that 0 + 1 + … + n =n(n + 1)

2

Page 29: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

INDUCTION ON THE FORMULA STRUCTURE

▸ The definition of a syntax of a formula is an inductive definition

▸ first, define atomic formulas; second, define more complex formulas from simple ones

▸ The definition of the semantics of a formula is also inductive

▸ first, determine value of atomic propositions; second, define values of more complex formulas

▸ The same principle works for proving properties of formulas

▸ To show that every formula F satisfies some property S:

▸ base case: show that S holds for atomic formulae

▸ induction step: assume S holds for an arbitrary fixed formulas F and G. Show that S holds for (F ∧ G), (F ∨ G), and (¬ F)

Page 30: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

SUBSTITUTION THEOREM PROOF

▸ Theorem: Let F and G be equivalent formulas. Let H be a formula in which F occurs as a sub-formula. Let H’ be a formula obtained from H by replacing every occurrence of F by G. Then, H and H’ are equivalent.

▸ Proof: by induction on formula structure

▸ base case: if H is atomic, then F = H, H’ = G, and F ≡ G

▸ inductive step:

▸ case 1: H = ¬ H1

▸ case 2: H = H1 ⋀ H2

▸ case 3: H = H1 ∨ H2

Page 31: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

USEFUL EQUIVALENCES

Page 32: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

NORMAL FORMS

▸ A literal is either an atomic proposition or its negation

▸ A clause is a disjunction of literals

▸ e.g.,

▸ A formula is in Conjunctive Normal Form (CNF) if it is a conjunction of

disjunctions of literals (i.e., a conjunction of clauses):

▸ e.g.,

▸ A formula is in Disjunctive Normal Form (DNF) if it is a disjunction of conjunctions of literals:

p ¬p

p ∨ ¬q ∨ r

n

⋀i=1

(mi

⋁j=1

Li,j)

(p ∨ ¬q) ∧ (r ∨ q)

n

⋁i=1

(mi

⋀j=1

Li, j)

Page 33: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

CONVERTING TO NORMAL FORMS

▸ Theorem: For every formula F, there is an equivalent formula F1 in CNF and F2 in DNF

▸ Proof: by induction on the structure of the formula F

1. Substitute in F every occurrence of a sub-formula of the form

‣ ¬¬G by G

‣ ¬(G ∧ H) by (¬G ∨ ¬H)

‣ ¬(G ∨ H) by (¬G ∧ ¬H)

‣ This is called Negation Normal Form (NNF)

2. Substitute in F each occurrence of a sub-formula of the form

‣ (F ∨ (G ∧ H)) by ((F ∨ G) ∧ (F ∨ H))

‣ ((F ∧ G) ∨ H) by ((F ∨ H) ∧ (G ∨ H))

‣ The resulting formula F is in CNF

‣ the result in CNF might be exponentially bigger than original formula F

Page 34: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

FROM TRUTH TABLES TO NORMAL FORMS

▸ DNF: each entry in the truth table that is ‘1’ is a term in DNF, where the literals that are ‘0’ appear with negation, and themselves otherwise

▸ CNF (dual to DNF): each entry in the truth table that is ‘0’ is a term in CNF, where the literals that are ‘1’ appear with negation, and themselves otherwise

Page 35: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

THE 2-CNF FRAGMENT

▸ A formula F is in 2-CNF iff

▸ F is in CNF

▸ Every clause of F has at most 2 literals

▸ Theorem: There is a polynomial algorithm for deciding wither a a 2-CNF formula F is satisfiable

▸ Not all formulas can be converted to 2-CNF

Page 36: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

3-CNF FRAGMENT

▸ A formula F is in 3-CNF iff

▸ F is in CNF

▸ Every clause of F has at most 3 literals

▸ Theorem: Deciding whether a 3-CNF formula F is satisfiable is at least as hard as deciding satisfiability of an arbitrary CNF formula G

▸ Proof: by effective reduction from CNF to 3-CNF

▸ Let G be an arbitrary CNF formula. Replace every clause of the form with 3-literal clauses

▸ where are fresh atomic propositions not appearing in F

(ℒ0 ∨ … ∨ ℒn)(ℒ0 ∨ b0) ∧ (¬b0 ∨ ℒ1 ∨ b1) ∧ … ∧ (¬bn−1 ∨ ℒn)

bi

Page 37: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

3-CNF-SAT IS NP-COMPLETE

▸ Theorem: Determining the satisfiability of an arbitrary 3-CNF formula is an NP-complete problem.

▸ Application: If you can solve 3-CNF you can solve any NP problem!

▸ Encode a problem into a 3-CNF satisfiability problem

▸ Use an automatic solver, so-called a SAT solver, to find a solution for your problem

▸ No need to write an algorithm whatsoever, just call the SAT solver!

▸ SAT solvers work for all forms of CNF!

Page 38: ECE 650 - REZA BABAEE PROPOSITIONAL LOGICrbabaeec/ece650/w20/assets/pdf/L9_logic.… · FORMAL LOGIC ‣ Definition (Britanica): “the abstract study of propositions, statements,

04-LOGIC