Recursion Schemesで考える並べ替えアルゴリズム

315791eefd7b239aaae562274dfc0e75?s=47 lotz
November 29, 2019

Recursion Schemesで考える並べ替えアルゴリズム

315791eefd7b239aaae562274dfc0e75?s=128

lotz

November 29, 2019
Tweet

Transcript

  1. Recursion SchemesͰߟ͑Δ ฒ΂ସ͑ΞϧΰϦζϜ 2019/11/29 by lotz at Gotanda.hs

  2. ࣗݾ঺հ • HaskellͱػցֶशΛ
 झຯͰ΍͍ͬͯΔऀͰ͢ • ීஈ͸൒ଂ໳Ͱಇ͘ΤϯδχΞ • Twitter: @lotz84_
 ࣭໰͕͋Ε͹ͪ͜Β·Ͱ

    • GitHub: @lotz84
  3. ฒ΂ସ͑ΞϧΰϦζϜ

  4. όϒϧιʔτ [1, 6, 5, 2] [1, 6, 5, 2] [1,

    5, 6, 2] [1, 5, 2, 6] [1, 5, 2, 6] [1, 5, 2, 6] [1, 2, 5, 6] [1, 2, 5, 6] ೖΕସ͑ ೖΕସ͑ ೖΕସ͑
  5. ૠೖιʔτ [] [1, 6, 5, 2] [1] [6, 5, 2]

    [1, 6] [5, 2] [1, 5, 6] [2] [1, 2, 5, 6] []
  6. ૠೖιʔτ - ࣮૷ insertSort :: [Int] -> [Int] insertSort xs

    = go [] xs where go ys [] = ys go ys (x:xs) = go (insert x ys) xs insert :: Int -> [Int] -> [Int] insert x ys = xs ++ [x] ++ zs where (xs, zs) = span (<= x) ys
  7. ૠೖιʔτ - foldr foldr :: (a -> b -> b)

    -> b -> [a] -> b foldr _ z [] = z foldr k z (y:ys) = y `k` foldr k z ys insertSort :: [Int] -> [Int] insertSort = foldr insert [] ࣮૷ͷ࠶ؼతͳߏ଄ΛGPMESΛ࢖ͬͯ෼཭ 3FDVSTJPO4DIFNFT
  8. Recursion Schemes ࠶ؼతͳߏ଄Λ෼཭͢Δ

  9. ࠶ؼతߏ଄͸Ͳ͔͜ΒདྷΔͷ͔ʁ data [] a = [] | a : [a]

    ͦ΋ͦ΋ܕͷఆ͕ٛ࠶ؼత
  10. data [] a = [] | a : [a] ࠶ؼతͰ͸ͳ͍ܕ

    data List r = Nil | Cons Int r ࠶ؼతͳߏ଄Λ࣋ͭ൚༻తͳܕ newtype Fix f = In {out :: f (Fix f)} ͋ͱͰܕΫϥε੍໿Λॻ͘ྔΛݮΒͨ͢Ί *OUͷ৔߹ʹݶఆ͠·͢ ࠶ؼతͳܕΛ෼ղ͢Δ
  11. Fix List ͸ [Int] ͱ ಉܕ toList :: Fix List

    -> [Int] toList (In Nil) = [] toList (In (Cons x xs)) = x : toList xs fromList :: [Int] -> Fix List fromList [] = In Nil fromList (x:xs) = In $ Cons x (fromList xs)
  12. Fix f f (Fix f) In out

  13. Catamorphism cata :: Functor f => (f a -> a)

    -> Fix f -> a cata g = g . fmap (cata g) . out cata g fmap (cata g) g
  14. Anamorphism ana :: Functor f => (a -> f a)

    -> a -> Fix f ana g = In . fmap (ana g) . g g ana g fmap (ana g)
  15. ฒ΂ସ͑ΞϧΰϦζϜͷܕ ιʔτࡁΈͷϦετΛද͢ܕ data SList r = Nil' | Cons' Int

    r sort :: Fix List -> Fix SList (List a -> a) -> Fix List -> a (a -> SList a) -> a -> Fix SList
  16. Fix List ʹ஫໨ͨ͠৔߹ sort :: Fix List -> Fix SList

    sort = cata (? :: List (Fix SList) -> Fix SList) = cata $ ana (?? :: List (Fix SList) -> SList (List (Fix SList))) = cata $ ana (??? . fmap out) ??? :: List (SList (Fix SList)) -> SList (List (Fix SList))
  17. Fix SList ʹ஫໨ͨ͠৔߹ sort :: Fix List -> Fix SList

    sort = ana (? :: Fix List -> SList (Fix List)) = ana $ cata (?? :: List (SList (Fix List)) -> SList (Fix List)) = ana $ cata (fmap In . ???) ??? :: List (SList (Fix List)) -> SList (List (Fix List))
  18. ෼഑๏ଇ swap :: List (SList x) -> SList (List x)

    swap Nil = Nil' swap (Cons a Nil') = Cons' a Nil swap (Cons a (Cons' b x)) | a <= b = Cons' a (Cons b x) | otherwise = Cons' b (Cons a x)
  19. 2ͭͷฒ΂ସ͑ΞϧΰϦζϜ > toList . cata (ana (swap . fmap out))

    . fromList $ [1,6,5,2] [1,2,5,6] > toList . ana (cata (fmap In . swap)) . fromList $ [1,6,5,2] [1,2,5,6]
  20. cata (ana (swap . fmap out)) (In (Cons 1 (In

    (Cons 6 (In (Cons 5 (In (Cons 2 (In Nil)…) ana f (Cons 1 (ana f (Cons 6 (ana f (Cons 5 (ana f (Cons 2 (ana f Nil)…) ana f (Cons 1 (ana f (Cons 6 (ana f (Cons 5 (In (Cons’ 2 (In Nil’))…) cata - ana - swap DBUB͸શͯͷ*OΛஔ͖׵͑Δ ͜͜·ͰධՁ͢Δ ͜Ε͸ιʔτࡁΈͷϦετ
  21. ana f (Cons 1 (ana f (Cons 6 (ana (swap

    . fmap out) (Cons 5 (In (Cons’ 2 (In Nil’))…) ana f (Cons 1 (ana f (Cons 6 (In . fmap (ana f) $ swap (Cons 5 (Cons’ 2 (In Nil’))…) ana f (Cons 1 (ana f (Cons 6 (In (Cons’ 2 (ana f (Cons 5 (In Nil’))…) ͜Ε͸ૠೖιʔτʂ˞ ల։ͯ͠GNBQPVUΛద༻͢Δ Ұͭ಺ଆʹBOBGΛ࢓ࠐΜͰશମΛ*OͰแΉ
  22. ana - cata - swap ana (cata (fmap In .

    swap)) (In (Cons 1 (In (Cons 6 (In (Cons 5 (In (Cons 2 (In Nil)…) In . fmap (ana f) . cata (fmap In . swap) $ (In (Cons 1 (In (Cons 6 (In (Cons 5 (In (Cons 2 (In Nil)…) In . fmap (ana f) $ g (Cons 1 (g (Cons 6 (g (Cons 5 (g (Cons 2 (g Nil)…) In . fmap (ana f) $ g (Cons 1 (g (Cons 6 (g (Cons 5 (Cons’ 2 (In Nil)…) BOBΛల։͢Δ DBUB͸શͯͷ*OΛஔ͖׵͑Δ ͜͜·ͰධՁ͢Δ
  23. In . fmap (ana f) $ g (Cons 1 (g

    (Cons 6 ((fmap In . swap) (Cons 5 (Cons’ 2 (In Nil)…) In . fmap (ana f) $ g (Cons 1 (g (Cons 6 (In (Cons’ 2 (In (Cons 5 (In Nil)…) In . fmap (ana f) $ Cons’ 1 (In (Cons 2 (In (Cons 6 (In (Cons 5 (In Nil)…) In (Cons’ 1 (ana (cata (fmap In . swap)) (In (Cons 2 (In (Cons 6 (In (Cons 5 (In Nil)...) ͜Ε͸όϒϧιʔτʂ ͜Ε͸ιʔτࡁΈͷϦετ
  24. ૠೖιʔτ※ ͱ όϒϧιʔτ͸ ૒ରʂʂ ˞ͨͩ͠φΠʔϒͳૠೖιʔτ

  25. ·ͱΊ • Recursion SchemesͰߟ͑Δͱ
 φΠʔϒͳૠೖιʔτͱόϒϧιʔτ͸૒ର • ࣮͸͞ΒʹPara / ApoΛ༻͍Δͱ
 ૠೖιʔτͱબ୒ιʔτͷ૒ରؔ܎΋෼͔Δ

    • ৄ͘͠͸
 Ralf Hinze, et al; “A Duality of Sorts”, 2013.
  26. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