1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 08:54:07 +03:00

Revert "Represent term/diff costs as Rationals."

This reverts commit aea4e7ec784422696631156415855eab920af38a.
This commit is contained in:
Rob Rix 2016-05-30 14:05:23 -04:00
parent 9049478594
commit 338ed4a5ce
6 changed files with 10 additions and 10 deletions

View File

@ -15,9 +15,9 @@ type instance Base (Free f a) = FreeF f a
instance (Functor f) => Foldable.Foldable (Free f a) where project = runFree
instance (Functor f) => Foldable.Unfoldable (Free f a) where embed = free
diffSum :: (Patch (Term a annotation) -> Rational) -> Diff a annotation -> Rational
diffSum :: (Patch (Term a annotation) -> Integer) -> Diff a annotation -> Integer
diffSum patchCost diff = sum $ fmap patchCost diff
-- | The sum of the node count of the diffs patches.
diffCost :: Diff a annotation -> Rational
diffCost :: Diff a annotation -> Integer
diffCost = diffSum $ patchSum termSize

View File

@ -86,11 +86,11 @@ diffFiles parser renderer sourceBlobs = do
pure $! renderer (runBothWith (diffTerms diffCostWithCachedTermSizes) $ replaceLeaves <*> terms) sourceBlobs
-- | The sum of the node count of the diffs patches.
diffCostWithCachedTermSizes :: Diff a Info -> Rational
diffCostWithCachedTermSizes :: Diff a Info -> Integer
diffCostWithCachedTermSizes = diffSum (getSum . foldMap (Sum . size . extract))
-- | The absolute difference between the node counts of a diff.
diffCostWithAbsoluteDifferenceOfCachedDiffSizes :: Diff a Info -> Rational
diffCostWithAbsoluteDifferenceOfCachedDiffSizes :: Diff a Info -> Integer
diffCostWithAbsoluteDifferenceOfCachedDiffSizes diff = case runFree diff of
Free (Join (before, after) :< _) -> abs $ size before - size after
Pure patch -> sum $ size . extract <$> patch

View File

@ -6,7 +6,7 @@ import Range
-- | An annotation for a source file, including the source range and semantic
-- | categories.
data Info = Info { characterRange :: !Range, categories :: !(Set Category), size :: !Rational }
data Info = Info { characterRange :: !Range, categories :: !(Set Category), size :: !Integer }
deriving (Eq, Show)
instance Categorizable Info where

View File

@ -33,7 +33,7 @@ unPatch (Insert b) = That b
unPatch (Delete a) = This a
-- | Calculate the cost of the patch given a function to compute the cost of a item.
patchSum :: (a -> Rational) -> Patch a -> Rational
patchSum :: (a -> Integer) -> Patch a -> Integer
patchSum termCost patch = maybe 0 termCost (before patch) + maybe 0 termCost (after patch)
-- | Return Just the value in This, or the first value in These, if any.

View File

@ -10,7 +10,7 @@ import qualified Data.Map as Map
type Compare a annotation = Term a annotation -> Term a annotation -> Maybe (Diff a annotation)
-- | A function that computes the cost of a diff.
type Cost a annotation = Diff a annotation -> Rational
type Cost a annotation = Diff a annotation -> Integer
-- | Find the shortest edit script (diff) between two terms given a function to compute the cost.
ses :: Compare a annotation -> Cost a annotation -> [Term a annotation] -> [Term a annotation] -> [Diff a annotation]
@ -18,7 +18,7 @@ ses diffTerms cost as bs = fst <$> evalState diffState Map.empty where
diffState = diffAt diffTerms cost (0, 0) as bs
-- | Find the shortest edit script between two terms at a given vertex in the edit graph.
diffAt :: Compare a annotation -> Cost a annotation -> (Integer, Integer) -> [Term a annotation] -> [Term a annotation] -> State (Map.Map (Integer, Integer) [(Diff a annotation, Rational)]) [(Diff a annotation, Rational)]
diffAt :: Compare a annotation -> Cost a annotation -> (Integer, Integer) -> [Term a annotation] -> [Term a annotation] -> State (Map.Map (Integer, Integer) [(Diff a annotation, Integer)]) [(Diff a annotation, Integer)]
diffAt _ _ _ [] [] = pure []
diffAt _ cost _ [] bs = pure $ foldr toInsertions [] bs where
toInsertions each = consWithCost cost (free . Pure . Insert $ each)
@ -48,5 +48,5 @@ diffAt diffTerms cost (i, j) (a : as) (b : bs) = do
recur = diffAt diffTerms cost
-- | Prepend a diff to the list with the cumulative cost.
consWithCost :: Cost a annotation -> Diff a annotation -> [(Diff a annotation, Rational)] -> [(Diff a annotation, Rational)]
consWithCost :: Cost a annotation -> Diff a annotation -> [(Diff a annotation, Integer)] -> [(Diff a annotation, Integer)]
consWithCost cost diff rest = (diff, cost diff + maybe 0 snd (fst <$> uncons rest)) : rest

View File

@ -30,6 +30,6 @@ zipTerms t1 t2 = annotate (zipUnwrap a b)
zipUnwrapMaps a' b' key = (,) key <$> zipTerms (a' ! key) (b' ! key)
-- | Return the node count of a term.
termSize :: Term a annotation -> Rational
termSize :: Term a annotation -> Integer
termSize = cata size where
size (_ :< syntax) = 1 + sum syntax