A Little Logic International Center for Computational Logic Technische Universität Dresden Germany Propositional Logic Satisfiability Problems Solving Sudokus First Order Logic Logic Programming A Little Logic 1
Propositions Atomic propositions (atomic formulas or atoms) The sun is shining The vertex with number 17 is green TG925 is flying from Munich to Bangkok At the forth position of the DNA strand is the nucleotide C Abbreviations for Atoms p 1 the sun is shining p 2... Truth values true ( ) and false ( ) Interpretation Mapping from the set of propositions to {, } p 1 p 2 the proposition the sun is shining is true the proposition the vertex with number 17 is green is false A Little Logic 2
Complex Propositions Complex propositions (sentences or formulas) The sun is shining p 1 The sun is not shining p 1 The sun is shining or the sun is not shining (p 1 p 1 ) The sun is shining and the vertex with number 17 is green (p 1 p 2 ) If the sun is shining then the vertex with number 17 is green (p 1 p 2 ) Definition The set of (propositional) formulas is the smallest set satisfying the following conditions All atoms are formulas If F is a formula then so is F If F and G are formulas then so are (F G), (F G), and (F G) Example ( (p 1 p 2 ) (p 1 p 1 )) Notation We sometimes omit p and denote atoms by natural numbers A Little Logic 3
Subformulas Definition Let F be a propositional formula. The set of subformulas of F is the smallest set of formulas satisfying the following conditions F is a subformula of F If G is a subformula of F then so is G If (G 1 G 2 ) is a subformula of F then so are G 1 and G 2, where {,, } Example The set of subformulas of F = ((p 1 p 2 ) p 1 ) is { ((p 1 p 2 ) p 1 ), ((p 1 p 2 ) p 1 ), (p 1 p 2 ), p 1, p 2 } The set of variables occurring in F is {p 1, p 2 } A Little Logic 4
Interpretations and Models Remember interpretations are mappings from the set of propositions into {, } How are complex propositions interpreted? F F F G (F G) (F G) (F G) Example Consider an interpretation I with 1 and 2 then I( ( (1 2) (1 1))) = Definition An interpretation assigning to a formula F is called model for F A Little Logic 5
Satisfiability and other Relations Definition Let F be a formula F is satisfiable if there is an interpretation I with I(F ) = F is valid if for all interpretations I we find I(F ) = F is falsifiable if there is an interpretation I with I(F ) = F is unsatisfiable if for all interpretations I we find I(F ) = A Little Logic 6
Truth Tables How can we compute the value of a formula F under all possible interpretations? Computing a truth table 1 Let m be the number of subformulas of F 2 Let n be the number of variables occurring in F 3 Form a table with 2 n rows and m columns, where the first n columns are marked by the n propositional variables occurring in F, the last column is marked by F, and the remaining columns are marked by the other subformulas of F 4 Fill in the first n columns with und as follows: In the first column fill in alternating downwards..., in the second column..., in the third column..., etc 5 Calculate the values in the remaining columns using the known functions on the set of truth values A Little Logic 7
Working with Truth Tables Let F be a formula and T (F ) a corresponding truth table F is satisfiable iff T (F ) contains a row with in the last column F is valid iff all rows in T (F ) have in the last column F is falsifiable iff T (F ) contains a row with in the final column F is unsatisfiable iff all rows T (F ) have in the final column A Little Logic 8
Logical Consequences Definition A formula F is a logical consequence of a set of propositional formulas G, in symbols G = F, iff for every interpretation I we find: if I(H) = for all H G then I(F ) = Theorem Let F, F 1,..., F n be formulas. {F 1,..., F n} = F holds iff ((F 1... F n) F ) is valid Examples {p, (p q)} {p, ( q p)} {p, p}? = q? = q? = q A Little Logic 9
Satisfiability Problems Remember A formula F is satisfiable if there is an interpretation I with I(F ) = Definition A satisfiability problem consists of a formula F and is the question whether F is satisfiable Example Is F = ( (1 2) (1 1)) satisfiable? interpretations formula 1 2 F Observation If a formula contains n different atoms then there are 2 n different interpretations Remark The complexity theory was developed having the satisfiability problem in mind! A Little Logic 10
Applications Termination of programs Planning and scheduling Biocomputing Verification of hard- and software Cryptoanalysis Verification of signal systems Answer set programming A Little Logic 11
The Graph Coloring Problem Given A finite graph and a finite set of colors Goal An assignment of colors to vertices such that neighboring vertices have different colors Formally At least one color is assigned to each vertex At most one color is assigned to each vertex Neighboring vertices have different colors 1 2 3 4 A Little Logic 12
Naive Encoding of the Graph Coloring Problem (1) Task For a given graph coloring program G find a propositional formula F such that each model for F is a solution for G Example We consider the set {1, 2, 3, 4} of colors and the graph 1 2 3 Naive encoding of an assignment of colors to vertices 4 11 color 1 is assigned to vertex 1 12 color 2 is assigned to vertex 1 13 color 3 is assigned to vertex 1.. 44 color 4 is assigned to vertex 4 A Little Logic 13
Naive Encoding of the Graph Coloring Problem (2) At least one color is assigned to each vertex (11 12 13 14)... (41 42 43 44) At most one color is assigned to each vertex (11 12) (11 13) (11 14)... (43 44) Neighboring vertices have different colors (11 21) (12 22) (13 23)... (34 44) Claim Let F be the conjunction of these formulas Each model for F represents a solution to the graph coloring problem Example The interpretation mapping 11, 22, 33, and 44 to and all other atoms to is a model für F A Little Logic 14
Some Observations The formulas are quite large 10 7 different atoms and 10 8 conjuncts may appear easily in applications Hence, the size of the search space is 2 107! The formulas are difficult to understand We need help by machines This help is provided by SAT solver A Little Logic 15
A Better Encoding of the Graph Coloring Problem (1) Idea We order the colors: 1 < 2 < 3 < 4 Encoding of an assignment of colors to vertices 11 a color greater equal 1 is assigned to vertex 1 12 a color greater equal 2 is assigned to vertex 1.. 44 a color greater equal 4 is assigned to vertex 4 11 12 vertex 1 has color 1 12 13 vertex 1 has color 2 13 14 vertex 1 has color 3 14 vertex 1 has color 4.. 44 vertex 4 has color 4 A Little Logic 16
A Better Encoding of the Graph Coloring Problem (2) At least one color is assigned to each vertex 11 21 31 41 At most one color is assigned to each vertex ( 11 12) ( 12 13) ( 13 14)... ( 43 44) Neighboring vertices have different colors (11 12 21 22)... (34 44) Claim Let F be the conjunction of these formulas Each model for F represents a solution to the graph coloring problem Observation In experiments the runtimes are reduced by 10% on average A Little Logic 17
Conjunctive Normal Form (1) Usually, SAT solvers accept formulas only in a particular form Definition A literal is an atom or the negation of an atom Examples 11, 12, 13, 14 Definition A formula is in conjunctive normal form (CNF) if it is of the form where each L ij is a literal (L 11... L 1n1 )... (L m1... L mnm ) Definition Two formulas F and G are equivalent, in symbols F G, if I(F ) = I(G) holds for all interpretations I Examples F F (F G) ( F G) (F G) ( F G) (F G) ( F G) A Little Logic 18
Conjunctive Normal Form (2) Theorem For each formula there is an equivalent formula in CNF Example ( 11 12) ( 11 13) ( 11 14)... ( 43 44) is an equivalent CNF of (11 12) (11 13) (11 14)... (43 44) Observation There are algorithms transforming a given formula into CNF A Little Logic 19
SAT Solvers There is a variety of freely available SAT solvers MiniSAT glucose riss They solve problems in CNF with up to 10 7 atoms and 10 8 conjuncts They are applied in industry There are yearly competitions for the best SAT solvers They are continuously improved The improvements concern the whole area of computer science from theory and applications to soft- and hardware A Little Logic 20
Solving the Graph Coloring Problem with Naive Encoding Input to a SAT solver p cnf nv nc 11 12 13 14 0. 41 42 43 44 0-11 -12 0. -43-44 0-11 -21 0. -34-44 0 where nv and nc are the numbers of atoms (variables) and conjuncts, resp. Possible output of a SAT solver 11 22 33 44 A Little Logic 21
Sudoku Puzzles Let n N; a Sudoku puzzle consists of an n 2 n 2 grid made up of n n subgrids called blocks with some integers from [1, n 2 ] placed in some cells where some of these placements are predefined The problem is to assign i [1, n 2 ] to each cell of the grid such that each row, column, and block contains exactly one occurrence of each integer in [1, n 2 ] There are more than 6 10 12 3-Sudoku puzzles Sudoku puzzles with n > 3 appear to be difficult to solve for humans A Little Logic 22
A Simple 3-Sudoku +-------+-------+-------+ - - 4 2 3 9 - - - - 8-5 - 6 - - - 9 - - 8-4 - 6 - +-------+-------+-------+ 5 7 1 - - - 9 4 6 8 - - - - - - - 3 2 3 9 - - - 7 8 1 +-------+-------+-------+ - - - 4-8 - - 7 - - 3 9-7 - 1 - - - - 1 2 3 4 - - +-------+-------+-------+ A Little Logic 23
Sudoku Exercise For a given 3-Sudoku S, specify a first order logic formula F such that each model for F is a solution for S. In particular, specify formulas for the following requirements Definedness Each cell contains one element of [1, 9] Uniqueness for Cells Uniqueness for Rows Uniqueness for Columns Uniqueness for Blocks Each cell has at most one value All numbers in [1, 9] must occur in every row All numbers in [1, 9] must occur in every column All numbers in [1, 9] must occur in every block Specify a propositional formula G corresponding to F Turn G into CNF Download a SAT solver from the internet Solve the given 3-Sudoku with the help of the SAT solver Extract the solution A Little Logic 24
First Order Logic Sentences like for each formula there is an equivalent formula in CNF cannot be expressed in the language of propositional logic The set of formulas is infinite Sentences like at most one color is assigned to each vertex can be expressed in the language of propositional logic if the set of colors as well as the graph is finite But the propositional representation is usually difficult to understand We need a more expressive language First order logic A Little Logic 25
Syntax of First Order Logic Definition An alphabet of (first order) logic consists of a finite or countably infinite set of relation symbols a finite or countably infinite set of function symbols a countably infinite set of variables the set {,,, } of connectives the set {, } of quantifiers the special characters (, ), and, A Little Logic 26
Syntax (Continued) is called universal quantifier is called existential quantifier An arity n N is assigned to each function and relation symbol Nullary function symbols are called constant symbols Notation p, q,... relation symbols g, h,... function symbols a, b,... constant symbols X, Y,... variables Agreement In the following we assume that the sets of relation symbols, function symbols, and variables are given A Little Logic 27
Terms Definition The set of terms is the smallest set satisfying the following conditions 1 Every variable is a term 2 If g is an n-ary function symbol and t 1,..., t n are terms then g(t 1,..., t n) is a term as well A term is closed or ground (instantiated) if it does not contain any variables Notation We write g instead of g() A Little Logic 28
First Order Logic Formulas Definition The set of atomic formulas (or atoms) is the set of strings of the form p(t 1,..., t n), where p is an n-ary relation symbol and t 1,..., t n are terms Notation We write p instead of p() Definition The set of (first order logic) formulas is the smallest set satisfying the following conditions Every atom is a formula If F is a formula then so is F If F and G are formulas then so are (F G), (F G), and (F G) If F is a formula, Q a quantifier, and X a variable then (QX) F is a formula as well Notation F, G, H (possibly indexed) denote formulas A Little Logic 29
Free and Bound Occurrences of Variables Definition The free occurrences of a variable in a formula are defined as follows The free occurrences of a variable in an atomic formula F are the occurrences of a variable in F The free occurrences of a variable in a formula F are the free occurrences of a variable in F The free occurrences of a variable in a formula (F 1 F 2 ) are the free occurrences of a variable in F 1 together with the free occurrences of a variable in F 2, where {,, } The free occurrences of a variable in a formula (QX) F are the free occurrences of a variable in F without the occurrences of X The occurrence of a variable is called bound if it is not free A Little Logic 30
Closed Terms and Formulas Remember A term is closed if it does not contain any occurrence of a variable Definition A closed formula (or sentence) is a formula where every occurrence of a variable is bound A Little Logic 31
Substitutions Definition A substitution is a mapping from the set of variables into the set of terms, where only a finite number of variables is not mapped to itself Let σ be a substitution σ can be represented by a finite set of pairs {X σ(x) X σ(x)} and vice versa If all variables are mapped to themselves, then σ is called empty substitution σ X = {Y t σ Y X} Instead of σ(x) we will write Xσ A Little Logic 32
Instances Definition A substitution σ is extended to a mapping σ from the set of terms and into the set of terms as follows { tσ if t is a variable t σ = f (t 1 σ,..., t n σ) if t is of the form f (t 1,..., t n) t σ is called instance of t under σ If t σ is closed, then t σ is called ground instance of t under σ, and σ is said to be a ground substitution for t Notation We usually omit ˆ and simply write σ A Little Logic 33
Applications of Substitutions to Formulas Definition Let σ be a substitution The application of a substitution to a formula is defined as follows p(t 1,..., t n)σ = p(t 1 σ,..., t nσ) for every atom of the form p(t 1,..., t n) ( F )σ = (F σ) (F 1 F 2 )σ = (F 1 σ F 2 σ), where {,, } ((QX) F )σ = (QX) (F σ X ), where Q {, } If F is a formula and F σ is closed, then F σ is called ground instance of F under σ, and σ is said to be a ground substitution for F A Little Logic 34
Semantics Relations and Functions Let D be a set Relations D 2 = D D = {(d 1, d 2 ) d 1 D and d 2 D} D n = D n 1 D = {(t, d n) t D n 1 and d n D} =: {(d 1,..., d n) d i D, 1 i n}, n > 2 D 1 = D =: {(d) d D} D 0 = = {( )} R D 2 R = {(n, m) n, m N and n < m} R D 3 R = {(x, y, z) x, y, z N and x 2 + y 2 = z 2 } R D 1 R = {(n) n N and n even} R D 0 R = or R = {( )} Notation Often d is written instead of (d) A Little Logic 35
Functions Remember Let D be a set D 2 = D D = {(d 1, d 2 ) d 1 D and d 2 D} D n = D n 1 D = {(t, d n) t D n 1 and d n D} =: {(d 1,..., d n) d i D, 1 i n}, n > 2 D 1 = D =: {(d) d D} D 0 = = {( )} Functions + : N 2 N +(2, 3) 5 s : N 1 N s(3) 4 0 : N 0 N 0( ) 0 A Little Logic 36
Interpretations Definition A predicate logic interpretation I consists of a non-empty set D and a mapping I satisfying the following conditions Every n-ary function symbol g is mapped to an n-ary function g I : D n D Every n-ary relation symbol p is mapped to an n-ary relation p I D n D is called domain of the interpretation A Little Logic 37
Variable Assignments What is the meaning of a variable? Definition A variable assignment wrt an interpretation I = (D, I ) is a mapping Z from the set of variables and into the domain D The image of a variable X under Z is denoted by X Z Let Z be a variable assignment and d D. {X d}z denotes the variable assignment, where { Y {X d}z d if Y = X = Y Z otherwise A Little Logic 38
Interpretation of Terms Definition Let I = (D, I ) be an interpretation and Z a variable assignment wrt I The meaning t I,Z of a term t is defined as follows For every variable X we have X I,Z = X Z For every term of the form g(t 1,..., t n) we have [g(t 1,..., t n)] I,Z = g I (t I,Z 1,..., t I,Z n ) where g is an n-ary function symbol and t 1,..., t n are terms A Little Logic 39
Example Let the set of function symbols consist of a constant symbol a, a unary function symbol h, and a binary function symbol g D 1 = N with successor function s and addition + D 2 = Z with predecessor function p and subtraction D 3 is set of words over Σ = {o, m} with functions add m which appends m to a word at the right conc which concatenates two words a h g X Z g(h(a), h(h(a))) g(h(x), g(h(a), h(x))) I 1 0 s + 4 3 11 I 2 1 p 8 1 14 I 3 o add m conc omo omomm omomomomom A Little Logic 40
Interpretation of Formulas Definition Let I = (D, I ) be an interpretation and Z a variable assignment wrt I. I and Z assign to every formula F a truth value F I,Z as follows [p(t 1,..., t n)] I,Z = [ F ] I,Z = (F I,Z ) iff (t I,Z 1,..., tn I,Z ) p I [(F 1 F 2 )] I,Z = (F I,Z 1 F I,Z 2 ), where {,, } [( X) F ] I,Z = iff for all d D: F I,{X d}z = [( X) F ] I,Z = iff for some d D: F I,{X d}z = Observation If a formula F is closed, then its meaning under an interpretation I is independent of a variable assignment Z and we write F I instead of F I,Z A Little Logic 41
Models for Closed Formulas Definition Let I = (D, I ) be an interpretation and F be a closed formula I is called model for F, in symbols I = F, if F I = holds Many notions and results known from propositional logic can be extended to predicate logic. For example: Validity, satisfiability, falsifiability, unsatisfiability Logical consequence A Little Logic 42