Logic for Computer Scientists - Propositional Logic Basics
Formal Logic
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
Example: Traffic Light Logic
- Syntax:
- 3 colours: green, red, yellow
- Semantics:
- Red: stop, Yellow: if in the intersection leave immediately otherwise stop, Green: go
- Proof system:
- if (in-the-intersection) then go!
The Prevalence of Logic in Software Engineering
- 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
Why study propositional logic?
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 ! 28 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
Studying propositional logic allows us to understand and use a SAT solver when necessary
- When is it necessary to use a SAT solver
- Using reduction to SAT (encoding) to solve an NP problem
Propositional Logic Basic
Propositional Logic Syntax
-
An atomic formula has a form , where
-
Formulas are defined inductively as follows:
- All atomic formulas are formulas
- For every formula (called not , negation of ) is a formula
- For all formulas and (called and) and (called or) are formulas
-
Abbreviations
- use instead of (implication)
- use instead of (if and only if)
- is called negation of . (not F)
- is the conjunction of and . (AND)
- is called disjunction of and (OR)
Any formula which occurs in another formula is called a subformula of .
Propositional Logic Semantics
- Truth values: {0 (false), 1 (true)}
- is any subset of the atomic formulas
- An assignment is a function such that (assigning true values).
- e.g.
- Note that is different from
- ⊇ set of formulas that can be built from
- An extended assignment such that (assigning true values).
- e.g.
- Note that is different from
For every atomic formula .
Since is an extension of ( and agree on ), from now on, we drop the distinction between and and just write . (The reason for this temporary distinction was to be able to define formally.)
Truth Table
- A formula with atomic sub-formulas has suitable assignments
- Build a truth table enumerating all assignments
Important Definitions
Let be a formula and let be an assignment, i.e. a mapping from a subset of to {0,1}.
suitable
- An assignment is suitable for a formula if assigns a truth value to every atomic proposition of
model
- If is suitable for F, and if , then we write . In this case we say F holds under the assignment , or is model for F.
- Otherwise we write , and say: under the assignment , does not hold, or is not a model for .
- Therefore: An assignment is a model for , written , if and only if
- is suitable for
- , i.e., holds under
satisfiable
- A formula is satisfiable if and only if has a model, otherwise is unsatisfiable (or contradictory)
valid
- A formula is valid (or a tautology), written , if and only if every suitable assignment for is a model for
- or if is unsatisfiable.
Determining Satisfiability via a Truth Table
- F is satisfiable if and only if there is at least one entry with 1 in the truth table of F
Validity and Satisfiability
- Theorem: A formula F is valid if and only if ¬F is unsatifsiable
Equivalence
Formulas with different atomic propositions can be equivalent!
- all tautologies are equivalent to True
- all unsatisfiable formulas are equivalent to False
- but all satisfiable does not equivalent to True
- two formulas can be true with different values on different rows of their truth tables
Semantic Equivalence
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.
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:
Converting to Normal Forms
Theorem: For every formula F, there is an equivalent formula in CNF and in DNF
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
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 whether a 2-CNF formula F is satisfiable
Not all formulas can be converted to 2-CNF
2-CNF is not NP-Complete
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
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
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 in polynomial time you can solve any NP problem in polynomial time!
- 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!