Artificial Intelligence Logic and Reasoning Dr. Edjard Mota Department of Computer Science edjard.de-souza-mota@city.ac.uk Class 1 Copyright 2016 Edjard Mota Agenda • Why Logic? • Logic-based Languages • Propositional Logic: Syntax and Semantics • Models and Validity • Rules of Inference Class 1 Copyright 2016 Edjard Mota Why Logic? • How can we represent ordinary word-language meaning so that a computer can process these representations? • How can we associate meaning representations with an unlimited set of sentences? • How can we use programs that connect the meaning representations of sentences to stores of knowledge? Class 1 Copyright 2016 Edjard Mota Why Logic? Formal Ambiguous Expressive Inference Natural languages ✖️ ✔️✔️✔️ ✔️✔️✔️ ✔️✔️✔️ Programming Languages ✔️✔️ ✖️ ✖️ ➕/➖ Logic-based Language ✔️✔️✔️ ✖️ ✔️ ✔️ Formal language for expressing knowledge and ways of reasoning, i.e. calculus for reasoning. Class 1 Copyright 2016 Edjard Mota Logic-based Languages Definition 1. Logic is defined by: • A set of sentences: - a sentence is built from a set of primitives according to syntax rules, i.e. the grammar. • A set of interpretations: - an interpretation gives a meaning to sentences. • The valuation (meaning) function (V) is a mapping • Inference rules to derive new sentences from existing ones Class 1 Copyright 2016 Edjard Mota Logic-based Languages A, B and C below are symbols used to denote propositions that are either true or false Examples of atomic propositions/sentences a : Alex goes to the party b : Beki goes to the party c : Chris goes to the party Examples of compound propositions If then If Alex goes to the party, then Beki also goes a b a Logical connectives b Other logical connectives: not, or, and if and only if Class 1 Copyright 2016 Edjard Mota Logic-based Languages From and other compound If Chris goes to the party, then Alex also goes It should be possible to conclude: But, Beki does not go: ¬ b It should be possible to conclude: ¬ a and ¬ c What if Chris goes is true? Inconsistency found c and ¬ c Class 1 Copyright 2016 Edjard Mota Propositional Logic - Syntax Notation: elements of the logic written with this font Definition 2. Syntax of the propositional language • Alphabet: set of atoms {p, q, r, s, …} for propositions, T (True), F(False), logical connectives ¬, ∧, ∨, →, ↔︎, variables of formulas are in capital letter. • Well-formed formula (wff) (sentence) in propositional logic 1. Any atom is a formula 2. T and F are formulas 3. If A is a formula, then ¬ A is a formula 4. If A and B are formula, then (A ∧ B), (A ∨ B), (A → B) and (A ↔︎ B) are formulas Class 1 Copyright 2016 Edjard Mota Propositional Logic - Syntax According to the definition, which of the following are wff? 1. 2. 3. 4. 5. 6. Class 1 ∨pq (¬ (p → (q ∧ p))) (¬ (p → (q = p)) (¬ (◇(q ∨ p)) (p ∧ ¬q) ∨ (q → r) r ¬q Copyright 2016 Edjard Mota Propositional Logic Semantics Truth-functionality of logical connectives. Definition 3. The meaning of a compound sentence is a function of the truth-value of its sub-sentences. 1. Negation: ¬A is false when A is true, and ¬A is true when A is false 2. Conjunction A and B: (A ∧ B) is true only when both are true, otherwise it is false. 3. Disjunction A and B: (A ∨ B) is false only when both are false, otherwise it is true (inclusive disjunction). 4. Logical implication of A and B: (A → B) is false if A is true and B is false, other otherwise it is true. 5. “If and only if”: A ↔︎ B, is true when A and B have the same truth-value Class 1 Copyright 2016 Edjard Mota Propositional Logic Semantics Truth-tables for schema of sentences Class 1 A B ¬A A∧B A∨B A→B A ↔︎ B T T F T T T T T F F F T F T F T T F T T T F F T F F T T Copyright 2016 Edjard Mota Propositional Logic Semantics Definition 4. An Interpretation is assignment of truthvalues (T or F) to the atoms of a sentence. Example: consider the sentence A: (p → q) ∨ ¬ s - The assignment I1 = {T, F,F} to {p, q, s} is an interpretation that makes the sentence A true. - We say that sentence A is satisfiable under I1 or that A is satisfied in I1 Definition 5. An interpretation that satisfies a sentence is called a Model for such a sentence Class 1 Copyright 2016 Edjard Mota Propositional Logic Semantics Build the truth-table for B: ((p → q) ∧ p) → q p q p→q (p → q)∧ p ((p → q)∧ p) → q T T F T T T F T F T F T F F T F F T F T Sentence B is true under all interpretations! 🤔 Class 1 Copyright 2016 Edjard Mota Propositional Logic Semantics Build the truth-table for C: ((p → q) ∧ (p ∧ ¬q) p q ¬q p→q (p ∧ ¬q) ((p → q)∧ (p ∧ ¬q) T T F T F F T F T F T F F T F T F F F F T T F F Sentence C is false under all interpretations! 🤔 Class 1 Copyright 2016 Edjard Mota Propositional Logic Semantics Complete the truth-table for A : (P → Q) ∨ ¬ S p→q ¬s (p → q) ∨¬s F F T T F T F F F T T F T T T p q s F F F F F T F T F F T T T F T Sentence A can be either true or false … 🤔 Class 1 Copyright 2016 Edjard Mota Propositional Logic Inference When a given argument is valid? Definition 6. An argument of the form “A1, A2, …, An Thus B” is said to be valid if for any interpretation in which A1, A2, …, An are all satisfied, B is also satisfied. We write • We say that A1, A2, …, An entails B • A1, A2, …, An is usually called a Knowledge Base (KB) • ⊨ is called a logical consequence relation Class 1 Copyright 2016 Edjard Mota Propositional Logic Inference Examples of valid arguments A⊨A A∧B⊨A A, A → B ⊨ B A → B, ¬ B ⊨ ¬ A Example of an invalid argument (⊭) A⊨A∧B Validity of an argument can be verified via truth-table! Class 1 Copyright 2016 Edjard Mota Propositional Logic Inference Example of validity check via truth-table a → b, ¬ b ⊨ ¬ a ?? a b a→b ¬b ((a → b)∧ ¬ b) ¬a F F T T T T F T T F F T T F F F F F T T T F F F Checking validity is computationally very expensive: a sentence with n atoms will a truth-table with 2n lines! 💥 Class 1 Copyright 2016 Edjard Mota Propositional Logic Inference Definition 7. Validity and Consistency of a formula. • A sentence A is valid or a tautology (written ⊨ A) if and only if it is true under all possible interpretations. e.g. A ∨ ¬A • A sentence A is inconsistent (or unsatisfiable) if and only if it is false under all possible interpretations. e.g. A ∧ ¬A • A sentence is consistent (satisfiable) if and only if it is not inconsistent. Class 1 Copyright 2016 Edjard Mota Propositional Logic Inference Interesting observations … 😎 1. A formula is valid iff its negation is inconsistent. 2. A formula is inconsistent iff its negation is valid. 3. A formula is invalid iff there is at least one interpretation under which the formula is false. 4. A formula is consistent (satisfiable) iff there is at least one interpretation under which the formula is true. 5. If a formula is valid, then it is consistent, but not vice versa. 6. If a formula is inconsistent, then it is invalid, but not vice versa. Class 1 Copyright 2016 Edjard Mota Propositional Logic: what for? Example: Modeling digital circuit of modern computers • Does it meet its specifications? • Does it function correctly for a given instance of signals? (p ∧ ¬ q) ∨ (¬ p ∧ q) ⟷ o (r ∧ o) ⟷ a (p ∧ q) ⟷ b (o ∧ ¬ r) ∨ (¬ o ∧ r) ⟷ s (a ∨ b) ⟷ c Class 1 • 28 = 256 lines in the truth-table • Can we reduce the number of gates or components and keep the same functionality? Copyright 2016 Edjard Mota Logical Equivalence as Reasoning Definition 8. Two formulas A and B are logically equivalent (A ≡ B) if and only if they have the same models. If A and B are logically equivalent, then A → B and B → A (E1) a → b ≡ ¬b → ¬a (E2) a → b ≡ ¬a ∨ b (E3) (a ∧ b) ∧ c ≡ a ∧ (b ∧ c) (E4) a ∧ (b ∨ c) ≡ (a ∧ b) ∨ (b ∧ c) (E5) ¬(a ∨ b) ≡ ¬a ∧ ¬b (E6) ¬(a ∧ b) ≡ ¬a ∨ ¬b (E7) a∨b ≡ b∨a (E8) a∧b ≡ b∧a (E9) ¬a ∧ a ≡ F (E10) ¬a ∨ a ≡ T Class 1 Contrapositive Conditional Associativity Distributivity DeMorgan Laws Commutativity Contradiction Tautology Copyright 2016 Edjard Mota Logical Equivalence as Reasoning “Suppose the stocks go down if the prime interest rate goes up. Suppose also that most people are unhappy when stock prices go down. Assume that the prime interest rate goes up. Show that you can conclude that most people are unhappy”. (1) If the prime interest goes up, stock prices go down. (2) If stock prices go down, most people are unhappy. (3) The prime interest goes up. (4) Most people are unhappy. [given] [given] [given] goal Considering the following atoms (1) p → s. p - Prime interest rate goes up. (2) s → u. (3) p. s - Stock prices go down. (4) u?? u - Most people are unhappy. We have to prove that (4) is true whenever (1)∧(2)∧(3) is true. Class 1 Copyright 2016 Edjard Mota ≡ (p ∧ (¬p ∨ s) ∧ (¬s ∨ u)) (E8) ≡ ((p ∧ ¬p)as ∨(p ∧Reasoning s)) ∧ (¬s ∨ u)) (E4) Logical Equivalence ≡ (F ∨(p ∧ s) ∧ (¬s ∨ u)) (E9) ≡ ((p ∧ s) ∧ (¬s ∨ u)) ≡ ((p ∧ s ∧ ¬s) ∨ (p ∧ s ∧ u)) (E4) ≡ ((p ∧ F) ∨ (p ∧ s ∧ u)) (E9) ≡ (F ∨ (p ∧ s ∧ u)) ≡ (p ∧ s ∧ u) Thus, if ((p → s) ∧ (s → u)) ∧ p) is true, then (p ∧ s ∧ u) is true. But (p ∧ s ∧ u) is true only if p, s and u are all true. Therefore, u is true. We say u is a logical consequence of (p → s), (s → u) and p Class 1 Copyright 2016 Edjard Mota Logical Consequence Theorem of Entailment. Given formulas 𝓕={F1, F2, …, Fn} and a formula G. We say that G is a logical consequence of 𝓕(𝓕 ⊨ G) iff the formula (F1 ∧ F2 ∧ … ∧ Fn → G) is valid (i.e. a tautology), we say that 𝓕 entails G (𝓕 ⊢ G) Definition 7 says ¬ (F1 ∧ F2 ∧ … ∧ Fn → G) is inconsistent ¬(F1 ∧ F2 ∧ … ∧ Fn → G) ≡ ¬(¬(F1 ∧ F2 ∧ … ∧ Fn) ∨ G) (E2) ≡ ¬(¬(F1 ∧ F2 ∧ … ∧ Fn) ∧ ¬G) (E5) ≡ (F1 ∧ F2 ∧ … ∧ Fn) ∧ ¬G ≡ F1 ∧ F2 ∧ … ∧ Fn ∧ ¬G (E3) In other words 𝓕 ∪ {¬G} is unsatisfiable! 𝓕 ∪ {¬G} ⊢ F Class 1 Copyright 2016 Edjard Mota Example of Reasoning (1) If the prime interest goes up (p), stock prices go down (s). (2) If stock prices go down, most people are unhappy (u). (3) The prime interest goes up. (4) Most people are unhappy. [given] [given] [given] goal Is ((p → s) ∧ (s → u) ∧ p) → u a valid argument? (p → s) ∧ (s → u) ∧ p ∧ ¬u ≡ (p → s) ∧ p ∧ ((¬s ∨ u) ∧ ¬u) Class 1 ≡ ≡ ≡ ≡ ≡ ≡ ≡ ≡ (p → s) ∧ p ∧ ((¬s ∧ ¬u) ∨ (u ∧ ¬u) (p → s) ∧ p ∧ ((¬s ∧ ¬u) ∨ F ((¬p ∨ s) ∧ p) ∧ (¬s ∧ ¬u) ((¬p ∧ p)∨ (s ∧ p)) ∧ (¬s ∧ ¬u) (F ∨ (s ∧ p)) ∧ (¬s ∧ ¬u) (s ∧ p ∧ ¬s ∧ ¬u) (s ∧ ¬s ∧ p ∧ ¬u) F Copyright 2016 Edjard Mota Propositional Logical Reasoning Definition 9. Modus Ponens: given an implication rule and fact we can infer the conclusion. Forward Chaining Reasoning. (computationally linear) 1.given a query or goal G. 2.until the goal is found: fires any rules whose premises are satisfied, adding their conclusion to the set of sentences Example of deducing u from {(p → s), (s → u), p} True fact p s Class 1 May draw conclusions not Rule fired Conclusion Goal? “relevant” to the goal, e.g. if p then prices goes up no p→s s if p then banks profit is higher … yes s→u u Copyright 2016 Edjard Mota Propositional Logical Reasoning Definition 10. Resolution: from a disjunction, if one of the disjuncts is false, then you can infer the rest is true. It “looks like” Modus Ponens. Based on logical the equivalences then we may say part of the KB Goal Theorem of entailment, Resolution and thinking backwards allows a more efficient reasoning technique: Backward Chaining. Class 1 Copyright 2016 Edjard Mota Propositional Logical Reasoning Definition 10. Propositional Horn Clause: restricted form of clause which is either 1. a fact (called unit clause) A. (also written A ←.) 2. an implication with only one consequent (called the head), i.e. A ← B1 ∧ B2 ∧… ∧ Bn. (the premisses is called the body) 3. a propositional goal is a clause with no consequent, i.e. ← B1 ∧ B2 ∧… ∧ Bn. (or equivalent to ¬B1 ∨ ¬B2 ∨… ∨ ¬Bn.) are called sub-goals. A definite program clause or definite logic program (DLP) is a set of Horn Clauses only of types (1) and (2). Remember: for a set 𝓕 of formulas and goal Backward Chaining Reasoning ∪ {¬G} ⊢ F, means G is a logical consequence of 𝓕. Class 1 Copyright 2016 Edjard Mota Propositional Logical Reasoning Backward Chaining Reasoning (BCR) For a given DLP 𝓕 and a goal G • If 𝓕 has a clause Z = A ← B1 ∧ B2 ∧… ∧ Bn , s.t A matches G, then apply resolution to Z and A, if n=0, then Z is a fact and so G is entailed from 𝓕 else for i = 1 to n, apply BCR on 𝓕 and Bi as a goal if every Bi a has been entailed from 𝓕,then so is G • If for G or some sub-goal B during BCR no clause was found with head matching it, then G is not entailed from 𝓕 𝓕 B11 ← C11 ∧… ∧ Ckk B2 ← … … A ← B11 ∧ B2 ∧… ∧ Bn A ← B’1 ∧ B’2 ∧… ∧ B’n … Class 1 G A 1st resolution step BB11 BB22 … BBnn … … … Copyright 2016 Edjard Mota Propositional Logical Reasoning Example of deducing u from 𝓕 = {(s ← p), (u ← s), p} New goal Matching rule Sub-goal u u u s u←s s←p p← s p s s p empty PROLOG: Logic Programming Language Inverse implication is written as “:-” Logic program Query to the system Class 1 p p p. s :- p. u :- s. ?- u. true. Copyright 2016 Edjard Mota Summary Main concepts of (Propositional) Logic Syntax: specifies the formal structure of sentences Semantics: defines the truth of sentences relate to facts in the world specifying the. Models are interpretations that make the value of a sentence to be true. Entailment: necessary truth of one sentence given another Inference: calculus for deriving sentences from other sentences Validity of a sentence can be checked via truth-tables or inference rules based on logical equivalence. Inference mechanisms: Forward chaining: validates a goal by deriving it from facts and rules via Modus Ponens Backward chaining: validates a goal by applying resolution only on rules whose consequent matches the goal, and turn the premisses of such rule in sub-goals to be validated Class 1 Copyright 2016 Edjard Mota