Computing Fundamentals 1 Lecture 2 by ivz21134

VIEWS: 4 PAGES: 74

									Computing Fundamentals 1
        Lecture 2
      Boolean Expressions
• Boolean constants: true and false.
• Boolean variables can only have values true
  or false.
• Boolean operators: ,, ,  , ,, .
• Expressions are of type Boolean.
 Truth table for  and identity (id)
     Unary Ops        id     
Argument

true             true true   false false



false            true false true   false
     Unary Op. Truth Table
• The previous TT provides a complete
  enumeration of the Boolean operations with
  one argument (unary operators). The first and
  last operation results do not depend on the
  argument, they have no names. The identity
  (id) operation returns the argument, and
  negation operation () returns the constant that
  is not the argument. The truth table for
  Boolean operations with two arguments is
  shown in the next slide.
         Truth table for two argument ops
                                                   
Operations
                                                    N                       N
             Arg2       O           I       =   A   A                       O
Arg1
                        R
                                    M
                                            E   N   N
                                                        =                   R
                                    P
                                    L       Q   D   D
                                    I       U
                                    E
                                            A
                                    S
                                            L

t            t      t   t   t   t   t   t   t   t   f   f   f   f   f   f   f   f

t            f      t   t   t   t   f   f   f   f   t   t   t   t   f   f   f   f
f            t      t   t   f   f   t   t   f   f   t   t   f   f   t   t   f   f
f            f      t   f   t   f   t   f   t   f   t   f   t   t   t   f   t   f
             A look at implication
•   While the TTs for  and  are symmetric and fairly intuitive
    the TT for implication requires some discussion.
•       All States            True States




1. T premise implies a T conclusion, therefore T -> T is T;
2. T premise cannot imply a F conclusion, therefore T -> F is
   F
3. and 4. You can conclude anything from a false assumption, so
   F -> anything is T.
Implication with constants T/F and
           variables P/Q
          A look at implication
• Logician claim that the TT for implication is
  consistent.
• P is stronger than Q
• Q is weaker than P
•        All States        True States
   Strong and Weak assertions
• Definition: If P and Q are two assertions, the
  P is stronger than Q if P  Q. If P is stronger
  than Q, then Q is weaker than P.
• Example: i<0 (P) is stronger than i<1(Q)
  because i<0  i<1. Fewer states satisfy
  i<0 than satisfy i<1.
• Stronger means more selective or specific.
• Weaker means more frequent or general.
   Binary Op Truth Table(1)
• The previous TT provide a complete enumeration of the
  Boolean operations with two arguments (binary operators).
  Eight of the infix ops have names.
• b = c “b equals c”
• b  c “b differs c” (AKA exclusive or, XOR)
• b  c “b equivales c” or b and c are equivalent. The Boolean
  expression b  c is evaluate exactly as b=c, except that 
  can be used only when b and c are Boolean expressions (= can
  be used for arithmetic equality e.g. 2 = 1 + 1).
• b  c “the inequivalence of b and c”, again strictly for
  Boolean operands.
   Binary Op Truth Table(2)
• b  c conjunction or AND, &, or &&
• b  c disjunction or OR, ||
• bc implication. “b implies c”, “if b then c”.
  b is called the antecedent
  c is called the consequent
• bc consequence. “b follows from c”
  b is called the consequent
  c is called the antecedent
    Binary Op Truth Table(2)
• Alternative symbols for equivalence:
   “x if and only if y”
   iff
   
   <->
   
• The last symbol is used in the course text. The should not be
  confused with the CafeOBJ equality operation ==.
• CafeOBJ‟s BOOL module uses iff for equivalence.
• CafeOBJ‟s PROPC module uses <-> for equivalence.
• Check the definition in CafeOBJ (note spaces):
• open BOOL
• show op (_ iff _)
      Evaluating Truth Tables
• In addition to defining Boolean operators TT can be
  used to compute the value of a Boolean expression in
  every state. They offer a decision procedure.
• The TT on the following slide gives the value of :
    p  (q  r)
• The precedence of operators are described at:
http://www.cs.cornell.edu/Info/People/gries/Logic/Prec.html
• Note the precedence rules may differ in CafeOBJ.
  This can be check with the show op command.
    Evaluating Truth Tables
p   q   r   r   q    p    (q  r)
                 r
t   t   t   f    f     t
t   t   f   t    t     t
t   f   t   f    f     t
t   f   f   t    f     t
f   t   t   f    f     f
f   t   f   t    t     t
f   t   t   f    f     f
f   f   f   t    f     f
  Truth Table Generators
