Sie sind auf Seite 1von 55

Logic in Computer Science

C-DAC 2009-10 1

Overview
Introduction to logic Propositional logic Predicate logic

C-DAC 2009-10 2

Introduction to Logic
The science or art of exact reasoning, or of pure and formal thought, or of the laws according to which the processes of pure thinking should be conducted. Aristotle Father of logic Recent developments resulted in symbolic logic
C-DAC 2009-10 3

Introduction to Logic
If you study well, you will pass the exam. You study well. Therefore you will pass the exam Is this valid?
C-DAC 2009-10 4

Structure of argument
A conclusion following from the assumptions A list of statement called premises or

assumptions
Note that arguments from different domains have similar structure. Can we have a general method to check their validity?
C-DAC 2009-10 5

Structure of argument
If you study well, -> p then you will pass the exam. -> q You study well. -> p Therefore you will pass the exam -> q If p, then q. p.Therefore q. Note that we can check the validity of the argument without really knowing what are p and q. C-DAC 2009-10
6

Symbolic Logic
If p, then q. p.Therefore q. We could represent the argument in the previous slide using general symbols instead of domain specific terms. Unambiguous, precise Easy for automated reasoning

C-DAC 2009-10 7

Symbolic logic
Subclasses 1. Propositional logic (PL) 2. First order logic (FL) 3. Higher order logic 4. Modal logic

C-DAC 2009-10 8

Propositional Logic
with propositions or declarative sentences. Declarative sentence is a grammatically correct English sentence Deals
It is raining You are beautiful

Which can be put in place of in the sentence Is it true that ? so that result is a grammatically correct question
C-DAC 2009-10 9

Declarative sentence
Called as propositions in PL True or False Also called atoms Which of the following are declarative sentences?
Is it hot today? Dont sleep! It is true that some of you are not listening. Do you need a break? No break is needed.
C-DAC 2009-10 10

Syntax of PL
Propositional symbols
The basic unit Also called atoms We will represent using lower-case letters or words

Truth constants
Special constant symbols T (for True) and F (for False)
C-DAC 2009-10 11

Syntax of PL
Logical connectives
1. 2. 3.

Conjunction (and): Disjunction (or): Negation (not): Implication (if, then): -> Equivalence (If and only if): <-> Usually with precedence
; , ; ->, <->

C-DAC 2009-10 12

Topic #1.0 Propositional Logic: Operators

Operators / Connectives
An operator or connective combines one or more operand expressions into a larger expression. (E.g., + in numeric exprs.) Unary operators take 1 operand (e.g., 3); binary operators take 2 operands (eg 3 4). Propositional or Boolean operators operate on propositions or truth values instead of C-DAC 2009-10 on numbers. 13

Some Popular Boolean Operators


Formal Name Negation operator Conjunction operator Disjunction operator Implication operator Biconditional operator
C-DAC 2009-10 14

Nickname Arity NOT AND OR IMPLIES IFF Unary Binary Binary Binary Binary Binary

Symbol

ExclusiveExclusive-OR operator XOR

Topic #1.0 Propositional Logic: Operators

The Negation Operator


The unary negation operator (NOT) transforms a proposition into its logical negation. E.g. If p = I have brown hair. then p = I do not have brown p p hair. T F Truth table for NOT: F T
C-DAC 2009-10 15

Topic #1.0 Propositional Logic: Operators

The Conjunction Operator


The binary conjunction operator (AND) combines two propositions to form their logical conjunction. E.g. If p=I will have salad for lunch. and q=I will have steak for dinner., then pq=I will have salad for lunch and I will have steak for dinner.
C-DAC 2009-10 16

Topic #1.0 Propositional Logic: Operators

Conjunction Truth Table


p Note that a F conjunction p1 p2 pn F of n propositions T will have 2n rows T q F T F T pq F F F T

in its truth table. Also: and operations together are sufficient to express any Boolean truth C-DAC 2009-10 table!
17

Topic #1.0 Propositional Logic: Operators

The Disjunction Operator


The binary disjunction operator (OR) combines two propositions to form their logical disjunction. p=My car has a bad engine. q=My car has a bad carburetor. pq=My car has a bad engine, or my car has a bad carburetor.
C-DAC 2009-10 18

Topic #1.0 Propositional Logic: Operators

Disjunction Truth Table


p q pq Note that pq means F F F that p is true, or q is true, or both are true! F T T T F T So, this operation is also called inclusive or, T T T because it includes the possibility that both p and q are true. and together are also universal.
C-DAC 2009-10 19

Topic #1.0 Propositional Logic: Operators

Nested Propositional Expressions


Use parentheses to group sub-expressions: I just saw my old friend, and either hes grown or Ive shrunk. = f (g s) (f g) s would mean something different f g s would be ambiguous By convention, takes precedence over both and . s f means (s) f , not (s f) C-DAC 2009-10
20

Topic #1.0 Propositional Logic: Operators

