Skip to content

Commit

Permalink
Merge pull request #4574 from unisonweb/24-01-04-delete-default-metadata
Browse files Browse the repository at this point in the history
cleanup: remove a bunch of metadata-related code
  • Loading branch information
mergify[bot] authored Jan 5, 2024
2 parents 014b3fc + f1b0d05 commit cd4408a
Show file tree
Hide file tree
Showing 51 changed files with 217 additions and 2,100 deletions.
27 changes: 0 additions & 27 deletions codebase2/codebase/U/Codebase/Branch/Type.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{-# LANGUAGE RecordWildCards #-}

module U.Codebase.Branch.Type
( Branch (..),
CausalBranch,
Expand All @@ -12,8 +10,6 @@ module U.Codebase.Branch.Type
childAt,
hoist,
hoistCausalBranch,
termMetadata,
typeMetadata,
U.Codebase.Branch.Type.empty,
)
where
Expand Down Expand Up @@ -105,26 +101,3 @@ hoistCausalBranch f cb =
cb
& Causal.hoist f
& Causal.emap (hoist f) (hoist f)

-- | Returns all the metadata value references that are attached to a term with the provided name in the
-- provided branch.
--
-- If only name is specified, metadata will be returned for all terms at that name.
termMetadata :: (Monad m) => Branch m -> NameSegment -> Maybe Referent -> m [Map MetadataValue MetadataType]
termMetadata Branch {terms} = metadataHelper terms

-- | Returns all the metadata value references that are attached to a type with the provided name in the
-- provided branch.
--
-- If only name is specified, metadata will be returned for all types at that name.
typeMetadata :: (Monad m) => Branch m -> NameSegment -> Maybe Reference -> m [Map MetadataValue MetadataType]
typeMetadata Branch {types} = metadataHelper types

metadataHelper :: (Monad m, Ord ref) => Map NameSegment (Map ref (m MdValues)) -> NameSegment -> Maybe ref -> m [Map MetadataValue MetadataType]
metadataHelper t ns mayQualifier = do
case Map.lookup ns t of
Nothing -> pure []
Just allRefsAtName -> do
case mayQualifier of
Nothing -> (fmap . fmap) unMdValues . sequenceA $ Map.elems allRefsAtName
Just qualifier -> (fmap . fmap) unMdValues . sequenceA . maybeToList $ Map.lookup qualifier allRefsAtName
4 changes: 1 addition & 3 deletions parser-typechecker/src/U/Codebase/Branch/Diff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ data Diff a = Diff

-- | Represents the changes to definitions at a given path, not including child paths.
--
-- Note: doesn't yet include any info on metadata or patch diffs. Feel free to add it.
-- Note: doesn't yet include any info on patch diffs. Feel free to add it.
data DefinitionDiffs = DefinitionDiffs
{ termDiffs :: Map NameSegment (Diff Referent),
typeDiffs :: Map NameSegment (Diff Reference)
-- termMetadataDiffs :: Map (NameSegment, Referent) (Diff Reference),
-- typeMetadataDiffs :: Map (NameSegment, Reference) (Diff Reference)
-- patchDiffs :: Map NameSegment (Diff ())
}
deriving stock (Show, Eq, Ord)
Expand Down
3 changes: 0 additions & 3 deletions parser-typechecker/src/Unison/Builtin/Terms.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

module Unison.Builtin.Terms
( builtinTermsRef,
builtinTermsSrc,
Expand Down
16 changes: 0 additions & 16 deletions parser-typechecker/src/Unison/Codebase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module Unison.Codebase
isTerm,
putTerm,
putTermComponent,
termMetadata,

-- ** Referents (sorta-termlike)
getTypeOfReferent,
Expand Down Expand Up @@ -123,7 +122,6 @@ import U.Codebase.Branch qualified as V2
import U.Codebase.Branch qualified as V2Branch
import U.Codebase.Causal qualified as V2Causal
import U.Codebase.HashTags (CausalHash)
import U.Codebase.Referent qualified as V2
import U.Codebase.Sqlite.Operations qualified as Operations
import U.Codebase.Sqlite.Queries qualified as Queries
import Unison.Builtin qualified as Builtin
Expand Down Expand Up @@ -153,7 +151,6 @@ import Unison.DataDeclaration (Decl)
import Unison.DataDeclaration qualified as DD
import Unison.Hash (Hash)
import Unison.Hashing.V2.Convert qualified as Hashing
import Unison.NameSegment qualified as NameSegment
import Unison.Parser.Ann (Ann)
import Unison.Parser.Ann qualified as Parser
import Unison.Prelude
Expand Down Expand Up @@ -270,19 +267,6 @@ expectBranchForHash codebase hash =
Just branch -> pure branch
Nothing -> error $ reportBug "E412939" ("expectBranchForHash: " ++ show hash ++ " not found in codebase")

-- | Get the metadata attached to the term at a given path and name relative to the given branch.
termMetadata ::
-- | The branch to search inside. Use the current root if 'Nothing'.
Maybe (V2Branch.Branch Sqlite.Transaction) ->
Split ->
-- | There may be multiple terms at the given name. You can specify a Referent to
-- disambiguate if desired.
Maybe V2.Referent ->
Sqlite.Transaction [Map V2Branch.MetadataValue V2Branch.MetadataType]
termMetadata mayBranch (path, nameSeg) ref = do
b <- getShallowBranchAtPath path mayBranch
V2Branch.termMetadata b (coerce @NameSegment.NameSegment nameSeg) ref

-- | Get the lowest common ancestor of two branches, i.e. the most recent branch that is an ancestor of both branches.
lca :: (MonadIO m) => Codebase m v a -> Branch m -> Branch m -> m (Maybe (Branch m))
lca code b1@(Branch.headHash -> h1) b2@(Branch.headHash -> h2) = do
Expand Down
82 changes: 18 additions & 64 deletions parser-typechecker/src/Unison/Codebase/Branch.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Unison.Codebase.Branch
Expand Down Expand Up @@ -131,7 +128,6 @@ import Unison.Util.List qualified as List
import Unison.Util.Monoid qualified as Monoid
import Unison.Util.Relation qualified as R
import Unison.Util.Relation qualified as Relation
import Unison.Util.Relation4 qualified as R4
import Unison.Util.Set qualified as Set
import Unison.Util.Star3 qualified as Star3
import Witherable (FilterableWithIndex (imapMaybe))
Expand Down Expand Up @@ -192,7 +188,6 @@ terms =
\branch terms ->
branch {_terms = terms}
& deriveDeepTerms
& deriveDeepTermMetadata

types :: Lens' (Branch0 m) (Star TypeReference NameSegment)
types =
Expand All @@ -201,7 +196,6 @@ types =
\branch types ->
branch {_types = types}
& deriveDeepTypes
& deriveDeepTypeMetadata

children :: Lens' (Branch0 m) (Map NameSegment (Branch m))
children = lens _children (\Branch0 {..} x -> branch0 _terms _types x _edits)
Expand Down Expand Up @@ -242,15 +236,11 @@ branch0 terms types children edits =
-- These are all overwritten immediately
deepTerms = R.empty,
deepTypes = R.empty,
deepTermMetadata = R4.empty,
deepTypeMetadata = R4.empty,
deepPaths = Set.empty,
deepEdits = Map.empty
}
& deriveDeepTerms
& deriveDeepTypes
& deriveDeepTermMetadata
& deriveDeepTypeMetadata
& deriveDeepPaths
& deriveDeepEdits

Expand Down Expand Up @@ -299,50 +289,6 @@ deriveDeepTypes branch =
children <- deepChildrenHelper e
go (work <> children) (types <> acc)

-- | Derive the 'deepTermMetadata' field of a branch.
deriveDeepTermMetadata :: forall m. Branch0 m -> Branch0 m
deriveDeepTermMetadata branch =
branch {deepTermMetadata = R4.fromList (makeDeepTermMetadata branch)}
where
makeDeepTermMetadata :: Branch0 m -> [(Referent, Name, Metadata.Type, Metadata.Value)]
makeDeepTermMetadata branch = State.evalState (go (Seq.singleton ([], 0, branch)) mempty) Set.empty
where
go ::
Seq (DeepChildAcc m) ->
[(Referent, Name, Metadata.Type, Metadata.Value)] ->
DeepState m [(Referent, Name, Metadata.Type, Metadata.Value)]
go Seq.Empty acc = pure acc
go (e@(reversePrefix, _, b0) Seq.:<| work) acc = do
let termMetadata :: [(Referent, Name, Metadata.Type, Metadata.Value)]
termMetadata =
map
(\(r, n, t, v) -> (r, Name.fromReverseSegments (n NonEmpty.:| reversePrefix), t, v))
(Metadata.starToR4List (_terms b0))
children <- deepChildrenHelper e
go (work <> children) (termMetadata <> acc)

-- | Derive the 'deepTypeMetadata' field of a branch.
deriveDeepTypeMetadata :: forall m. Branch0 m -> Branch0 m
deriveDeepTypeMetadata branch =
branch {deepTypeMetadata = R4.fromList (makeDeepTypeMetadata branch)}
where
makeDeepTypeMetadata :: Branch0 m -> [(TypeReference, Name, Metadata.Type, Metadata.Value)]
makeDeepTypeMetadata branch = State.evalState (go (Seq.singleton ([], 0, branch)) mempty) Set.empty
where
go ::
Seq (DeepChildAcc m) ->
[(TypeReference, Name, Metadata.Type, Metadata.Value)] ->
DeepState m [(TypeReference, Name, Metadata.Type, Metadata.Value)]
go Seq.Empty acc = pure acc
go (e@(reversePrefix, _, b0) Seq.:<| work) acc = do
let typeMetadata :: [(TypeReference, Name, Metadata.Type, Metadata.Value)]
typeMetadata =
map
(\(r, n, t, v) -> (r, Name.fromReverseSegments (n NonEmpty.:| reversePrefix), t, v))
(Metadata.starToR4List (_types b0))
children <- deepChildrenHelper e
go (work <> children) (typeMetadata <> acc)

-- | Derive the 'deepPaths' field of a branch.
deriveDeepPaths :: forall m. Branch0 m -> Branch0 m
deriveDeepPaths branch =
Expand Down Expand Up @@ -483,7 +429,17 @@ one = Branch . Causal.one

empty0 :: Branch0 m
empty0 =
Branch0 mempty mempty mempty mempty True mempty mempty mempty mempty mempty mempty
Branch0
{ _terms = mempty,
_types = mempty,
_children = Map.empty,
_edits = Map.empty,
isEmpty0 = True,
deepTerms = Relation.empty,
deepTypes = Relation.empty,
deepPaths = Set.empty,
deepEdits = Map.empty
}

-- | Checks whether a branch is empty AND has no history.
isEmpty :: Branch m -> Bool
Expand Down Expand Up @@ -718,15 +674,13 @@ batchUpdatesM (toList -> actions) curBranch = foldM execActions curBranch (group
pathLocation _ = ChildActions

-- todo: consider inlining these into Actions2
addTermName ::
Referent -> NameSegment -> Metadata.Metadata -> Branch0 m -> Branch0 m
addTermName r new md =
over terms (Metadata.insertWithMetadata (r, md) . Star3.insertD1 (r, new))

addTypeName ::
TypeReference -> NameSegment -> Metadata.Metadata -> Branch0 m -> Branch0 m
addTypeName r new md =
over types (Metadata.insertWithMetadata (r, md) . Star3.insertD1 (r, new))
addTermName :: Referent -> NameSegment -> Branch0 m -> Branch0 m
addTermName r new =
over terms (Star3.insertD1 (r, new))

addTypeName :: TypeReference -> NameSegment -> Branch0 m -> Branch0 m
addTypeName r new =
over types (Star3.insertD1 (r, new))

deleteTermName :: Referent -> NameSegment -> Branch0 m -> Branch0 m
deleteTermName r n b
Expand Down
5 changes: 1 addition & 4 deletions parser-typechecker/src/Unison/Codebase/Branch/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,9 @@ data Branch0 m = Branch0
-- | True if a branch and its children have no definitions or edits in them.
-- (Computed recursively, and small enough to justify storing here to avoid computing more than once.)
isEmpty0 :: Bool,
-- names and metadata for this branch and its children
-- (ref, (name, value)) iff ref has metadata `value` at name `name`
-- names for this branch and its children
deepTerms :: Relation Referent Name,
deepTypes :: Relation Reference Name,
deepTermMetadata :: Metadata.R4 Referent Name,
deepTypeMetadata :: Metadata.R4 Reference Name,
deepPaths :: Set Path,
deepEdits :: Map Name PatchHash
}
Expand Down
Loading

0 comments on commit cd4408a

Please sign in to comment.