@@ -481,7 +481,7 @@ newtype NonEmptyListE (e::E) (n::S) = NonEmptyListE { fromNonEmptyListE :: NonEm
481
481
deriving (Show , Eq , Generic )
482
482
483
483
newtype LiftE (a :: * ) (n :: S ) = LiftE { fromLiftE :: a }
484
- deriving (Show , Eq , Generic , Monoid , Semigroup )
484
+ deriving (Show , Eq , Ord , Generic , Monoid , Semigroup )
485
485
486
486
newtype ComposeE (f :: * -> * ) (e :: E ) (n :: S ) =
487
487
ComposeE { fromComposeE :: (f (e n )) }
@@ -3256,113 +3256,85 @@ instance HoistableB b => HoistableB (WithAttrB a b) where
3256
3256
3257
3257
-- === extra data structures ===
3258
3258
3259
- -- A map from names in some scope to values that do not contain names. This is
3260
- -- not trying to enforce completeness -- a name in the scope can fail to be in
3261
- -- the map.
3262
-
3263
- -- Hoisting the map removes entries that are no longer in scope.
3264
-
3265
- newtype NameMap (c :: C ) (a :: * ) (n :: S ) = UnsafeNameMap (RawNameMap a )
3266
- deriving (Eq , Semigroup , Monoid , Store )
3267
-
3268
- hoistFilterNameMap :: BindsNames b => b n l -> NameMap c a l -> NameMap c a n
3269
- hoistFilterNameMap b (UnsafeNameMap raw) =
3270
- UnsafeNameMap $ raw `R.difference` frag
3271
- where UnsafeMakeScopeFrag frag = toScopeFrag b
3272
- {-# INLINE hoistFilterNameMap #-}
3273
-
3274
- insertNameMap :: Name c n -> a -> NameMap c a n -> NameMap c a n
3275
- insertNameMap (UnsafeMakeName n) x (UnsafeNameMap raw) = UnsafeNameMap $ R. insert n x raw
3276
- {-# INLINE insertNameMap #-}
3277
-
3278
- lookupNameMap :: Name c n -> NameMap c a n -> Maybe a
3279
- lookupNameMap (UnsafeMakeName n) (UnsafeNameMap raw) = R. lookup n raw
3280
- {-# INLINE lookupNameMap #-}
3281
-
3282
- singletonNameMap :: Name c n -> a -> NameMap c a n
3283
- singletonNameMap (UnsafeMakeName n) x = UnsafeNameMap $ R. singleton n x
3284
- {-# INLINE singletonNameMap #-}
3285
-
3286
- toListNameMap :: NameMap c a n -> [(Name c n , a )]
3287
- toListNameMap (UnsafeNameMap raw) = R. toList raw <&> \ (r, x) -> (UnsafeMakeName r, x)
3288
- {-# INLINE toListNameMap #-}
3289
-
3290
- unionWithNameMap :: (a -> a -> a ) -> NameMap c a n -> NameMap c a n -> NameMap c a n
3291
- unionWithNameMap f (UnsafeNameMap raw1) (UnsafeNameMap raw2) =
3292
- UnsafeNameMap $ R. unionWith f raw1 raw2
3293
- {-# INLINE unionWithNameMap #-}
3294
-
3295
- unionsWithNameMap :: (Foldable f ) => (a -> a -> a ) -> f (NameMap c a n ) -> NameMap c a n
3296
- unionsWithNameMap func maps =
3297
- foldl' (unionWithNameMap func) mempty maps
3298
- {-# INLINE unionsWithNameMap #-}
3299
-
3300
- traverseNameMap :: (Applicative f ) => (a -> f b )
3301
- -> NameMap c a n -> f (NameMap c b n )
3302
- traverseNameMap f (UnsafeNameMap raw) = UnsafeNameMap <$> traverse f raw
3303
- {-# INLINE traverseNameMap #-}
3304
-
3305
- mapNameMap :: (a -> b ) -> NameMap c a n -> (NameMap c b n )
3306
- mapNameMap f (UnsafeNameMap raw) = UnsafeNameMap $ fmap f raw
3307
- {-# INLINE mapNameMap #-}
3308
-
3309
- keysNameMap :: NameMap c a n -> [Name c n ]
3310
- keysNameMap = map fst . toListNameMap
3311
- {-# INLINE keysNameMap #-}
3312
-
3313
- keySetNameMap :: (Color c ) => NameMap c a n -> NameSet n
3314
- keySetNameMap nmap = freeVarsE $ ListE $ keysNameMap nmap
3315
-
3316
- instance SinkableE (NameMap c a ) where
3317
- sinkingProofE = undefined
3259
+ -- A map from names in some scope to values that may contain names
3260
+ -- from the same scope. This is not trying to enforce completeness --
3261
+ -- a name in the scope can fail to be in the map.
3262
+
3263
+ -- Hoisting the map removes entries for names that are no longer in
3264
+ -- scope, and then attempts to hoist the remaining values.
3265
+
3266
+ -- This structure is useful for bottom-up code traversals. Once one
3267
+ -- has traversed some term in scope n, one may be carrying information
3268
+ -- associated with (some of) the free variables of the term. These
3269
+ -- free variables are necessarily in the scope n, though they need by
3270
+ -- no means be all the names in the scope n (that's what a Subst is
3271
+ -- for). But, if the traversal is alpha-invariant, it cannot be
3272
+ -- carrying any information about names bound within the term, only
3273
+ -- the free ones.
3274
+ --
3275
+ -- Further, if the information being carried is E-kinded, the names
3276
+ -- therein should be resolvable in the same scope n, since those are
3277
+ -- the only names that are given meaning by the context of the term
3278
+ -- being traversed.
3318
3279
3319
- newtype NameMapE (c :: C ) (e :: E ) (n :: S ) = NameMapE ( NameMap c (e n ) n )
3280
+ newtype NameMapE (c :: C ) (e :: E ) (n :: S ) = UnsafeNameMapE ( RawNameMap (e n ))
3320
3281
deriving (Eq , Semigroup , Monoid , Store )
3321
3282
3322
3283
-- Filters out the entry(ies) for the binder being hoisted above,
3323
3284
-- and hoists the values of the remaining entries.
3324
3285
hoistNameMapE :: (BindsNames b , HoistableE e , ShowE e )
3325
3286
=> b n l -> NameMapE c e l -> HoistExcept (NameMapE c e n )
3326
- hoistNameMapE b (NameMapE nmap) =
3327
- NameMapE <$> (traverseNameMap (hoist b) $ hoistFilterNameMap b nmap) where
3287
+ hoistNameMapE b (UnsafeNameMapE raw) =
3288
+ UnsafeNameMapE <$> traverse (hoist b) diff
3289
+ where
3290
+ diff = raw `R.difference` frag
3291
+ UnsafeMakeScopeFrag frag = toScopeFrag b
3328
3292
{-# INLINE hoistNameMapE #-}
3329
3293
3330
3294
insertNameMapE :: Name c n -> e n -> NameMapE c e n -> NameMapE c e n
3331
- insertNameMapE n x (NameMapE nmap) = NameMapE $ insertNameMap n x nmap
3295
+ insertNameMapE (UnsafeMakeName n) x (UnsafeNameMapE raw)
3296
+ = UnsafeNameMapE $ R. insert n x raw
3332
3297
{-# INLINE insertNameMapE #-}
3333
3298
3334
3299
lookupNameMapE :: Name c n -> NameMapE c e n -> Maybe (e n )
3335
- lookupNameMapE n ( NameMapE nmap ) = lookupNameMap n nmap
3300
+ lookupNameMapE ( UnsafeMakeName n) ( UnsafeNameMapE raw ) = R. lookup n raw
3336
3301
{-# INLINE lookupNameMapE #-}
3337
3302
3338
3303
singletonNameMapE :: Name c n -> e n -> NameMapE c e n
3339
- singletonNameMapE n x = NameMapE $ singletonNameMap n x
3304
+ singletonNameMapE ( UnsafeMakeName n) x = UnsafeNameMapE $ R. singleton n x
3340
3305
{-# INLINE singletonNameMapE #-}
3341
3306
3342
3307
toListNameMapE :: NameMapE c e n -> [(Name c n , (e n ))]
3343
- toListNameMapE (NameMapE nmap) = toListNameMap nmap
3308
+ toListNameMapE (UnsafeNameMapE raw) =
3309
+ R. toList raw <&> \ (r, x) -> (UnsafeMakeName r, x)
3344
3310
{-# INLINE toListNameMapE #-}
3345
3311
3346
3312
unionWithNameMapE :: (e n -> e n -> e n ) -> NameMapE c e n -> NameMapE c e n -> NameMapE c e n
3347
- unionWithNameMapE f (NameMapE nmap1 ) (NameMapE nmap2 ) =
3348
- NameMapE $ unionWithNameMap f nmap1 nmap2
3313
+ unionWithNameMapE f (UnsafeNameMapE raw1 ) (UnsafeNameMapE raw2 ) =
3314
+ UnsafeNameMapE $ R. unionWith f raw1 raw2
3349
3315
{-# INLINE unionWithNameMapE #-}
3350
3316
3317
+ unionsWithNameMapE :: (Foldable f ) => (e n -> e n -> e n ) -> f (NameMapE c e n ) -> NameMapE c e n
3318
+ unionsWithNameMapE func maps =
3319
+ foldl' (unionWithNameMapE func) mempty maps
3320
+ {-# INLINE unionsWithNameMapE #-}
3321
+
3351
3322
traverseNameMapE :: (Applicative f ) => (e1 n -> f (e2 n ))
3352
3323
-> NameMapE c e1 n -> f (NameMapE c e2 n )
3353
- traverseNameMapE f (NameMapE nmap ) = NameMapE <$> traverseNameMap f nmap
3324
+ traverseNameMapE f (UnsafeNameMapE raw ) = UnsafeNameMapE <$> traverse f raw
3354
3325
{-# INLINE traverseNameMapE #-}
3355
3326
3356
3327
mapNameMapE :: (e1 n -> e2 n )
3357
3328
-> NameMapE c e1 n -> NameMapE c e2 n
3358
- mapNameMapE f (NameMapE nmap ) = NameMapE $ mapNameMap f nmap
3329
+ mapNameMapE f (UnsafeNameMapE raw ) = UnsafeNameMapE $ fmap f raw
3359
3330
{-# INLINE mapNameMapE #-}
3360
3331
3361
3332
keysNameMapE :: NameMapE c e n -> [Name c n ]
3362
- keysNameMapE (NameMapE nmap) = keysNameMap nmap
3333
+ keysNameMapE = map fst . toListNameMapE
3334
+ {-# INLINE keysNameMapE #-}
3363
3335
3364
3336
keySetNameMapE :: (Color c ) => NameMapE c e n -> NameSet n
3365
- keySetNameMapE ( NameMapE nmap) = keySetNameMap nmap
3337
+ keySetNameMapE nmap = freeVarsE $ ListE $ keysNameMapE nmap
3366
3338
3367
3339
instance SinkableE e => SinkableE (NameMapE c e ) where
3368
3340
sinkingProofE = undefined
@@ -3373,6 +3345,16 @@ instance RenameE e => RenameE (NameMapE c e) where
3373
3345
instance HoistableE e => HoistableE (NameMapE c e ) where
3374
3346
freeVarsE = undefined
3375
3347
3348
+ -- A small short-cut: When the information in a NameMapE does not, in
3349
+ -- fact, reference any names, hoisting the entries cannot fail.
3350
+
3351
+ type NameMap (c :: C ) (a :: * ) = NameMapE c (LiftE a )
3352
+
3353
+ hoistNameMap :: (BindsNames b , Show a )
3354
+ => b n l -> NameMap c a l -> (NameMap c a n )
3355
+ hoistNameMap b = ignoreHoistFailure . hoistNameMapE b
3356
+ {-# INLINE hoistNameMap #-}
3357
+
3376
3358
-- === E-kinded IR coercions ===
3377
3359
3378
3360
-- XXX: the intention is that we won't have to use this much
0 commit comments