> {-# LANGUAGE StandaloneDeriving, > TypeSynonymInstances, > FlexibleInstances #-} > import Control.Exception > import Data.Foldable > import Data.List hiding (insert) > import Data.Map (Map) > import qualified Data.Map as Map > import Data.Set (Set) > import qualified Data.Set as Set > import Data.Sort ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A Case Study in Deforestation Diff-ing (and more) in Xitou Pierre-Évariste Dagand CNRS - Inria - Sorbonne Université ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sorting with patience > input1 :: [Int] > input1 = [5,3,4,9,6,2,1,8,5,7,10,7] > weigh1 = [1,1,1,1,1,1,1,1,3,1,1,3] yields < 5{1} 4{2} 9{3} 8{4} < 3{1} 6{3} | < 2{1} 5{5} < 1{1} 7{6} 10{7} < 7{8} ---------------------------------------- - Cover data-structure ---------------------------------------- > data Elem w v = Elem { value :: v > , weight :: w > , prev :: Maybe (Elem w v) } > newtype RTree a = Node a > type RCover a = Set (RTree a) > newtype Cover w v = Cover (RCover (Elem w v)) > emptyCover :: Cover w v > emptyCover = Cover Set.empty > peek :: RTree a -> a > peek (Node a) = a > covers :: a -> RCover a -> RTree a > a `covers` cs = Node a ---------------------------------------- - Single symbol insertion ---------------------------------------- > instance Ord a => Ord (RTree a) where > compare t1 t2 = compare (peek t1) (peek t2) > instance (Ord v, Ord w) => Ord (Elem w v) where > compare x y = > case compare (value x) (value y) of > EQ -> compare (weight x) (weight y) > c -> c Time complexity: O(log(n)) > insert :: (Ord v, Ord w, Num w) => Cover w v -> (w, v) -> Cover w v > insert (Cover c) (w, v) = > Cover (Set.union prefix > (Set.insert (Elem v wp prev `covers` subsumed) remainder)) > where (prefix, postfix) = Set.spanAntitone below c > below cs = value (peek cs) < v > (wp, prev) = case Set.lookupMax prefix of > Nothing -> (w, Nothing) > Just e -> (weight (peek e) + w, Just (peek e)) > (subsumed, remainder) = minViewWhile isLighter postfix > isLighter cs = weight (peek cs) <= wp < minViewWhile :: Ord a => (a -> Bool) -> Set a -> (Set a, Set a) ---------------------------------------------------------------- - The Algorithm ---------------------------------------------------------------- Time complexity: O(n log(n)) > greedy :: (Ord v, Ord w, Num w) => [w] -> [v] -> Cover w v > greedy ws vs = foldl' insert emptyCover $ zip ws vs > extract :: (Ord v, Ord w) => Cover w v -> [v] > extract (Cover cs) | Set.size cs == 0 = [] > extract (Cover cs) = unravel [] m > where h : hds = peek <$> Set.toAscList cs > m = foldr maxWeight h hds > maxWeight x y = > if weight x < weight y then y > else if value x < value y then y > else x > unravel acc e = > case prev e of > Nothing -> value e : acc > Just e' -> unravel (value e : acc) e' > his :: (Ord v, Ord w, Num w) => [w] -> [v] -> [v] > his ws vs = extract $ greedy ws vs > {-# INLINE [0] weight1 #-} > {-# INLINE [0] greedy #-} > {-# RULES > "greedy/weight1" [1] > forall vs. greedy (map weight1 vs) vs > = foldl' (\ c v -> insert c (v, 1)) emptyCover vs #-} > weight1 _ = 1 > lis :: Ord v => [v] -> [v] > lis vs = extract $ greedy (weight1 <$> vs) vs ---------------------------------------- - Diff-ing with LIS ---------------------------------------- > input2, input3 :: [Int] > input2 = [5,6,5,7,9] > input3 = [6,5,5,6,7,5] > input23 :: [(Int, Int)] > input23 = [(0,5),(0,2),(0,1), > (1,3),(1,0), > (2,5),(2,2),(2,1), > (3,4)] yields < (0, 5) (1, 3) (2, 5) < (0, 2) (2, 2) (3, 4) < (0, 1) (2, 1) < (1, 0) ---------------------------------------- - Matching sequences ---------------------------------------- > data Bi = Bi Int Int > instance Ord Bi where > compare (Bi _ j1) (Bi _ j2) = compare j1 j2 Time complexity: O(n log(n)) > matchlist :: Ord v => [v] -> [v] -> [Bi] > matchlist vs ws = concat $ extract <$> vsIx > where wsIx = contract ws > vsIx = zip vs [0..] > extract (v, i) = Bi i <$> Map.findWithDefault [] v wsIx > contract :: Ord v => [v] -> Map v [Int] > contract vs = help 0 vs Map.empty > where help i [] m = m > help i (v : vs) m = > help (i+1) vs $ > Map.alter (maybe (Just [i]) (Just . ((:) i))) v m > r :: Ord v => [v] -> [v] -> Int > r vs1 vs2 = length $ matchlist vs1 vs2 Remark: r vs1 vs2 = length vs1 * length vs2 in the worst case [2 strings of a single char] ---------------------------------------- - The Algorithm, II ---------------------------------------- > weightBi :: (Int -> Int -> w) -> [Bi] -> [w] > weightBi w = fmap $ \ (Bi i j) -> w i j Time complexity: O(n log(n) + r(vs1, vs2) log(r(vs1, vs2))) ~~> could strive for: O((n + r(vs1, vs2)) log(n)) TODO? implement `shortestPrefix vs1 vs2 i k` satisfying shortestPrefix vs1 vs2 i k = +\infty if \mathcal{E} = \empty shortestPrefix vs1 vs2 i k = min \mathcal{E} otherwise where \mathcal{E} = { j \in [0, l] | |LCS(vs1[0, i[, vs2[0,j[| = k } Time complexity: O((r vs1 vs2) log(n)) TODO? show that LCS can be implemented with `shortestPrefix` > greedyCommon :: (Ord v, Ord w, Num w) => > (Int -> Int -> w) -> [v] -> [v] -> Cover w Bi > greedyCommon w vs1 vs2 = greedy (weightBi w bi) bi > where bi = matchlist vs1 vs2 > hcs :: (Ord v, Ord w, Num w) => > (Int -> Int -> w) -> [v] -> [v] -> [v] > hcs w vs1 vs2 = get vs1 vs2 <$> (extract $ greedyCommon w vs1 vs2) > where get :: Eq v => [v] -> [v] -> Bi -> v > get vs1 vs2 (Bi i j) = > assert (vs1 !! i == vs2 !! j) $ > vs1 !! i > {-# INLINE [0] weightBi #-} > {-# INLINE [0] weightBi1 #-} > {-# RULES > "greedy/weightBi1" [1] > forall bi. greedy (weightBi weightBi1 bi) bi > = foldl' (\ c v -> insert c (v, 1)) emptyCover bi #-} > weightBi1 x y = 1 > lcs :: Ord v => [v] -> [v] -> [v] > lcs = hcs weightBi1 ---------------------------------------------------------------- - Boring stuff: ---------------------------------------------------------------- > main = do > putStrLn . show $ greedyCommon (const (const 1)) input1 input2 > putStrLn . show $ lcs input1 input2 > instance (Show w, Show v) => Show (Elem w v) where > show e = show (value e) ++ "{" ++ show (weight e) ++ "}" > deriving instance Show a => Show (RTree a) > instance {-# OVERLAPS #-} Show a => Show (RCover a) where > show s = show $ Set.toAscList s > deriving instance (Show w, Show v) => Show (Cover w v) > instance (Eq w, Eq v) => Eq (Elem w v) where > x == y = value x == value y && weight x == weight y > instance Eq a => Eq (RTree a) where > t1 == t2 = peek t1 == peek t2 > instance Foldable RTree where > foldMap f (Node a) = f a > instance Foldable (Cover w) where > foldMap f (Cover cs) = foldMap (foldMap (f . value)) cs > minViewWhile :: Ord a => (a -> Bool) -> Set a -> (Set a, Set a) > minViewWhile p s = help s Set.empty > where help s mins > | Nothing <- Set.minView s = (mins, Set.empty) > | Just (m, s') <- Set.minView s = > if p m then help s' (Set.insert m mins) > else (mins, s) > instance Show Bi where > show (Bi i j) = show (i, j) > instance Eq Bi where > (Bi _ j1) == (Bi _ j2) = j1 == j2