From 78701ddd9002c63ef55aa067b41727d54fee9079 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 3 Aug 2016 20:08:20 -0400 Subject: [PATCH] Defines a constructor of algorithms over terms. --- src/Interpreter.hs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Interpreter.hs b/src/Interpreter.hs index 6221fed6b..a1627f139 100644 --- a/src/Interpreter.hs +++ b/src/Interpreter.hs @@ -44,6 +44,15 @@ constructAndRun construct comparable cost t1 t2 (annotation1 :< a, annotation2 :< b) = (runCofree t1, runCofree t2) annotate = pure . construct . (both annotation1 annotation2 :<) +algorithmWithTerms :: Eq leaf => Term leaf (Record fields) -> Term leaf (Record fields) -> Algorithm (Term leaf (Record fields)) (Diff leaf (Record fields)) (Diff leaf (Record fields)) +algorithmWithTerms t1 t2 = case (unwrap t1, unwrap t2) of + (Indexed a, Indexed b) -> do + diffs <- byIndex a b + annotate (Indexed diffs) + (Leaf a, Leaf b) | a == b -> annotate (Leaf b) + _ -> recursively t1 t2 + where annotate = pure . wrap . (both (extract t1) (extract t2) :<) + -- | Runs the diff algorithm run :: (Eq leaf, Hashable leaf, Eq (Record fields), HasField fields Category) => DiffConstructor leaf (Record fields) -> Comparable leaf (Record fields) -> SES.Cost (Diff leaf (Record fields)) -> Algorithm (Term leaf (Record fields)) (Diff leaf (Record fields)) (Diff leaf (Record fields)) -> Maybe (Diff leaf (Record fields)) run construct comparable cost algorithm = (`F.iter` fmap Just algorithm) $ \case