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

本当はすごい newtype

本当はすごい newtype

FIVE社ピザ会で 2018/08/07 に発表した、Haskell の newtype に関する講演です。
GHC 8.6.1 で導入される DerivingVia 言語拡張の説明もあります。

ソースコード:http://github.com/konn/newtype-talk-five

717e56bac925ce2db1e6f1cf7708056d?s=128

Hiromi Ishii

August 07, 2018
Tweet

Transcript

  1. ຊ౰͸͍͢͝ newtype !NS@LPOO IUUQTLPOOTBODPN 4MJEFTBSFBWBJMBCMFBUhttp://bit.ly/derivia
 &YBNQMFDPEFTBSFPO(JU)VCkonn/newtype-talk-five

  2. ࣗݾ঺հ w ੴҪେւʢ!NS@LPOOʣ ͍ ͠ ͍ ͻ Ζ Έ w

    ਺ֶઐ߈ത࢜՝ఔ೥ w ਺ཧ࿦ཧֶͱܭࢉػՊֶͱ͔ w ؾ͚ͮ͹)BTLFMMྺ೥
  3. ຊ౰͸͍͢͝newtype 3PMFT 4BGF[FSPDPTUDPFSDJPOT BOE%FSJWJOH7JB ʙ.POPJE&'PMEBCMF΋͋ΔΑʙ ͍͢͝ʂ

  4. newtype

  5. newtype newtype Foo α = Bar α newtype Id =

    MkId Word
  6. newtype w ͨͩҰͭͷߏஙࢠͱϑΟʔϧυΛ࣋ͭσʔλܕ newtype Foo α = Bar α newtype

    Id = MkId Word
  7. newtype w ͨͩҰͭͷߏஙࢠͱϑΟʔϧυΛ࣋ͭσʔλܕ w ಺෦දݱ͕།ҰͷϑΟʔϧυͷܕͱಉҰ newtype Foo α = Bar

    α newtype Id = MkId Word
  8. newtype w ͨͩҰͭͷߏஙࢠͱϑΟʔϧυΛ࣋ͭσʔλܕ w ಺෦දݱ͕།ҰͷϑΟʔϧυͷܕͱಉҰ w ܕͷ্Ͱ͸۠ผ͞ΕΔ͕ɺϝϞϦ্Ͱͷදݱ ͸ಉҰͰɺϑΟʔϧυ΋ʮਖ਼֨ʯʹධՁ͞ΕΔ newtype Foo

    α = Bar α newtype Id = MkId Word
  9. ॳ৺ऀ͋Δ͋Δ

  10. ॳ৺ऀ͋Δ͋Δ ʮ;ͭ͏ͷdataએݴͱԿ͕ҧ͏ͷʁʯ

  11. ॳ৺ऀ͋Δ͋Δ ʮ;ͭ͏ͷdataએݴͱԿ͕ҧ͏ͷʁʯ ʮ಺෦දݱ͕ಉ͔ͩ͡Βޮ཰తͩΑʯ

  12. ॳ৺ऀ͋Δ͋Δ ʮ;ͭ͏ͷdataએݴͱԿ͕ҧ͏ͷʁʯ ʮ಺෦දݱ͕ಉ͔ͩ͡Βޮ཰తͩΑʯ ʮޮ཰·ͩͲ͏Ͱ΋͍͍͠dataͰ͍͍΍ʯ

  13. ॳ৺ऀ͋Δ͋Δ ʮ;ͭ͏ͷdataએݴͱԿ͕ҧ͏ͷʁʯ ʮ಺෦දݱ͕ಉ͔ͩ͡Βޮ཰తͩΑʯ ʮޮ཰·ͩͲ͏Ͱ΋͍͍͠dataͰ͍͍΍ʯ ʮࠓ͸VOQBDLTUSJDUpFME΋͋Δ͠ʜʜʯ

  14. ͦΜͳ͜ͱ͸ͳ͍

  15. newtypeͷͭͷ༻్

  16. newtypeͷͭͷ༻్ ࣮૷ͷӅṭ module Data.Id (Id ()) where newtype Id =

    MkId Word
  17. newtypeͷͭͷ༻్ ࣮૷ͷӅṭ module Data.Id (Id ()) where newtype Id =

    MkId Word ܕͷ্Ͱ͸۠ผ Ͱ΋಺෦తʹ͸Word
  18. newtypeͷͭͷ༻్ ࣮૷ͷӅṭ module Data.Id (Id ()) where newtype Id =

    MkId Word ܕͷ্Ͱ͸۠ผ Ͱ΋಺෦తʹ͸Word ֎෦ʹ͸σʔλߏஙࢠ MkId͸Ӆṭ
  19. newtypeͷͭͷ༻్ ࣮૷ͷӅṭ ࣮૷ͷڞ༗ module Data.Id (Id ()) where newtype Id

    = MkId Word ܕͷ্Ͱ͸۠ผ Ͱ΋಺෦తʹ͸Word {-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype Id = MkId Word deriving (Num, Eq) ֎෦ʹ͸σʔλߏஙࢠ MkId͸Ӆṭ
  20. newtypeͷͭͷ༻్ ࣮૷ͷӅṭ ࣮૷ͷڞ༗ module Data.Id (Id ()) where newtype Id

    = MkId Word ܕͷ্Ͱ͸۠ผ Ͱ΋಺෦తʹ͸Word {-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype Id = MkId Word deriving (Num, Eq) ຊདྷ͸ಋग़ෆՄͰ΋ Wordͷ࣮૷͕ڞ༗Ͱ͖Δ ֎෦ʹ͸σʔλߏஙࢠ MkId͸Ӆṭ
  21. newtypeͷͭͷ༻్ ࣮૷ͷӅṭ ࣮૷ͷڞ༗ module Data.Id (Id ()) where newtype Id

    = MkId Word ܕͷ্Ͱ͸۠ผ Ͱ΋಺෦తʹ͸Word {-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype Id = MkId Word deriving (Num, Eq) ຊདྷ͸ಋग़ෆՄͰ΋ Wordͷ࣮૷͕ڞ༗Ͱ͖Δ %FSJWJOH7JB ͰߋʹਐԽ ֎෦ʹ͸σʔλߏஙࢠ MkId͸Ӆṭ
  22. newtypeͷͭͷ༻్ ࣮૷ͷӅṭ ࣮૷ͷڞ༗ ࣮૷ͷબ୒ module Data.Id (Id ()) where newtype

    Id = MkId Word ܕͷ্Ͱ͸۠ผ Ͱ΋಺෦తʹ͸Word {-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype Id = MkId Word deriving (Num, Eq) ຊདྷ͸ಋग़ෆՄͰ΋ Wordͷ࣮૷͕ڞ༗Ͱ͖Δ %FSJWJOH7JB ͰߋʹਐԽ ֎෦ʹ͸σʔλߏஙࢠ MkId͸Ӆṭ
  23. newtypeͷͭͷ༻్ ࣮૷ͷӅṭ ࣮૷ͷڞ༗ ࣮૷ͷબ୒ {-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype Id

    = MkId Word deriving (Num, Eq) ຊདྷ͸ಋग़ෆՄͰ΋ Wordͷ࣮૷͕ڞ༗Ͱ͖Δ %FSJWJOH7JB ͰߋʹਐԽ module Data.Id (Id ()) where newtype Id = MkId Word ܕͷ্Ͱ͸۠ผ Ͱ΋಺෦తʹ͸Word ֎෦ʹ͸σʔλߏஙࢠ MkId͸Ӆṭ
  24. ࣮૷ͷબ୒

  25. ࣮૷ͷબ୒ .POPJEͱ'PMEBCMFΛྫʹ

  26. ྫ୊ɿϦετͷ૸ࠪ ໰ ੔਺ͷϦετ͕༩͑ΒΕͨ࣌ɺͦͷ࠷େ஋ͱ ૯࿨ΛҰճͷ૸ࠪͰٻΊΑɻ ˞foldl͓Αͼfoldsύοέʔδ͸Έͳ͔ͬͨࣄʹ͍ͯͩ͘͠͞

  27. ղ౴ྫ

  28. ղ౴ྫ ৞ΈࠐΈͩʂ

  29. ղ౴ྫ ৞ΈࠐΈͩʂ aggregate :: [ℕ] ! (Maybe ℕ, ℕ) aggregate

    = foldr (λ a (m, s) ! (Just a `max` m, a + s)) (Nothing, 0) ˞ℕ͸*OUFHFSͷུͩͱࢥ͍ͬͯͩ͘͞
  30. ղ౴ྫ ৞ΈࠐΈͩʂ aggregate :: [ℕ] ! (Maybe ℕ, ℕ) aggregate

    = foldr (λ a (m, s) ! (Just a `max` m, a + s)) (Nothing, 0) Կ͔ࣅͨΑ͏ͳ͜ͱ΍ͬͯΜͳʜʜ ˞ℕ͸*OUFHFSͷུͩͱࢥ͍ͬͯͩ͘͞
  31. ղ౴ྫ ৞ΈࠐΈͩʂ aggregate :: [ℕ] ! (Maybe ℕ, ℕ) aggregate

    = foldr (λ a (m, s) ! (Just a `max` m, a + s)) (Nothing, 0) Կ͔ࣅͨΑ͏ͳ͜ͱ΍ͬͯΜͳʜʜ ˞ℕ͸*OUFHFSͷུͩͱࢥ͍ͬͯͩ͘͞
  32. ղ౴ྫ ৞ΈࠐΈͩʂ aggregate :: [ℕ] ! (Maybe ℕ, ℕ) aggregate

    = foldr (λ a (m, s) ! (Just a `max` m, a + s)) (Nothing, 0) Կ͔ࣅͨΑ͏ͳ͜ͱ΍ͬͯΜͳʜʜ ม׵ʴೋ߲ԋࢉ ˞ℕ͸*OUFHFSͷུͩͱࢥ͍ͬͯͩ͘͞
  33. ղ౴ྫ ৞ΈࠐΈͩʂ aggregate :: [ℕ] ! (Maybe ℕ, ℕ) aggregate

    = foldr (λ a (m, s) ! (Just a `max` m, a + s)) (Nothing, 0) Կ͔ࣅͨΑ͏ͳ͜ͱ΍ͬͯΜͳʜʜ ม׵ʴೋ߲ԋࢉ ˞ℕ͸*OUFHFSͷུͩͱࢥ͍ͬͯͩ͘͞
  34. ղ౴ྫ ৞ΈࠐΈͩʂ aggregate :: [ℕ] ! (Maybe ℕ, ℕ) aggregate

    = foldr (λ a (m, s) ! (Just a `max` m, a + s)) (Nothing, 0) Կ͔ࣅͨΑ͏ͳ͜ͱ΍ͬͯΜͳʜʜ ม׵ʴೋ߲ԋࢉ ୯Ґݩ ˞ℕ͸*OUFHFSͷུͩͱࢥ͍ͬͯͩ͘͞
  35. ղ౴ྫ ৞ΈࠐΈͩʂ aggregate :: [ℕ] ! (Maybe ℕ, ℕ) aggregate

    = foldr (λ a (m, s) ! (Just a `max` m, a + s)) (Nothing, 0) Կ͔ࣅͨΑ͏ͳ͜ͱ΍ͬͯΜͳʜʜ .POPJEͩʂ ม׵ʴೋ߲ԋࢉ ୯Ґݩ ˞ℕ͸*OUFHFSͷུͩͱࢥ͍ͬͯͩ͘͞
  36. .POPJE w ࠨӈͷผͳ͘ܭࢉͰ͖ͯɺ୯Ґݩ͕͋Δ΍ͭ • max ΋ + ΋…… • ࠨ͔Β΍ͬͯ΋ӈ͔Β΍ͬͯ΋ಉ͡

    • Nothing (࠷େ஋ແ͠) ͱ 0 ͕୯Ґݩ (ɛ) • ϞϊΠυม׵ʴ৞ΈࠐΈ ⤳ Foldable! x ∙ (y ∙ z) = (x ∙ y) ∙ z x ∙ ε = x = ε ∙ x
  37. 'PMEBCMFܕΫϥε class Foldable t where foldMap :: Monoid m 㱺

    (a ! m) ! t a ! m …
  38. 'PMEBCMFܕΫϥε class Foldable t where foldMap :: Monoid m 㱺

    (a ! m) ! t a ! m … ϞϊΠυʹม׵
  ࠨ͔Β৞ΈࠐΈ
  39. 'PMEBCMFܕΫϥε ͜ͷ࿮૊ΈʹNBY  ΛམͱͤΕ͹Αͦ͞͏ class Foldable t where foldMap ::

    Monoid m 㱺 (a ! m) ! t a ! m … ϞϊΠυʹม׵
  ࠨ͔Β৞ΈࠐΈ
  40. 'PMEBCMFܕΫϥε ͜ͷ࿮૊ΈʹNBY  ΛམͱͤΕ͹Αͦ͞͏ Monoid WordΠϯελϯε͸ߴʑҰ͔ͭ͠ॻ͚ͳ͍ʜʜ class Foldable t where

    foldMap :: Monoid m 㱺 (a ! m) ! t a ! m … ϞϊΠυʹม׵
  ࠨ͔Β৞ΈࠐΈ
  41. 'PMEBCMFܕΫϥε ͜ͷ࿮૊ΈʹNBY  ΛམͱͤΕ͹Αͦ͞͏ Monoid WordΠϯελϯε͸ߴʑҰ͔ͭ͠ॻ͚ͳ͍ʜʜ ͦ͜ͰnewtypeʹΑΔ࣮૷ͷબ୒ʂ class Foldable t

    where foldMap :: Monoid m 㱺 (a ! m) ! t a ! m … ϞϊΠυʹม׵
  ࠨ͔Β৞ΈࠐΈ
  42. Πϯελϯεྫ newtype Sum a = Sum { getSum :: a}

    deriving (Num, Integral) instance Num a 㱺 Monoid (Sum a) where (<>) = (+); ε = 0 newtype Max a = Max { getMax :: a } deriving (Num, Integral, Ord) instance Ord a 㱺 Semigroup (Max a) where (<>) = max
  43. Πϯελϯεྫ newtype Sum a = Sum { getSum :: a}

    deriving (Num, Integral) instance Num a 㱺 Monoid (Sum a) where (<>) = (+); ε = 0 newtype Max a = Max { getMax :: a } deriving (Num, Integral, Ord) instance Ord a 㱺 Semigroup (Max a) where (<>) = max ਺ͷՃ๏ʹؔ͢Δ
 ϞϊΠυ
  44. Πϯελϯεྫ newtype Sum a = Sum { getSum :: a}

    deriving (Num, Integral) instance Num a 㱺 Monoid (Sum a) where (<>) = (+); ε = 0 newtype Max a = Max { getMax :: a } deriving (Num, Integral, Ord) instance Ord a 㱺 Semigroup (Max a) where (<>) = max ਺ͷՃ๏ʹؔ͢Δ
 ϞϊΠυ ࠷େݶʹؔ͢Δ
 ൒܈
  45. Πϯελϯεྫ newtype Sum a = Sum { getSum :: a}

    deriving (Num, Integral) instance Num a 㱺 Monoid (Sum a) where (<>) = (+); ε = 0 newtype Max a = Max { getMax :: a } deriving (Num, Integral, Ord) instance Ord a 㱺 Semigroup (Max a) where (<>) = max ਺ͷՃ๏ʹؔ͢Δ
 ϞϊΠυ ࠷େݶʹؔ͢Δ
 ൒܈ (/%ʹΑΔ
 ࣮૷ͷڞ༗
  46. ࣮ࡍ্ͷ஫ҙ w ༗քͳܕ͔͠ϞϊΠυʹͳΒͳ͍ʂʢ࠷େখݩ͕ͳ͍ͱ୯ ҐݩʹͳΒͳ͍ͷͰʣ w ୯Ґݩͷͳ͍൒܈ʹ͸ͳΔ w ൒܈͔ΒϞϊΠυʹม׵͠ͳ͍ͱ'PMEBCMF͸࢖͑ͳ͍ʂ newtype Max

    a = Max a instance Ord a 㱺 Semigroup (Max a) instance Bounded a 㱺 Monoid (Max a)
  47. ࣮ࡍ্ͷ஫ҙ newtype Option a = Option (Maybe a) instance Semigroup

    a 㱺 Monoid (Option a)
  48. ࣮ࡍ্ͷ஫ҙ 0QUJPO͸൒܈ʹ୯ҐݩΛ෇͚଍ͯ͠ϞϊΠυʹ͢Δ newtype Option a = Option (Maybe a) instance

    Semigroup a 㱺 Monoid (Option a)
  49. ࣮ࡍ্ͷ஫ҙ 0QUJPO͸൒܈ʹ୯ҐݩΛ෇͚଍ͯ͠ϞϊΠυʹ͢Δ ʙ()$ɿ.BZCF͸੍໿ͱͯ͠.POPJEBΛཁٻʂ newtype Option a = Option (Maybe a)

    instance Semigroup a 㱺 Monoid (Option a) instance Monoid a 㱺 Monoid (Maybe a)
  50. ࣮ࡍ্ͷ஫ҙ 0QUJPO͸൒܈ʹ୯ҐݩΛ෇͚଍ͯ͠ϞϊΠυʹ͢Δ ʙ()$ɿ.BZCF͸੍໿ͱͯ͠.POPJEBΛཁٻʂ ()$ʙɿ4FNJHSPVQBʹͳΓɺ0QUJPO͸ෆཁʹʂ newtype Option a = Option (Maybe

    a) instance Semigroup a 㱺 Monoid (Option a) instance Monoid a 㱺 Monoid (Maybe a) instance Semigroup a 㱺 Monoid (Maybe a)
  51. ࣮ࡍ্ͷ஫ҙ 0QUJPO͸൒܈ʹ୯ҐݩΛ෇͚଍ͯ͠ϞϊΠυʹ͢Δ ʙ()$ɿ.BZCF͸੍໿ͱͯ͠.POPJEBΛཁٻʂ ()$ʙɿ4FNJHSPVQBʹͳΓɺ0QUJPO͸ෆཁʹʂ ͱ͸͍͑ɺϙʔλϒϧͳίʔυΛॻ͜͏ͱࢥͬͨΒ0QUJPOʹཔΔ ඞཁ͋Γʜʜɻ newtype Option a =

    Option (Maybe a) instance Semigroup a 㱺 Monoid (Option a) instance Monoid a 㱺 Monoid (Maybe a) instance Semigroup a 㱺 Monoid (Maybe a)
  52. 'PMEBCMF/FXUZQF൛ import Control.Arrow aggregate :: [ℕ] ! (Maybe ℕ, ℕ)

    aggregate = fmap getMax *** getSum ◦ foldMap (Just . Max &&& Sum)
  53. 'PMEBCMF/FXUZQF൛ ਵ෼͖ͬ͢Γʂ import Control.Arrow aggregate :: [ℕ] ! (Maybe ℕ,

    ℕ) aggregate = fmap getMax *** getSum ◦ foldMap (Just . Max &&& Sum)
  54. 'PMEBCMF/FXUZQF൛ ਵ෼͖ͬ͢Γʂ Ͱ΋Ұʑ.BY 4VNΛണ͢ͷ͸μϧ͍ͳ͋ʜʜωε τͯ͠Δ͠ʜʜ import Control.Arrow aggregate :: [ℕ]

    ! (Maybe ℕ, ℕ) aggregate = fmap getMax *** getSum ◦ foldMap (Just . Max &&& Sum)
  55. 'PMEBCMF/FXUZQF൛ ਵ෼͖ͬ͢Γʂ Ͱ΋Ұʑ.BY 4VNΛണ͢ͷ͸μϧ͍ͳ͋ʜʜωε τͯ͠Δ͠ʜʜ import Control.Arrow aggregate :: [ℕ]

    ! (Maybe ℕ, ℕ) aggregate = fmap getMax *** getSum ◦ foldMap (Just . Max &&& Sum) ‎;FSP$PTU$PFSDJPOT
  56. 4BGF;FSP$PTU $PFSDJPOTBOE3PMFT

  57. 4BGF;FSP$PTU $PFSDJPOTBOE3PMFT [1] Breitner, Eisenberg, Peyton Jones and Weirich, 2014

    ʙOFXUZQFͷະདྷΛ੾Γ୓͍ͨେൃ໌ʙ
  58. ೥Ҏલͷ )BTLFMMFSͷ΅΍͖ɿ

  59. ͔ͨ͠ʹ࣮૷ͷબ୒ʹ OFXUZQF͸ศར͚ͩͲ͞ʜʜ

  60. ݁ہҰຕҰຕണͯ͠
 ͍͔ͳ͍ͱ͍͚ͳ͍͠

  61. ͦ͏͢Δͱޮ཰͸
 ѱ͍͡ΌΜ

  62. ҆શ͔ͩΒVOTBGF$PFSDF ࢖͏ख΋͋Δ͚Ͳ

  63. ͋Μ·៉ྷ͡Όͳ͍ʜʜ

  64. ͔͠͠

  65. ɹ೥ɺ OFXUZQFʹ ֵ໋͕ى͖ͨ

  66. ;FSP$PTU$PFSDJPO

  67. ;FSP$PTU$PFSDJPO import Data.Coerce (coerce) coerce :: Coercible a b 㱺

    a ! b
  68. ;FSP$PTU$PFSDJPO w $PFSDJCMF͸಺෦දݱ͕ಉ͡ܕಉ࢜Λؔ࿈͚ͮΔܕ੍໿ import Data.Coerce (coerce) coerce :: Coercible a

    b 㱺 a ! b
  69. ;FSP$PTU$PFSDJPO w $PFSDJCMF͸಺෦දݱ͕ಉ͡ܕಉ࢜Λؔ࿈͚ͮΔܕ੍໿ w ҰݟܕΫϥε͕ͩɺ࣮ࡍʹ͸()$͕ίϯύΠϧ࣌ʹ৘ใΛੜ੒ ͠ɺউखʹΠϯελϯεΛ௥ՃͰ͖ͳ͍ import Data.Coerce (coerce) coerce

    :: Coercible a b 㱺 a ! b
  70. ;FSP$PTU$PFSDJPO w $PFSDJCMF͸಺෦දݱ͕ಉ͡ܕಉ࢜Λؔ࿈͚ͮΔܕ੍໿ w ҰݟܕΫϥε͕ͩɺ࣮ࡍʹ͸()$͕ίϯύΠϧ࣌ʹ৘ใΛੜ੒ ͠ɺউखʹΠϯελϯεΛ௥ՃͰ͖ͳ͍ w Data.Coerceͷcoerceؔ਺ͰθϩίετͷΩϟετ͕Ͱ͖Δʂ import Data.Coerce

    (coerce) coerce :: Coercible a b 㱺 a ! b
  71. ;FSP$PTU$PFSDJPO w $PFSDJCMF͸಺෦දݱ͕ಉ͡ܕಉ࢜Λؔ࿈͚ͮΔܕ੍໿ w ҰݟܕΫϥε͕ͩɺ࣮ࡍʹ͸()$͕ίϯύΠϧ࣌ʹ৘ใΛੜ੒ ͠ɺউखʹΠϯελϯεΛ௥ՃͰ͖ͳ͍ w Data.Coerceͷcoerceؔ਺ͰθϩίετͷΩϟετ͕Ͱ͖Δʂ w Ϟδϡʔϧຖʹਪ࿦ɺcoerceʹ͸σʔλߏஙࢠͷ৘ใ͕ඞཁ

    import Data.Coerce (coerce) coerce :: Coercible a b 㱺 a ! b
  72. ͜ΕΛ࢖͑͹ʜʜ import Control.Arrow aggregate :: [ℕ] ! (Maybe ℕ, ℕ)

    aggregate = fmap getMax *** getSum ◦ foldMap (Just . Max &&& Sum)
  73. ͜͏ͳΓ·͢ import Control.Arrow import Data.Coerce aggregate :: [ℕ] ! (Maybe

    ℕ, ℕ) aggregate = coerce ◦ foldMap (Just . Max &&& Sum)
  74. ͜͏ͳΓ·͢ import Control.Arrow import Data.Coerce aggregate :: [ℕ] ! (Maybe

    ℕ, ℕ) aggregate = coerce ◦ foldMap (Just . Max &&& Sum) ω ε τ ܕ ΋  θ ϩ ί ε τ Ͱ ม ׵ ʂ
  75. ͜͏ͳΓ·͢ import Control.Arrow import Data.Coerce aggregate :: [ℕ] ! (Maybe

    ℕ, ℕ) aggregate = coerce ◦ foldMap (Just . Max &&& Sum) coerce :: (Maybe (Max ), Sum ) → (Maybe , ) ω ε τ ܕ ΋  θ ϩ ί ε τ Ͱ ม ׵ ʂ
  76. ͜͏ͳΓ·͢ import Control.Arrow import Data.Coerce aggregate :: [ℕ] ! (Maybe

    ℕ, ℕ) aggregate = coerce ◦ foldMap (Just . Max &&& Sum) w θϩίετͳͷͰɺ૸ࠪ͸Ұճ͚ͩ coerce :: (Maybe (Max ), Sum ) → (Maybe , ) ω ε τ ܕ ΋  θ ϩ ί ε τ Ͱ ม ׵ ʂ
  77. ͜͏ͳΓ·͢ import Control.Arrow import Data.Coerce aggregate :: [ℕ] ! (Maybe

    ℕ, ℕ) aggregate = coerce ◦ foldMap (Just . Max &&& Sum) w θϩίετͳͷͰɺ૸ࠪ͸Ұճ͚ͩ w લޙͷܕ͕͔ͬ͠Γܾ·͍ͬͯΔͷͰDPFSDFҰൃͰऴྃ coerce :: (Maybe (Max ), Sum ) → (Maybe , ) ω ε τ ܕ ΋  θ ϩ ί ε τ Ͱ ม ׵ ʂ
  78. ωετܕͷม׵

  79. ωετܕͷม׵ ͲΜͳίϯςφதͷܕ΋ม׵Ͱ͖ͯศརʂ

  80. ωετܕͷม׵ ͲΜͳίϯςφதͷܕ΋ม׵Ͱ͖ͯศརʂ ຊ౰ʹʁ

  81. ωετܕͷม׵ ͲΜͳίϯςφதͷܕ΋ม׵Ͱ͖ͯศརʂ ຊ౰ʹʁ newtype Down a = Down a instance

    Ord a 㱺 Ord (Down a) where a ≤ b = b ≤ a data Heap a minView :: Heap a ! Maybe a
  82. ωετܕͷม׵ ͲΜͳίϯςφதͷܕ΋ม׵Ͱ͖ͯศརʂ ຊ౰ʹʁ newtype Down a = Down a instance

    Ord a 㱺 Ord (Down a) where a ≤ b = b ≤ a data Heap a minView :: Heap a ! Maybe a ٯॱΛೖΕΔ
  83. ωετܕͷม׵ ͲΜͳίϯςφதͷܕ΋ม׵Ͱ͖ͯศརʂ ຊ౰ʹʁ newtype Down a = Down a instance

    Ord a 㱺 Ord (Down a) where a ≤ b = b ≤ a data Heap a minView :: Heap a ! Maybe a ٯॱΛೖΕΔ ώʔϓ
  84. ωετܕͷม׵ ͲΜͳίϯςφதͷܕ΋ม׵Ͱ͖ͯศརʂ ຊ౰ʹʁ newtype Down a = Down a instance

    Ord a 㱺 Ord (Down a) where a ≤ b = b ≤ a data Heap a minView :: Heap a ! Maybe a ٯॱΛೖΕΔ ώʔϓ ࠷খݩ0 
  85. ωετܕͷม׵ ͲΜͳίϯςφதͷܕ΋ม׵Ͱ͖ͯศརʂ ຊ౰ʹʁ newtype Down a = Down a instance

    Ord a 㱺 Ord (Down a) where a ≤ b = b ≤ a data Heap a minView :: Heap a ! Maybe a ҙຯ࿦తʹ Heap aͱHeap (Down a)͸Ωϟετग़དྷͳ͍΂͖ ٯॱΛೖΕΔ ώʔϓ ࠷খݩ0 
  86. ͦ͜Ͱ3PMFT

  87. ͦ͜Ͱ3PMFT w ͜ͷ··Ͱ͸DPFSDFͰม׵ग़དྷͯ͠·͏ʂ

  88. ͦ͜Ͱ3PMFT w ͜ͷ··Ͱ͸DPFSDFͰม׵ग़དྷͯ͠·͏ʂ ghci> h = fromList [1,2,3] :: Heap

    Int ghci> minView (coerce h :: Heap (Down Int)) Just 1
  89. ͦ͜Ͱ3PMFT w ͜ͷ··Ͱ͸DPFSDFͰม׵ग़དྷͯ͠·͏ʂ ghci> h = fromList [1,2,3] :: Heap

    Int ghci> minView (coerce h :: Heap (Down Int)) Just 1 Just 3Ͱ͋Δ΂͖ʂ
  90. ͦ͜Ͱ3PMFT w ͜ͷ··Ͱ͸DPFSDFͰม׵ग़དྷͯ͠·͏ʂ ghci> h = fromList [1,2,3] :: Heap

    Int ghci> minView (coerce h :: Heap (Down Int)) Just 1 w ͦ͜Ͱ3PMFΛࢦఆɿ Just 3Ͱ͋Δ΂͖ʂ
  91. ͦ͜Ͱ3PMFT w ͜ͷ··Ͱ͸DPFSDFͰม׵ग़དྷͯ͠·͏ʂ type role Heap nominal ghci> h =

    fromList [1,2,3] :: Heap Int ghci> minView (coerce h :: Heap (Down Int)) Just 1 w ͦ͜Ͱ3PMFΛࢦఆɿ Just 3Ͱ͋Δ΂͖ʂ
  92. ͦ͜Ͱ3PMFT w ͜ͷ··Ͱ͸DPFSDFͰม׵ग़དྷͯ͠·͏ʂ type role Heap nominal ghci> h =

    fromList [1,2,3] :: Heap Int ghci> minView (coerce h :: Heap (Down Int)) Just 1 w ͦ͜Ͱ3PMFΛࢦఆɿ ghci> minView (coerce h :: Heap (Down Int)) error: Couldn't match type ‘Int’ with ‘Down Int’ Just 3Ͱ͋Δ΂͖ʂ
  93. ͦ͜Ͱ3PMFT w ͜ͷ··Ͱ͸DPFSDFͰม׵ग़དྷͯ͠·͏ʂ type role Heap nominal ghci> h =

    fromList [1,2,3] :: Heap Int ghci> minView (coerce h :: Heap (Down Int)) Just 1 w ͦ͜Ͱ3PMFΛࢦఆɿ ghci> minView (coerce h :: Heap (Down Int)) error: Couldn't match type ‘Int’ with ‘Down Int’ Just 3Ͱ͋Δ΂͖ʂ w σʔλߏஙࢠΛ࿐ग़͠ͳ͚Ε͹ɺଞͷϞδϡʔϧͰ͸OPNJOBMʹਪ࿦͞ΕΔഺ
  94. ͦ͜Ͱ3PMFT w ͜ͷ··Ͱ͸DPFSDFͰม׵ग़དྷͯ͠·͏ʂ type role Heap nominal ghci> h =

    fromList [1,2,3] :: Heap Int ghci> minView (coerce h :: Heap (Down Int)) Just 1 w ͦ͜Ͱ3PMFΛࢦఆɿ ghci> minView (coerce h :: Heap (Down Int)) error: Couldn't match type ‘Int’ with ‘Down Int’ Just 3Ͱ͋Δ΂͖ʂ w σʔλߏஙࢠΛ࿐ग़͠ͳ͚Ε͹ɺଞͷϞδϡʔϧͰ͸OPNJOBMʹਪ࿦͞ΕΔഺ w OFXUZQFߏஙࢠͷ৘ใΛJNQPSU͠ͳ͍ͱDPFSDFͰ͖ͳ͍
  95. 3PMFTৄࡉ

  96. 3PMFTৄࡉ w 3PMFʮ͜ͷܕม਺͸͜͏͍͏ౕͰ͢ʯ

  97. 3PMFTৄࡉ w 3PMFʮ͜ͷܕม਺͸͜͏͍͏ౕͰ͢ʯ w ࡾछɿrepresentational / nominal / phantom

  98. 3PMFTৄࡉ w 3PMFʮ͜ͷܕม਺͸͜͏͍͏ౕͰ͢ʯ w ࡾछɿrepresentational / nominal / phantom w

    SFQSʮ͜͜͸಺෦දݱ͕ಉ͡ͳΒಉ஋ʯ
  99. 3PMFTৄࡉ w 3PMFʮ͜ͷܕม਺͸͜͏͍͏ౕͰ͢ʯ w ࡾछɿrepresentational / nominal / phantom w

    SFQSʮ͜͜͸಺෦දݱ͕ಉ͡ͳΒಉ஋ʯ w OPNJOBMʮ׬શʹಉ͡ܕ͡Όͳ͍ͱͩΊʯ
  100. 3PMFTৄࡉ w 3PMFʮ͜ͷܕม਺͸͜͏͍͏ౕͰ͢ʯ w ࡾछɿrepresentational / nominal / phantom w

    SFQSʮ͜͜͸಺෦දݱ͕ಉ͡ͳΒಉ஋ʯ w OPNJOBMʮ׬શʹಉ͡ܕ͡Όͳ͍ͱͩΊʯ w QIBOUPNʮλμͷ০Γʂத਎ʹؔ܎ͳ͠ʯ
  101. 3PMFTৄࡉ w 3PMFʮ͜ͷܕม਺͸͜͏͍͏ౕͰ͢ʯ w ࡾछɿrepresentational / nominal / phantom w

    SFQSʮ͜͜͸಺෦දݱ͕ಉ͡ͳΒಉ஋ʯ w OPNJOBMʮ׬શʹಉ͡ܕ͡Όͳ͍ͱͩΊʯ w QIBOUPNʮλμͷ০Γʂத਎ʹؔ܎ͳ͠ʯ w ()$͸దٓ࠷΋ҰൠతͳSPMFΛਪ࿦ͯ͘͠ΕΔ
  102. 3PMFTৄࡉ w 3PMFʮ͜ͷܕม਺͸͜͏͍͏ౕͰ͢ʯ w ࡾछɿrepresentational / nominal / phantom w

    SFQSʮ͜͜͸಺෦දݱ͕ಉ͡ͳΒಉ஋ʯ w OPNJOBMʮ׬શʹಉ͡ܕ͡Όͳ͍ͱͩΊʯ w QIBOUPNʮλμͷ০Γʂத਎ʹؔ܎ͳ͠ʯ w ()$͸దٓ࠷΋ҰൠతͳSPMFΛਪ࿦ͯ͘͠ΕΔ w σʔλܕݻ༗ͷҙຯ࿦͸Ϣʔβ͔͠஌Βͳ͍ͷͰɺࠓճͷΑ ͏ͳέʔεͰ͸ϥΠϒϥϦ࣮૷ऀ͕ࢦఆ
  103. 3PMFTৄࡉ w 3PMFʮ͜ͷܕม਺͸͜͏͍͏ౕͰ͢ʯ w ࡾछɿrepresentational / nominal / phantom w

    SFQSʮ͜͜͸಺෦දݱ͕ಉ͡ͳΒಉ஋ʯ w OPNJOBMʮ׬શʹಉ͡ܕ͡Όͳ͍ͱͩΊʯ w QIBOUPNʮλμͷ০Γʂத਎ʹؔ܎ͳ͠ʯ w ()$͸దٓ࠷΋ҰൠతͳSPMFΛਪ࿦ͯ͘͠ΕΔ w σʔλܕݻ༗ͷҙຯ࿦͸Ϣʔβ͔͠஌Βͳ͍ͷͰɺࠓճͷΑ ͏ͳέʔεͰ͸ϥΠϒϥϦ࣮૷ऀ͕ࢦఆ w OFXUZQFߏஙࢠͷ৘ใ͕ͳ͍ͱcoerce͸ෆՄೳ
  104. $PFSDJPO3PMFT·ͱΊ w DPFSDFؔ਺Λ࢖͏ͱ಺෦දݱͷಉ͡ܕ͸ωε τ͍ͯͯ͠΋θϩίετͰม׵Մೳ w OFXUZQFͷར༻͕ΑΓ҆શɾ؆୯ʹʂ w ࡉ͔ͳม׵Մೳੑ͸3PMFΛࢦఆ੍ͯ͠ޚ w ීஈ͸3PMF͸ਪ࿦͞ΕΔͷͰ໰୊ͳ͠

    w ҙຯ࿦্ม׵͞ΕΔͱࠔΔʗม׵ग़དྷͯ΄͠ ͍΋ͷ͸ࣗ෼Ͱࢦఆ
  105. ͜Ε͜Εɺ
 ͜͏͍͏ͷཉ͔ͬͨ͠

  106. ͔ͯͳΜͰͳ͔ͬͨͷʁ

  107. ;FSP$PTU$PFSDJPOલ࢙ɿ (/%ͷड೉ w (FOFSBMJ[FE/FXUZQF%FSJWJOH (/% ͸()$ ͷࠒʹ͸طʹ͋ͬͨ w ͜ͷࠒ͸()$͸ܕγεςϜ͕߇͑໨Ͱɺશͯ ͸্ख͘ճ͍ͬͯͨ

    w ͦͷޙɺܕ଒΍("%5TͳͲ͕ೖΓʜʜ w ؾ෇͘ͱ(/%͸ෆ݈શʹͳ͍ͬͯͨʂ
  108. (/%͸ෆ݈શ newtype Id1 a = MkId1 a newtype Id2 a

    = MkId2 (Id1 a) deriving (UnsafeCast b) type family Discern a b type instance Discern (Id1 a) b = a type instance Discern (Id2 a) b = b class UnsafeCast to from where unsafe :: from ! Discern from to instance UnsafeCast b (Id1 a) where unsafe (MkId1 x) = x unsafeCoerce :: a ! b unsafeCoerce x = unsafe (MkId2 (MkId1 x))
  109. (/%͸ෆ݈શ newtype Id1 a = MkId1 a newtype Id2 a

    = MkId2 (Id1 a) deriving (UnsafeCast b) type family Discern a b type instance Discern (Id1 a) b = a type instance Discern (Id2 a) b = b class UnsafeCast to from where unsafe :: from ! Discern from to instance UnsafeCast b (Id1 a) where unsafe (MkId1 x) = x unsafeCoerce :: a ! b unsafeCoerce x = unsafe (MkId2 (MkId1 x)) (/%
  110. (/%͸ෆ݈શ newtype Id1 a = MkId1 a newtype Id2 a

    = MkId2 (Id1 a) deriving (UnsafeCast b) type family Discern a b type instance Discern (Id1 a) b = a type instance Discern (Id2 a) b = b class UnsafeCast to from where unsafe :: from ! Discern from to instance UnsafeCast b (Id1 a) where unsafe (MkId1 x) = x unsafeCoerce :: a ! b unsafeCoerce x = unsafe (MkId2 (MkId1 x)) (/% ೚ҙͷܕؒͷΩϟετʂ
  111. ͜Ε͸·͍ͣʜʜ

  112. (/%Λٹ͍͍ͨʜʜ

  113. ʜʜͱ͍͏ܦҢͰ 3PMF͕ݱΕͨ

  114. (/%͸ෆ݈શ newtype Id1 a = MkId1 a newtype Id2 a

    = MkId2 (Id1 a) deriving (UnsafeCast b) type family Discern a b type instance Discern (Id1 a) b = a type instance Discern (Id2 a) b = b class UnsafeCast to from where unsafe :: from ! Discern from to instance UnsafeCast b (Id1 a) where unsafe (MkId1 x) = x unsafeCoerce :: a ! b unsafeCoerce x = unsafe (MkId2 (MkId1 x))
  115. (/%͸ෆ݈શ newtype Id1 a = MkId1 a newtype Id2 a

    = MkId2 (Id1 a) deriving (UnsafeCast b) type family Discern a b type instance Discern (Id1 a) b = a type instance Discern (Id2 a) b = b class UnsafeCast to from where unsafe :: from ! Discern from to instance UnsafeCast b (Id1 a) where unsafe (MkId1 x) = x unsafeCoerce :: a ! b unsafeCoerce x = unsafe (MkId2 (MkId1 x)) (/%͸DPFSDF Λܦ༝͢ΔΑ͏ʹ
  116. (/%͸ෆ݈શ newtype Id1 a = MkId1 a newtype Id2 a

    = MkId2 (Id1 a) deriving (UnsafeCast b) type family Discern a b type instance Discern (Id1 a) b = a type instance Discern (Id2 a) b = b class UnsafeCast to from where unsafe :: from ! Discern from to instance UnsafeCast b (Id1 a) where unsafe (MkId1 x) = x unsafeCoerce :: a ! b unsafeCoerce x = unsafe (MkId2 (MkId1 x)) (/%͸DPFSDF Λܦ༝͢ΔΑ͏ʹ ()$͸ݡ͍ͷͰ BΛOPNJOBMʹਪ࿦
  117. (/%͸ෆ݈શ newtype Id1 a = MkId1 a newtype Id2 a

    = MkId2 (Id1 a) deriving (UnsafeCast b) type family Discern a b type instance Discern (Id1 a) b = a type instance Discern (Id2 a) b = b class UnsafeCast to from where unsafe :: from ! Discern from to instance UnsafeCast b (Id1 a) where unsafe (MkId1 x) = x unsafeCoerce :: a ! b unsafeCoerce x = unsafe (MkId2 (MkId1 x)) (/%͸DPFSDF Λܦ༝͢ΔΑ͏ʹ ()$͸ݡ͍ͷͰ BΛOPNJOBMʹਪ࿦ 3FKFDU
  118. 3PMFTͷొ৔Ͱ OFXUZQF͸
 ໾ׂΛՌͤΔΑ͏ʹ

  119. ໾ׂɿ

  120. ࣮૷ͷӅṭ

  121. ࣮૷ͷڞ༗

  122. ࣮૷ͷબ୒

  123. ͜Ε͕ݱࡏͷ
 ͍ · OFXUZQF

  124. ͔͜͜Β͸ ະདྷͷOFXUZQF ͷ࿩ ͋ͨ͠

  125. OFXUZQFͷະདྷ ·ͨ͸ɺ%FSJWJOH7JB ͋ͨ͠

  126. OFXUZQFͷະདྷ ·ͨ͸ɺ%FSJWJOH7JB ʙ࣮૷ͷڞ༗ͱબ୒͕Ͱ͋͏ͱ͖ʙ [2] Blöndal, Löh and Scott, 2018 ͋ͨ͠

  127. %FSJWJOH7JBΑΓॊೈͳ ࣮૷ͷڞ༗

  128. %FSJWJOH7JBΑΓॊೈͳ ࣮૷ͷڞ༗ w ()$͔ΒೖΔ৽ػೳ

  129. %FSJWJOH7JBΑΓॊೈͳ ࣮૷ͷڞ༗ w ()$͔ΒೖΔ৽ػೳ w ݱ࣌఺Ͱ()$BMQIB͕ग़͍ͯΔ

  130. %FSJWJOH7JBΑΓॊೈͳ ࣮૷ͷڞ༗ w ()$͔ΒೖΔ৽ػೳ w ݱ࣌఺Ͱ()$BMQIB͕ग़͍ͯΔ w OFXUZQFΛEFSJWJOHઅͷώϯτͱͯ͠༻͍Δ ͜ͱ͕Ͱ͖ΔΑ͏ʹͳΔ

  131. ྫɿ*EͷϞϊΠυߏ଄ {-# LANGUAGE DerivingVia #-} newtype Id = MkId Word

    deriving (Semigroup, Monoid) via Max Word
  132. ྫɿ*EͷϞϊΠυߏ଄ w લʹݟͨΑ͏ʹɺ8PSEͷϞϊΠυ࣮૷͸ෳ਺͋Δ {-# LANGUAGE DerivingVia #-} newtype Id =

    MkId Word deriving (Semigroup, Monoid) via Max Word
  133. ྫɿ*EͷϞϊΠυߏ଄ w લʹݟͨΑ͏ʹɺ8PSEͷϞϊΠυ࣮૷͸ෳ਺͋Δ w *EͰ͸ʮҰ൪৽͍͠*E ࠷େͷ*E ΛબͿʯԋࢉΛϞϊΠυԋ ࢉͱͯ͠࢖͍͔ͨͬͨͱ͢Δ {-# LANGUAGE

    DerivingVia #-} newtype Id = MkId Word deriving (Semigroup, Monoid) via Max Word
  134. ྫɿ*EͷϞϊΠυߏ଄ w લʹݟͨΑ͏ʹɺ8PSEͷϞϊΠυ࣮૷͸ෳ਺͋Δ w *EͰ͸ʮҰ൪৽͍͠*E ࠷େͷ*E ΛબͿʯԋࢉΛϞϊΠυԋ ࢉͱͯ͠࢖͍͔ͨͬͨͱ͢Δ w Max

    Word͸IdͱಉҰදݱɺ NBY  ʹؔ͠ϞϊΠυ {-# LANGUAGE DerivingVia #-} newtype Id = MkId Word deriving (Semigroup, Monoid) via Max Word
  135. ྫɿ*EͷϞϊΠυߏ଄ w લʹݟͨΑ͏ʹɺ8PSEͷϞϊΠυ࣮૷͸ෳ਺͋Δ w *EͰ͸ʮҰ൪৽͍͠*E ࠷େͷ*E ΛબͿʯԋࢉΛϞϊΠυԋ ࢉͱͯ͠࢖͍͔ͨͬͨͱ͢Δ w Max

    Word͸IdͱಉҰදݱɺ NBY  ʹؔ͠ϞϊΠυ w %FSJWJOH7JB͸͜ͷ࣮૷Λࣗಈతʹ*Eʹ্࣋ͪ͛ͯ͘ΕΔʂ {-# LANGUAGE DerivingVia #-} newtype Id = MkId Word deriving (Semigroup, Monoid) via Max Word
  136. %FSJWJOH7JBWT(/%

  137. %FSJWJOH7JBWT(/% w %FSJWJOH7JB͸(/%ͷεʔύʔηοτ

  138. %FSJWJOH7JBWT(/% w %FSJWJOH7JB͸(/%ͷεʔύʔηοτ w (/%͸แ·ΕΔҰ൪಺ଆͷܕͷ࣮૷ΛݟΔ

  139. %FSJWJOH7JBWT(/% w %FSJWJOH7JB͸(/%ͷεʔύʔηοτ w (/%͸แ·ΕΔҰ൪಺ଆͷܕͷ࣮૷ΛݟΔ w %FSJWJOH7JB͸$PFSDJCMFͳ೚ҙͷܕͷ࣮ ૷ΛɺίετθϩͰ࠶ར༻͢Δ͜ͱ͕Ͱ͖Δʂ

  140. %FSJWJOH7JBͷࣹఔ͸ OFXUZQFʹݶΒͳ͍

  141. %FSJWJOH7JBͷࣹఔ͸ OFXUZQFʹݶΒͳ͍ w ݪ࿦จͰ͸೚ҙͷಉܕͳܕͷؒͰ࣮૷Λڞ༗ ͢Δํ๏΋ఏҊ͞Ε͍ͯΔ ˞͜ͷlಉܕz͸ਖ਼֬ʹ͸ʮ૯শతͳදݱ͕$PFSDJCMFʯͱ͍͏΍΍ڧ͍৚݅

  142. %FSJWJOH7JBͷࣹఔ͸ OFXUZQFʹݶΒͳ͍ w ݪ࿦จͰ͸೚ҙͷಉܕͳܕͷؒͰ࣮૷Λڞ༗ ͢Δํ๏΋ఏҊ͞Ε͍ͯΔ ૯শϓϩάϥϛϯάͱ$PFSDJPOͷ߹Θٕͤ ˞͜ͷlಉܕz͸ਖ਼֬ʹ͸ʮ૯শతͳදݱ͕$PFSDJCMFʯͱ͍͏΍΍ڧ͍৚݅

  143. %FNP +40/ม׵Πϯελϯεͷ੩తఆٛ ಉܕͳܕͷؒͰͷ࣮૷ͷڞ༗ ˞$PNQMFUFDPEFJTBWBJMBCMFBUhttp://bit.ly/derivia

  144. ෳࡶͳྫ w ܕϨϕϧͰΤϯίʔυͷ࢓༷Λࢦఆ w 'SPN+40/5P+40/Ͱ࢓༷͕ڞ༗͞ΕΔ͜ͱΛ੩తʹอূ data OtherConfig = OtherConfig {

    otrNameOfProcess :: Maybe String , otrArgsToProcess :: [String] } deriving (Read, Show, Eq, Ord, Generic) deriving (ToJSON, FromJSON) via WithOptions '[ FieldLabelModifier '[CamelTo2 "-"] , ConstructorTagModifier '[CamelTo2 "-"] , OmitNothingFields 'True ] OtherConfig
  145. ಉܕͷྫ data Blog = Blog { authors :: [Author] ,

    articles :: [Article] } deriving (Generic) deriving (Semigroup, Monoid) via Blog `SameRepAs` ([Author], Dual [Article]) {- ghci> mconcat [Blog ["1"] ["1"], Blog ["2"] ["3","4"]] Blog {authors = ["1","2"], articles = ["3","4","1"]} -}
  146. ಉܕͷྫ DPOU newtype SameRepAs a b = SameRepAs { runSameRepAs

    :: a } type Iso a b = (Generic a, Generic b, Coercible (Rep a ()) (Rep b ())) instance (Semigroup b, Iso a b) 㱺 Semigroup (SameRepAs a b) where SameRepAs a <> SameRepAs b = ... instance (Monoid b, Iso a b) 㱺 Monoid (SameRepAs a b) where mempty = SameRepAs $ toA mempty where toA :: b -> a toA = to . (coerce :: Rep b () -> Rep a ()) . from
  147. %FSJWJOH7JB·ͱΊ

  148. %FSJWJOH7JB·ͱΊ w ()$͔Βར༻Մೳ

  149. %FSJWJOH7JB·ͱΊ w ()$͔Βར༻Մೳ w OFXUZQFΛ࢖ͬͯEFSJWJOHʹ࢖͏࣮૷Λબ୒ ग़དྷΔ

  150. %FSJWJOH7JB·ͱΊ w ()$͔Βར༻Մೳ w OFXUZQFΛ࢖ͬͯEFSJWJOHʹ࢖͏࣮૷Λબ୒ ग़དྷΔ w DPFSDFͱ(FOFSJDTͱ૊Έ߹ΘͤΕ͹ɺදݱ͕ ಉ͡ͱ͸ݶΒͳ͍͕ಉܕͳܕͷ࣮૷΋ಋग़Մೳ

  151. %FSJWJOH7JB·ͱΊ w ()$͔Βར༻Մೳ w OFXUZQFΛ࢖ͬͯEFSJWJOHʹ࢖͏࣮૷Λબ୒ ग़དྷΔ w DPFSDFͱ(FOFSJDTͱ૊Έ߹ΘͤΕ͹ɺදݱ͕ ಉ͡ͱ͸ݶΒͳ͍͕ಉܕͳܕͷ࣮૷΋ಋग़Մೳ w

    ੍໿ͰදݱͰ͖ΔಉܕͳΒଞʹ΋ద༻Մೳ
  152. ·ͱΊ

  153. ·ͱΊ

  154. ·ͱΊ w OFXUZQFͷ༻్͸ࡾͭ

  155. ·ͱΊ w OFXUZQFͷ༻్͸ࡾͭ w ࣮૷ͷӅṭʗ࣮૷ͷڞ༗ʗ࣮૷ͷબ୒

  156. ·ͱΊ w OFXUZQFͷ༻్͸ࡾͭ w ࣮૷ͷӅṭʗ࣮૷ͷڞ༗ʗ࣮૷ͷબ୒ w %BUB$PFSDFΛ࢖͏ͱ಺෦දݱ͕ಉ͡ܕಉ࢜Λθϩ ίετͰΩϟετՄೳ

  157. ·ͱΊ w OFXUZQFͷ༻్͸ࡾͭ w ࣮૷ͷӅṭʗ࣮૷ͷڞ༗ʗ࣮૷ͷબ୒ w %BUB$PFSDFΛ࢖͏ͱ಺෦දݱ͕ಉ͡ܕಉ࢜Λθϩ ίετͰΩϟετՄೳ w OFXUZQFͷग़ੈ͸͔͜͜Β࢝·ͬͨ

  158. ·ͱΊ w OFXUZQFͷ༻్͸ࡾͭ w ࣮૷ͷӅṭʗ࣮૷ͷڞ༗ʗ࣮૷ͷબ୒ w %BUB$PFSDFΛ࢖͏ͱ಺෦දݱ͕ಉ͡ܕಉ࢜Λθϩ ίετͰΩϟετՄೳ w OFXUZQFͷग़ੈ͸͔͜͜Β࢝·ͬͨ

    w ෳ߹ܕ΋3PMFਪ࿦ɾḼऍͰద੾ʹѻ͑Δ
  159. ·ͱΊ w OFXUZQFͷ༻్͸ࡾͭ w ࣮૷ͷӅṭʗ࣮૷ͷڞ༗ʗ࣮૷ͷબ୒ w %BUB$PFSDFΛ࢖͏ͱ಺෦දݱ͕ಉ͡ܕಉ࢜Λθϩ ίετͰΩϟετՄೳ w OFXUZQFͷग़ੈ͸͔͜͜Β࢝·ͬͨ

    w ෳ߹ܕ΋3PMFਪ࿦ɾḼऍͰద੾ʹѻ͑Δ w ()$͔Β%FSJWJOH7JBͰOFXUZQFʹΑΓ
 ಋग़અΛΧελϚΠζՄೳʹʂ
  160. ࢀߟจݙ 1. J. Breitner, R. A. Eisenberg, S. P. Jones

    and S. Weirich, Safe Zero-cost Coercions for Haskell, ICFP 2014. 2. Baldur Blöndal, Andres Löh and Ryan Scott, Deriving Via: How to Turn Hand-Written Instances into an Anti-Pattern, ICFP18.
  161. ޚਗ਼ௌ
 ͋Γ͕ͱ͏͍͟͝·ͨ͠

  162. "OZ2VFTUJPO w OFXUZQFͷ༻్͸ࡾͭ w ࣮૷ͷӅṭʗ࣮૷ͷڞ༗ʗ࣮૷ͷબ୒ w %BUB$PFSDFΛ࢖͏ͱ಺෦දݱ͕ಉ͡ܕಉ࢜Λθϩ ίετͰΩϟετՄೳ w OFXUZQFͷग़ੈ͸͔͜͜Β࢝·ͬͨ

    w ෳ߹ܕ΋3PMFਪ࿦ɾḼऍͰద੾ʹѻ͑Δ w ()$͔Β%FSJWJOH7JBͰOFXUZQFʹΑΓ
 ಋग़અΛΧελϚΠζՄೳʹʂ
  163. ͓·͚

  164. ͳͥ.POPJEͱ'PMEBCMF͸
 ஥͕͍͍ͷ͔ʁ w 'PMEBCMFͷ࠷খఆٛ w ࣮͸͜Ε͸ࣗ༝ϞϊΠυͷීวੑͷҰ෦͕༝དྷ class Foldable t where

    foldMap :: Monoid m 㱺 (a ! m) ! t a ! m
  165. 7 ↵ M ↵ ' [ · ] 9! 7QH/JT

    ' <latexit sha1_base64="tdNbhyHmLM0k2MhCNVTaF9VDoQE=">AAAHwnicjVRNj9xEEK0dPiYMkGSBAxKXhk2iRHIGT0gEAs1qBQhxiZQPNok0HlYeu2fHGdtt3D1f6/WBK3+AAyeQEEL8DC78AQ75CYhjkLhw4HXZm9ld7c7G1tjdVe9Vvarq8SCLI21c98la44UXX3q5ee6V1quvvX7+wsX1Nx5oNckDuR2oWOWPBr6WcZTKbROZWD7Kcukng1g+HIw/s/6HU5nrSKVfmUUm+4m/m0bDKPANTDvra794A7kbpYWJxntZFJhJLsuWl/gmj+aiV7/VUGA1EqkKpXYE0k6SVGiZdTvtW0HiiFzNqm2Q9EXREmL/6he5lGJ4bV8UNtrImGJYesLz42zkl+IKEIl13i49j/EVtHbDVn7S8jIQe9c3nTD3Z30BGFBChruSlfT0zM8cf2KUM1Sp6Xo6yKPM6GhPQsQlb+rn2Si6VApkAtdeS+6SVhzilZbYg8wgVMYTfUuuC7HZ6yWH6YW+HsmwfzzcMRVfF56cY476XVRlG6GyZUNUHN72s7JE4ZXYslKL0mUaHpnJzsWNTtvlS5y+2Nh6i/i6o9Yb35JHISk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υͳΒ؆୯ʹॻ͚Δ class (Pointed f, Foldable f,

    ∀a. Monoid (f a)) 㱺 FreeMonoid f instance (Pointed f, Foldable f, ∀a. Monoid (f a)) 㱺 FreeMonoid f fold :: (FreeMonoid f) 㱺 (a ! b ! b) ! b ! f a ! b fold g n xs = appEndo (foldMap (Endo . g) xs) n traverseF :: (FreeMonoid f, Applicative t) 㱺 (a ! t b) ! f a ! t (f b) traverseF f = fold (λa tb ! (<>) <$> (iota <$> f a) <*> tb) (pure mempty) ͔ΒೖΔ QuantifiedConstraints $PNQMFUFDPEFɿsrc/Data/Foldable/Monoid.hs