make endsWithSegments less weird

This commit is contained in:
Mitchell Rosen 2021-10-25 17:34:11 -04:00
parent e06427617b
commit 394201e9ae
3 changed files with 20 additions and 14 deletions

View File

@ -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)

View File

@ -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

View File

@ -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?
--