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

多相型、推論、Ruby

Soutaro Matsumoto
March 20, 2017
6.6k

 多相型、推論、Ruby

大江戸Ruby会議06 https://asakusarb.github.io/oedo06/

Soutaro Matsumoto

March 20, 2017
Tweet

Transcript

  1. ෦෼ܕଟ૬ w ෦෼ܕ
 “String is a Object” interface X {

    void f(Object x); } X x = …; x.f("Ruby"); x.f(new ArrayList());
  2. ෦෼ܕଟ૬ w ෦෼ܕ
 “String is a Object” interface X {

    void f(Object x); } X x = …; x.f("Ruby"); x.f(new ArrayList()); Ҿ਺ͷܕ͸String
  3. ෦෼ܕଟ૬ w ෦෼ܕ
 “String is a Object” interface X {

    void f(Object x); } X x = …; x.f("Ruby"); x.f(new ArrayList()); Ҿ਺ͷܕ͸String Ҿ਺ͷܕ͸ArrayList
  4. ʢ׬શͳʣܕਪ࿦ [Milner, 1978] w શ͘ܕΛॻ͔ͳͯ͘΋ྑ͍
 ʢܕਪ࿦ͷ׬શੑCompletenessʣ w ϓϩάϥϛϯάݴޠ΁ͷ੍໿͕େ͖͍ʢMLʣ # let

    has_even_leaf tree = exists_leaf (fun n -> n mod 2 = 0) tree;; val has_even_leaf : tree -> bool = <fun>
  5. ϩʔΧϧܕਪ࿦ [Pierce, 1997] w ϩʔΧϧม਺ͷܕ͸ॻ͔ͳͯ͘΋ྑ͍
 ϝιουͱ͔ͷܕ͸ॻ͘ɻ w ͍ΖΜͳϓϩάϥϛϯάݴޠʹಋೖͰ͖Δ
 ʢScala/Swift/C#/C++ʣ func

    greet(person: String) -> String { let greeting = "Hello, " + person + "!" return greeting } greetingͷܕ͸ਪ࿦͞ΕΔ
  6. ϩʔΧϧܕਪ࿦ [Pierce, 1997] w ϩʔΧϧม਺ͷܕ͸ॻ͔ͳͯ͘΋ྑ͍
 ϝιουͱ͔ͷܕ͸ॻ͘ɻ w ͍ΖΜͳϓϩάϥϛϯάݴޠʹಋೖͰ͖Δ
 ʢScala/Swift/C#/C++ʣ func

    greet(person: String) -> String { let greeting = "Hello, " + person + "!" return greeting } greetingͷܕ͸ਪ࿦͞ΕΔ ؔ਺ͷܕ͸ॻ͘
  7. OCamlͳΜ͔͍ۙͷͰ͸ ύϥϝʔλଟ૬ ߏ଄తͳ෦෼ܕଟ૬
  ׬શͳܕਪ࿦ Ruby # let f x

    = print_string x#to_s;; val f : < to_s : string; .. >
 -> unit = <fun>
  8. OCamlͳΜ͔͍ۙͷͰ͸ ύϥϝʔλଟ૬ ߏ଄తͳ෦෼ܕଟ૬
  ׬શͳܕਪ࿦ Ruby # let f x

    = print_string x#to_s;; val f : < to_s : string; .. >
 -> unit = <fun> #Ͱϝιουݺͼग़͠
  9. OCamlͳΜ͔͍ۙͷͰ͸ ύϥϝʔλଟ૬ ߏ଄తͳ෦෼ܕଟ૬
  ׬શͳܕਪ࿦ Ruby # let f x

    = print_string x#to_s;; val f : < to_s : string; .. >
 -> unit = <fun> to_sͱ͍͏ϝιου͕͋Ε͹ͳΜͰ΋͍͍ #Ͱϝιουݺͼग़͠
  10. ͝ఏҊ w ߏ଄తͳ෦෼ܕଟ૬ ϩʔΧϧܕਪ࿦ Gradual Typing w ʢΘΓͱʣDuck TypingͰ͖ΔͷͰخ͍͠ w

    ॻ͔ͳͯ͘ྑ͍ܕ͸ॻ͔ͳ͍ w ܕ͕ॻ͍ͯͳ͍ͱ͖͸ࠓͷRubyͱಉ͡ w ܕͷͨΊͷݴޠ͸Rubyͱผ
 requireͱ͔ΦʔϓϯΫϥεͱ͔ϝλϓϩάϥϛϯάͱ ͔͸ߟ͑ͳ͍ɻ
  11. # group.rbi interface String def gsub: (Regexp, String) -> String

    end interface IGroup def people: ActiveRecord.Association<IPerson> end interface IPerson def email: String end # foo_controller.rb group: IGroup = Group.create!(name: name) person = group.people.new escaped = person.email.map {|x| … }
  12. # group.rbi interface String def gsub: (Regexp, String) -> String

    end interface IGroup def people: ActiveRecord.Association<IPerson> end interface IPerson def email: String end # foo_controller.rb group: IGroup = Group.create!(name: name) person = group.people.new escaped = person.email.map {|x| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ
  13. # group.rbi interface String def gsub: (Regexp, String) -> String

    end interface IGroup def people: ActiveRecord.Association<IPerson> end interface IPerson def email: String end # foo_controller.rb group: IGroup = Group.create!(name: name) person = group.people.new escaped = person.email.map {|x| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
  14. # group.rbi interface String def gsub: (Regexp, String) -> String

    end interface IGroup def people: ActiveRecord.Association<IPerson> end interface IPerson def email: String end # foo_controller.rb group: IGroup = Group.create!(name: name) person = group.people.new escaped = person.email.map {|x| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ܕ஫ऍ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
  15. # group.rbi interface String def gsub: (Regexp, String) -> String

    end interface IGroup def people: ActiveRecord.Association<IPerson> end interface IPerson def email: String end # foo_controller.rb group: IGroup = Group.create!(name: name) person = group.people.new escaped = person.email.map {|x| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ܕ஫ऍ ӈลͷܕ͕Θ͔ΔͷͰɺܕ஫ऍ͸ෆཁ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
  16. # group.rbi interface String def gsub: (Regexp, String) -> String

    end interface IGroup def people: ActiveRecord.Association<IPerson> end interface IPerson def email: String end # foo_controller.rb group: IGroup = Group.create!(name: name) person = group.people.new escaped = person.email.map {|x| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ܕ஫ऍ ӈลͷܕ͕Θ͔ΔͷͰɺܕ஫ऍ͸ෆཁ person.emailͷܕ͕StringͩͱΘ ͔ΔͷͰɺmap͕ͳ͍͜ͱ͕Θ͔Δ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
  17. ܕ஫ऍ͕ͳ͚Ε͹͜Ε·ͰͷRubyͱಉ͡ group = Group.create!(name: name) person = group.people.new escaped =

    person.email.map {|x| … } ܕ஫ऍ͕ͳ͍ͷͰશ෦anyʹͳΔԿ΋ݕࠪ͠ͳ͍
  18. ܕ஫ऍ͕ͳ͚Ε͹͜Ε·ͰͷRubyͱಉ͡ group = Group.create!(name: name) person = group.people.new escaped =

    person.email.map {|x| … } map͕ແ͍͜ͱ͸࣮ߦ࣌ʹൃ֮ ܕ஫ऍ͕ͳ͍ͷͰશ෦anyʹͳΔԿ΋ݕࠪ͠ͳ͍
  19. # group.rbi interface String def gsub: (Regexp, String) -> String

    ... end interface IGroup def people: ActiveRecord.Association<IPerson> end interface IPerson def email: String end # foo_controller.rb group: IGroup = Group.create!(name: name) person = group.people.new escaped = person.email.map {|x| … }