Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Programming and Logic

Swift Programming and Logic

Yasuhiro Inami

September 20, 2019
Tweet

More Decks by Yasuhiro Inami

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    ! NSSpain is Awesome

    View full-size slide

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

    View full-size slide

  5. Q. Is this always true?

    View full-size slide

  6. I am Gorilla
    Gorilla is Handsome
    Therefore, 

    I am Handsome

    View full-size slide

  7. What is True?
    How should we define 

    “correctness” more precisely?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. 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

    View full-size slide

  14. Example of Proof Tree

    View full-size slide

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

    View full-size slide

  16. 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

    View full-size slide

  17. 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)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. 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)

    View full-size slide

  21. (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

    View full-size slide

  22. 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 ⋯

    View full-size slide

  23. Building
    Formal System

    View full-size slide

  24. 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ʣ

    View full-size slide

  25. 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)

    View full-size slide

  26. 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]

    View full-size slide

  27. 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

    View full-size slide

  28. Formulas look like

    Swift Programming

    View full-size slide

  29. 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

    View full-size slide

  30. 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]

    View full-size slide

  31. /// `B ===> A → B`
    /// - Note: More formally, A ⊢ B ===> ⊢ A → B.
    func implyIntro(_ b: B) -> ((A) -> B) {
    { _ in b }
    }
    /// `A, A → B ===> B` (Modus ponens)
    func implyElim(_ f: (A) -> B, _ a: A) -> B {
    f(a)
    }
    A → B A
    B
    B
    A → B

    [A]

    View full-size slide

  32. /// `A, B ===> A ∧ B`
    func andIntro(_ a: A, _ b: B) -> (A, B) {
    (a, b)
    }
    /// `A ∧ B ===> A`
    func andElim1(_ ab: (A, B)) -> A {
    ab.0
    }
    /// `A ∧ B ===> B`
    func andElim2(_ ab: (A, B)) -> B {
    ab.1
    }
    A B
    A ∧ B
    A ∧ B
    A
    A ∧ B
    B

    View full-size slide

  33. /// `A ===> A ∨ B`
    func orIntro1(_ a: A) -> Either {
    .left(a)
    }
    /// `B ===> A ∨ B`
    func orIntro2(_ b: B) -> Either {
    .right(b)
    }
    /// `A ∨ B, A → C, B → C ===> C`
    func orElim(
    _ e: Either, _ ac: (A) -> C, _ bc: (B) -> C
    ) -> C {
    switch e {
    case let .left(a):
    return ac(a)
    case let .right(b):
    return bc(b)
    }
    }
    A
    A ∨ B
    B
    A ∨ B
    A ∨ B C C
    C

    [A]

    [B]

    View full-size slide

  34. struct Not {
    let f: (A) -> Never
    }
    /// `A ⊢ ⊥ ===> ⊢ ¬A`
    func notIntro(_ x: Never) -> Not {
    Not { _ in x }
    }
    /// `A, ¬A ===> ⊥` (NOTE: not using `fatalError`)
    func notElim(_ a: A, _ notA: Not) -> Never {
    notA.f(a)
    }

    ¬A
    A ¬A


    [A]

    View full-size slide

  35. /// `⊥ ===> A`
    func absurd(_ x: Never) -> A {
    // Do nothing, but it compiles.
    }

    A

    View full-size slide

  36. 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)

    View full-size slide

  37. (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)

    View full-size slide

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

    View full-size slide

  39. /// `K = A → (B → A)`
    func k(_ a: A) -> (B) -> A {
    { _ in a }
    }
    /// `S = (A → (B → C)) → ((A → B) → (A → C))`
    func s(_ a: @escaping (C) -> (A) -> B)
    -> (_ b: @escaping (C) -> A)
    -> (C) -> B
    {
    { b in { c in a(c)(b(c)) } }
    }

    View full-size slide

  40. /// `I = SKK: A → A`
    func i(_ a: A) -> A {
    let k_: (A) -> (A) -> A = k
    let skk: (A) -> A = s(k)(k_)
    return skk(a)
    }
    i("hello") // "hello"
    i(123) // 123

    View full-size slide

  41. Logic

    Swift Programming

    View full-size slide

  42. ((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

    View full-size slide

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

    View full-size slide

  44. /// `A ∨ ¬A` (Law of excluded middle)
    func excludedMiddle() -> Either> {
    fatalError("Can't impl in Swift”)
    }
    ANY LANGUAGES!
    (No Premises)
    A ∨ ¬A

    View full-size slide

  45. /// `((A → B) → A) → A`
    func peirceLaw() -> (((A) -> B) -> A) -> A {
    { (aba: ((A) -> B) -> A) in
    let ab: (A) -> B = {
    fatalError("Can't impl ")
    }()
    let a = aba(ab)
    return a
    }
    }

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  49. Classical Logic
    ↓

    Constructive Logic

    (Intuitionistic Logic)

    View full-size slide

  50. Intuitionistic Logic

    Swift Programming

    View full-size slide

  51. Natural Deduction in
    Intuitionistic Logic

    (Simply) Typed 

    Lambda Calculus
    Curry-Howard
    Correspondence

    View full-size slide

  52. 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

    View full-size slide

  53. Intuitionistic Logic

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

    View full-size slide

  54. Possible World in Intuitionistic Logic

    A 0
    B 0
    A 1
    B 0
    A 1
    B 1

    View full-size slide

  55. 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

    View full-size slide

  56. 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

    View full-size slide

  57. 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

    View full-size slide

  58. 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

    View full-size slide

  59. Inheritance of truth value
    "͕ಘΒΕΔͱɺ

    #΋ࣗಈతʹಘΒΕΔ



    A 0
    B 0
    A 1
    B 0
    A 1
    B 1

    A 0
    B 1

    View full-size slide

  60. 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
    "͕ಘΒΕΔͱɺ

    #΋ࣗಈతʹಘΒΕΔ

    View full-size slide

  61. 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)

    View full-size slide

  62. Glivenko’s Theorem: Classical to Intuitionistic Embedding
    Intuitionistic
    Classical
    A ∨ ¬A ¬¬(A ∨ ¬A)
    ¬¬
    Double

    Negation

    Translation
    Law of

    Excluded Middle
    Double-negated

    Excluded Middle

    View full-size slide

  63. ((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

    View full-size slide

  64. ¬¬(((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!

    View full-size slide

  65. ¬¬((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

    View full-size slide

  66. /// `¬¬(A ∨ ¬A)` (Law of excluded middle in Intuitionistic Logic)
    func excludedMiddle_IL() -> Not>>> {
    Not>>> { notEither in
    let notA: Not = Not { a in notEither.f(.left(a)) }
    let either: Either> = .right(notA)
    return notEither.f(either)
    }
    }

    View full-size slide

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

    View full-size slide

  68. /// `¬¬¬(A ∧ B) ===> ¬¬(¬A ∨ ¬B)` (De-Morgan's Law in IL)
    func deMorgan4_IL(_ notNotNotAB: Not>>)
    -> Not, Not>>>
    {
    Not, Not>>> { (notEither: Not, Not>>) in
    let notNotAB = Not> { notAB in
    let notB = Not { b in
    let notA = Not { a in
    notAB.f((a, b))
    }
    let either: Either, Not> = .left(notA)
    return notEither.f(either)
    }
    let either: Either, Not> = .right(notB)
    return notEither.f(either)
    }
    return notNotNotAB.f(notNotAB)
    }
    }

    View full-size slide

  69. /// `¬¬((A → B) → A) → ¬¬A` (Peirce's Law in IL)
    func peirceLaw_IL() -> (Not B) -> A>>) -> Not> {
    { notNotF in
    Not> { notA in
    let notABA = Not<((A) -> B) -> A> { aba in
    let ab: (A) -> B = { a in absurd(notElim(a, notA)) }
    let a = aba(ab)
    return notA.f(a)
    }
    return notNotF.f(notABA)
    }
    }
    }

    View full-size slide

  70. 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

    View full-size slide

  71. /// 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)
    }
    }

    View full-size slide

  72. /// - 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)
    }
    }

    View full-size slide

  73. 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)

    View full-size slide

  74. Sample Code

    http://github.com/inamiy/SwiftAndLogic

    View full-size slide

  75. Gracias!
    Yasuhiro Inami
    @inamiy

    View full-size slide