<commit_before>{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ConstraintKinds #-}

-- | The Duplex monad, used for full duplex communication between
-- server and client commands.

module GHC.Server.Duplex where

import GHC.Compat
import GHC.Server.Types

import Control.Concurrent
import Control.Monad.Reader

--------------------------------------------------------------------------------
-- Duplexing monad

-- | Receive an input.
recv :: (MonadDuplex i o m)
     => m i
recv =
  do inp <-  (asks duplexIn)
     io (readChan inp)

-- | Send an output.
send :: (MonadDuplex i o m)
     => o -> m ()
send o =
  do out <-  (asks duplexOut)
     io (writeChan out o)

-- | Run the GHC action in isolation.
liftGhc :: (MonadDuplex i o m)
        => Ghc r -> m r
liftGhc m =
  do ghcChan <- asks duplexRunGhc
     io (do result <- newEmptyMVar
            io (writeChan ghcChan
                          (do v <- m
                              io (putMVar result v)))
            takeMVar result)

-- | Transform over Ghc.
withGhc :: (Inputish i,Outputish o)
        => DuplexT Ghc i o r -> Duplex i o r
withGhc m =
  do st <- DuplexT ask
     ghcChan <- DuplexT (asks duplexRunGhc)
     io (do result <- newEmptyMVar
            io (writeChan ghcChan
                          (do v <- runReaderT (runDuplexT m) st
                              io (putMVar result v)))
            takeMVar result)<commit_msg>Improve Duplex<commit_after>{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ConstraintKinds #-}

-- | The Duplex monad, used for full duplex communication between
-- server and client commands.

module GHC.Server.Duplex where

import GHC.Compat
import GHC.Server.Types

import Control.Concurrent
import Control.Monad.Reader

--------------------------------------------------------------------------------
-- Duplexing monad

-- | Receive an input.
recv :: (MonadDuplex i o m)
     => m i
recv =
  do inp <-  (asks duplexIn)
     io (readChan inp)

-- | Send an output.
send :: (MonadDuplex i o m)
     => o -> m ()
send o =
  do out <-  (asks duplexOut)
     io (writeChan out o)

-- | Run the GHC action in isolation.
liftGhc :: Ghc r -> Duplex i o r
liftGhc m =
  do ghcChan <- asks duplexRunGhc
     io (do result <- newEmptyMVar
            io (writeChan ghcChan
                          (do v <- m
                              io (putMVar result v)))
            takeMVar result)

-- | Transform over Ghc, running the transformed GHC in isolation.
withGhc :: (Inputish i,Outputish o)
        => DuplexT Ghc i o r -> Duplex i o r
withGhc m =
  do st <- DuplexT ask
     ghcChan <- DuplexT (asks duplexRunGhc)
     io (do result <- newEmptyMVar
            io (writeChan ghcChan
                          (do v <- runReaderT (runDuplexT m) st
                              io (putMVar result v)))
            takeMVar result)
{'fraction_non_alphanumeric': 0.14887737478411053, 'fraction_numerical': 0.0, 'mean_word_length': 1.6399270738377394, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7427416', 'n_tokens_mistral': 983, 'n_tokens_neox': 910, 'n_words': 310}