TDDC36 (L OGIC ): E XAM Solutions to Exercises E XERCISE 1 1. Prove the following propositional formula: (¬P ∨ ¬Q) ∧ (P ∨ ¬Q) ∧ R → ¬Q ∧ R (a) (2 points) using tableaux (b) (2 points) using Gentzen system (as provided in the book or during lectures - up to your choice). 2. Prove the following formula of predicate logic, where a is a constant: ∀x P (x, x, x) ∧ ¬∃x∃y∃z P (x, y, z) ∧ ¬P (z, x, a) → ∃z P (a, z, a) (a) (3 points) using tableaux (b) (3 points) using resolution. S OLUTION TO 1( A ) We first negate the formula: ¬{ (¬P ∨ ¬Q) ∧ (P ∨ ¬Q) ∧ R → ¬Q ∧ R } ≡ (¬P ∨ ¬Q) ∧ (P ∨ ¬Q) ∧ R ∧ ¬ ¬Q ∧ R ≡ (¬P ∨ ¬Q) ∧ (P ∨ ¬Q) ∧ R ∧ (Q ∨ ¬R). Next, we construct a tableau for the negated formula: (¬P ∨ ¬Q) ∧ (P ∨ ¬Q) ∧ R ∧ (Q ∨ ¬R) ↓ (α-rule) (¬P ∨ ¬Q), (P ∨ ¬Q) ∧ R ∧ (Q ∨ ¬R) ↓ (α-rule) (¬P ∨ ¬Q), (P ∨ ¬Q), R ∧ (Q ∨ ¬R) ↓ (α-rule) (¬P ∨ ¬Q), (P ∨ ¬Q), R, (Q ∨ ¬R) . (β-rule) & (¬P ∨ ¬Q), (P ∨ ¬Q), R, Q (¬P ∨ ¬Q), (P ∨ ¬Q), R, ¬R | {z } . (β-rule) & closed ¬P, (P ∨ ¬Q), R, Q ¬Q, (P ∨ ¬Q), R, Q | {z } . (β-rule) & closed ¬P, P, R, Q ¬P, ¬Q, R, Q | {z } | {z } closed closed 1 2(9) TDDC36 (Logic) Exam: Solutions to Exercises S OLUTION TO 1( B ) A possible proof: ∅ ⇒ (¬P ∨ ¬Q) ∧ (P ∨ ¬Q) ∧ R → ¬Q ∧ R (→ r) (¬P ∨ ¬Q) ∧ (P ∨ ¬Q) ∧ R ⇒ ¬Q ∧ R (∧l) (¬P ∨ ¬Q), (P ∨ ¬Q) ∧ R ⇒ ¬Q ∧ R (∧l) (¬P ∨ ¬Q), (P ∨ ¬Q), R ⇒ ¬Q ∧ R (∧r) (¬P ∨ ¬Q), (P ∨ ¬Q), R ⇒ ¬Q (¬P ∨ ¬Q), (P ∨ ¬Q), R ⇒ R (∨l) | {z } ¬P, (P ∨ ¬Q), R ⇒ ¬Q ¬Q, (P ∨ ¬Q), R ⇒ ¬Q axiom (∨l) | {z } ¬P, P, R ⇒ ¬Q ¬P, ¬Q, R ⇒ ¬Q axiom | {z } (¬l) P, R ⇒ P, ¬Q axiom | {z } axiom S OLUTION TO 2( A ) We first negate the formula: ¬{∀x P (x, x,x) ∧ ¬∃x∃y∃z P (x, y, z) ∧ ¬P (z, x,a) → ∃z P (a, z, a) } ≡ ∀xP (x, x, x) ∧ ¬∃x∃y∃z P (x, y, z) ∧ ¬P (z, x, a) ∧ ¬∃z P (a, z, a) ≡ ∀x P (x, x, x) ∧ ∀x∀y∀z ¬P (x, y, z) ∨ P (z, x, a) ∧ ∀z ¬P (a, z, a) . Next, we construct a tableau for the negated formula: ∀x P (x, x, x) ∧ ∀x∀y∀z ¬P (x, y, z) ∨ P (z, x, a) ∧ ∀z ¬P (a, z, a) ↓ (α-rule) ∀x P (x, x, x) , ∀x∀y∀z ¬P (x, y, z) ∨ P (z, x, a) ∧ ∀z ¬P (a, z, a) ↓ (α-rule) ∀x P (x, x, x) , ∀x∀y∀z ¬P (x, y, z) ∨ P (z, x, a) , ∀z ¬P (a, z, a) ↓ (γ-rule) P (a, a, a), ∀x∀y∀z ¬P (x, y, z) ∨ P (z, x, a) , ∀z ¬P (a, z, a) ↓ (γ-rule) P (a, a, a), ∀x∀y∀z ¬P (x, y, z) ∨ P (z, x, a) , ¬P (a, a, a) | {z } closed I DA U NIVERSITY OF L INK ÖPING 3(9) TDDC36 (Logic) Exam: Solutions to Exercises S OLUTION TO 2(B) We first negate the formula and obtain (see the solution for 2(a)): ∀x P (x, x, x) ∧ ∀x∀y∀z ¬P (x, y, z) ∨ P (z, x, a) ∧ ∀z ¬P (a, z, a) so we have clauses: (1) P (x, x, x) (2) ¬P (x, y, z) ∨ P (z, x, a) (3) ¬P (a, z, a) A possible proof by resolution: (5) P (x, x, a) – (res) applied to (1), (2) with unification x = y = z (6) FALSE – (res) applied to (3), (5) with unification x = z = a. I DA U NIVERSITY OF L INK ÖPING 4(9) TDDC36 (Logic) Exam: Solutions to Exercises E XERCISE 2 1. (4 points) Translate the following sentences into a set of propositional formulas: “chose one of three roads: short, medium or long” “the short road is always crowded” “the medium road is not comfortable, but fast” “the long road is comfortable” “the chosen road should be comfortable.” 2. (2 points) Assuming that fast roads are not crowded and crowded roads are not comfortable, hypothesize what roads can be chosen explain your reasoning informally. 3. (4 points) Prove your claim formally using a proof system of your choice (tableaux, Gentzen system or resolution. Please do not use truth table method, as this will give no points). S OLUTION TO 2.1 A possible translation: short ∨ medium ∨ long short → crowded medium → (¬comf ortable ∧ f ast) long → comf ortable comf ortable. (1) S OLUTION TO 2.2 The chosen road should be comfortable. Therefore it cannot be medium since medium roads are not comfortable. The chosen road cannot be short since short roads are crowded and crowded roads are not comfortable. Therefore the only road that can be chosen if the long one. I DA U NIVERSITY OF L INK ÖPING 5(9) TDDC36 (Logic) Exam: Solutions to Exercises S OLUTION TO 2.3 To prove our hypothesis, we also need the translation of assumptions provided in point 2 of Exercise: f ast → ¬crowded crowded → ¬comf ortable (2) We use resolution. We first transform (1) and (2) into a set of clauses: (1) short ∨ medium ∨ long (2) ¬short ∨ crowded (3) ¬medium ∨ ¬comf ortable (4) ¬medium ∨ f ast (5) ¬long ∨ comf ortable (6) comf ortable (7) ¬f ast ∨ ¬crowded (8) ¬crowded ∨ ¬comf ortable. Now we negate formula [(1) ∧ (2) ∧ (3) ∧ (4) ∧ (5) ∧ (6) ∧ (7) ∧ (8)] → long and obtain: [(1) ∧ (2) ∧ (3) ∧ (4) ∧ (5) ∧ (6) ∧ (7) ∧ (8)] ∧ ¬long. Therefore, we consider clauses (1) – (8) together with (9) ¬long. A possible proof by resolution: (10) short ∨ medium (11) crowded ∨ medium (12) ¬medium (13) crowded (14) ¬comf ortable (15) FALSE I DA – (res) applied to (1), (9) – (res) applied to (2), (10) – (res) applied to (3), (6) – (res) applied to (11), (12) – (res) applied to (8), (13) – (res) applied to (6), (14). U NIVERSITY OF L INK ÖPING 6(9) TDDC36 (Logic) Exam: Solutions to Exercises E XERCISE 3 Consider a relation R and properties: (a) ∀x∀y∀z[(R(x, y) ∧ R(x, z)) → R(y, z)] (b) ∀x∀y R(x, y) → ∃u[R(u, x) ∧ R(u, y)] (c) ∀x∀y[R(x, y) → R(y, x)]. 1. (4 points) Check informally whether the conjunction of (a) and (b) implies (c). 2. (6 points) Verify your informal reasoning using resolution. S OLUTION TO 3(1) To check whether (c) holds assume that for arbitrarily chosen elements s and t we have that R(s, t) holds. To prove (c), we have to show that given assumptions (a) and (b), R(t, s) holds, too. 1. First, using (b), we observe that R(s, t) implies that there exists u such that R(u, s) and R(u, t) hold. 2. Second, using (a) we know that for all x, y and z, we have that R(x, y) and R(x, z) imply R(y, z). Now, for y = t, z = s and x = u we have that R(u, t) and R(u, s) imply R(t, s). 3. From the first point we have that R(u, s) and R(u, t) hold. From the second point we have that these imply R(t, s). Therefore we conclude that R(t, s). That way we have shown that indeed the conjunction of (a) and (b) implies (c). I DA U NIVERSITY OF L INK ÖPING 7(9) TDDC36 (Logic) Exam: Solutions to Exercises S OLUTION TO 3(2) To apply resolution we negate the implication ((a)∧ (b))→ (c): ¬{∀x∀y∀z[(R(x, y) ∧ R(x, z)) → R(y, z)]∧ ∀x∀y R(x, y) → ∃u[R(u, x) ∧ R(u, y)] → ∀x∀y[R(x, y) → R(y, x)]} ≡ ∀x∀y∀z[(R(x, y) ∧ R(x, z)) → R(y, z)]∧ ∀x∀y R(x, y) → ∃u[R(u, x) ∧ R(u, y)] ∧ ¬∀x∀y[R(x, y) → R(y, x)] ≡ ∀x∀y∀z[(R(x, y) ∧ R(x, z)) → R(y, z)]∧ ∀x∀y R(x, y) → ∃u[R(u, x) ∧ R(u, y)] ∧ ∃x∃y[R(x, y) ∧ ¬R(y, x)]. We deal with the following conjuncts: ∀x∀y∀z[(R(x, y) ∧ R(x, z)) → R(y, z)] ∀x∀y R(x, y) → ∃u[R(u, x) ∧ R(u, y)] (3) ∃x∃y[R(x, y) ∧ ¬R(y, x)]. (5) (4) We first Skolemize (4) and (5): ∀x∀y R(x, y) → R(f (x, y), x) ∧ R(f (x, y), y) (6) R(s, t) ∧ ¬R(t, s), (7) where s, t are constants. Formulas (3), (6) and (7) are transformed into the following clauses: (1) ¬R(x, y) ∨ ¬R(x, z) ∨ R(y, z) (2) ¬R(x, y) ∨ R(f (x, y), x) (3) ¬R(x, y) ∨ R(f (x, y), y) (4) R(s, t) (5) ¬R(t, s). A possible proof by resolution: (6) R(f (s, t), s) (7) R(f (s, t), t) (8) ¬R(f (s, t), z) ∨ R(t, z) (9) R(t, s) (10) FALSE – (res) applied to (2), (4) with x = s, y = t – (res) applied to (3), (4) with x = s, y = t – (res) applied to (1), (7) with x = f (s, t), y = t – (res) applied to (6), (8) with z = s – (res) applied to (5), (9) which completes the proof. I DA U NIVERSITY OF L INK ÖPING 8(9) TDDC36 (Logic) Exam: Solutions to Exercises E XERCISE 4 1. (2 points) Design a Datalog database for storing information about scientific papers, containing, among others, title, authors and information about direct references between papers (i.e., for each paper, information about papers listed in its bibliography section). By p q we denote that paper q is among references listed in paper p. We define that paper q is indirectly referenced by paper p (and denote this by p q) if there is k ≥ 1 and papers p1 , p2 , . . . , pk such that p p1 p2 ... pk−1 pk q. 2. (1 point) Provide an exemplary integrity constraint concerning . 3. (1 point) Provide an exemplary integrity constraint concerning . 4. Formulate in logic queries selecting: (a) (2 points) all pairs P 1, P 2 of papers both (co-)authored by ‘John Smith’ such that P 1 P2 (b) (4 points) all pairs P 1, P 2 of papers both (co-)authored by ‘John Smith’ such that P 1 P 2. S OLUTION TO 4.1 The database contain relations: • paper(id, title), where id is a (unique) identifier for a paper, title ∈ String is its title • author(id, name), where id is a paper identifier and name ∈ String is a name of its (co-)author • ref ers to(id1, id2), where id1, id2 are paper identifiers. Example: paper(11,0 Logic in Databases0 ) – paper entitled Logic in Databases has id = 11 paper(22,0 F ixpoint Logics0 ) – paper entitled F ixpoint Logics has id = 22 author(11,0 John Smith0 ) – John Smith is a (co-)author of paper 11 author(11,0 M ary Jones0 ) – M ary Jones is a (co-)author of paper 11 ref ers to(11, 22) – paper 11 refers to paper 22. I DA U NIVERSITY OF L INK ÖPING 9(9) TDDC36 (Logic) Exam: Solutions to Exercises S OLUTION TO 4.2 The relation of road intersection is irreflexive: ∀x[¬ref ers to(x, x)]. S OLUTION TO 4.3 Relation is transitive: ∀x∀y∀z[ x y ∧ y z → x z]. S OLUTION TO 4.4( A ) Select all pairs P 1, P 2 of papers both (co-)authored by ‘John Smith’ such that P 1 P 2 (recall that is an anonymous “don’t care” variable): answer(Id1, Id2) : − paper(Id1, ), author(Id1,0 John Smith0 ), paper(Id2, ), author(Id2,0 John Smith0 ), ref ers to(Id1, Id2). Now answer(X, Y ) provides all required pairs of papers. S OLUTION TO 4.4( B ) Select all pairs P 1, P 2 of papers both (co-)authored by ‘John Smith’ such that P 1 P 2. We need to define the relation P 1 P 2 (we give it the name indirectly ref ): indirectly ref (Id1, Id2) : −ref ers to(Id1, Id), ref ers to(Id, Id 2). indirectly ref (Id1, Id2) : −ref ers to(Id1, Id), indirectly ref (Id, Id2). Now we can formulate the required query: answer(Id1, Id2) : − paper(Id1, ), author(Id1,0 John Smith0 ), paper(Id2, ), author(Id2,0 John Smith0 ), indirectly ref (Id1, Id2). I DA U NIVERSITY OF L INK ÖPING