Shohei Yasutake
May 28, 2015
1.8k

# 代数的データ型について #w8lt

May 28, 2015

## Transcript

2. ### ͸͡Ίʹ • આ໌͕ؒҧ͍ͬͯΔՄೳੑ͕͋Γ·͢ • ಛʹޙ൒ • ؒҧ͍ͬͯͨΒ @amutake_s ʹڭ͑ͯԼ͍͞ •

͜Ε͸W8LTͱ͍͏LTձͰൃද͞Εͨ΋ͷͰ͢ • (౦޻େੜͷΈͰ͕͢)Α͚Ε͹ࢀՃ͍ͯͩ͘͠͞ • http://w8lt.connpass.com/

6. ### ू߹࿦Ͱͷ௚ੵͱ௚࿨ • ू߹Aͱू߹Bͷ௚ੵ (direct product):   • ΑΓҰൠʹ  • ू߹Aͱू߹Bͷ௚࿨

(direct sum):   • ͨͩ͠ 0, 1 ͸ 0 ≠ 1 Ͱ͋ΔΑ͏ͳλά • ΑΓҰൠʹ A ⇥ B = {(a, b) | a 2 A ^ b 2 B} A + B = {(0, a) | a 2 A} [ {(1, b) | b 2 B} Y i20..n Xi = {( x0, · · · , xn) | x0 2 X0 ^ · · · ^ xn 2 Xn } a i20..n Xi = [ i20..n {( i, xi) | xi 2 Xi }
7. ### ܕཧ࿦Ͱͷ௚ੵͱ௚࿨ • ܕAͱܕBͷ௚ੵܕ (product type): Pair A B = Pair

A B • ΑΓҰൠʹ Tuple A B C … • ܕAͱܕBͷ௚࿨ܕ (sum type): Either A B = T0 A | T1 B • ΑΓҰൠʹ T0 A | T1 B | T2 C | …
8. ### ௚ੵͷ௚࿨ • ࠶ܝ: ୅਺తσʔλܕ͸ܕͷ௚ੵͷ௚࿨Ͱߏ੒ ͞ΕΔΑ͏ͳܕ • ྫ: ADTExample = T0

A B | T1 | T2 C ܕͷ௚ੵ ͷ௚࿨
9. ### ୅දతͳྫ • ࣗવ਺ (ϖΞϊ਺) • Nat = Zero | Succ

Nat • Ϧετ • List A = Nil | Cons A (List A) • 2෼໦ • BinTree A = Empty | Node A (BinTree A) (BinTree A)
10. ### ͍Ζ͍Ζͳϓϩάϥϛϯάݴޠ Ͱͷ୅਺తσʔλܕ • ୅਺తσʔλܕΛαϙʔτ͍ͯ͠ΔݴޠͰɺ Ͳ͏͍͏;͏ʹॻ͚Δ͔Λݟ͍͖ͯ·͢ • Haskell • OCaml •

Scala • Coq • Rust • Swift
11. ### Haskell data Nat = Zero | Succ Nat data List

a = Nil | Cons a (List a) data BinTree a = Empty | Node a (BinTree a) (BinTree a)
12. ### OCaml type nat = Zero | Succ of nat type

'a list = Nil | Cons of 'a * 'a list type 'a bin_tree = | Empty | Node of 'a * 'a bin_tree * 'a bin_tree
13. ### Scala sealed trait Nat case object Zero extends Nat case

class Succ(n: Nat) extends Nat sealed trait List[+A] case object Nil extends List[Nothing] case class Cons[A](hd: A, tl: List[A]) extends List[A] sealed trait BinTree[+A] case object Nil extends BinTree[Nothing] case class Node( v: A, l: BinTree[A], r: BinTree[A]) extends BinTree[A]
14. ### Coq Inductive nat := | Zero : nat | Succ

: nat -> nat. Inductive list (A : Type) := | nil : list A | cons : A -> list A -> list A. Inductive bin_tree (A : Type) := | empty : bin_tree A | node : A -> bin_tree A -> bin_tree A -> bin_tree A.
15. ### Rust enum Nat { Zero, Succ(Box<Nat>) } enum List<A> {

Nil, Cons(A, Box<List<A>>) } enum BinTree<A> { Empty, Node(A, Box<BinTree<A>>, Box<BinTree<A>>) }
16. ### Swift protocol NatLike {} enum Nat : NatLike { case

Zero case Succ(NatLike) } // List, BinTree ͸ΊΜͲ͍ͬ͘͞Ά͍ // (Α͘Θ͔Βͳ͍)
17. ### ୅਺తσʔλܕͷΑ͍ͱ͜Ζ • େ఍ͷϓϩάϥϛϯάݴޠͰ͍ͭͰʹύλʔϯϚον͕͍ͭͯ͘Δ • ύλʔϯϚον: ௚࿨෦෼ʹରͯ͠৔߹෼͚͠ɺ௚ੵ෦෼Λ෼ղͯ͠ଋറ • ৔߹෼͚ͷ໢ཏੑͷ੩తݕࠪ • ͍Ζ͍Ζͳߏ଄Λ௚ײతʹද͢͜ͱ͕Ͱ͖Δ

