1
1
mirror of https://github.com/github/semantic.git synced 2024-12-25 16:02:43 +03:00
semantic/src/Algorithm.hs

36 lines
1.4 KiB
Haskell
Raw Normal View History

2015-11-18 03:20:52 +03:00
module Algorithm where
2015-11-18 03:24:01 +03:00
import Control.Monad.Free.Church
2016-08-04 01:12:31 +03:00
import Prologue
-- | A single step in a diffing algorithm.
data AlgorithmF
term -- ^ The type of terms.
diff -- ^ The type of diffs.
2016-08-04 01:12:31 +03:00
f -- ^ The type representing another level of the diffing algorithm. Often Algorithm.
-- | Recursively diff two terms and pass the result to the continuation.
= Recursive term term (diff -> f)
2016-08-04 19:07:19 +03:00
-- | Diff two lists by each elements position, and pass the resulting list of diffs to the continuation.
| ByIndex [term] [term] ([diff] -> f)
2016-08-04 19:07:24 +03:00
-- | Diff two lists by each elements similarity and pass the resulting list of diffs to the continuation.
| BySimilarity [term] [term] ([diff] -> f)
2016-08-04 01:12:31 +03:00
deriving Functor
2015-11-18 03:24:01 +03:00
2016-08-04 19:10:19 +03:00
-- | The free monad for 'AlgorithmF'. This enables us to construct diff values using do-notation. We use the Church-encoded free monad 'F' for efficiency.
type Algorithm term diff = F (AlgorithmF term diff)
2016-08-04 19:08:34 +03:00
-- DSL
2016-08-04 19:08:18 +03:00
-- | Constructs a 'Recursive' diff of two terms.
recursively :: term -> term -> Algorithm term diff diff
recursively a b = wrap (Recursive a b pure)
2016-08-04 19:08:18 +03:00
-- | Constructs a 'ByIndex' diff of two lists of terms.
byIndex :: [term] -> [term] -> Algorithm term diff [diff]
byIndex a b = wrap (ByIndex a b pure)
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.
bySimilarity :: [term] -> [term] -> Algorithm term diff [diff]
bySimilarity a b = wrap (BySimilarity a b pure)