2015-11-18 04:15:10 +03:00
|
|
|
module Interpreter (interpret) 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:17:45 +03:00
|
|
|
constructAndRun :: Term a Info -> Term a Info -> Maybe (Diff a)
|
|
|
|
constructAndRun a b =
|
2015-11-18 04:05:16 +03:00
|
|
|
run algorithm where
|
|
|
|
algorithm = Free $ Recursive a b Pure
|
|
|
|
|
|
|
|
run :: Algorithm a (Diff a) -> Maybe (Diff a)
|
|
|
|
run (Pure diff) = Just diff
|
2015-11-18 04:29:12 +03:00
|
|
|
run (Free (Recursive a b f)) = recur a b where
|
|
|
|
recur _ _ = run $ f $ Pure Patch { old = Just a, new = Just b }
|
2015-11-18 04:13:48 +03:00
|
|
|
|
|
|
|
interpret :: Term a Info -> Term a Info -> Diff a
|
2015-11-18 04:17:45 +03:00
|
|
|
interpret a b = maybeReplace $ constructAndRun a b where
|
2015-11-18 04:13:48 +03:00
|
|
|
maybeReplace (Just a) = a
|
|
|
|
maybeReplace Nothing = Just a </> Just b
|