1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 06:11:49 +03:00

Revert "Revert "Environments hold locations, not addresses.""

This reverts commit b63e5f0b9c0099d7584d57fd1e1588c282f19043.
This commit is contained in:
Rob Rix 2018-05-11 08:52:19 -04:00
parent 4892f748c6
commit e18827f03f

View File

@ -37,7 +37,7 @@ import qualified Data.List.NonEmpty as NonEmpty
-- | A LIFO stack of maps of names to addresses, representing a lexically-scoped evaluation environment.
-- All behaviors can be assumed to be frontmost-biased: looking up "a" will check the most specific
-- scope for "a", then the next, and so on.
newtype Environment location value = Environment { unEnvironment :: NonEmpty (Map.Map Name (Address location value)) }
newtype Environment location value = Environment { unEnvironment :: NonEmpty (Map.Map Name location) }
deriving (Eq, Generic1, Ord, Show)
instance Eq location => Eq1 (Environment location) where liftEq = genericLiftEq
@ -49,8 +49,8 @@ instance Show location => Show1 (Environment location) where liftShowsPrec = gen
-- same Name or you violate the axiom that toList . fromList == id.
instance IsList (Environment location value) where
type Item (Environment location value) = (Name, Address location value)
fromList xs = Environment (Map.fromList xs :| [])
toList (Environment (x :| _)) = Map.toList x
fromList xs = Environment (Map.fromList (second unAddress <$> xs) :| [])
toList (Environment (x :| _)) = second Address <$> Map.toList x
mergeEnvs :: Environment location value -> Environment location value -> Environment location value
mergeEnvs (Environment (a :| as)) (Environment (b :| bs)) =
@ -87,7 +87,7 @@ mergeNewer (Environment a) (Environment b) =
-- >>> pairs shadowed
-- [(Name {unName = "foo"},Address {unAddress = Precise 1)]
pairs :: Environment location value -> [(Name, Address location value)]
pairs = Map.toList . fold . unEnvironment
pairs = map (second Address) . Map.toList . fold . unEnvironment
unpairs :: [(Name, Address location value)] -> Environment location value
unpairs = fromList
@ -97,11 +97,11 @@ unpairs = fromList
-- >>> lookup (name "foo") shadowed
-- Just (Address {unAddress = Precise 1})
lookup :: Name -> Environment location value -> Maybe (Address location value)
lookup k = foldMapA (Map.lookup k) . unEnvironment
lookup k = fmap Address . foldMapA (Map.lookup k) . unEnvironment
-- | Insert a 'Name' in the environment.
insert :: Name -> Address location value -> Environment location value -> Environment location value
insert name value (Environment (a :| as)) = Environment (Map.insert name value a :| as)
insert name (Address value) (Environment (a :| as)) = Environment (Map.insert name value a :| as)
-- | Remove a 'Name' from the environment.
--