A Simple Exercise
Let p=It rained last night, q=The sprinklers came on last night, r=The lawn was wet this morning. Translate each of the following into English: p r p rpq
C-DAC 2009-10 21

Topic #1.0 Propositional Logic: Operators

The Exclusive Or Operator


The binary exclusive-or operator (XOR) combines two propositions to form their logical exclusive or (exjunction?). p = I will earn an A in this course, q = I will drop this course, p q = I will either earn an A for this course, or I will drop it (but not both!)
C-DAC 2009-10 22

Topic #1.0 Propositional Logic: Operators

Exclusive-Or Truth Table


p q pq Note that pq means F F F that p is true, or q is true, but not both! F T T T F T This operation is T T F called exclusive or, because it excludes the possibility that both p and q are true. and together are not universal.
C-DAC 2009-10 23

Topic #1.0 Propositional Logic: Operators

The Implication Operator


The implication p q states that p implies q. I.e., If p is true, then q is true; but if p is not true, then q could be either true or false. E.g., let p = You study hard. q = You will get a good grade. p q = If you study hard, then you will get a good grade. (else, it could go either way)
C-DAC 2009-10 24

Topic #1.0 Propositional Logic: Operators

Implication Truth Table


p q is false only when p is true but q is not true. p q does not say that p causes q! p q does not require that p or q are ever true! E.g. (1=0) pigs can fly is TRUE!
C-DAC 2009-10 25

p F F T T

q pq F T T T F F T T

Topic #1.0 Propositional Logic: Operators

Examples of Implications
If this lecture ends, then the sun will rise tomorrow. True or False? If Tuesday is a day of the week, then I am a penguin. True or False? If 1+1=6, then Bush is president. True or False? If the moon is made of green cheese, then I am richer than Bill Gates. True C-DAC 2009-10 26 or False?

Topic #1.0 Propositional Logic: Operators

English Phrases Meaning pq


p implies q if p, then q if p, q when p, q whenever p, q q if p q when p q whenever p
C-DAC 2009-10 27

p p q p q q

only if q is sufficient for q is necessary for follows from p is implied by p

We will see some equivalent logic expressions later.

Topic #1.0 Propositional Logic: Operators

The biconditional operator


The biconditional p q states that p is true if and only if (IFF) q is true. p = X wins the 2009 election. q = X will be president for all of 2010. p q = If, and only if, X wins the 2009 election, X will be president for all of 2010.

C-DAC 2009-10 28

Topic #1.0 Propositional Logic: Operators

Biconditional Truth Table


p q means that p and q

p have the same truth value. F Note this truth table is the F exact opposite of s! T p q means (p q) T

p q does not imply p and q are true, or cause each other.


C-DAC 2009-10

q p q F T T F F F T T

29

Topic #1.0 Propositional Logic: Operators

Boolean Operations Summary


truth tables of 1 unary operator and 5 binary operators.

p F F T T

q F T F T

p pq pq pq pq pq T F F F T T T F T T T F F F T T F F F T T F T T
30

C-DAC 2009-10

Express with symbolic logic


The following facts are known about a robbery: 1. If A is guilty and B is innocent, then C is guilty. 2. C never works alone. 3. A never works with C. 4. No one other than A, B, or C was involved, and at least one of them is guilty.

C-DAC 2009-10 31

p represents the statement A is guilty, q the statement B is guilty and r the statement C is guilty. 1.If A is guilty and B is innocent, then C is guilty. p q r 2.C never works alone. rpq 3. A never works with C. p r 4. No one other than A, B, or C was involved, and at least one of them is guilty. pqr
C-DAC 2009-10

Topic #1.1 Propositional Logic: Equivalences

Propositional Equivalence
Two syntactically (i.e., textually) different compound propositions may be the semantically identical (i.e., have the same meaning). We call them equivalent. Learn: Various equivalence rules or laws. How to prove equivalences using symbolic derivations.
C-DAC 2009-10 33

Topic #1.1 Propositional Logic: Equivalences

Tautologies and Contradictions


A tautology is a compound proposition that is true no matter what the truth values of its atomic propositions are! Ex. p p [What is its truth table?] A contradiction is a compound proposition that is false no matter what! Ex. p p [Truth table?] Other compound props. are contingencies.
C-DAC 2009-10 34

Topic #1.1 Propositional Logic: Equivalences

Logical Equivalence
Compound proposition p is logically equivalent to compound proposition q, written pq, IFF the compound proposition pq is a tautology. Compound propositions p and q are logically equivalent to each other IFF p and q contain the same truth values as each other in all rows of their truth C-DAC 2009-10 tables.
35

Topic #1.1 Propositional Logic: Equivalences

Proving Equivalence via Truth Tables


Ex. Prove that pq (p q).
p F F T T q F T F T p q F T T T p T T F F q p q (p q) T T F F F T T F T F F T

C-DAC 2009-10 36

