From 394201e9aef8bd9e2cddf6f6f133efa177e31ad8 Mon Sep 17 00:00:00 2001 From: Mitchell Rosen Date: Mon, 25 Oct 2021 17:34:11 -0400 Subject: [PATCH] make endsWithSegments less weird --- parser-typechecker/src/Unison/FileParsers.hs | 5 +++-- .../tests/Unison/Core/Test/Name.hs | 12 ++++++++---- unison-core/src/Unison/Name.hs | 17 +++++++++-------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/parser-typechecker/src/Unison/FileParsers.hs b/parser-typechecker/src/Unison/FileParsers.hs index c9a3c844c..be5a780b0 100644 --- a/parser-typechecker/src/Unison/FileParsers.hs +++ b/parser-typechecker/src/Unison/FileParsers.hs @@ -14,6 +14,7 @@ import Data.Bifunctor ( first ) import qualified Data.Foldable as Foldable import qualified Data.Map as Map import Data.List (partition) +import qualified Data.List.NonEmpty as List.NonEmpty import qualified Data.Set as Set import qualified Data.Sequence as Seq import Data.Text (unpack) @@ -95,7 +96,7 @@ resolveNames typeLookupf preexistingNames uf = do possibleDeps = [ (Name.toText name, Var.name v, r) | (name, r) <- Rel.toList (Names.terms preexistingNames), v <- Set.toList (Term.freeVars tm), - name `Name.endsWithSegments` Name.unsafeFromVar v ] + name `Name.endsWithSegments` List.NonEmpty.toList (Name.segments (Name.unsafeFromVar v)) ] possibleRefs = Referent.toReference . view _3 <$> possibleDeps tl <- lift . lift . fmap (UF.declsToTypeLookup uf <>) $ typeLookupf (deps <> Set.fromList possibleRefs) @@ -118,7 +119,7 @@ resolveNames typeLookupf preexistingNames uf = do [ (Var.name v, nr) | (name, r) <- Rel.toList (Names.terms $ UF.toNames uf), v <- Set.toList (Term.freeVars tm), - name `Name.endsWithSegments` Name.unsafeFromVar v, + name `Name.endsWithSegments` List.NonEmpty.toList (Name.segments (Name.unsafeFromVar v)), typ <- toList $ TL.typeOfReferent tl r, let nr = Typechecker.NamedReference (Name.toText name) typ (Right r) ] pure (tm, fqnsByShortName, tl) diff --git a/parser-typechecker/tests/Unison/Core/Test/Name.hs b/parser-typechecker/tests/Unison/Core/Test/Name.hs index d7dd79c58..a4179ca5f 100644 --- a/parser-typechecker/tests/Unison/Core/Test/Name.hs +++ b/parser-typechecker/tests/Unison/Core/Test/Name.hs @@ -17,6 +17,7 @@ test = scope "name" $ tests [ scope "compareSuffix" (tests testCompareSuffix), + scope "endsWithSegments" (tests testEndsWithSegments), scope "segments" (tests testSegments), scope "splitName" (tests testSplitName), scope "suffixSearch" (tests testSuffixSearch), @@ -59,10 +60,6 @@ test = scope "countSegments" do n1 <- rname Name.countSegments n1 `expectEqual` Name.oldCountSegments (old n1), - scope "endsWithSegments" do - n1 <- rname - n2 <- rname - Name.endsWithSegments n1 n2 `expectEqual` Name.oldEndsWithSegments (old n1) (old n2), scope "isPrefixOf" do n1 <- rname n2 <- rname @@ -134,6 +131,13 @@ testCompareSuffix = scope "[b.b a.b.c]" (expectEqual (compareSuffix "b.b" "a.b.c") GT) ] +testEndsWithSegments :: [Test ()] +testEndsWithSegments = + [ scope "a.b.c ends with []" (expectEqual True (endsWithSegments "a.b.c" [])), + scope "a.b.c ends with [b, c]" (expectEqual True (endsWithSegments "a.b.c" ["b", "c"])), + scope "a.b.c doesn't end with [d]" (expectEqual False (endsWithSegments "a.b.c" ["d"])) + ] + testSegments :: [Test ()] testSegments = [ do diff --git a/unison-core/src/Unison/Name.hs b/unison-core/src/Unison/Name.hs index f2357d095..0fa9ffa92 100644 --- a/unison-core/src/Unison/Name.hs +++ b/unison-core/src/Unison/Name.hs @@ -145,20 +145,21 @@ countSegments :: Name -> Int countSegments (Name _ ss) = length ss --- | @endsWithSegments x y@ returns whether @x@ ends with the segments of relative name @y@. If @y@ is absolute, returns --- @False@. +-- | @endsWithSegments x y@ returns whether @x@ ends with @y@. -- --- >>> endsWithSegments "a.b.c" "b.c" +-- >>> endsWithSegments "a.b.c" ["b", "c"] -- True -- --- >>> endsWithSegments "a.b.c" ".b.c" +-- >>> endsWithSegments "a.b.c" ["d"] -- False -- +-- >>> endsWithSegments "a.b.c" [] +-- True +-- -- /O(n)/, where /n/ is the number of name segments. -endsWithSegments :: Name -> Name -> Bool -endsWithSegments (Name _ ss0) = \case - Name Absolute _ -> False - Name Relative ss1 -> List.NonEmpty.isPrefixOf (toList ss1) ss0 +endsWithSegments :: Name -> [NameSegment] -> Bool +endsWithSegments (Name _ ss0) ss1 = + List.NonEmpty.isPrefixOf ss1 ss0 -- | Is this name absolute? --