Skip to content

Commit

Permalink
Merge branch 'trunk' of https://github.com/unisonweb/unison into prec…
Browse files Browse the repository at this point in the history
…edence
  • Loading branch information
runarorama committed Aug 20, 2024
2 parents 4c166f0 + e388786 commit da75484
Show file tree
Hide file tree
Showing 53 changed files with 2,207 additions and 1,281 deletions.
18 changes: 15 additions & 3 deletions codebase2/codebase-sqlite/U/Codebase/Sqlite/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3500,7 +3500,11 @@ getProjectReflog numEntries projectId =
SELECT project_id, project_branch_id, time, from_root_causal_id, to_root_causal_id, reason
FROM project_branch_reflog
WHERE project_id = :projectId
ORDER BY time DESC
ORDER BY
time DESC,
-- Strictly for breaking ties in transcripts with the same time,
-- this will break ties in the correct order, sorting later inserted rows first.
ROWID DESC
LIMIT :numEntries
|]

Expand All @@ -3512,7 +3516,11 @@ getProjectBranchReflog numEntries projectBranchId =
SELECT project_id, project_branch_id, time, from_root_causal_id, to_root_causal_id, reason
FROM project_branch_reflog
WHERE project_branch_id = :projectBranchId
ORDER BY time DESC
ORDER BY
time DESC,
-- Strictly for breaking ties in transcripts with the same time,
-- this will break ties in the correct order, sorting later inserted rows first.
ROWID DESC
LIMIT :numEntries
|]

Expand All @@ -3523,7 +3531,11 @@ getGlobalReflog numEntries =
[sql|
SELECT project_id, project_branch_id, time, from_root_causal_id, to_root_causal_id, reason
FROM project_branch_reflog
ORDER BY time DESC
ORDER BY
time DESC,
-- Strictly for breaking ties in transcripts with the same time,
-- this will break ties in the correct order, sorting later inserted rows first.
ROWID DESC
LIMIT :numEntries
|]

Expand Down
3 changes: 2 additions & 1 deletion codebase2/core/Unison/NameSegment.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Unison.NameSegment
( NameSegment,
toUnescapedText,

-- * Sentinel name segments
defaultPatchSegment,
Expand All @@ -23,7 +24,7 @@ module Unison.NameSegment
)
where

import Unison.NameSegment.Internal (NameSegment (NameSegment))
import Unison.NameSegment.Internal (NameSegment (NameSegment, toUnescapedText))

------------------------------------------------------------------------------------------------------------------------
-- special segment names
Expand Down
1 change: 1 addition & 0 deletions lib/unison-util-relation/src/Unison/Util/BiMultimap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ withoutRan ys m =
domain :: BiMultimap a b -> Map a (NESet b)
domain = toMultimap

-- | /O(1)/.
range :: BiMultimap a b -> Map b a
range = toMapR

Expand Down
51 changes: 34 additions & 17 deletions parser-typechecker/src/Unison/Codebase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ import Unison.Parser.Ann (Ann)
import Unison.Parser.Ann qualified as Parser
import Unison.Prelude
import Unison.Project (ProjectAndBranch (ProjectAndBranch), ProjectBranchName, ProjectName)
import Unison.Reference (Reference, TermReferenceId, TypeReference)
import Unison.Reference (Reference, TermReference, TermReferenceId, TypeReference)
import Unison.Reference qualified as Reference
import Unison.Referent qualified as Referent
import Unison.Runtime.IOSource qualified as IOSource
Expand All @@ -163,6 +163,7 @@ import Unison.Type qualified as Type
import Unison.Typechecker.TypeLookup (TypeLookup (TypeLookup))
import Unison.Typechecker.TypeLookup qualified as TL
import Unison.UnisonFile qualified as UF
import Unison.Util.Defns (Defns (..), DefnsF)
import Unison.Util.Relation qualified as Rel
import Unison.Var (Var)
import Unison.WatchKind qualified as WK
Expand Down Expand Up @@ -364,35 +365,51 @@ lookupWatchCache codebase h = do
-- and all of their type dependencies, including builtins.
typeLookupForDependencies ::
Codebase IO Symbol Ann ->
Set Reference ->
DefnsF Set TermReference TypeReference ->
Sqlite.Transaction (TL.TypeLookup Symbol Ann)
typeLookupForDependencies codebase s = do
when debug $ traceM $ "typeLookupForDependencies " ++ show s
(<> Builtin.typeLookup) <$> depthFirstAccum mempty s
(<> Builtin.typeLookup) <$> depthFirstAccum s
where
depthFirstAccum :: TL.TypeLookup Symbol Ann -> Set Reference -> Sqlite.Transaction (TL.TypeLookup Symbol Ann)
depthFirstAccum tl refs = foldM go tl (Set.filter (unseen tl) refs)
depthFirstAccum ::
DefnsF Set TermReference TypeReference ->
Sqlite.Transaction (TL.TypeLookup Symbol Ann)
depthFirstAccum refs = do
tl <- depthFirstAccumTypes mempty refs.types
foldM goTerm tl (Set.filter (unseen tl) refs.terms)

