2017-02-21 18:44:48 +03:00
|
|
|
|
{-# LANGUAGE GADTs, RankNTypes #-}
|
2015-11-18 03:20:52 +03:00
|
|
|
|
module Algorithm where
|
2015-11-18 03:24:01 +03:00
|
|
|
|
|
2016-09-25 09:27:59 +03:00
|
|
|
|
import Control.Applicative.Free
|
|
|
|
|
import Prologue hiding (Pure)
|
2016-08-04 01:12:31 +03:00
|
|
|
|
|
2017-02-21 18:26:27 +03:00
|
|
|
|
-- | A single step in a diffing algorithm, parameterized by the types of terms, diffs, and the result of the applicable algorithm.
|
2017-02-21 18:25:25 +03:00
|
|
|
|
data AlgorithmF term diff result where
|
2017-02-21 18:38:10 +03:00
|
|
|
|
-- | Diff two terms recursively in O(n) time, resulting in a single diff node.
|
2017-02-21 18:46:19 +03:00
|
|
|
|
Linear :: term -> term -> AlgorithmF term diff diff
|
2017-02-21 18:38:10 +03:00
|
|
|
|
-- | Diff two lists of terms by each element’s position in O(n³) time, resulting in a list of diffs.
|
2017-02-21 18:46:19 +03:00
|
|
|
|
SES :: [term] -> [term] -> AlgorithmF term diff [diff]
|
2017-02-21 18:38:10 +03:00
|
|
|
|
-- | Diff two lists of terms by each element’s similarity in O(n³ log n), resulting in a list of diffs.
|
2017-02-21 18:46:19 +03:00
|
|
|
|
RWS :: [term] -> [term] -> AlgorithmF term diff [diff]
|
2015-11-18 03:24:01 +03:00
|
|
|
|
|
2016-09-25 09:50:49 +03:00
|
|
|
|
-- | The free applicative for 'AlgorithmF'. This enables us to construct diff values using <$> and <*> notation.
|
2016-09-25 09:27:59 +03:00
|
|
|
|
type Algorithm term diff = Ap (AlgorithmF term diff)
|
|
|
|
|
|
2016-09-25 10:22:14 +03:00
|
|
|
|
-- | Tear down an Ap by iteration.
|
2016-09-25 10:21:40 +03:00
|
|
|
|
iterAp :: Functor g => (g a -> a) -> Ap g a -> a
|
|
|
|
|
iterAp algebra = go
|
2016-09-25 09:27:59 +03:00
|
|
|
|
where go (Pure a) = a
|
2016-09-25 11:10:14 +03:00
|
|
|
|
go (Ap underlying apply) = algebra (go . (apply <*>) . pure <$> underlying)
|
2016-08-04 01:15:18 +03:00
|
|
|
|
|
2017-02-21 18:44:48 +03:00
|
|
|
|
iterAp' :: (forall x. g x -> (x -> a) -> a) -> Ap g a -> a
|
|
|
|
|
iterAp' algebra = go
|
|
|
|
|
where go (Pure a) = a
|
|
|
|
|
go (Ap underlying apply) = algebra underlying (go . (apply <*>) . pure)
|
|
|
|
|
|
2016-08-04 19:08:34 +03:00
|
|
|
|
|
|
|
|
|
-- DSL
|
|
|
|
|
|
2016-08-04 19:08:18 +03:00
|
|
|
|
-- | Constructs a 'Recursive' diff of two terms.
|
2017-02-21 18:46:19 +03:00
|
|
|
|
linearly :: term -> term -> Algorithm term diff diff
|
|
|
|
|
linearly a b = liftAp (Linear a b)
|
2016-08-04 01:38:35 +03:00
|
|
|
|
|
2016-08-04 19:08:18 +03:00
|
|
|
|
-- | Constructs a 'ByIndex' diff of two lists of terms.
|
2017-02-21 18:46:19 +03:00
|
|
|
|
bySES :: [term] -> [term] -> Algorithm term diff [diff]
|
|
|
|
|
bySES a b = liftAp (SES a b)
|
2016-08-04 01:40:17 +03:00
|
|
|
|
|
2016-08-04 19:08:18 +03:00
|
|
|
|
-- | Constructs a 'BySimilarity' diff of two lists of terms.
|
2017-02-21 18:46:19 +03:00
|
|
|
|
byRWS :: [term] -> [term] -> Algorithm term diff [diff]
|
|
|
|
|
byRWS a b = liftAp (RWS a b)
|