2015-11-18 22:09:18 +03:00
|
|
|
module Interpreter (interpret, Comparable) where
|
2015-11-18 04:05:16 +03:00
|
|
|
|
|
|
|
import Algorithm
|
|
|
|
import Control.Monad.Free
|
|
|
|
import Control.Comonad.Cofree
|
|
|
|
import Operation
|
|
|
|
import Diff
|
2015-11-18 04:29:18 +03:00
|
|
|
import Syntax
|
2015-11-18 04:50:15 +03:00
|
|
|
import Data.Map
|
2015-11-18 05:23:53 +03:00
|
|
|
import Patch
|
2015-11-18 18:11:05 +03:00
|
|
|
import SES
|
2015-11-18 04:05:16 +03:00
|
|
|
|
2015-11-18 22:09:11 +03:00
|
|
|
constructAndRun :: Eq a => Comparable a -> Term a Info -> Term a Info -> Maybe (Diff a)
|
2015-11-18 22:13:13 +03:00
|
|
|
constructAndRun comparable a b | not $ comparable a b = Nothing
|
2015-11-18 22:09:11 +03:00
|
|
|
constructAndRun comparable a b =
|
|
|
|
run comparable $ algorithm a b where
|
2015-11-18 21:52:09 +03:00
|
|
|
algorithm (_ :< Indexed a) (_ :< Indexed b) = Free $ ByIndex a b (Pure . Free . Indexed)
|
2015-11-18 21:52:49 +03:00
|
|
|
algorithm (_ :< Keyed a) (_ :< Keyed b) = Free $ ByKey a b (Pure . Free . Keyed)
|
2015-11-18 21:59:40 +03:00
|
|
|
algorithm (_ :< Leaf a) (_ :< Leaf b) | a == b = Pure . Free $ Leaf b
|
2015-11-18 21:53:59 +03:00
|
|
|
algorithm a b = Free $ Recursive a b Pure
|
2015-11-18 04:05:16 +03:00
|
|
|
|
2015-11-18 22:09:11 +03:00
|
|
|
run :: Eq a => Comparable a -> Algorithm a (Diff a) -> Maybe (Diff a)
|
|
|
|
run _ (Pure diff) = Just diff
|
2015-11-18 05:26:07 +03:00
|
|
|
|
2015-11-18 22:09:11 +03:00
|
|
|
run comparable (Free (Recursive a b f)) = run comparable . f $ recur a b where
|
2015-11-18 04:29:18 +03:00
|
|
|
recur (_ :< Indexed a') (_ :< Indexed b') | length a' == length b' =
|
2015-11-18 22:09:11 +03:00
|
|
|
Free . Indexed $ zipWith (interpret comparable) a' b'
|
2015-11-18 04:35:00 +03:00
|
|
|
recur (_ :< Fixed a') (_ :< Fixed b') | length a' == length b' =
|
2015-11-18 22:09:11 +03:00
|
|
|
Free . Fixed $ zipWith (interpret comparable) a' b'
|
2015-11-18 04:50:15 +03:00
|
|
|
recur (_ :< Keyed a') (_ :< Keyed b') | keys a' == keys b' =
|
2015-11-18 05:16:39 +03:00
|
|
|
Free . Keyed . fromList . fmap repack $ keys b' where
|
|
|
|
repack key = (key, interpretInBoth key a' b')
|
2015-11-18 04:50:15 +03:00
|
|
|
interpretInBoth key a' b' = maybeInterpret (Data.Map.lookup key a') (Data.Map.lookup key b')
|
2015-11-18 22:09:11 +03:00
|
|
|
maybeInterpret (Just a) (Just b) = interpret comparable a b
|
2015-11-18 05:44:55 +03:00
|
|
|
recur _ _ = Pure $ Replace a b
|
2015-11-18 04:13:48 +03:00
|
|
|
|
2015-11-18 22:09:11 +03:00
|
|
|
run comparable (Free (ByKey a b f)) = run comparable $ f byKey where
|
2015-11-18 11:21:27 +03:00
|
|
|
byKey = unions [ deleted, inserted, patched ]
|
|
|
|
deleted = (Pure . Delete) <$> difference a b
|
|
|
|
inserted = (Pure . Insert) <$> difference b a
|
2015-11-18 22:09:11 +03:00
|
|
|
patched = intersectionWith (interpret comparable) a b
|
2015-11-18 11:21:27 +03:00
|
|
|
|
2015-11-18 22:09:11 +03:00
|
|
|
run comparable (Free (ByIndex a b f)) = run comparable . f $ ses (constructAndRun comparable) cost a b
|
2015-11-18 18:02:47 +03:00
|
|
|
|
2015-11-18 22:13:00 +03:00
|
|
|
type Comparable a = Term a Info -> Term a Info -> Bool
|
2015-11-18 22:09:11 +03:00
|
|
|
|
|
|
|
interpret :: Eq a => Comparable a -> Term a Info -> Term a Info -> Diff a
|
|
|
|
interpret comparable a b = maybeReplace $ constructAndRun comparable a b where
|
2015-11-18 04:13:48 +03:00
|
|
|
maybeReplace (Just a) = a
|
2015-11-18 05:44:55 +03:00
|
|
|
maybeReplace Nothing = Pure $ Replace a b
|