depthFirstAccumTypes ::
TL.TypeLookup Symbol Ann ->
Set TypeReference ->
Sqlite.Transaction (TL.TypeLookup Symbol Ann)
depthFirstAccumTypes tl refs =
foldM goType tl (Set.filter (unseen tl) refs)

-- We need the transitive dependencies of data decls
-- that are scrutinized in a match expression for
-- pattern match coverage checking (specifically for
-- the inhabitation check). We ensure these are found
-- by collecting all transitive type dependencies.
go tl ref@(Reference.DerivedId id) =
goTerm :: TypeLookup Symbol Ann -> TermReference -> Sqlite.Transaction (TypeLookup Symbol Ann)
goTerm tl ref =
getTypeOfTerm codebase ref >>= \case
Just typ ->
let z = tl <> TypeLookup (Map.singleton ref typ) mempty mempty
in depthFirstAccum z (Type.dependencies typ)
Nothing ->
getTypeDeclaration codebase id >>= \case
Just (Left ed) ->
let z = tl <> TypeLookup mempty mempty (Map.singleton ref ed)
in depthFirstAccum z (DD.typeDependencies $ DD.toDataDecl ed)
Just (Right dd) ->
let z = tl <> TypeLookup mempty (Map.singleton ref dd) mempty
in depthFirstAccum z (DD.typeDependencies dd)
Nothing -> pure tl
go tl Reference.Builtin {} = pure tl -- codebase isn't consulted for builtins
in depthFirstAccumTypes z (Type.dependencies typ)
Nothing -> pure tl

goType :: TypeLookup Symbol Ann -> TypeReference -> Sqlite.Transaction (TypeLookup Symbol Ann)
goType tl ref@(Reference.DerivedId id) =
getTypeDeclaration codebase id >>= \case
Just (Left ed) ->
let z = tl <> TypeLookup mempty mempty (Map.singleton ref ed)
in depthFirstAccumTypes z (DD.typeDependencies $ DD.toDataDecl ed)
Just (Right dd) ->
let z = tl <> TypeLookup mempty (Map.singleton ref dd) mempty
in depthFirstAccumTypes z (DD.typeDependencies dd)
Nothing -> pure tl
goType tl Reference.Builtin {} = pure tl -- codebase isn't consulted for builtins

unseen :: TL.TypeLookup Symbol a -> Reference -> Bool
unseen tl r =
isNothing
Expand Down
3 changes: 2 additions & 1 deletion parser-typechecker/src/Unison/Codebase/CodeLookup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Unison.Prelude
import Unison.Reference qualified as Reference
import Unison.Term (Term)
import Unison.Term qualified as Term
import Unison.Util.Defns (Defns (..))
import Unison.Util.Set qualified as Set
import Unison.Var (Var)

