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

Swiftプログラミングと論理 〜そして帰ってきた圏論〜 / Swift and Logic, and Category Theory

Yasuhiro Inami
September 06, 2019

Swiftプログラミングと論理 〜そして帰ってきた圏論〜 / Swift and Logic, and Category Theory

Swiftプログラミングと論理 〜そして帰ってきた圏論〜 iOSDC Japan 2019 (Sep 6, 2019)
https://fortee.jp/iosdc-japan-2019/proposal/9529444c-a849-47cd-94a7-bf2b694320aa

Yasuhiro Inami

September 06, 2019
Tweet

More Decks by Yasuhiro Inami

Other Decks in Programming

Transcript

  1. Swiftͱ࿦ཧ
    ʙͦͯ͠ؼ͖ͬͯͨݍ࿦ʙ
    2019/09/06 iOSDC 2019
    Yasuhiro Inami / @inamiy

    View full-size slide

  2. ࿦ཧֶ
    ࢥߟͷಓےΛʮਅཧ 5SVUI
    ʯ
    ͱ͍͏֓೦Λ༻͍ͯઆ໌͢Δֶ໰

    View full-size slide

  3. J04%$͸J04ΧϯϑΝϨϯε
    J04ΧϯϑΝϨϯε͸ͨͷ͠ʔ
    ΑͬͯɺJ04%$͸ͨͷ͠ʔ

    View full-size slide

  4. ΋͠"ͳΒ͹#
    ΋͠#ͳΒ͹$
    Αͬͯɺ΋͠"ͳΒ͹$

    View full-size slide

  5. 2͜Ε͸ৗʹਖ਼͍͠ʁ

    View full-size slide

  6. ൴ࢯ͸ΰϦϥ
    ΰϦϥ͸Πέϝϯ
    Αͬͯɺ൴ࢯ͸Πέϝϯ

    View full-size slide

  7. ʮਖ਼͍͠ʯͱ͸ͳΜͧ΍ʁ
    ԿΛਖ਼͍͠ͱߟ͑ɺࢥߟΛల։͍͚ͯ͠Δ͔ʁ

    View full-size slide

  8. ࿦ཧͷʮਖ਼͠͞ʯΛ෼͚ͯߟ͑Δ
    ࿦ཧͷʮܗʯ͕ਖ਼͍͠
    ࿦ཧͷʮ಺༰ʯ͕ਖ਼͍͠

    View full-size slide

  9. ߏจ࿦(Syntax)
    ه߸తͳ࿦ཧࣜͱਪ࿦نଇΛ࢖ͬͯɺ
    ʢػցతʹʣ৽͍͠࿦ཧࣜΛܭࢉʢূ໌ʣ͢Δ

    View full-size slide

  10. ࿦ཧͷύλʔϯ
    "ͳΒ͹#
    "ͱ#
    "·ͨ͸#
    "Ͱͳ͍
    A → B
    A ∧ B
    A ∨ B
    ¬A

    View full-size slide

  11. ໋୊ͱ࿦ཧࣜ
    ࿦ཧه߸
    ໋୊ม߲
    ໋୊ఆ߲
    ∧ , ∨ , ¬, →
    A, B, C, ⋯
    ⊤ , ⊥

    View full-size slide

  12. ࿦ཧࣜͷྫ
    A ∧ (B → C)
    (A ∨ B) ∧ (C → D)
    (¬A → ⊥ ) ∨ (¬B ∧ ⊤ )

    View full-size slide

  13. ਪ࿦نଇͷྫɿϞʔμεϙωϯε
    A → B A
    B جຊతͳਪ࿦نଇΛ
    ެཧܥͱͯ͠ఆΊΔ
    લఏ̍ɿ"ͳΒ͹#
    લఏ̎ɿ"Ͱ͋Δ
    ݁࿦ɹɿ#Ͱ͋Δ

    View full-size slide

  14. ূ໌ਤͷྫʢਪ࿦نଇͷద༻աఔͷ໦ߏ଄ʣ

    View full-size slide

  15. ҙຯ࿦(Semantics)
    ࿦ཧࣜʹʮਅʯ·ͨ͸ʮِʯͷ
    ਅཧ஋Λ෇༩ͯ͠ղऍ͢Δ

    View full-size slide

  16. ࿦ཧࣜʢه߸ʣˠਅཧ஋ͷׂΓ౰ͯ
    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
    ෇஋
    ਅ ِ

    A → B
    A ∧ B
    A ∨ B
    ¬A
    ֤ม߲ͷਅِΛܾΊΔ
    ʹߦΛબͿ

    View full-size slide

  17. ਅཧ஋දͷྫ
    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
    ৗʹʹͳΔ
    ࿦ཧ͕ࣜଘࡏ

    View full-size slide

  18. (A → B) ↔ (¬A ∨ B)
    ͸ɺ໋୊"ɺ#ʹΑΒͣɺৗʹਅ
    ߃ਅ τʔτϩδʔ

    P ↔ Q := (P → Q) ∧ (Q → P)

    View full-size slide

  19. ߃ਅʢτʔτϩδʔʣͷྫ
    ྗͱ͸ύϫʔ
    ຖ೔͕ΤϒϦσΠ
    A → A
    ಉҰ཯

    View full-size slide

  20. ߃ਅʢτʔτϩδʔʣͷྫ
    ੈͷதʹ͸छྨͷஉ͔͠ډͳ͍
    Զ͔ɺԶҎ֎͔
    A ∨ ¬A
    ഉத཯

    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)

    View full-size slide

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

  23. ߃ਅʢτʔτϩδʔʣͷྫ
    ߃ਅͳ࿦ཧࣜ͸ແݶʹଘࡏ͢Δ

    ˠ༗ݶͷنଇ͔Βಋ͖ग़ͤͳ͍͔ʁ
    A → ¬¬A
    A → ¬¬¬¬A
    A → ¬¬¬¬¬¬A ⋯

    View full-size slide

  24. ߃ਅͳ࿦ཧ͔ࣜΒ
    ܗࣜతମܥΛߟ͑Δ

    View full-size slide

  25. ܗࣜతମܥʢه߸ʴจ๏ʴެཧʴਪ࿦نଇʣ
    ެཧʹલఏͱͯ͠ಋೖ͞ΕΔجຊతͳԾఆ
    ʢҰ෦ͷ߃ਅͳ࿦ཧࣜʣ
    ਪ࿦نଇʹ࿦ཧ͔ࣜΒଞͷ࿦ཧࣜΛಋ͘͜ͱ
    ެཧͱਪ࿦نଇʢܗࣜతମܥʣΛ࢖ͬͯ
    ͢΂ͯͷ߃ਅͳ࿦ཧࣜʢఆཧʣΛಋ͘

    View full-size slide

  26. ܗࣜతମܥͷछྨ
    w ήϯπΣϯྲྀ
    w ࣗવԋ៷ʜ೔ৗͷਪ࿦ʹ͍ۙɺެཧͳ͠ɺਪ࿦نଇ͕ଟ͍
    w γʔΫΤϯτܭࢉʜެཧ͕̍ͭʢಉҰੑʣɺਪ࿦نଇ͕ଟ͍
    w ώϧϕϧτྲྀ
    w ެཧ͕ଟ͍ɺਪ࿦نଇ͕̍ͭʢϞʔμεϙωϯεʣ
    w ܗࣜతମܥ͔Βಋग़͞ΕΔ࿦ཧࣜʢఆཧʣ͸ɺ߃ਅʢ݈શੑʣ

    View full-size slide

  27. ήϯπΣϯྲྀɾࣗવԋ៷
    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

  28. ήϯπΣϯྲྀɾࣗવԋ៷ʢγʔΫΤϯτܭࢉ෩ʣ
    Γ ⊢ A Γ ⊢ B
    Γ ⊢ A ∧ B
    Γ ⊢ A ∧ B
    Γ ⊢ A
    Γ ⊢ A ∧ B
    Γ ⊢ B
    Γ ⊢ A
    Γ ⊢ A ∨ B
    Γ ⊢ B
    Γ ⊢ A ∨ B
    Γ ⊢ A ∨ B Γ, A ⊢ C Γ, B ⊢ C
    Γ ⊢ C
    Γ ⊢ A → B Γ ⊢ A
    Γ ⊢ B
    Γ, A ⊢ B
    Γ ⊢ A → B
    Γ, A ⊢ ⊥
    Γ ⊢ ¬A
    Γ ⊢ A Γ ⊢ ¬A
    Γ ⊢ ⊥
    Γ ⊢ ⊥
    Γ ⊢ A
    Γ ⊢ ¬¬A
    Γ ⊢ A
    A ∈ Γ
    Γ ⊢ A
    ϵ⊢"ʜ࿦ཧࣜͷ༗ݶྻϵ͔Βɺ࿦ཧࣜ"͕ূ໌Ͱ͖Δ

    View full-size slide

  29. ώϧϕϧτྲྀ 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)

    View full-size slide

  30. ͦΕͱͳ͘

    ʮ4XJGUϓϩάϥϛϯάʯ
    ͬΆ͍

    View full-size slide

  31. ࿦ཧ˱4XJGUϓϩάϥϛϯά
    A → B
    A ∧ B
    A ∨ B
    ¬A := A → ⊥
    ରԠ

    ؔ਺ܕ
    ௚ੵܕ
    ௚࿨ܕ

    Either
    Never
    typealias Not
    = (A) -> Never
    (A, B)
    (A) -> B
    ϘτϜܕ
    㲄Λ࢖ͬͯ
    ൱ఆΛఆٛͰ͖Δ

    View full-size slide

  32. ʲ࠶ܝʳήϯπΣϯྲྀɾࣗવԋ៷
    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

  33. /// `B ===> A → B`
    /// - Note: More formally, A ⊢ B ===> ⊢ A → B.
    func implyIntro(_ b: B) -> ((A) -> B) {
    { _ in b }
    }
    /// `A → B, A ===> 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

  34. /// `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

  35. /// `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

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

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

    A

    View full-size slide

  38. ʲ࠶ܝʳώϧϕϧτྲྀ
    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
    ਪ࿦نଇ͸
    ϞʔμεϙωϯεͷΈ
    ¬¬A → A
    ⊥ → A
    (A → ¬A) → ¬A
    ¬A → (A → B)

    View full-size slide

  39. ʲ࠶ܝʳώϧϕϧτྲྀ
    (A → C) → ((B → C) → ((A ∨ B) → C))
    A → B A
    B
    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)
    (A → (B → C)) → ((A → B) → (A → C))
    A ∨ B := ¬A → B := ¬B → A
    A ∧ B := ¬(A → ¬B) := ¬(B → ¬A)
    ¬A := A → ⊥
    /05&ˮ ˭ ™͸ɺˠͱ˵ͷΈΛ࢖ͬͯهड़Ͱ͖Δ
    B → (A → B)

    View full-size slide

  40. ίϯϏωʔλ࿦ཧ
    Kxy = x
    Sxyz = xz(yz)
    I = SKK
    K : X → Y → X
    S : (Z → Y → X) → (Z → Y) → Z → X
    I : X → X
    ஋ͷؔ਺Խ ҰൠԽ

    3FBEFSϞφυͷQVSF

    Ϟʔμεϙωϯε
    ͷҰൠԽ

    3FBEFSϞφυͷBQ

    View full-size slide

  41. /// `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

  42. /// `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

  43. ࿦ཧ

    4XJGUϓϩάϥϛϯά

    View full-size slide

  44. ((A → B) → A) → A
    A ∨ ¬A
    ¬¬A → A
    ¬(A ∧ B) → (¬A ∨ ¬B)
    (A → B) → (¬A ∨ B)
    (¬B → ¬A) → (A → B)
    ഉத཯
    ೋॏ൱ఆআڈ
    ରۮ཯ ൱ఆআڈWFS

    υɾϞϧΨϯͷ๏ଇ ™㱸

    ؚ࣮࣭ҙ
    ύʔεͷ๏ଇ
    /05&
    ʮٯʯ͸߃ਅ
    4XJGUͰ࣮૷Ͱ͖ͳ͍߃ਅͳ࿦ཧࣜ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  47. /// `((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

  48. /// `((A → B) → A) → A`
    func peirceLaw() -> (((A) -> B) -> A) -> A {
    { (aba: ((A) -> B) -> A) in
    // WARNING: This compiles, but using invalid `excludedMiddle()`.
    let either: Either> = excludedMiddle()
    switch either {
    case let .left(a):
    return a
    case let .right(notA):
    return aba { a in absurd(notA.f(a)) }
    }
    }
    }
    ݟ্͔͚͸࣮૷Ͱ͖͍ͯΔ͕ɺ
    ഉத཯Λ࢖͍ͬͯΔͷͰɺ

    View full-size slide

  49. ͜Ε·Ͱͷ࿦ཧ͸ɾɾɾ
    ݹయ࿦ཧ ϒʔϧ࿦ཧ

    ࿦ཧࣜʹʮਅʯ·ͨ͸ʮِʯͷ

    ਅཧ஋Λ෇༩ͯ͠ղऍ͢Δ

    View full-size slide

  50. ݹయ࿦ཧʹ͓͚Δʮˠʯ
    A → B
    Bool
    true
    false
    USVFPSGBMTF
    ௨Γ͔͠ͳ͍

    View full-size slide

  51. 4XJGUϓϩάϥϛϯάʹ͓͚Δʮˠʯ
    Types
    Int
    Bool
    (A) -> B
    (Int)->Bool
    ৽͍͠ܕ͕
    ߏ੒͞ΕΔ

    View full-size slide

  52. ݹయ࿦ཧ ϒʔϧ࿦ཧ

    ˣ

    ߏ੒త࿦ཧ

    ௚؍ओٛ࿦ཧ

    ഉத཯΍ೋॏ൱ఆআڈ͕
    ੒Γཱͨͳ͘ͳΔ

    View full-size slide

  53. ௚؍ओٛ࿦ཧ

    4XJGUϓϩάϥϛϯά

    View full-size slide

  54. ௚؍ओ໋ٛ୊࿦ཧʹ͓͚Δ
    ࣗવԋ៷

    ୯७ܕ෇͖ϥϜμܭࢉ

    ؔ਺ܕϓϩάϥϛϯάͷཧ࿦తج൫

    View full-size slide

  55. ܕͳ͠ϥϜμܭࢉʢུ֓ʣ
    t ::= x
    | λx . t
    | t t
    ʢม਺ʣ
    ʢλந৅ɺؔ਺ఆٛʣ
    ʢؔ਺ద༻ʣ
    λx . x λx . (λy . x) (λx . xx)(λx . xx)
    λ߲ͷྫ
    ϥϜμ߲

    View full-size slide

  56. ܕ෇͖ϥϜμܭࢉʢུ֓ʣ
    T ::= X
    | T → T
    ʢܕม਺ʣ
    ʢؔ਺ܕʣ
    t ::= x
    | λ(x : T) . t
    | t t
    ʢม਺ʣ
    ʢλந৅ʣ
    ʢؔ਺ద༻ʣ
    : T
    ϥϜμ߲
    ܕɹ

    View full-size slide

  57. ܕ෇͖ϥϜμܭࢉʢུ֓ʣ
    A ∈ Γ
    Γ ⊢ A
    Γ, A ⊢ B
    Γ ⊢ A → B
    Γ ⊢ A → B Γ ⊢ A
    Γ ⊢ B
    (x : A) ∈ Γ
    Γ ⊢ x : A
    Γ, x : A ⊢ t : B
    Γ ⊢ λ(x : A) . t : A → B
    Γ ⊢ t12
    : A → B Γ ⊢ t1
    : A
    Γ ⊢ t12
    t1
    : B
    ܕ෇͚نଇͷʮܕͷΈʯʹ஫໨͢Δͱʜ
    ࣗવԋ៷ͱ
    ಉ౳
    λ߲ͷܕ෇͚نଇ
    ม਺ Еந৅
    ؔ਺ద༻

    View full-size slide

  58. ௚؍ओ໋ٛ୊࿦ཧʹ͓͚Δ
    ࣗવԋ៷

    ୯७ܕ෇͖ϥϜμܭࢉ

    ؔ਺ܕϓϩάϥϛϯάͷཧ࿦తج൫

    ΧϦʔɾϋϫʔυ
    ಉܕରԠ

    View full-size slide

  59. ΧϦʔɾϋϫʔυಉܕରԠ
    ௚؍ओ໋ٛ୊࿦ཧʹ͓͚Δ

    ࣗવԋ៷
    ୯७
    ܕ෇͖ϥϜμܭࢉ

    ؔ਺ܕϓϩάϥϛϯά

    ໋୊ɾఆཧ ܕ
    ূ໌ਤ ϥϜμ߲ʢϓϩάϥϜʣ
    ؚҙಋೖ ϥϜμந৅ʢؔ਺ఆٛʣ
    Ϟʔμεϙωϯε ؔ਺ద༻
    ެཧ άϩʔόϧม਺
    Ծఆ ࣗ༝ม਺
    ԾఆΛด͡Δ ଋറม਺
    ଞͷ࿦ཧԋࢉ 㱸 㱹ͳͲ
    ͷ
    ֦ுͱಉܕରԠ΋Մೳ

    View full-size slide

  60. ௚؍ओٛ࿦ཧ

    ࿦ཧࣜʹʮՄೳੈքʯΛߟྀͨ͠
    ʮਅʯʮِʯͷਅཧ஋Λ෇༩ͯ͠ղऍ͢Δ

    View full-size slide

  61. ௚؍ओٛ࿦ཧͱʮՄೳੈքʯʢ஌ࣝঢ়ଶʣ

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

    View full-size slide

  62. ֤ੈքͱਅཧ஋ͷҨ఻ؔ܎

    A 0
    B 0
    A 1
    B 0
    A 1
    B 0
    ͸ະདྷ΁Ҩ఻͢Δ
    ʹ໭Βͳ͍

    ৽͍͠஌ݟ

    View full-size slide

  63. ֤ੈքͱਅཧ஋ͷҨ఻ؔ܎

    A 0
    ¬A 0
    A 0
    ¬A 1
    A 0
    ¬A 1
    ™"ͷ৔߹ɺ
    ݱࡏ͔Βະདྷʹ͔͚ͯ"
    ʢ"ʹͳΔ͜ͱ͸ͳ͍ʣ
    Ҩ఻

    View full-size slide


  64. A 0
    ¬A 0
    A 0
    ¬A 1
    A 0
    ¬A 1
    ™"ͷ৔߹ɺ
    ݱࡏ͔Βະདྷʹ͔͚ͯ"
    ʢ"ʹͳΔ͜ͱ͸ͳ͍ʣ
    Ҩ఻
    ֤ੈքͱਅཧ஋දͷҨ఻ؔ܎
    A ∨ ¬A
    ഉத཯͸߃ਅͰ͸ͳ͍

    View full-size slide

  65. ֤ੈքͱਅཧ஋ͷҨ఻ؔ܎

    A → B 1
    A 0
    B 0
    A → B 1
    A 1
    B 1
    A → B 1
    A 1
    B 1
    Ҩ఻
    "͕ಘΒΕΔͱɺ

    #΋ࣗಈతʹಘΒΕΔ
    " #ʹ
    ͳΔ͜ͱ͸ͳ͍

    View full-size slide

  66. ֤ੈքͱਅཧ஋ͷҨ఻ؔ܎
    "͕ಘΒΕΔͱɺ

    #΋ࣗಈతʹಘΒΕΔ



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

    A 0
    B 1

    View full-size slide

  67. ΫϦϓΩɾϞσϧʢࡶͳղઆʣ
    wΫϦϓΩɾϑϨʔϜʜˠˠͳͲͷੈքͷॱংू߹
    wΫϦϓΩɾϞσϧʜϑϨʔϜͷ֤ੈքͷ্ʹҨ఻͢Δ

    ɹɹɹɹɹɹɹɹɹਅཧ஋ͷׂΓ౰ͯΛߦͬͨ΋ͷ
    w௚؍ओٛ࿦ཧʹ͓͚Δ߃ਅ

    ʜ೚ҙͷϑϨʔϜʴ೚ҙͷਅཧ஋ͷׂΓ౰ͯ

    ɹʹ೚ҙͷϞσϧʹ͓͍ͯਅʹͳΔ࿦ཧࣜͷ͜ͱ
    "͕ಘΒΕΔͱɺ

    #΋ࣗಈతʹಘΒΕΔ

    View full-size slide

  68. ݹయ࿦ཧ˩௚؍ओٛ࿦ཧ
    ࠷খ࿦ཧ
    ௚؍ओٛ࿦ཧ
    ݹయ࿦ཧ
    A ∨ ¬A
    ⊥ → A
    → , ∧ , ∨ , ¬, ⊥
    Λ࢖ͬͨ࿦ཧࣜ
    A → A ∨ B
    A ∧ B → A
    ໃ६཯
    ഉத཯ ⋮
    ʲ஫ҙʳ௚؍ओٛ࿦ཧ͕ഉத཯Λ௚઀ؚΉΘ͚Ͱ͸ͳ͍ʢ࣍ϖʔδࢀরʣ

    View full-size slide

  69. άϦϕϯίͷఆཧɿݹయ࿦ཧˠ௚؍ओٛ࿦ཧ΁ͷຒΊࠐΈ
    ௚؍ओٛ࿦ཧ
    ݹయ࿦ཧ
    A ∨ ¬A ¬¬(A ∨ ¬A)
    ¬¬
    ೋॏ൱ఆಋೖ
    ഉத཯ ೋॏ൱ఆഉத཯

    View full-size slide

  70. ((A → B) → A) → A
    A ∨ ¬A
    ¬¬A → A
    ¬(A ∧ B) → (¬A ∨ ¬B)
    (A → B) → (¬A ∨ B)
    (¬B → ¬A) → (A → B)
    ഉத཯
    ೋॏ൱ఆআڈ
    ରۮ཯ ൱ఆআڈWFS

    υɾϞϧΨϯͷ๏ଇ ™㱸

    ؚ࣮࣭ҙ
    ύʔεͷ๏ଇ
    ʲ࠶ܝʳ௚؍ओٛ࿦ཧͰ੒Γཱͨͳ͍࿦ཧࣜ
    /05&
    ʮٯʯ͸߃ਅ

    View full-size slide

  71. ¬¬(((A → B) → A) → A)
    ¬¬(A ∨ ¬A)
    ¬¬(¬¬A → A)
    ¬¬(¬(A ∧ B) → (¬A ∨ ¬B))
    ¬¬((A → B) → (¬A ∨ B))
    ¬¬((¬B → ¬A) → (A → B))
    ௚؍ओٛ࿦ཧͰ߃ਅͳ࿦ཧࣜ
    ͢΂ͯ
    ௚؍ओٛ࿦ཧ
    Ͱ੒Γཱͭ

    View full-size slide

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

  73. /// `¬¬(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

  74. /// `¬¬¬¬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

  75. /// `¬¬¬(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

  76. /// `¬¬((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

  77. ύʔεͷ๏ଇˠDBMMDD
    ¬¬(A → B)
    ¬¬A → ¬¬B
    ((A → ¬¬B) → ¬¬A) → ¬¬A
    ͜͜Ͱ Λ࢖͏ͱ
    ¬¬(A → B)
    A → ¬¬B
    ¬¬(((A → B) → A) → A) ύʔεͷ๏ଇ

    View full-size slide

  78. /// Continuation.
    typealias Cont = (@escaping (A) -> R) -> R
    ///
    /// Call with Current Continuation.
    /// - Peirce: `((A → B) → A) → A`
    /// - CallCC: `((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

  79. func doubleNegationElim_callCC(
    _ doubleNegation: @escaping (
    _ neg: @escaping (A) -> Cont
    ) -> Cont
    ) -> Cont
    {
    callCC { exit -> Cont in
    flatMap(doubleNegation(exit), absurd)
    }
    }

    View full-size slide

  80. ֊໋୊࿦ཧ

    ͢΂ͯʮ˲ʯͱଘࡏʮ˳ʯ

    View full-size slide

  81. ֊໋୊࿦ཧ
    A1
    ∧ A2
    ∧ ⋯ ≅ ∀X . X
    A1
    ∨ A2
    ∨ ⋯ ≅ ∃X . X
    (A1
    → C) ∧ (A2
    → C) ∧ ⋯ ≅ ∀X . (X → C)
    ʮ໋୊ʯͷશশྔԽ㱼ɺଘࡏྔԽ㱽

    ֊ड़ޠ࿦ཧͷʮ߲ʯͷྔԽͱҟͳΔͷͰ஫ҙ

    㱸ͱ㱼
    㱹ͱ㱽

    View full-size slide

  82. ֊໋୊࿦ཧͷਪ࿦نଇ
    ∀X . A(X)
    A(T)
    A(Y)
    ∀X . A(X)
    ∃X . A(X) ∀X . (A(X) → C)
    C
    A(T)
    ∃X . A(X)
    ͸ɹɹɹʹࢸΔԋ៷ͷԾఆ͢΂ͯͱɹɹɹͷதͰ
    ࣗ༝ม਺ͱͯ͠࢖ΘΕ͍ͯͳ͍ܕม਺
    Y A(Y) A(X)
    ͸ɹɹʹ
    ୅ೖՄೳͳܕ
    T A

    View full-size slide

  83. (A1
    → C) ∧ (A2
    → C) ∧ ⋯ ≅ ∀X . (X → C)
    let intToBool: (Int) -> Bool = { ... }
    let floatToBool: (Float) -> Bool = { ... }
    let strToBool: (String) -> Bool = { ... }
    // ... ͢΂ͯͷܕʹ͍ͭͯಉ͡Α͏ͳ࣮૷Λߦ͍͍ͨ
    // Swift ͷδΣωϦΫεʢଟ૬ؔ਺ʣΛ࢖͏ͱɺ
    // ্هͷॏෳίʔυΛ1ͭʹ·ͱΊͯந৅ԽͰ͖Δɻ
    func xToBool(_ x: X) -> Bool { ... }

    View full-size slide

  84. ˲ͱ˳ͷؔ܎
    (A1
    ∨ A2
    ) → C ≅ (A1
    → C) ∧ (A2
    → C)
    (∃X . X) → C ≅ ∀X . (X → C)
    (A1
    ∨ A2
    ∨ ⋯) → C ≅ (A1
    → C) ∧ (A2
    → C) ∧ ⋯
    (A → C) → ((B → C) → ((A ∨ B) → C))
    ࢀߟɿ

    View full-size slide

  85. ∀X . (X → C) ≅ (∃X . X) → C
    func forallXToBool(_ x: X) -> Bool {
    anyToBool(x)
    }
    /// - Note: `∃X.X` is isomorphic to `Any`.
    func anyToBool(_ any: Any) -> Bool {
    forallXToBool(any)
    }

    View full-size slide

  86. ∀(X <: P) . (X → C) ≅ (∃(X <: P) . X) → C
    protocol P {
    var value: String { get }
    }
    struct AnyP: P {
    let value: String
    }
    func forallXProtocolToBool(_ x: X) -> Bool { // Generic func
    protocolToBool(x)
    }
    func protocolToBool(_ p: P) -> Bool { // Protocol func (dynamic)
    forallXProtocolToBool(AnyP(value: p.value))
    }

    View full-size slide

  87. ∀(X <: P) . (X → C) ≅ (∃(X <: P) . X) → C
    // Reverse Opaque Result Type (static)
    func someProtocolToBool(_ p: some P) -> Bool {
    forallXProtocolToBool(p)
    }
    // ERROR: Isn’t supported yet in Swift 5.1

    View full-size slide

  88. ֊໋୊࿦ཧ

    δΣωϦΫε ଟ૬ੑ

    Ұ෦ɺϓϩτίϧϕʔεʹஔ͖׵͑΋Մೳ

    View full-size slide

  89. ϥϜμΩϡʔϒ

    View full-size slide

  90. ϥϜμΩϡʔϒ
    ֊ ଟ૬
    ϥϜμ
    ֊௚؍ओ໋ٛ୊࿦ཧ

    ܕʹґଘͨ͠஋
    ଟ૬ؔ਺
    ͕࡞ΕΔ
    ୯७ܕ෇͖ϥϜμ
    ֊௚؍ओ໋ٛ୊࿦ཧ

    ஋ʹґଘͨ͠஋
    ߴ֊ؔ਺
    ͕࡞ΕΔ
    ґଘܕ
    ֊௚؍ओٛड़ޠ࿦ཧ

    ஋ʹґଘͨ͠ܕ͕࡞ΕΔ
    ߴ֊ଟ૬ϥϜμ
    ߴ֊ΧΠϯυʴଟ૬ؔ਺
    ߴ֊ΧΠϯυܕ
    ܕʹґଘͨ͠ܕ
    ߴ֊ͷܕؔ਺
    ͕࡞ΕΔ

    View full-size slide

  91. ϥϜμΩϡʔϒ
    4XJGU͸
    ΠϚίί
    ࠓճ
    ֶΜͩ͜ͱ

    View full-size slide

  92. ϥϜμΩϡʔϒ
    ֊ ଟ૬
    ϥϜμ
    ֊௚؍ओ໋ٛ୊࿦ཧ

    ܕʹґଘͨ͠஋
    ଟ૬ؔ਺
    ͕࡞ΕΔ
    ୯७ܕ෇͖ϥϜμ
    ௚؍ओ໋ٛ୊࿦ཧ

    ஋ʹґଘͨ͠஋
    ௨ৗͷߴ֊ؔ਺
    ͕࡞ΕΔ
    ґଘܕ
    ֊௚؍ओٛड़ޠ࿦ཧ

    ஋ʹґଘͨ͠ܕ͕࡞ΕΔ
    ߴ֊ଟ૬ϥϜμ
    ଟ૬ͳߴ֊ܕߏஙࢠ͕࡞ΕΔ
    ߴ֊ΧΠϯυܕ
    ܕʹґଘͨ͠ܕ
    ߴ֊ͷܕߏஙࢠ
    ͕࡞ΕΔ
    Calculus
    of
    Constructions

    View full-size slide

  93. ·ͱΊ
    w࿦ཧΛʮܗࣜʯͱʮҙຯʯͷͭͷଆ໘͔Βߟ͑Δ
    wܗࣜతମܥʜެཧʢҰ෦ͷ߃ਅͳ࿦ཧࣜʣͱਪ࿦نଇ
    wݹయ࿦ཧ͔Β௚؍ओٛ࿦ཧ΁ʢഉத཯ͷഇࢭͱΫϦϓΩҙຯ࿦ʣ
    w֊໋୊࿦ཧʢδΣωϦΫεͱϓϩτίϧʣ
    w௚؍ओٛ࿦ཧ˱4XJGUϓϩάϥϛϯάʢΧϦʔɾϋϫʔυରԠʣ
    w࿦ཧΛֶͿͱɺܕͷҙຯͱՁ஋͕෼͔Δ

    View full-size slide

  94. Sample Code

    http://gist.github.com/inamiy/SwiftAndLogic

    View full-size slide

  95. One more thing…

    View full-size slide

  96. ʙؼ͖ͬͯͨݍ࿦ʙ

    View full-size slide

  97. ࡢ೥ͷJ04%$ͷൃද

    IUUQTTQFBLFSEFDLDPNJOBNJZJPTEDKBQBO

    IUUQTRJJUBDPNJOBNJZJUFNTFDEFBGCDE

    View full-size slide

  98. A
    B C
    ݍ
    idA
    idB idC

    View full-size slide

  99. A
    B C
    ݍ
    f g ∘ f
    g

    View full-size slide

  100. ߃౳ࣹͱ߹੒
    A → B B → C
    A → C
    A → A

    View full-size slide

  101. ࿦ཧԋࢉ˭ ͔ͭ

    A ∧ B
    A
    A ∧ B
    B
    A B
    A ∧ B

    View full-size slide

  102. A ∧ B
    A B
    A ∧ B
    A
    A ∧ B
    B

    View full-size slide

  103. (C → A) ∧ (C → B)
    C → (A ∧ B)
    A B
    C
    A ∧ B
    A B
    A ∧ B
    HomC
    (C, A) × HomC
    (C, B)
    ≅ HomC
    (C, A × B)

    View full-size slide

  104. ࿦ཧԋࢉˮ ·ͨ͸

    A
    A ∨ B
    B
    A ∨ B
    A ∨ B A → C B → C
    C

    View full-size slide

  105. A ∨ B
    A B
    A
    A ∨ B
    B
    A ∨ B

    View full-size slide

  106. A B
    C
    A ∨ B (A → C) ∧ (B → C)
    (A ∨ B) → C
    A ∨ B A → C B → C
    C
    HomC
    (A, C) × HomC
    (B, C)
    ≅ HomC
    (A + B, C)

    View full-size slide

  107. ถాͷิ୊
    Hom[C,Set]
    (HomC
    (A, − ), F) ≅ F(A)
    ∀B . (A → B) → F(B) ≅ F(A)
    A ⟺ ∀B . (A → B) → B
    A ⟹ (A → ⊥ ) → ⊥
    '߃౳ؔखͷͱ͖
    #˵ʹಛघԽ

    ˲আڈ

    ೋॏ൱ఆ
    ¬¬A

    View full-size slide

  108. άϦϕϯίͷఆཧɿݹయ࿦ཧˠ௚؍ओٛ࿦ཧ΁ͷຒΊࠐΈ
    ௚؍ओٛ࿦ཧ
    ݹయ࿦ཧ
    A ∨ ¬A ¬¬(A ∨ ¬A)
    ¬¬
    ೋॏ൱ఆಋೖ
    ഉத཯ ೋॏ൱ఆഉத཯
    $14ม׵
    A ⟹ (A → ⊥ ) → ⊥

    View full-size slide

  109. F(C)
    D
    ݍ
    F
    C
    ݍ
    U(D)
    ࣹͷू߹
    U

    rightAdjunct
    leftAdjunct

    View full-size slide

  110. F(C)
    D
    ݍ
    F
    U
    C
    U(D)
    ݍ
    F(U(D)) U(F(C))
    f
    f
    F(f)
    U(f)
    unitC
    counitD

    View full-size slide

  111. A × X
    B
    ݍ
    − × X
    ( − )X
    A
    BX
    ݍ
    BX × X
    f
    f
    f × id
    eval
    ႈ ࢦ਺ର৅

    ධՁࣹ

    View full-size slide

  112. B
    BX × X
    eval
    A × X
    − × X
    ( − )X
    A
    BX
    f
    f
    f × id
    ႈ ࢦ਺ର৅

    ݍ ݍ
    ධՁࣹ
    X → B B
    B

    View full-size slide

  113. σΧϧτ ΧϧςγΞϯ
    ดݍ
    ऴର৅ ⊤

    ௚ੵ"ʷ# "˭#

    ႈ ࢦ਺ର৅
    #" "ˠ#

    Λ࣋ͭݍͷ͜ͱ
    ௚؍ओٛ
    ࿦ཧͷܗࣜతମܥ͸σΧϧτดݍʹΑͬͯ

    ݍ࿦తʹҙຯ෇͚͢Δ͜ͱ͕Ͱ͖Δ
    σΧϧτดݍ ࿦ཧࣜͷূ໌ ܕ෇͖ϥϜμܭࢉ
    ର৅ ࿦ཧࣜ ܕ
    ࣹ ূ໌ ϥϜμ߲
    ࣗ༝ม਺Λ࣋ͨͳ͍

    ႈର৅ ˠΛ࢖ͬͨ࿦ཧࣜ ؔ਺ܕ
    ධՁࣹ Ϟʔμεϙωϯε ؔ਺ద༻
    ΧϦʔɾϋϫʔυɾϥϯϕοΫରԠ

    View full-size slide

  114. "MHFCSBJD%BUB5ZQFJO4XJGU

    IUUQTTQFBLFSEFDLDPNJOBNJZBMHFCSBJDEBUBUZQFJOTXJGU

    View full-size slide

  115. τϙεʹσΧϧτดݍʴ෦෼ର৅෼ྨࢠ
    {a ∈ A|p(a)}
    A
    τϙε
    Ω
    true
    1
    unit
    m
    p
    p.b.

    View full-size slide

  116. τϙεʹσΧϧτดݍʴ෦෼ର৅෼ྨࢠ
    {a ∈ A|p(a)}
    A
    τϙε
    Ω
    true
    1
    unit
    m
    ෦෼ର৅
    4VCPCKFDU

    Ϟϊࣹ

    ෦෼ର৅෼ྨࢠ
    4VCPCKFDU$MBTTJpFS

    ྫɿτϙε͕ू߹ͷݍͷͱ͖

    Њ\USVF GBMTF^
    p
    p.b.
    Ҿ͖໭͠

    1VMMCBDL QC

    ಛੑࣹ
    "্ͷड़ޠ

    View full-size slide

  117. ࢀߟจݙʢ࿦ཧֶɺϥϜμܭࢉɺܕγεςϜʣ
    w ʰܭࢉ࿦ཧֶʱߨٛࢿྉُࢁ޾ٛ
    w ΧϦʔʹϋϫʔυಉܕରԠ8JLJQFEJB
    w ΧϦʔɾϋϫʔυରԠͱ௚؍ओٛ࿦ཧͷ
    ҙຯ࿦ೖ໳໼ా෦ढ़հ
    w ௚؍ओٛ࿦ཧ΁ͷট଴রҪҰ੒
    w ਺ֶಉ޷ձފా
    w $MBTTJDBM-PHJDJO)BTLFMM
    w "5ZQF4ZTUFN'SPN4DSBUDIr
    3PCFSU8JENBOO
    w "4IPSU*OUSPEVDUJPOUP4ZTUFNT'
    BOE'Т1BCMP/PHVFJSB
    w ࿦ཧͱܭࢉͷ͘͠ΈԮ୩ণݾɺ੢࡚ਅ໵
    w ৘ใՊֶʹ͓͚Δ࿦ཧখ໺׮᏷
    w ϓϩάϥϜҙຯ࿦ԣ಺׮จ
    w ܕγεςϜೖ໳ 5B1-
    1JFSDF

    View full-size slide

  118. ࢀߟจݙʢݍ࿦ɺτϙεʣ
    w ೥ݍ࿦ษڧձࢿྉதଜߊҰ
    w ᐻࢁਖ਼޾ͷΩϚΠϥࣂҭه
    w ఩ֶऀͷͨΊͷݍ࿦ೖ໳৿ాਅੜ
    w ݍ࿦ʹΑΔϓϩάϥϛϯάͱ࿦ཧ
    !TIJBUTVNBU !@@JOU
    w τϙεBMHE
    w 5IF%BSL4JEFPG'PSDJOH
    w 0MJWJB$BSBNFMMPTXFCTJUF
    w "O*OUSPEVDUJPOUP5PQPT5IFPSZ
    w ,SJQLFr+PZBMҙຯ࿦;JQIJM"MFTIMBT
    w (SPUIFOEJFDL5PQPT΁ͷೖ໳
    !LSJQLFKPZBM
    w ϓϩάϥϛϯάݴޠͷҙຯ࿦ͱݍ࿦௕୩઒
    ਅਓ
    w τϙεͱߴ֊࿦ཧ5BJDIJ6FNVSB
    w ݍ࿦"XPEFZ
    w ݍ࿦ʹΑΔ࿦ཧֶʕߴ֊࿦ཧͱτϙεਗ਼
    ਫٛ෉
    w ݍ࿦ͷา͖ํ೔ຊධ࿦ࣾ

    View full-size slide

  119. Thanks!
    Yasuhiro Inami
    @inamiy

    View full-size slide