Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit ba8e03b

Browse files
committed
Derive the HFunctor & Effect instances for Readline.
1 parent 935b73a commit ba8e03b

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

semantic-core/src/Control/Effect/Readline.hs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE DeriveFunctor, ExistentialQuantification, FlexibleContexts, FlexibleInstances, GeneralizedNewtypeDeriving, KindSignatures, MultiParamTypeClasses, OverloadedStrings, RankNTypes, StandaloneDeriving, TypeApplications, TypeOperators, UndecidableInstances #-}
1+
{-# LANGUAGE DeriveAnyClass, DeriveFunctor, DeriveGeneric, DerivingStrategies, FlexibleContexts, FlexibleInstances, GeneralizedNewtypeDeriving, KindSignatures, MultiParamTypeClasses, OverloadedStrings, RankNTypes, StandaloneDeriving, TypeApplications, TypeOperators, UndecidableInstances #-}
22

33
module Control.Effect.Readline
44
( Readline (..)
@@ -27,28 +27,25 @@ import Data.Int
2727
import Data.String
2828
import Data.Text.Prettyprint.Doc
2929
import Data.Text.Prettyprint.Doc.Render.Text
30+
import GHC.Generics (Generic1)
3031
import System.Console.Haskeline hiding (Handler, handle)
3132
import System.Directory
3233
import System.FilePath
3334

3435
data Readline (m :: * -> *) k
3536
= Prompt String (Maybe String -> m k)
36-
| forall a . Print (Doc a) (m k)
37+
| Print AnyDoc (m k)
3738
| AskLine (Line -> m k)
39+
deriving stock (Functor, Generic1)
40+
deriving anyclass (Effect, HFunctor)
3841

39-
deriving instance Functor m => Functor (Readline m)
40-
41-
instance HFunctor Readline where
42-
hmap f (Prompt s k) = Prompt s (f . k)
43-
hmap f (Print d k) = Print d (f k)
44-
hmap f (AskLine k) = AskLine (f . k)
45-
42+
newtype AnyDoc = AnyDoc { unAnyDoc :: forall a . Doc a }
4643

4744
prompt :: (IsString str, Member Readline sig, Carrier sig m) => String -> m (Maybe str)
4845
prompt p = fmap fromString <$> send (Prompt p pure)
4946

5047
print :: (Pretty a, Carrier sig m, Member Readline sig) => a -> m ()
51-
print s = send (Print (pretty s) (pure ()))
48+
print s = send (Print (AnyDoc (pretty s)) (pure ()))
5249

5350
println :: (Pretty a, Carrier sig m, Member Readline sig) => a -> m ()
5451
println s = print s >> print @String "\n"
@@ -62,7 +59,7 @@ increment :: Line -> Line
6259
increment (Line n) = Line (n + 1)
6360

6461
newtype ReadlineC m a = ReadlineC { runReadlineC :: ReaderC Line (TransC InputT m) a }
65-
deriving (Applicative, Functor, Monad, MonadIO)
62+
deriving newtype (Applicative, Functor, Monad, MonadIO)
6663

6764
runReadline :: MonadException m => Prefs -> Settings m -> ReadlineC m a -> m a
6865
runReadline prefs settings = runInputTWithPrefs prefs settings . runTransC . runReader (Line 0) . runReadlineC
@@ -73,7 +70,7 @@ instance (Carrier sig m, Effect sig, MonadException m, MonadIO m) => Carrier (Re
7370
local increment (runReadlineC (k str))
7471
where cyan = "\ESC[1;36m\STX"
7572
plain = "\ESC[0m\STX"
76-
eff (L (Print text k)) = liftIO (putDoc text) *> k
73+
eff (L (Print text k)) = liftIO (putDoc (unAnyDoc text)) *> k
7774
eff (L (AskLine k)) = ReadlineC ask >>= k
7875
eff (R other) = ReadlineC (eff (R (handleCoercible other)))
7976

@@ -93,7 +90,7 @@ runReadlineWithHistory block = do
9390

9491
-- | Promote a monad transformer into an effect.
9592
newtype TransC t (m :: * -> *) a = TransC { runTransC :: t m a }
96-
deriving (Applicative, Functor, Monad, MonadIO, MonadTrans)
93+
deriving newtype (Applicative, Functor, Monad, MonadIO, MonadTrans)
9794

9895
instance (Carrier sig m, Effect sig, Monad (t m), MonadTrans t) => Carrier sig (TransC t m) where
9996
eff = TransC . join . lift . eff . handle (pure ()) (pure . (runTransC =<<))
@@ -103,7 +100,7 @@ runControlIO handler = runReader (Handler handler) . runControlIOC
103100

104101
-- | This exists to work around the 'MonadException' constraint that haskeline entails.
105102
newtype ControlIOC m a = ControlIOC { runControlIOC :: ReaderC (Handler m) m a }
106-
deriving (Applicative, Functor, Monad, MonadIO)
103+
deriving newtype (Applicative, Functor, Monad, MonadIO)
107104

108105
newtype Handler m = Handler (forall x . m x -> IO x)
109106

0 commit comments

Comments
 (0)