Expand Down Expand Up @@ -56,7 +57,7 @@ transitiveDependencies code seen0 rid =
getIds = Set.mapMaybe Reference.toId
in getTerm code rid >>= \case
Just t ->
foldM (transitiveDependencies code) seen (getIds $ Term.dependencies t)
foldM (transitiveDependencies code) seen (getIds $ let deps = Term.dependencies t in deps.terms <> deps.types)
Nothing ->
getTypeDeclaration code rid >>= \case
Nothing -> pure seen
Expand Down
17 changes: 8 additions & 9 deletions parser-typechecker/src/Unison/Codebase/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ module Unison.Codebase.Type
where

import U.Codebase.HashTags (CausalHash)
import U.Codebase.Reference qualified as V2
import Unison.Codebase.Branch (Branch)
import Unison.CodebasePath (CodebasePath)
import Unison.ConstructorType qualified as CT
import Unison.DataDeclaration (Decl)
import Unison.Hash (Hash)
import Unison.Prelude
import Unison.Reference (Reference, TypeReference)
import Unison.Reference (Reference, TypeReference, TermReferenceId, TypeReferenceId)
import Unison.Reference qualified as Reference
import Unison.Referent qualified as Referent
import Unison.ShortHash (ShortHash)
Expand All @@ -31,27 +30,27 @@ data Codebase m v a = Codebase
--
-- Note that it is possible to call 'putTerm', then 'getTerm', and receive @Nothing@, per the semantics of
-- 'putTerm'.
getTerm :: Reference.Id -> Sqlite.Transaction (Maybe (Term v a)),
getTerm :: TermReferenceId -> Sqlite.Transaction (Maybe (Term v a)),
-- | Get the type of a user-defined term.
--
-- Note that it is possible to call 'putTerm', then 'getTypeOfTermImpl', and receive @Nothing@, per the semantics of
-- 'putTerm'.
getTypeOfTermImpl :: Reference.Id -> Sqlite.Transaction (Maybe (Type v a)),
getTypeOfTermImpl :: TermReferenceId -> Sqlite.Transaction (Maybe (Type v a)),
-- | Get a type declaration.
--
-- Note that it is possible to call 'putTypeDeclaration', then 'getTypeDeclaration', and receive @Nothing@, per the
-- semantics of 'putTypeDeclaration'.
getTypeDeclaration :: Reference.Id -> Sqlite.Transaction (Maybe (Decl v a)),
getTypeDeclaration :: TypeReferenceId -> Sqlite.Transaction (Maybe (Decl v a)),
-- | Get the type of a given decl.
getDeclType :: V2.Reference -> Sqlite.Transaction CT.ConstructorType,
getDeclType :: TypeReference -> Sqlite.Transaction CT.ConstructorType,
-- | Enqueue the put of a user-defined term (with its type) into the codebase, if it doesn't already exist. The
-- implementation may choose to delay the put until all of the term's (and its type's) references are stored as
-- well.
putTerm :: Reference.Id -> Term v a -> Type v a -> Sqlite.Transaction (),
putTerm :: TermReferenceId -> Term v a -> Type v a -> Sqlite.Transaction (),
putTermComponent :: Hash -> [(Term v a, Type v a)] -> Sqlite.Transaction (),
-- | Enqueue the put of a type declaration into the codebase, if it doesn't already exist. The implementation may
-- choose to delay the put until all of the type declaration's references are stored as well.
putTypeDeclaration :: Reference.Id -> Decl v a -> Sqlite.Transaction (),
putTypeDeclaration :: TypeReferenceId -> Decl v a -> Sqlite.Transaction (),
putTypeDeclarationComponent :: Hash -> [Decl v a] -> Sqlite.Transaction (),
-- getTermComponent :: Hash -> m (Maybe [Term v a]),
getTermComponentWithTypes :: Hash -> Sqlite.Transaction (Maybe [(Term v a, Type v a)]),
Expand All @@ -66,7 +65,7 @@ data Codebase m v a = Codebase
-- | Copy a branch and all of its dependencies from this codebase into the given codebase.
syncToDirectory :: CodebasePath -> Branch m -> m (),
-- | @getWatch k r@ returns watch result @t@ that was previously put by @putWatch k r t@.
getWatch :: WK.WatchKind -> Reference.Id -> Sqlite.Transaction (Maybe (Term v a)),
getWatch :: WK.WatchKind -> TermReferenceId -> Sqlite.Transaction (Maybe (Term v a)),
-- | Get the set of user-defined terms-or-constructors that have the given type.
termsOfTypeImpl :: Reference -> Sqlite.Transaction (Set Referent.Id),
-- | Get the set of user-defined terms-or-constructors mention the given type anywhere in their signature.
Expand Down
20 changes: 15 additions & 5 deletions parser-typechecker/src/Unison/FileParsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ import Control.Lens
import Control.Monad.State (evalStateT)
import Data.Foldable qualified as Foldable
import Data.List (partition)
import Data.List qualified as List
import Data.List.NonEmpty qualified as List.NonEmpty
import Data.Map qualified as Map
import Data.Sequence qualified as Seq
import Data.Set qualified as Set
import Unison.ABT qualified as ABT
import Unison.Blank qualified as Blank
import Unison.Builtin qualified as Builtin
import Unison.ConstructorReference qualified as ConstructorReference
import Unison.Name qualified as Name
import Unison.Names qualified as Names
import Unison.NamesWithHistory qualified as Names
import Unison.Parser.Ann (Ann)
import Unison.Prelude
import Unison.PrettyPrintEnv.Names qualified as PPE
import Unison.Reference (Reference)
import Unison.Reference (TermReference, TypeReference)
import Unison.Referent qualified as Referent
import Unison.Result (CompilerBug (..), Note (..), ResultT, pattern Result)
import Unison.Result qualified as Result
Expand All @@ -37,6 +39,7 @@ import Unison.Typechecker.TypeLookup qualified as TL
import Unison.UnisonFile (definitionLocation)
import Unison.UnisonFile qualified as UF
import Unison.UnisonFile.Names qualified as UF
import Unison.Util.Defns (Defns (..), DefnsF)
import Unison.Util.List qualified as List
import Unison.Util.Relation qualified as Rel
import Unison.Var (Var)
Expand Down Expand Up @@ -76,7 +79,7 @@ computeTypecheckingEnvironment ::
(Var v, Monad m) =>
ShouldUseTndr m ->
[Type v] ->
(Set Reference -> m (TL.TypeLookup v Ann)) ->
(DefnsF Set TermReference TypeReference -> m (TL.TypeLookup v Ann)) ->
UnisonFile v ->
m (Typechecker.Env v Ann)
computeTypecheckingEnvironment shouldUseTndr ambientAbilities typeLookupf uf =
Expand All @@ -99,8 +102,15 @@ computeTypecheckingEnvironment shouldUseTndr ambientAbilities typeLookupf uf =
let shortname = Name.unsafeParseVar v,
name `Name.endsWithReverseSegments` List.NonEmpty.toList (Name.reverseSegments shortname)
]
possibleRefs = Referent.toReference . view _3 <$> possibleDeps
tl <- fmap (UF.declsToTypeLookup uf <>) (typeLookupf (UF.dependencies uf <> Set.fromList possibleRefs))
possibleRefs =
List.foldl'
( \acc -> \case
(_, _, Referent.Con ref _) -> acc & over #types (Set.insert (ref ^. ConstructorReference.reference_))
(_, _, Referent.Ref ref) -> acc & over #terms (Set.insert ref)
)
(Defns Set.empty Set.empty)
possibleDeps
tl <- fmap (UF.declsToTypeLookup uf <>) (typeLookupf (UF.dependencies uf <> possibleRefs))
-- For populating the TDNR environment, we pick definitions
-- from the namespace and from the local file whose full name
-- has a suffix that equals one of the free variables in the file.
Expand Down Expand Up @@ -130,7 +140,7 @@ computeTypecheckingEnvironment shouldUseTndr ambientAbilities typeLookupf uf =
]
pure
Typechecker.Env
{ ambientAbilities = ambientAbilities,
{ ambientAbilities,
typeLookup = tl,
termsByShortname = fqnsByShortName
}
Expand Down
Loading

0 comments on commit da75484

Please sign in to comment.