Find whether the statements are Equivalent via Truth Tables (1) (pq)r p(qr)

(2) pq (pq) (qp)


(3) (pq) p q

C-DAC 2009-10

Translate into propositional logic (abbreviating `it has rained' as r, `it's been cold' as c and `the plant is dead' as d): (a) If it has rained and it's been cold then the plant is dead (b) If it has rained then either it hasn't been cold or the plant is dead. Use truth tables to determine whether one of these is logically equivalent to the other.

C-DAC 2009-10

(a) If it has rained and it's been cold then the plant is dead. (r c) d (b) If it has rained then either it hasn't been cold or the plant is dead. r ( c d)

C-DAC 2009-10

Topic #1.1 Propositional Logic: Equivalences

Equivalence Laws
These are similar to the arithmetic identities you may have learned in algebra, but for propositional equivalences instead. They provide a pattern or template that can be used to match all or part of a much more complicated proposition and to find an equivalence for it.
C-DAC 2009-10 40

Topic #1.1 Propositional Logic: Equivalences

Equivalence Laws - Examples


Identity: pT p pF p Domination: pT T pF F Idempotent: pp p pp p Double negation: p p Commutative: pq qp pq qp Associative: (pq)r p(qr) (pq)r p(qr)
C-DAC 2009-10 41

Topic #1.1 Propositional Logic: Equivalences

More Equivalence Laws


Distributive: p(qr) (pq)(pr) p(qr) (pq)(pr) De Morgans: (pq) p q (pq) p q Trivial tautology/contradiction: p p T p p F

C-DAC 2009-10 42

Topic #1.1 Propositional Logic: Equivalences

Defining Operators via Equivalences


Using equivalences, we can define operators in terms of other operators. Exclusive or: pq (pq)(pq) pq (pq)(qp) Implies: pq p q Biconditional: pq (pq) (qp) pq (pq)
C-DAC 2009-10 43

Proof of consistency
A statement, or set of statements is logically consistent when it involves no

logical contradiction
A logical contradiction is the conjunction of a statement S and it's denial not-S.

C-DAC 2009-10 44

Resolution Principle

(pq)(pr)](pr)
If we have two true disjunctions that have mutually exclusive propositions, then we can conclude that the disjunction of the two non-mutually exclusive propositions is true.

C-DAC 2009-10 45

Using the resolution rule (an example)


1. p r 2. r q
p Anna is skiing q r Bart is playing hockey it is snowing

pq p r q r

Resolution rule

1. Anna is skiing or it is not snowing. 2. It is snowing or Bart is playing hockey. Consequently Anna is skiing or Bart is playing hockey
C-DAC 2009-10 46

Use equivalences to prove that (r s) s r. (r s) (De Morgan) r s (Commutativity) s r (2x Double Negation) sr

C-DAC 2009-10

Topic #1.1 Propositional Logic: Equivalences

An Example Problem
Check using a symbolic derivation whether (p q) (p r) p q r.
(p q) (p r) [Expand definition of ] (p q) (p r) [Defn. of ] (p q) ((p r) (p r)) [DeMorgans Law] (p q) ((p r) (p r)) [associative law] cont.
C-DAC 2009-10 48

Topic #1.1 Propositional Logic: Equivalences

Example Continued...
(p q) ((p r) (p r)) [ commutes] (q p) ((p r) (p r)) [ associative] q (p ((p r) (p r))) [distrib. over ] q (((p (p r)) (p (p r))) [assoc.] q (((p p) r) (p (p r))) [trivail taut.] q ((T r) (p (p r))) [domination] q (T (p (p r))) [identity] q (p (p r)) cont.
C-DAC 2009-10 49

Topic #1.1 Propositional Logic: Equivalences

Example Continued
q (p (p r)) [DeMorgans] q (p (p r)) [Assoc.] q ((p p) r) [Idempotent] q (p r) [Assoc.] (q p) r [Commut.] p q r
C-DAC 2009-10 50

Topic #1.0 Propositional Logic: Operators

Converse, Inverse, Contrapositive


Some terminology, for an implication p q: Its converse is: q p. Its inverse is: p q. Its contrapositive: q p.

C-DAC 2009-10 51

Topic #1.0 Propositional Logic: Operators

Proving the equivalence of p q and its contrapositive


Using truth tables:

p F F T T

q F T F T

q T F T F

p T T F F

pq q p T T T T F F T T
52

C-DAC 2009-10

Logical Consequence
Given a set of formulae, does a given formula logically follows from the set? A formula g is said to be logical consequence (LC) of a set of formulae f1, f2, .., fn if
Whenever all fi evaluates to T under any interpretation, g also evaluates to T under the same interpretation, OR The formula f1 f2 .. fn -> g is valid

Logical consequence
Premises (1)It rains (2)If it rains the roads will be wet. Prove that the roads will be wet is the consequence of the given premises(1) and (2).

Class room exercise

Das könnte Ihnen auch gefallen