<commit_before>module Unison.Codebase.BranchDiff where

import Data.Map (Map)
import Data.Set (Set)
import Unison.Codebase.Branch (Branch0(..))
--import qualified Unison.Codebase.Branch as Branch
import qualified Unison.Codebase.Metadata as Metadata
import qualified Unison.Codebase.Patch as P
import qualified Unison.Codebase.TermEdit as TermEdit
import qualified Unison.Codebase.TypeEdit as TypeEdit
import Unison.Name (Name)
import Unison.Reference (Reference)
import Unison.Referent (Referent)
import qualified Unison.Referent as Referent
import qualified Unison.Util.Relation as R

data DiffType a = Create a | Delete a | Modify a

-- todo: maybe simplify this file using Relation3?

data NamespaceSlice r = NamespaceSlice {
  names :: R.Relation r Name,
  metadata :: R.Relation r (Name, Metadata.Value)
}

data DiffSlice r = DiffSlice {
  tpatchUpdates :: R.Relation r r, -- old new
  tnamespaceUpdates :: R.Relation (r, r) Name,
  tadds :: R.Relation r Name,
  tremoves :: R.Relation r Name,
  tcopies :: Map r (Set Name, Set Name), -- ref (old, new)
  tmoves :: Map r (Set Name, Set Name), -- ref (old, new)
  taddedMetadata :: R.Relation r (Name, Metadata.Value),
  tremovedMetadata :: R.Relation r (Name, Metadata.Value)
}

data BranchDiff = BranchDiff
  { termsDiff :: DiffSlice Referent
  , typesDiff :: DiffSlice Reference
  -- patchesDiff :: Map Name (DiffType Patch.PatchDiff)
  }

diff0 :: forall m. Monad m => Branch0 m -> Branch0 m -> P.Patch -> BranchDiff
diff0 _old _new _patch = BranchDiff terms types where
  (terms, types) = undefined
--    computeSlices
--      (deepr4ToSlice (Branch.deepTerms old))
--      (deepr4ToSlice (Branch.deepTerms new))
--      (deepr4ToSlice (Branch.deepTypes old))
--      (deepr4ToSlice (Branch.deepTypes new))
--      patch

--unpackMetadata :: Branch0 m ->

deepr4ToSlice :: Metadata.R4 r Name -> NamespaceSlice r
deepr4ToSlice = undefined -- NamespaceSlice (Star3.d1 s) (unpackMetadata s) where

computeSlices :: NamespaceSlice Referent<commit_msg>fixup deepR4ToSlice<commit_after>module Unison.Codebase.BranchDiff where

import Data.Map (Map)
import Data.Set (Set)
import Unison.Codebase.Branch (Branch0(..))
import qualified Unison.Codebase.Branch as Branch
import qualified Unison.Codebase.Metadata as Metadata
import qualified Unison.Codebase.Patch as P
import qualified Unison.Codebase.TermEdit as TermEdit
import qualified Unison.Codebase.TypeEdit as TypeEdit
import Unison.Name (Name)
import Unison.Reference (Reference)
import Unison.Referent (Referent)
import qualified Unison.Referent as Referent
import qualified Unison.Util.Relation as R
import qualified Unison.Util.Relation4 as R4

data DiffType a = Create a | Delete a | Modify a

-- todo: maybe simplify this file using Relation3?

data NamespaceSlice r = NamespaceSlice {
  names :: R.Relation r Name,
  metadata :: R.Relation r (Name, Metadata.Value)
}

data DiffSlice r = DiffSlice {
  tpatchUpdates :: R.Relation r r, -- old new
  tnamespaceUpdates :: R.Relation (r, r) Name,
  tadds :: R.Relation r Name,
  tremoves :: R.Relation r Name,
  tcopies :: Map r (Set Name, Set Name), -- ref (old, new)
  tmoves :: Map r (Set Name, Set Name), -- ref (old, new)
  taddedMetadata :: R.Relation r (Name, Metadata.Value),
  tremovedMetadata :: R.Relation r (Name, Metadata.Value)
}

data BranchDiff = BranchDiff
  { termsDiff :: DiffSlice Referent
  , typesDiff :: DiffSlice Reference
  -- todo: patchesDiff :: Map Name (DiffType Patch.PatchDiff)  
  }

diff0 :: forall m. Monad m => Branch0 m -> Branch0 m -> P.Patch -> BranchDiff
diff0 old new patch = BranchDiff terms types where
  (terms, types) = undefined
    computeSlices
      (deepr4ToSlice (Branch.deepTerms old) (Branch.deepTermMetadata old))
      (deepr4ToSlice (Branch.deepTerms new) (Branch.deepTermMetadata new))
      (deepr4ToSlice (Branch.deepTypes old) (Branch.deepTypeMetadata old))
      (deepr4ToSlice (Branch.deepTypes new) (Branch.deepTypeMetadata new))
      patch

deepr4ToSlice :: Ord r
              => R.Relation r Name
              -> Metadata.R4 r Name
              -> NamespaceSlice r
deepr4ToSlice deepNames deepMetadata =
  NamespaceSlice deepNames (unpackMetadata deepMetadata)
  where
   unpackMetadata = R.fromList . fmap (\(r,n,_t,v) -> (r, (n,v))) . R4.toList

computeSlices :: NamespaceSlice Referent
{'fraction_non_alphanumeric': 0.10106629578117757, 'fraction_numerical': 0.0076495132127955496, 'mean_word_length': 3.9145785876993164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3627124', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1386, 'n_words': 439}