0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Propositional logic for Beginners

1,070

Published on

Published in: Technology, News & Politics
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,070
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
29
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Propositional Logic for Programmers <ul><li>(or how I learnt to stop over-expressing and love the precedance) </li></ul>
• 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. <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. <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. <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. P ∧ ( ￢ Q ∨ P)
• 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. <ul><li>De Morgan&apos;s Law </li></ul>￢ (P ∧ Q) === ￢ P ∨ ￢ Q ￢ (P ∨ Q) === ￢ P ∧ ￢ Q
• 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. <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) &amp;&amp; &amp; ∨ (OR) || |
• 11. <ul><li>Lazy &amp; Side Effects </li></ul>class A {     int _a;     public awesome() {         _a++;         return true;      }     public freakin() {         return true;      } } a.awesome() &amp;&amp; a.freakin() &gt; true (A._a == 1) a.freakin() &amp;&amp; a.awesome() &gt; true (A._a == 0) Make sure you understand the side effects of methods before refactoring expressions.
• 12. <ul><li>Why we love &amp;&amp; </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. <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. Example <ul><li>var list = PopulateArray(); &lt;-- 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 &amp;quot;awesome&amp;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 &amp;&amp; ! item.Contains(x)) { </li></ul><ul><li>    return &amp;quot;awesome&amp;quot;; </li></ul><ul><li>} </li></ul>
• 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>