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

Haskell の型クラスと Typeclassopedia の紹介 #w8lt

Haskell の型クラスと Typeclassopedia の紹介 #w8lt

Shohei Yasutake

December 16, 2015
Tweet

More Decks by Shohei Yasutake

Other Decks in Programming

Transcript

  1. ܕΫϥε  ܕʹڞ௨͢Δੑ࣭Λൈ͖ग़ͨ͠΋ͷ  ΦϒδΣΫτࢦ޲ͷΫϥεͱ͸ҧ͏ͷͰ஫ҙ  ܕΫϥε͸͍Ζ͍ΖͳݴޠͰ࢖͑Δ  ͜ͷεϥΠυͰ͸)BTLFMMΛ࢖͏ 

    ྫ  &RΫϥε೚ҙͷͭͷ஋ʹ͍ͭͯ౳͍͔͠౳͘͠ͳ ͍͔Θ͔Δ  0SEΫϥε೚ҙͷͭͷ஋ʹ͍ͭͯॱংΛ͚ͭΒΕΔ  4IPXΫϥεจࣈྻʹม׵Ͱ͖Δ
  2. ܕΫϥεͷఆٛ class Eq a => Ord a where compare ::

    a -> a -> Ordering (<=) :: a -> a -> Bool (<) :: a -> a -> Bool ... ʮܕΫϥε͸0SEͱ͍͏໊લͰ͢ʯ ʮBͱ͍͏ܕ ม਺ ʹ͍ͭͯҎԼͷ ؔ਺Λ࣮૷͍ͯͩ͘͠͞ʯ ʮB͕&RͰ͋Δ͜ͱ Λཁٻ͠·͢ʯ ʮ͜ΕΒͷܕΛ࣋ͭؔ਺ ϝιου Λ࣮૷͍ͯͩ͘͠͞ʯ NJOJNBMDPNQMFUFEFpOJUJPO͕͋Δ৔߹͸ͦΕ͚࣮ͩ૷͢Ε͹͍͍
  3. Πϯελϯεએݴ instance Ord Bool where True <= False = False

    _ <= _ = True ʮ#PPMܕΛ0SEͷΠϯελϯεʹ͠·͢ʯ ʮ  ͱ͍͏ؔ਺ͷ࣮૷Ͱ͢ʯ ʮ0SEͷ.JOJNBMDPNQMFUFEFpOJUJPO͸ DPNQBSF·ͨ͸  ͳͷͰ͜ΕͰऴΘΓͰ͢ʯ
  4. ܕΫϥεΛ࢖ͬͨؔ਺ sort :: Ord a => [a] -> [a] sort

    = ... ʮ0SEͷੑ࣭Λ࢖͏ͷͰɺܕม਺B͸ 0SEͷΠϯελϯεͰͳ͚Ε͹͍͚·ͤΜɻ Ͱ΋0SEͰ͋Ε͹ͲΜͳܕͰ΋େৎ෉ʯ
  5. ܕΫϥεͷخ͠͞  ࣗ෼Ͱ࡞ͬͨܕΛͳʹ͔ͷܕΫϥεͷΠϯελϯεʹ͢Δ
 ˠͦͷܕΫϥεʹ͍ͭͯఆٛͯ͋͠Δؔ਺͕λμͰ࢖͑ ΔΑ͏ʹͳΔ  ྫࣗ࡞ͷܕΛ.POBEͷΠϯελϯεʹ͢Δ   Λ࣮૷͢Δ

    ͱɺ$POUSPM.POBEϞδϡʔϧʹఆٛ͞ Ε͍ͯΔؔ਺͚ͩͰ΋ݸͷؔ਺͕࢖͑ΔΑ͏ʹͳΔ  Կ͔ͷܕΫϥεʹ͍ͭͯͷؔ਺Λ࡞Δ
 ˠͦͷܕΫϥεͷΠϯελϯεʹͳ͍ͬͯΔ͢΂ͯͷܕ Ͱͦͷؔ਺͕࢖͑ΔΑ͏ʹͳΔ CBTF
  6. ঺հ͞Ε͍ͯΔܕΫϥε  'VODUPS "QQMJDBUJWF .POBE .POBE5SBOT  .POBE'JY 4FNJHSPVQ .POPJE

    'PMEBCMF  5SBWFSTBCMF $BUFHPSZ "SSPX $PNPOBE  Ұ֮ͭ͑Δ͝ͱʹͰ͖Δ͜ͱ͕޿͕Δ  'VODUPS "QQMJDBUJWF .POBE .POBE5SBOT͘ Β͍·Ͱ΍Δͱ͍ͩͿ͍Ζ͍ΖͰ͖ΔΑ͏ʹͳΔ  lThere is no royal road to Haskell.zͱ͍͏ϢʔΫ Ϧουͷ༗໊ͳݴ༿͕͋Δͬͯ5ZQFDMBTTPQFEJBʹॻ͍ͯΔ ࣌఺
  7. ཧղͷͨΊͷ৺ߏ͑  ˞͜ΕΒ͸ݸਓతͳҙݟͰ͢  આ໌ΛಡΜͰ͏Μ͏ΜᄬΔΑΓͱΓ͍͋͑ͣΖ͍ΖͳΠϯε λϯεΛ࢖ͬͯΈͯ׳ΕΔͷ͕Ұ൪͍͍ͱࢥ͏  .POBEͳͲͷΠϝʔδΛߟ͑͸͡ΊΔͱυπϘʹ͸·ΔՄ ೳੑ͕͋ΔͷͰɺܕͱίʔυ͚ͩΈͯߟ͑Δ 

    ܕΫϥεͷϝιου͚ͩݟΔΑΓݸʑͷΠϯελϯεͷఆٛ ΛΈͯߟ͑Δ  'VODUPS΋.POBE΋ͨͩͷܕΫϥεɻগ͠ܕ͕ෳࡶͳͩ ͚Ͱ&R΍0SEͱಉ͡  ࢖͏͚ͩͳΒ୅਺΍ݍ࿦͸஌Βͳͯ͘΋େৎ෉
  8. ༨ஊ  ࣋ͭ΂͖ੑ࣭Λຬͨ͞ͳ͍Α͏ͳΠϯελϯε΋࡞ΕΔ  ੑ࣭Λຬ͍ͨͯ͠Δ͜ͱΛલఏͱ͢Δؔ਺͕յΕΔ ͦ͠ ͯؾ͖ͮʹ͍͘   ϞφυଇΛຬ͍ͨͯ͠ͳ͍Ϟφυͱ͔؆୯ʹ࡞ΕΔ

     ੑ࣭Λຬ͍ͨͯ͠ͳ͍ͱΠϯελϯεΛ࡞Εͳ͍Α͏ʹͨ͠ ͍  ˠ$PRͱ͔*ESJTͱ͔࢖͑͹Ͱ͖·͢  ϝιουͷܕͷͱ͜Ζʹ໋୊Λॻ͘  )BTLFMMͰ΋Ͱ͖Δʁ ࠷ۙͷ()$ͷܕϨϕϧࣄ৘Λ͋ ·Γ஌Βͳ͍