Slide 1

Slide 1 text

Swift Programming ∧ Logic 2019/09/20 NSSpain 2019 Yasuhiro Inami / @inamiy

Slide 2

Slide 2 text

Logic A method of making a conclusion from given premises using the concept of Truth

Slide 3

Slide 3 text

! NSSpain is iOS Conference iOS Conference is Awesome Therefore, 
 ! NSSpain is Awesome

Slide 4

Slide 4 text

If A then B If B then C Therefore, if A then C

Slide 5

Slide 5 text

Q. Is this always true?

Slide 6

Slide 6 text

I am Gorilla Gorilla is Handsome Therefore, 
 I am Handsome

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

What is True? How should we define 
 “correctness” more precisely?

Slide 9

Slide 9 text

Separate the definition of “correctness” 1. Form (Syntax) 2. Meaning (Semantics)

Slide 10

Slide 10 text

Syntax Using symbolic formulas and inference rules to systematically deduce a new formula

Slide 11

Slide 11 text

Patterns of Formula If A then B A and B A or B not A A → B A ∧ B A ∨ B ¬A

Slide 12

Slide 12 text

Symbols in Formula Operators Variables Constants ∧ , ∨ , ¬, → A, B, C, ⋯ ⊤ , ⊥

Slide 13

Slide 13 text

Examples of Formula A ∧ (B → C) (A ∨ B) ∧ (C → D) (¬A → ⊥ ) ∨ (¬B ∧ ⊤ )

Slide 14

Slide 14 text

Example of Inference Rule: Modus Ponens A → B A B Inference Rules as a part of axiomatic system Premise 1: If A then B Premise 2: A Conclusion: B

Slide 15

Slide 15 text

Example of Proof Tree

Slide 16

Slide 16 text

Semantics Interpreting the meaning of formula by assigning truth value (true or false)

Slide 17

Slide 17 text

Truth Table (Interpretation of the formula) A B ¬A A∧B A∨B A→B 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 1 0 0 1 0 0 1 Valuation A → B A ∧ B A ∨ B ¬A

Slide 18

Slide 18 text

Example of Truth Table A B ¬A ¬A∨B A→B (¬A∨B) → (A→B) (A→B) → (¬A∨B) 1 1 0 1 1 1 1 1 0 0 0 0 1 1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 Some formulas are always 1 (true)

Slide 19

Slide 19 text

(A → B) ↔ (¬A ∨ B) Tautology: Formulas that are always true in every possible interpretation P ↔ Q := (P → Q) ∧ (Q → P)

Slide 20

Slide 20 text

Examples of Tautology A → A Law of Identity A ∨ ¬A Law of Excluded Middle

Slide 21

Slide 21 text

Examples of Tautology (A → B) → ((B → C) → (A → C)) ((((A → B) → (¬C → ¬D)) → C) → E) → ((E → A) → (D → A)) ¬(A ∧ ¬A) (A ∧ ¬A) → B ((A → B) → A) → A ¬¬A → A (A → B) ↔ (¬B → ¬A) ¬(A ∧ B) ↔ (¬A ∨ ¬B) ¬(A ∨ B) ↔ (¬A ∧ ¬B)

Slide 22

Slide 22 text

(A → B) → ((B → C) → (A → C)) ((((A → B) → (¬C → ¬D)) → C) → E) → ((E → A) → (D → A)) ¬(A ∧ ¬A) (A ∧ ¬A) → B ((A → B) → A) → A ¬¬A → A (A → B) ↔ (¬B → ¬A) ¬(A ∧ B) ↔ (¬A ∨ ¬B) ¬(A ∨ B) ↔ (¬A ∧ ¬B) Examples of Tautology Law of 
 Contradiction Principle of
 Explosion Double Negation
 Elimination Peirce’s Law Law of
 Contraposition De Morgan’s Laws Transitive Law Meredith’s Axiom

Slide 23

Slide 23 text