• b  ( b  c )
• b  ( b  c )
     Truth Table Example
• Is b  ( b  c ) a tautology?
• Draw TT of b  ( b  c )
• Solution
b c b (b  c) b  (b  c)
t t    f   t       t
t f    f   t       t
f t    t   t       t
f f    t    f       f
       Table Example(*)
b  ( b  c )
• Solution
• b c b (bc) b  (c  b)
• t t    f t    f
• t f    f t    f
• f t     t t  t
• f f    t f    f
Equality versus Equivalence
• The Boolean expression a  b is evaluated exactly
  the same as a = b, except that  can only be used
  when b and c are Boolean expressions.
• They each have different precedence:
      = (arithmetic) higher precedence (done before  )
        (Boolean) lower precedence (done last)
• Conjunctional and Associative:
      Conjunctional: b = c < d abbreviation for b = c  c < d
      Associative: a  b  c, (a  b)  c, a  (b  c)
    Satisfiability and validity
• A Boolean expression P is satisfied in a state if its
  value is true in that state; P is satisfiable if there is a
  state in which it satisfied; and P is valid if it satisfied
  in every state. A valid Boolean expression is called a
  tautology.
• If P cannot be satisfied (e.g. AA) then P is
  always false for all values of A. If P is unsatisfable it
  is called a contradiction.
• A proposition that is neither a tautology nor a
  contradiction is called a contingency or satisfiable .
Tautologies, satisfiable, contradiction
      Satisfiable and Valid1
• Recall, State is a list of variables with
  associated values.
• pq is satisfiable because it is satisfied if
  (p, true) is in state S1. It is not valid
  in the state S2: (p,false), (q,false).
•
• pp  p is valid in every state.
                    Duality
• The dual PD of a Boolean expression P is constructed
  from P by interchanging the occurrences of:
• true and false
•  and 
•  and 
•  and 
•  and 
• Duality principle: the Boolean identity remains valid
  when both sides of a Boolean expression are replaced
  by their duals. The dual is not the negation of an
  expression, in the negation each p is replaced by p.
           Examples of Duals.
P                    PD
p  q                 p  q

p  q                 p  q
p  p                p  p


false    true  p   true     false  p


p  q  r          p  q  r
     Meta-theorem Duality

(a) P is valid iff PD is valid



(b) (P  Q) is valid iff PD  QD is valid
       Using duality to generate valid
                expression
P (valid)             P (also valid)


true                  false


p  true              (p  false)


p  p                (p  p)
      Using duality to generate valid
               expression
P  Q (valid)          ( PD  QD ) (also valid)
true     true         false  false

p  q     q      p   p  q      q  p


pq      qp          pq  qp



(p  q)  p  q     (p  q)  p  q
 Modelling English propositions
• A proposition can be interpreted as being either
  true or false. For example:
• “Henry VIII had one son and Cleopatra had two”
• We wish to translate English propositions to
  Boolean expressions because:
  – English is ambiguous, computer logical clarity.
  – We can automate, analyse, reason about, simplify,
    and prove properties of Boolean expressions.
  – Rules of logic provide effective computer based way
    to simulate human reasoning (in English).
          Converting English
• Lets use:- R: its raining, S: I‟m going
  swimming, SC: its raining cats, SD: its
  raining dogs, EH: I‟ll eat my hat.
• Whether or not it is raining, I am going
  swimming: (RR)  S
• If it is raining then I will not go swimming:
  RS
• I will go swimming only if it is not raining:
  SR (see slide 45)
            Logic and reality
• Two ways to say the same thing?
  – To stay dry it is sufficient to have an umbrella.
  – If you have an umbrella, then you will stay
    dry: hu  sd
• Initially, we will avoid complex interpretation
  issues.



                                     Coventry and Garrod1
 Modelling English propositions
• Let Boolean (or propositional) variable p
  denote the proposition:
• p : Henry VIII had one son and Cleopatra had two.
• The proposition p contains two sub-
  propositions:
• x: Henry VIII had one son.
• y: Cleopatra had two (sons).
•x  y
     Translating into a Boolean
            Expression
• 1. Introduce a Boolean variable to denote
  sub-propositions.
• 2. Replace these sub-propositions by their
  corresponding Boolean variables.
• 3. Translate the result of step 2 into a
  Boolean expression, using obvious
  translations of English words into
  operators. See table on next slide.
     Translation of English Words
