se (IntMap.alterF alter n board)
  where
    -- Pin to pin board at a hash key: either there's nothing there (ifMiss), or there's a nonempty bucket (ifHit).
    alter :: Maybe (Bucket a) -> Compose IO ((,) a) (Maybe (Bucket a))
    alter =
      Compose . maybe ifMiss ifHit
    -- Pin a new value: create a new singleton bucket.
    ifMiss :: IO (a, Maybe (Bucket a))
    ifMiss =
      (x,) . Just <$> newBucket x finalizer
    -- Possibly pin a new value: if it already exists in the bucket, return that one instead. Otherwise, insert it.
    ifHit :: Bucket a -> IO (a, Maybe (Bucket a))
    ifHit bucket =
      bucketFind bucket x >>= \case
        -- Hash collision: the bucket has things in it, but none are the given value. Insert.
        Nothing -> (x,) . Just <$> bucketAdd bucket x finalizer
        -- The thing being inserted already exists; return it.
        Just y -> pure (y, Just bucket)
    -- When each thing pinned here is garbage collected, compact its bucket.
    finalizer :: IO ()
    finalizer =
      modifyMVar_ boardVar (IntMap.alterF (maybe (pure Nothing) bucketCompact) n)
    n :: Int
    n =
      hash x

debugDump :: MonadIO m => (a -> Text) -> PinBoard a -> m ()
debugDump f (PinBoard boardVar) = liftIO do
  board <- readMVar boardVar
  contents <- (traverse . trave
pin :: forall a m. (Eq a, Hashable a, MonadIO m) => PinBoard a -> a -> m a
pin (PinBoard boardVar) x = liftIO do
  modifyMVar boardVar \board ->
    swap <$> getCompose (IntMap.alterF alter n board)
  where
    -- Pin to pin board at a hash key: either there's nothing there (ifMiss), or there's a nonempty bucket (ifHit).
    alter :: Maybe (Bucket a) -> Compose IO ((,) a) (Maybe (Bucket a))
    alter =
      Compose . maybe ifMiss ifHit
    -- Pin a new value: create a new singleton bucket.
    ifMiss :: IO (a, Maybe (Bucket a))
    ifMiss =
      (x,) . Just <$> newBucket x finalizer
    -- Possibly pin a new value: if it already exists in the bucket, return that one instead. Otherwise, insert it.
    ifHit :: Bucket a -> IO (a, Maybe (Bucket a))
    ifHit bucket =
      bucketFind bucket x >>= \case
        -- Hash collision: the bucket has things in it, but none are the given value. Insert.
        Nothing -> (x,) . Just <$> bucketAdd bucket x finalizer
        -- The thing being inserted already exists; return it.
        Just y -> pure (y, Just bucket)
    -- When each thing pinned here is garbage collected, compact its bucket.
    finalizer :: IO ()
    finalizer =
      modifyMVar_ boardVar (IntMap.alterF (maybe (pure Nothing) bucketCompact) n)
    n :: Int
    n =
      hash x
pin (PinBoard boardVar) x = liftIO do
  modifyMVar boardVar \board ->
    swap <$> getCompose (IntMap.alterF alter n board)
  where
    -- Pin to pin board at a hash key: either there's nothing there (ifMiss), or there's a nonempty bucket (ifHit).
    alter :: Maybe (Bucket a) -> Compose IO ((,) a) (Maybe (Bucket a))
    alter =
      Compose . maybe ifMiss ifHit
    -- Pin a new value: create a new singleton bucket.
    ifMiss :: IO (a, Maybe (Bucket a))
    ifMiss =
      (x,) . Just <$> newBucket x finalizer
    -- Possibly pin a new value: if it already exists in the bucket, return that one instead. Otherwise, insert it.
    ifHit :: Bucket a -> IO (a, Maybe (Bucket a))
    ifHit bucket =
      bucketFind bucket x >>= \case
        -- Hash collision: the bucket has things in it, but none are the given value. Insert.
        Nothing -> (x,) . Just <$> bucketAdd bucket x finalizer
        -- The thing being inserted already exists; return it.
        Just y -> pure (y, Just bucket)
    -- When each thing pinned here is garbage collected, compact its bucket.
    finalizer :: IO ()
    finalizer =
      modifyMVar_ boardVar (IntMap.alterF (maybe (pure Nothing) bucketCompact) n)
    n :: Int
    n =
      hash x