class: center, middle # Artificial Intelligence ### Logic --- class: center, middle # Logic --- # Why Logic? * Remember: Everything in AI is either representation or search * Logical formulas are often a very convenient representation * Why convenient? - Can be generated automatically - Can be processed automatically - Are very expressive * Logic is used extensively in AI! --- # Propositional Logic * `\(a \wedge b\)`: a **and** b * `\(a \vee b\)`: a **or** b (or both!) * `\(\neg a\)`: **not** a * `\(a \rightarrow b\)`: a **implies** b, which means: if `a` is true, `b` also has to be true (but if `a` is false, `b` can be true or false). Logically equivalent to `\(\neg a \vee b\)` * When you see **iff** in text it is not a typo, it means "if and only if", a bidirectional implication ("a iff b" means that if `a` is true `b` is true, but if `a` is false, `b` is also false) --- # Propositional Logic * How do we tell if a formula is true? * We need to know if a and b (and all other constants) are "true" * In other words, if we have some "interpretation" `W` of `a` and `b`, we can determine truth values * `\( W \models a \wedge b \)` iff `a` and `b` are both true in `W`, i.e. `\( W \models a \)` **and** `\(W \models b \)` * So what is W? --- class: medium # Interpretations * W is an *interpretation* (also called *state* or *world*), which assigns truth values to every constant * Sometimes it is convenient to just represent W as a set that contains everything that is true * Conversely, everything that is not in the set is false ("closed world assumption") * A *model* is an interpretation under which a formula is satisfied * We say W *models* a formula, if the formula is satisfied under that interpretation * A formula is a tautology if *all* interpretations are models --- # Interpretations as sets * Why might it be convenient to represent `W` as a set? * Determining the truth value of a formula reduces to set-operations $$ W \models (\neg a) \equiv a \not\in W\\\\ W \models (a \wedge b) \equiv a \in W, b \in W\\\\ W \models (a \vee b) \equiv a \in W \text{or} b \in W\\\\ W \models (a \rightarrow b) \equiv a \not\in W \text{or} b \in W $$ More complex formulas need to be split up until we reach atoms. --- # Interpretation Given an interpretation `\(W = \{ a, b, c \}\)`. For each of the following formulas determine if `\(W\)` is a model for that formula. $$ (a \wedge b)\\\\ (b \rightarrow d)\\\\ (d \rightarrow b)\\\\ b \wedge ((c \rightarrow e) \rightarrow d)\\\\ e \vee (c \wedge \neg d)\\\\ \neg d \rightarrow \neg e $$ --- # Interpretation $$ W = \\{a,b,c\\} $$ $$ W \models a \wedge b $$ ### because `a` and `b` are both elements of `W`. --- # Interpretation $$ W = \\{a,b,c\\} $$ $$ W \not\models (b \rightarrow d) $$ ### because `b` is an element of `W`, but `d` is not. --- class: medium # Interpretation $$ W = \\{a,b,c\\} $$ $$ W \models b \wedge ((c \rightarrow e) \rightarrow d) $$ * `b` is an element of `W` * Next we need to interpret `\(((c \rightarrow e) \rightarrow d)\)` * For **that** we need to interpret `\(c \rightarrow e\)` * `c` is an element of `W`, but `e` is not, so `W` does not model `\(c \rightarrow e\)` * However, this means that `W` **does** model `\(((c \rightarrow e) \rightarrow d)\)` * And therefore `W` models our entire formula `\(b \wedge ((c \rightarrow e) \rightarrow d)\)` --- # Interpretation $$ W = \\{a,b,c\\} $$ $$ W \models e \vee (c \wedge \neg d) $$ * `e` is not an element of `W` * But maybe we have more luck with the right side `\(c \wedge \neg d\)` * `c` is an element of `W`, `d` is not, so `\(c \wedge \neg d\)` is true --- # Interpretation $$ W = \\{a,b,c\\} $$ $$ W \models \neg d \rightarrow \neg e $$ * `d` is not an element of `W` * Neither is `e` * This means both sides of our implication are true * And therefore `W` is a model of this formula --- class: center, middle # Predicate Logic --- # Predicate Logic * Representing any non-trivial state in propositional logic is tedious (imagine chess: there has to be one variable for each possible location of each piece) * *Predicate Logic*, on the other hand, represents the world as objects and relations between them * Objects: constants * Relations: Sets of n-tuples of objects, called predicates (n is the *arity* of the predicate) * (Functors: Assignments of n-tuples of objects to objects) * We also have quantifiers `\( \forall, \exists \)` --- # Predicate Logic: Example $$ \mathit{human}(\mathit{socrates})\\\\ \forall h: \mathit{human}(h) \rightarrow \mathit{mortal}(h)\\\\ \mathit{friends}(\mathit{socrates}, \mathit{plato}) $$ -- `\(\mathit{human}(h)\)` is an unary relation, i.e. a set of 1-tuples/single elements. It is often more convenient to write: $$ \forall h \in \mathit{human}: \mathit{mortal}(h) $$ -- Using set-operators, we could also write `\((\mathit{socrates}, \mathit{plato}) \in \mathit{friends}\)` In fact, as we will see, that's how our interpretations work. --- # Predicate Logic: Syntax * A predicate name with parameters is called an *Atom* or *atomic formula*, e.g. `\(\mathit{human}(h)\)` * A *literal* is an atom of the negation of an atom, e.g. `\(\neg \mathit{human}(h)\)` * The `h` can either be a constant or a variable * If `h` is part of our *domain*, it is a constant * If `h` does not refer to any concrete object, it is a variable. A variable can be *bound* by a quantifier, or it can be *unbound*/*free* * A formula with no free variables is called *ground* --- # Predicate Logic: Syntax * Given two predicate logic formulas `\(\phi\)` and `\(\psi\)`, we can use the usual logical connectives `\(\wedge, \vee, \rightarrow, \neg \)` to construct more complex formulas * Conceptually, this will result in a (syntax) tree, where each interior node is an operator, and the leaves are atoms * If we have a formula `\(\phi\)` that contains a free variable `x`, we can *substitute* that variable with a value t, written `\(\phi[t/x]\)` --- # Predicate Logic: Semantics * Given a (ground) formula `\(\phi\)` and an interpretation/world `\(W\)`, we want to know if the formula holds in that world, i.e. if the world is a model for the formula * We write this as `\(W \models \phi\)` ("W models phi") * How do we determine that? - If `\(\phi\)` is an atom, it is true iff `\(\phi \in W\)` - If `\(\phi\)` is a more complex formula, use the syntax tree to determine the truth value --- # Predicate Logic: Semantics $$ W \models \neg \phi \:\:\text{iff}\:\:W \not\models \phi\\\\ W \models \phi \wedge \psi\:\:\text{iff}\:\:W \models \phi\:\:\text{and}\:\:W \models \psi\\\\ W \models \phi \vee \psi\:\:\text{iff}\:\:W \models \phi\:\:\text{or}\:\:W \models \psi\\\\ W \models \phi \rightarrow \psi\:\:\text{iff}\:\:W \not\models \phi\:\:\text{or}\:\:W \models \psi\\\\ W \models \forall x \in X: \phi\:\:\text{iff}\:\:W \models \phi[t/x]\:\:\text{for all}\:t \in X\\\\ W \models \exists x \in X: \phi\:\:\text{iff}\:\:W \models \phi[t/x]\:\:\text{for any}\:t \in X $$ --- class: small # Predicate Logic: Interpretations * We start with a *domain*, the set of all objects we can talk about * Technically, an interpretation has to assign objects to all our constants, since Socrates and Plato could be the same person in an interpretation! We will use the *unique-names assumption* to avoid such problems and will just assume that the constants are the same as the objects. * Then, the interpretation has to contain definitions for every predicate, in the form of a set of tuples. * The *closed world assumption* states that everything we don't know to be true (i.e. everything not in the set representing a predicate) is in fact false. * Using these assumptions, we can represent our interpretation as a set of true atoms, or as a collection of sets, one for each predicate. --- # Predicate Logic: Interpretations W: $$ \mathit{human} = \\{\mathit{socrates}, \mathit{plato}, \mathit{aristotle}\\}\\\\ \mathit{mortal} = \\{\mathit{socrates}, \mathit{plato}, \mathit{aristotle}, \mathit{DonaldDuck}\\}\\\\ \mathit{friends} = \\{(\mathit{socrates}, \mathit{plato}), (\mathit{socrates}, \mathit{DonaldDuck})\\}\\\\ $$ $$ W \models \mathit{human}(\mathit{socrates}) \\\\ W \models \forall h: \mathit{human}(h) \rightarrow \mathit{mortal}(h) \\\\ W \models \mathit{friends}(\mathit{socrates}, \mathit{plato})\\\\ W \not\models \mathit{friends}(\mathit{socrates}, \mathit{socrates})\\\\ \ldots $$ --- # Predicate Logic: Interpretations $$ W = \\{\mathit{human}(\mathit{socrates}), \mathit{human}(\mathit{plato}), \mathit{human}(\mathit{aristotle}), \\\\ \mathit{mortal}(\mathit{socrates}), \mathit{mortal}(\mathit{plato}), \mathit{mortal}(\mathit{aristotle}), \mathit{mortal}(\mathit{DonaldDuck}),\\\\ \mathit{friends}(\mathit{socrates}, \mathit{plato}), \mathit{friends}(\mathit{socrates}, \mathit{DonaldDuck})\\}\\\\ $$ $$ W \models \mathit{human}(\mathit{socrates}) \\\\ W \models \forall h: \mathit{human}(h) \rightarrow \mathit{mortal}(h) \\\\ W \models \mathit{friends}(\mathit{socrates}, \mathit{plato})\\\\ W \not\models \mathit{friends}(\mathit{socrates}, \mathit{socrates})\\\\ \ldots $$ --- # Example You are given an interpretation $$ W = \\{ \mathit{Lannister}(\mathit{Tywin}), \mathit{Lannister}(\mathit{Tyrion}), \mathit{Lannister}(\mathit{Cersei}), \\\\ \mathit{Lannister}(\mathit{Jaime}), \mathit{owes}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{owes}(\mathit{Jaime},\mathit{Brienne}), \\\\ \mathit{owes}(\mathit{Brienne}, \mathit{Catelyn}), \mathit{paidDebt}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{paidDebt}(\mathit{Jaime}, \mathit{Brienne}) \\} $$ Over the domain $$ D= \\{\mathit{Tywin}, \mathit{Tyrion}, \mathit{Cersei}, \mathit{Jaime}, \mathit{Bronn}, \mathit{Brienne}, \mathit{Catelyn}\\} $$ For each of the following formulas, determine if $W$ is a model of that formula over $D$. $$ \forall x \forall y \in Lannister: \mathit{owes}(y,x) \rightarrow \mathit{paidDebt}(y,x)\\\\ \exists x \forall y: \neg \mathit{owes}(x,y)\\\\ \exists x \forall y: \neg \mathit{owes}(y,x)\\\\ \forall y \exists x: \mathit{owes}(y,x) \vee \mathit{owes}(x,y)\\\\ \forall x: \mathit{owes}(x, \mathit{Catelyn}) \rightarrow \neg \mathit{Lannister}(x) $$ --- # Example $$ W = \\{ \mathit{Lannister}(\mathit{Tywin}), \mathit{Lannister}(\mathit{Tyrion}), \mathit{Lannister}(\mathit{Cersei}), \\\\ \mathit{Lannister}(\mathit{Jaime}), \mathit{owes}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{owes}(\mathit{Jaime},\mathit{Brienne}), \\\\ \mathit{owes}(\mathit{Brienne}, \mathit{Catelyn}), \mathit{paidDebt}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{paidDebt}(\mathit{Jaime}, \mathit{Brienne}) \\} $$ $$ W \models \forall x \forall y \in Lannister: \mathit{owes}(y,x) \rightarrow \mathit{paidDebt}(y,x)\\\\ $$ * We need to consider all `x` and `y` (for which `y` is in the set Lannister). * If `y` owes `x`, they also have to pay their debt. * Tyrion owes Bronn, and Jaime owes Brienne, which are the only Lannisters to owe anyone, and both paid their debt. **A Lannister always pays his debt** --- # Example $$ W = \\{ \mathit{Lannister}(\mathit{Tywin}), \mathit{Lannister}(\mathit{Tyrion}), \mathit{Lannister}(\mathit{Cersei}), \\\\ \mathit{Lannister}(\mathit{Jaime}), \mathit{owes}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{owes}(\mathit{Jaime},\mathit{Brienne}), \\\\ \mathit{owes}(\mathit{Brienne}, \mathit{Catelyn}), \mathit{paidDebt}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{paidDebt}(\mathit{Jaime}, \mathit{Brienne}) \\} $$ $$ W \models \exists x \forall y: \neg \mathit{owes}(x,y) $$ * "There is someone for whom it is false that they owe them, for everyone else" ("There is someone that does not owe anyone"). * Tywin, in our interpretation, does not owe anyone. --- # Example $$ W = \\{ \mathit{Lannister}(\mathit{Tywin}), \mathit{Lannister}(\mathit{Tyrion}), \mathit{Lannister}(\mathit{Cersei}), \\\\ \mathit{Lannister}(\mathit{Jaime}), \mathit{owes}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{owes}(\mathit{Jaime},\mathit{Brienne}), \\\\ \mathit{owes}(\mathit{Brienne}, \mathit{Catelyn}), \mathit{paidDebt}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{paidDebt}(\mathit{Jaime}, \mathit{Brienne}) \\} $$ $$ W \models \exists x \forall y: \neg \mathit{owes}(x,y) $$ * "There is someone for whom it is false that they are owed something, for everyone else" ("There is someone that is not owed by anyone"). * Once again, we can use Tywin as `x`, because no one owes Tywin. --- # Example $$ W = \\{ \mathit{Lannister}(\mathit{Tywin}), \mathit{Lannister}(\mathit{Tyrion}), \mathit{Lannister}(\mathit{Cersei}), \\\\ \mathit{Lannister}(\mathit{Jaime}), \mathit{owes}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{owes}(\mathit{Jaime},\mathit{Brienne}), \\\\ \mathit{owes}(\mathit{Brienne}, \mathit{Catelyn}), \mathit{paidDebt}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{paidDebt}(\mathit{Jaime}, \mathit{Brienne}) \\} $$ $$ W \not\models \forall y \exists x: \mathit{owes}(y,x) \vee \mathit{owes}(x,y) $$ * "For everyone there is someone that they owe to or that owes them". * We need to look at each character and see if they have any outgoing or incoming debts. * However, Tywin has (as we discovered in formula 2) no one that he owes, nor (as we discovered in formula 3) anyone that owed him, and therefore `W` is not a model for this formula. --- # Example $$ W = \\{ \mathit{Lannister}(\mathit{Tywin}), \mathit{Lannister}(\mathit{Tyrion}), \mathit{Lannister}(\mathit{Cersei}), \\\\ \mathit{Lannister}(\mathit{Jaime}), \mathit{owes}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{owes}(\mathit{Jaime},\mathit{Brienne}), \\\\ \mathit{owes}(\mathit{Brienne}, \mathit{Catelyn}), \mathit{paidDebt}(\mathit{Tyrion}, \mathit{Bronn}), \mathit{paidDebt}(\mathit{Jaime}, \mathit{Brienne}) \\} $$ $$ W \models \exists x \forall y: \neg \mathit{owes}(x,y) $$ * "Everyone that owes Catelyn is not a Lannister". * For every `x` we first need to determine if they owe Catelyn, which is only true for Brienne. * For every other `x` the antecendent is false, and therefore the implication is true. * For Brienne, the consequence is true, since she is not a Lannister. Therefore, the implication holds for all `x`. --- # WHY? * You might say: "But we've already done logic in CS1300" * Or: "This seems more complicated than necessary" * So, why are we doing this? * AI: Representation and Search; logic is our representation * But that's not enough, we also need to implement it! --- # Implementing Logic ### What do we need to actually use this? * Data structure for interpretations and formulas * Methods to determine truth values ("models") -- ### What about changes in the world and/or actions our agent can perform? ### Next time we will look into these topics