Soutaro Matsumoto
March 20, 2017
6.7k

# 多相型、推論、Ruby

March 20, 2017

## Transcript

10. ### ଟ૬ܕ w ύϥϝʔλଟ૬Parametric Polymorphism w ෦෼ܕଟ૬Subtyping Polymorphism w ΞυϗοΫଟ૬Ad-hoc Polymorphism

จ຺ʹԠͯ͡ɺҰఆͷ੍ݶͷݩͰɺ  ࣜʹ͍Ζ͍ΖͳܕΛ༩͑Δ
11. ### ෦෼ܕଟ૬ w ෦෼ܕ  “String is a Object” interface X {

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

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

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

16. ### ʢ׬શͳʣܕਪ࿦ [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>
17. ### ϩʔΧϧܕਪ࿦ [Pierce, 1997] w ϩʔΧϧม਺ͷܕ͸ॻ͔ͳͯ͘΋ྑ͍  ϝιουͱ͔ͷܕ͸ॻ͘ɻ w ͍ΖΜͳϓϩάϥϛϯάݴޠʹಋೖͰ͖Δ  ʢScala/Swift/C#/C++ʣ func

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

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

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

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

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

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

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

28. ### eval w ແཧ w evalͷฦΓ஋ͷܕ͸anyΈ͍ͨͳܕʹͯ͠͠·͑͹ ͍͍ͷͰ͸ʢGradual Typing [Siek and Taha,

2006]ʣ eval(gets)
29. ### ActiveRecord w DBʹ઀ଓ͠ͳ͍ͱΞτϦϏϡʔτ͕Θ͔Βͳ͍ w anyͰͳΜͱ͔͠Α͏ class Person < ApplicationRecord belongs_to

:group has_many :contacts end

32. ### require w require͕࣮ߦ͞ΕΔॠؒ·ͰͲͷϓϩάϥϜ͕ಡ· ΕΔ͔Θ͔Βͳ͍ w require͸ϝιουͳͷͰɺ࠶ఆٛͰ͖Δ͠ɺͲ͜ ʹͰ΋ॻ͚Δ͠ɺҾ਺ʹ͕ࣜॻ͚Δ w \$LOAD_PATH΋͋ΔΑʂ w

requireΛߏจʹͯ͠จࣈྻ͔͠ड͚औΕͳ͍ˍτο ϓϨϕϧʹ͔͠ॻ͚ͳ͍Α͏ʹ͢Ε͹ྑ͍ͷͰ͸

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

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

38. ### # 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| … }
39. ### # 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| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ
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| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
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| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ܕ஫ऍ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
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| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ܕ஫ऍ ӈลͷܕ͕Θ͔ΔͷͰɺܕ஫ऍ͸ෆཁ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
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| … } ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ ܕ஫ऍ ӈลͷܕ͕Θ͔ΔͷͰɺܕ஫ऍ͸ෆཁ person.emailͷܕ͕StringͩͱΘ ͔ΔͷͰɺmap͕ͳ͍͜ͱ͕Θ͔Δ ΠϯλϑΣʔεʹϝιουͱܕΛએݴ͢Δ
44. ### ܕ஫ऍ͕ͳ͚Ε͹͜Ε·ͰͷRubyͱಉ͡ group = Group.create!(name: name) person = group.people.new escaped =

person.email.map {|x| … }
45. ### ܕ஫ऍ͕ͳ͚Ε͹͜Ε·ͰͷRubyͱಉ͡ group = Group.create!(name: name) person = group.people.new escaped =

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

person.email.map {|x| … } map͕ແ͍͜ͱ͸࣮ߦ࣌ʹൃ֮ ܕ஫ऍ͕ͳ͍ͷͰશ෦anyʹͳΔԿ΋ݕࠪ͠ͳ͍
47. ### # 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| … }

49. ### w Group.create!ͷܕ͕GroupʹͳΔͷ͸Θ͔Βͳ͍ͷʁ  Groupͷܕ͕Θ͔ΔͱࢥͬͯΔͷʁʁ w ܕΛҰͭ΋ॻ͍͍ͯͳ͍ͱͳʹ΋Θ͔Βͳ͍ͷʁʁ  ϦςϥϧͳΜ͔͸Θ͔Δ͸ͣɻҙ֎ͱ͍Ζ͍Ζݟ͔ͭΔͷͰ͸ɻ w Ϋϥεఆٛʹ͸ܕΛॻ͚ͳ͍ͷʁΦʔϓϯΫϥε͸Ͳ͏ͳΔͷʁ  ࠓޙͷ՝୊ɻΦʔϓϯΫϥεͱ0CKFDUJWF\$ͷ\$BUFHPSZͳΜ͔ ͸͍ۙؾ͕͢Δɻ

w ࣮૷͸ͳ͍ͷʁ  Α͘ݟΔͱɺʢܕ஫ऍΛίϝϯτͱ͔Ͱॻ͘͜ͱʹ͢Ε͹ʣ͙͢ ʹ࣮૷Ͱ͖Δͳ͜Εʜʜ