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

論文紹介: Unboxed objects and polymorphic typing

nomaddo
March 31, 2014

論文紹介: Unboxed objects and polymorphic typing

関数型LT大会

nomaddo

March 31, 2014
Tweet

More Decks by nomaddo

Other Decks in Technology

Transcript

  1. Who am I? OCaml, SML#, Emacs Haskell ͸෼͔Γ·ͤΜʜɻ • ౦๺େֶͷେֶӃੜ

    (M1) • ࿦ཧֶͱ͔ܕཧ࿦ͱ͔ίϯύΠϥͱ͔ษڧͯ͠·͢ • झຯ͸ list-package Ͱ֦ுϓϥάΠϯͷϦετΛோΊΔ͜ͱ • ؋͜ΕͰͳΒࢁ৓ɾැ܂͕޷͖Ͱ͢ • ΠϯλʔϯઌΛ୳͍ͯ͠·͢ʂ 2 / 37
  2. ຊ೔࿩͢͜ͱ • ࿦จ঺հ Unboxed objects and polymorphic typing Xavier Leroy

    (POPL92) http://gallium.inria.fr/~xleroy/publi/ unboxed-polymorphism.pdf Έͳ͞Μ୯७ܕ෇͖ϥϜμܭࢉ͘Β͍Θ͔Γ·͢ΑͶʜʁ ʁ ʁ 4 / 37
  3. ੩తܕ෇͚ͱσʔλදݱ ੩తܕ෇͖ݴޠͰ͸ɺܕ৘ใΛར༻ͯ͠ΑΓΑΓσʔλදݱΛબͿ ͜ͱ͕ग़དྷΔ • ಈతܕ෇͖ݴޠͰ͸ɺ஋͸ uniform ʹදݱ͢Δ͔͠ແ͍ • ͜ͷΑ͏ͳݴޠͰ͸ 1

    ϫʔυͰදݱͰ͖ͳ͍σʔλ͸ Box Խ͠ ͯ 1 ϫʔυʹ͢Δඞཁ͕͋Δ • C ͷΑ͏ͳ੩తܕ෇͖ݴޠͰ͸ࣄલʹ஋ͷܕ͕Θ͔͍ͬͯΔͷ Ͱɺͦͷ஋ͷ௕͞΋Θ͔Δɻ • σʔλͷදݱΛޮ཰తͳ΋ͷʹ͢Δ͜ͱ͕ग़དྷΔ box Խ ώʔϓʹΞϩέʔτͯ͠ϙΠϯλͱͯ͠ѻ͏͜ͱ uniform representatin ஋Λ box Խͯ͠ɺ͢΂ͯͷ஋Λ̍ϫʔυͰද͢͜ͱ 7 / 37
  4. polymorphismͷ໰୊఺ • ੩తͳܕ৘ใΛίϯύΠϧʹར༻͢ΔͨΊʹ͸ શͯͷ஋ʹ͸ϢχʔΫͳܕ͕͍͍ͭͯͳ͚Ε͹ͳΒͳ͍ • ͔͠͠ɺpolymorphism Λѻ͏ݴޠͰ͸͜Ε͸੒Γཱͨͳ͍ɻ (* int ->

    int. int ܕ ͷ ஋ ͷ α Π ζ ͸ 1 ϫ ʔ υ *) let succ x = x + 1 (* ’a -> ’a. ’a ܕ ͷ ஋ ͷ α Π ζ ͸ ෼ ͔ Β ͳ ͍ *) let id x = x ͜ΕΛͲ͏΍ͬͯίϯύΠϧ͢Ε͹Α͍ͩΖ͏͔ɻ 8 / 37
  5. ղܾࡦ ίϯύΠϧͷઌૹΓ Ada ͷ generic ͷ࣮૷Ͱ࢖ΘΕ͍ͯΔղܾํ๏ ଟ૬తͳ object ͸࢖ΘΕΔ·ͰίϯύΠϧΛઌૹΓ͢Δ σʔλදݱ͸ޮ཰త͕ͩɺίʔυෳ੡Λ͠ͳ͍ͱ͍͚ͳ͍͠ɺ

    ෼ׂίϯύΠϧͰ͖ͳ͘ͳΔʂ ʢ͍·Ͱ͍͏ C++ͷςϯϓϨʔτͷղܾࡦʣ શͯͷදݱΛ Box Խ͢Δ ଟ͘ͷ ML ܥݴޠͷॲཧܥͰ࠾༻͞Ε͍ͯΔղܾํ๏ γϦΞεʹޮ཰Λଛͳ͏ʂ ྫ͑͹ tuple ͸ৗʹώʔϓʹΞϩέʔτ͠ͳ͍ͱ͍͚ͳ͍ 9 / 37
  6. ຊ࿦จͷఏҊ: Mixed Representation ຊ࿦จͰ͸ɺଞͷղܾखஈΛఏҊ͢Δɻ • ୯૬తͳ஋͸ͦΕͧΕͷ Specialized Representation Λ༻͍Δ (σʔλܕ֤ʑͷαΠζͷදݱΛ༻͍Δ)

    • ଟ૬తͳ஋͸ box Խͯ͠ uniform ʹදݱ͢Δ ઌߦݚڀͱͯ͠ [PeytonJones 91] Ͱ͸ಉ༷ͷදݱΛܗࣜԽ͍ͯ͠Δ ຊ࿦จͰ͸ PeytonJones’ representatin ΁ͷม׵ํ๏΍࣮ࡍͷίϯύ ΠϥͰͷϕϯνϚʔΫ݁Ռʹ͍ͭͯใࠂ͍ͯ͠Δ 10 / 37
  7. ΍Γ͍ͨม׵ͷྫ1 ·ͣɺ؆୯ͳྫ͔ΒݟͯΈΔɻ let make_pair = λx.(x, x) (∗ ∀α. α

    → α ∗ α ∗) makepair(3.14) ͱ͍͏ؔ਺ద༻Λߟ͑ͯΈΔͱɺ • ଟ૬తͳ෦෼ʹ͘Δ஋͸ box Խ͍ͯ͠Δͱ͢Δ • ͦͷ·· makepair ʹ 3.14 Λ౉͢͜ͱ͸ग़དྷͳ͍ (makepair ͷ α ܕͷҾ਺͸ box Խ͞Ε͍ͯΔ͜ͱΛԾఆ͢Δ) • ฦΓ஋ʹ͸ϑϥοτͳɺbox Խ͞Εͯͳ͍ pair Λฦͯ͠΄͍͠ • ஋Λ box Խͨ͠Γ͢Δɺม׵͕ඞཁͳ͜ͱ͕Θ͔Δ wrap(τ), unwrap(τ) ͱ͍͏ 2 ͭͷΦϖϨʔλΛಋೖ͢Δͱɺ let x = makepair(wrap(float )(3.14)) in (unwrap(float )(fst(x)), unwrap(float )(snd(x))) 13 / 37
  8. ΍Γ͍ͨม׵ͷྫ2 (* map_pair : ∀α, β. (α → β) →

    α × α → β × β *) let map pair = λ f. λ x. (f(fst(x), f(snd(x)))) let int pair = map pair int of float (3.14, 2.718) ม׵ޙ let int pair = let y = map pair (λ x. wrap(int) (int of float (unwrap(float)(x)))) (wrap(float)(3.14), wrap(float)(2.718)) in (unwrap(int)(fst(y)), unwrap(int)(snd(y))) ଟ૬తͳ஋͸ box Խ͞Ε͍ͯΔ͜ͱʹ஫ҙɻ ଟ૬ؔ਺ͷݺͼग़͠ଆͰ unbox ͯ͠΍Δɻ 14 / 37
  9. ѻ͏ݴޠͷఆٛ ୯७ܕ෇͖ϥϜμܭࢉʹܕεΩʔϜɺlet ߏจΛ௥Ճͨ͠ݴޠ • a ͸ source language, a’ ͸

    target language • i ͸ int ͷ஋ɺf ͸ float ͷ஋ • τ ͸ܕɺσ ͸ ܕεΩʔϜ • Gen(τ, E) ͸ τ ͷࣗ༝ͳม਺Λ ∀ Ͱଋറͨ͠ܕεΩʔϜ 17 / 37
  10. ܕ෇͚ͷྫ E′ = E + x : α E′′ =

    E + id : ∀α.α → α ρ = α ← int (E + x : α)(x) = α Dom(∅) ⊆ ∅ E + x : α ⊢ x : α E ⊢ λx.x : α → α E′′(id) = ∀α.α → α Dom(ρ) ⊆ {α} E′′ ⊢ id : ρ(α → α) = int → int E′′ ⊢ 14 : int E′′ = E + id : ∀α.α → α ⊢ id 14 : int E ⊢ λx.x : α → α E + id : Gen(α → α, E) ⊢ id 14 : int E ⊢ let id = λx.x in id 14 : int 19 / 37
  11. ม׵نଇ1 ܕΛ֦ு͠·͢ τ′ ::= α | int | float |

    τ1 ′ → τ2 ′ | τ1 ′ ∗ τ2 ′ | [τ′] [τ′] ͸ wrap ͞Εͨ஋ͷܕ E ⊢ a : τ => a′ Ͱ a ʹ τ ܕ͕ͭ͘ͱ͖ɺ ߲ a ͸ a’ ʹม׵Ͱ͖Δ 20 / 37
  12. ͠ΜͲ͍࿩1 ML Ͱ͸Ϣʔβ͕৽͍͠ܕΛ࡞Δ͜ͱ͕ग़དྷΔ ΋͏ͪΐ ͬͱෳࡶͳσʔλߏ଄ͷٞ࿦Λ͢Δ ૬มΘΒͣ List, Array ͸ Flat

    ʹͰ͖ͳ͍!!! (* list ͷ ఆ ٛ *) datatype ’a list = Nil | Cons of ’a * ’a list fun length Nil = 0 (* length : ’a list -> int *) | length (Cons e, l) = 1 + length l Cons(3.14, Nil) ͸ϑϥοτʹѻ͑Ε͹ۭؒޮ཰Αͦ͞͏ ͔͠͠ length ؔ਺ʹ͜ͷϦετΛ౉͢͜ͱΛߟ͑Δͱ·͍ͣ 29 / 37
  13. ͠ΜͲ͍࿩2 • length: ’a list → int ͷ’a ʹ୅ೖ͞ΕΔܕͷαΠζ͸ෆ໌ •

    ࠷ॳͷ make_pair ͷྫͰ͸ α ∗ α ܕͷ஋͔Β fst,snd Ͱ஋ΛऔΓ ग़ͯ͠΋͏Ұ౓ pair Λ࡞͍ͬͯΔ • List ΋’a list ΛҾ਺ʹͱΔଟ૬ؔ਺ʹ౉͢ͱ͖ʹɺ࡞Γͳ͓ͤ ͹ྑ͍ʁ ⇒ ࡞Γ௚͢ͷ͸࣌ؒతʹ΋ۭؒతʹ΋ίετ͕ॏ͍ʜ List ͷΑ͏ͳߏ଄͸ flat ʹͰ͖ͳ͍ʜɻͭΒ͍ɻͭΒ͍ɻ Table: flat list Table: a little nested list 30 / 37
  14. ͠ΜͲ͍࿩3 • ࠓ౓͸ α ∗ α list → ... ͱ͍͏ؔ਺Λߟ͑ͯΈΔ

    • float ∗ float list Λ౉͢͜ͱΛߟ͑ΔɻͳΔ΂͘ tuple ΋ϑϥο τʹ͍͕ͨ͠ɺؔ਺͕ཁٻ͢Δܗ͸Լͷ௨Γ • ͜ͷέʔεͰ͸ tuple ΋࡞Γ௚͢ͷͰ͸ͳ͘ box Խͯ͠͠·ͬ ͨ΄͏͕ྑ͍? 31 / 37
  15. GalliumίϯύΠϥͷ࣮૷1 Mixed representation Λ࠾༻ͨ͠ίϯύΠϥΛ࣮૷ ͦͷ࢓༷ʹ͍ͭͯड़΂͍ͯ͘ • unwraped integer ͸ unboxed

    ͳ 32bit ௕ͷσʔλɺwrap ͯ͠΋ 1 ϫʔυͰࡁΉ͕ GC ͷͨΊ box Խ • 8bit ੔਺͕ char, boolean, enum ͷͨΊʹଘࡏ wrap ͞Εͯ΋ unboxed ͷ·· • unit ͸ 0 ݸཁૉͷλϓϧͰ warp ͞Εͨ unit ͸ಛผͳఆ਺ • closure ͸ 2 ϫʔυ௕ͷ wrap ͞Εͯͳ͍දݱͱͯ͠ѻ͏ ʢ࣮ߦίʔυྻ΁ͷϙΠϯλͱɺ؀ڥ΁ͷϙΠϯλʣ • Array ͷཁૉ͸શͯ wrap ͞Ε͍ͯΔ • tuple ͸ཁૉ͕ 4 ͭҎ্ͷͱ͖͸ unboxed Ͱ͸ѻΘͳ͍ • Ϣʔβʔఆٛͷσʔλߏ଄͸લʹٞ࿦ͨ͠Α͏ʹѻ͏ 33 / 37
  16. GalliumίϯύΠϥͷ࣮૷2 ࡉ͔͍࿩͕ଟ͍ͷͰɺେ੾ͦ͏ͳ෦෼͚ͩॻ͖·͢ • Gallium compiler ͷόοΫΤϯυ͸ C-- C--͸ C ͔Β

    struct ͱ union ൈ͍ͯ GC ͱྫ֎Λαϙʔτ • ΦϦδφϧͷ෦෼͸ɺλάແ͠ͷ஋͕ϙΠϯλ͔ͦΕҎ֎͔۠ ผ͢ΔͨΊʹɺܕ৘ใΛ༻͍Δ͜ͱ ʢݹయత GC ͸λάʹΑΓϙΠϯλ͔ͦΕҎ֎͔۠ผ͢Δʣ 34 / 37
  17. ϕϯνϚʔΫ݁Ռ2 • test1, 2 ͸਺஋ܭࢉͰ Unboxed ͳ int ͱ float

    ͷޮՌ͕ݱΕ͍ͯΔ • 5,6,8 Ͱ΋ box Խ͞Εͳ͍දݱͷϝϦοτΛڗडͰ͖͍ͯΔ • ͜ΕΒ͸ཁૉ͕શͯ box Խ͞ΕΔߏ଄ʹΞΫηε͢Δς ετ • ஋ͷड͚౉͠ʹ unboxed tuple, closure Λѻ͑ͨӨڹͰ͸ • test7 ͷ࿩ • ଟ૬తͰߴ֊ͳؔ਺Λଟ༻ͨ͠৔߹ɺuniform ͳදݱͷ΄ ͏͕ޮ཰͕ྑ͍ • ม׵ίʔυ͕୔ࢁૠೖ͞ΕΔͨΊɺ test7 ͸࿐ࠎʹͦͷӨ ڹ͕ग़͍ͯΔ • test7 ͸ int list Λ quad quad (λx.x + 1), quad = λf.λx.f(fx) ͰϚοϓ͢Δςετ 36 / 37
  18. ·ͱΊ • ੩తͳܕ৘ใΛίϯύΠϧʹ׆༻͠ɺͳΔ΂͘ unboxed ͳදݱ Λ༻͍Δγϯϓϧͳํ๏ΛఏҊ • ͦΕͰ΋ list ΍

    array ͷཁૉ͸ box Խͤ͟ΔΛಘͳ͍͕ɺ unboxed ͳ tupleɺclosure ͳͲͷ༗༻ੑΛ֬ೝͨ͠ • ਺஋ܭࢉͰ͸໨֮·͘͠ޮ཰͕޲্͠ɺߴ֊ؔ਺Λଟ͘࢖͏ͱ ͖ʹ͸ޮ཰͕ѱ͍ࣄ͕Θ͔ͬͨ • ୯७ܕ෇͖ϥϜμܭࢉʴ let ଟ૬͕෼͔Δͱ࿦จಡΊ·͢!ʂ ʂ ʂ • ࿦จಡΉͷָ͍͠ʂ ʂ ʂ(’ Т’ ) ࡾ (’ Т’) ࡾ ( ’ Т’) 37 / 37