1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 14:54:16 +03:00
semantic/src/DiffSummary.hs

165 lines
6.3 KiB
Haskell
Raw Normal View History

2016-05-13 18:44:03 +03:00
{-# LANGUAGE GeneralizedNewtypeDeriving, DataKinds, RecordWildCards, TypeFamilies #-}
2016-04-25 18:46:10 +03:00
module DiffSummary where
import Prelude hiding (fst, snd)
2016-04-25 18:46:10 +03:00
import Diff
import Info
import Patch
import Term
import Syntax
2016-05-13 18:44:03 +03:00
import qualified Range as R
2016-05-11 00:48:55 +03:00
import Category as Category
2016-05-03 22:50:38 +03:00
import Data.Functor.Both
2016-04-25 18:46:10 +03:00
import Data.Monoid
import Data.Maybe (listToMaybe)
import Data.Set (toList)
import Control.Comonad
2016-05-03 19:17:38 +03:00
import Control.Comonad.Trans.Cofree
2016-05-03 22:50:38 +03:00
import Control.Monad.Trans.Free
2016-05-13 18:44:03 +03:00
import qualified Control.Monad.Free as Free
2016-05-03 22:50:38 +03:00
import qualified Control.Comonad.Cofree as Cofree
2016-05-03 19:17:38 +03:00
import Data.Functor.Foldable as Foldable
2016-05-13 18:44:03 +03:00
import Control.Monad.State hiding (sequence)
import qualified Data.Map as M
import Data.Functor.Identity
2016-04-25 18:46:10 +03:00
2016-05-13 18:44:03 +03:00
instance Unfoldable (DiffSummary a) where
embed (DiffSummary x y) = (DDiffSummary x y)
embed (TermSummary s b f) = DTermSummary s b
embed EmptySummary = DEmptySummary
2016-04-25 18:46:10 +03:00
2016-05-13 18:44:03 +03:00
apo f a = case f a of
Cons x (Left xs) -> x : xs
Cons x (Right b) -> x : apo f b
Nil -> []
2016-05-13 18:44:03 +03:00
-- * --
-- -- *
data DiffSummary a = BranchSummary [DiffSummary a]
| TermSummary String a
| EmptySummary
deriving (Eq, Show, Functor, Ord)
data instance Prim (DiffSummary a) b = PBranchSummary (Prim [a] b) b | PTermSummary String a b | PEmptySummary
type instance Base (DiffSummary a) = Prim (DiffSummary a)
instance Foldable.Foldable (DiffSummary a) where project = Const
instance Unfoldable (DiffSummary a) where embed = getConst
-- data DiffSummary' = [(String, [String])]
-- data DiffSummary'' = DiffSummaryForPatch'' String | DiffSummaryForBranch'' String [DiffSummary'']
-- x = DiffSummaryForBranch'' "module Foo" [ DiffSummaryForPatch'' "inserted thing" ]
-- x = DiffSummaryForBranch'' "module Foo" [ DiffSummaryForBranch'' "module Bar" [ DiffSummaryForPatch'' "deleted thing" ], DiffSummaryForPatch'' "inserted thing" ]
-- data Context = DiffContext DiffSummary
-- deriving (Show)
-- data DiffResult = DiffResult {
-- assumptions :: M.Map String [DiffSummary]
-- } deriving (Show)
-- instance Monoid DiffResult where
-- mempty = DiffResult mempty
-- mappend a b = DiffResult (assumptions a `mappend` assumptions b)
-- newtype DiffState t m = DiffState {
-- memo :: M.Map t m
-- }
-- type Summarize t = State (DiffState t (DiffSummary, DiffResult)) (DiffSummary, DiffResult)
-- memoSummarize :: Ord c => (c -> Summarize c) -> c -> Summarize c
-- memoSummarize f c = gets memo >>= maybe memoize return . M.lookup c where
-- memoize = do
-- r <- f c
-- modify $ \s -> s { memo = M.insert c r $ memo s }
-- return r
-- generateSummaries :: DiffF leaf Info f -> Summarize (Diff leaf Info)
-- generateSummaries (Pure patch) = return (DEmptySummary, mempty)
-- generateSummaries (Free (_ :< Leaf _)) = return (DEmptySummary, mempty)
-- generateSummaries (Free (info :< Indexed f)) = do
-- childStates <- _
-- -- * --
-- -- * --
-- -- *
-- let childContexts = maybe [] (map $ DiffContext _) (M.lookup key . assumptions $ snd childState)
-- let as = M.delete key . assumptions $ snd childState
-- let diffResult = DiffResult {
-- assumptions = _ }
-- return (toCategory (snd info), diffResult)
-- T { 1 }
-- T { 2 }
-- T {1, 2}
-- T {'array', 'dictionary'}
-- T {Nothing, 'dictionary'}
-- Given two (Both (Maybe String))
-- TermSummary { name1 :: Maybe String, name2 :: Maybe String }
-- TermSummary { name1 = Just "1", name2 :: Just "2" }
-- DiffSummary { beforeSummary = (TermSummary { name1 = Just "1"}), afterSummary = (TermSummary { name = Just "2"}), diffContext = Nothing }
2016-04-25 18:46:10 +03:00
2016-05-13 18:44:03 +03:00
info :: Info
info = Info (R.rangeAt 0) mempty 1
eLeaf :: Diff String Info
eLeaf = retract . free . Pure . Insert . cofree $ info :< Leaf "a"
freeLeaf :: Diff String Info
freeLeaf = free . Free $ (pure info :< Indexed [free . Free $ (pure info :< Leaf "a"), free $ Pure (Insert $ cofree (info :< Leaf "b"))])
eIndexed :: Diff String Info
eIndexed = free . Pure . Insert . cofree $ info :< Indexed [cofree $ info :< Leaf "a"]
patchToSummary :: (Term a Info -> DiffSummary a) -> Patch (Term a Info) -> DiffSummary a
patchToSummary termSummary patch = undefined -- memptyOrDiff (before patch) <> memptyOrDiff (after patch)
-- diffSummary :: Diff leaf Info -> DiffSummary a
-- -- histo :: Foldable t => (Base t (Cofree (Base t) a) -> a) -> t -> a
-- diffSummary = histo diffSummary' . fmap (patchToSummary termToSummary) where
-- --diffSummary' :: DiffF leaf (Cofree.Cofree (DiffF leaf Info) DiffSummary) f -> DiffSummary
-- -- Skip any child that doesn't have any changes (that will always include leaves)
-- diffSummary' :: DiffSummaryF leaf Info DiffSummary (Cofree.Cofree (DiffSummaryF leaf Info DiffSummary) DiffSummary) -> DiffSummary
-- -- Prune leaves
-- diffSummary' (Free (info :< Leaf _)) = undefined
-- -- Return a contextless indexed summary with it's indexed context distributed to its children
-- diffSummary' (Free (_ :< Indexed [])) = undefined
-- diffSummary' (Free (_ :< Indexed ((summary Cofree.:< f):xs))) = summary :: DiffSummary
-- -- Return a contextless fixed diff summary with it's fixed context distributed to its children
-- diffSummary' (Free (_ :< Fixed children)) = undefined
-- diffSummary' (Free (_ :< Keyed children)) = undefined
-- -- Return a contextless diff summary
-- diffSummary' (Pure summary) = summary :: DiffSummary
-- -- (patchSummary termSummary)
diffSummary' :: Diff leaf Info -> DiffSummary a
-- futu :: Unfoldable t => (a -> Base t (Free (Base t) a)) -> a -> t
diffSummary' = futu diffSummary'' where
diffSummary'' :: (Diff leaf Info) -> Prim (DiffSummary a) (Free.Free (Prim (DiffSummary a)) (Diff leaf Info))
diffSummary'' diff = case project diff of
2016-04-25 18:46:10 +03:00
-- Syntax Text DiffSummary -> DiffSummary Text
2016-05-03 19:17:38 +03:00
-- If termSummary returns a DiffEntry that just contains the term name, we need to
-- Instead of foldMap we need a histomorphism
2016-05-13 18:44:03 +03:00
termToSummary :: Term leaf Info -> DiffSummary a
termToSummary = Foldable.cata summary where
2016-05-13 18:44:03 +03:00
summary :: TermF leaf Info f -> DiffSummary a
summary (info :< Leaf replace) = toCategory info replace
summary (info :< Indexed children) = _
summary (info :< Fixed children) = _
summary (info :< Keyed _) = _
2016-05-11 00:48:55 +03:00
maybeFirstCategory :: (Categorizable a) => a -> Maybe Category
maybeFirstCategory term = listToMaybe . toList $ Category.categories term
2016-05-13 18:44:03 +03:00
toCategory :: Info -> a -> DiffSummary a
toCategory info a = case maybeFirstCategory info of
Just category -> DTermSummary (show category) a
Nothing -> DEmptySummary