Examples of Tautology There are infinite number of tautologies… Can we abstract into a finite subset as a basis? A → ¬¬A A → ¬¬¬¬A A → ¬¬¬¬¬¬A ⋯

Slide 24

Slide 24 text

Building Formal System

Slide 25

Slide 25 text

Formal System Axiom l Base premises (part of tautologies) Inference Rules l Deduces new formula from existing ones Using Axioms and Inference Rules to deduce Theorem (all possible tautologiesʣ

Slide 26

Slide 26 text

Styles of Formal System •Gentzen Style l Many inference rules •Natural Deduction l + No axioms •Sequent Calculus l + 1 axiom (identity) •Hilbert Style •Many axioms + 1 inference rule (modus ponens)

Slide 27

Slide 27 text

Gentzen Natural Deduction A B A ∧ B A ∧ B A A ∧ B B A A ∨ B B A ∨ B A ∨ B C C C A → B A B B A → B ⊥ ¬A A ¬A ⊥ ⊥ A ⋮ [A] ¬¬A A ⋮ [A] ⋮ [B] ⋮ [A]

Slide 28

Slide 28 text

Hilbert Style A → B A B B → (A → B) (A → (B → C)) → ((A → B) → (A → C)) ¬¬A → A A → A ∨ B ⊥ → A B → A ∨ B (A → C) → ((B → C) → ((A ∨ B) → C)) A ∧ B → A A ∧ B → B A → (B → (A ∧ B)) (A → ¬A) → ¬A ¬A → (A → B) Modus ponens is the only available
 inference rule

Slide 29

Slide 29 text

Formulas look like
 Swift Programming

Slide 30

Slide 30 text

Logic 㱻 Swift Programming A → B A ∧ B A ∨ B ¬A := A → ⊥ 㱻 Function Tuple Either ⊥ Either Never typealias Not = (A) -> Never (A, B) (A) -> B Bottom

Slide 31

Slide 31 text

Gentzen Natural Deduction A B A ∧ B A ∧ B A A ∧ B B A A ∨ B B A ∨ B A ∨ B C C C A → B A B B A → B ⊥ ¬A A ¬A ⊥ ⊥ A ⋮ [A] ¬¬A A ⋮ [A] ⋮ [B] ⋮ [A]

Slide 32

Slide 32 text

Slide 36

Slide 36 text

Slide 37

Slide 37 text

Hilbert Style B → (A → B) (A → (B → C)) → ((A → B) → (A → C)) A → A ∨ B B → A ∨ B (A → C) → ((B → C) → ((A ∨ B) → C)) A ∧ B → A A ∧ B → B A → (B → (A ∧ B)) A → B A B Modus ponens is the only available
 inference rule ¬¬A → A ⊥ → A (A → ¬A) → ¬A ¬A → (A → B)

Slide 38

Slide 38 text

(A → C) → ((B → C) → ((A ∨ B) → C)) A → A ∨ B B → A ∨ B A ∧ B → A A ∧ B → B A → (B → (A ∧ B)) ¬¬A → A ⊥ → A (A → ¬A) → ¬A ¬A → (A → B) Hilbert Style A → B A B Modus ponens is the only available
 inference rule (A → (B → C)) → ((A → B) → (A → C)) A ∨ B := ¬A → B := ¬B → A A ∧ B := ¬(A → ¬B) := ¬(B → ¬A) ¬A := A → ⊥ NOTE: ∨, ∧, ¬ can be rewritten using → and ⊥ only B → (A → B)

Slide 39

Slide 39 text

Combinatory Logic Kxy = x Sxyz = xz(yz) I = SKK K : X → Y → X S : (Z → Y → X) → (Z → Y) → Z → X I : X → X

Slide 42

Slide 42 text

Logic ⇕ Swift Programming

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

((A → B) → A) → A A ∨ ¬A ¬¬A → A ¬(A ∧ B) → (¬A ∨ ¬B) (A → B) → (¬A ∨ B) (¬B → ¬A) → (A → B) Law of Excluded Middle Double Negation Elimination (One of)
 Law of Contraposition (One of) De Morgan’s Law Material Implication Peirce’s Law NOTE: Converse
 is tautology Tautologies that can’t be implemented in Swift

Slide 45

Slide 45 text

/// `A ∨ ¬A` (Law of excluded middle) func excludedMiddle() -> Either> { fatalError("Can't impl in Swift”) }

Slide 46

Slide 46 text

Slide 48

Slide 48 text

8IBUXFTBXTPGBSJTʜ Classical Logic Interpreting the meaning of formula by assigning truth value (true or false)

Slide 49

Slide 49 text

“→” in Classical Logic A → B Bool true false true or false (2 values)

Slide 50

Slide 50 text

“→” in Swift Programming Types Int Bool (A) -> B (Int)->Bool New type is constructed

Slide 51

Slide 51 text

Classical Logic ↓
 Constructive Logic
 (Intuitionistic Logic)

Slide 52

Slide 52 text

Intuitionistic Logic ⇕ Swift Programming

Slide 53

Slide 53 text

Natural Deduction in Intuitionistic Logic ⇕ (Simply) Typed 
 Lambda Calculus Curry-Howard Correspondence

Slide 54

Slide 54 text

Curry-Howard Correspondence Intuitionistic Logic Swift Programming Proposition Type Proof Program → Introduction Function / Closure Modus Ponens Function Application Axiom Global Variable Assumption Free Variable Discharged Assumption Bound Variable

Slide 55

Slide 55 text

Intuitionistic Logic
 Interpreting the meaning of formula by assigning truth value for each possible world

Slide 56

Slide 56 text

Possible World in Intuitionistic Logic A 0 B 0 A 1 B 0 A 1 B 1

Slide 57

Slide 57 text

Inheritance of truth value A 0 B 0 A 1 B 0 A 1 B 0 A = 1 will be 
 inherited to future (Never becomes A = 0) New Theorem

Slide 58

Slide 58 text

Inheritance of truth value A 0 ¬A 0 A 0 ¬A 1 A 0 ¬A 1 If ¬A = 1, A = 0 from now & future (Never becomes A = 1) inherit

Slide 59

Slide 59 text

Inheritance of truth value A 0 ¬A 0 A 0 ¬A 1 A 0 ¬A 1 If ¬A = 1, A = 0 from now & future (Never becomes A = 1) JOIFSJU A 0 ¬A 0 A ∨ ¬A

Slide 60

Slide 60 text

Inheritance of truth value A → B 1 A 0 B 0 A → B 1 A 1 B 1 A → B 1 A 1 B 1 inherit If A = 1, B = 1 
 can also be obtained Never becomes
 A = 1, B = 0

Slide 61

Slide 61 text

Inheritance of truth value "͕ಘΒΕΔͱɺ
 #΋ࣗಈతʹಘΒΕΔ A 0 B 0 A 1 B 0 A 1 B 1 A 0 B 1

Slide 62

Slide 62 text

Kripke Semantics • Kripke Frame l Ordered set of possible world 
 e.g. → → • Kripke Model l Assigning inheriting truth value 
 for every possible world on the Frame • Tautology in Intuitionistic Logic
 … For any frame and for any valuation (= for any model), 
 formula is always true "͕ಘΒΕΔͱɺ
 #΋ࣗಈతʹಘΒΕΔ

Slide 63

Slide 63 text

Classical Logic ⊂ Intuitionistic Logic Minimal Intuitionistic Classical A ∨ ¬A ⊥ → A → , ∧ , ∨ , ¬, ⊥ Formulas using A → A ∨ B A ∧ B → A Law of
 Contradiction Law of 
 Excluded Middle ⋮ NOTE: Doesn’t mean Intuitionistic Logic owns Law of Excluded Middle ”directly” (See next slide)

Slide 64

Slide 64 text

Glivenko’s Theorem: Classical to Intuitionistic Embedding Intuitionistic Classical A ∨ ¬A ¬¬(A ∨ ¬A) ¬¬ Double
 Negation
 Translation Law of
 Excluded Middle Double-negated
 Excluded Middle

Slide 65

Slide 65 text

((A → B) → A) → A A ∨ ¬A ¬¬A → A ¬(A ∧ B) → (¬A ∨ ¬B) (A → B) → (¬A ∨ B) (¬B → ¬A) → (A → B) Law of Excluded Middle Double Negation Elimination (One of)
 Law of Contraposition (One of) De Morgan’s Law Material Implication Peirce’s Law NOTE: Converse
 is tautology Tautologies that can’t be implemented in Swift

Slide 66

Slide 66 text

¬¬(((A → B) → A) → A) ¬¬(A ∨ ¬A) ¬¬(¬¬A → A) ¬¬(¬(A ∧ B) → (¬A ∨ ¬B)) ¬¬((A → B) → (¬A ∨ B)) ¬¬((¬B → ¬A) → (A → B)) Tautologies in Intuitionistic Logic Adding double-negation will hold in 
 Intuitionistic Logic!

Slide 67

Slide 67 text

¬¬((A → B) → A) → ¬¬A ¬¬(A ∨ ¬A) ¬¬¬¬A → ¬¬A ¬¬¬(A ∧ B) → ¬¬(¬A ∨ ¬B) ¬¬(A → B) → ¬¬(¬A ∨ B) ¬¬(¬B → ¬A) → ¬¬(A → B) Tautologies in Intuitionistic Logic Using helper rule ¬¬(A → B) ¬¬A → ¬¬B

Slide 68

Slide 68 text

Slide 69

Slide 69 text

/// `¬¬¬¬A ===> ¬¬A` (Double negation elimination in IL) func doubleNegationElim_IL( _ notNotNotNotA: Not>>> ) -> Not> { Not> { notA in notNotNotNotA.f( Not>> { notNotA in notNotA.f(notA) } ) } }

Slide 72

Slide 72 text

Peirce’s Law → Call with Current Continuation ¬¬(A → B) ¬¬A → ¬¬B ((A → ¬¬B) → ¬¬A) → ¬¬A Here, are used to gain ¬¬(A → B) A → ¬¬B ¬¬(((A → B) → A) → A) Peirce’s Law

Slide 73

Slide 73 text

/// Continuation. typealias Cont = (@escaping (A) -> R) -> R /// /// Call with Current Continuation. /// - Peirce (classical): `((A → B) → A) → A` /// - CallCC (intuitionistic): `((A → M) → M) → M` /// /// - Note: `callCC` is like control operators e.g. `goto`, `return`, `throw`. /// func callCC( _ f: @escaping (_ exit: @escaping (A) -> Cont) -> Cont ) -> Cont { { outer in f { a in { _ in outer(a) } }(outer) } }

Slide 74

Slide 74 text

/// - DoubleNegationElim (classical): `((A → ⊥) → ⊥) ===> A` /// - DoubleNegationElim (intuitionistic): `((A → M<⊥>) → M<⊥>) ===> M` func doubleNegationElim_callCC( _ doubleNegation: @escaping ( _ neg: @escaping (A) -> Cont ) -> Cont ) -> Cont { callCC { exit -> Cont in flatMap(doubleNegation(exit), absurd) } }

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Recap •Syntax and Semantics •Tautology as “always true” formula •Formal System: Axioms and Inference Rules •Intuitionistic Logic 㱻 Swift Programming (Curry-Howard) •Writing Law of Excluded Middle in Swift (Glivenko) •Learning Logic will help understand Swift’s type (system)

Slide 77

Slide 77 text

Sample Code
 http://github.com/inamiy/SwiftAndLogic

Slide 78

Slide 78 text

Gracias! Yasuhiro Inami @inamiy