Propositional logic for Beginners

1,512 views
1,304 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,512
On SlideShare
0
From Embeds
0
Number of Embeds
140
Actions
Shares
0
Downloads
38
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Propositional logic for Beginners

  1. 1. Propositional Logic for Programmers <ul><li>(or how I learnt to stop over-expressing and love the precedance) </li></ul>
  2. 2. Propositions <ul><li>Functions/Statements which can be resolved to True or False. </li></ul><ul><li>P – Do you like Sausage Butties? </li></ul><ul><li>Q – Do you like Bacon Butties[1]? </li></ul><ul><li>[1] Technically Q is a tautology. </li></ul>
  3. 3. <ul><li>¬ , ∧ ∧ ∨ </li></ul>¬ (NOT) ∧  (AND) ∨  (OR) P Q P ∧ Q TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE P Q P ∨ Q TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE P ¬ P TRUE FALSE FALSE TRUE
  4. 4. <ul><li>Truth Tables - The Unit Tests of Logic </li></ul>For a given proposition - express results for all combinations of inputs. P Q ¬ P ∨ ( P ∧ Q ) TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE
  5. 5. <ul><li>Tautologies </li></ul><ul><li>Always true for any given input. </li></ul><ul><li>Can usually be reduced to P v ¬P </li></ul><ul><li>Contradiction - opposite of tautology. </li></ul><ul><li>If not sure - check with a truth table. </li></ul>
  6. 6. P ∧ ( ¬ Q ∨ P)
  7. 7. <ul><li>Equivalence and Reduction </li></ul>Two propositions are equivalent if their truth tables are the same. P Q P ∧ ( ¬ Q ∨ P) TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE
  8. 8. <ul><li>De Morgan's Law </li></ul>¬ (P ∧ Q) === ¬ P ∨ ¬ Q ¬ (P ∨ Q) === ¬ P ∧ ¬ Q
  9. 9. <ul><li>Operational Cost </li></ul>Some operations *unavoidably* cost more than others to execute. Aim: Juggle evaluation order so lower cost operations are evaluated first and if possible reduce the number of calls to more expensive operations.
  10. 10. <ul><li>Eager and Lazy Operators </li></ul>Eager - Will evaluate both sides of expression. Lazy (Short Circuit) - Will only evaluate RHS of expression if will effect return. (Most developers use lazy without realising the full implication) Operator C-Standard Lazy C-Standard Eager ¬ (NOT) ! ! ∧ (AND) && & ∨ (OR) || |
  11. 11. <ul><li>Lazy & Side Effects </li></ul>class A {     int _a;     public awesome() {         _a++;         return true;      }     public freakin() {         return true;      } } a.awesome() && a.freakin() > true (A._a == 1) a.freakin() && a.awesome() > true (A._a == 0) Make sure you understand the side effects of methods before refactoring expressions.
  12. 12. <ul><li>Why we love && </li></ul><ul><li>Fails where the first proposition is false. </li></ul><ul><li>If we can organise with cheap, failing, first proposition - reduce the number of times expensive functions are called. </li></ul>
  13. 13. <ul><li>Why we love || </li></ul>Succeeds where the first proposition is true. Therefore - if we can express a statement with a cheap successful first proposition, then we limit the number of calls to second proposition.
  14. 14. Example <ul><li>var list = PopulateArray(); <-- populate a sparse 2d array. </li></ul><ul><li>foreach(var item in list) { </li></ul><ul><li>if ( ! (item == null || item.Contains(x))) { </li></ul><ul><li>    return &quot;awesome&quot;; </li></ul><ul><li>} </li></ul><ul><li>-- </li></ul><ul><li>foreach(var item in list) { </li></ul><ul><li>if ( item != null && ! item.Contains(x)) { </li></ul><ul><li>    return &quot;awesome&quot;; </li></ul><ul><li>} </li></ul>
  15. 15. <ul><li>Summary </li></ul><ul><li>The Essence of Discrete Mathematics, Neville Dean, ISBN: 0-13-345943-8 </li></ul><ul><li>Kian Ryan </li></ul><ul><li>http://www.kianryan.co.uk/ </li></ul>

×