diff --git a/src/Data/Abstract/Environment.hs b/src/Data/Abstract/Environment.hs index 77bc0b185..bdd2c4d94 100644 --- a/src/Data/Abstract/Environment.hs +++ b/src/Data/Abstract/Environment.hs @@ -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. --