and, but                  

or                        

not                       

it is not the case that   

if p then q               p  q
              Translation
Boolean variable English Sentence

             x: Henry VIII has one son

             y: Cleopatra had two sons

             z: I‟ll eat my hat

             w: 1 is prime
                           Translation
Proposition                      translation

Henry VIII has one son or I‟ll
eat my hat
                                 x  z

Henry VIII has one son and 1
is not prime
                                 x  w

If 1 is prime and Cleopatra
had two sons,
                                 w  y  z
I‟ll eat my hat
              Translating Or
• We must distinguish between:
• “inclusive or” (or, )
• “exclusive or” (xor, b  c, b  c )
  – sometimes written „||‟ or 
• Would you like coffee or tea?
• Would you like sugar or milk?
• Is it raining or not raining? Yes!
                         Translating




Can you express the above statement using just one logical connective?
This example is from Logic Made Easy by Deborah J. Bennett
                 Implication
• “If a then b” becomes ab
• “if you don‟t eat your spinach I will scold
  you.” becomes:
• es  sy
• In English the following have same
  meaning:
  – “if you don‟t eat your spinach then I will scold
    you.”
  – “eat your spinach or I will scold you.”
                  Implication
•   This gives an equivalence:
•   es  sy
•    es  sy
•   Hidden implication:
• “Every name in the Dublin West Side telephone
  directory is in the Dublin directory”
• “If a name is in the Dublin West Side telephone
  directory, then it is in the Dublin directory”.
                Implication
• In English the antecedent and the
  consequence can be switched, and the
  then dropped:
• If you pay me then I will go.
• I will go if you give me money.
• There is a subtle difference between
• „If‟ meaning „in case‟ or „in the event of‟
• „If‟ meaning „when‟
                     Implication
• There is a subtle difference between
   – „If‟ meaning „in case‟ or „in the event of‟
   – „If‟ meaning „when‟
• “When the sun is shining, I feel happy”
• Also, in logic a consequential relation is not
  necessary.
• If I drink too much, then I get sick.
• If pigs can fly, then 2+2=5.
• Logic usually needs a sensible interpretation to be
  useful.
   Implication And Equivalence
• The English “if” should be sometimes
  regarded as an equivalence.
• “If two sides of a triangle are equal, the
  triangle is isosceles.”
• Which as a definition could be stated:
• t : two sides of the triangle are equal
• is: the triangle is isosceles.
• t  is
                 Necessity
• A necessary condition for the occurrence of a
  specified event is a circumstance in whose
  absence the event cannot occur. Oxygen is
  necessary for combustion or oxygen is
  necessary for human life.
• A necessary implication can be used to
  represent sub-set-of relation (AKA subsumption
  or “is kind of”).
• Student => Person
• Does “B is a kind of A” mean “All Bs are As”?
    All members of B are members of A




All members of a subclass can be inferred to be members of its superclass.
Given an object of type A it cannot be inferred that it is a B.
                Sufficient
• A sufficient condition for the occurrence of
  an event is a circumstance in whose
  presence the event must occur.
• Getting an A is sufficient condition for
  passing the course (event).
    Necessity & Sufficiency(1)
• Two ways to say the same thing?
  – To stay dry it is sufficient to wear a raincoat.
  – If you wear a rain coat, then you will stay dry.
• wr  sd
    Necessity & Sufficiency(2)
• Two ways to say the same thing?
  – To stay dry it is necessary to wear a raincoat.
  – You will stay dry only if you wear a raincoat.
• sd  wr
    Necessity & Sufficiency(3)
• “x is sufficient for y” means
•x y
• “x is necessary for y” means
•y x
• “x is necessary and sufficient for y”
  means
• (x  y)  (y  x)
• Note: care is needed when „only‟ is used.
   Necessity & Sufficiency(3)
• "If X then Y", or "X is enough for Y",
  can also be understood as saying that
  X is a sufficient condition for Y.
         Bob‟s Eyesight(*)
• Bob can use no eyes
• Bob can use one eye
• Bob can use two eyes
• Generalizes to “Bob can use j eyes”
• Bob0  Bob1  Bob2
• Is this always true?
              Examples(*)
• None or both of p and q is true.
• pq  pq
                 Examples(*)
• Exactly one of p and q is true.
• (pq)
• Alternatives
     (pq)
     (pq)
     (p xor q)
              Examples(*)
• Whether or not it is raining, I am going
  swimming.
• (RR)  S
          A logical equivalence