(ͱࢥ͏) • Bool, List, Tree… • ࣦഊͷՄೳੑͷ͋Δ஋ (Option, Either) • ͳͲͳͲ

ݍ࿦ʹΑ͏ͦ͜ʂ
20. ### F-࢝୅਺ • F-࢝୅਺ (ࣗݾؔख F : C → C ͷ࢝୅਺)

ͱ ͸ɺF-୅਺͕ͳ͢ݍͷ࢝ର৅Ͱ͋Δ • ʁʁʁ
21. ### ୅਺తߏ଄ (algebraic structure) • Magma, Semigroup, Monoid, Group, Ring, …

• ͋Δू߹ɺͦͷ্ͷԋࢉͷू߹ɺ౳ࣜެཧͷ ू߹ɺͷ3ͭ૊Ͱද͞ΕΔߏ଄
22. ### ྫ: ϞϊΠυ • : ू߹Xɺೋ߲ԋࢉɾɺ୯Ґݩ eɺ ҎԼͷ౳ࣜެཧ E • associativity:

• identity: • ྫ: ࣗવ਺ͱ଍͠ࢉɺจࣈྻͱ࿈݁ɺͳͲͳͲ 8 x, y, z 2 X, ( x · y ) · z = x · ( y · z ) 8 x 2 X, e · x = x ^ x · e = x (X, {·, e}, E)
23. ### ݍ (category) • ର৅ • ࣹ • dom, cod •

ࣹͷ߹੒ͷ݁߹ੑ • ߃౳ࣹ

25. ### ࢝ର৅ͱऴର৅ (initial object, terminal object) • ݍCʹ͍ͭͯɺ • ࢝ର৅: C

ͷ೚ҙͷର৅ʹରͯ͠ɺͪΐ͏Ͳ ͻͱͭͷࣹ͕ଘࡏ͢ΔΑ͏ͳର৅ • ऴର৅: C ͷ೚ҙͷର৅͔Βɺͪΐ͏Ͳͻͱ ͭͷࣹ͕ଘࡏ͢ΔΑ͏ͳର৅
26. ### ؔख (functor) • F : C → D • ҎԼͷߏ଄Λอͪͳ͕ΒݍΛࣸ͢

• F : C → C ͷΑ͏ʹಉ͡ݍ΁ͷؔखΛಛʹࣗݾؔख (endofunctor) ͱݺͿ F(idA) = idF (A) F(g f) = F(g) F(f)
27. ### ଟ߲ࣜؔख (polynomial functor) • ੵͱ༨ੵ (Sets ͩͱ௚ੵͱ௚࿨) Ͱද͞ΕΔؔख • ͨͩ͠ɺ1

͸ऴର৅ɺC_n ͸ఆ਺ • Sets ͷݍͩͱɺऴର৅͸ཁૉ͕1ͭͷू߹Ͱɺ1 → X ͷࣹ(ؔ਺)͸ X ͷཁૉ਺௨Γ͋Δ • (ͳΜͱͳ͘୅਺తσʔλܕͬΆ͍Ͱ͢Ͷʂ) F(X) = C0 ⇥ 1 + C1 ⇥ X + C2 ⇥ X2 + · · · + Cn ⇥ Xn
28. ### F-୅਺ (F-algebra) • ࣗݾؔख F : C → C ʹର͢ΔɺC

ͷର৅ X ͱ α : F(X) → X ͷࣹ ͷ૊ (X, α) Λ F-୅਺ ͱ͍͏ • ୅਺తߏ଄ͱͷؔ܎: F Λݍ Sets ͷଟ߲ࣜࣗݾؔखͱ͢ΔͱɺF- ୅਺ (X, α) ͸ू߹ X ͱͦͷ্ͷԋࢉͷू߹ α ʹͳΔ (౳ࣜެཧ ͸ͳ͍) • ͜Ε͕୅਺ͱݺ͹ΕΔॴҎʁ • ྫ: F(X) = 1 + X × X ʹର͢Δ (N, [0, +])
29. ### F-४ಉܕ (catamorphism) • F-୅਺ͷ४ಉܕ (F-४ಉܕ) ͸ҎԼͷਤࣜΛՄ ׵ʹ͢ΔΑ͏ͳ h ͷ͜ͱ F(A)

↵ ! A F (h) ? ? y ? ? yh F(B) ! B

31. ### ୅਺తσʔλܕͱF-୅਺ • ର৅ΛܕɺࣹΛؔ਺ͱ͢Δݍʹ͍ͭͯɺଟ߲ࣜࣗݾؔख F Λߟ͑Δͱɺ୅਺తσʔλܕ T ʹ͍ͭͯɺF-୅਺͸ (T, α) ʹͳΔ

