AI Lecture 4 Logic
AI Lecture 4 Logic
Logic
Logic?
• In general, logic is the study of correct reasoning.
• This includes how to identify good arguments and avoid bad ones.
• It helps us ensure our thinking is clear and our conclusions follow from our
evidence.
• More specifically, logic can refer to the field of formal logic.
• This is a branch of mathematics that studies the structure of valid
arguments.
• It uses symbols and rules to analyze arguments without worrying about the
specific content.
Logic?
• Logic is essentially the toolbox for good reasoning.
• It equips you to distinguish between strong arguments and weak ones.
• Here's a breakdown of the concept:
• Core principle: Logic is the study of how to arrive at well-
founded conclusions.
• Focus: It emphasizes analyzing information and arguments based
on reason and evidence, steering clear of emotions or biases.
• Applications: Logic is used in various fields like math, philosophy,
and computer science, but also in everyday situations where we
need to make sound judgments.
Categories/Types of Logic
1. Formal vs. Informal Logic
• Formal Logic: This is the technical side, using symbols and strict rules to
analyze the structure of arguments. It focuses on whether the form of the
argument guarantees a true conclusion if the starting statements (premises)
are true. Examples include:
• Propositional Logic: Deals with basic statements (propositions) like "It is
raining" and explores how they can be combined (AND, OR, NOT) to form
more complex statements.
• First-Order Logic: Expands on propositional logic by allowing variables and
quantifiers (all, some, none) to represent objects and properties.
• Informal Logic: This deals with arguments expressed in everyday language. It
equips you to analyze arguments based on their content and structure, identify
fallacies (errors in reasoning) like hasty generalizations or post hoc fallacies,
and evaluate the strength of the argument.
Categories/Types of Logic (Cont’)
2. Other Specialized Logics
Beyond the formal/informal divide, there are various forms of logic that focus on
specific areas:
• Modal Logic: Deals with possibility, necessity, and obligation. Useful in fields
like philosophy and computer science.
• Deontic Logic: Focuses on concepts of obligation, permission, and prohibition.
Relevant in legal reasoning and ethics.
• Fuzzy Logic: Used in situations where there are degrees of truth (not just true
or false). Useful in control systems and artificial intelligence.
• Higher-order logic (HOL): is a powerful type of formal logic that expands on
the capabilities of first-order logic.
• Aristotelian logic, Euclidean geometry, Peano axioms, Zermelo Fraenkel set
theory, etc
• By understanding logic, you can:
• Think critically: You can assess information objectively and
avoid being swayed by misleading arguments.
• Strengthen your arguments: You can learn how to present
your ideas in a clear and convincing way.
• Solve problems: You can approach challenges in a logical
and systematic manner.
6
Propositional Logic
• Propositional logic, also sometimes called sentential logic or statement
logic, is the foundation of formal logic.
• It deals with the relationships between basic statements, also known as
propositions.
• A statement is a verbal or written assertion.
• A proposition is a statement that is either true or false, but not both.
• We say that the truth value of a proposition is either true (T) or false (F).
7
Statement/Proposition
• For example,
• p:Kofi is a student.
• The basic property of any statement is, it is either true (T) or false (F) and
cannot be true or false at the same time.
10
Logical connectives or operators
The basic logical operators are
Conjunction (AND or ∧ )
Disjunction (OR or ∨)
Negation (NOT or ~)
p: Kofi is a student
The converse of the compound statement: “Kofi is a student and in form five” is
‘Kofi is in form five and a student’. This converse statement is true only when the
compound statement is true.
i.e. if p∧q is true then the converse statement q∧p is also true.13
Converse statements
c. Converse of disjunction (OR)
p: Kofi is a student
This converse statement is true only when the compound statement is true.
i.e. if p∨q is true then the converse statement q∨p is also true.14
The truth table
• The truth table is used to show whether two logical expressions are the same
or not.
• It also shows how the truth of a compound statement depends on the truth of
its component simple statements.
15
And () (Conjunction)
Two simple statements p and q can be combined by the word “AND” to form a
compound statement “p and q”.
For example, p: Kofi is a student and q: Kofi is in form five, can be combined as “
Kofi is a student and in form five”.
The compound statement p ^ q is true only when both p and q are true.
Otherwise it is false. 16
And () (Conjunction) cont’
• The table below is the truth table for p q
p q pq
T T T
T F F
F T F
F F F
The first two columns give the four possible values of p and q. The third column if for p q.
For the first row p is the true (T) and q is also true (T), therefore p q is true since both p and q are true.
For the other three rows p q is false since both p and q are not true.
Note that the compound statement p q is true only when p and q are both true.
17
‘Or’ (Disjunction)
When two statements are connected by the word “OR”, the result of this
combination is called disjunction of the two statements.
Exclusive disjunction(⊻)
18
Inclusive Or (∨) (Inclusive
disjunction)
• If p and q are two simple statements then the compound statement p or q
written as p ∨ q is true when any of the statements p or q or
both are true. Otherwise it is false.
• Otherwise it is false. 19
Inclusive Or (∨) (Inclusive
disjunction)
• Below is the truth table of p ∨ q (p or q or both)
p q p∨q
T T T
T F T
F T T
F F F
The third column is p ∨ q. Since both p and q are false in the fourth row, p ∨ q is also false.
Note that if at least one of the two simple statements is true, then p ∨ q is true.
20
Exclusive Or (⊻) ( Exclusive disjunction)
• Otherwise it is false.
p q p⊻ q
T T F
T F T
F T T
F F F
The third column is p ⊻ q . Since both p and q are false in the fourth row, p ⊻ q
is also false.
Also since both p and q are true in the first row, p ⊻ q is false.
Note that if exactly one of the two simple staements is true p ⊻ q is true.
22
Not (~) (Negation)
• Given a simple statement p, another statement called the negation of p can be formed
by inserting in statement p the word “not”.
• For these two statements, if p is true, then q must be false. q is said to be the negation
of p
p ~p
T F
F T
24
Implication/Conditional (⇒) (→ )
For any two statements p and q, p⇒q means q is true only when p is true.
The compound statement ‘If Abena has measles then Abena is in the
25
Implication/Conditional (⇒) (→ )
• Consider the truth table for p⇒q below
p q p⇒q
T T T
T F F
F T T
F F T
For the fourth row, although both statements are false p⇒q
26
is true.
Identifying conditional statements
• “if p, then q”
• “p implies q”
• Because conditional statements • “if p, q”
play such an essential role in • “p only if q”
mathematical/logical reasoning, • “p is sufficient for q”
a variety of terminology is used • “a sufficient condition for q is p”
to express p → q. • “q if p”
• You will encounter most if not • “q whenever p”
all of the following ways to • “q when p”
express this conditional • “q is necessary for p”
statement: • “a necessary condition for p is q”
• “q follows from p”
27
• “q unless ¬ p”
CONVERSE, CONTRAPOSITIVE, AND
INVERSE of Conditional Statements
• We can form some new conditional statements starting with a conditional
statement p → q.
• In particular, there are three related conditional statements that occur so
often that they have special names.
• The proposition q → p is called the converse of p → q.
• The contrapositive of p → q is the proposition ¬ q → ¬ p.
• The proposition ¬ p → ¬ q is called the inverse of p → q.
• Of these three conditional statements formed from p → q, only the
contrapositive always has the same truth value as p → q.
28
EXAMPLE: What are the contrapositive, the
converse, and the inverse of the conditional
statement:
“The home team wins whenever it is raining?”
• Solution: Because “q whenever p” is one of the ways to express the conditional
statement p → q, the original statement can be rewritten as
• “If it is raining, then the home team wins.”
• Consequently, the contrapositive of this conditional statement is:
• “If the home team does not win, then it is not raining.”
• The converse is:
• “If the home team wins, then it is raining.”
• The inverse is:
• “If it is not raining, then the home team does not win.”
29
Equivalence/Biconditional (↔)
• If p and q are said to be equivalent, it means that they have
the same truth value.
30
Equivalence/Biconditional (↔)
• Truth table that shows the possible truth values for the equivalence "p ↔ q":
p q p↔q
T T T
T F F
F T F
F F T
• p and q are equivalent if and only if they have the same truth value in all cases.
31
Identifying biconditional
statements
• There are some other common ways to express p ↔ q:
• “p is necessary and sufficient for q”
• “if p then q, and conversely”
• “p iff q.”
• The last way of expressing the biconditional statement p ↔ q uses the
abbreviation “iff” for “if and only if.”
• Note that p ↔ q has exactly the same truth value as (p → q) ∧ (q → p).
32
Examples:
1. Form the truth table for (p ~ q) and hence ~(p ~ q)
p q ~q p^~q ~(p^~q)
T T F F T
T F T T F
F T F F T
F F T F T
33
Examples:
2. Construct a truth table for the logical expression "(p ↔ q) ∧
(~r)" (conjunction of equivalence and negation).
34
Famous logical equivalences
• (a OR b) ↔ (b OR a) commutatitvity
• (a AND b) ↔ (b AND a) commutatitvity
• ((a AND b) AND c) ↔ (a AND (b AND c)) associativity
• ((a OR b) OR c) ↔ (a OR (b OR c)) associativity
• NOT(NOT(a)) ↔ a double-negation elimination
• (a => b) ↔ (NOT(b) => NOT(a)) contraposition
• (a => b) ↔ (NOT(a) OR b) implication elimination
• NOT(a AND b) ↔ (NOT(a) OR NOT(b)) De Morgan
• NOT(a OR b) ↔ (NOT(a) AND NOT(b)) De Morgan
• (a AND (b OR c)) ↔ ((a AND b) OR (a AND c)) distributivity
• (a OR (b AND c)) ↔ ((a OR b) AND (a OR c)) distributivity
Tautology
In logic, a tautology is a statement or logical expression that
is always true, regardless of the truth values of its
components or variables.
In other words, a tautology is a statement that is true in every
possible interpretation or truth assignment.
To identify a tautology, we can construct a truth table for the
logical expression and observe that the result is always true.
36
Tautology
• Example of a tautology and its truth table:
• From the truth table, regardless of the truth value of p, the expression p OR (NOT p) always
• Tautologies are important in logic and mathematics as they represent statements that are
37
Contradiction
In logic, a contradiction is a statement or logical expression that is
always false, regardless of the truth values of its components or
variables.
It represents a situation where a statement asserts both a claim and
its negation simultaneously.
To identify a contradiction, we can construct a truth table for the
logical expression and observe that the result is always false.
38
Contradiction
• Example of a contradiction and its truth table:
• Logical Expression: p AND (NOT p)
• Equivalence is a contingency.
40
Construct truth tables to test the following
sentences for tautology, contradiction and
contingency.
1. P→Q 11. [P → (Q → R)] & (P → R)
2. (P v ~P) & (Q & ~Q) 12. (P & Q) → (P → Q)
3. P←→Q 13. ~P → P
4. ~ (P & ~P) 14. ~P → (Q v P)
5. ~ (P v ~P) 15. ~P & ~(~P v ~Q)
6. ~ (P v P) 16. P v (Q → P)
7. (P & ~P) v (Q & ~Q) 17. (P ← → Q) & [(~P v ~Q) & P]
8. ~ (P v ~~Q) & (P & ~Q) 18. (P ← → Q) & (P → Q)
9. (P ← → Q) v (Q ← → P) 19. [P → (Q → R)] & (R → P)
10. ~ [(P → Q) → R] 20. (P ← → Q) v ~ [(~P & Q) v (P & ~Q)
41
Properties of Operators
1. Commutativity: 4. Identity:
• P ∧ true = P
• P∧Q↔Q∧P
• P ∨ false = P
• P∨Q↔Q∨P
5. Domination:
2. Associativity: • P ∨ true = true
• (P ∧ Q) ∧ R ↔ P ∧ (Q ∧ R) • P ∧ false = false
• (P ∨ Q) ∨ R ↔ P ∨ (Q ∨ R)
6. Double Negation:
• ¬ (¬P) = P
3. Distributivity:
• P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R) 7. Idempotence:
• P∧P=P
• P ∨ (Q ∧ R) ↔ (P ∨ Q) ∧ (P ∨ R)
• P∨P=P
Applications of Propositional
Logic in AI
1. Knowledge Representation:
Propositional logic is used to represent knowledge in a structured and
unambiguous way. It allows AI systems to store and manipulate facts
about the world. For instance, in expert systems, knowledge is encoded
as a set of propositions and logical rules.
2. Automated Reasoning:
AI systems use propositional logic to perform automated reasoning.
Logical inference rules, such as Modus Ponens and Modus Tollens, enable
systems to derive new knowledge from existing facts. For example:
Modus Ponens: If “P → Q” and “P” are true, then “Q” must be true.
Modus Tollens: If “P → Q” and “¬Q” are true, then “¬P” must be true.
Applications of Propositional
Logic in AI
3. Problem Solving and Planning:
Propositional logic is fundamental in solving problems and planning actions.
AI planners use logical representations of actions, states, and goals to
generate sequences of actions that achieve desired outcomes. For example,
the STRIPS planning system employs propositional logic to represent
preconditions and effects of actions.
4. Decision Making:
In decision-making processes, propositional logic helps AI systems evaluate
various options and determine the best course of action. Logical rules can
encode decision criteria, and truth tables can be used to assess the
outcomes of different choices.
Applications of Propositional
Logic in AI
5. Natural Language Processing (NLP):
Propositional logic is applied in NLP for tasks like semantic parsing, where
natural language sentences are converted into logical representations. This
helps in understanding and reasoning about the meaning of sentences.
6. Game Theory and Multi-Agent Systems:
In game theory and multi-agent systems, propositional logic is used to
model the beliefs and actions of agents. Logical frameworks help in
predicting the behavior of agents and designing strategies for interaction.
Limitations of Propositional
Logic
• Lack of Expressiveness: does not allow for the representation of how one
proposition relates to another or to use variables to refer to objects in the
world
• Scalability: the size of the resultant truth tables increases exponentially (2n)
with the number of propositions, which makes practical problems infeasible.
• Limited Inference: It can only take on ‘true or false’ solutions, not probabilities
or multidimensional levels of truth.
• Absence of Quantifiers: Unlike predicate logic, propositional logic does not
allow the use of quantifiers such as “for all” (denoted by ∀) or “there exists”
(denoted by ∃), both of which are powerful expressions.
Inference
• The act of checking the validity of each proposition and operation, and
finally evaluating the outcome (conclusion) is known as inference.
• However, besides evaluating extensive truth tables all the time, we can also
make use of several inference rules to arrive at the final outcome or
conclusion.
• The main reason for doing so would be that the size of these truth tables
with the various operations starts increasing exponentially (with the
number of propositions increasing.
Inference rules (Rules of Inference)
• Rules of inference are syntactical transform rules which one can use to infer
a conclusion from a premise (hypotheses, beliefs, etc.)
• Rules of Inference provide the templates or guidelines for constructing valid
arguments from the statements that we already have.
• A sequence of inference rules, when applied, leads to a conclusion that is
often termed as a logical proof.
• The most popular inference rules are as follows:
Modus Ponens
• Perhaps the most popular inference rule, it’s also known as the Implication
Elimination rule or law of detachment.
• It can be represented as ((P → Q) ^ P) → Q , which indicates that if P
implies Q and P is asserted to be True , then it is inferred that Q is True .
• The representation can be evaluated easily using truth tables.
• A simple example would be the statement: If it is sunny, we will play
football, represented by P → Q .
• Now if we say that It is sunny, this indicates that P is True, hence Q
automatically is inferred as True , indicating We will play football.
Modus Tollens
• This is quite similar to the previous rule and is represented formally as
((P → Q) ^ ¬Q) → ¬P, which indicates that if P implies Q and Q is actually
asserted to be False, then it is inferred that P is False.
• An example proposition would be If he is a bachelor, he is not married,
indicated by P → Q .
• Now if we propose that He is married, represented by ¬Q, then we can
infer ¬P, which translates to He is not a bachelor.
Disjunctive Syllogism
• This is also known as Disjunction Elimination and is formally represented
as ((P v Q), ^ ¬P) → Q, which indicates that if either P or Q is True and P is
False , then Q is True.
• A simple example would be the statement: He is a miracle worker or a
fraud represented by P v Q and the statement: He is not a miracle worker
represented by ¬P. We can then infer He is a fraud , depicted by Q.
Hypothetical Syllogism
• This is often known as the Chain Rule of Deduction or Transitivity and is
formally represented as ((P → Q) ^ (Q → R)) → (P → R) , which tells us that if
P implies Q , and Q implies R , we can infer that P implies R .
• A really interesting example to understand this would be the statement: If I
am sick, I can’t go to work represented by P → Q and If I can’t go to work,
the building construction will not be complete represented by Q → R .
• Then we can infer If I am sick, the building construction will not be complete,
which can be represented by P → R .
Constructive Dilemma
• This inference rule is the disjunctive version of Modus Ponens and can be formally
represented as (((P → Q) ^ (R → S)) ^ (P v R)) → (Q v S), which indicates that if P
implies Q , and R implies S, and either P or R is True , then it can be inferred that
either Q or S is True.
• Consider the following propositions: If I work hard, I will be successful represented
by P → Q , and If I win the lottery, I will be rich represented by R → S.
• Now we propose that I work hard or I win the lottery is True , which is represented
by P v R .
• We can then infer that I will be successful or I will be rich, represented by Q v S.
• The complement of this rule is Destructive Dilemma, the disjunctive version of
Modus Tollens.
First-Order Logic (FOL)
A story
• Your roommate comes home; he/she is completely wet
• You know the following things:
• Your roommate is wet
• If your roommate is wet, it is because of rain, sprinklers, or both
• If your roommate is wet because of sprinklers, the sprinklers must be on
• If your roommate is wet because of rain, your roommate must not be carrying
the umbrella
• The umbrella is not in the umbrella holder
• If the umbrella is not in the umbrella holder, either you must be carrying the
umbrella, or your roommate must be carrying the umbrella
• You are not carrying the umbrella
• Can you conclude that the sprinklers are on?
• Can AI conclude that the sprinklers are on?
Knowledge base for the story
• RoommateWet
• RoommateWet => (RoommateWetBecauseOfRain OR
RoommateWetBecauseOfSprinklers)
• RoommateWetBecauseOfSprinklers => SprinklersOn
• RoommateWetBecauseOfRain =>
NOT(RoommateCarryingUmbrella)
• UmbrellaGone
• UmbrellaGone => (YouCarryingUmbrella OR
RoommateCarryingUmbrella)
• NOT(YouCarryingUmbrella)
Limitations of propositional
logic
• So far we studied propositional logic
• Some English statements are hard to model in
propositional logic:
• “If your roommate is wet because of rain, your
roommate must not be carrying any umbrella”
• Pathetic attempt at modeling this:
• RoommateWetBecauseOfRain =>
(NOT(RoommateCarryingUmbrella0) AND
NOT(RoommateCarryingUmbrella1) AND
NOT(RoommateCarryingUmbrella2) AND …)
Problems with propositional
logic
• No notion of objects
• No notion of relations among objects
• RoommateCarryingUmbrella0 is instructive to us, suggesting
• there is an object we call Roommate,
• there is an object we call Umbrella0,
• there is a relationship Carrying between these two objects
• (ꓯx), (ꓱy) : loves(x, y) means that everyone in the world loves at least
someone.
• (ꓱy) : (ꓯx), loves(x, y) means that someone in the world is loved by
everyone.
• (ꓯy), (ꓱx) : loves(x, y) means that everyone in the world has at least
someone who loves them.
• (ꓱx) : (ꓯy), loves(x, y) means that there is at least someone in the
world who loves everyone.
Quantifier Identities and
Properties
Natural Language and FOL
Practice converting English to first-order
logic
• for all x: a
• is equivalent to
• NOT(there exists x: NOT(a))
Something we cannot do in first-order logic