•   Note SR is logically equivalent to S  R
•   S    R    S         SR            SR
•   T    T      F        T            T
•   T    F      F        F            F
•   F    T      T        T            T
•   F    F      T        T            T
                 Examples(*)
•   I will go swimming only if it is not raining.
•   : SR, R is necessary for S
•   Note SR is not logically equivalent to RS
•   S      R     R      SR              RS
•   T      T      F      F                     T
•   T      F      T      T                     T
•   F      T      F      T                     T
•   F      F      T      T                     F
     The English use of Unless1
• „Unless‟ generally means „or‟. „I will go out unless it rains‟
  translates to „I will go out  it will rain‟. (Note the extra
  „will‟.)
• You could also use „¬(it will rain) → I will
  go out‟.
• But you may think that „I will go out unless it rains‟
  implies that if it does rain then I won‟t go out. This is a
  stronger reading of „unless‟. If you take that view, you‟d
  translate it as „I will go out ↔ ¬(it will
  rain)‟. This is a bit like „exclusive or‟.
• Deciding whether to take the strong or weak reading is
  done by individual context, and can be impossible:
  English is not always precise. You must decide what the
  correct logical translation is.
     The English use of Unless
• English is sometimes difficult to translate:
• “I will go out unless it rains” could be
¬(rains) → I will go out
or
¬(rains) ↔ I will go out
or
(rains)  I will go out
       The English use of But1
• „But‟ can mean „and‟.
• „I will go out, but it is raining‟ translates to
• „I will go out  it is raining‟.
               Translating
• If the bill was sent, then you will be paid
  and the bill was sent.
• B = Bill was sent
• P = You will be paid
• (B->P)  B
               Translating
• If Mary is the author of the book, then the
  book is fiction, but the book is non-fiction
  implies that Mary did not write the book.
• M = Mary is Author
• F = Book is Fiction
• (M -> F)  (F-> M)
               Translating
• Exactly one of p and q is true.
• (pq)
• alternatives
• (pq)
• (pq)
• (p xor q)
 Translating from English Example
• Whether or not it is raining, I am going
  swimming.
• Solution: (RR)  S
• If it is raining then I will not go swimming.
• Solution: RS
• I will go swimming only if it is not raining.
• Solution: SR, R is necessary for S
 Translating from English Example
• Premise Indicators
  – For
  – Since
  – Because
  – Assuming that
 Translating from English Example
• Conclusion Indicators
  – Therefore
  – Thus
  – Hence
  – So
     Expressions in CafeOBJ
• b  ( b  c )
• b  ( b  c )
• These convert to in the PROPC module of
  CafeOBJ.
• red not(b) -> (b or c)
• red not(b) <-> (b or c)
• In CafeOBJ PROPC module is very like BOOL,
  but it offers a decision procedure for the
  prepositional calculus (see next lecture).
Prove the following
(p  q)  p  q
Prove the following
(p  q)  p  q
Prove the following
(p  q)  p  q
Prove the following
(p  q)  p  q
Draw a truth table
 b  ( b  c )
b  ( b  c )
Expressions in TT and CafeOBJ




  What results does CafeOBJ give for the following?
  open PROPC
  set trace whole on .
  red not(b)    <-> (b or c) .
  ed not(b)    <-> b or c .
  red not(b) -> (b or c) .
              Substitution
• Substitute: (x + x  2)[x := x  y]
• Solution: (xy)+(xy)2
• Substitution:(x + y  x + x  y  z)[x:=
  d+3]
• Solution: (d+3)+y(d+3)+(d+3)yz
                   Assignment
•   {precondition?} x:=x-1 {x2 + 2  x = 3 }
•   Solution: {(x-1)2 + 2  (x-1) = 3 }
•              {(x2-2x+1) + 2(x-2) = 3 }
•               {2x-3 = 3} giving {x =0}
•   {precondition} x:=x-1 { x = 0 }
•   Solution: { (x -1) = 0 } (x-1)=0 simplifies to x=1.
•   {precondition} y:=x + y { y = x + y }
•   Solution: (x+y) = x + (x+y)      simplifies to x=0.
 Translate to propositional logic
• If there are fewer cars on the road the
  pollution will be acceptable. Either we
  have fewer cars on the road or there
  should be a road pricing, or both. If there
  is road pricing the summer will be
  unbearable hot. The summer is actually
  turning out to be quite cool. The
  conclusion is inescapable: pollution is
  acceptable.

								
To top