• α ͸஋ίϯετϥΫλ (λά) Λ͢΂ͯҰॹʹͨ͠Α͏ͳؔ਺ • ྫ: F(X) = 1 + X ͱ͢Δͱɺ୅਺తσʔλܕ T = T0 | T1 T ʹ͍ͭͯɺF-୅਺͸ (T, [T0, T1]) ʹͳΔ • ͜ͷݍͰ͸ऴର৅͸ϢχοτܕʹͳΔ • T0 Λ () → T ͷؔ਺ɺT1 Λ T → T ͷؔ਺ͱݟΔ • [T0, T1] ͸ɺ() + T → T ͷؔ਺ͰɺҾ਺ʹ () ͕དྷͨΒ T0 ΛɺT ͕དྷͨΒ T1 Λ࢖͏ͱ ͍͏ҙຯ
32. ### ୅਺తσʔλܕͱF-࢝୅਺ • ୅਺తσʔλܕ T ͷF-୅਺ (T, α) ͕ F-୅਺ͷݍͷ࢝ର৅ʹͳΔ •

࢝ର৅ʹͳΔͱ͍͏͜ͱ͸ɺ೚ҙͷ F-୅਺ (X, x) ʹରͯͪ͠ΐ͏ͲҰͭͷ४ಉܕ ͕ଘࡏ͢Δͱ͍͏͜ͱ • ஋ίϯετϥΫλ α Λɺα ͷܕʹݱΕΔ T Λ X ʹม͑ͨؔ਺ x ʹஔ͖׵͑ͯɺࣗ ෼ࣗ਎ͷܕ͕ݱΕ͍ͯΔͱ͜Ζ͸࠶ؼ͢ΔΑ͏ʹ͢Δͱɺ೚ҙͷF-୅਺ (X, x) ʹ ରͯͪ͠ΐ͏ͲҰͭͷ४ಉܕΛ࡞Δ͜ͱ͕Ͱ͖Δ • ྫ: Either a b ܕ͸ԼͷΑ͏ͳఆٛͰ४ಉܕΛ࡞ΕΔ data Either a b = Left a | Right b either_cata : (a -> x) -> (b -> x) -> Either a b -> x either_cata lf rf (Left a) = lf a either_cata lf rf (Right b) = rf b
33. ### ྫ: Nat ܕ data Nat = Zero | Succ Nat

-- F(X) = 1 + X ͱͯ͠ɺ -- Լͷ nat_cata ʹ e : X, f : X -> X Λ౉͢ͱɺ -- F-୅਺ (Nat, [Zero, Succ]) ͔Β F-୅਺ (X, [e, f]) ΁ͷ४ಉܕͱͳΔ nat_cata :: x -> (x -> x) -> Nat -> x nat_cata e _ Zero = e nat_cata e f (Succ n) = f (nat_cata e f n)
34. ### ྫ: Stream ܕ data Stream i o r = Done

r | Yield o (Stream i o r) | Await (i -> Stream i o r) stream_cata :: (r -> x) -> (o -> x -> x) -> ((i -> x) -> x) -> Stream i o r -> x stream_cata df _ _ (Done r) = df r stream_cata df yf af (Yield o s) = yf o (stream_cata df yf af s) stream_cata df yf af (Await f) = af (stream_cata df yf af . f) • ͢͜͠ෳࡶͳྫ
35. ### ྫ: Swap ܕ • ࣗ෼ࣗ਎ͷܕίϯετϥΫλ͕ݱΕΔ͚ΕͲ΋ܕύϥϝʔλ͕ҧ͍ͬͯΔྫ • ࣗ৴͕ͳ͍ data Swap l

r = Pair l r | Swap (Swap r l) -- ୈೋҾ਺ͷܕ͕͜ΕͰ͍͍ͷ͔Θ͔Βͳ͍ swap_cata :: (l -> r -> x) -> (x -> x) -> Swap l r -> x swap_cata pf _ (Pair l r) = pf l r swap_cata pf sf (Swap sw) = sf (swap_cata (swap pf) sf sw)
36. ### ୅਺తσʔλܕͱF-࢝୅਺ (cont) • ͜Μͳ͔Μ͡Ͱɺ֤୅਺తσʔλܕ͔Β೚ҙ ͷF-୅਺΁ͷ४ಉܕ͕࡞ΕΔ (ͬΆ͍) ͷͰɺ ୅਺తσʔλܕ͸F-࢝୅਺ • F-୅਺͸౳ࣜެཧ͕ͳ͍୅਺తߏ଄ΛҰൠԽ

ͨ͠΋ͷͱݟΕΔͷͰɺ୅਺తσʔλܕ͕ͳ ͥ୅਺తͱݺ͹ΕΔͷ͔Θ͔ͬͨ

38. ### ओͳࢀߟจݙ • Wikipedia ͷ͍Ζ͍Ζͳ߲໨ • 2013೥ ݍ࿦ษڧձ ࢿྉ • http://nineties.github.io/category-seminar/

• nLab (਺ֶʹಛԽͨ͠ Wikipedia Έ͍ͨͳ΋ͷ) ͷ͍Ζ͍Ζͳ߲໨ • http://ncatlab.org/nlab/show/HomePage • Benjamin C. Pierce, Basic Category Theory for Computer Scientists, 1991 • Steve Awodey, Category Theory, 2010