多相型、推論、Ruby

1fab9d01b25e99522f3dfd01e3d4cb51?s=47 Soutaro Matsumoto
March 20, 2017
5.6k

 多相型、推論、Ruby

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

1fab9d01b25e99522f3dfd01e3d4cb51?s=128

Soutaro Matsumoto

March 20, 2017
Tweet

Transcript

  1. 2.
  2. 3.
  3. 13.

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

    void f(Object x); } X x = …; x.f("Ruby"); x.f(new ArrayList());
  4. 14.

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

    void f(Object x); } X x = …; x.f("Ruby"); x.f(new ArrayList()); Ҿ਺ͷܕ͸String
  5. 15.

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

    void f(Object x); } X x = …; x.f("Ruby"); x.f(new ArrayList()); Ҿ਺ͷܕ͸String Ҿ਺ͷܕ͸ArrayList
  6. 18.

    ʢ׬શͳʣܕਪ࿦ [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>
  7. 20.

    ϩʔΧϧܕਪ࿦ [Pierce, 1997] w ϩʔΧϧม਺ͷܕ͸ॻ͔ͳͯ͘΋ྑ͍
 ϝιουͱ͔ͷܕ͸ॻ͘ɻ w ͍ΖΜͳϓϩάϥϛϯάݴޠʹಋೖͰ͖Δ
 ʢScala/Swift/C#/C++ʣ func

    greet(person: String) -> String { let greeting = "Hello, " + person + "!" return greeting } greetingͷܕ͸ਪ࿦͞ΕΔ
  8. 21.

    ϩʔΧϧܕਪ࿦ [Pierce, 1997] w ϩʔΧϧม਺ͷܕ͸ॻ͔ͳͯ͘΋ྑ͍
 ϝιουͱ͔ͷܕ͸ॻ͘ɻ w ͍ΖΜͳϓϩάϥϛϯάݴޠʹಋೖͰ͖Δ
 ʢScala/Swift/C#/C++ʣ func

    greet(person: String) -> String { let greeting = "Hello, " + person + "!" return greeting } greetingͷܕ͸ਪ࿦͞ΕΔ ؔ਺ͷܕ͸ॻ͘
  9. 23.

    OCamlͳΜ͔͍ۙͷͰ͸ ύϥϝʔλଟ૬ ߏ଄తͳ෦෼ܕଟ૬
  ׬શͳܕਪ࿦ Ruby # let f x

    = print_string x#to_s;; val f : < to_s : string; .. >
 -> unit = <fun>
  10. 24.

    OCamlͳΜ͔͍ۙͷͰ͸ ύϥϝʔλଟ૬ ߏ଄తͳ෦෼ܕଟ૬
  ׬શͳܕਪ࿦ Ruby # let f x

    = print_string x#to_s;; val f : < to_s : string; .. >
 -> unit = <fun> #Ͱϝιουݺͼग़͠
  11. 25.

    OCamlͳΜ͔͍ۙͷͰ͸ ύϥϝʔλଟ૬ ߏ଄తͳ෦෼ܕଟ૬
  ׬શͳܕਪ࿦ Ruby # let f x

    = print_string x#to_s;; val f : < to_s : string; .. >
 -> unit = <fun> to_sͱ͍͏ϝιου͕͋Ε͹ͳΜͰ΋͍͍ #Ͱϝιουݺͼग़͠
  12. 38.

    ͝ఏҊ w ߏ଄తͳ෦෼ܕଟ૬ ϩʔΧϧܕਪ࿦ Gradual Typing w ʢΘΓͱʣDuck TypingͰ͖ΔͷͰخ͍͠ w

    ॻ͔ͳͯ͘ྑ͍ܕ͸ॻ͔ͳ͍ w ܕ͕ॻ͍ͯͳ͍ͱ͖͸ࠓͷRubyͱಉ͡ w ܕͷͨΊͷݴޠ͸Rubyͱผ
 requireͱ͔ΦʔϓϯΫϥεͱ͔ϝλϓϩάϥϛϯάͱ ͔͸ߟ͑ͳ͍ɻ
  13. 40.

    # 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. 41.

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

    # 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. 43.

    # 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| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ܕ஫ऍ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
  17. 44.

    # 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| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ܕ஫ऍ ӈลͷܕ͕Θ͔ΔͷͰɺܕ஫ऍ͸ෆཁ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
  18. 45.

    # 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͕ͳ͍͜ͱ͕Θ͔Δ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
  19. 47.

    ܕ஫ऍ͕ͳ͚Ε͹͜Ε·ͰͷRubyͱಉ͡ group = Group.create!(name: name) person = group.people.new escaped =

    person.email.map {|x| … } ܕ஫ऍ͕ͳ͍ͷͰશ෦anyʹͳΔԿ΋ݕࠪ͠ͳ͍
  20. 48.

    ܕ஫ऍ͕ͳ͚Ε͹͜Ε·ͰͷRubyͱಉ͡ group = Group.create!(name: name) person = group.people.new escaped =

    person.email.map {|x| … } map͕ແ͍͜ͱ͸࣮ߦ࣌ʹൃ֮ ܕ஫ऍ͕ͳ͍ͷͰશ෦anyʹͳΔԿ΋ݕࠪ͠ͳ͍
  21. 49.

    # 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| … }